AppInfoBase.java revision 65ecadc62550b10eb634c14dd671f47b66ea8dbf
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 17package com.android.settings.applications; 18 19import android.app.Activity; 20import android.app.AlertDialog; 21import android.app.Dialog; 22import android.app.DialogFragment; 23import android.app.Fragment; 24import android.app.admin.DevicePolicyManager; 25import android.content.Context; 26import android.content.Intent; 27import android.content.pm.PackageInfo; 28import android.content.pm.PackageManager; 29import android.content.pm.PackageManager.NameNotFoundException; 30import android.hardware.usb.IUsbManager; 31import android.os.Bundle; 32import android.os.IBinder; 33import android.os.ServiceManager; 34import android.os.UserHandle; 35import android.os.UserManager; 36import android.util.Log; 37 38import com.android.settings.InstrumentedPreferenceFragment; 39import com.android.settings.SettingsActivity; 40import com.android.settings.SettingsPreferenceFragment; 41import com.android.settings.Utils; 42import com.android.settings.applications.ApplicationsState.AppEntry; 43 44import java.util.ArrayList; 45 46public abstract class AppInfoBase extends SettingsPreferenceFragment 47 implements ApplicationsState.Callbacks { 48 49 public static final String ARG_PACKAGE_NAME = "package"; 50 public static final String ARG_PACKAGE_UID = "uid"; 51 52 protected static final String TAG = AppInfoBase.class.getSimpleName(); 53 protected static final boolean localLOGV = false; 54 55 protected boolean mAppControlRestricted = false; 56 57 protected ApplicationsState mState; 58 private ApplicationsState.Session mSession; 59 protected ApplicationsState.AppEntry mAppEntry; 60 protected PackageInfo mPackageInfo; 61 protected int mUserId; 62 protected String mPackageName; 63 64 protected IUsbManager mUsbManager; 65 protected DevicePolicyManager mDpm; 66 protected UserManager mUserManager; 67 protected PackageManager mPm; 68 69 // Dialog identifiers used in showDialog 70 protected static final int DLG_BASE = 0; 71 72 protected boolean mFinishing; 73 74 @Override 75 public void onCreate(Bundle savedInstanceState) { 76 super.onCreate(savedInstanceState); 77 mFinishing = false; 78 79 mState = ApplicationsState.getInstance(getActivity().getApplication()); 80 mSession = mState.newSession(this); 81 Context context = getActivity(); 82 mDpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); 83 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 84 mPm = context.getPackageManager(); 85 IBinder b = ServiceManager.getService(Context.USB_SERVICE); 86 mUsbManager = IUsbManager.Stub.asInterface(b); 87 88 // Need to make sure we have loaded applications at this point. 89 mSession.resume(); 90 91 retrieveAppEntry(); 92 } 93 94 @Override 95 public void onResume() { 96 super.onResume(); 97 mAppControlRestricted = mUserManager.hasUserRestriction(UserManager.DISALLOW_APPS_CONTROL); 98 mSession.resume(); 99 100 if (!refreshUi()) { 101 setIntentAndFinish(true, true); 102 } 103 } 104 105 @Override 106 public void onPause() { 107 super.onPause(); 108 mSession.pause(); 109 } 110 111 @Override 112 public void onDestroyView() { 113 super.onDestroyView(); 114 mSession.release(); 115 } 116 117 protected String retrieveAppEntry() { 118 final Bundle args = getArguments(); 119 mPackageName = (args != null) ? args.getString(ARG_PACKAGE_NAME) : null; 120 if (mPackageName == null) { 121 Intent intent = (args == null) ? 122 getActivity().getIntent() : (Intent) args.getParcelable("intent"); 123 if (intent != null) { 124 mPackageName = intent.getData().getSchemeSpecificPart(); 125 } 126 } 127 mUserId = UserHandle.myUserId(); 128 mAppEntry = mState.getEntry(mPackageName, mUserId); 129 if (mAppEntry != null) { 130 // Get application info again to refresh changed properties of application 131 try { 132 mPackageInfo = mPm.getPackageInfo(mAppEntry.info.packageName, 133 PackageManager.GET_DISABLED_COMPONENTS | 134 PackageManager.GET_UNINSTALLED_PACKAGES | 135 PackageManager.GET_SIGNATURES); 136 } catch (NameNotFoundException e) { 137 Log.e(TAG, "Exception when retrieving package:" + mAppEntry.info.packageName, e); 138 } 139 } else { 140 Log.w(TAG, "Missing AppEntry; maybe reinstalling?"); 141 mPackageInfo = null; 142 } 143 144 return mPackageName; 145 } 146 147 protected void setIntentAndFinish(boolean finish, boolean appChanged) { 148 if (localLOGV) Log.i(TAG, "appChanged="+appChanged); 149 Intent intent = new Intent(); 150 intent.putExtra(ManageApplications.APP_CHG, appChanged); 151 SettingsActivity sa = (SettingsActivity)getActivity(); 152 sa.finishPreferencePanel(this, Activity.RESULT_OK, intent); 153 mFinishing = true; 154 } 155 156 protected void showDialogInner(int id, int moveErrorCode) { 157 DialogFragment newFragment = new MyAlertDialogFragment(id, moveErrorCode); 158 newFragment.setTargetFragment(this, 0); 159 newFragment.show(getFragmentManager(), "dialog " + id); 160 } 161 162 protected abstract boolean refreshUi(); 163 protected abstract AlertDialog createDialog(int id, int errorCode); 164 165 @Override 166 public void onRunningStateChanged(boolean running) { 167 // No op. 168 } 169 170 @Override 171 public void onRebuildComplete(ArrayList<AppEntry> apps) { 172 // No op. 173 } 174 175 @Override 176 public void onPackageIconChanged() { 177 // No op. 178 } 179 180 @Override 181 public void onPackageSizeChanged(String packageName) { 182 // No op. 183 } 184 185 @Override 186 public void onAllSizesComputed() { 187 // No op. 188 } 189 190 @Override 191 public void onLauncherInfoChanged() { 192 // No op. 193 } 194 195 @Override 196 public void onLoadEntriesCompleted() { 197 // No op. 198 } 199 200 @Override 201 public void onPackageListChanged() { 202 refreshUi(); 203 } 204 205 public static void startAppInfoFragment(Class<?> fragment, int titleRes, 206 String pkg, int uid, Fragment source, int request) { 207 Bundle args = new Bundle(); 208 args.putString(AppInfoBase.ARG_PACKAGE_NAME, pkg); 209 args.putInt(AppInfoBase.ARG_PACKAGE_UID, uid); 210 211 Intent intent = Utils.onBuildStartFragmentIntent(source.getActivity(), fragment.getName(), 212 args, null, titleRes, null, false); 213 source.getActivity().startActivityForResultAsUser(intent, request, 214 new UserHandle(UserHandle.getUserId(uid))); 215 } 216 217 public class MyAlertDialogFragment extends DialogFragment { 218 public MyAlertDialogFragment(int id, int errorCode) { 219 Bundle args = new Bundle(); 220 args.putInt("id", id); 221 args.putInt("moveError", errorCode); 222 setArguments(args); 223 } 224 225 @Override 226 public Dialog onCreateDialog(Bundle savedInstanceState) { 227 int id = getArguments().getInt("id"); 228 int errorCode = getArguments().getInt("moveError"); 229 Dialog dialog = createDialog(id, errorCode); 230 if (dialog == null) { 231 throw new IllegalArgumentException("unknown id " + id); 232 } 233 return dialog; 234 } 235 } 236} 237