1 /*
2  * Copyright (C) 2021 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.storage;
18 
19 import android.app.Dialog;
20 import android.app.settings.SettingsEnums;
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.os.storage.StorageManager;
24 import android.os.storage.VolumeInfo;
25 import android.os.storage.VolumeRecord;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.widget.EditText;
29 
30 import androidx.appcompat.app.AlertDialog;
31 import androidx.fragment.app.Fragment;
32 
33 import com.android.settings.R;
34 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
35 
36 /**
37  * Dialog that allows editing of volume nickname.
38  */
39 public class StorageRenameFragment extends InstrumentedDialogFragment {
40     private static final String TAG_RENAME = "rename";
41 
42     /** Shows the rename dialog. */
show(Fragment parent, VolumeInfo vol)43     public static void show(Fragment parent, VolumeInfo vol) {
44         final StorageRenameFragment dialog = new StorageRenameFragment();
45         dialog.setTargetFragment(parent, 0 /* requestCode */);
46         final Bundle args = new Bundle();
47         args.putString(VolumeRecord.EXTRA_FS_UUID, vol.getFsUuid());
48         dialog.setArguments(args);
49         dialog.show(parent.getFragmentManager(), TAG_RENAME);
50     }
51 
52     @Override
getMetricsCategory()53     public int getMetricsCategory() {
54         return SettingsEnums.DIALOG_VOLUME_RENAME;
55     }
56 
57     @Override
onCreateDialog(Bundle savedInstanceState)58     public Dialog onCreateDialog(Bundle savedInstanceState) {
59         final Context context = getActivity();
60         final StorageManager storageManager = context.getSystemService(StorageManager.class);
61 
62         final String fsUuid = getArguments().getString(VolumeRecord.EXTRA_FS_UUID);
63         final VolumeRecord rec = storageManager.findRecordByUuid(fsUuid);
64 
65         final AlertDialog.Builder builder = new AlertDialog.Builder(context);
66         final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
67 
68         final View view = dialogInflater.inflate(R.layout.dialog_edittext, null, false);
69         final EditText nickname = (EditText) view.findViewById(R.id.edittext);
70         nickname.setText(rec.getNickname());
71         nickname.requestFocus();
72 
73         return builder.setTitle(R.string.storage_rename_title)
74                 .setView(view)
75                 .setPositiveButton(R.string.save, (dialog, which) ->
76                     // TODO: move to background thread
77                     storageManager.setVolumeNickname(fsUuid, nickname.getText().toString()))
78                 .setNegativeButton(R.string.cancel, null)
79                 .create();
80     }
81 }
82