Simplify selection of default apps

The lists / priorities can now be changed in the `strings.xml` file.
This commit is contained in:
Finn M Glas 2020-05-19 10:59:08 +02:00
parent 49baed4d65
commit 83299d926f
No known key found for this signature in database
GPG key ID: 25037A2E81AB459C
2 changed files with 95 additions and 87 deletions

View file

@ -1,5 +1,6 @@
package com.finnmglas.launcher package com.finnmglas.launcher
import android.app.Activity
import android.app.AlertDialog import android.app.AlertDialog
import android.content.Context import android.content.Context
import android.content.DialogInterface import android.content.DialogInterface
@ -10,6 +11,20 @@ import android.net.Uri
import android.provider.Settings import android.provider.Settings
import android.widget.Toast import android.widget.Toast
/** Parsing functions */
// Related question: https://stackoverflow.com/q/3013655/12787264
fun parseStringMap(stringArrayResourceId: Int, context: Context): HashMap<String, String>? {
val stringArray: Array<String> =
context.resources.getStringArray(stringArrayResourceId)
val outputArray = HashMap<String, String>(stringArray.size)
for (entry in stringArray) {
val splitResult = entry.split("|").toTypedArray()
outputArray.put(splitResult[0], splitResult[1])
}
return outputArray
}
/** Activity related */ /** Activity related */
fun isInstalled(uri: String, context: Context): Boolean { fun isInstalled(uri: String, context: Context): Boolean {
@ -32,13 +47,16 @@ fun launchApp(packageName: String, context: Context) {
if (intent1 != null) { if (intent1 != null) {
context.startActivity(intent1) context.startActivity(intent1)
//overridePendingTransition(0, 0)
if (context is Activity) {
context.overridePendingTransition(0, 0)
}
} else { } else {
if (isInstalled(packageName, context)){ if (isInstalled(packageName, context)){
AlertDialog.Builder(context) AlertDialog.Builder(context)
.setTitle("Can't open app") .setTitle(context.getString(R.string.alert_cant_open_title))
.setMessage("Want to change its settings ('add it to the apps screen')?") .setMessage(context.getString(R.string.alert_cant_open_message))
.setPositiveButton(android.R.string.yes, .setPositiveButton(android.R.string.yes,
DialogInterface.OnClickListener { dialog, which -> DialogInterface.OnClickListener { dialog, which ->
openAppSettings(packageName, context) openAppSettings(packageName, context)
@ -47,12 +65,12 @@ fun launchApp(packageName: String, context: Context) {
.setIcon(android.R.drawable.ic_dialog_info) .setIcon(android.R.drawable.ic_dialog_info)
.show() .show()
} else { } else {
Toast.makeText( context, "Open settings to choose an app for this action", Toast.LENGTH_SHORT).show() Toast.makeText( context, context.getString(R.string.toast_cant_open_message), Toast.LENGTH_SHORT).show()
} }
} }
} }
/** Settings related */ /** Settings related functions */
fun openAppSettings(pkg :String, context:Context){ fun openAppSettings(pkg :String, context:Context){
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
@ -73,116 +91,56 @@ fun loadSettings(sharedPref : SharedPreferences){
} }
fun resetSettings(sharedPref : SharedPreferences, context: Context) : MutableList<String>{ fun resetSettings(sharedPref : SharedPreferences, context: Context) : MutableList<String>{
val defaultList :MutableList<String> = mutableListOf<String>() val defaultList :MutableList<String> = mutableListOf<String>()
val editor: SharedPreferences.Editor = sharedPref.edit() val editor: SharedPreferences.Editor = sharedPref.edit()
val (chosenUpName, chosenUpPackage) = pickDefaultUpApp(context) val (chosenUpName, chosenUpPackage) = pickDefaultApp("action_upApp", context)
editor.putString("action_upApp", chosenUpPackage) editor.putString("action_upApp", chosenUpPackage)
defaultList.add(chosenUpName) defaultList.add(chosenUpName)
val (chosenDownName, chosenDownPackage) = pickDefaultDownApp(context) val (chosenDownName, chosenDownPackage) = pickDefaultApp("action_downApp", context)
editor.putString("action_downApp", chosenDownPackage) editor.putString("action_downApp", chosenDownPackage)
defaultList.add(chosenDownName) defaultList.add(chosenDownName)
val (chosenRightName, chosenRightPackage) = pickDefaultRightApp(context) val (chosenRightName, chosenRightPackage) = pickDefaultApp("action_rightApp", context)
editor.putString("action_rightApp", chosenRightPackage) editor.putString("action_rightApp", chosenRightPackage)
defaultList.add(chosenRightName) defaultList.add(chosenRightName)
val (chosenLeftName, chosenLeftPackage) = pickDefaultLeftApp(context) val (chosenLeftName, chosenLeftPackage) = pickDefaultApp("action_leftApp", context)
editor.putString("action_leftApp", chosenLeftPackage) editor.putString("action_leftApp", chosenLeftPackage)
editor.putString("action_calendarApp", chosenLeftPackage) editor.putString("action_calendarApp", chosenLeftPackage)
defaultList.add(chosenLeftName) defaultList.add(chosenLeftName)
val (chosenVolumeUpName, chosenVolumeUpPackage) = pickDefaultVolumeUpApp(context) val (chosenVolumeUpName, chosenVolumeUpPackage) = pickDefaultApp("action_volumeUpApp", context)
editor.putString("action_volumeUpApp", chosenVolumeUpPackage) editor.putString("action_volumeUpApp", chosenVolumeUpPackage)
defaultList.add(chosenVolumeUpName) defaultList.add(chosenVolumeUpName)
val (chosenVolumeDownName, chosenVolumeDownPackage) = pickDefaultVolumeDownApp(context) val (chosenVolumeDownName, chosenVolumeDownPackage) = pickDefaultApp("action_volumeDownApp", context)
editor.putString("action_volumeDownApp", chosenVolumeDownPackage) editor.putString("action_volumeDownApp", chosenVolumeDownPackage)
defaultList.add(chosenVolumeDownName) defaultList.add(chosenVolumeDownName)
// clockApp default val (_, chosenClockPackage) = pickDefaultApp("action_clockApp", context)
editor.putString("action_clockApp", "com.sec.android.app.clockpackage") editor.putString("action_clockApp", chosenClockPackage)
editor.apply() editor.apply()
return defaultList // UP, DOWN, RIGHT, LEFT, VOLUME_UP, VOLUME_DOWN return defaultList // UP, DOWN, RIGHT, LEFT, VOLUME_UP, VOLUME_DOWN
} }
// Default upApps are Browsers fun pickDefaultApp(action: String, context: Context) : Pair<String, String>{
fun pickDefaultUpApp(context :Context) : Pair<String, String>{ val arrayResource = when (action) {
if(isInstalled("org.mozilla.firefox", context)) "action_upApp" -> R.array.default_up
return Pair("Firefox", "org.mozilla.firefox") "action_downApp" -> R.array.default_down
else if(isInstalled("com.android.chrome", context)) "action_rightApp" -> R.array.default_right
return Pair("Chrome", "com.android.chrome") "action_leftApp" -> R.array.default_left
else if(isInstalled("com.sec.android.app.sbrowser", context)) "action_volumeUpApp" -> R.array.default_volume_up
return Pair("Samsung Internet", "com.sec.android.app.sbrowser") "action_volumeDownApp" -> R.array.default_volume_down
else "action_clockApp" -> R.array.default_clock
return Pair(context.getString(R.string.none_found), "") else -> return Pair(context.getString(R.string.none_found), "") // just prevent crashing on unknown input
} }
// Default downApps are Internal Search Apps val defaultAppsMap = parseStringMap(arrayResource, context)
fun pickDefaultDownApp(context :Context) : Pair<String, String>{ for (item in defaultAppsMap!!) if (isInstalled(item.key, context)) return Pair(item.value, item.key)
if(isInstalled("com.samsung.android.app.galaxyfinder", context))
return Pair("GalaxyFinder", "com.samsung.android.app.galaxyfinder")
else if(isInstalled("com.prometheusinteractive.voice_launcher", context))
return Pair("VoiceSearch", "com.prometheusinteractive.voice_launcher")
else
return Pair(context.getString(R.string.none_found), "")
}
// Default rightApps are Mailing Applications
fun pickDefaultRightApp(context :Context) : Pair<String, String>{
if(isInstalled("de.web.mobile.android.mail", context))
return Pair("WebMail", "de.web.mobile.android.mail")
else if(isInstalled("com.samsung.android.email.provider", context))
return Pair("Samsung Mail", "com.samsung.android.email.provider")
else if(isInstalled("com.google.android.gm", context))
return Pair("Google Mail", "com.google.android.gm")
else
return Pair(context.getString(R.string.none_found), "")
}
// Default leftApps are Calendar Applications
fun pickDefaultLeftApp(context :Context) : Pair<String, String>{
if(isInstalled("com.google.android.calendar", context))
return Pair("Google Calendar", "com.google.android.calendar")
else if(isInstalled("com.samsung.android.calendar", context))
return Pair("Samsung Calendar", "com.samsung.android.calendar")
else
return Pair(context.getString(R.string.none_found), "")
}
// Default volumeUpApps are Messengers
fun pickDefaultVolumeUpApp(context: Context) : Pair<String, String>{
if(isInstalled("com.whatsapp", context))
return Pair("WhatsApp", "com.whatsapp")
else if(isInstalled("com.facebook.orca", context))
return Pair("Facebook Messenger", "com.facebook.orca")
else if(isInstalled("com.viber.voip", context))
return Pair("Viber", "com.viber.voip")
else if(isInstalled("com.skype.raider", context))
return Pair("Skype", "com.skype.raider")
else if(isInstalled("com.snapchat.android", context))
return Pair("Snapchat", "com.snapchat.android")
else if(isInstalled("com.instagram.android", context))
return Pair("Instagram", "com.instagram.android")
else if(isInstalled("com.samsung.android.messaging", context))
return Pair("Samsung SMS", "com.samsung.android.messaging")
else
return Pair(context.getString(R.string.none_found), "")
}
// Default volumeDownApps are Utilities
fun pickDefaultVolumeDownApp(context: Context) : Pair<String, String>{
if(isInstalled("com.github.android", context))
return Pair("GitHub", "com.github.android")
else if(isInstalled("com.soundbrenner.pulse", context))
return Pair("Soundbrenner Metronome", "com.soundbrenner.pulse")
else if(isInstalled("com.sec.android.app.popupcalculator", context))
return Pair("Calculator", "com.sec.android.app.popupcalculator")
else
return Pair(context.getString(R.string.none_found), "") return Pair(context.getString(R.string.none_found), "")
} }

View file

@ -1,6 +1,56 @@
<resources> <resources>
<!-- General -->
<string name="app_name">Launcher</string> <string name="app_name">Launcher</string>
<string name="preference_file_key">V3RYR4ND0MK3YCR4P</string> <string name="preference_file_key">V3RYR4ND0MK3YCR4P</string>
<!-- Errors, Exceptions (Alerts, Toasts ...) -->
<string name="none_found">None found</string> <string name="none_found">None found</string>
<string name="alert_cant_open_title">Can\'t open app</string>
<string name="alert_cant_open_message">Want to change its settings (\'add it to the apps screen\')?</string>
<string name="toast_cant_open_message">Open settings to choose an app for this action</string>
<!-- Default Apps for different actions (button-press, swipes ...) -->
<string-array name="default_up">
<item>org.mozilla.firefox|Firefox</item>
<item>com.sec.android.app.sbrowser|Samsung Internet</item>
<item>com.android.chrome|Chrome</item>
</string-array>
<string-array name="default_down">
<item>com.samsung.android.app.galaxyfinder|GalaxyFinder</item>
<item>com.prometheusinteractive.voice_launcher|VoiceSearch</item>
</string-array>
<string-array name="default_right">
<item>de.web.mobile.android.mail|WebMail</item>
<item>com.samsung.android.email.provider|Samsung Mail</item>
<item>com.google.android.gm|Google Mail</item>
</string-array>
<string-array name="default_left">
<item>com.google.android.calendar|Google Calendar</item>
<item>com.samsung.android.calendar|Samsung Calendar</item>
</string-array>
<string-array name="default_volume_up">
<item>com.whatsapp|WhatsApp</item>
<item>com.facebook.orca|Facebook Messenger</item>
<item>com.viber.voip|Viber</item>
<item>com.skype.raider|Skype</item>
<item>com.snapchat.android|Snapchat</item>
<item>com.instagram.android|Instagram</item>
<item>com.samsung.android.messaging|Samsung SMS</item>
</string-array>
<string-array name="default_volume_down">
<item>com.github.android|GitHub</item>
<item>com.soundbrenner.pulse|Soundbrenner Metronome</item>
<item>com.sec.android.app.popupcalculator|Calculator</item>
</string-array>
<string-array name="default_clock">
<item>com.sec.android.app.clockpackage|Android Clock</item>
</string-array>
</resources> </resources>