Repository navigation
Start on boot: an opt-in setting for boxes without a firmware autorun (#255) #256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
19aef69
feat: Start on boot, an opt-in setting that opens the app after the d…
compot-dev 12506f0
fix: Start on boot keeps the app op over a reboot, a second start doe…
compot-dev bf02fbc
fix: Start on boot leaves the app alone when a firmware autorun opene…
compot-dev 0d7ab38
fix: the Settings dialog scrolls; a Start on boot section in the thre…
compot-dev 79f812b
fix: Start on boot checks for a live activity, not a resumed one; /st…
compot-dev 35689ee
fix: Start on boot shows a 1x1 overlay window on Android 15+; start_o…
compot-dev 7b7ed04
fix: Start on boot skips an activity that is opening or shown, not on…
compot-dev cbee135
2.19.0: Start on boot (#255)
compot-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
126 changes: 126 additions & 0 deletions
126
android/app/src/main/java/com/openipc/decoder/BootReceiver.java
This file contains hidden or 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,126 @@ | ||
| /* | ||
| * | ||
| * Copyright (c) OpenIPC https://openipc.org Prosperity Public License 3.0.0 | ||
| * | ||
| * BootReceiver.java — opens the app after the device boots, when Start on boot is on (#255) | ||
| * | ||
| */ | ||
|
|
||
| package com.openipc.decoder; | ||
|
|
||
| import android.annotation.TargetApi; | ||
| import android.content.BroadcastReceiver; | ||
| import android.content.Context; | ||
| import android.content.Intent; | ||
| import android.graphics.PixelFormat; | ||
| import android.os.Build; | ||
| import android.os.Handler; | ||
| import android.os.Looper; | ||
| import android.provider.Settings; | ||
| import android.util.Log; | ||
| import android.view.View; | ||
| import android.view.WindowManager; | ||
|
|
||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class BootReceiver extends BroadcastReceiver { | ||
| private static final String TAG = "OpenIPCDecoder"; | ||
| /** In the "settings" preferences, so Reset (#171) turns it off too. */ | ||
| static final String PREF = "start_on_boot"; | ||
|
|
||
| static boolean enabled(Context context) { | ||
| return context.getSharedPreferences("settings", Context.MODE_PRIVATE) | ||
| .getBoolean(PREF, false); | ||
| } | ||
|
|
||
| static void setEnabled(Context context, boolean on) { | ||
| context.getSharedPreferences("settings", Context.MODE_PRIVATE).edit() | ||
| .putBoolean(PREF, on).apply(); | ||
| } | ||
|
|
||
| /** | ||
| * Android 10+ starts an activity from the background only for an app that may draw over | ||
| * other apps; many TV boxes have no screen to allow it, adb does (appops). | ||
| */ | ||
| static boolean overlayNeeded() { | ||
| return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q; | ||
| } | ||
|
|
||
| static boolean overlayAllowed(Context context) { | ||
| return !overlayNeeded() || Settings.canDrawOverlays(context); | ||
| } | ||
|
|
||
| /** For /stats.json, with or without an activity (Qodo on #256). */ | ||
| static Map<String, Object> state(Context context) { | ||
| Map<String, Object> m = new LinkedHashMap<>(); | ||
| m.put("on", enabled(context)); | ||
| m.put("overlay", !overlayNeeded() ? "not needed" | ||
| : overlayAllowed(context) ? "allowed" : "missing"); | ||
| return m; | ||
| } | ||
|
|
||
| @Override | ||
| public void onReceive(Context context, Intent intent) { | ||
| if (!Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) return; | ||
| if (!enabled(context)) return; | ||
| // A firmware autorun opened it already: another start would pause and resume it, | ||
| // which restarts every stream | ||
| if (Decoder.openOrOpening()) { | ||
| Log.i(TAG, "Start on boot: the app is open already"); | ||
| return; | ||
|
compot-dev marked this conversation as resolved.
Resolved
|
||
| } | ||
| if (!overlayAllowed(context)) { | ||
| Log.w(TAG, "Start on boot: no permission to draw over other apps, Android may block the start"); | ||
| } | ||
| Log.i(TAG, "Start on boot: opening the app"); | ||
| Intent start = new Intent(context, Decoder.class); | ||
| start.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||
| // Android 15+ lets an app that targets 35+ start from the background with this | ||
| // permission only while one of its overlay windows is visible (Qodo on #256) | ||
| View overlay = Build.VERSION.SDK_INT >= 35 && overlayAllowed(context) | ||
| ? showOverlay(context) : null; | ||
| try { | ||
| context.startActivity(start); | ||
|
Comment on lines
+81
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 4. Boot start on android 15+ may still be blocked onReceive calls showOverlay() and then calls context.startActivity(start) straight away, in the same main-thread turn. wm.addView only queues the window: the 1×1 view has not been laid out, drawn or put on screen when startActivity runs, so the overlay may not count as visible yet when Android 15+ decides whether to allow the background start. In that case the start is blocked even with the permission granted, the 2 s postDelayed only removes the overlay afterwards, and the Android 10 device test does not cover this path. Agent Prompt
|
||
| } catch (RuntimeException e) { | ||
| Log.w(TAG, "Start on boot failed", e); | ||
| } | ||
| if (overlay != null) { | ||
| PendingResult pending = goAsync(); | ||
| new Handler(Looper.getMainLooper()).postDelayed(() -> { | ||
| removeOverlay(context, overlay); | ||
| pending.finish(); | ||
| }, OVERLAY_MS); | ||
| } | ||
| } | ||
|
|
||
| /** Long enough for the start to pass the check, short enough to stay well under 10 s. */ | ||
| private static final long OVERLAY_MS = 2000; | ||
|
|
||
| /** A 1×1 transparent window that takes no touch or focus; null if it cannot be shown. */ | ||
| @TargetApi(35) | ||
| private static View showOverlay(Context context) { | ||
| WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); | ||
| View v = new View(context.getApplicationContext()); | ||
| WindowManager.LayoutParams lp = new WindowManager.LayoutParams(1, 1, | ||
| WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, | ||
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | ||
| | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, | ||
| PixelFormat.TRANSPARENT); | ||
| try { | ||
| wm.addView(v, lp); | ||
| return v; | ||
| } catch (RuntimeException e) { | ||
| Log.w(TAG, "Start on boot: no overlay window", e); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private static void removeOverlay(Context context, View v) { | ||
| try { | ||
| ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).removeView(v); | ||
| } catch (RuntimeException e) { | ||
| Log.w(TAG, "Start on boot: overlay window not removed", e); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.