Skip to content

Commit

Permalink
Add option to ignore focused apps
Browse files Browse the repository at this point in the history
Related to #2
  • Loading branch information
Domi04151309 committed Jul 14, 2022
1 parent e20192e commit 592d9e1
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,35 @@ object AppHelper {

private fun hibernateApps(c: Context, playingMusicPackage: String?) {
val appArray = JSONArray(
PreferenceManager.getDefaultSharedPreferences(c)
.getString(P.PREF_APP_LIST, P.PREF_APP_LIST_DEFAULT)
PreferenceManager.getDefaultSharedPreferences(c).getString(
P.PREF_APP_LIST, P.PREF_APP_LIST_DEFAULT
)
)
val forcedSet = ForcedSet(PreferenceManager.getDefaultSharedPreferences(c))
val commandArray: ArrayList<String> = ArrayList(appArray.length() / 2)
val services = Root.getServices()
val focused = if (
PreferenceManager.getDefaultSharedPreferences(c).getBoolean(
P.PREF_IGNORE_FOCUSED_APPS, P.PREF_IGNORE_FOCUSED_APPS_DEFAULT
)
) Root.getFocusedApps() else PseudoHashSet()
for (i in 0 until appArray.length()) {
try {
val packageName = appArray.getString(i)
if (
!packageName.equals(playingMusicPackage)
&& !focused.contains(packageName)
&& (c.packageManager.getApplicationInfo(
packageName,
PackageManager.GET_META_DATA
).flags and ApplicationInfo.FLAG_STOPPED == 0)
&& (services.contains(packageName) || forcedSet.contains(packageName))
) {
commandArray.add("am force-stop $packageName")
}
) commandArray.add("am force-stop $packageName")
} catch (e: PackageManager.NameNotFoundException) {
continue
}
}
if (commandArray.isNotEmpty()) Root.shell(commandArray.toArray(arrayOf<String>()))
if (commandArray.isNotEmpty()) Root.shell(commandArray.toTypedArray())
}

internal fun hibernate(c: Context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ object P {
internal const val PREF_AGGRESSIVE_DOZE: String = "aggressive_doze"
internal const val PREF_AGGRESSIVE_DOZE_DELAY: String = "aggressive_doze_delay"
internal const val PREF_THEME: String = "theme"
internal const val PREF_IGNORE_FOCUSED_APPS: String = "ignore_focused_apps"
internal const val PREF_ALLOW_MUSIC: String = "allow_music"
internal const val PREF_APP_LIST_DEFAULT: String = "[]"
internal const val PREF_FORCED_LIST_DEFAULT: String = "[]"
internal const val PREF_AUTO_STOP_DEFAULT: Boolean = true
internal const val PREF_AUTO_STOP_DELAY_DEFAULT: Int = 10
internal const val PREF_AGGRESSIVE_DOZE_DEFAULT: Boolean = false
internal const val PREF_IGNORE_FOCUSED_APPS_DEFAULT: Boolean = false
internal const val PREF_ALLOW_MUSIC_DEFAULT: Boolean = false
internal const val PREF_AGGRESSIVE_DOZE_DELAY_DEFAULT: Int = 60 * 10
internal const val PREF_THEME_DEFAULT: String = "auto"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.github.domi04151309.batterytool.helpers

class PseudoHashSet : HashSet<String>() {
override fun contains(element: String): Boolean = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,32 @@ internal object Root {
}
}

fun getFocusedApps(): HashSet<String> {
var result = ""
try {
Runtime.getRuntime().exec(
arrayOf(
"su",
"-c",
"dumpsys activity activities | grep mResumedActivity | cut -d '{' -f2 | cut -d ' ' -f3 | cut -d '/' -f1"
)
).inputStream.use { inputStream ->
Scanner(inputStream).useDelimiter("\\A").use { s ->
result = if (s.hasNext()) s.next() else ""
}
}
} catch (e: Exception) {
Log.e("Superuser", e.toString())
}
return parseFocusedApps(result)
}

private fun parseFocusedApps(services: String): HashSet<String> {
val set = HashSet<String>()
for (line in services.lines()) set.add(line)
return set
}

fun getServices(): HashSet<String> {
var result = ""
try {
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_focus.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="?android:attr/textColorSecondary" android:pathData="M5,15L3,15v4c0,1.1 0.9,2 2,2h4v-2L5,19v-4zM5,5h4L9,3L5,3c-1.1,0 -2,0.9 -2,2v4h2L5,5zM19,3h-4v2h4v4h2L21,5c0,-1.1 -0.9,-2 -2,-2zM19,19h-4v2h4c1.1,0 2,-0.9 2,-2v-4h-2v4zM12,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3 3,-1.34 3,-3 -1.34,-3 -3,-3z"/>
</vector>
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<string name="main_empty_summary">It seems that no app falls into this category</string>
<string name="main_forced">(forced)</string>

<string name="pref_allow_music_apps">Whitelist music app</string>
<string name="pref_allow_music_apps">Ignore music app</string>
<string name="pref_allow_music_apps_summary">Don\'t stop currently playing music app</string>
<string name="notifications_permission">Notifications Permission</string>
<string name="notifications_permission_explanation">To detect Apps which are playing music this app needs to read currently showing notifications, and thus needs the permission</string>
Expand Down Expand Up @@ -52,6 +52,8 @@
<string name="pref_aggressive_doze">Aggressive doze</string>
<string name="pref_aggressive_doze_summary">Enter doze mode after the screen turns off</string>
<string name="pref_aggressive_doze_delay">Aggressive doze delay</string>
<string name="pref_ignore_focused_app">Ignore focused apps</string>
<string name="pref_ignore_focused_app_summary">Don\'t stop currently focused apps</string>
<string name="pref_theme">Change theme</string>
<string name="pref_theme_summary">Changes the look of the app</string>

Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml/pref_general.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
android:key="aggressive_doze_delay"
android:summary="@string/pref_aggressive_doze_delay"
android:title="@string/pref_aggressive_doze_delay" />
<SwitchPreference
android:defaultValue="false"
android:icon="@drawable/ic_focus"
android:key="ignore_focused_apps"
android:summary="@string/pref_ignore_focused_app_summary"
android:title="@string/pref_ignore_focused_app" />
<SwitchPreference
android:defaultValue="false"
android:icon="@drawable/ic_music"
Expand Down

0 comments on commit 592d9e1

Please sign in to comment.