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