Implement device volume control actions

using the intent data `launcher:volumeUp` and `launcher:volumeDown`.

Related: #68
This commit is contained in:
Finn M Glas 2020-10-12 17:04:55 +02:00
parent 0957f5bff1
commit 9942b021a8
No known key found for this signature in database
GPG key ID: 902A30146014DFBF
5 changed files with 75 additions and 16 deletions

View file

@ -8,6 +8,7 @@ import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.graphics.*
import android.media.AudioManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
@ -30,6 +31,7 @@ import com.finnmglas.launcher.settings.intendedSettingsPause
import com.finnmglas.launcher.tutorial.TutorialActivity
import kotlin.math.roundToInt
/* Preferences (global, initialised when app is started) */
lateinit var launcherPreferences: SharedPreferences
@ -154,8 +156,10 @@ fun View.fadeRotateIn(duration: Long = 500L) {
}
)
combined.addAnimation(
RotateAnimation(0F, 180F, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF,0.5f).also {
RotateAnimation(
0F, 180F, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f
).also {
it.duration = duration * 2
it.interpolator = DecelerateInterpolator()
}
@ -173,8 +177,10 @@ fun View.fadeRotateOut(duration: Long = 500L) {
}
)
combined.addAnimation(
RotateAnimation(0F, 180F, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF,0.5f).also {
RotateAnimation(
0F, 180F, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f
).also {
it.duration = duration
it.interpolator = AccelerateInterpolator()
}
@ -202,13 +208,17 @@ private fun getIntent(packageName: String, context: Context): Intent? {
return intent
}
fun launch(data: String, activity: Activity,
animationIn: Int = android.R.anim.fade_in, animationOut: Int = android.R.anim.fade_out) {
fun launch(
data: String, activity: Activity,
animationIn: Int = android.R.anim.fade_in, animationOut: Int = android.R.anim.fade_out
) {
if (data.startsWith("launcher:")) // [type]:[info]
when(data.split(":")[1]) {
"settings" -> openSettings(activity)
"choose" -> openAppsList(activity)
"volumeUp" -> audioVolumeUp(activity)
"volumeDown" -> audioVolumeDown(activity)
"tutorial" -> openTutorial(activity)
}
else launchApp(data, activity) // app
@ -216,6 +226,28 @@ fun launch(data: String, activity: Activity,
activity.overridePendingTransition(animationIn, animationOut)
}
fun audioVolumeUp(activity: Activity) {
val audioManager =
activity.getSystemService(Context.AUDIO_SERVICE) as AudioManager
audioManager.adjustStreamVolume(
AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE,
AudioManager.FLAG_SHOW_UI
)
}
fun audioVolumeDown(activity: Activity) {
val audioManager =
activity.getSystemService(Context.AUDIO_SERVICE) as AudioManager
audioManager.adjustStreamVolume(
AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER,
AudioManager.FLAG_SHOW_UI
)
}
fun launchApp(packageName: String, context: Context) {
val intent = getIntent(packageName, context)
@ -224,7 +256,8 @@ fun launchApp(packageName: String, context: Context) {
} else {
if (isInstalled(packageName, context)){
AlertDialog.Builder(context,
AlertDialog.Builder(
context,
R.style.AlertDialogCustom
)
.setTitle(context.getString(R.string.alert_cant_open_title))
@ -240,12 +273,16 @@ fun launchApp(packageName: String, context: Context) {
.setIcon(android.R.drawable.ic_dialog_info)
.show()
} else {
Toast.makeText( context, context.getString(R.string.toast_cant_open_message), Toast.LENGTH_SHORT).show()
Toast.makeText(
context,
context.getString(R.string.toast_cant_open_message),
Toast.LENGTH_SHORT
).show()
}
}
}
fun openNewTabWindow(urls: String, context : Context) {
fun openNewTabWindow(urls: String, context: Context) {
val uris = Uri.parse(urls)
val intents = Intent(Intent.ACTION_VIEW, uris)
val b = Bundle()
@ -256,11 +293,11 @@ fun openNewTabWindow(urls: String, context : Context) {
/* Settings related functions */
fun getSavedTheme(context : Context) : String {
fun getSavedTheme(context: Context) : String {
return launcherPreferences.getString(PREF_THEME, "finn").toString()
}
fun saveTheme(themeName : String) : String {
fun saveTheme(themeName: String) : String {
launcherPreferences.edit()
.putString(PREF_THEME, themeName)
.apply()
@ -302,7 +339,7 @@ fun resetToDarkTheme(activity: Activity) {
}
fun openAppSettings(pkg :String, context:Context) {
fun openAppSettings(pkg: String, context: Context) {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
intent.data = Uri.parse("package:$pkg")
context.startActivity(intent)
@ -399,14 +436,18 @@ fun setWindowFlags(window: Window) {
// Display notification bar
if (launcherPreferences.getBoolean(PREF_SCREEN_FULLSCREEN, true))
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN)
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
else window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
// Screen Timeout
if (launcherPreferences.getBoolean(PREF_SCREEN_TIMEOUT_DISABLED, false))
window.setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
window.setFlags(
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
)
else window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}

View file

@ -66,6 +66,14 @@ class OtherRecyclerAdapter(val activity: Activity):
OtherInfo(activity.getString(R.string.list_other_list),
"launcher:choose",
activity.getString(R.string.fas_bars)))
othersList.add(
OtherInfo(activity.getString(R.string.list_other_volume_up),
"launcher:volumeUp",
activity.getString(R.string.fas_plus)))
othersList.add(
OtherInfo(activity.getString(R.string.list_other_volume_down),
"launcher:volumeDown",
activity.getString(R.string.fas_minus)))
}
/* */

View file

@ -104,6 +104,10 @@ class ActionsRecyclerAdapter(val activity: Activity):
viewHolder.fontAwesome.text = activity.getString(R.string.fas_settings)
"choose" ->
viewHolder.fontAwesome.text = activity.getString(R.string.fas_bars)
"volumeUp" ->
viewHolder.fontAwesome.text = activity.getString(R.string.fas_plus)
"volumeDown" ->
viewHolder.fontAwesome.text = activity.getString(R.string.fas_minus)
}
} else {
// Set image icon (by packageName)

View file

@ -13,6 +13,10 @@
<string name="fas_trash" translatable="false">&#xf1f8;</string>
<string name="fas_times" translatable="false">&#xf00d;</string> <!-- 'close' -->
<string name="fas_three_dots" translatable="false">&#xf142;</string> <!-- 'ellipsis-v' -->
<string name="fas_volume_down" translatable="false">&#xf027;</string>
<string name="fas_volume_up" translatable="false">&#xf028;</string>
<string name="fas_plus" translatable="false">&#xf067;</string>
<string name="fas_minus" translatable="false">&#xf068;</string>
<string name="fas_angle_double_left" translatable="false">&#xf100;</string>
<string name="fas_angle_double_right" translatable="false">&#xf101;</string>

View file

@ -144,6 +144,8 @@
<string name="list_other_settings">Launcher Settings</string>
<string name="list_other_list">Launcher AppsList</string>
<string name="list_other_volume_up">Increase Volume</string>
<string name="list_other_volume_down">Decrease Volume</string>
<!--
-