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