Skip to content

Commit 8b4b634

Browse files
Mattias Peterssonjredestig
authored andcommitted
Avoid memory leak in the Shutdown confirmation dialog.
How to reproduce: 1) Lock the screen. 2) Open the Phone options menu by long pressing the power button. 3) Tap "Power off" to display the confirmation dialog. 4) Repeat step 2 and 3 a few times (without closing the confirmation dialog. Each time the confirmation dialog is displayed, a new instance is created. A stack of confirmation dialogs are created on the screen. This is fixed by making sure the previous dialog is dismissed before launching a new dialog. Change-Id: I6b6c61ccc56364b66eed3528019f761e75bbe268
1 parent 0748a56 commit 8b4b634

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

core/java/com/android/internal/app/ShutdownThread.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public final class ShutdownThread extends Thread {
7676
private PowerManager.WakeLock mCpuWakeLock;
7777
private PowerManager.WakeLock mScreenWakeLock;
7878
private Handler mHandler;
79+
80+
private static AlertDialog sConfirmDialog;
7981

8082
private ShutdownThread() {
8183
}
@@ -108,7 +110,10 @@ public static void shutdown(final Context context, boolean confirm) {
108110

109111
if (confirm) {
110112
final CloseDialogReceiver closer = new CloseDialogReceiver(context);
111-
final AlertDialog dialog = new AlertDialog.Builder(context)
113+
if (sConfirmDialog != null) {
114+
sConfirmDialog.dismiss();
115+
}
116+
sConfirmDialog = new AlertDialog.Builder(context)
112117
.setTitle(com.android.internal.R.string.power_off)
113118
.setMessage(resourceId)
114119
.setPositiveButton(com.android.internal.R.string.yes, new DialogInterface.OnClickListener() {
@@ -118,10 +123,10 @@ public void onClick(DialogInterface dialog, int which) {
118123
})
119124
.setNegativeButton(com.android.internal.R.string.no, null)
120125
.create();
121-
closer.dialog = dialog;
122-
dialog.setOnDismissListener(closer);
123-
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
124-
dialog.show();
126+
closer.dialog = sConfirmDialog;
127+
sConfirmDialog.setOnDismissListener(closer);
128+
sConfirmDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
129+
sConfirmDialog.show();
125130
} else {
126131
beginShutdownSequence(context);
127132
}

0 commit comments

Comments
 (0)