1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */
16 package com.android.settings.applications.appinfo;
17 
18 import android.app.Dialog;
19 import android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.os.Bundle;
23 
24 import androidx.annotation.IntDef;
25 import androidx.annotation.VisibleForTesting;
26 import androidx.appcompat.app.AlertDialog;
27 
28 import com.android.settings.R;
29 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
30 
31 import java.lang.annotation.Retention;
32 import java.lang.annotation.RetentionPolicy;
33 
34 /**
35  * Fragment to show the dialog for uninstall or forcestop. This fragment uses function in
36  * target fragment to handle the dialog button click.
37  */
38 public class ButtonActionDialogFragment extends InstrumentedDialogFragment implements
39         DialogInterface.OnClickListener {
40 
41     /**
42      * Interface to handle the dialog click
43      */
44     public interface AppButtonsDialogListener {
handleDialogClick(int type)45         void handleDialogClick(int type);
46     }
47 
48     @Retention(RetentionPolicy.SOURCE)
49     @IntDef({
50             DialogType.DISABLE,
51             DialogType.FORCE_STOP
52     })
53     public @interface DialogType {
54         int DISABLE = 0;
55         int FORCE_STOP = 2;
56     }
57 
58     private static final String ARG_ID = "id";
59     @VisibleForTesting
60     int mId;
61 
newInstance(@ialogType int id)62     public static ButtonActionDialogFragment newInstance(@DialogType int id) {
63         ButtonActionDialogFragment dialogFragment = new ButtonActionDialogFragment();
64         Bundle args = new Bundle(1);
65         args.putInt(ARG_ID, id);
66         dialogFragment.setArguments(args);
67 
68         return dialogFragment;
69     }
70 
71     @Override
getMetricsCategory()72     public int getMetricsCategory() {
73         //TODO(35810915): update the metrics label because for now this fragment will be shown
74         // in two screens
75         return SettingsEnums.DIALOG_APP_INFO_ACTION;
76     }
77 
78     @Override
onCreateDialog(Bundle savedInstanceState)79     public Dialog onCreateDialog(Bundle savedInstanceState) {
80         final Bundle bundle = getArguments();
81         mId = bundle.getInt(ARG_ID);
82         Dialog dialog = createDialog(mId);
83         if (dialog == null) {
84             throw new IllegalArgumentException("unknown id " + mId);
85         }
86         return dialog;
87     }
88 
89     @Override
onClick(DialogInterface dialog, int which)90     public void onClick(DialogInterface dialog, int which) {
91         // When it's in a multi-window mode, force stopping an app will lead to an activity
92         // recreate, and the dialog fragment will also be recreated. So dismiss the dialog before
93         // stopping the app.
94         if (mId == ButtonActionDialogFragment.DialogType.FORCE_STOP) {
95             dialog.dismiss();
96         }
97         final AppButtonsDialogListener lsn =
98                 (AppButtonsDialogListener) getTargetFragment();
99         lsn.handleDialogClick(mId);
100     }
101 
createDialog(int id)102     private AlertDialog createDialog(int id) {
103         final Context context = getContext();
104         switch (id) {
105             case DialogType.DISABLE:
106                 return new AlertDialog.Builder(context)
107                         .setMessage(R.string.app_disable_dlg_text)
108                         .setPositiveButton(R.string.app_disable_dlg_positive, this)
109                         .setNegativeButton(R.string.dlg_cancel, null)
110                         .create();
111             case DialogType.FORCE_STOP:
112                 return new AlertDialog.Builder(context)
113                         .setTitle(R.string.force_stop_dlg_title)
114                         .setMessage(R.string.force_stop_dlg_text)
115                         .setPositiveButton(R.string.dlg_ok, this)
116                         .setNegativeButton(R.string.dlg_cancel, null)
117                         .create();
118         }
119         return null;
120     }
121 }
122 
123