From 631d55af858e8235c2c227a6ab55bab4b6708d07 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Fri, 28 May 2021 08:49:41 +0900 Subject: [PATCH] Convert InstaGrabberApplication to kotlin --- .../instagrabber/InstaGrabberApplication.kt | 145 ++++++++---------- .../awais/instagrabber/utils/TextUtils.kt | 2 +- .../java/awais/instagrabber/utils/Utils.java | 2 - 3 files changed, 65 insertions(+), 84 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/InstaGrabberApplication.kt b/app/src/main/java/awais/instagrabber/InstaGrabberApplication.kt index b4ec56f3..cf6c5f03 100644 --- a/app/src/main/java/awais/instagrabber/InstaGrabberApplication.kt +++ b/app/src/main/java/awais/instagrabber/InstaGrabberApplication.kt @@ -1,89 +1,72 @@ -package awais.instagrabber; +package awais.instagrabber -import android.app.Application; -import android.content.ClipboardManager; -import android.content.Context; -import android.os.Handler; -import android.util.Log; +import android.app.Application +import android.content.ClipboardManager +import android.util.Log +import awais.instagrabber.fragments.settings.PreferenceKeys.CUSTOM_DATE_TIME_FORMAT +import awais.instagrabber.fragments.settings.PreferenceKeys.CUSTOM_DATE_TIME_FORMAT_ENABLED +import awais.instagrabber.fragments.settings.PreferenceKeys.DATE_TIME_FORMAT +import awais.instagrabber.utils.* +import awais.instagrabber.utils.LocaleUtils.currentLocale +import awais.instagrabber.utils.Utils.settingsHelper +import awais.instagrabber.utils.extensions.TAG +import awaisomereport.CrashReporter +import com.facebook.drawee.backends.pipeline.Fresco +import com.facebook.imagepipeline.core.ImagePipelineConfig +import java.net.CookieHandler +import java.time.format.DateTimeFormatter +import java.util.* -import com.facebook.drawee.backends.pipeline.Fresco; -import com.facebook.imagepipeline.core.ImagePipelineConfig; -import java.net.CookieHandler; -import java.time.format.DateTimeFormatter; -import java.util.UUID; - -import awais.instagrabber.fragments.settings.PreferenceKeys; -import awais.instagrabber.utils.Constants; -import awais.instagrabber.utils.LocaleUtils; -import awais.instagrabber.utils.SettingsHelper; -import awais.instagrabber.utils.TextUtils; -import awaisomereport.CrashReporter; - -import static awais.instagrabber.utils.CookieUtils.NET_COOKIE_MANAGER; -import static awais.instagrabber.utils.Utils.applicationHandler; -import static awais.instagrabber.utils.Utils.cacheDir; -import static awais.instagrabber.utils.Utils.clipboardManager; -import static awais.instagrabber.utils.Utils.settingsHelper; - -public final class InstaGrabberApplication extends Application { - private static final String TAG = "InstaGrabberApplication"; - - @Override - public void onCreate() { - super.onCreate(); - CookieHandler.setDefault(NET_COOKIE_MANAGER); - - if (settingsHelper == null) { - settingsHelper = new SettingsHelper(this); +@Suppress("unused") +class InstaGrabberApplication : Application() { + override fun onCreate() { + super.onCreate() + CookieHandler.setDefault(NET_COOKIE_MANAGER) + setupCrashReporter() + setupCloseGuard() + setupFresco() + Utils.cacheDir = cacheDir.absolutePath + settingsHelper = SettingsHelper(this) + Utils.clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager + LocaleUtils.setLocale(baseContext) + val pattern = if (settingsHelper.getBoolean(CUSTOM_DATE_TIME_FORMAT_ENABLED)) { + settingsHelper.getString(CUSTOM_DATE_TIME_FORMAT) + } else { + settingsHelper.getString(DATE_TIME_FORMAT) } - - if (!BuildConfig.DEBUG) { - CrashReporter.get(this).start(); - } - // logCollector = new LogCollector(this); - - if (BuildConfig.DEBUG) { - try { - Class.forName("dalvik.system.CloseGuard") - .getMethod("setEnabled", boolean.class) - .invoke(null, true); - } catch (Exception e) { - Log.e(TAG, "Error", e); - } - } - - // final Set requestListeners = new HashSet<>(); - // requestListeners.add(new RequestLoggingListener()); - final ImagePipelineConfig imagePipelineConfig = ImagePipelineConfig - .newBuilder(this) - // .setMainDiskCacheConfig(diskCacheConfig) - // .setRequestListeners(requestListeners) - .setDownsampleEnabled(true) - .build(); - Fresco.initialize(this, imagePipelineConfig); - // FLog.setMinimumLoggingLevel(FLog.VERBOSE); - - if (applicationHandler == null) { - applicationHandler = new Handler(getApplicationContext().getMainLooper()); - } - - if (cacheDir == null) { - cacheDir = getCacheDir().getAbsolutePath(); - } - - LocaleUtils.setLocale(getBaseContext()); - - if (clipboardManager == null) - clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); - - TextUtils.setFormatter(DateTimeFormatter.ofPattern( - settingsHelper.getBoolean(PreferenceKeys.CUSTOM_DATE_TIME_FORMAT_ENABLED) ? - settingsHelper.getString(PreferenceKeys.CUSTOM_DATE_TIME_FORMAT) : - settingsHelper.getString(PreferenceKeys.DATE_TIME_FORMAT), LocaleUtils.getCurrentLocale())); - + TextUtils.setFormatter(DateTimeFormatter.ofPattern(pattern, currentLocale)) if (TextUtils.isEmpty(settingsHelper.getString(Constants.DEVICE_UUID))) { - settingsHelper.putString(Constants.DEVICE_UUID, UUID.randomUUID().toString()); + settingsHelper.putString(Constants.DEVICE_UUID, UUID.randomUUID().toString()) } } + + private fun setupCrashReporter() { + if (BuildConfig.DEBUG) return + CrashReporter.get(this).start() + // logCollector = new LogCollector(this); + } + + private fun setupCloseGuard() { + if (!BuildConfig.DEBUG) return + try { + Class.forName("dalvik.system.CloseGuard") + .getMethod("setEnabled", Boolean::class.javaPrimitiveType) + .invoke(null, true) + } catch (e: Exception) { + Log.e(TAG, "Error", e) + } + } + + private fun setupFresco() { + // final Set requestListeners = new HashSet<>(); + // requestListeners.add(new RequestLoggingListener()); + val imagePipelineConfig = ImagePipelineConfig + .newBuilder(this) // .setMainDiskCacheConfig(diskCacheConfig) + // .setRequestListeners(requestListeners) + .setDownsampleEnabled(true) + .build() + Fresco.initialize(this, imagePipelineConfig) + // FLog.setMinimumLoggingLevel(FLog.VERBOSE); + } } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/utils/TextUtils.kt b/app/src/main/java/awais/instagrabber/utils/TextUtils.kt index 8da94d3e..b2a28f75 100644 --- a/app/src/main/java/awais/instagrabber/utils/TextUtils.kt +++ b/app/src/main/java/awais/instagrabber/utils/TextUtils.kt @@ -11,7 +11,7 @@ import java.util.* import kotlin.math.absoluteValue object TextUtils { - var datetimeParser: DateTimeFormatter = DateTimeFormatter.ofPattern("") + lateinit var datetimeParser: DateTimeFormatter @JvmStatic fun isEmpty(charSequence: CharSequence?): Boolean { diff --git a/app/src/main/java/awais/instagrabber/utils/Utils.java b/app/src/main/java/awais/instagrabber/utils/Utils.java index e0db3068..b64cc0d6 100644 --- a/app/src/main/java/awais/instagrabber/utils/Utils.java +++ b/app/src/main/java/awais/instagrabber/utils/Utils.java @@ -17,7 +17,6 @@ import android.media.MediaScannerConnection; import android.media.MediaScannerConnection.OnScanCompletedListener; import android.net.Uri; import android.os.Build; -import android.os.Handler; import android.provider.Browser; import android.util.DisplayMetrics; import android.util.Log; @@ -80,7 +79,6 @@ public final class Utils { public static SimpleCache simpleCache; private static int statusBarHeight; private static int actionBarHeight; - public static Handler applicationHandler; public static String cacheDir; public static String tabOrderString; private static int defaultStatusBarColor;