aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2018-02-14 20:18:18 -0500
committerGitHub <noreply@github.com>2018-02-14 20:18:18 -0500
commit20f0d085d6940be30b076a8cff3de25fe4a6e21a (patch)
tree417f2e67afdf53981344c4c3c4ba9f5636d8f8f1 /core
parente9111feb62bf245309d1ffde9f8579195d8f6449 (diff)
downloadkau-20f0d085d6940be30b076a8cff3de25fe4a6e21a.tar.gz
kau-20f0d085d6940be30b076a8cff3de25fe4a6e21a.tar.bz2
kau-20f0d085d6940be30b076a8cff3de25fe4a6e21a.zip
Misc (#137)
* Reorder final override * Update restart methods * Update changelog
Diffstat (limited to 'core')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt33
1 files changed, 28 insertions, 5 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt
index fb88e61..833ec3a 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt
@@ -2,11 +2,15 @@ package ca.allanwang.kau.utils
import android.annotation.SuppressLint
import android.app.Activity
+import android.app.AlarmManager
+import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.Color
+import android.os.Build
import android.os.Bundle
import android.support.annotation.ColorInt
+import android.support.annotation.RequiresApi
import android.support.annotation.StringRes
import android.support.design.widget.Snackbar
import android.view.Menu
@@ -56,7 +60,9 @@ inline fun <T : Activity> Activity.startActivityForResult(
*/
inline fun Activity.restart(intentBuilder: Intent.() -> Unit = {}) {
val i = Intent(this, this::class.java)
- i.putExtras(intent.extras)
+ val oldExtras = intent.extras
+ if (oldExtras != null)
+ i.putExtras(oldExtras)
i.intentBuilder()
startActivity(i)
overridePendingTransition(R.anim.kau_fade_in, R.anim.kau_fade_out) //No transitions
@@ -64,6 +70,25 @@ inline fun Activity.restart(intentBuilder: Intent.() -> Unit = {}) {
overridePendingTransition(R.anim.kau_fade_in, R.anim.kau_fade_out)
}
+
+/**
+ * Force restart an entire application
+ */
+@Suppress("NOTHING_TO_INLINE")
+@RequiresApi(Build.VERSION_CODES.M)
+inline fun Activity.restartApplication() {
+ val intent = packageManager.getLaunchIntentForPackage(packageName)
+ intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
+ val pending = PendingIntent.getActivity(this, 666, intent, PendingIntent.FLAG_CANCEL_CURRENT)
+ val alarm = getSystemService(Context.ALARM_SERVICE) as AlarmManager
+ if (buildIsMarshmallowAndUp)
+ alarm.setExactAndAllowWhileIdle(AlarmManager.RTC, System.currentTimeMillis() + 100, pending)
+ else
+ alarm.setExact(AlarmManager.RTC, System.currentTimeMillis() + 100, pending)
+ finish()
+ System.exit(0)
+}
+
fun Activity.finishSlideOut() {
finish()
overridePendingTransition(R.anim.kau_fade_in, R.anim.kau_slide_out_right_top)
@@ -113,8 +138,6 @@ fun Activity.hideKeyboard() = currentFocus.hideKeyboard()
fun Activity.showKeyboard() = currentFocus.showKeyboard()
-fun Activity.snackbar(text: String, duration: Int = Snackbar.LENGTH_LONG, builder: Snackbar.() -> Unit = {})
- = contentView!!.snackbar(text, duration, builder)
+fun Activity.snackbar(text: String, duration: Int = Snackbar.LENGTH_LONG, builder: Snackbar.() -> Unit = {}) = contentView!!.snackbar(text, duration, builder)
-fun Activity.snackbar(@StringRes textId: Int, duration: Int = Snackbar.LENGTH_LONG, builder: Snackbar.() -> Unit = {})
- = contentView!!.snackbar(textId, duration, builder) \ No newline at end of file
+fun Activity.snackbar(@StringRes textId: Int, duration: Int = Snackbar.LENGTH_LONG, builder: Snackbar.() -> Unit = {}) = contentView!!.snackbar(textId, duration, builder) \ No newline at end of file