aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
blob: 7d4a934d645cbb4e0da596623f47befdb219f578 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * Copyright 2017 Allan Wang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
@file:Suppress("NOTHING_TO_INLINE")

package ca.allanwang.kau.utils

import android.app.Activity
import android.content.*
import android.content.pm.PackageManager
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Bundle
import android.os.Looper
import android.util.TypedValue
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.Toast
import androidx.annotation.*
import androidx.core.content.ContextCompat
import ca.allanwang.kau.BuildConfig
import ca.allanwang.kau.R
import ca.allanwang.kau.logging.KL
import com.afollestad.materialdialogs.DialogBehavior
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.ModalDialog

/**
 * Created by Allan Wang on 2017-06-03.
 */
fun Context.runOnUiThread(f: Context.() -> Unit) {
    if (ContextHelper.looper === Looper.myLooper()) f() else ContextHelper.handler.post { f() }
}

/**
 * Helper class to launch an activity from a context
 * Counterpart of [Context.startActivity]
 * For starting activities for results, see [startActivityForResult]
 */
@Suppress("DEPRECATION")
inline fun <reified T : Activity> Context.startActivity(
    clearStack: Boolean = false,
    bundleBuilder: Bundle.() -> Unit = {},
    intentBuilder: Intent.() -> Unit = {}
) = startActivity(T::class.java, clearStack, bundleBuilder, intentBuilder)

@Deprecated(
        "Use reified generic instead of passing class",
        ReplaceWith("startActivity<T>(clearStack, bundleBuilder, intentBuilder)"),
        DeprecationLevel.WARNING
)
inline fun <T : Activity> Context.startActivity(
    clazz: Class<T>,
    clearStack: Boolean = false,
    bundleBuilder: Bundle.() -> Unit = {},
    intentBuilder: Intent.() -> Unit = {}
) {
    val intent = Intent(this, clazz)
    if (clearStack) {
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
    }
    intent.intentBuilder()
    val bundle = Bundle()
    bundle.bundleBuilder()
    startActivity(intent, bundle.takeIf { !it.isEmpty })
    if (clearStack && this is Activity) {
        finish()
    }
}

fun Context.startPlayStoreLink(@StringRes packageIdRes: Int): Boolean = startPlayStoreLink(string(packageIdRes))

/**
 * Open play store link for [packageId].
 *
 * Returns [true] if intent succeeded.
 */
fun Context.startPlayStoreLink(packageId: String): Boolean {
    val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$packageId"))
    return try {
        startActivity(intent)
        true
    } catch (e: ActivityNotFoundException) {
        if (BuildConfig.DEBUG) toast("Cannot resolve play store", log = true)
        false
    }
}

/**
 * Starts a url.
 *
 * If given a series of links, will open the first one that isn't null.
 * Returns [true] if link was opened.
 */
fun Context.startLink(vararg url: String?): Boolean {
    val link = url.firstOrNull { !it.isNullOrBlank() } ?: return false
    val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(link))
    return try {
        startActivity(browserIntent)
        true
    } catch (e: ActivityNotFoundException) {
        if (BuildConfig.DEBUG) toast("Cannot resolve browser", log = true)
        false
    }
}

fun Context.startLink(@StringRes url: Int): Boolean = startLink(string(url))

// Toast helpers
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 View.toast(text: String, duration: Int = Toast.LENGTH_LONG, log: Boolean = false) =
        context.toast(text, duration, log)

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" }
}

const val INVALID_ID = 0

// Resource retrievers
inline fun Context.string(@StringRes id: Int): String = getString(id)

inline fun Context.string(@StringRes id: Int, fallback: String?): String? =
        if (id != INVALID_ID) string(id) else fallback

inline fun Context.string(@StringRes id: Int, fallback: () -> String?): String? =
        if (id != INVALID_ID) string(id) else fallback()

inline fun Context.color(@ColorRes id: Int): Int = ContextCompat.getColor(this, id)
inline fun Context.boolean(@BoolRes id: Int): Boolean = resources.getBoolean(id)
inline fun Context.integer(@IntegerRes id: Int): Int = resources.getInteger(id)
inline fun Context.dimen(@DimenRes id: Int): Float = resources.getDimension(id)
inline fun Context.dimenPixelSize(@DimenRes id: Int): Int = resources.getDimensionPixelSize(id)
inline fun Context.drawable(@DrawableRes id: Int): Drawable = ContextCompat.getDrawable(this, id)
        ?: throw KauException("Drawable with id $id not found")

