From 66e7d9505e0baffaf17877c1800939a2e0b936d6 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sat, 23 Sep 2017 15:14:12 -0400 Subject: v3.4.1 (#63) * Check browser intent before launching (#54) * Update changelog * fix/misc (#55) * Add kapt plugin * Fix kau vector * Debug lintRelease * Revert debug * Update dependencies * Check context finishing state before showing dialog (#61) * Keep copy of shared pref rather than application context (#60) * Keep copy of shared pref rather than application context * Add back preference name * Add resolver checks (#62) Squashed commit of the following: commit 7fe57d4ab1dbfe8bfb4d4a15bd0fbf636da491fa Author: Allan Wang Date: Sat Sep 23 15:25:18 2017 -0400 Add missing quote commit ffc3ac99248c2250a7f14ef709f37d787cbe0d83 Author: Allan Wang Date: Sat Sep 23 15:20:54 2017 -0400 Update changelog Update gradle Update version name --- .../kotlin/ca/allanwang/kau/email/EmailBuilder.kt | 6 ++- .../main/kotlin/ca/allanwang/kau/kpref/KPref.kt | 17 +++----- .../kotlin/ca/allanwang/kau/utils/ContextUtils.kt | 48 ++++++++++++++++------ 3 files changed, 45 insertions(+), 26 deletions(-) (limited to 'core/src/main/kotlin') diff --git a/core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt b/core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt index 88a0945..8c6acff 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt @@ -13,6 +13,7 @@ import ca.allanwang.kau.logging.KL import ca.allanwang.kau.utils.installerPackageName import ca.allanwang.kau.utils.isAppInstalled import ca.allanwang.kau.utils.string +import ca.allanwang.kau.utils.toast /** @@ -76,7 +77,10 @@ class EmailBuilder(val email: String, val subject: String) { emailBuilder.append("\n").append(footer) intent.putExtra(Intent.EXTRA_TEXT, emailBuilder.toString()) - context.startActivity(Intent.createChooser(intent, context.resources.getString(R.string.kau_send_via))) + if (intent.resolveActivity(context.packageManager) != null) + context.startActivity(Intent.createChooser(intent, context.resources.getString(R.string.kau_send_via))) + else + context.toast("Cannot resolve email activity", log = true) } } diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt index be16c7c..fbb8c7d 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt @@ -3,6 +3,7 @@ package ca.allanwang.kau.kpref import android.content.Context import android.content.SharedPreferences import ca.allanwang.kau.kotlin.ILazyResettable +import ca.allanwang.kau.logging.KL /** * Created by Allan Wang on 2017-06-07. @@ -22,15 +23,13 @@ import ca.allanwang.kau.kotlin.ILazyResettable */ open class KPref { - lateinit private var c: Context - lateinit internal var PREFERENCE_NAME: String - private var initialized = false + lateinit var PREFERENCE_NAME: String + lateinit var sp: SharedPreferences fun initialize(c: Context, preferenceName: String) { - if (initialized) throw KPrefException("KPref object $preferenceName has already been initialized; please only do so once") - initialized = true - this.c = c.applicationContext PREFERENCE_NAME = preferenceName + sp = c.applicationContext.getSharedPreferences(preferenceName, Context.MODE_PRIVATE) + KL.d("Shared Preference $preferenceName has been initialized") val toDelete = deleteKeys() if (toDelete.isNotEmpty()) { val edit = sp.edit() @@ -39,12 +38,6 @@ open class KPref { } } - //todo hide this - val sp: SharedPreferences by lazy { - if (!initialized) throw KPrefException("KPref object has not yet been initialized; please initialize it with a context and preference name") - c.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE) - } - internal val prefMap: MutableMap> = mutableMapOf() fun reset() { diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt index d561c8a..a72c7dd 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt @@ -77,7 +77,11 @@ fun Context.startActivitySlideOut(clazz: Class, clearStack: Boolea fun Context.startPlayStoreLink(@StringRes packageIdRes: Int) = startPlayStoreLink(string(packageIdRes)) fun Context.startPlayStoreLink(packageId: String) { - startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$packageId"))) + val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$packageId")) + if (intent.resolveActivity(packageManager) != null) + startActivity(intent) + else + toast("Cannot resolve play store", log = true) } /** @@ -87,22 +91,24 @@ fun Context.startPlayStoreLink(packageId: String) { fun Context.startLink(vararg url: String?) { val link = url.firstOrNull { !it.isNullOrBlank() } ?: return val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(link)) - startActivity(browserIntent) + if (browserIntent.resolveActivity(packageManager) != null) + startActivity(browserIntent) + else + toast("Cannot resolve browser", log = true) } -fun Context.startLink(@StringRes url: Int) { - val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(string(url))) - startActivity(browserIntent) -} +fun Context.startLink(@StringRes url: Int) = startLink(string(url)) //Toast helpers -inline fun View.toast(@StringRes id: Int, duration: Int = Toast.LENGTH_LONG) = context.toast(id, duration) +inline fun View.toast(@StringRes id: Int, duration: Int = Toast.LENGTH_LONG, log: Boolean = false) = context.toast(id, duration, log) + +inline fun Context.toast(@StringRes id: Int, duration: Int = Toast.LENGTH_LONG, log: Boolean = false) = toast(this.string(id), duration, log) -inline fun Context.toast(@StringRes id: Int, duration: Int = Toast.LENGTH_LONG) = toast(this.string(id), duration) +inline fun View.toast(text: String, duration: Int = Toast.LENGTH_LONG, log: Boolean = false) = context.toast(text, duration, log) -inline fun View.toast(text: String, duration: Int = Toast.LENGTH_LONG) = context.toast(text, duration) -inline fun Context.toast(text: String, duration: Int = Toast.LENGTH_LONG) { +inline fun Context.toast(text: String, duration: Int = Toast.LENGTH_LONG, log: Boolean = false) { Toast.makeText(this, text, duration).show() + if (log) KL.i("Toast: $text") } //Resource retrievers @@ -163,6 +169,10 @@ fun Context.resolveString(@AttrRes attr: Int, fallback: String = ""): String { inline fun Context.materialDialog(action: MaterialDialog.Builder.() -> Unit): MaterialDialog { val builder = MaterialDialog.Builder(this) builder.action() + if (isFinishing) { + KL.d("Material Dialog triggered from finishing context; did not show") + return builder.build() + } return builder.show() } @@ -192,9 +202,21 @@ fun Context.copyToClipboard(text: String?, label: String = "Copied Text", showTo } fun Context.shareText(text: String?) { - if (text == null) return toast(R.string.kau_text_is_null) + if (text == null) return toast("Share text is null") val intent = Intent(Intent.ACTION_SEND) intent.type = "text/plain" intent.putExtra(Intent.EXTRA_TEXT, text) - startActivity(Intent.createChooser(intent, string(R.string.kau_share))) -} \ No newline at end of file + val chooserIntent = Intent.createChooser(intent, string(R.string.kau_share)) + if (chooserIntent.resolveActivity(packageManager) != null) + startActivity(chooserIntent) + else + toast("Cannot resolve activity to share text", log = true) +} + +/** + * Check if given context is finishing. + * This is a wrapper to check if it's both an activity and finishing + * As of now, it is only checked when tied to an activity + */ +inline val Context.isFinishing: Boolean + get() = (this as? Activity)?.isFinishing ?: false \ No newline at end of file -- cgit v1.2.3