From e916afddb89550d7f99f17e1477ad17f3bd770f9 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 20 Jun 2017 15:50:08 -0700 Subject: Add user switching for notifications --- app/build.gradle | 3 +++ app/src/main/AndroidManifest.xml | 12 ++++----- .../com/pitchedapps/frost/FrostWebActivity.kt | 18 ++++++-------- .../kotlin/com/pitchedapps/frost/MainActivity.kt | 10 ++++++-- .../com/pitchedapps/frost/SettingsActivity.kt | 1 - .../kotlin/com/pitchedapps/frost/StartActivity.kt | 21 ++++++++-------- .../com/pitchedapps/frost/WebOverlayActivity.kt | 29 +++++++++++++++++----- .../com/pitchedapps/frost/facebook/FbCookie.kt | 14 +++++++++++ .../frost/services/NotificationExtensions.kt | 13 ---------- .../frost/services/NotificationService.kt | 2 ++ .../kotlin/com/pitchedapps/frost/utils/Prefs.kt | 2 ++ .../kotlin/com/pitchedapps/frost/utils/Utils.kt | 2 +- app/src/main/res/values/dimens.xml | 1 - app/src/main/res/values/strings.xml | 1 - app/src/main/res/values/styles.xml | 2 ++ app/src/main/res/xml/changelog.xml | 5 +++- 16 files changed, 83 insertions(+), 53 deletions(-) delete mode 100644 app/src/main/kotlin/com/pitchedapps/frost/services/NotificationExtensions.kt (limited to 'app') diff --git a/app/build.gradle b/app/build.gradle index 7c6956b0..39bcab46 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -39,6 +39,7 @@ android { applicationIdSuffix ".debug" versionNameSuffix "-debug" resValue "string", "app_name", "Frost Debug" + resValue "string", "frost_web", "Frost Web Debug" } releaseTest { minifyEnabled true @@ -48,12 +49,14 @@ android { signingConfig signingConfigs.test versionNameSuffix "-test" resValue "string", "app_name", "Frost Test" + resValue "string", "frost_web", "Frost Web Test" } release { minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' resValue "string", "app_name", "Frost" + resValue "string", "frost_web", "Frost Web" } } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e715dc54..67366d71 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -10,7 +10,6 @@ - + android:theme="@style/FrostTheme.Splash"> @@ -47,16 +46,15 @@ android:name=".FrostWebActivity" android:autoRemoveFromRecents="true" android:exported="true" - android:label="@string/frost_web" android:hardwareAccelerated="true" + android:label="@string/frost_web" android:launchMode="singleInstance" android:taskAffinity="com.pitchedapps.frost.single.web" android:theme="@style/FrostTheme.Overlay"> - - - diff --git a/app/src/main/kotlin/com/pitchedapps/frost/FrostWebActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/FrostWebActivity.kt index 31ebfda2..c853e8f0 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/FrostWebActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/FrostWebActivity.kt @@ -1,17 +1,8 @@ package com.pitchedapps.frost import android.os.Bundle -import android.support.design.widget.CoordinatorLayout -import android.support.v7.app.AppCompatActivity -import android.support.v7.widget.Toolbar -import ca.allanwang.kau.utils.* -import com.jude.swipbackhelper.SwipeBackHelper -import com.pitchedapps.frost.facebook.FbTab -import com.pitchedapps.frost.utils.ARG_URL import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.utils.formattedFbUrl -import com.pitchedapps.frost.utils.setFrostColors -import com.pitchedapps.frost.web.FrostWebView /** @@ -20,8 +11,15 @@ import com.pitchedapps.frost.web.FrostWebView * Replica of [WebOverlayActivity] with a different base url * Didn't use activity-alias because it causes issues when only one activity has the singleInstance mode */ -class FrostWebActivity:WebOverlayActivity() { +class FrostWebActivity : WebOverlayActivity() { + override val url: String get() = intent.dataString!!.formattedFbUrl + + override fun onCreate(savedInstanceState: Bundle?) { + Prefs.prevId = Prefs.userId + super.onCreate(savedInstanceState) + } + } \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt index ca50d4e2..fcffffb2 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt @@ -28,6 +28,7 @@ import com.mikepenz.iconics.IconicsDrawable import com.mikepenz.materialdrawer.AccountHeader import com.mikepenz.materialdrawer.Drawer import com.pitchedapps.frost.dbflow.loadFbTabs +import com.pitchedapps.frost.facebook.FbCookie import com.pitchedapps.frost.facebook.FbCookie.switchUser import com.pitchedapps.frost.facebook.FbTab import com.pitchedapps.frost.facebook.PROFILE_PICTURE_URL @@ -83,8 +84,8 @@ class MainActivity : BaseActivity() { super.onPageScrolled(position, positionOffset, positionOffsetPixels) val delta: Float by lazy { positionOffset * (255 - 128).toFloat() } tabsForEachView { - position, view -> - view.setAllAlpha(when (position) { + tabPosition, view -> + view.setAllAlpha(when (tabPosition) { position -> 255.0f - delta position + 1 -> 128.0f + delta else -> 128f @@ -243,6 +244,11 @@ class MainActivity : BaseActivity() { return true } + override fun onResume() { + super.onResume() + FbCookie.switchBackUser { } + } + override fun onBackPressed() { if (currentFragment.onBackPressed()) return super.onBackPressed() diff --git a/app/src/main/kotlin/com/pitchedapps/frost/SettingsActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/SettingsActivity.kt index b55b2a10..19fa6362 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/SettingsActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/SettingsActivity.kt @@ -87,7 +87,6 @@ class SettingsActivity : KPrefActivity() { _, _, which, text -> item.pref = options[which] this@SettingsActivity.scheduleNotifications(item.pref) - this@SettingsActivity.toast(text) true }) } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt index bdac366e..0b2be3ee 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt @@ -1,9 +1,10 @@ package com.pitchedapps.frost import android.os.Bundle +import android.os.Handler import android.support.v7.app.AppCompatActivity import com.pitchedapps.frost.dbflow.loadFbCookiesAsync -import com.pitchedapps.frost.services.requestNotifications +import com.pitchedapps.frost.facebook.FbCookie import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.utils.launchNewTask @@ -16,15 +17,15 @@ class StartActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) L.d("Load cookies ${System.currentTimeMillis()}") - loadFbCookiesAsync { - cookies -> - cookies.forEach { requestNotifications(it.id) } - - L.d("Cookies loaded ${System.currentTimeMillis()} $cookies") - if (cookies.isNotEmpty()) - launchNewTask(if (Prefs.userId != -1L) MainActivity::class.java else SelectorActivity::class.java, ArrayList(cookies)) - else - launchNewTask(LoginActivity::class.java) + FbCookie.switchBackUser { + loadFbCookiesAsync { + cookies -> + L.d("Cookies loaded ${System.currentTimeMillis()} $cookies") + if (cookies.isNotEmpty()) + launchNewTask(if (Prefs.userId != -1L) MainActivity::class.java else SelectorActivity::class.java, ArrayList(cookies)) + else + launchNewTask(LoginActivity::class.java) + } } } } \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/WebOverlayActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/WebOverlayActivity.kt index 320e4028..1660d8db 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/WebOverlayActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/WebOverlayActivity.kt @@ -6,6 +6,8 @@ import android.support.v7.app.AppCompatActivity import android.support.v7.widget.Toolbar import ca.allanwang.kau.utils.* import com.jude.swipbackhelper.SwipeBackHelper +import com.mikepenz.google_material_typeface_library.GoogleMaterial +import com.pitchedapps.frost.facebook.FbCookie import com.pitchedapps.frost.utils.ARG_URL import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.utils.formattedFbUrl @@ -22,27 +24,38 @@ open class WebOverlayActivity : AppCompatActivity() { val frostWeb: FrostWebView by bindView(R.id.overlay_frost_webview) val coordinator: CoordinatorLayout by bindView(R.id.overlay_main_content) + companion object { + const val ARG_USER_ID = "arg_user_id" + } + open val url: String get() = intent.extras!!.getString(ARG_URL).formattedFbUrl + val userId: Long + get() = intent.extras?.getLong(ARG_USER_ID) ?: Prefs.userId + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_web_overlay) setSupportActionBar(toolbar) supportActionBar?.setDisplayShowHomeEnabled(true) supportActionBar?.setDisplayHomeAsUpEnabled(true) - toolbar.setNavigationOnClickListener { onBackPressed() } - frostWeb.web.setupWebview(url) - frostWeb.web.loadBaseUrl() + toolbar.navigationIcon = GoogleMaterial.Icon.gmd_close.toDrawable(this, 16, Prefs.iconColor) + toolbar.setNavigationOnClickListener { slideOut() } SwipeBackHelper.onCreate(this) SwipeBackHelper.getCurrentPage(this) .setSwipeBackEnable(true) .setSwipeSensitivity(0.5f) .setSwipeRelateEnable(true) .setSwipeRelateOffset(300) - frostWeb.web.addTitleListener({ toolbar.title = it }) setFrostColors(toolbar, themeWindow = false) coordinator.setBackgroundColor(Prefs.bgColor.withAlpha(255)) + + frostWeb.web.setupWebview(url) + frostWeb.web.addTitleListener({ toolbar.title = it }) + + if (userId != Prefs.userId) FbCookie.switchUser(userId) { frostWeb.web.loadBaseUrl() } + else frostWeb.web.loadBaseUrl() } /** @@ -70,8 +83,12 @@ open class WebOverlayActivity : AppCompatActivity() { override fun onBackPressed() { if (!frostWeb.onBackPressed()) { - finish() - overridePendingTransition(R.anim.kau_fade_in, R.anim.kau_slide_out_right_top) + slideOut() } } + + fun slideOut() { + finish() + overridePendingTransition(R.anim.kau_fade_in, R.anim.kau_slide_out_right_top) + } } \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt index 42232fc6..ac58fb2f 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt @@ -84,4 +84,18 @@ object FbCookie { removeCookie(id) reset(callback) } + + /** + * Notifications may come from different accounts, and we need to switch the cookies to load them + * When coming back to the main app, switch back to our original account before continuing + */ + fun switchBackUser(callback: () -> Unit) { + if (Prefs.prevId != -1L && Prefs.prevId != Prefs.userId) { + switchUser(Prefs.prevId) { + L.d("Switched from ${Prefs.userId} to ${Prefs.prevId}") + Prefs.prevId = -1L + callback() + } + } else callback() + } } \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationExtensions.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationExtensions.kt deleted file mode 100644 index ac94b527..00000000 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationExtensions.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.pitchedapps.frost.services - -import android.content.Context -import android.content.Intent - -/** - * Created by Allan Wang on 2017-06-14. - */ -fun Context.requestNotifications(id: Long) { - val intent = Intent(this, NotificationService::class.java) - intent.putExtra(NotificationService.ARG_ID, id) - startService(intent) -} \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt index 813bdce2..9c8bf2e6 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt @@ -12,6 +12,7 @@ import ca.allanwang.kau.utils.string import com.pitchedapps.frost.BuildConfig import com.pitchedapps.frost.FrostWebActivity import com.pitchedapps.frost.R +import com.pitchedapps.frost.WebOverlayActivity import com.pitchedapps.frost.dbflow.* import com.pitchedapps.frost.facebook.FACEBOOK_COM import com.pitchedapps.frost.facebook.FB_URL_BASE @@ -109,6 +110,7 @@ class NotificationService : JobService() { } else { val intent = Intent(context, FrostWebActivity::class.java) intent.data = Uri.parse("$FB_URL_BASE$href") + intent.putExtra(WebOverlayActivity.ARG_USER_ID, data.id) val group = "frost_${data.id}" val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0) val notifBuilder = context.frostNotification diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt index 62ed3cdd..40735544 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt @@ -17,6 +17,8 @@ object Prefs : KPref() { var userId: Long by kpref("user_id", -1L) + var prevId: Long by kpref("prev_id", -1L) + var theme: Int by kpref("theme", 0, postSetter = { value: Int -> loader.invalidate() }) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt index fe56d6d6..d4942e9e 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt @@ -30,7 +30,7 @@ import com.pitchedapps.frost.services.NotificationService internal const val EXTRA_COOKIES = "extra_cookies" internal const val ARG_URL = "arg_url" -fun Context.launchNewTask(clazz: Class, cookieList: ArrayList = arrayListOf(), clearStack: Boolean = clazz != LoginActivity::class.java) { +fun Context.launchNewTask(clazz: Class, cookieList: ArrayList = arrayListOf(), clearStack: Boolean = false) { startActivity(clazz, clearStack, { putParcelableArrayListExtra(EXTRA_COOKIES, cookieList) }) diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index 24bba22f..28f09eea 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -10,5 +10,4 @@ 16dp 1dip 100dp - diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6fd465c2..f05d2ce5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,5 +1,4 @@ - Frost Web Settings Hello World from section: %1$d diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 0adb5967..b999d606 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -3,6 +3,7 @@