Skip to content

Commit 9840233

Browse files
committed
Somes fixes
1 parent aca87af commit 9840233

10 files changed

Lines changed: 68 additions & 105 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@
472472

473473
<activity
474474
android:name=".widget.WidgetConfigureActivity"
475-
android:exported="false">
475+
android:exported="true">
476476
<intent-filter>
477477
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
478478
</intent-filter>

app/src/main/java/com/wstxda/switchai/fragment/SettingsFragment.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.os.Build
55
import android.os.Bundle
66
import androidx.activity.result.contract.ActivityResultContracts
77
import androidx.core.net.toUri
8+
import androidx.fragment.app.activityViewModels
89
import androidx.fragment.app.viewModels
910
import androidx.lifecycle.ViewModelProvider
1011
import androidx.lifecycle.lifecycleScope
@@ -30,10 +31,7 @@ import kotlinx.coroutines.launch
3031

3132
class SettingsFragment : PreferenceFragmentCompat() {
3233

33-
private val viewModel: SettingsViewModel by viewModels {
34-
ViewModelProvider.AndroidViewModelFactory.getInstance(requireActivity().application)
35-
}
36-
34+
private val viewModel: SettingsViewModel by activityViewModels()
3735
private val assistantSelectorViewModel: AssistantSelectorViewModel by viewModels {
3836
ViewModelProvider.AndroidViewModelFactory.getInstance(requireActivity().application)
3937
}

app/src/main/java/com/wstxda/switchai/fragment/preferences/DigitalAssistantPreference.kt

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
11
package com.wstxda.switchai.fragment.preferences
22

33
import android.content.Context
4-
import android.os.Build
54
import androidx.core.content.edit
65
import androidx.preference.PreferenceFragmentCompat
7-
import com.wstxda.switchai.logic.PreferenceHelper
6+
import com.wstxda.switchai.logic.DigitalAssistantChecker
87
import com.wstxda.switchai.utils.Constants
98

109
class DigitalAssistantPreference(private val fragment: PreferenceFragmentCompat) {
1110

1211
private val context: Context get() = fragment.requireContext()
13-
private val preferenceHelper by lazy { PreferenceHelper(context) }
1412

15-
fun checkDigitalAssistSetupStatus(): Boolean =
16-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
17-
context.getSystemService(android.app.role.RoleManager::class.java)
18-
?.isRoleHeld(android.app.role.RoleManager.ROLE_ASSISTANT) == true
19-
} else {
20-
preferenceHelper.getBoolean(Constants.IS_ASSIST_SETUP_DONE, false)
21-
}
13+
fun checkDigitalAssistSetupStatus(): Boolean = DigitalAssistantChecker.isSetupDone(context)
2214

2315
fun setDigitalAssistSetupStatus(isSetupDone: Boolean) {
2416
val prefs = androidx.preference.PreferenceManager.getDefaultSharedPreferences(context)
25-
prefs.edit {
26-
putBoolean(Constants.IS_ASSIST_SETUP_DONE, isSetupDone)
27-
}
17+
prefs.edit { putBoolean(Constants.IS_ASSIST_SETUP_DONE, isSetupDone) }
2818
}
2919

3020
fun updateDigitalAssistantPreferences(isAssistSetupDone: Boolean) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.wstxda.switchai.logic
2+
3+
import android.app.role.RoleManager
4+
import android.content.Context
5+
import android.os.Build
6+
import com.wstxda.switchai.utils.Constants
7+
8+
object DigitalAssistantChecker {
9+
fun isSetupDone(context: Context): Boolean =
10+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
11+
context.getSystemService(RoleManager::class.java)
12+
?.isRoleHeld(RoleManager.ROLE_ASSISTANT) == true
13+
} else {
14+
PreferenceHelper(context).getBoolean(Constants.IS_ASSIST_SETUP_DONE, false)
15+
}
16+
}

app/src/main/java/com/wstxda/switchai/logic/RootChecker.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import kotlinx.coroutines.withContext
77

88
suspend fun isRootAvailable(): Boolean = withContext(Dispatchers.IO) {
99
runCatching {
10-
val process = ProcessBuilder("su", "-c", "id").start()
11-
val result = process.waitFor() == 0
12-
process.destroy()
13-
result
10+
val process = ProcessBuilder("su", "-c", "id").redirectErrorStream(true).start()
11+
12+
process.inputStream.bufferedReader().readText()
13+
process.waitFor() == 0
1414
}.getOrElse {
1515
Log.e(Constants.ROOT_CHECKER, "Root check failed", it)
1616
false
@@ -22,12 +22,12 @@ suspend fun launchRootActivity(packageName: String, activityName: String): Boole
2222
runCatching {
2323
val process = ProcessBuilder(
2424
"su", "-c", "am", "start", "-n", "$packageName/$activityName"
25-
).start()
26-
val result = process.waitFor() == 0
27-
process.destroy()
28-
result
25+
).redirectErrorStream(true).start()
26+
27+
process.inputStream.bufferedReader().readText()
28+
process.waitFor() == 0
2929
}.getOrElse {
3030
Log.e(Constants.ROOT_CHECKER, "Failed to launch root activity", it)
3131
false
3232
}
33-
}
33+
}

