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.MetricsProto.MetricsEvent;
37 import com.android.settings.InstrumentedFragment;
38 import com.android.settings.R;
39 
40 public class PrivateVolumeForget extends InstrumentedFragment {
41     private static final String TAG_FORGET_CONFIRM = "forget_confirm";
42 
43     private VolumeRecord mRecord;
44 
45     @Override
getMetricsCategory()46     protected int getMetricsCategory() {
47         return MetricsEvent.DEVICEINFO_STORAGE;
48     }
49 
50     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)51     public View onCreateView(LayoutInflater inflater, ViewGroup container,
52             Bundle savedInstanceState) {
53         final StorageManager storage = getActivity().getSystemService(StorageManager.class);
54         final String fsUuid = getArguments().getString(VolumeRecord.EXTRA_FS_UUID);
55         mRecord = storage.findRecordByUuid(fsUuid);
56 
57         final View view = inflater.inflate(R.layout.storage_internal_forget, container, false);
58         final TextView body = (TextView) view.findViewById(R.id.body);
59         final Button confirm = (Button) view.findViewById(R.id.confirm);
60 
61         body.setText(TextUtils.expandTemplate(getText(R.string.storage_internal_forget_details),
62                 mRecord.getNickname()));
63         confirm.setOnClickListener(mConfirmListener);
64 
65         return view;
66     }
67 
68     private final OnClickListener mConfirmListener = new OnClickListener() {
69         @Override
70         public void onClick(View v) {
71             ForgetConfirmFragment.show(PrivateVolumeForget.this, mRecord.getFsUuid());
72         }
73     };
74 
75     public static class ForgetConfirmFragment extends DialogFragment {
show(Fragment parent, String fsUuid)76         public static void show(Fragment parent, String fsUuid) {
77             final Bundle args = new Bundle();
78             args.putString(VolumeRecord.EXTRA_FS_UUID, fsUuid);
79 
80             final ForgetConfirmFragment dialog = new ForgetConfirmFragment();
81             dialog.setArguments(args);
82             dialog.setTargetFragment(parent, 0);
83             dialog.show(parent.getFragmentManager(), TAG_FORGET_CONFIRM);
84         }
85 
86         @Override
onCreateDialog(Bundle savedInstanceState)87         public Dialog onCreateDialog(Bundle savedInstanceState) {
88             final Context context = getActivity();
89             final StorageManager storage = context.getSystemService(StorageManager.class);
90 
91             final String fsUuid = getArguments().getString(VolumeRecord.EXTRA_FS_UUID);
92             final VolumeRecord record = storage.findRecordByUuid(fsUuid);
93 
94             final AlertDialog.Builder builder = new AlertDialog.Builder(context);
95             builder.setTitle(TextUtils.expandTemplate(
96                     getText(R.string.storage_internal_forget_confirm_title), record.getNickname()));
97             builder.setMessage(TextUtils.expandTemplate(
98                     getText(R.string.storage_internal_forget_confirm), record.getNickname()));
99 
100             builder.setPositiveButton(R.string.storage_menu_forget,
101                     new DialogInterface.OnClickListener() {
102                 @Override
103                 public void onClick(DialogInterface dialog, int which) {
104                     storage.forgetVolume(fsUuid);
105                     getActivity().finish();
106                 }
107             });
108             builder.setNegativeButton(R.string.cancel, null);
109 
110             return builder.create();
111         }
112     }
113 }
114