Suspend Shutdown hooks

The TomTom BRIDGE allows you to intervene when it is about to be suspended or shut down. It will look for apps listening for the ACTION_REQUEST_CONFIRM_SUSPEND_SHUTDOWN intent and if one is installed, control over the suspend/shutdown flow is handed over to this app. This can be useful for situations where you do not want the device to suspend/shutdown because some process is not finished or users first need to log out of your own systems, for example.

Make sure to use a priority higher than 5 for this intent-filter, to make sure your app will be called. The default handler uses priority 5 so to supersede it your priority must be higher. So your AndroidManifest.xml will contain this:

1<!-- Permissions needed -->
2<uses-permission android:name="android.permission.DEVICE_POWER" />
3<uses-permission android:name="android.permission.SHUTDOWN" />
4...
5<!-- Priority needed > 5 -->
6<intent-filter android:priority="10" >s
7 <action android:name="android.intent.action.ACTION_REQUEST_CONFIRM_SUSPEND_SHUTDOWN" />
8 <category android:name="android.intent.category.DEFAULT" />
9</intent-filter>

The intent comes with two integer extras: poweroff_state and poweroff_reason. The former indicates the state the system is trying to get to, 1 for a shutdown and 2 to be suspended, and the reason gives an indication why the device is trying to get to that state:

Value Reason
0 By user request, eg. the power button was pressed
1 Device administration policy
2 Screen timeout
3 Disconnected from power
4 Lid switch (from 17.6 onward)
5 Application request (from 17.6 onward)
6 HDMI (from 17.6 onward)
7 Sleep button (from 17.6 onward)

Once you have decided to proceed with the suspend/shutdown it's the apps responsibility now to make that happen! These are the calls you can use:

1private void suspend() {
2 try {
3 finish();
4 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
5 pm.goToSleep(SystemClock.uptimeMillis());
6 } catch (final SecurityException e) {
7 Log.e(TAG, "SecurityException ", e);
8 }
9}
10
11private void shutdown() {
12 try {
13 finish();
14 final Intent shutdownIntent = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
15 startActivity(shutdownIntent);
16 } catch (final SecurityException e) {
17 Log.e(TAG, "SecurityException ", e);
18 }
19}

To prevent the suspend/shutdown from happening, the only thing you need to do is ignore it: don't suspend or request a shutdown from your app and nothing will happen.