app/src/main/java/com/wstxda/switchai/services/AssistantTileService.kt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package com.wstxda.switchai.services
22

33
import android.annotation.SuppressLint
44
import android.app.PendingIntent
5-
import android.app.role.RoleManager
65
import android.content.Intent
76
import android.os.Build
87
import android.service.quicksettings.Tile
98
import android.service.quicksettings.TileService
109
import androidx.core.graphics.drawable.IconCompat
1110
import com.wstxda.switchai.R
11+
import com.wstxda.switchai.logic.DigitalAssistantChecker
1212
import com.wstxda.switchai.logic.PreferenceHelper
1313
import com.wstxda.switchai.ui.utils.AssistantResourcesManager
1414
import com.wstxda.switchai.utils.Constants
@@ -47,13 +47,7 @@ class AssistantTileService : TileService() {
4747
private fun refreshTileContent() {
4848
val tile = qsTile ?: return
4949

50-
val isSetupDone = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
51-
getSystemService(RoleManager::class.java)?.isRoleHeld(RoleManager.ROLE_ASSISTANT) == true
52-
} else {
53-
preferenceHelper.getBoolean(Constants.IS_ASSIST_SETUP_DONE, false)
54-
}
55-
56-
if (!isSetupDone) {
50+
if (!DigitalAssistantChecker.isSetupDone(this)) {
5751
tile.state = Tile.STATE_UNAVAILABLE
5852
tile.updateTile()
5953
return

app/src/main/java/com/wstxda/switchai/ui/ShortcutManager.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,5 @@ class ShortcutManager(private val context: Context) {
8787
return IconCompat.createWithAdaptiveBitmap(bitmap)
8888
}
8989

90-
private val dp108: Int
91-
get() = (108 * context.resources.displayMetrics.density).toInt()
90+
private val dp108: Int = (108 * context.resources.displayMetrics.density).toInt()
9291
}
Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.wstxda.switchai.ui.utils
22

3-
import android.app.NotificationManager
43
import android.content.Context
5-
import android.media.AudioManager
4+
import android.media.AudioAttributes
65
import android.media.MediaPlayer
76
import com.wstxda.switchai.R
87
import com.wstxda.switchai.logic.PreferenceHelper
98
import com.wstxda.switchai.utils.Constants
9+
import androidx.core.net.toUri
1010

1111
object SoundService {
1212

@@ -16,32 +16,24 @@ object SoundService {
1616
}
1717

1818
private fun Context.performSound() {
19-
val mediaPlayer = MediaPlayer.create(this, R.raw.open_sound) ?: return
20-
mediaPlayer.setOnCompletionListener { it.release() }
21-
mediaPlayer.setOnErrorListener { mp, _, _ ->
22-
mp.release()
23-
true
19+
val usage = AudioAttributes.USAGE_ASSISTANT
20+
21+
val attrs = AudioAttributes.Builder().setUsage(usage)
22+
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).build()
23+
24+
MediaPlayer().apply {
25+
setAudioAttributes(attrs)
26+
setDataSource(
27+
this@performSound, "android.resource://${packageName}/${R.raw.open_sound}".toUri()
28+
)
29+
setOnPreparedListener { it.start() }
30+
setOnCompletionListener { it.release() }
31+
setOnErrorListener { mp, _, _ -> mp.release(); true }
32+
prepareAsync()
2433
}
25-
mediaPlayer.start()
2634
}
2735

2836
private fun Context.canPlaySound(): Boolean {
29-
val preferenceHelper = PreferenceHelper(this)
30-
31-
val isAppSoundEnabled =
32-
preferenceHelper.getBoolean(Constants.ASSISTANT_SOUND_PREF_KEY, false)
33-
if (!isAppSoundEnabled) {
34-
return false
35-
}
36-
37-
val audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
38-
if (audioManager.ringerMode != AudioManager.RINGER_MODE_NORMAL) {
39-
return false
40-
}
41-
42-
val notificationManager =
43-
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
44-
val currentFilter = notificationManager.currentInterruptionFilter
45-
return currentFilter == NotificationManager.INTERRUPTION_FILTER_ALL
37+
return PreferenceHelper(this).getBoolean(Constants.ASSISTANT_SOUND_PREF_KEY, false)
4638
}
4739
}
Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package com.wstxda.switchai.ui.utils
22

3-
import android.app.NotificationManager
43
import android.content.Context
54
import android.media.AudioManager
65
import android.os.Build
7-
import android.os.PowerManager
86
import android.os.VibrationAttributes
97
import android.os.VibrationEffect
108
import android.os.Vibrator
@@ -13,9 +11,6 @@ import com.wstxda.switchai.utils.Constants
1311

1412
object VibrationService {
1513

16-
private const val EFFECT_CLICK = 1
17-
private const val EFFECT_TICK = 2
18-
1914
fun Context.openAssistantVibration() {
2015
if (!isVibrationAllowed()) return
2116
performVibration(effectId = getEffectClick(), fallbackDuration = 12)
@@ -26,15 +21,11 @@ object VibrationService {
2621
performVibration(effectId = getEffectTick(), fallbackDuration = 8)
2722
}
2823

29-
private fun Context.performVibration(
30-
effectId: Int,
31-
fallbackDuration: Long,
32-
) {
24+
private fun Context.performVibration(effectId: Int, fallbackDuration: Long) {
3325
val vibrator = getSystemService(Vibrator::class.java) ?: return
3426

3527
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
3628
val effect = VibrationEffect.createPredefined(effectId)
37-
3829
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
3930
val attrs =
4031
VibrationAttributes.Builder().setUsage(VibrationAttributes.USAGE_TOUCH).build()
@@ -43,32 +34,20 @@ object VibrationService {
4334
vibrator.vibrate(effect)
4435
}
4536
} else {
46-
val effect = VibrationEffect.createOneShot(
47-
fallbackDuration, VibrationEffect.DEFAULT_AMPLITUDE
37+
vibrator.vibrate(
38+
VibrationEffect.createOneShot(fallbackDuration, VibrationEffect.DEFAULT_AMPLITUDE)
4839
)
49-
vibrator.vibrate(effect)
5040
}
5141
}
5242

53-
private fun getEffectClick(): Int {
54-
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
55-
VibrationEffect.EFFECT_CLICK
56-
} else {
57-
EFFECT_CLICK
58-
}
59-
}
43+
private fun getEffectClick(): Int =
44+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) VibrationEffect.EFFECT_CLICK else 1
6045

61-
private fun getEffectTick(): Int {
62-
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
63-
VibrationEffect.EFFECT_TICK
64-
} else {
65-
EFFECT_TICK
66-
}
67-
}
46+
private fun getEffectTick(): Int =
47+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) VibrationEffect.EFFECT_TICK else 2
6848

6949
private fun Context.isVibrationAllowed(): Boolean {
70-
val preferenceHelper = PreferenceHelper(this)
71-
if (!preferenceHelper.getBoolean(Constants.ASSISTANT_VIBRATION_PREF_KEY, true)) {
50+
if (!PreferenceHelper(this).getBoolean(Constants.ASSISTANT_VIBRATION_PREF_KEY, true)) {
7251
return false
7352
}
7453

@@ -77,19 +56,7 @@ object VibrationService {
7756
return false
7857
}
7958

80-
val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
81-
if (powerManager.isPowerSaveMode) {
82-
return false
83-
}
84-
8559
val audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
86-
if (audioManager.ringerMode == AudioManager.RINGER_MODE_SILENT) {
87-
return false
88-
}
89-
90-
val notificationManager =
91-
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
92-
val currentFilter = notificationManager.currentInterruptionFilter
93-
return currentFilter == NotificationManager.INTERRUPTION_FILTER_ALL
60+
return audioManager.ringerMode != AudioManager.RINGER_MODE_SILENT
9461
}
9562
}

app/src/main/java/com/wstxda/switchai/viewmodel/AssistantSelectorViewModel.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.content.Context
66
import android.content.Intent
77
import android.content.IntentFilter
88
import android.content.SharedPreferences
9+
import android.os.Build
910
import androidx.core.content.edit
1011
import androidx.lifecycle.AndroidViewModel
1112
import androidx.lifecycle.LiveData
@@ -85,7 +86,13 @@ class AssistantSelectorViewModel(application: Application) : AndroidViewModel(ap
8586
addAction(Intent.ACTION_PACKAGE_REMOVED)
8687
addDataScheme("package")
8788
}
88-
getApplication<Application>().registerReceiver(packageChangeReceiver, intentFilter)
89+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
90+
getApplication<Application>().registerReceiver(
91+
packageChangeReceiver, intentFilter, Context.RECEIVER_NOT_EXPORTED
92+
)
93+
} else {
94+
getApplication<Application>().registerReceiver(packageChangeReceiver, intentFilter)
95+
}
8996

9097
loadAssistants()
9198
}

0 commit comments

Comments
 (0)