-
-
Notifications
You must be signed in to change notification settings - Fork 664
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace deprecated menu functions with MenuProvider (#3776)
* Squash merge Migrate to MenuProvider (#2974) by @NotWoods * Merge fixes * Update sensor detail fragment menu for multiserver * Menu fixes/additions - Add HelpMenuProvider to Android Auto favorites - Fix notification history fragment having no view because of missing super call - Fix crash in device controls view if a server that is offline is selected * ktlint * Simplify some code - Undo formatting change in SettingsActivity - Don't require each activity to convert the string to URIs if we can do it in one place * Remove setHasOptionsMenu(false) usage
- Loading branch information
Showing
27 changed files
with
489 additions
and
559 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
app/src/main/java/io/homeassistant/companion/android/settings/HelpMenuProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.homeassistant.companion.android.settings | ||
|
||
import android.content.Intent | ||
import android.content.Intent.ACTION_VIEW | ||
import android.net.Uri | ||
import android.view.Menu | ||
import android.view.MenuInflater | ||
import android.view.MenuItem | ||
import androidx.core.net.toUri | ||
import androidx.core.view.MenuHost | ||
import androidx.core.view.MenuProvider | ||
import androidx.fragment.app.Fragment | ||
import androidx.lifecycle.Lifecycle | ||
import io.homeassistant.companion.android.R | ||
|
||
/** | ||
* Adds a "Get Help" menu option that opens the given [helpLink] in the browser. | ||
*/ | ||
class HelpMenuProvider(private val helpLink: Uri) : MenuProvider { | ||
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) { | ||
menuInflater.inflate(R.menu.menu_help, menu) | ||
} | ||
|
||
override fun onPrepareMenu(menu: Menu) { | ||
menu.findItem(R.id.get_help).apply { | ||
intent = Intent(ACTION_VIEW, helpLink) | ||
} | ||
} | ||
|
||
// Don't handle the help item so the intent is automatically launched | ||
override fun onMenuItemSelected(menuItem: MenuItem) = false | ||
} | ||
|
||
/** | ||
* Wrapper around [MenuHost.addMenuProvider] to attach a [HelpMenuProvider] to a fragment's parent activity. | ||
*/ | ||
fun Fragment.addHelpMenuProvider(helpLink: String) { | ||
val menuHost: MenuHost = requireActivity() | ||
menuHost.addMenuProvider(HelpMenuProvider(helpLink.toUri()), viewLifecycleOwner, Lifecycle.State.RESUMED) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.