inline fun Context.drawable(@DrawableRes id: Int, fallback: Drawable?): Drawable? =
        if (id != INVALID_ID) drawable(id) else fallback

inline fun Context.drawable(@DrawableRes id: Int, fallback: () -> Drawable?): Drawable? =
        if (id != INVALID_ID) drawable(id) else fallback()

inline fun Context.interpolator(@InterpolatorRes id: Int) = AnimationUtils.loadInterpolator(this, id)!!
inline fun Context.animation(@AnimRes id: Int) = AnimationUtils.loadAnimation(this, id)!!

/**
 * Returns plural form of res. The quantity is also passed to the formatter as an int
 */
inline fun Context.plural(@PluralsRes id: Int, quantity: Number) =
        resources.getQuantityString(id, quantity.toInt(), quantity.toInt())

// Attr retrievers
fun Context.resolveColor(@AttrRes attr: Int, @ColorInt fallback: Int = 0): Int {
    val a = theme.obtainStyledAttributes(intArrayOf(attr))
    try {
        return a.getColor(0, fallback)
    } finally {
        a.recycle()
    }
}

fun Context.resolveDrawable(@AttrRes attr: Int): Drawable? {
    val a = theme.obtainStyledAttributes(intArrayOf(attr))
    try {
        return a.getDrawable(0)
    } finally {
        a.recycle()
    }
}

fun Context.resolveBoolean(@AttrRes attr: Int, fallback: Boolean = false): Boolean {
    val a = theme.obtainStyledAttributes(intArrayOf(attr))
    try {
        return a.getBoolean(0, fallback)
    } finally {
        a.recycle()
    }
}

fun Context.resolveString(@AttrRes attr: Int, fallback: String = ""): String {
    val v = TypedValue()
    return if (theme.resolveAttribute(attr, v, true)) v.string.toString() else fallback
}

/**
 * Wrapper function for MaterialDialog
 *
 * Mainly handles invalid creations, such as showing a dialog when an activity is finishing
 * See https://github.com/afollestad/material-dialogs/issues/1778
 */
inline fun Context.materialDialog(
    dialogBehavior: DialogBehavior = ModalDialog,
    action: MaterialDialog.() -> Unit
): MaterialDialog {
    val dialog = MaterialDialog(this, dialogBehavior)
    dialog.action()
    if (isFinishing) {
        KL.d { "Material Dialog triggered from finishing context; did not show" }
    } else {
        dialog.show()
    }
    return dialog
}

fun Context.getDip(value: Float): Float =
        TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, resources.displayMetrics)

inline val Context.isRtl: Boolean
    get() = resources.configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL

/**
 * Determine if the navigation bar will be on the bottom of the screen, based on logic in
 * PhoneWindowManager.
 */
inline val Context.isNavBarOnBottom: Boolean
    get() {
        val cfg = resources.configuration
        val dm = resources.displayMetrics
        val canMove = dm.widthPixels != dm.heightPixels && cfg.smallestScreenWidthDp < 600
        return !canMove || dm.widthPixels < dm.heightPixels
    }

fun Context.hasPermission(permissions: String) = !buildIsMarshmallowAndUp || ContextCompat.checkSelfPermission(
        this,
        permissions
) == PackageManager.PERMISSION_GRANTED

fun Context.copyToClipboard(text: String?, label: String = "Copied Text", showToast: Boolean = true) {
    val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    clipboard.setPrimaryClip(ClipData.newPlainText(label, text ?: ""))
    if (showToast) {
        toast(R.string.kau_text_copied)
    }
}

/**
 * Share text to external activity.
 *
 * Returns [true] if intent succeeds. Return [false] otherwise, or if text is empty.
 */
fun Context.shareText(text: String?): Boolean {
    if (text.isNullOrBlank()) {
        if (BuildConfig.DEBUG) toast("Share text is null")
        return false
    }
    val intent = Intent(Intent.ACTION_SEND)
    intent.type = "text/plain"
    intent.putExtra(Intent.EXTRA_TEXT, text)
    val chooserIntent = Intent.createChooser(intent, string(R.string.kau_share))
    return try {
        startActivity(chooserIntent)
        true
    } catch (e: ActivityNotFoundException) {
        if (BuildConfig.DEBUG) toast("Cannot resolve activity to share text", log = true)
        false
    }
}

/**
 * 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