1631c8b1605589e6935f429b87d640aeba03999ccTony Mantler/* 2631c8b1605589e6935f429b87d640aeba03999ccTony Mantler * Copyright (C) 2015 The Android Open Source Project 3631c8b1605589e6935f429b87d640aeba03999ccTony Mantler * 4631c8b1605589e6935f429b87d640aeba03999ccTony Mantler * Licensed under the Apache License, Version 2.0 (the "License"); 5631c8b1605589e6935f429b87d640aeba03999ccTony Mantler * you may not use this file except in compliance with the License. 6631c8b1605589e6935f429b87d640aeba03999ccTony Mantler * You may obtain a copy of the License at 7631c8b1605589e6935f429b87d640aeba03999ccTony Mantler * 8631c8b1605589e6935f429b87d640aeba03999ccTony Mantler * http://www.apache.org/licenses/LICENSE-2.0 9631c8b1605589e6935f429b87d640aeba03999ccTony Mantler * 10631c8b1605589e6935f429b87d640aeba03999ccTony Mantler * Unless required by applicable law or agreed to in writing, software 11631c8b1605589e6935f429b87d640aeba03999ccTony Mantler * distributed under the License is distributed on an "AS IS" BASIS, 12631c8b1605589e6935f429b87d640aeba03999ccTony Mantler * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13631c8b1605589e6935f429b87d640aeba03999ccTony Mantler * See the License for the specific language governing permissions and 14631c8b1605589e6935f429b87d640aeba03999ccTony Mantler * limitations under the License 15631c8b1605589e6935f429b87d640aeba03999ccTony Mantler */ 16631c8b1605589e6935f429b87d640aeba03999ccTony Mantler 17631c8b1605589e6935f429b87d640aeba03999ccTony Mantlerpackage com.android.tv.settings.device.storage; 18631c8b1605589e6935f429b87d640aeba03999ccTony Mantler 19631c8b1605589e6935f429b87d640aeba03999ccTony Mantlerimport android.content.pm.ApplicationInfo; 20631c8b1605589e6935f429b87d640aeba03999ccTony Mantlerimport android.content.pm.PackageManager; 21631c8b1605589e6935f429b87d640aeba03999ccTony Mantlerimport android.graphics.drawable.Drawable; 22631c8b1605589e6935f429b87d640aeba03999ccTony Mantlerimport android.os.Bundle; 23631c8b1605589e6935f429b87d640aeba03999ccTony Mantlerimport android.os.storage.StorageManager; 24631c8b1605589e6935f429b87d640aeba03999ccTony Mantlerimport android.os.storage.VolumeInfo; 25631c8b1605589e6935f429b87d640aeba03999ccTony Mantlerimport android.support.annotation.NonNull; 262a55a49f5a2d3f102c11f822c76f190a00249d50Tony Mantlerimport android.support.v17.leanback.app.GuidedStepFragment; 27631c8b1605589e6935f429b87d640aeba03999ccTony Mantlerimport android.support.v17.leanback.widget.GuidanceStylist; 28631c8b1605589e6935f429b87d640aeba03999ccTony Mantlerimport android.support.v17.leanback.widget.GuidedAction; 29631c8b1605589e6935f429b87d640aeba03999ccTony Mantlerimport android.text.TextUtils; 30631c8b1605589e6935f429b87d640aeba03999ccTony Mantlerimport android.text.format.Formatter; 31631c8b1605589e6935f429b87d640aeba03999ccTony Mantlerimport android.util.Log; 32631c8b1605589e6935f429b87d640aeba03999ccTony Mantler 33631c8b1605589e6935f429b87d640aeba03999ccTony Mantlerimport com.android.tv.settings.R; 34631c8b1605589e6935f429b87d640aeba03999ccTony Mantler 35631c8b1605589e6935f429b87d640aeba03999ccTony Mantlerimport java.io.File; 36631c8b1605589e6935f429b87d640aeba03999ccTony Mantlerimport java.util.List; 37631c8b1605589e6935f429b87d640aeba03999ccTony Mantler 382a55a49f5a2d3f102c11f822c76f190a00249d50Tony Mantlerpublic class MoveAppStepFragment extends GuidedStepFragment { 39631c8b1605589e6935f429b87d640aeba03999ccTony Mantler 40631c8b1605589e6935f429b87d640aeba03999ccTony Mantler private static final String TAG = "MoveAppStepFragment"; 41631c8b1605589e6935f429b87d640aeba03999ccTony Mantler 42631c8b1605589e6935f429b87d640aeba03999ccTony Mantler private static final String ARG_PACKAGE_NAME = "packageName"; 43631c8b1605589e6935f429b87d640aeba03999ccTony Mantler private static final String ARG_PACKAGE_DESC = "packageDesc"; 44631c8b1605589e6935f429b87d640aeba03999ccTony Mantler 45631c8b1605589e6935f429b87d640aeba03999ccTony Mantler private PackageManager mPackageManager; 46631c8b1605589e6935f429b87d640aeba03999ccTony Mantler private StorageManager mStorageManager; 47631c8b1605589e6935f429b87d640aeba03999ccTony Mantler 48631c8b1605589e6935f429b87d640aeba03999ccTony Mantler private String mPackageName; 49631c8b1605589e6935f429b87d640aeba03999ccTony Mantler private String mPackageDesc; 50631c8b1605589e6935f429b87d640aeba03999ccTony Mantler private List<VolumeInfo> mCandidateVolumes; 5144b99342a04c5f3b2d001a7ffc4e8ddcf06b914dTony Mantler private VolumeInfo mCurrentVolume; 52631c8b1605589e6935f429b87d640aeba03999ccTony Mantler 53697bc6350e01bc9cf10ab07e1dc200b87db6bc71Tony Mantler public interface Callback { 54697bc6350e01bc9cf10ab07e1dc200b87db6bc71Tony Mantler void onRequestMovePackageToVolume(String packageName, VolumeInfo destination); 55697bc6350e01bc9cf10ab07e1dc200b87db6bc71Tony Mantler } 56697bc6350e01bc9cf10ab07e1dc200b87db6bc71Tony Mantler 57631c8b1605589e6935f429b87d640aeba03999ccTony Mantler public static MoveAppStepFragment newInstance(String packageName, String packageDesc) { 58631c8b1605589e6935f429b87d640aeba03999ccTony Mantler final MoveAppStepFragment fragment = new MoveAppStepFragment(); 59631c8b1605589e6935f429b87d640aeba03999ccTony Mantler final Bundle b = new Bundle(2); 60631c8b1605589e6935f429b87d640aeba03999ccTony Mantler b.putString(ARG_PACKAGE_NAME, packageName); 61631c8b1605589e6935f429b87d640aeba03999ccTony Mantler b.putString(ARG_PACKAGE_DESC, packageDesc); 62631c8b1605589e6935f429b87d640aeba03999ccTony Mantler fragment.setArguments(b); 63631c8b1605589e6935f429b87d640aeba03999ccTony Mantler return fragment; 64631c8b1605589e6935f429b87d640aeba03999ccTony Mantler } 65631c8b1605589e6935f429b87d640aeba03999ccTony Mantler 66631c8b1605589e6935f429b87d640aeba03999ccTony Mantler @Override 67631c8b1605589e6935f429b87d640aeba03999ccTony Mantler public void onCreate(Bundle savedInstanceState) { 68631c8b1605589e6935f429b87d640aeba03999ccTony Mantler // Need mPackageManager before onCreateActions, which is called from super.onCreate 69631c8b1605589e6935f429b87d640aeba03999ccTony Mantler mPackageManager = getActivity().getPackageManager(); 70631c8b1605589e6935f429b87d640aeba03999ccTony Mantler mStorageManager = getActivity().getSystemService(StorageManager.class); 71631c8b1605589e6935f429b87d640aeba03999ccTony Mantler 72631c8b1605589e6935f429b87d640aeba03999ccTony Mantler mPackageDesc = getArguments().getString(ARG_PACKAGE_DESC, ""); 73631c8b1605589e6935f429b87d640aeba03999ccTony Mantler mPackageName = getArguments().getString(ARG_PACKAGE_NAME); 74631c8b1605589e6935f429b87d640aeba03999ccTony Mantler 75631c8b1605589e6935f429b87d640aeba03999ccTony Mantler super.onCreate(savedInstanceState); 76631c8b1605589e6935f429b87d640aeba03999ccTony Mantler } 77631c8b1605589e6935f429b87d640aeba03999ccTony Mantler 78631c8b1605589e6935f429b87d640aeba03999ccTony Mantler @Override 79631c8b1605589e6935f429b87d640aeba03999ccTony Mantler public void onResume() { 80631c8b1605589e6935f429b87d640aeba03999ccTony Mantler super.onResume(); 81631c8b1605589e6935f429b87d640aeba03999ccTony Mantler } 82631c8b1605589e6935f429b87d640aeba03999ccTony Mantler 83631c8b1605589e6935f429b87d640aeba03999ccTony Mantler @Override 84631c8b1605589e6935f429b87d640aeba03999ccTony Mantler public @NonNull GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) { 85631c8b1605589e6935f429b87d640aeba03999ccTony Mantler Drawable icon; 86631c8b1605589e6935f429b87d640aeba03999ccTony Mantler try { 87631c8b1605589e6935f429b87d640aeba03999ccTony Mantler icon = mPackageManager.getApplicationIcon(mPackageName); 88631c8b1605589e6935f429b87d640aeba03999ccTony Mantler } catch (PackageManager.NameNotFoundException e) { 89631c8b1605589e6935f429b87d640aeba03999ccTony Mantler Log.d(TAG, "Missing package while resolving icon", e); 90631c8b1605589e6935f429b87d640aeba03999ccTony Mantler icon = null; 91631c8b1605589e6935f429b87d640aeba03999ccTony Mantler } 92631c8b1605589e6935f429b87d640aeba03999ccTony Mantler return new GuidanceStylist.Guidance(getString(R.string.storage_wizard_move_app_title), 93631c8b1605589e6935f429b87d640aeba03999ccTony Mantler null, 94631c8b1605589e6935f429b87d640aeba03999ccTony Mantler mPackageDesc, 95631c8b1605589e6935f429b87d640aeba03999ccTony Mantler icon); 96631c8b1605589e6935f429b87d640aeba03999ccTony Mantler } 97631c8b1605589e6935f429b87d640aeba03999ccTony Mantler 98631c8b1605589e6935f429b87d640aeba03999ccTony Mantler @Override 99631c8b1605589e6935f429b87d640aeba03999ccTony Mantler public void onCreateActions(@NonNull List<GuidedAction> actions, Bundle savedInstanceState) { 100631c8b1605589e6935f429b87d640aeba03999ccTony Mantler final String packageName = getArguments().getString(ARG_PACKAGE_NAME); 101631c8b1605589e6935f429b87d640aeba03999ccTony Mantler final ApplicationInfo info; 102631c8b1605589e6935f429b87d640aeba03999ccTony Mantler try { 103631c8b1605589e6935f429b87d640aeba03999ccTony Mantler info = mPackageManager.getApplicationInfo(packageName, 0); 104631c8b1605589e6935f429b87d640aeba03999ccTony Mantler } catch (PackageManager.NameNotFoundException e) { 105631c8b1605589e6935f429b87d640aeba03999ccTony Mantler Log.d(TAG, "Package missing while resolving storage", e); 106631c8b1605589e6935f429b87d640aeba03999ccTony Mantler return; 107631c8b1605589e6935f429b87d640aeba03999ccTony Mantler } 10844b99342a04c5f3b2d001a7ffc4e8ddcf06b914dTony Mantler mCurrentVolume = mPackageManager.getPackageCurrentVolume(info); 109631c8b1605589e6935f429b87d640aeba03999ccTony Mantler mCandidateVolumes = mPackageManager.getPackageCandidateVolumes(info); 110631c8b1605589e6935f429b87d640aeba03999ccTony Mantler 111631c8b1605589e6935f429b87d640aeba03999ccTony Mantler for (final VolumeInfo candidate : mCandidateVolumes) { 112631c8b1605589e6935f429b87d640aeba03999ccTony Mantler if (!candidate.isMountedWritable()) { 113631c8b1605589e6935f429b87d640aeba03999ccTony Mantler continue; 114631c8b1605589e6935f429b87d640aeba03999ccTony Mantler } 115631c8b1605589e6935f429b87d640aeba03999ccTony Mantler final File path = candidate.getPath(); 116631c8b1605589e6935f429b87d640aeba03999ccTony Mantler final String avail = Formatter.formatFileSize(getActivity(), path.getFreeSpace()); 1172a55a49f5a2d3f102c11f822c76f190a00249d50Tony Mantler actions.add(new GuidedAction.Builder(getContext()) 118631c8b1605589e6935f429b87d640aeba03999ccTony Mantler .title(mStorageManager.getBestVolumeDescription(candidate)) 119631c8b1605589e6935f429b87d640aeba03999ccTony Mantler .description( 120631c8b1605589e6935f429b87d640aeba03999ccTony Mantler getString(R.string.storage_wizard_back_up_apps_space_available, avail)) 12144b99342a04c5f3b2d001a7ffc4e8ddcf06b914dTony Mantler .checked(TextUtils.equals(mCurrentVolume.getId(), candidate.getId())) 122631c8b1605589e6935f429b87d640aeba03999ccTony Mantler .checkSetId(GuidedAction.DEFAULT_CHECK_SET_ID) 123631c8b1605589e6935f429b87d640aeba03999ccTony Mantler .id(mCandidateVolumes.indexOf(candidate)) 124631c8b1605589e6935f429b87d640aeba03999ccTony Mantler .build()); 125631c8b1605589e6935f429b87d640aeba03999ccTony Mantler } 126631c8b1605589e6935f429b87d640aeba03999ccTony Mantler } 127631c8b1605589e6935f429b87d640aeba03999ccTony Mantler 128631c8b1605589e6935f429b87d640aeba03999ccTony Mantler @Override 129631c8b1605589e6935f429b87d640aeba03999ccTony Mantler public void onGuidedActionClicked(GuidedAction action) { 130697bc6350e01bc9cf10ab07e1dc200b87db6bc71Tony Mantler final Callback callback = (Callback) getActivity(); 13144b99342a04c5f3b2d001a7ffc4e8ddcf06b914dTony Mantler final VolumeInfo destination = mCandidateVolumes.get((int) action.getId()); 13244b99342a04c5f3b2d001a7ffc4e8ddcf06b914dTony Mantler if (destination.equals(mCurrentVolume)) { 13344b99342a04c5f3b2d001a7ffc4e8ddcf06b914dTony Mantler if (!getFragmentManager().popBackStackImmediate()) { 13444b99342a04c5f3b2d001a7ffc4e8ddcf06b914dTony Mantler getActivity().finish(); 13544b99342a04c5f3b2d001a7ffc4e8ddcf06b914dTony Mantler } 13644b99342a04c5f3b2d001a7ffc4e8ddcf06b914dTony Mantler } else { 13744b99342a04c5f3b2d001a7ffc4e8ddcf06b914dTony Mantler callback.onRequestMovePackageToVolume(mPackageName, destination); 13844b99342a04c5f3b2d001a7ffc4e8ddcf06b914dTony Mantler } 139631c8b1605589e6935f429b87d640aeba03999ccTony Mantler } 140631c8b1605589e6935f429b87d640aeba03999ccTony Mantler 141631c8b1605589e6935f429b87d640aeba03999ccTony Mantler} 142