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