Home | History | Annotate | Download | only in deletionhelper
      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 
     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                 .setTitle(R.string.deletion_helper_clear_dialog_title)
     65                 .setMessage(
     66                         context.getString(
     67                                 getClearWarningText(),
     68                                 Formatter.formatFileSize(context, mFreeableBytes)))
     69                 .setPositiveButton(R.string.deletion_helper_clear_dialog_remove, this)
     70                 .setNegativeButton(android.R.string.cancel, this)
     71                 .create();
     72     }
     73 
     74     @Override
     75     public void onClick(DialogInterface dialog, int which) {
     76         // Set the first time flag to avoid showing the first time warning twice.
     77         SharedPreferences.Editor editor = getSharedPreferences().edit();
     78         editor.putBoolean(SHOWN_BEFORE, true);
     79         editor.apply();
     80 
     81         switch (which) {
     82             case DialogInterface.BUTTON_POSITIVE:
     83                 ((DeletionHelperSettings) getTargetFragment()).clearData();
     84                 MetricsLogger.action(getContext(),
     85                         MetricsEvent.ACTION_DELETION_HELPER_REMOVE_CONFIRM);
     86                 if (StorageManagerUpsellDialog.shouldShow(
     87                         getContext(), System.currentTimeMillis())) {
     88                     StorageManagerUpsellDialog upsellDialog =
     89                             StorageManagerUpsellDialog.newInstance(mFreeableBytes);
     90                     upsellDialog.show(getFragmentManager(), StorageManagerUpsellDialog.TAG);
     91                 } else {
     92                     Activity activity = getActivity();
     93                     if (activity != null) {
     94                         activity.finish();
     95                     }
     96                 }
     97                 break;
     98             case DialogInterface.BUTTON_NEGATIVE:
     99                 MetricsLogger.action(getContext(),
    100                         MetricsEvent.ACTION_DELETION_HELPER_REMOVE_CANCEL);
    101                 break;
    102             default:
    103                 break;
    104         }
    105     }
    106 
    107     private int getClearWarningText() {
    108         // If the storage manager is on by default, we can use the normal message.
    109         boolean warningUnneeded = SystemProperties.getBoolean(
    110                 Constants.STORAGE_MANAGER_VISIBLE_PROPERTY, false);
    111         if (warningUnneeded) {
    112             return R.string.deletion_helper_clear_dialog_message;
    113         }
    114 
    115         SharedPreferences sp = getSharedPreferences();
    116         boolean shownBefore = sp.getBoolean(SHOWN_BEFORE, false);
    117         return shownBefore ? R.string.deletion_helper_clear_dialog_message :
    118                 R.string.deletion_helper_clear_dialog_message_first_time;
    119     }
    120 
    121     private SharedPreferences getSharedPreferences() {
    122         return getContext().getSharedPreferences(Constants.SHARED_PREFERENCE_NAME,
    123                 Context.MODE_PRIVATE);
    124     }
    125 }
    126