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.SPECIAL_DISABLE, 52 DialogType.FORCE_STOP 53 }) 54 public @interface DialogType { 55 int DISABLE = 0; 56 int SPECIAL_DISABLE = 1; 57 int FORCE_STOP = 2; 58 } 59 60 private static final String ARG_ID = "id"; 61 @VisibleForTesting 62 int mId; 63 newInstance(@ialogType int id)64 public static ButtonActionDialogFragment newInstance(@DialogType int id) { 65 ButtonActionDialogFragment dialogFragment = new ButtonActionDialogFragment(); 66 Bundle args = new Bundle(1); 67 args.putInt(ARG_ID, id); 68 dialogFragment.setArguments(args); 69 70 return dialogFragment; 71 } 72 73 @Override getMetricsCategory()74 public int getMetricsCategory() { 75 //TODO(35810915): update the metrics label because for now this fragment will be shown 76 // in two screens 77 return SettingsEnums.DIALOG_APP_INFO_ACTION; 78 } 79 80 @Override onCreateDialog(Bundle savedInstanceState)81 public Dialog onCreateDialog(Bundle savedInstanceState) { 82 final Bundle bundle = getArguments(); 83 mId = bundle.getInt(ARG_ID); 84 Dialog dialog = createDialog(mId); 85 if (dialog == null) { 86 throw new IllegalArgumentException("unknown id " + mId); 87 } 88 return dialog; 89 } 90 91 @Override onClick(DialogInterface dialog, int which)92 public void onClick(DialogInterface dialog, int which) { 93 final AppButtonsDialogListener lsn = 94 (AppButtonsDialogListener) getTargetFragment(); 95 lsn.handleDialogClick(mId); 96 } 97 createDialog(int id)98 private AlertDialog createDialog(int id) { 99 final Context context = getContext(); 100 switch (id) { 101 case DialogType.DISABLE: 102 case DialogType.SPECIAL_DISABLE: 103 return new AlertDialog.Builder(context) 104 .setMessage(R.string.app_disable_dlg_text) 105 .setPositiveButton(R.string.app_disable_dlg_positive, this) 106 .setNegativeButton(R.string.dlg_cancel, null) 107 .create(); 108 case DialogType.FORCE_STOP: 109 return new AlertDialog.Builder(context) 110 .setTitle(R.string.force_stop_dlg_title) 111 .setMessage(R.string.force_stop_dlg_text) 112 .setPositiveButton(R.string.dlg_ok, this) 113 .setNegativeButton(R.string.dlg_cancel, null) 114 .create(); 115 } 116 return null; 117 } 118 } 119 120