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.apps; 18 19 import android.annotation.Nullable; 20 import android.app.Activity; 21 import android.app.Fragment; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.pm.ApplicationInfo; 25 import android.content.pm.PackageManager; 26 import android.os.Bundle; 27 import android.os.Handler; 28 import android.os.storage.VolumeInfo; 29 import android.util.Log; 30 import android.widget.Toast; 31 32 import androidx.annotation.NonNull; 33 34 import com.android.tv.settings.device.storage.MoveAppProgressFragment; 35 import com.android.tv.settings.device.storage.MoveAppStepFragment; 36 37 public class MoveAppActivity extends Activity implements MoveAppStepFragment.Callback { 38 39 private static final String TAG = "MoveAppActivity"; 40 41 private static final String SAVE_STATE_MOVE_ID = "MoveAppActivity.moveId"; 42 43 private static final String ARG_PACKAGE_NAME = "packageName"; 44 private static final String ARG_PACKAGE_DESC = "packageDesc"; 45 46 private PackageManager mPackageManager; 47 private int mAppMoveId = -1; 48 private final PackageManager.MoveCallback mMoveCallback = new PackageManager.MoveCallback() { 49 @Override 50 public void onStatusChanged(int moveId, int status, long estMillis) { 51 if (moveId != mAppMoveId || !PackageManager.isMoveStatusFinished(status)) { 52 return; 53 } 54 55 finish(); 56 57 if (status != PackageManager.MOVE_SUCCEEDED) { 58 Log.d(TAG, "Move failure status: " + status); 59 Toast.makeText(MoveAppActivity.this, 60 MoveAppProgressFragment.moveStatusToMessage(MoveAppActivity.this, status), 61 Toast.LENGTH_LONG).show(); 62 } 63 } 64 }; 65 getLaunchIntent(Context context, String packageName, String packageDesc)66 public static Intent getLaunchIntent(Context context, String packageName, String packageDesc) { 67 final Intent i = new Intent(context, MoveAppActivity.class); 68 i.putExtra(ARG_PACKAGE_NAME, packageName); 69 i.putExtra(ARG_PACKAGE_DESC, packageDesc); 70 return i; 71 } 72 73 @Override onCreate(@ullable Bundle savedInstanceState)74 protected void onCreate(@Nullable Bundle savedInstanceState) { 75 super.onCreate(savedInstanceState); 76 77 mPackageManager = getPackageManager(); 78 mPackageManager.registerMoveCallback(mMoveCallback, new Handler()); 79 80 if (savedInstanceState != null) { 81 mAppMoveId = savedInstanceState.getInt(SAVE_STATE_MOVE_ID); 82 } else { 83 final String packageDesc = getIntent().getStringExtra(ARG_PACKAGE_DESC); 84 final String packageName = getIntent().getStringExtra(ARG_PACKAGE_NAME); 85 86 final Fragment fragment = MoveAppStepFragment.newInstance(packageName, packageDesc); 87 getFragmentManager().beginTransaction() 88 .add(android.R.id.content, fragment) 89 .commit(); 90 } 91 } 92 93 @Override onSaveInstanceState(@onNull Bundle outState)94 protected void onSaveInstanceState(@NonNull Bundle outState) { 95 super.onSaveInstanceState(outState); 96 outState.putInt(SAVE_STATE_MOVE_ID, mAppMoveId); 97 } 98 99 @Override onDestroy()100 protected void onDestroy() { 101 super.onDestroy(); 102 mPackageManager.unregisterMoveCallback(mMoveCallback); 103 } 104 105 @Override onRequestMovePackageToVolume(String packageName, VolumeInfo destination)106 public void onRequestMovePackageToVolume(String packageName, VolumeInfo destination) { 107 mAppMoveId = mPackageManager.movePackage(packageName, destination); 108 final ApplicationInfo applicationInfo; 109 try { 110 applicationInfo = mPackageManager 111 .getApplicationInfo(packageName, 0); 112 } catch (final PackageManager.NameNotFoundException e) { 113 throw new IllegalStateException(e); 114 } 115 116 final MoveAppProgressFragment fragment = MoveAppProgressFragment 117 .newInstance(mPackageManager.getApplicationLabel(applicationInfo)); 118 119 getFragmentManager().beginTransaction() 120 .replace(android.R.id.content, fragment) 121 .commit(); 122 } 123 124 } 125