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.tv.settings.device.storage;
18 
19 import android.app.Fragment;
20 import android.os.Bundle;
21 import android.os.storage.DiskInfo;
22 import android.os.storage.StorageManager;
23 import android.os.storage.VolumeInfo;
24 import android.text.TextUtils;
25 
26 import androidx.annotation.NonNull;
27 import androidx.leanback.app.GuidedStepFragment;
28 import androidx.leanback.widget.GuidanceStylist;
29 import androidx.leanback.widget.GuidedAction;
30 
31 import com.android.tv.settings.R;
32 
33 import java.util.List;
34 
35 public class FormatAsPublicStepFragment extends GuidedStepFragment {
36     private static final int ACTION_ID_BACKUP = 1;
37     private static final int ACTION_ID_FORMAT = 2;
38 
39     private String mVolumeId;
40     private String mDiskId;
41 
42     public interface Callback {
onRequestFormatAsPublic(String diskId, String volumeId)43         void onRequestFormatAsPublic(String diskId, String volumeId);
onCancelFormatDialog()44         void onCancelFormatDialog();
45     }
46 
newInstance(String diskId)47     public static FormatAsPublicStepFragment newInstance(String diskId) {
48         final FormatAsPublicStepFragment fragment = new FormatAsPublicStepFragment();
49         final Bundle b = new Bundle(1);
50         b.putString(DiskInfo.EXTRA_DISK_ID, diskId);
51         fragment.setArguments(b);
52         return fragment;
53     }
54 
55     @Override
onCreate(Bundle savedInstanceState)56     public void onCreate(Bundle savedInstanceState) {
57         mDiskId = getArguments().getString(DiskInfo.EXTRA_DISK_ID);
58         final StorageManager storageManager = getActivity().getSystemService(StorageManager.class);
59         final List<VolumeInfo> volumes = storageManager.getVolumes();
60         for (final VolumeInfo volume : volumes) {
61             if ((volume.getType() == VolumeInfo.TYPE_PRIVATE ||
62                     volume.getType() == VolumeInfo.TYPE_PUBLIC) &&
63                     TextUtils.equals(volume.getDiskId(), mDiskId)) {
64                 mVolumeId = volume.getId();
65                 break;
66             }
67         }
68         super.onCreate(savedInstanceState);
69     }
70 
71     @Override
onCreateGuidance(Bundle savedInstanceState)72     public @NonNull GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
73         return new GuidanceStylist.Guidance(
74                 getString(R.string.storage_wizard_format_as_public_title),
75                 getString(R.string.storage_wizard_format_as_public_description), "",
76                 getActivity().getDrawable(R.drawable.ic_warning_132dp));
77     }
78 
79     @Override
onCreateActions(@onNull List<GuidedAction> actions, Bundle savedInstanceState)80     public void onCreateActions(@NonNull List<GuidedAction> actions, Bundle savedInstanceState) {
81         actions.add(new GuidedAction.Builder(getContext())
82                 .clickAction(GuidedAction.ACTION_ID_CANCEL)
83                 .build());
84         if (!TextUtils.isEmpty(mVolumeId)) {
85             actions.add(new GuidedAction.Builder(getContext())
86                     .id(ACTION_ID_BACKUP)
87                     .title(getString(R.string.storage_wizard_backup_apps_action))
88                     .build());
89         }
90         actions.add(new GuidedAction.Builder(getContext())
91                 .id(ACTION_ID_FORMAT)
92                 .title(getString(R.string.storage_wizard_format_action))
93                 .build());
94     }
95 
96     @Override
onGuidedActionClicked(GuidedAction action)97     public void onGuidedActionClicked(GuidedAction action) {
98         final long id = action.getId();
99 
100         if (id == GuidedAction.ACTION_ID_CANCEL) {
101             ((Callback) getActivity()).onCancelFormatDialog();
102         } else if (id == ACTION_ID_BACKUP) {
103             final Fragment f = BackupAppsStepFragment.newInstance(mVolumeId);
104             getFragmentManager().beginTransaction()
105                     .replace(android.R.id.content, f)
106                     .addToBackStack(null)
107                     .commit();
108         } else if (id == ACTION_ID_FORMAT) {
109             ((Callback) getActivity()).onRequestFormatAsPublic(mDiskId, mVolumeId);
110         }
111     }
112 }
113