forked from nadlabak/android_device_motorola_umts_sholes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unlike the stock ROM, we don't simply exec /sbin/reboot, but present UI to the user where he can cancel the reboot (if he's currently doing something) and is presented with a notice after the reboot. Change-Id: Ib64ad901b9c407d21434e2cd7048b66f86bb443f
- Loading branch information
Showing
10 changed files
with
356 additions
and
2 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
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
134 changes: 134 additions & 0 deletions
134
MmParts/src/com/cyanogenmod/mmparts/BpPanicHandlerService.java
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,134 @@ | |||
package com.cyanogenmod.mmparts; | |||
|
|||
import android.app.AlarmManager; | |||
import android.app.Notification; | |||
import android.app.PendingIntent; | |||
import android.app.Service; | |||
import android.content.SharedPreferences; | |||
import android.graphics.Color; | |||
import android.content.Intent; | |||
import android.os.IBinder; | |||
import android.os.PowerManager; | |||
import android.preference.PreferenceManager; | |||
import android.text.TextUtils; | |||
import android.util.Log; | |||
|
|||
public class BpPanicHandlerService extends Service { | |||
private static final String TAG = "BpPanicHandlerService"; | |||
private static final int NOTIFICATION_ID = 1; | |||
|
|||
private static final String ACTION_HANDLE_PANIC = "com.cyanogenmod.mmparts.action.HANDLE_BP_PANIC"; | |||
static final String ACTION_TIMER_UPDATE = "com.cyanogenmod.mmparts.action.TIMER_UPDATE"; | |||
static final String ACTION_REBOOT = "com.cyanogenmod.mmparts.action.BP_PANIC_REBOOT"; | |||
static final String ACTION_SNOOZE = "com.cyanogenmod.mmparts.action.REMIND_LATER"; | |||
static final String ACTION_DISMISS = "com.cyanogenmod.mmparts.action.DISMISS"; | |||
|
|||
static final String EXTRA_TIMEOUT = "timeout"; | |||
|
|||
static final String KEY_NEED_REBOOT_NOTICE = "need_bppanic_reboot_notice"; | |||
|
|||
private static final long SNOOZE_DELAY = 5 * 60 * 1000; /* 5 minutes */ | |||
private static final long REBOOT_TIMEOUT = 60 * 1000; /* 1 minute */ | |||
|
|||
private SharedPreferences mPrefs; | |||
private AlarmManager mAM; | |||
private long mRebootTimeout = -1; | |||
private Notification mNotification; | |||
private Intent mNotifyIntent; | |||
private PendingIntent mTimerUpdateIntent; | |||
private PendingIntent mPanicHandleIntent; | |||
|
|||
@Override | |||
public void onCreate() { | |||
super.onCreate(); | |||
|
|||
mAM = (AlarmManager) getSystemService(ALARM_SERVICE); | |||
mPrefs = PreferenceManager.getDefaultSharedPreferences(this); | |||
|
|||
Intent intent = new Intent(this, getClass()); | |||
intent.setAction(ACTION_HANDLE_PANIC); | |||
mPanicHandleIntent = PendingIntent.getService(this, 0, intent, 0); | |||
|
|||
intent = new Intent(this, getClass()); | |||
intent.setAction(ACTION_TIMER_UPDATE); | |||
mTimerUpdateIntent = PendingIntent.getService(this, 1, intent, 0); | |||
|
|||
mNotifyIntent = new Intent(this, BpPanicNotifyActivity.class); | |||
mNotifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |||
} | |||
|
|||
@Override | |||
public int onStartCommand(Intent intent, int flags, int startId) { | |||
String action = intent.getAction(); | |||
|
|||
if (TextUtils.equals(action, ACTION_HANDLE_PANIC)) { | |||
Log.d(TAG, "Got BP panic intent"); | |||
if (mNotification == null) { | |||
mNotification = createNotification(); | |||
} | |||
mRebootTimeout = System.currentTimeMillis() + REBOOT_TIMEOUT; | |||
updateTimeout(); | |||
startActivity(mNotifyIntent); | |||
startForeground(NOTIFICATION_ID, mNotification); | |||
} else if (TextUtils.equals(action, ACTION_REBOOT)) { | |||
doReboot(false); | |||
} else if (TextUtils.equals(action, ACTION_SNOOZE)) { | |||
cancelTimeout(); | |||
schedule(mPanicHandleIntent, SNOOZE_DELAY); | |||
} else if (TextUtils.equals(action, ACTION_DISMISS)) { | |||
cancelTimeout(); | |||
} else if (TextUtils.equals(action, ACTION_TIMER_UPDATE)) { | |||
updateTimeout(); | |||
} | |||
|
|||
return START_STICKY; | |||
} | |||
|
|||
@Override | |||
public IBinder onBind(Intent intent) { | |||
return null; | |||
} | |||
|
|||
private void schedule(PendingIntent intent, long timeout) { | |||
mAM.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeout, intent); | |||
} | |||
|
|||
private void cancelTimeout() { | |||
removeStickyBroadcast(new Intent(ACTION_TIMER_UPDATE)); | |||
mRebootTimeout = -1; | |||
mAM.cancel(mTimerUpdateIntent); | |||
} | |||
|
|||
private Notification createNotification() { | |||
Notification notification = new Notification(android.R.drawable.stat_notify_error, null, System.currentTimeMillis()); | |||
notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE; | |||
notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_SHOW_LIGHTS; | |||
notification.ledARGB = Color.RED; | |||
notification.ledOnMS = 1000; | |||
notification.ledOffMS = 0; | |||
notification.setLatestEventInfo(this, | |||
getString(R.string.modem_error), | |||
getString(R.string.touch_for_info), | |||
PendingIntent.getActivity(this, 0, mNotifyIntent, 0)); | |||
|
|||
return notification; | |||
} | |||
|
|||
private void updateTimeout() { | |||
int timeout = (int) (mRebootTimeout - System.currentTimeMillis() + 500) / 1000; | |||
if (timeout <= 0) { | |||
doReboot(true); | |||
} else { | |||
Intent intent = new Intent(ACTION_TIMER_UPDATE); | |||
intent.putExtra(EXTRA_TIMEOUT, timeout); | |||
sendStickyBroadcast(intent); | |||
schedule(mTimerUpdateIntent, timeout <= 5 ? 1000 : 5000); | |||
} | |||
} | |||
|
|||
private void doReboot(boolean requireNotice) { | |||
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE); | |||
mPrefs.edit().putBoolean(KEY_NEED_REBOOT_NOTICE, requireNotice).commit(); | |||
pm.reboot("bppanic"); | |||
} | |||
} |
100 changes: 100 additions & 0 deletions
100
MmParts/src/com/cyanogenmod/mmparts/BpPanicNotifyActivity.java
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,100 @@ | |||
package com.cyanogenmod.mmparts; | |||
|
|||
import android.app.Activity; | |||
import android.app.AlertDialog; | |||
import android.app.Dialog; | |||
import android.content.BroadcastReceiver; | |||
import android.content.Context; | |||
import android.content.DialogInterface; | |||
import android.content.Intent; | |||
import android.content.IntentFilter; | |||
import android.os.Bundle; | |||
|
|||
public class BpPanicNotifyActivity extends Activity implements DialogInterface.OnClickListener { | |||
private static final int DIALOG_ACTION_SELECT = 1; | |||
|
|||
private AlertDialog mDialog; | |||
|
|||
private BroadcastReceiver mTimerUpdateReceiver = new BroadcastReceiver() { | |||
@Override | |||
public void onReceive(Context context, Intent intent) { | |||
int timeout = intent.getIntExtra(BpPanicHandlerService.EXTRA_TIMEOUT, -1); | |||
updateDialogMessage(timeout); | |||
} | |||
}; | |||
|
|||
@Override | |||
protected Dialog onCreateDialog(int id, Bundle args) { | |||
if (id == DIALOG_ACTION_SELECT) { | |||
mDialog = new AlertDialog.Builder(this) | |||
.setTitle(R.string.modem_error) | |||
.setMessage(R.string.modem_error_msg) | |||
.setCancelable(false) | |||
.setPositiveButton(R.string.reboot_btn, this) | |||
.setNeutralButton(R.string.snooze_btn, this) | |||
.setNegativeButton(R.string.dismiss_btn, this) | |||
.create(); | |||
return mDialog; | |||
} | |||
|
|||
return super.onCreateDialog(id, args); | |||
} | |||
|
|||
@Override | |||
protected void onPrepareDialog(int id, Dialog dialog, Bundle args) { | |||
if (id == DIALOG_ACTION_SELECT) { | |||
updateDialogMessage(-1); | |||
} | |||
super.onPrepareDialog(id, dialog, args); | |||
} | |||
|
|||
@Override | |||
protected void onResume() { | |||
super.onResume(); | |||
showDialog(DIALOG_ACTION_SELECT); | |||
registerReceiver(mTimerUpdateReceiver, new IntentFilter(BpPanicHandlerService.ACTION_TIMER_UPDATE)); | |||
} | |||
|
|||
@Override | |||
protected void onPause() { | |||
unregisterReceiver(mTimerUpdateReceiver); | |||
super.onPause(); | |||
} | |||
|
|||
private void doServiceAction(String action) { | |||
Intent intent = new Intent(this, BpPanicHandlerService.class); | |||
intent.setAction(action); | |||
startService(intent); | |||
} | |||
|
|||
public void onClick(DialogInterface dialog, int which) { | |||
String action; | |||
|
|||
switch (which) { | |||
case DialogInterface.BUTTON_POSITIVE: | |||
action = BpPanicHandlerService.ACTION_REBOOT; | |||
break; | |||
case DialogInterface.BUTTON_NEUTRAL: | |||
action = BpPanicHandlerService.ACTION_SNOOZE; | |||
break; | |||
case DialogInterface.BUTTON_NEGATIVE: | |||
action = BpPanicHandlerService.ACTION_DISMISS; | |||
break; | |||
default: | |||
return; | |||
} | |||
|
|||
doServiceAction(action); | |||
finish(); | |||
} | |||
|
|||
private void updateDialogMessage(int timeout) { | |||
StringBuilder message = new StringBuilder(); | |||
message.append(getString(R.string.modem_error_msg)); | |||
if (timeout >= 0) { | |||
message.append("\n\n"); | |||
message.append(getString(R.string.reboot_timeout_msg, timeout)); | |||
} | |||
mDialog.setMessage(message.toString()); | |||
} | |||
} |
47 changes: 47 additions & 0 deletions
47
MmParts/src/com/cyanogenmod/mmparts/BpPanicRebootNoticeActivity.java
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,47 @@ | |||
package com.cyanogenmod.mmparts; | |||
|
|||
import android.app.Activity; | |||
import android.app.AlertDialog; | |||
import android.app.Dialog; | |||
import android.content.DialogInterface; | |||
import android.os.Bundle; | |||
|
|||
public class BpPanicRebootNoticeActivity extends Activity { | |||
private static final int DIALOG_NOTICE = 1; | |||
|
|||
@Override | |||
protected Dialog onCreateDialog(int id, Bundle args) { | |||
if (id == DIALOG_NOTICE) { | |||
return new AlertDialog.Builder(this) | |||
.setTitle(R.string.modem_error) | |||
.setMessage(R.string.device_rebooted_modem_error) | |||
.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() { | |||
@Override | |||
public void onClick(DialogInterface dialog, int which) { | |||
dialog.cancel(); | |||
} | |||
}) | |||
.setOnCancelListener(new DialogInterface.OnCancelListener() { | |||
@Override | |||
public void onCancel(DialogInterface dialog) { | |||
finish(); | |||
} | |||
}) | |||
.create(); | |||
} | |||
|
|||
return super.onCreateDialog(id, args); | |||
} | |||
|
|||
@Override | |||
protected void onResume() { | |||
super.onResume(); | |||
showDialog(DIALOG_NOTICE); | |||
} | |||
|
|||
@Override | |||
protected void onPause() { | |||
dismissDialog(DIALOG_NOTICE); | |||
super.onPause(); | |||
} | |||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,26 @@ | |||
package com.cyanogenmod.mmparts; | |||
|
|||
import android.app.AlertDialog; | |||
import android.content.BroadcastReceiver; | |||
import android.content.Context; | |||
import android.content.Intent; | |||
import android.content.SharedPreferences; | |||
import android.preference.PreferenceManager; | |||
|
|||
public class BpPanicReceiver extends BroadcastReceiver { | |||
@Override | |||
public void onReceive(Context context, Intent intent) { | |||
final String action = intent.getAction(); | |||
|
|||
if (Intent.ACTION_BOOT_COMPLETED.equals(action)) { | |||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); | |||
if (prefs.getBoolean(BpPanicHandlerService.KEY_NEED_REBOOT_NOTICE, false)) { | |||
Intent noticeIntent = new Intent(context, BpPanicRebootNoticeActivity.class); | |||
noticeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |||
|
|||
context.startActivity(noticeIntent); | |||
prefs.edit().putBoolean(BpPanicHandlerService.KEY_NEED_REBOOT_NOTICE, false).commit(); | |||
} | |||
} | |||
} | |||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,11 @@ | |||
#!/system/bin/sh | |||
|
|||
# stop panic_daemon restarting | |||
setprop ctl.stop panic_daemon | |||
|
|||
# let UI handle the failure | |||
am startservice -a com.cyanogenmod.mmparts.action.HANDLE_BP_PANIC -n com.cyanogenmod.mmparts/.BpPanicHandlerService 2>&1 | grep Error | |||
if [ $? -eq 0 ]; then | |||
# some error occured, use fallback and reboot directly | |||
reboot bppanic | |||
fi |
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.