-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto finish secure activities when the screen goes off
(and the user is inactive for a certain short duration)
- Loading branch information
1 parent
37990f8
commit b571fef
Showing
5 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
app/src/main/java/app/grapheneos/camera/AutoFinishOnSleep.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,53 @@ | ||
package app.grapheneos.camera | ||
|
||
import android.app.Activity | ||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.IntentFilter | ||
import android.os.Handler | ||
import android.os.Looper | ||
import androidx.core.app.ActivityCompat | ||
|
||
// Finishes the passed [activity] and the ones present below it in the stack if the screen | ||
// turns off and the user is inactive for [WAITING_TIME_IN_MS] milliseconds | ||
class AutoFinishOnSleep(val activity: Activity) { | ||
|
||
companion object { | ||
private const val TAG = "AutoFinishOnSleep" | ||
private const val WAITING_TIME_IN_MS = 1500L | ||
|
||
private val intentFilter = IntentFilter().apply { | ||
addAction(Intent.ACTION_SCREEN_ON) | ||
addAction(Intent.ACTION_SCREEN_OFF) | ||
} | ||
} | ||
|
||
private val handler = Handler(Looper.getMainLooper()) | ||
|
||
private val runnable = Runnable { | ||
ActivityCompat.finishAffinity(activity) | ||
} | ||
|
||
private val receiver = object: BroadcastReceiver() { | ||
override fun onReceive(context: Context?, intent: Intent?) { | ||
when (intent?.action) { | ||
Intent.ACTION_SCREEN_OFF -> { | ||
handler.postDelayed(runnable, WAITING_TIME_IN_MS) | ||
} | ||
|
||
Intent.ACTION_SCREEN_ON -> { | ||
handler.removeCallbacks(runnable) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun start() { | ||
activity.registerReceiver(receiver, intentFilter) | ||
} | ||
|
||
fun stop() { | ||
activity.unregisterReceiver(receiver) | ||
} | ||
} |
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
18 changes: 17 additions & 1 deletion
18
app/src/main/java/app/grapheneos/camera/ui/activities/MoreSettingsSecure.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 |
---|---|---|
@@ -1,3 +1,19 @@ | ||
package app.grapheneos.camera.ui.activities | ||
|
||
class MoreSettingsSecure : MoreSettings() | ||
import android.os.Bundle | ||
import app.grapheneos.camera.AutoFinishOnSleep | ||
|
||
class MoreSettingsSecure : MoreSettings() { | ||
|
||
private val autoFinisher = AutoFinishOnSleep(this) | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
autoFinisher.start() | ||
} | ||
|
||
override fun onDestroy() { | ||
super.onDestroy() | ||
autoFinisher.stop() | ||
} | ||
} |
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
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