Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/activity/BaseActivity.kt

391 lines
15 KiB
Kotlin
Raw Normal View History

2016-07-02 05:54:53 +02:00
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.activity
import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
2017-02-07 09:47:34 +01:00
import android.content.SharedPreferences
2016-07-02 05:54:53 +02:00
import android.content.res.Resources
import android.graphics.Rect
import android.nfc.NfcAdapter
import android.os.Bundle
2016-12-23 15:46:59 +01:00
import android.support.annotation.StyleRes
2017-01-03 14:22:20 +01:00
import android.support.v4.graphics.ColorUtils
2016-07-02 05:54:53 +02:00
import android.support.v7.preference.Preference
import android.support.v7.preference.PreferenceFragmentCompat
import android.support.v7.preference.PreferenceFragmentCompat.OnPreferenceDisplayDialogCallback
import android.support.v7.view.menu.ActionMenuItemView
import android.support.v7.widget.TwidereActionMenuView
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.View
import com.squareup.otto.Bus
2017-03-22 13:00:29 +01:00
import nl.komponents.kovenant.Promise
2016-12-18 03:32:11 +01:00
import org.mariotaku.chameleon.Chameleon
2016-12-20 13:52:00 +01:00
import org.mariotaku.chameleon.Chameleon.Theme.LightStatusBarMode
2016-12-18 06:21:24 +01:00
import org.mariotaku.chameleon.ChameleonActivity
2016-12-20 01:10:45 +01:00
import org.mariotaku.chameleon.ChameleonUtils
import org.mariotaku.kpreferences.KPreferences
2016-12-23 15:46:59 +01:00
import org.mariotaku.kpreferences.get
import org.mariotaku.restfu.http.RestHttpClient
2016-07-02 05:54:53 +02:00
import org.mariotaku.twidere.BuildConfig
2016-07-07 05:42:08 +02:00
import org.mariotaku.twidere.TwidereConstants.SHARED_PREFERENCES_NAME
2017-03-01 02:11:56 +01:00
import org.mariotaku.twidere.activity.iface.IBaseActivity
2017-03-21 03:33:27 +01:00
import org.mariotaku.twidere.activity.iface.IControlBarActivity
2016-07-02 05:54:53 +02:00
import org.mariotaku.twidere.activity.iface.IThemedActivity
2017-03-21 03:33:27 +01:00
import org.mariotaku.twidere.constant.themeBackgroundAlphaKey
import org.mariotaku.twidere.constant.themeBackgroundOptionKey
2016-12-23 15:46:59 +01:00
import org.mariotaku.twidere.constant.themeColorKey
import org.mariotaku.twidere.constant.themeKey
2016-07-02 05:54:53 +02:00
import org.mariotaku.twidere.fragment.iface.IBaseFragment.SystemWindowsInsetsCallback
import org.mariotaku.twidere.model.DefaultFeatures
2016-07-02 05:54:53 +02:00
import org.mariotaku.twidere.preference.iface.IDialogPreference
import org.mariotaku.twidere.util.*
import org.mariotaku.twidere.util.KeyboardShortcutsHandler.KeyboardShortcutCallback
import org.mariotaku.twidere.util.dagger.GeneralComponentHelper
2017-01-20 15:08:42 +01:00
import org.mariotaku.twidere.util.premium.ExtraFeaturesService
2017-01-03 16:37:24 +01:00
import org.mariotaku.twidere.util.support.ActivitySupport
import org.mariotaku.twidere.util.support.ActivitySupport.TaskDescriptionCompat
2016-12-23 03:26:44 +01:00
import org.mariotaku.twidere.util.theme.TwidereAppearanceCreator
2017-01-07 16:51:21 +01:00
import org.mariotaku.twidere.util.theme.getCurrentThemeResource
2016-07-02 05:54:53 +02:00
import org.mariotaku.twidere.view.iface.IExtendedView.OnFitSystemWindowsListener
import java.lang.reflect.InvocationTargetException
import java.util.*
import javax.inject.Inject
@SuppressLint("Registered")
2017-03-01 02:11:56 +01:00
open class BaseActivity : ChameleonActivity(), IBaseActivity<BaseActivity>, IThemedActivity,
2016-07-07 09:39:32 +02:00
IControlBarActivity, OnFitSystemWindowsListener, SystemWindowsInsetsCallback,
2016-12-18 06:21:24 +01:00
KeyboardShortcutCallback, OnPreferenceDisplayDialogCallback {
2016-12-24 05:30:43 +01:00
2016-07-02 05:54:53 +02:00
@Inject
lateinit var keyboardShortcutsHandler: KeyboardShortcutsHandler
@Inject
lateinit var twitterWrapper: AsyncTwitterWrapper
@Inject
lateinit var readStateManager: ReadStateManager
@Inject
lateinit var bus: Bus
@Inject
lateinit var preferences: SharedPreferencesWrapper
@Inject
lateinit var kPreferences: KPreferences
@Inject
2016-07-02 05:54:53 +02:00
lateinit var notificationManager: NotificationManagerWrapper
@Inject
lateinit var userColorNameManager: UserColorNameManager
@Inject
lateinit var permissionsManager: PermissionsManager
2017-01-20 15:08:42 +01:00
@Inject
lateinit var extraFeaturesService: ExtraFeaturesService
@Inject
lateinit var defaultFeatures: DefaultFeatures
@Inject
lateinit var restHttpClient: RestHttpClient
2016-07-02 05:54:53 +02:00
2017-03-01 02:11:56 +01:00
private val actionHelper = IBaseActivity.ActionHelper(this)
2017-03-21 03:33:27 +01:00
private val themePreferences by lazy {
getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE)
}
2016-07-02 05:54:53 +02:00
// Registered listeners
2016-12-18 06:21:24 +01:00
private val controlBarOffsetListeners = ArrayList<IControlBarActivity.ControlBarOffsetListener>()
2016-07-02 05:54:53 +02:00
2016-12-20 01:10:45 +01:00
private val userTheme: Chameleon.Theme by lazy {
val theme = Chameleon.Theme.from(this)
theme.colorAccent = ThemeUtils.getUserAccentColor(this)
theme.colorPrimary = ThemeUtils.getUserAccentColor(this)
if (theme.isToolbarColored) {
theme.colorToolbar = theme.colorPrimary
}
2017-01-03 14:22:20 +01:00
if (ThemeUtils.isTransparentBackground(themeBackgroundOption)) {
theme.colorToolbar = ColorUtils.setAlphaComponent(theme.colorToolbar,
2017-03-21 03:33:27 +01:00
ThemeUtils.getActionBarAlpha(themePreferences[themeBackgroundAlphaKey]))
2017-01-03 14:22:20 +01:00
}
2016-12-20 01:10:45 +01:00
theme.statusBarColor = ChameleonUtils.darkenColor(theme.colorToolbar)
2016-12-20 13:52:00 +01:00
theme.lightStatusBarMode = LightStatusBarMode.AUTO
2016-12-20 01:10:45 +01:00
theme.textColorLink = ThemeUtils.getOptimalAccentColor(theme.colorAccent, theme.colorForeground)
return@lazy theme
}
2016-07-02 05:54:53 +02:00
// Data fields
2016-12-18 06:21:24 +01:00
private var systemWindowsInsets: Rect? = null
2016-07-02 05:54:53 +02:00
var keyMetaState: Int = 0
private set
override fun getSystemWindowsInsets(insets: Rect): Boolean {
2016-12-18 06:21:24 +01:00
if (systemWindowsInsets == null) return false
insets.set(systemWindowsInsets)
2016-07-02 05:54:53 +02:00
return true
}
override fun onFitSystemWindows(insets: Rect) {
2016-12-18 06:21:24 +01:00
if (systemWindowsInsets == null)
systemWindowsInsets = Rect(insets)
2016-07-02 05:54:53 +02:00
else {
2016-12-18 06:21:24 +01:00
systemWindowsInsets!!.set(insets)
2016-07-02 05:54:53 +02:00
}
notifyControlBarOffsetChanged()
}
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
val keyCode = event.keyCode
if (KeyEvent.isModifierKey(keyCode)) {
val action = event.action
if (action == MotionEvent.ACTION_DOWN) {
keyMetaState = keyMetaState or KeyboardShortcutsHandler.getMetaStateForKeyCode(keyCode)
} else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
keyMetaState = keyMetaState and KeyboardShortcutsHandler.getMetaStateForKeyCode(keyCode).inv()
}
}
return super.dispatchKeyEvent(event)
}
override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
if (handleKeyboardShortcutSingle(keyboardShortcutsHandler, keyCode, event, keyMetaState))
return true
return isKeyboardShortcutHandled(keyboardShortcutsHandler, keyCode, event, keyMetaState) || super.onKeyUp(keyCode, event)
}
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
if (handleKeyboardShortcutRepeat(keyboardShortcutsHandler, keyCode, event.repeatCount, event, keyMetaState))
return true
return isKeyboardShortcutHandled(keyboardShortcutsHandler, keyCode, event, keyMetaState) || super.onKeyDown(keyCode, event)
}
override fun handleKeyboardShortcutSingle(handler: KeyboardShortcutsHandler, keyCode: Int, event: KeyEvent, metaState: Int): Boolean {
return false
}
override fun isKeyboardShortcutHandled(handler: KeyboardShortcutsHandler, keyCode: Int, event: KeyEvent, metaState: Int): Boolean {
return false
}
override fun handleKeyboardShortcutRepeat(handler: KeyboardShortcutsHandler, keyCode: Int, repeatCount: Int, event: KeyEvent, metaState: Int): Boolean {
return false
}
override fun onCreate(savedInstanceState: Bundle?) {
if (BuildConfig.DEBUG) {
StrictModeUtils.detectAllVmPolicy()
StrictModeUtils.detectAllThreadPolicy()
}
2016-12-23 15:46:59 +01:00
val prefs = getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE)
2017-02-07 09:47:34 +01:00
val themeResource = getThemeResource(prefs, prefs[themeKey], prefs[themeColorKey])
2016-12-23 15:46:59 +01:00
if (themeResource != 0) {
setTheme(themeResource)
}
2016-07-02 05:54:53 +02:00
super.onCreate(savedInstanceState)
2017-01-03 16:37:24 +01:00
ActivitySupport.setTaskDescription(this, TaskDescriptionCompat(title.toString(), null,
ColorUtils.setAlphaComponent(overrideTheme.colorToolbar, 0xFF)))
2016-07-02 05:54:53 +02:00
GeneralComponentHelper.build(this).inject(this)
}
override fun onResume() {
super.onResume()
val adapter = NfcAdapter.getDefaultAdapter(this)
if (adapter != null && adapter.isEnabled) {
val handlerFilter = IntentUtils.getWebLinkIntentFilter(this)
if (handlerFilter != null) {
val linkIntent = Intent(this, WebLinkHandlerActivity::class.java)
val intent = PendingIntent.getActivity(this, 0, linkIntent, 0)
val intentFilter = IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED)
for (i in 0 until handlerFilter.countDataSchemes()) {
intentFilter.addDataScheme(handlerFilter.getDataScheme(i))
2016-07-02 05:54:53 +02:00
}
for (i in 0 until handlerFilter.countDataAuthorities()) {
2016-07-02 05:54:53 +02:00
val authorityEntry = handlerFilter.getDataAuthority(i)
val port = authorityEntry.port
intentFilter.addDataAuthority(authorityEntry.host, if (port < 0) null else Integer.toString(port))
}
try {
adapter.enableForegroundDispatch(this, intent, arrayOf(intentFilter), null)
} catch (e: Exception) {
// Ignore if blocked by modified roms
}
}
}
}
override fun onPause() {
val adapter = NfcAdapter.getDefaultAdapter(this)
if (adapter != null && adapter.isEnabled) {
try {
adapter.disableForegroundDispatch(this)
} catch (e: Exception) {
// Ignore if blocked by modified roms
}
}
actionHelper.dispatchOnPause()
super.onPause()
}
override fun notifyControlBarOffsetChanged() {
val offset = controlBarOffset
2016-12-18 06:21:24 +01:00
for (l in controlBarOffsetListeners) {
2016-07-02 05:54:53 +02:00
l.onControlBarOffsetChanged(this, offset)
}
}
override fun registerControlBarOffsetListener(listener: IControlBarActivity.ControlBarOffsetListener) {
2016-12-18 06:21:24 +01:00
controlBarOffsetListeners.add(listener)
2016-07-02 05:54:53 +02:00
}
override fun unregisterControlBarOffsetListener(listener: IControlBarActivity.ControlBarOffsetListener) {
2016-12-18 06:21:24 +01:00
controlBarOffsetListeners.remove(listener)
2016-07-02 05:54:53 +02:00
}
override fun onResumeFragments() {
super.onResumeFragments()
actionHelper.dispatchOnResumeFragments()
}
2017-03-22 13:00:29 +01:00
override fun executeAfterFragmentResumed(useHandler: Boolean, action: (BaseActivity) -> Unit): Promise<Unit, Exception> {
return actionHelper.executeAfterFragmentResumed(useHandler, action)
2016-07-02 05:54:53 +02:00
}
override final val currentThemeBackgroundAlpha by lazy {
themeBackgroundAlpha
}
override final val currentThemeBackgroundOption by lazy {
themeBackgroundOption
}
override val themeBackgroundAlpha: Int
2017-03-21 03:33:27 +01:00
get() = themePreferences[themeBackgroundAlphaKey]
2016-07-02 05:54:53 +02:00
override val themeBackgroundOption: String
2017-03-21 03:33:27 +01:00
get() = themePreferences[themeBackgroundOptionKey]
2016-07-02 05:54:53 +02:00
2017-02-08 05:12:25 +01:00
protected open val shouldApplyWindowBackground: Boolean
get() {
return true
}
2016-07-02 05:54:53 +02:00
override fun onApplyThemeResource(theme: Resources.Theme, resId: Int, first: Boolean) {
super.onApplyThemeResource(theme, resId, first)
2016-07-07 05:42:08 +02:00
if (window != null && shouldApplyWindowBackground) {
2016-07-02 05:54:53 +02:00
ThemeUtils.applyWindowBackground(this, window, themeBackgroundOption,
themeBackgroundAlpha)
}
}
override fun onCreateView(parent: View?, name: String, context: Context, attrs: AttributeSet): View? {
// Fix for https://github.com/afollestad/app-theme-engine/issues/109
if (context != this) {
2016-07-02 05:54:53 +02:00
val delegate = delegate
var view: View? = delegate.createView(parent, name, context, attrs)
if (view == null) {
view = newInstance(name, context, attrs)
}
if (view == null) {
view = newInstance(name, context, attrs)
}
if (view != null) {
return view
}
}
if (parent is TwidereActionMenuView) {
val cls = findClass(name)
if (cls != null && ActionMenuItemView::class.java.isAssignableFrom(cls)) {
return parent.createActionMenuView(context, attrs)
}
}
return super.onCreateView(parent, name, context, attrs)
}
private fun findClass(name: String): Class<*>? {
var cls: Class<*>? = null
try {
cls = Class.forName(name)
} catch (e: ClassNotFoundException) {
// Ignore
}
if (cls != null) return cls
for (prefix in sClassPrefixList) {
try {
cls = Class.forName(prefix + name)
} catch (e: ClassNotFoundException) {
// Ignore
}
if (cls != null) return cls
}
return null
}
private fun newInstance(name: String, context: Context, attrs: AttributeSet): View? {
try {
val cls = findClass(name) ?: throw ClassNotFoundException(name)
val constructor = cls.getConstructor(Context::class.java, AttributeSet::class.java)
return constructor.newInstance(context, attrs) as View
} catch (e: InstantiationException) {
return null
} catch (e: IllegalAccessException) {
return null
} catch (e: InvocationTargetException) {
return null
} catch (e: NoSuchMethodException) {
return null
} catch (e: ClassNotFoundException) {
return null
}
}
override fun onPreferenceDisplayDialog(fragment: PreferenceFragmentCompat, preference: Preference): Boolean {
if (preference is IDialogPreference) {
preference.displayDialog(fragment)
return true
}
return false
}
2016-12-18 03:32:11 +01:00
override fun getOverrideTheme(): Chameleon.Theme {
2016-12-20 01:10:45 +01:00
return userTheme
2016-07-02 05:54:53 +02:00
}
2016-12-23 03:26:44 +01:00
override fun onCreateAppearanceCreator(): Chameleon.AppearanceCreator? {
return TwidereAppearanceCreator
}
2016-12-23 15:46:59 +01:00
@StyleRes
2017-02-07 09:47:34 +01:00
protected open fun getThemeResource(preferences: SharedPreferences, theme: String, themeColor: Int): Int {
2017-01-07 16:51:21 +01:00
return getCurrentThemeResource(this, theme)
}
2016-12-23 15:46:59 +01:00
2016-07-02 05:54:53 +02:00
companion object {
private val sClassPrefixList = arrayOf("android.widget.", "android.view.", "android.webkit.")
}
}
2016-12-23 03:26:44 +01:00