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