AppInfoBase.java revision 265d3c2a0c36251bf8a9f571d7239b6dd404d942
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.BroadcastReceiver; 26import android.content.Context; 27import android.content.Intent; 28import android.content.IntentFilter; 29import android.content.pm.PackageInfo; 30import android.content.pm.PackageManager; 31import android.content.pm.PackageManager.NameNotFoundException; 32import android.hardware.usb.IUsbManager; 33import android.os.Bundle; 34import android.os.IBinder; 35import android.os.ServiceManager; 36import android.os.UserHandle; 37import android.os.UserManager; 38import android.util.Log; 39 40import com.android.internal.logging.nano.MetricsProto; 41import com.android.settings.SettingsActivity; 42import com.android.settings.SettingsPreferenceFragment; 43import com.android.settings.Utils; 44import com.android.settings.core.instrumentation.InstrumentedDialogFragment; 45import com.android.settingslib.RestrictedLockUtils; 46import com.android.settingslib.applications.ApplicationsState; 47import com.android.settingslib.applications.ApplicationsState.AppEntry; 48 49import java.util.ArrayList; 50 51import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 52 53public abstract class AppInfoBase extends SettingsPreferenceFragment 54 implements ApplicationsState.Callbacks { 55 56 public static final String ARG_PACKAGE_NAME = "package"; 57 public static final String ARG_PACKAGE_UID = "uid"; 58 59 protected static final String TAG = AppInfoBase.class.getSimpleName(); 60 protected static final boolean localLOGV = false; 61 62 protected EnforcedAdmin mAppsControlDisallowedAdmin; 63 protected boolean mAppsControlDisallowedBySystem; 64 65 protected ApplicationsState mState; 66 protected ApplicationsState.Session mSession; 67 protected ApplicationsState.AppEntry mAppEntry; 68 protected PackageInfo mPackageInfo; 69 protected int mUserId; 70 protected String mPackageName; 71 72 protected IUsbManager mUsbManager; 73 protected DevicePolicyManager mDpm; 74 protected UserManager mUserManager; 75 protected PackageManager mPm; 76 77 // Dialog identifiers used in showDialog 78 protected static final int DLG_BASE = 0; 79 80 protected boolean mFinishing; 81 protected boolean mListeningToPackageRemove; 82 83 @Override 84 public void onCreate(Bundle savedInstanceState) { 85 super.onCreate(savedInstanceState); 86 mFinishing = false; 87 88 mState = ApplicationsState.getInstance(getActivity().getApplication()); 89 mSession = mState.newSession(this); 90 Context context = getActivity(); 91 mDpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); 92 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 93 mPm = context.getPackageManager(); 94 IBinder b = ServiceManager.getService(Context.USB_SERVICE); 95 mUsbManager = IUsbManager.Stub.asInterface(b); 96 97 retrieveAppEntry(); 98 startListeningToPackageRemove(); 99 } 100 101 @Override 102 public void onResume() { 103 super.onResume(); 104 mSession.resume(); 105 mAppsControlDisallowedAdmin = RestrictedLockUtils.checkIfRestrictionEnforced(getActivity(), 106 UserManager.DISALLOW_APPS_CONTROL, mUserId); 107 mAppsControlDisallowedBySystem = RestrictedLockUtils.hasBaseUserRestriction(getActivity(), 108 UserManager.DISALLOW_APPS_CONTROL, mUserId); 109 110 if (!refreshUi()) { 111 setIntentAndFinish(true, true); 112 } 113 } 114 115 @Override 116 public void onPause() { 117 mSession.pause(); 118 super.onPause(); 119 } 120 121 @Override 122 public void onDestroy() { 123 stopListeningToPackageRemove(); 124 mSession.release(); 125 super.onDestroy(); 126 } 127 128 protected String retrieveAppEntry() { 129 final Bundle args = getArguments(); 130 mPackageName = (args != null) ? args.getString(ARG_PACKAGE_NAME) : null; 131 if (mPackageName == null) { 132 Intent intent = (args == null) ? 133 getActivity().getIntent() : (Intent) args.getParcelable("intent"); 134 if (intent != null) { 135 mPackageName = intent.getData().getSchemeSpecificPart(); 136 } 137 } 138 mUserId = UserHandle.myUserId(); 139 mAppEntry = mState.getEntry(mPackageName, mUserId); 140 if (mAppEntry != null) { 141 // Get application info again to refresh changed properties of application 142 try { 143 mPackageInfo = mPm.getPackageInfo(mAppEntry.info.packageName, 144 PackageManager.GET_DISABLED_COMPONENTS | 145 PackageManager.GET_UNINSTALLED_PACKAGES | 146 PackageManager.GET_SIGNATURES | 147 PackageManager.GET_PERMISSIONS); 148 } catch (NameNotFoundException e) { 149 Log.e(TAG, "Exception when retrieving package:" + mAppEntry.info.packageName, e); 150 } 151 } else { 152 Log.w(TAG, "Missing AppEntry; maybe reinstalling?"); 153 mPackageInfo = null; 154 } 155 156 return mPackageName; 157 } 158 159 protected void setIntentAndFinish(boolean finish, boolean appChanged) { 160 if (localLOGV) Log.i(TAG, "appChanged="+appChanged); 161 Intent intent = new Intent(); 162 intent.putExtra(ManageApplications.APP_CHG, appChanged); 163 SettingsActivity sa = (SettingsActivity)getActivity(); 164 sa.finishPreferencePanel(this, Activity.RESULT_OK, intent); 165 mFinishing = true; 166 } 167 168 protected void showDialogInner(int id, int moveErrorCode) { 169 DialogFragment newFragment = MyAlertDialogFragment.newInstance(id, moveErrorCode); 170 newFragment.setTargetFragment(this, 0); 171 newFragment.show(getFragmentManager(), "dialog " + id); 172 } 173 174 protected abstract boolean refreshUi(); 175 protected abstract AlertDialog createDialog(int id, int errorCode); 176 177 @Override 178 public void onRunningStateChanged(boolean running) { 179 // No op. 180 } 181 182 @Override 183 public void onRebuildComplete(ArrayList<AppEntry> apps) { 184 // No op. 185 } 186 187 @Override 188 public void onPackageIconChanged() { 189 // No op. 190 } 191 192 @Override 193 public void onPackageSizeChanged(String packageName) { 194 // No op. 195 } 196 197 @Override 198 public void onAllSizesComputed() { 199 // No op. 200 } 201 202 @Override 203 public void onLauncherInfoChanged() { 204 // No op. 205 } 206 207 @Override 208 public void onLoadEntriesCompleted() { 209 // No op. 210 } 211 212 @Override 213 public void onPackageListChanged() { 214 refreshUi(); 215 } 216 217 public static void startAppInfoFragment(Class<?> fragment, int titleRes, 218 String pkg, int uid, Fragment source, int request) { 219 startAppInfoFragment(fragment, titleRes, pkg, uid, source.getActivity(), request); 220 } 221 222 public static void startAppInfoFragment(Class<?> fragment, int titleRes, 223 String pkg, int uid, Activity source, int request) { 224 Bundle args = new Bundle(); 225 args.putString(AppInfoBase.ARG_PACKAGE_NAME, pkg); 226 args.putInt(AppInfoBase.ARG_PACKAGE_UID, uid); 227 228 Intent intent = Utils.onBuildStartFragmentIntent(source, fragment.getName(), 229 args, null, titleRes, null, false); 230 source.startActivityForResultAsUser(intent, request, 231 new UserHandle(UserHandle.getUserId(uid))); 232 } 233 234 public static class MyAlertDialogFragment extends InstrumentedDialogFragment { 235 236 private static final String ARG_ID = "id"; 237 238 @Override 239 public int getMetricsCategory() { 240 return MetricsProto.MetricsEvent.DIALOG_APP_INFO_ACTION; 241 } 242 243 @Override 244 public Dialog onCreateDialog(Bundle savedInstanceState) { 245 int id = getArguments().getInt(ARG_ID); 246 int errorCode = getArguments().getInt("moveError"); 247 Dialog dialog = ((AppInfoBase) getTargetFragment()).createDialog(id, errorCode); 248 if (dialog == null) { 249 throw new IllegalArgumentException("unknown id " + id); 250 } 251 return dialog; 252 } 253 254 public static MyAlertDialogFragment newInstance(int id, int errorCode) { 255 MyAlertDialogFragment dialogFragment = new MyAlertDialogFragment(); 256 Bundle args = new Bundle(); 257 args.putInt(ARG_ID, id); 258 args.putInt("moveError", errorCode); 259 dialogFragment.setArguments(args); 260 return dialogFragment; 261 } 262 } 263 264 protected void startListeningToPackageRemove() { 265 if (mListeningToPackageRemove) { 266 return; 267 } 268 mListeningToPackageRemove = true; 269 final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED); 270 filter.addDataScheme("package"); 271 getContext().registerReceiver(mPackageRemovedReceiver, filter); 272 } 273 274 protected void stopListeningToPackageRemove() { 275 if (!mListeningToPackageRemove) { 276 return; 277 } 278 mListeningToPackageRemove = false; 279 getContext().unregisterReceiver(mPackageRemovedReceiver); 280 } 281 282 protected void onPackageRemoved() { 283 getActivity().finishAndRemoveTask(); 284 } 285 286 protected final BroadcastReceiver mPackageRemovedReceiver = new BroadcastReceiver() { 287 @Override 288 public void onReceive(Context context, Intent intent) { 289 String packageName = intent.getData().getSchemeSpecificPart(); 290 if (!mFinishing && mAppEntry.info.packageName.equals(packageName)) { 291 onPackageRemoved(); 292 } 293 } 294 }; 295 296} 297