OSDN Git Service

Merge "Fix NPE crash in AppInfoBase" am: e49cc3d659 am: abcb069d95
[android-x86/packages-apps-Settings.git] / src / com / android / settings / fuelgauge / ButtonActionDialogFragment.java
1 package com.android.settings.fuelgauge;
2
3 import android.app.Activity;
4 import android.app.AlertDialog;
5 import android.app.Dialog;
6 import android.content.Context;
7 import android.content.DialogInterface;
8 import android.os.Bundle;
9 import android.support.annotation.IntDef;
10 import android.support.annotation.VisibleForTesting;
11
12 import com.android.internal.logging.nano.MetricsProto;
13 import com.android.settings.R;
14 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
15
16 import java.lang.annotation.Retention;
17 import java.lang.annotation.RetentionPolicy;
18
19 /**
20  * Fragment to show the dialog for uninstall or forcestop. This fragment uses function in
21  * target fragment to handle the dialog button click.
22  */
23 public class ButtonActionDialogFragment extends InstrumentedDialogFragment implements
24         DialogInterface.OnClickListener {
25
26     /**
27      * Interface to handle the dialog click
28      */
29     interface AppButtonsDialogListener {
30         void handleDialogClick(int type);
31     }
32
33     @Retention(RetentionPolicy.SOURCE)
34     @IntDef({
35             DialogType.DISABLE,
36             DialogType.SPECIAL_DISABLE,
37             DialogType.FORCE_STOP
38     })
39     public @interface DialogType {
40         int DISABLE = 0;
41         int SPECIAL_DISABLE = 1;
42         int FORCE_STOP = 2;
43     }
44
45     private static final String ARG_ID = "id";
46     @VisibleForTesting
47     int mId;
48
49     public static ButtonActionDialogFragment newInstance(@DialogType int id) {
50         ButtonActionDialogFragment dialogFragment = new ButtonActionDialogFragment();
51         Bundle args = new Bundle(1);
52         args.putInt(ARG_ID, id);
53         dialogFragment.setArguments(args);
54
55         return dialogFragment;
56     }
57
58     @Override
59     public int getMetricsCategory() {
60         //TODO(35810915): update the metrics label because for now this fragment will be shown
61         // in two screens
62         return MetricsProto.MetricsEvent.DIALOG_APP_INFO_ACTION;
63     }
64
65     @Override
66     public Dialog onCreateDialog(Bundle savedInstanceState) {
67         final Bundle bundle = getArguments();
68         mId = bundle.getInt(ARG_ID);
69         Dialog dialog = createDialog(mId);
70         if (dialog == null) {
71             throw new IllegalArgumentException("unknown id " + mId);
72         }
73         return dialog;
74     }
75
76     @Override
77     public void onClick(DialogInterface dialog, int which) {
78         final AppButtonsDialogListener lsn =
79                 (AppButtonsDialogListener) getTargetFragment();
80         lsn.handleDialogClick(mId);
81     }
82
83     private AlertDialog createDialog(int id) {
84         final Context context = getContext();
85         switch (id) {
86             case DialogType.DISABLE:
87             case DialogType.SPECIAL_DISABLE:
88                 return new AlertDialog.Builder(context)
89                         .setMessage(R.string.app_disable_dlg_text)
90                         .setPositiveButton(R.string.app_disable_dlg_positive, this)
91                         .setNegativeButton(R.string.dlg_cancel, null)
92                         .create();
93             case DialogType.FORCE_STOP:
94                 return new AlertDialog.Builder(context)
95                         .setTitle(R.string.force_stop_dlg_title)
96                         .setMessage(R.string.force_stop_dlg_text)
97                         .setPositiveButton(R.string.dlg_ok, this)
98                         .setNegativeButton(R.string.dlg_cancel, null)
99                         .create();
100         }
101         return null;
102     }
103 }
104