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