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.content.Intent;
23 import android.os.Bundle;
24 import android.os.storage.DiskInfo;
25 import android.os.storage.StorageManager;
26 import android.text.TextUtils;
27 
28 import androidx.appcompat.app.AlertDialog;
29 import androidx.fragment.app.Fragment;
30 
31 import com.android.settings.R;
32 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
33 import com.android.settings.deviceinfo.StorageWizardInit;
34 
35 /** A dialog which guides users to initialize a specified unsupported disk. */
36 public class DiskInitFragment extends InstrumentedDialogFragment {
37 
38     private static final String TAG_DISK_INIT = "disk_init";
39 
40     @Override
getMetricsCategory()41     public int getMetricsCategory() {
42         return SettingsEnums.DIALOG_VOLUME_INIT;
43     }
44 
45     /** Shows the dialog for the specified diskId from DiskInfo. */
show(Fragment parent, int resId, String diskId)46     public static void show(Fragment parent, int resId, String diskId) {
47         final Bundle args = new Bundle();
48         args.putInt(Intent.EXTRA_TEXT, resId);
49         args.putString(DiskInfo.EXTRA_DISK_ID, diskId);
50 
51         final DiskInitFragment dialog = new DiskInitFragment();
52         dialog.setArguments(args);
53         dialog.setTargetFragment(parent, 0);
54         dialog.show(parent.getFragmentManager(), TAG_DISK_INIT);
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         final int resId = getArguments().getInt(Intent.EXTRA_TEXT);
62         final String diskId = getArguments().getString(DiskInfo.EXTRA_DISK_ID);
63         final DiskInfo disk = storageManager.findDiskById(diskId);
64 
65         final AlertDialog.Builder builder = new AlertDialog.Builder(context);
66         return builder.setMessage(TextUtils.expandTemplate(getText(resId), disk.getDescription()))
67                 .setPositiveButton(R.string.storage_menu_set_up, (dialog, which) -> {
68                     final Intent intent = new Intent(context, StorageWizardInit.class);
69                     intent.putExtra(DiskInfo.EXTRA_DISK_ID, diskId);
70                     startActivity(intent); })
71                 .setNegativeButton(R.string.cancel, null)
72                 .create();
73     }
74 }
75 
76