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