-
Notifications
You must be signed in to change notification settings - Fork 868
Upgrade androidx activity and fragment to 1.5.0 #6436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
62a20ba
356718d
529898d
c6a89c7
1297ccd
aae6e20
944b447
3976141
f7a0615
be099dc
81505d3
1355178
d43c802
3e42cec
293a177
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Let your Activity or Fragment implement `VectorMenuProvider` if they provide a menu. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,8 +18,12 @@ package im.vector.lib.ui.styles.debug | |
|
|
||
| import android.os.Bundle | ||
| import android.view.Menu | ||
| import android.view.MenuInflater | ||
| import android.view.MenuItem | ||
| import android.widget.Toast | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import androidx.core.view.MenuProvider | ||
| import androidx.lifecycle.Lifecycle | ||
| import com.google.android.material.dialog.MaterialAlertDialogBuilder | ||
| import com.google.android.material.snackbar.Snackbar | ||
| import im.vector.lib.ui.styles.R | ||
|
|
@@ -31,6 +35,7 @@ abstract class DebugMaterialThemeActivity : AppCompatActivity() { | |
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| setupMenu() | ||
| val views = ActivityDebugMaterialThemeBinding.inflate(layoutInflater) | ||
| setContentView(views.root) | ||
|
|
||
|
|
@@ -72,6 +77,27 @@ abstract class DebugMaterialThemeActivity : AppCompatActivity() { | |
| } | ||
| } | ||
|
|
||
| private fun setupMenu() { | ||
| addMenuProvider( | ||
| object : MenuProvider { | ||
| override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) { | ||
| menuInflater.inflate(R.menu.menu_debug, menu) | ||
| } | ||
|
|
||
| override fun onMenuItemSelected(menuItem: MenuItem): Boolean { | ||
| Toast.makeText( | ||
| this@DebugMaterialThemeActivity, | ||
| "Menu ${menuItem.title} clicked!", | ||
| Toast.LENGTH_SHORT | ||
| ).show() | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added this toast to have a visual feedback |
||
| return true | ||
| } | ||
| }, | ||
| this, | ||
| Lifecycle.State.RESUMED | ||
| ) | ||
| } | ||
|
|
||
| private fun showTestDialog(theme: Int) { | ||
| MaterialAlertDialogBuilder(this, theme) | ||
| .setTitle("Dialog title") | ||
|
|
@@ -82,9 +108,4 @@ abstract class DebugMaterialThemeActivity : AppCompatActivity() { | |
| .setNeutralButton("Neutral", null) | ||
| .show() | ||
| } | ||
|
|
||
| override fun onCreateOptionsMenu(menu: Menu): Boolean { | ||
| menuInflater.inflate(R.menu.menu_debug, menu) | ||
| return true | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -180,3 +180,8 @@ System\.currentTimeMillis\(\)===2 | |
|
|
||
| ### Remove extra space between the name and the description | ||
| \* @\w+ \w+ + | ||
|
|
||
| ### Please use the MenuProvider interface now | ||
| onCreateOptionsMenu | ||
| onOptionsItemSelected | ||
| onPrepareOptionsMenu | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not deprecated (yet?) on Activities, and better to avoid using a mix of solutions (this was not working well) |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,27 +19,30 @@ package im.vector.app.core.platform | |
| import android.annotation.SuppressLint | ||
| import android.app.Activity | ||
| import android.content.Context | ||
| import android.content.res.Configuration | ||
| import android.os.Build | ||
| import android.os.Bundle | ||
| import android.os.Parcelable | ||
| import android.view.Menu | ||
| import android.view.MenuInflater | ||
| import android.view.MenuItem | ||
| import android.view.View | ||
| import android.view.WindowInsetsController | ||
| import android.view.WindowManager | ||
| import android.widget.TextView | ||
| import androidx.annotation.CallSuper | ||
| import androidx.annotation.MainThread | ||
| import androidx.annotation.MenuRes | ||
| import androidx.annotation.StringRes | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import androidx.coordinatorlayout.widget.CoordinatorLayout | ||
| import androidx.core.app.MultiWindowModeChangedInfo | ||
| import androidx.core.content.ContextCompat | ||
| import androidx.core.util.Consumer | ||
| import androidx.core.view.MenuProvider | ||
| import androidx.core.view.isVisible | ||
| import androidx.fragment.app.Fragment | ||
| import androidx.fragment.app.FragmentFactory | ||
| import androidx.fragment.app.FragmentManager | ||
| import androidx.lifecycle.Lifecycle | ||
| import androidx.lifecycle.ViewModelProvider | ||
| import androidx.lifecycle.lifecycleScope | ||
| import androidx.viewbinding.ViewBinding | ||
|
|
@@ -86,6 +89,7 @@ import im.vector.app.features.themes.ThemeUtils | |
| import im.vector.app.receivers.DebugReceiver | ||
| import kotlinx.coroutines.flow.launchIn | ||
| import kotlinx.coroutines.flow.onEach | ||
| import org.matrix.android.sdk.api.extensions.orFalse | ||
| import org.matrix.android.sdk.api.extensions.tryOrNull | ||
| import org.matrix.android.sdk.api.failure.GlobalError | ||
| import org.matrix.android.sdk.api.failure.InitialSyncRequestReason | ||
|
|
@@ -199,6 +203,8 @@ abstract class VectorBaseActivity<VB : ViewBinding> : AppCompatActivity(), Maver | |
| supportFragmentManager.fragmentFactory = fragmentFactory | ||
| viewModelFactory = activityEntryPoint.viewModelFactory() | ||
| super.onCreate(savedInstanceState) | ||
| addOnMultiWindowModeChangedListener(onMultiWindowModeChangedListener) | ||
| setupMenu() | ||
| configurationViewModel = viewModelProvider.get(ConfigurationViewModel::class.java) | ||
| bugReporter = singletonEntryPoint.bugReporter() | ||
| pinLocker = singletonEntryPoint.pinLocker() | ||
|
|
@@ -249,6 +255,32 @@ abstract class VectorBaseActivity<VB : ViewBinding> : AppCompatActivity(), Maver | |
| } | ||
| } | ||
|
|
||
| private fun setupMenu() { | ||
| // Always add a MenuProvider to handle the back action from the Toolbar | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean even if |
||
| val vectorMenuProvider = this as? VectorMenuProvider | ||
| addMenuProvider( | ||
| object : MenuProvider { | ||
| override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) { | ||
| vectorMenuProvider?.let { | ||
| menuInflater.inflate(it.getMenuRes(), menu) | ||
| it.handlePostCreateMenu(menu) | ||
| } | ||
| } | ||
|
|
||
| override fun onPrepareMenu(menu: Menu) { | ||
| vectorMenuProvider?.handlePrepareMenu(menu) | ||
| } | ||
|
|
||
| override fun onMenuItemSelected(menuItem: MenuItem): Boolean { | ||
| return vectorMenuProvider?.handleMenuItemSelected(menuItem).orFalse() || | ||
| handleMenuItemHome(menuItem) | ||
| } | ||
| }, | ||
| this, | ||
| Lifecycle.State.RESUMED | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * This method has to be called for the font size setting be supported correctly. | ||
| */ | ||
|
|
@@ -332,6 +364,7 @@ abstract class VectorBaseActivity<VB : ViewBinding> : AppCompatActivity(), Maver | |
| } | ||
|
|
||
| override fun onDestroy() { | ||
| removeOnMultiWindowModeChangedListener(onMultiWindowModeChangedListener) | ||
| super.onDestroy() | ||
| Timber.i("onDestroy Activity ${javaClass.simpleName}") | ||
| } | ||
|
|
@@ -417,11 +450,9 @@ abstract class VectorBaseActivity<VB : ViewBinding> : AppCompatActivity(), Maver | |
| } | ||
| } | ||
|
|
||
| override fun onMultiWindowModeChanged(isInMultiWindowMode: Boolean, newConfig: Configuration?) { | ||
| super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig) | ||
|
|
||
| Timber.w("onMultiWindowModeChanged. isInMultiWindowMode: $isInMultiWindowMode") | ||
| bugReporter.inMultiWindowMode = isInMultiWindowMode | ||
| private val onMultiWindowModeChangedListener = Consumer<MultiWindowModeChangedInfo> { | ||
| Timber.w("onMultiWindowModeChanged. isInMultiWindowMode: ${it.isInMultiWindowMode}") | ||
| bugReporter.inMultiWindowMode = it.isInMultiWindowMode | ||
| } | ||
|
|
||
| protected fun createFragment(fragmentClass: Class<out Fragment>, argsParcelable: Parcelable? = null): Fragment { | ||
|
|
@@ -463,28 +494,14 @@ abstract class VectorBaseActivity<VB : ViewBinding> : AppCompatActivity(), Maver | |
| } | ||
| } | ||
|
|
||
| /* ========================================================================================== | ||
| * MENU MANAGEMENT | ||
| * ========================================================================================== */ | ||
|
|
||
| override fun onCreateOptionsMenu(menu: Menu): Boolean { | ||
| val menuRes = getMenuRes() | ||
|
|
||
| if (menuRes != -1) { | ||
| menuInflater.inflate(menuRes, menu) | ||
| return true | ||
| } | ||
|
|
||
| return super.onCreateOptionsMenu(menu) | ||
| } | ||
|
|
||
| override fun onOptionsItemSelected(item: MenuItem): Boolean { | ||
| if (item.itemId == android.R.id.home) { | ||
| onBackPressed(true) | ||
| return true | ||
| private fun handleMenuItemHome(item: MenuItem): Boolean { | ||
| return when (item.itemId) { | ||
| android.R.id.home -> { | ||
| onBackPressed(true) | ||
| true | ||
| } | ||
| else -> false | ||
| } | ||
|
|
||
| return super.onOptionsItemSelected(item) | ||
| } | ||
|
|
||
| override fun onBackPressed() { | ||
|
|
@@ -587,9 +604,6 @@ abstract class VectorBaseActivity<VB : ViewBinding> : AppCompatActivity(), Maver | |
| @StringRes | ||
| open fun getTitleRes() = -1 | ||
|
|
||
| @MenuRes | ||
| open fun getMenuRes() = -1 | ||
|
|
||
| /** | ||
| * Return a object containing other themes for this activity. | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Copyright (c) 2022 New Vector Ltd | ||
| * | ||
| * 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 | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| package im.vector.app.core.platform | ||
|
|
||
| import android.view.Menu | ||
| import android.view.MenuItem | ||
| import androidx.annotation.MenuRes | ||
|
|
||
| /** | ||
| * Let your Activity of Fragment implement this interface if they provide a Menu. | ||
| */ | ||
| interface VectorMenuProvider { | ||
| @MenuRes | ||
| fun getMenuRes(): Int | ||
|
|
||
| // No op by default | ||
| fun handlePostCreateMenu(menu: Menu) {} | ||
|
|
||
| // No op by default | ||
| fun handlePrepareMenu(menu: Menu) {} | ||
|
|
||
| fun handleMenuItemSelected(item: MenuItem): Boolean | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be able to use
addMenuProviderinDebugMaterialThemeActivity