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