2019-01-18 10:12:08 +00:00
|
|
|
/*
|
2019-01-25 13:04:59 +00:00
|
|
|
* Copyright 2019 New Vector Ltd
|
2019-01-18 10:12:08 +00:00
|
|
|
*
|
2019-01-25 13:04:59 +00:00
|
|
|
* 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
|
2019-01-18 10:12:08 +00:00
|
|
|
*
|
2019-01-25 13:04:59 +00:00
|
|
|
* 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.
|
2019-01-18 10:12:08 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-03 15:56:33 +00:00
|
|
|
package im.vector.riotredesign.core.platform
|
|
|
|
|
2019-03-28 16:28:20 +00:00
|
|
|
import android.content.res.Configuration
|
2019-02-28 17:50:30 +00:00
|
|
|
import android.os.Bundle
|
2019-03-28 10:53:28 +00:00
|
|
|
import android.view.Menu
|
|
|
|
import android.view.MenuItem
|
2019-03-28 16:28:20 +00:00
|
|
|
import android.view.View
|
2019-03-28 10:53:28 +00:00
|
|
|
import androidx.annotation.*
|
|
|
|
import androidx.appcompat.widget.Toolbar
|
|
|
|
import butterknife.BindView
|
|
|
|
import butterknife.ButterKnife
|
|
|
|
import butterknife.Unbinder
|
2018-10-28 18:18:14 +00:00
|
|
|
import com.airbnb.mvrx.BaseMvRxActivity
|
2019-02-28 17:50:30 +00:00
|
|
|
import com.bumptech.glide.util.Util
|
2019-03-20 13:06:29 +00:00
|
|
|
import im.vector.riotredesign.BuildConfig
|
2019-03-28 10:53:28 +00:00
|
|
|
import im.vector.riotredesign.R
|
2019-03-28 16:28:20 +00:00
|
|
|
import im.vector.riotredesign.features.rageshake.BugReportActivity
|
|
|
|
import im.vector.riotredesign.features.rageshake.BugReporter
|
2019-03-28 10:53:28 +00:00
|
|
|
import im.vector.riotredesign.features.rageshake.RageShake
|
2019-03-28 16:28:20 +00:00
|
|
|
import im.vector.riotredesign.features.themes.ThemeUtils
|
2019-03-20 13:06:29 +00:00
|
|
|
import im.vector.riotredesign.receivers.DebugReceiver
|
2019-03-28 10:53:28 +00:00
|
|
|
import im.vector.ui.themes.ActivityOtherThemes
|
2019-01-17 17:20:01 +00:00
|
|
|
import io.reactivex.disposables.CompositeDisposable
|
|
|
|
import io.reactivex.disposables.Disposable
|
2019-03-28 16:28:20 +00:00
|
|
|
import timber.log.Timber
|
2018-10-03 15:56:33 +00:00
|
|
|
|
2019-03-28 10:53:28 +00:00
|
|
|
|
2019-01-17 17:20:01 +00:00
|
|
|
abstract class RiotActivity : BaseMvRxActivity() {
|
2019-03-28 10:53:28 +00:00
|
|
|
/* ==========================================================================================
|
|
|
|
* UI
|
|
|
|
* ========================================================================================== */
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@BindView(R.id.toolbar)
|
|
|
|
protected lateinit var toolbar: Toolbar
|
|
|
|
|
|
|
|
/* ==========================================================================================
|
|
|
|
* DATA
|
|
|
|
* ========================================================================================== */
|
|
|
|
|
|
|
|
private var unBinder: Unbinder? = null
|
|
|
|
|
|
|
|
private var savedInstanceState: Bundle? = null
|
2019-01-17 17:20:01 +00:00
|
|
|
|
2019-03-20 13:06:29 +00:00
|
|
|
// For debug only
|
|
|
|
private var debugReceiver: DebugReceiver? = null
|
|
|
|
|
2019-01-17 17:20:01 +00:00
|
|
|
private val uiDisposables = CompositeDisposable()
|
2019-02-28 17:50:30 +00:00
|
|
|
private val restorables = ArrayList<Restorable>()
|
|
|
|
|
2019-03-28 10:53:28 +00:00
|
|
|
private var rageShake: RageShake? = null
|
|
|
|
|
2019-02-28 17:50:30 +00:00
|
|
|
override fun onSaveInstanceState(outState: Bundle) {
|
|
|
|
super.onSaveInstanceState(outState)
|
|
|
|
restorables.forEach { it.onSaveInstanceState(outState) }
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
|
|
|
|
restorables.forEach { it.onRestoreInstanceState(savedInstanceState) }
|
|
|
|
super.onRestoreInstanceState(savedInstanceState)
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainThread
|
|
|
|
protected fun <T : Restorable> T.register(): T {
|
|
|
|
Util.assertMainThread()
|
|
|
|
restorables.add(this)
|
|
|
|
return this
|
|
|
|
}
|
2019-01-17 17:20:01 +00:00
|
|
|
|
|
|
|
protected fun Disposable.disposeOnDestroy(): Disposable {
|
|
|
|
uiDisposables.add(this)
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2019-03-28 10:53:28 +00:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
|
|
|
// Shake detector
|
|
|
|
rageShake = RageShake(this)
|
|
|
|
|
|
|
|
ThemeUtils.setActivityTheme(this, getOtherThemes())
|
|
|
|
|
|
|
|
doBeforeSetContentView()
|
|
|
|
|
|
|
|
if (getLayoutRes() != -1) {
|
|
|
|
setContentView(getLayoutRes())
|
|
|
|
}
|
|
|
|
|
|
|
|
unBinder = ButterKnife.bind(this)
|
|
|
|
|
|
|
|
this.savedInstanceState = savedInstanceState
|
|
|
|
|
|
|
|
initUiAndData()
|
|
|
|
|
|
|
|
val titleRes = getTitleRes()
|
|
|
|
if (titleRes != -1) {
|
|
|
|
supportActionBar?.let {
|
|
|
|
it.setTitle(titleRes)
|
|
|
|
} ?: run {
|
|
|
|
setTitle(titleRes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 16:28:20 +00:00
|
|
|
override fun onDestroy() {
|
|
|
|
super.onDestroy()
|
|
|
|
|
|
|
|
unBinder?.unbind()
|
|
|
|
unBinder = null
|
|
|
|
}
|
|
|
|
|
2019-03-20 13:06:29 +00:00
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
|
2019-03-28 16:28:20 +00:00
|
|
|
if (this !is BugReportActivity) {
|
|
|
|
rageShake?.start()
|
|
|
|
}
|
2019-03-28 10:53:28 +00:00
|
|
|
|
2019-03-20 13:06:29 +00:00
|
|
|
DebugReceiver
|
|
|
|
.getIntentFilter(this)
|
|
|
|
.takeIf { BuildConfig.DEBUG }
|
|
|
|
?.let {
|
|
|
|
debugReceiver = DebugReceiver()
|
|
|
|
registerReceiver(debugReceiver, it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPause() {
|
|
|
|
super.onPause()
|
|
|
|
|
2019-03-28 10:53:28 +00:00
|
|
|
rageShake?.stop()
|
|
|
|
|
2019-03-20 13:06:29 +00:00
|
|
|
debugReceiver?.let {
|
|
|
|
unregisterReceiver(debugReceiver)
|
|
|
|
debugReceiver = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 16:28:20 +00:00
|
|
|
override fun onWindowFocusChanged(hasFocus: Boolean) {
|
|
|
|
super.onWindowFocusChanged(hasFocus)
|
|
|
|
|
|
|
|
if (hasFocus && displayInFullscreen()) {
|
|
|
|
setFullScreen()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onMultiWindowModeChanged(isInMultiWindowMode: Boolean, newConfig: Configuration?) {
|
|
|
|
super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig)
|
|
|
|
|
|
|
|
Timber.w("onMultiWindowModeChanged. isInMultiWindowMode: $isInMultiWindowMode")
|
|
|
|
BugReporter.inMultiWindowMode = isInMultiWindowMode
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ==========================================================================================
|
|
|
|
* PRIVATE METHODS
|
|
|
|
* ========================================================================================== */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Force to render the activity in fullscreen
|
|
|
|
*/
|
|
|
|
private fun setFullScreen() {
|
|
|
|
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|
|
|
|
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
|
|
|
|
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
|
|
|
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
|
|
|
|
or View.SYSTEM_UI_FLAG_FULLSCREEN
|
|
|
|
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
|
|
|
|
}
|
|
|
|
|
2019-03-28 10:53:28 +00:00
|
|
|
/* ==========================================================================================
|
|
|
|
* MENU MANAGEMENT
|
|
|
|
* ========================================================================================== */
|
|
|
|
|
|
|
|
final override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
|
|
|
val menuRes = getMenuRes()
|
|
|
|
|
|
|
|
if (menuRes != -1) {
|
|
|
|
menuInflater.inflate(menuRes, menu)
|
|
|
|
ThemeUtils.tintMenuIcons(menu, ThemeUtils.getColor(this, getMenuTint()))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.onCreateOptionsMenu(menu)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
|
if (item.itemId == android.R.id.home) {
|
|
|
|
setResult(RESULT_CANCELED)
|
|
|
|
finish()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.onOptionsItemSelected(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ==========================================================================================
|
|
|
|
* PROTECTED METHODS
|
|
|
|
* ========================================================================================== */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the saved instance state.
|
|
|
|
* Ensure {@link isFirstCreation()} returns false before calling this
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
protected fun getSavedInstanceState(): Bundle {
|
|
|
|
return savedInstanceState!!
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is first creation
|
|
|
|
*
|
|
|
|
* @return true if Activity is created for the first time (and not restored by the system)
|
|
|
|
*/
|
|
|
|
protected fun isFirstCreation() = savedInstanceState == null
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure the Toolbar. It MUST be present in your layout with id "toolbar"
|
|
|
|
*/
|
|
|
|
protected fun configureToolbar() {
|
|
|
|
setSupportActionBar(toolbar)
|
|
|
|
|
|
|
|
supportActionBar?.let {
|
|
|
|
it.setDisplayShowHomeEnabled(true)
|
|
|
|
it.setDisplayHomeAsUpEnabled(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ==========================================================================================
|
|
|
|
* OPEN METHODS
|
|
|
|
* ========================================================================================== */
|
|
|
|
|
|
|
|
@LayoutRes
|
|
|
|
open fun getLayoutRes() = -1
|
|
|
|
|
|
|
|
open fun displayInFullscreen() = false
|
|
|
|
|
|
|
|
open fun doBeforeSetContentView() = Unit
|
|
|
|
|
|
|
|
open fun initUiAndData() = Unit
|
|
|
|
|
|
|
|
@StringRes
|
|
|
|
open fun getTitleRes() = -1
|
|
|
|
|
|
|
|
@MenuRes
|
|
|
|
open fun getMenuRes() = -1
|
|
|
|
|
|
|
|
@AttrRes
|
|
|
|
open fun getMenuTint() = 0 // TODO R.attr.vctr_icon_tint_on_dark_action_bar_color
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a object containing other themes for this activity
|
|
|
|
*/
|
|
|
|
open fun getOtherThemes(): ActivityOtherThemes = ActivityOtherThemes.Default
|
2019-01-17 17:20:01 +00:00
|
|
|
}
|