1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.storagemanager.deletionhelper; 18 19 import android.app.Activity; 20 import android.app.AlertDialog; 21 import android.app.Dialog; 22 import android.app.DialogFragment; 23 import android.content.Context; 24 import android.content.DialogInterface; 25 import android.content.SharedPreferences; 26 import android.os.Bundle; 27 import android.os.SystemProperties; 28 import android.text.format.Formatter; 29 import com.android.internal.logging.MetricsLogger; 30 import com.android.internal.logging.MetricsProto.MetricsEvent; 31 import com.android.storagemanager.R; 32 import com.android.storagemanager.utils.Constants; 33 34 /** 35 * Fragment used to confirm that the user wishes to delete a certain amount of data. 36 */ 37 public class ConfirmDeletionDialog extends DialogFragment implements 38 DialogInterface.OnClickListener { 39 public static final String TAG = "ConfirmDeletionDialog"; 40 private static final String ARG_TOTAL_SPACE = "total_freeable"; 41 // If the confirm deletion dialog has been shown before. Used to choose which warning message 42 // we show to the user. 43 private static final String SHOWN_BEFORE = "shown_before"; 44 45 private long mFreeableBytes; 46 47 public static ConfirmDeletionDialog newInstance(long freeableBytes) { 48 Bundle args = new Bundle(1); 49 args.putLong(ARG_TOTAL_SPACE, freeableBytes); 50 51 ConfirmDeletionDialog dialog = new ConfirmDeletionDialog(); 52 dialog.setArguments(args); 53 54 return dialog; 55 } 56 57 @Override 58 public Dialog onCreateDialog(Bundle savedInstanceState) { 59 final Bundle args = getArguments(); 60 mFreeableBytes = args.getLong(ARG_TOTAL_SPACE); 61 62 final Context context = getContext(); 63 return new AlertDialog.Builder(context) 64 .setMessage(context.getString(getClearWarningText(), 65 Formatter.formatFileSize(context, mFreeableBytes))) 66 .setPositiveButton(R.string.deletion_helper_clear_dialog_remove, this) 67 .setNegativeButton(android.R.string.cancel, this) 68 .create(); 69 } 70 71 @Override 72 public void onClick(DialogInterface dialog, int which) { 73 // Set the first time flag to avoid showing the first time warning twice. 74 SharedPreferences.Editor editor = getSharedPreferences().edit(); 75 editor.putBoolean(SHOWN_BEFORE, true); 76 editor.apply(); 77 78 switch (which) { 79 case DialogInterface.BUTTON_POSITIVE: 80 ((DeletionHelperSettings) getTargetFragment()).clearData(); 81 MetricsLogger.action(getContext(), 82 MetricsEvent.ACTION_DELETION_HELPER_REMOVE_CONFIRM); 83 if (StorageManagerUpsellDialog.shouldShow(getContext())) { 84 StorageManagerUpsellDialog upsellDialog = 85 StorageManagerUpsellDialog.newInstance(mFreeableBytes); 86 upsellDialog.show(getFragmentManager(), StorageManagerUpsellDialog.TAG); 87 } else { 88 Activity activity = getActivity(); 89 if (activity != null) { 90 activity.finish(); 91 } 92 } 93 break; 94 case DialogInterface.BUTTON_NEGATIVE: 95 MetricsLogger.action(getContext(), 96 MetricsEvent.ACTION_DELETION_HELPER_REMOVE_CANCEL); 97 break; 98 default: 99 break; 100 } 101 } 102 103 private int getClearWarningText() { 104 // If the storage manager is on by default, we can use the normal message. 105 boolean warningUnneeded = SystemProperties.getBoolean( 106 Constants.STORAGE_MANAGER_VISIBLE_PROPERTY, false); 107 if (warningUnneeded) { 108 return R.string.deletion_helper_clear_dialog_message; 109 } 110 111 SharedPreferences sp = getSharedPreferences(); 112 boolean shownBefore = sp.getBoolean(SHOWN_BEFORE, false); 113 return shownBefore ? R.string.deletion_helper_clear_dialog_message : 114 R.string.deletion_helper_clear_dialog_message_first_time; 115 } 116 117 private SharedPreferences getSharedPreferences() { 118 return getContext().getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, 119 Context.MODE_PRIVATE); 120 } 121 } 122