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