-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get the active app using events, not recent apps
This approach is more accurate (doesn't misreport when the notification shade is shown) and more efficient (events are already ordered so we don't need to sort them).
- Loading branch information
Showing
8 changed files
with
153 additions
and
168 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
71 changes: 71 additions & 0 deletions
71
app/src/main/java/com/jmstudios/redmoon/filter/overlay/CurrentAppChecker.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,71 @@ | ||
/* | ||
* Copyright (c) 2016 Marien Raat <marienraat@riseup.net> | ||
* Copyright (c) 2017 Stephen Michel <s@smichel.me> | ||
* SPDX-License-Identifier: GPL-3.0+ | ||
*/ | ||
package com.jmstudios.redmoon.filter.overlay | ||
|
||
import android.annotation.TargetApi | ||
import android.app.ActivityManager | ||
import android.app.usage.UsageEvents | ||
import android.app.usage.UsageStatsManager | ||
import android.content.Context | ||
import android.content.ContextWrapper | ||
|
||
import com.jmstudios.redmoon.util.* | ||
|
||
class CurrentAppChecker(private val context: Context) { | ||
val isWorking: Boolean = currentApp != "" | ||
|
||
val currentApp: String | ||
// http://stackoverflow.com/q/33581311 | ||
get() = try { | ||
if (atLeastAPI(21)) { | ||
currentAppFromUsageStats | ||
} else { | ||
currentAppFromActivityManager | ||
} | ||
} catch (e: Exception) { | ||
"" | ||
} | ||
|
||
private val currentAppFromUsageStats: String | ||
@TargetApi(21) get() { | ||
// Although the UsageStatsManager was added in API 21, the | ||
// constant to specify it wasn't added until API 22. | ||
// So we use the value of that constant on API 21. | ||
val uss = if (belowAPI(22)) "usagestats" else @TargetApi(22) { | ||
Context.USAGE_STATS_SERVICE | ||
} | ||
val usm = context.getSystemService(uss) as UsageStatsManager | ||
val time = System.currentTimeMillis() | ||
// Only look at events in the past 10 seconds | ||
val eventList = usm.queryEvents(time - 1000 * 10, time) | ||
val event = UsageEvents.Event() | ||
|
||
tailrec fun findLastApp(app: String): String { | ||
return if (eventList.hasNextEvent()) { | ||
eventList.getNextEvent(event) | ||
if (event.className == null) { | ||
findLastApp(app) | ||
} else { | ||
findLastApp(event.packageName) | ||
} | ||
} else app | ||
} | ||
|
||
lastApp = findLastApp(lastApp) | ||
return lastApp | ||
} | ||
|
||
private val currentAppFromActivityManager: String | ||
@Suppress("DEPRECATION") get() { | ||
val bc = ContextWrapper(context).baseContext | ||
val am = bc.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager | ||
return am.getRunningTasks(1)[0].topActivity.packageName | ||
} | ||
|
||
companion object: Logger() { | ||
private var lastApp: String = "" | ||
} | ||
} |
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
117 changes: 0 additions & 117 deletions
117
app/src/main/java/com/jmstudios/redmoon/filter/overlay/CurrentAppMonitoringThread.kt
This file was deleted.
Oops, something went wrong.
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.