1 /*
2  * Copyright (C) 2015 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.settings.deviceinfo;
18 
19 import android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.app.DialogFragment;
22 import android.app.Fragment;
23 import android.content.Context;
24 import android.content.DialogInterface;
25 import android.os.Bundle;
26 import android.os.storage.StorageManager;
27 import android.os.storage.VolumeRecord;
28 import android.text.TextUtils;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.View.OnClickListener;
32 import android.view.ViewGroup;
33 import android.widget.Button;
34 import android.widget.TextView;
35 
36 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
37 import com.android.settings.R;
38 import com.android.settings.SettingsPreferenceFragment;
39 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
40 
41 public class PrivateVolumeForget extends SettingsPreferenceFragment {
42     private static final String TAG_FORGET_CONFIRM = "forget_confirm";
43 
44     private VolumeRecord mRecord;
45 
46     @Override
getMetricsCategory()47     public int getMetricsCategory() {
48         return MetricsEvent.DEVICEINFO_STORAGE;
49     }
50 
51     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)52     public View onCreateView(LayoutInflater inflater, ViewGroup container,
53             Bundle savedInstanceState) {
54         final StorageManager storage = getActivity().getSystemService(StorageManager.class);
55         final String fsUuid = getArguments().getString(VolumeRecord.EXTRA_FS_UUID);
56         // Passing null will crash the StorageManager, so let's early exit.
57         if (fsUuid == null) {
58             getActivity().finish();
59             return null;
60         }
61         mRecord = storage.findRecordByUuid(fsUuid);
62 
63         if (mRecord == null) {
64             getActivity().finish();
65             return null;
66         }
67 
68         final View view = inflater.inflate(R.layout.storage_internal_forget, container, false);
69         final TextView body = (TextView) view.findViewById(R.id.body);
70         final Button confirm = (Button) view.findViewById(R.id.confirm);
71 
72         body.setText(TextUtils.expandTemplate(getText(R.string.storage_internal_forget_details),
73                 mRecord.getNickname()));
74         confirm.setOnClickListener(mConfirmListener);
75 
76         return view;
77     }
78 
79     private final OnClickListener mConfirmListener = new OnClickListener() {
80         @Override
81         public void onClick(View v) {
82             ForgetConfirmFragment.show(PrivateVolumeForget.this, mRecord.getFsUuid());
83         }
84     };
85 
86     public static class ForgetConfirmFragment extends InstrumentedDialogFragment {
87 
88         @Override
getMetricsCategory()89         public int getMetricsCategory() {
90             return MetricsEvent.DIALOG_VOLUME_FORGET;
91         }
92 
show(Fragment parent, String fsUuid)93         public static void show(Fragment parent, String fsUuid) {
94             final Bundle args = new Bundle();
95             args.putString(VolumeRecord.EXTRA_FS_UUID, fsUuid);
96 
97             final ForgetConfirmFragment dialog = new ForgetConfirmFragment();
98             dialog.setArguments(args);
99             dialog.setTargetFragment(parent, 0);
100             dialog.show(parent.getFragmentManager(), TAG_FORGET_CONFIRM);
101         }
102 
103         @Override
onCreateDialog(Bundle savedInstanceState)104         public Dialog onCreateDialog(Bundle savedInstanceState) {
105             final Context context = getActivity();
106             final StorageManager storage = context.getSystemService(StorageManager.class);
107 
108             final String fsUuid = getArguments().getString(VolumeRecord.EXTRA_FS_UUID);
109             final VolumeRecord record = storage.findRecordByUuid(fsUuid);
110 
111             final AlertDialog.Builder builder = new AlertDialog.Builder(context);
112             builder.setTitle(TextUtils.expandTemplate(
113                     getText(R.string.storage_internal_forget_confirm_title), record.getNickname()));
114             builder.setMessage(TextUtils.expandTemplate(
115                     getText(R.string.storage_internal_forget_confirm), record.getNickname()));
116 
117             builder.setPositiveButton(R.string.storage_menu_forget,
118                     new DialogInterface.OnClickListener() {
119                 @Override
120                 public void onClick(DialogInterface dialog, int which) {
121                     storage.forgetVolume(fsUuid);
122                     getActivity().finish();
123                 }
124             });
125             builder.setNegativeButton(R.string.cancel, null);
126 
127             return builder.create();
128         }
129     }
130 }
131