diff options
Diffstat (limited to 'core/src/main')
6 files changed, 78 insertions, 10 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/internal/KauBaseActivity.kt b/core/src/main/kotlin/ca/allanwang/kau/internal/KauBaseActivity.kt new file mode 100644 index 0000000..87d94ce --- /dev/null +++ b/core/src/main/kotlin/ca/allanwang/kau/internal/KauBaseActivity.kt @@ -0,0 +1,21 @@ +package ca.allanwang.kau.internal + +import android.support.v7.app.AppCompatActivity +import ca.allanwang.kau.permissions.kauOnRequestPermissionsResult + +/** + * Created by Allan Wang on 2017-08-01. + * + * Base activity for any activity that would have extended [AppCompatActivity] + * + * Ensures that some singleton methods are called. + * This is simply a convenience class; + * you can always copy and paste this to your own class. + */ +abstract class KauBaseActivity : AppCompatActivity() { + + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + kauOnRequestPermissionsResult(permissions, grantResults) + } +}
\ No newline at end of file diff --git a/core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyResettable.kt b/core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyResettable.kt index 701cb07..2ac5d2f 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyResettable.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyResettable.kt @@ -50,7 +50,7 @@ open class LazyResettable<T : Any>(private val initializer: () -> T, lock: Any? } } -interface ILazyResettable<T> : Lazy<T> { +interface ILazyResettable<out T> : Lazy<T> { fun invalidate() } 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 c1ce282..be16c7c 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt @@ -39,7 +39,8 @@ open class KPref { } } - internal val sp: SharedPreferences by lazy { + //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) } diff --git a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt index d6e17db..0c42574 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt @@ -2,13 +2,17 @@ package ca.allanwang.kau.permissions import android.app.Activity import android.content.Context +import android.content.pm.PackageManager import android.support.v4.app.ActivityCompat +import ca.allanwang.kau.kotlin.lazyContext import ca.allanwang.kau.logging.KL import ca.allanwang.kau.utils.KauException import ca.allanwang.kau.utils.buildIsMarshmallowAndUp import ca.allanwang.kau.utils.hasPermission +import ca.allanwang.kau.utils.toast import java.lang.ref.WeakReference + /** * Created by Allan Wang on 2017-07-03. */ @@ -17,6 +21,17 @@ internal object PermissionManager { var requestInProgress = false val pendingResults: MutableList<WeakReference<PermissionResult>> by lazy { mutableListOf<WeakReference<PermissionResult>>() } + /** + * Retrieve permissions requested in our manifest + */ + val manifestPermission = lazyContext<Array<String>> { + try { + it.packageManager.getPackageInfo(it.packageName, PackageManager.GET_PERMISSIONS)?.requestedPermissions ?: emptyArray() + } catch (e: Exception) { + emptyArray() + } + } + operator fun invoke(context: Context, permissions: Array<out String>, callback: (granted: Boolean, deniedPerm: String?) -> Unit) { KL.d("Permission manager for: ${permissions.contentToString()}") if (!buildIsMarshmallowAndUp) return callback(true, null) @@ -30,6 +45,13 @@ internal object PermissionManager { } @Synchronized internal fun requestPermissions(context: Context, permissions: Array<out String>) { + permissions.forEach { + if (!manifestPermission(context).contains(it)) { + KL.e("Requested permission $it is not stated in the manifest") + context.toast("$it is not in the manifest") + //we'll let the request pass through so it can be denied and so the callback can be triggered + } + } val activity = (context as? Activity) ?: throw KauException("Context is not an instance of an activity; cannot request permissions") KL.d("Requesting permissions ${permissions.contentToString()}") ActivityCompat.requestPermissions(activity, permissions, 1) diff --git a/core/src/main/kotlin/ca/allanwang/kau/ui/views/MeasureSpecDelegate.kt b/core/src/main/kotlin/ca/allanwang/kau/ui/views/MeasureSpecDelegate.kt index edc1536..716fd71 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/ui/views/MeasureSpecDelegate.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/ui/views/MeasureSpecDelegate.kt @@ -11,6 +11,8 @@ import ca.allanwang.kau.utils.parentViewGroup * Created by Allan Wang on 2017-07-14. * * Handles relative sizes for any view + * You may delegate all methods to [MeasureSpecDelegate] + * and call the two methods: [initAttrs] and [onMeasure] */ interface MeasureSpecContract { @@ -53,6 +55,16 @@ interface MeasureSpecContract { * Calculates the final measure specs * Call this from [View.onMeasure] and send the Pair result as the specs * The pair is of the format (width, height) + * + * Example: + * <pre> + * {@code + * override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + * val result = onMeasure(this, widthMeasureSpec, heightMeasureSpec) + * super.onMeasure(result.first, result.second) + * } + * } + * </pre> */ fun onMeasure(view: View, widthMeasureSpec: Int, heightMeasureSpec: Int): Pair<Int, Int> } diff --git a/core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt b/core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt index dedfbbf..b39540c 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt @@ -22,16 +22,28 @@ fun Context.kauParseFaq(@XmlRes xmlRes: Int, withNumbering: Boolean = true): Lis parser: XmlResourceParser -> var eventType = parser.eventType var question: Spanned? = null + var flag = -1 //-1, 0, 1 -> invalid, question, answer while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_TAG) { - if (parser.name == "question") { - var q = parser.text.replace("\n", "<br/>") - if (withNumbering) q = "${items.size + 1}. $q" - question = Html.fromHtml(q) - } else if (parser.name == "answer") { - items.add(Pair(question ?: throw IllegalArgumentException("KAU FAQ answer found without a question"), - Html.fromHtml(parser.text.replace("\n", "<br/>")))) - question = null + flag = when (parser.name) { + "question" -> 0 + "answer" -> 1 + else -> -1 + } + } else if (eventType == XmlPullParser.TEXT) { + when (flag) { + 0 -> { + var q = parser.text.replace("\n", "<br/>") + if (withNumbering) q = "${items.size + 1}. $q" + question = Html.fromHtml(q) + flag = -1 + } + 1 -> { + items.add(Pair(question ?: throw IllegalArgumentException("KAU FAQ answer found without a question"), + Html.fromHtml(parser.text.replace("\n", "<br/>")))) + question = null + flag = -1 + } } } |