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