• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.nano.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 
newInstance(long freeableBytes)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
onCreateDialog(Bundle savedInstanceState)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
onClick(DialogInterface dialog, int which)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(
84                         getContext(), System.currentTimeMillis())) {
85                     StorageManagerUpsellDialog upsellDialog =
86                             StorageManagerUpsellDialog.newInstance(mFreeableBytes);
87                     upsellDialog.show(getFragmentManager(), StorageManagerUpsellDialog.TAG);
88                 } else {
89                     Activity activity = getActivity();
90                     if (activity != null) {
91                         activity.finish();
92                     }
93                 }
94                 break;
95             case DialogInterface.BUTTON_NEGATIVE:
96                 MetricsLogger.action(getContext(),
97                         MetricsEvent.ACTION_DELETION_HELPER_REMOVE_CANCEL);
98                 break;
99             default:
100                 break;
101         }
102     }
103 
getClearWarningText()104     private int getClearWarningText() {
105         // If the storage manager is on by default, we can use the normal message.
106         boolean warningUnneeded = SystemProperties.getBoolean(
107                 Constants.STORAGE_MANAGER_VISIBLE_PROPERTY, false);
108         if (warningUnneeded) {
109             return R.string.deletion_helper_clear_dialog_message;
110         }
111 
112         SharedPreferences sp = getSharedPreferences();
113         boolean shownBefore = sp.getBoolean(SHOWN_BEFORE, false);
114         return shownBefore ? R.string.deletion_helper_clear_dialog_message :
115                 R.string.deletion_helper_clear_dialog_message_first_time;
116     }
117 
getSharedPreferences()118     private SharedPreferences getSharedPreferences() {
119         return getContext().getSharedPreferences(Constants.SHARED_PREFERENCE_NAME,
120                 Context.MODE_PRIVATE);
121     }
122 }
123