AppRestrictionsFragment.java revision 9e9c77c130a96a3855acf2d62225e21024b05834
1/*
2 * Copyright (C) 2013 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.users;
18
19import android.app.Activity;
20import android.app.AppGlobals;
21import android.appwidget.AppWidgetManager;
22import android.content.BroadcastReceiver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
26import android.content.RestrictionEntry;
27import android.content.pm.ApplicationInfo;
28import android.content.pm.IPackageManager;
29import android.content.pm.PackageInfo;
30import android.content.pm.PackageManager;
31import android.content.pm.PackageManager.NameNotFoundException;
32import android.content.pm.ResolveInfo;
33import android.content.res.Resources;
34import android.graphics.Bitmap;
35import android.graphics.ColorFilter;
36import android.graphics.ColorMatrix;
37import android.graphics.ColorMatrixColorFilter;
38import android.graphics.drawable.Drawable;
39import android.os.AsyncTask;
40import android.os.Bundle;
41import android.os.RemoteException;
42import android.os.ServiceManager;
43import android.os.UserHandle;
44import android.os.UserManager;
45import android.preference.CheckBoxPreference;
46import android.preference.ListPreference;
47import android.preference.MultiSelectListPreference;
48import android.preference.Preference;
49import android.preference.Preference.OnPreferenceChangeListener;
50import android.preference.Preference.OnPreferenceClickListener;
51import android.preference.PreferenceGroup;
52import android.preference.SwitchPreference;
53import android.text.TextUtils;
54import android.util.Log;
55import android.view.View;
56import android.view.View.OnClickListener;
57import android.view.inputmethod.InputMethodInfo;
58import android.view.inputmethod.InputMethodManager;
59import android.view.ViewGroup;
60import android.widget.CompoundButton;
61import android.widget.CompoundButton.OnCheckedChangeListener;
62import android.widget.Switch;
63
64import com.android.settings.R;
65import com.android.settings.SettingsPreferenceFragment;
66
67import java.util.ArrayList;
68import java.util.Collections;
69import java.util.Comparator;
70import java.util.HashMap;
71import java.util.HashSet;
72import java.util.List;
73import java.util.Map;
74import java.util.Set;
75import java.util.StringTokenizer;
76
77public class AppRestrictionsFragment extends SettingsPreferenceFragment implements
78        OnPreferenceChangeListener, OnClickListener, OnPreferenceClickListener {
79
80    private static final String TAG = AppRestrictionsFragment.class.getSimpleName();
81
82    private static final boolean DEBUG = false;
83
84    private static final String PKG_PREFIX = "pkg_";
85
86    protected PackageManager mPackageManager;
87    protected UserManager mUserManager;
88    protected UserHandle mUser;
89
90    private PreferenceGroup mAppList;
91
92    private static final int MAX_APP_RESTRICTIONS = 100;
93
94    private static final String DELIMITER = ";";
95
96    /** Key for extra passed in from calling fragment for the userId of the user being edited */
97    public static final String EXTRA_USER_ID = "user_id";
98
99    /** Key for extra passed in from calling fragment to indicate if this is a newly created user */
100    public static final String EXTRA_NEW_USER = "new_user";
101
102    HashMap<String,Boolean> mSelectedPackages = new HashMap<String,Boolean>();
103    private boolean mFirstTime = true;
104    private boolean mNewUser;
105    private boolean mAppListChanged;
106    protected boolean mRestrictedProfile;
107
108    private static final int CUSTOM_REQUEST_CODE_START = 1000;
109    private int mCustomRequestCode = CUSTOM_REQUEST_CODE_START;
110
111    private HashMap<Integer, AppRestrictionsPreference> mCustomRequestMap =
112            new HashMap<Integer,AppRestrictionsPreference>();
113
114    private List<SelectableAppInfo> mVisibleApps;
115    private List<ApplicationInfo> mUserApps;
116    private AsyncTask mAppLoadingTask;
117
118    private BroadcastReceiver mUserBackgrounding = new BroadcastReceiver() {
119        @Override
120        public void onReceive(Context context, Intent intent) {
121            // Update the user's app selection right away without waiting for a pause
122            // onPause() might come in too late, causing apps to disappear after broadcasts
123            // have been scheduled during user startup.
124            if (mAppListChanged) {
125                if (DEBUG) Log.d(TAG, "User backgrounding, update app list");
126                applyUserAppsStates();
127                if (DEBUG) Log.d(TAG, "User backgrounding, done updating app list");
128            }
129        }
130    };
131
132    static class SelectableAppInfo {
133        String packageName;
134        CharSequence appName;
135        CharSequence activityName;
136        Drawable icon;
137        SelectableAppInfo masterEntry;
138
139        @Override
140        public String toString() {
141            return packageName + ": appName=" + appName + "; activityName=" + activityName
142                    + "; icon=" + icon + "; masterEntry=" + masterEntry;
143        }
144    }
145
146    static class AppRestrictionsPreference extends SwitchPreference {
147        private boolean hasSettings;
148        private OnClickListener listener;
149        private ArrayList<RestrictionEntry> restrictions;
150        boolean panelOpen;
151        private boolean immutable;
152        List<Preference> childPreferences = new ArrayList<Preference>();
153        private final ColorFilter grayscaleFilter;
154
155        AppRestrictionsPreference(Context context, OnClickListener listener) {
156            super(context);
157            setLayoutResource(R.layout.preference_app_restrictions);
158            this.listener = listener;
159
160            ColorMatrix colorMatrix = new ColorMatrix();
161            colorMatrix.setSaturation(0f);
162            float[] matrix = colorMatrix.getArray();
163            matrix[18] = 0.5f;
164            grayscaleFilter = new ColorMatrixColorFilter(colorMatrix);
165        }
166
167        private void setSettingsEnabled(boolean enable) {
168            hasSettings = enable;
169        }
170
171        @Override
172        public void setChecked(boolean checked) {
173            if (checked) {
174                getIcon().setColorFilter(null);
175            } else {
176                getIcon().setColorFilter(grayscaleFilter);
177            }
178            super.setChecked(checked);
179        }
180
181        void setRestrictions(ArrayList<RestrictionEntry> restrictions) {
182            this.restrictions = restrictions;
183        }
184
185        void setImmutable(boolean immutable) {
186            this.immutable = immutable;
187        }
188
189        boolean isImmutable() {
190            return immutable;
191        }
192
193        RestrictionEntry getRestriction(String key) {
194            if (restrictions == null) return null;
195            for (RestrictionEntry entry : restrictions) {
196                if (entry.getKey().equals(key)) {
197                    return entry;
198                }
199            }
200            return null;
201        }
202
203        ArrayList<RestrictionEntry> getRestrictions() {
204            return restrictions;
205        }
206
207        @Override
208        protected void onBindView(View view) {
209            super.onBindView(view);
210
211            View appRestrictionsSettings = view.findViewById(R.id.app_restrictions_settings);
212            appRestrictionsSettings.setVisibility(hasSettings ? View.VISIBLE : View.GONE);
213            view.findViewById(R.id.settings_divider).setVisibility(
214                    hasSettings ? View.VISIBLE : View.GONE);
215            appRestrictionsSettings.setOnClickListener(listener);
216            appRestrictionsSettings.setTag(this);
217
218            View appRestrictionsPref = view.findViewById(R.id.app_restrictions_pref);
219            appRestrictionsPref.setOnClickListener(listener);
220            appRestrictionsPref.setTag(this);
221
222            ViewGroup widget = (ViewGroup) view.findViewById(android.R.id.widget_frame);
223            widget.setEnabled(!isImmutable());
224            if (widget.getChildCount() > 0) {
225                final Switch switchView = (Switch) widget.getChildAt(0);
226                switchView.setEnabled(!isImmutable());
227                switchView.setTag(this);
228                switchView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
229                    @Override
230                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
231                        listener.onClick(switchView);
232                    }
233                });
234            }
235        }
236    }
237
238    protected void init(Bundle icicle) {
239        if (icicle != null) {
240            mUser = new UserHandle(icicle.getInt(EXTRA_USER_ID));
241        } else {
242            Bundle args = getArguments();
243            if (args != null) {
244                if (args.containsKey(EXTRA_USER_ID)) {
245                    mUser = new UserHandle(args.getInt(EXTRA_USER_ID));
246                }
247                mNewUser = args.getBoolean(EXTRA_NEW_USER, false);
248            }
249        }
250
251        if (mUser == null) {
252            mUser = android.os.Process.myUserHandle();
253        }
254
255        mPackageManager = getActivity().getPackageManager();
256        mUserManager = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
257        mRestrictedProfile = mUserManager.getUserInfo(mUser.getIdentifier()).isRestricted();
258
259        addPreferencesFromResource(R.xml.app_restrictions);
260        mAppList = getAppPreferenceGroup();
261    }
262
263    @Override
264    public void onSaveInstanceState(Bundle outState) {
265        super.onSaveInstanceState(outState);
266        outState.putInt(EXTRA_USER_ID, mUser.getIdentifier());
267    }
268
269    public void onResume() {
270        super.onResume();
271
272        getActivity().registerReceiver(mUserBackgrounding,
273                new IntentFilter(Intent.ACTION_USER_BACKGROUND));
274        mAppListChanged = false;
275        if (mAppLoadingTask == null || mAppLoadingTask.getStatus() == AsyncTask.Status.FINISHED) {
276            mAppLoadingTask = new AppLoadingTask().execute((Void[]) null);
277        }
278    }
279
280    public void onPause() {
281        super.onPause();
282        mNewUser = false;
283        getActivity().unregisterReceiver(mUserBackgrounding);
284        if (mAppListChanged) {
285            new Thread() {
286                public void run() {
287                    applyUserAppsStates();
288                }
289            }.start();
290        }
291    }
292
293    protected PreferenceGroup getAppPreferenceGroup() {
294        return getPreferenceScreen();
295    }
296
297    protected Drawable getCircularUserIcon() {
298        Bitmap userIcon = mUserManager.getUserIcon(mUser.getIdentifier());
299        CircleFramedDrawable circularIcon =
300                CircleFramedDrawable.getInstance(this.getActivity(), userIcon);
301        return circularIcon;
302    }
303
304    protected void clearSelectedApps() {
305        mSelectedPackages.clear();
306    }
307
308    private void applyUserAppsStates() {
309        IPackageManager ipm = IPackageManager.Stub.asInterface(
310                ServiceManager.getService("package"));
311        final int userId = mUser.getIdentifier();
312        if (!mUserManager.getUserInfo(userId).isRestricted() && userId != UserHandle.myUserId()) {
313            Log.e(TAG, "Cannot apply application restrictions on another user!");
314            return;
315        }
316        for (Map.Entry<String,Boolean> entry : mSelectedPackages.entrySet()) {
317            String packageName = entry.getKey();
318            if (entry.getValue()) {
319                // Enable selected apps
320                try {
321                    ApplicationInfo info = ipm.getApplicationInfo(packageName,
322                            PackageManager.GET_UNINSTALLED_PACKAGES, userId);
323                    if (info == null || info.enabled == false) {
324                        ipm.installExistingPackageAsUser(packageName, mUser.getIdentifier());
325                        if (DEBUG) {
326                            Log.d(TAG, "Installing " + packageName);
327                        }
328                    }
329                    if (info != null && (info.flags&ApplicationInfo.FLAG_BLOCKED) != 0
330                            && (info.flags&ApplicationInfo.FLAG_INSTALLED) != 0) {
331                        ipm.setApplicationBlockedSettingAsUser(packageName, false, userId);
332                        if (DEBUG) {
333                            Log.d(TAG, "Unblocking " + packageName);
334                        }
335                    }
336                } catch (RemoteException re) {
337                }
338            } else {
339                // Blacklist all other apps, system or downloaded
340                try {
341                    ApplicationInfo info = ipm.getApplicationInfo(packageName, 0, userId);
342                    if (info != null) {
343                        if (mRestrictedProfile) {
344                            ipm.deletePackageAsUser(entry.getKey(), null, mUser.getIdentifier(),
345                                    PackageManager.DELETE_SYSTEM_APP);
346                            if (DEBUG) {
347                                Log.d(TAG, "Uninstalling " + packageName);
348                            }
349                        } else {
350                            ipm.setApplicationBlockedSettingAsUser(packageName, true, userId);
351                            if (DEBUG) {
352                                Log.d(TAG, "Blocking " + packageName);
353                            }
354                        }
355                    }
356                } catch (RemoteException re) {
357                }
358            }
359        }
360    }
361
362    private boolean isSystemPackage(String packageName) {
363        try {
364            final PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0);
365            if (pi.applicationInfo == null) return false;
366            final int flags = pi.applicationInfo.flags;
367            if ((flags & ApplicationInfo.FLAG_SYSTEM) != 0
368                    || (flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
369                return true;
370            }
371        } catch (NameNotFoundException nnfe) {
372            // Missing package?
373        }
374        return false;
375    }
376
377    /**
378     * Find all pre-installed input methods that are marked as default
379     * and add them to an exclusion list so that they aren't
380     * presented to the user for toggling.
381     * Don't add non-default ones, as they may include other stuff that we
382     * don't need to auto-include.
383     * @param excludePackages the set of package names to append to
384     */
385    private void addSystemImes(Set<String> excludePackages) {
386        final Context context = getActivity();
387        if (context == null) return;
388        InputMethodManager imm = (InputMethodManager)
389                context.getSystemService(Context.INPUT_METHOD_SERVICE);
390        List<InputMethodInfo> imis = imm.getInputMethodList();
391        for (InputMethodInfo imi : imis) {
392            try {
393                if (imi.isDefault(context) && isSystemPackage(imi.getPackageName())) {
394                    excludePackages.add(imi.getPackageName());
395                }
396            } catch (Resources.NotFoundException rnfe) {
397                // Not default
398            }
399        }
400    }
401
402    /**
403     * Add system apps that match an intent to the list, excluding any packages in the exclude list.
404     * @param visibleApps list of apps to append the new list to
405     * @param intent the intent to match
406     * @param excludePackages the set of package names to be excluded, since they're required
407     */
408    private void addSystemApps(List<SelectableAppInfo> visibleApps, Intent intent,
409            Set<String> excludePackages) {
410        if (getActivity() == null) return;
411        final PackageManager pm = mPackageManager;
412        List<ResolveInfo> launchableApps = pm.queryIntentActivities(intent,
413                PackageManager.GET_DISABLED_COMPONENTS | PackageManager.GET_UNINSTALLED_PACKAGES);
414        for (ResolveInfo app : launchableApps) {
415            if (app.activityInfo != null && app.activityInfo.applicationInfo != null) {
416                int flags = app.activityInfo.applicationInfo.flags;
417                if ((flags & ApplicationInfo.FLAG_SYSTEM) != 0
418                        || (flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
419                    // System app
420                    // Skip excluded packages
421                    if (excludePackages.contains(app.activityInfo.packageName)) continue;
422
423                    SelectableAppInfo info = new SelectableAppInfo();
424                    info.packageName = app.activityInfo.packageName;
425                    info.appName = app.activityInfo.applicationInfo.loadLabel(pm);
426                    info.icon = app.activityInfo.loadIcon(pm);
427                    info.activityName = app.activityInfo.loadLabel(pm);
428                    if (info.activityName == null) info.activityName = info.appName;
429
430                    visibleApps.add(info);
431                }
432            }
433        }
434    }
435
436    private class AppLoadingTask extends AsyncTask<Void, Void, Void> {
437
438        @Override
439        protected Void doInBackground(Void... params) {
440            fetchAndMergeApps();
441            return null;
442        }
443
444        @Override
445        protected void onPostExecute(Void result) {
446            populateApps();
447        }
448
449        @Override
450        protected void onPreExecute() {
451        }
452    }
453
454    private void fetchAndMergeApps() {
455        mAppList.setOrderingAsAdded(false);
456        mVisibleApps = new ArrayList<SelectableAppInfo>();
457        final Context context = getActivity();
458        if (context == null) return;
459        final PackageManager pm = mPackageManager;
460        IPackageManager ipm = AppGlobals.getPackageManager();
461
462        final HashSet<String> excludePackages = new HashSet<String>();
463        addSystemImes(excludePackages);
464
465        // Add launchers
466        Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
467        launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
468        addSystemApps(mVisibleApps, launcherIntent, excludePackages);
469
470        // Add widgets
471        Intent widgetIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
472        addSystemApps(mVisibleApps, widgetIntent, excludePackages);
473
474        List<ApplicationInfo> installedApps = pm.getInstalledApplications(
475                PackageManager.GET_UNINSTALLED_PACKAGES);
476        for (ApplicationInfo app : installedApps) {
477            // If it's not installed, skip
478            if ((app.flags & ApplicationInfo.FLAG_INSTALLED) == 0) continue;
479
480            if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 0
481                    && (app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 0) {
482                // Downloaded app
483                SelectableAppInfo info = new SelectableAppInfo();
484                info.packageName = app.packageName;
485                info.appName = app.loadLabel(pm);
486                info.activityName = info.appName;
487                info.icon = app.loadIcon(pm);
488                mVisibleApps.add(info);
489            } else {
490                try {
491                    PackageInfo pi = pm.getPackageInfo(app.packageName, 0);
492                    // If it's a system app that requires an account and doesn't see restricted
493                    // accounts, mark for removal. It might get shown in the UI if it has an icon
494                    // but will still be marked as false and immutable.
495                    if (mRestrictedProfile
496                            && pi.requiredAccountType != null && pi.restrictedAccountType == null) {
497                        mSelectedPackages.put(app.packageName, false);
498                    }
499                } catch (NameNotFoundException re) {
500                }
501            }
502        }
503
504        mUserApps = null;
505        try {
506            mUserApps = ipm.getInstalledApplications(
507                    PackageManager.GET_UNINSTALLED_PACKAGES, mUser.getIdentifier()).getList();
508        } catch (RemoteException re) {
509        }
510
511        if (mUserApps != null) {
512            for (ApplicationInfo app : mUserApps) {
513                if ((app.flags & ApplicationInfo.FLAG_INSTALLED) == 0) continue;
514
515                if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 0
516                        && (app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 0) {
517                    // Downloaded app
518                    SelectableAppInfo info = new SelectableAppInfo();
519                    info.packageName = app.packageName;
520                    info.appName = app.loadLabel(pm);
521                    info.activityName = info.appName;
522                    info.icon = app.loadIcon(pm);
523                    mVisibleApps.add(info);
524                }
525            }
526        }
527        Collections.sort(mVisibleApps, new AppLabelComparator());
528
529        // Remove dupes
530        Set<String> dedupPackageSet = new HashSet<String>();
531        for (int i = mVisibleApps.size() - 1; i >= 0; i--) {
532            SelectableAppInfo info = mVisibleApps.get(i);
533            if (DEBUG) Log.i(TAG, info.toString());
534            String both = info.packageName + "+" + info.activityName;
535            if (!TextUtils.isEmpty(info.packageName)
536                    && !TextUtils.isEmpty(info.activityName)
537                    && dedupPackageSet.contains(both)) {
538                mVisibleApps.remove(i);
539            } else {
540                dedupPackageSet.add(both);
541            }
542        }
543
544        // Establish master/slave relationship for entries that share a package name
545        HashMap<String,SelectableAppInfo> packageMap = new HashMap<String,SelectableAppInfo>();
546        for (SelectableAppInfo info : mVisibleApps) {
547            if (packageMap.containsKey(info.packageName)) {
548                info.masterEntry = packageMap.get(info.packageName);
549            } else {
550                packageMap.put(info.packageName, info);
551            }
552        }
553    }
554
555    private boolean isAppEnabledForUser(PackageInfo pi) {
556        if (pi == null) return false;
557        final int flags = pi.applicationInfo.flags;
558        // Return true if it is installed and not blocked
559        return ((flags&ApplicationInfo.FLAG_INSTALLED) != 0
560                && (flags&ApplicationInfo.FLAG_BLOCKED) == 0);
561    }
562
563    private void populateApps() {
564        final Context context = getActivity();
565        if (context == null) return;
566        final PackageManager pm = mPackageManager;
567        IPackageManager ipm = AppGlobals.getPackageManager();
568        mAppList.removeAll();
569        Intent restrictionsIntent = new Intent(Intent.ACTION_GET_RESTRICTION_ENTRIES);
570        final List<ResolveInfo> receivers = pm.queryBroadcastReceivers(restrictionsIntent, 0);
571        int i = 0;
572        if (mVisibleApps.size() > 0) {
573            for (SelectableAppInfo app : mVisibleApps) {
574                String packageName = app.packageName;
575                if (packageName == null) continue;
576                final boolean isSettingsApp = packageName.equals(context.getPackageName());
577                AppRestrictionsPreference p = new AppRestrictionsPreference(context, this);
578                final boolean hasSettings = resolveInfoListHasPackage(receivers, packageName);
579                p.setIcon(app.icon != null ? app.icon.mutate() : null);
580                p.setChecked(false);
581                p.setTitle(app.activityName);
582                if (app.masterEntry != null) {
583                    p.setSummary(context.getString(R.string.user_restrictions_controlled_by,
584                            app.masterEntry.activityName));
585                }
586                p.setKey(PKG_PREFIX + packageName);
587                p.setSettingsEnabled(hasSettings || isSettingsApp);
588                p.setPersistent(false);
589                p.setOnPreferenceChangeListener(this);
590                p.setOnPreferenceClickListener(this);
591                PackageInfo pi = null;
592                try {
593                    pi = ipm.getPackageInfo(packageName,
594                            PackageManager.GET_UNINSTALLED_PACKAGES, mUser.getIdentifier());
595                } catch (RemoteException e) {
596                }
597                if (pi != null && pi.requiredForAllUsers) {
598                    p.setChecked(true);
599                    p.setImmutable(true);
600                    // If the app is required and has no restrictions, skip showing it
601                    if (!hasSettings && !isSettingsApp) continue;
602                    // Get and populate the defaults, since the user is not going to be
603                    // able to toggle this app ON (it's ON by default and immutable).
604                    // Only do this for restricted profiles, not single-user restrictions
605                    if (hasSettings && mRestrictedProfile) {
606                        requestRestrictionsForApp(packageName, p);
607                    }
608                } else if (!mNewUser && isAppEnabledForUser(pi)) { /*appInfoListHasPackage(mUserApps, packageName)*/
609                    p.setChecked(true);
610                }
611                if (mRestrictedProfile
612                        && pi.requiredAccountType != null && pi.restrictedAccountType == null) {
613                    p.setChecked(false);
614                    p.setImmutable(true);
615                    p.setSummary(R.string.app_not_supported_in_limited);
616                }
617                if (mRestrictedProfile && pi.restrictedAccountType != null) {
618                    p.setSummary(R.string.app_sees_restricted_accounts);
619                }
620                if (app.masterEntry != null) {
621                    p.setImmutable(true);
622                    p.setChecked(mSelectedPackages.get(packageName));
623                }
624                mAppList.addPreference(p);
625                if (isSettingsApp) {
626                    p.setOrder(MAX_APP_RESTRICTIONS * 1);
627                } else {
628                    p.setOrder(MAX_APP_RESTRICTIONS * (i + 2));
629                }
630                mSelectedPackages.put(packageName, p.isChecked());
631                mAppListChanged = true;
632                i++;
633            }
634        }
635        // If this is the first time for a new profile, install/uninstall default apps for profile
636        // to avoid taking the hit in onPause(), which can cause race conditions on user switch.
637        if (mNewUser && mFirstTime) {
638            mFirstTime = false;
639            applyUserAppsStates();
640        }
641    }
642
643    private class AppLabelComparator implements Comparator<SelectableAppInfo> {
644
645        @Override
646        public int compare(SelectableAppInfo lhs, SelectableAppInfo rhs) {
647            String lhsLabel = lhs.activityName.toString();
648            String rhsLabel = rhs.activityName.toString();
649            return lhsLabel.toLowerCase().compareTo(rhsLabel.toLowerCase());
650        }
651    }
652
653    private boolean resolveInfoListHasPackage(List<ResolveInfo> receivers, String packageName) {
654        for (ResolveInfo info : receivers) {
655            if (info.activityInfo.packageName.equals(packageName)) {
656                return true;
657            }
658        }
659        return false;
660    }
661
662    private void updateAllEntries(String prefKey, boolean checked) {
663        for (int i = 0; i < mAppList.getPreferenceCount(); i++) {
664            Preference pref = mAppList.getPreference(i);
665            if (pref instanceof AppRestrictionsPreference) {
666                if (prefKey.equals(pref.getKey())) {
667                    ((AppRestrictionsPreference) pref).setChecked(checked);
668                }
669            }
670        }
671    }
672
673    @Override
674    public void onClick(View v) {
675        if (v.getTag() instanceof AppRestrictionsPreference) {
676            AppRestrictionsPreference pref = (AppRestrictionsPreference) v.getTag();
677            if (v.getId() == R.id.app_restrictions_settings) {
678                toggleAppPanel(pref);
679            } else if (!pref.isImmutable()) {
680                pref.setChecked(!pref.isChecked());
681                final String packageName = pref.getKey().substring(PKG_PREFIX.length());
682                mSelectedPackages.put(packageName, pref.isChecked());
683                if (pref.isChecked() && pref.hasSettings
684                        && pref.restrictions == null) {
685                    // The restrictions have not been initialized, get and save them
686                    requestRestrictionsForApp(packageName, pref);
687                }
688                mAppListChanged = true;
689                updateAllEntries(pref.getKey(), pref.isChecked());
690            }
691        }
692    }
693
694    @Override
695    public boolean onPreferenceChange(Preference preference, Object newValue) {
696        String key = preference.getKey();
697        if (key != null && key.contains(DELIMITER)) {
698            StringTokenizer st = new StringTokenizer(key, DELIMITER);
699            final String packageName = st.nextToken();
700            final String restrictionKey = st.nextToken();
701            AppRestrictionsPreference appPref = (AppRestrictionsPreference)
702                    mAppList.findPreference(PKG_PREFIX+packageName);
703            ArrayList<RestrictionEntry> restrictions = appPref.getRestrictions();
704            if (restrictions != null) {
705                for (RestrictionEntry entry : restrictions) {
706                    if (entry.getKey().equals(restrictionKey)) {
707                        switch (entry.getType()) {
708                        case RestrictionEntry.TYPE_BOOLEAN:
709                            entry.setSelectedState((Boolean) newValue);
710                            break;
711                        case RestrictionEntry.TYPE_CHOICE:
712                        case RestrictionEntry.TYPE_CHOICE_LEVEL:
713                            ListPreference listPref = (ListPreference) preference;
714                            entry.setSelectedString((String) newValue);
715                            String readable = findInArray(entry.getChoiceEntries(),
716                                    entry.getChoiceValues(), (String) newValue);
717                            listPref.setSummary(readable);
718                            break;
719                        case RestrictionEntry.TYPE_MULTI_SELECT:
720                            Set<String> set = (Set<String>) newValue;
721                            String [] selectedValues = new String[set.size()];
722                            set.toArray(selectedValues);
723                            entry.setAllSelectedStrings(selectedValues);
724                            break;
725                        default:
726                            continue;
727                        }
728                        if (packageName.equals(getActivity().getPackageName())) {
729                            RestrictionUtils.setRestrictions(getActivity(), restrictions, mUser);
730                        } else {
731                            mUserManager.setApplicationRestrictions(packageName,
732                                    RestrictionUtils.restrictionsToBundle(restrictions),
733                                    mUser);
734                        }
735                        break;
736                    }
737                }
738            }
739        }
740        return true;
741    }
742
743    private void toggleAppPanel(AppRestrictionsPreference preference) {
744        if (preference.getKey().startsWith(PKG_PREFIX)) {
745            if (preference.panelOpen) {
746                for (Preference p : preference.childPreferences) {
747                    mAppList.removePreference(p);
748                }
749                preference.childPreferences.clear();
750            } else {
751                String packageName = preference.getKey().substring(PKG_PREFIX.length());
752                if (packageName.equals(getActivity().getPackageName())) {
753                    // Settings, fake it by using user restrictions
754                    ArrayList<RestrictionEntry> restrictions = RestrictionUtils.getRestrictions(
755                            getActivity(), mUser);
756                    onRestrictionsReceived(preference, packageName, restrictions);
757                } else {
758                    requestRestrictionsForApp(packageName, preference);
759                }
760            }
761            preference.panelOpen = !preference.panelOpen;
762        }
763    }
764
765    private void requestRestrictionsForApp(String packageName,
766            AppRestrictionsPreference preference) {
767        Bundle oldEntries =
768                mUserManager.getApplicationRestrictions(packageName, mUser);
769        Intent intent = new Intent(Intent.ACTION_GET_RESTRICTION_ENTRIES);
770        intent.setPackage(packageName);
771        intent.putExtra(Intent.EXTRA_RESTRICTIONS_BUNDLE, oldEntries);
772        intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
773        getActivity().sendOrderedBroadcast(intent, null,
774                new RestrictionsResultReceiver(packageName, preference),
775                null, Activity.RESULT_OK, null, null);
776    }
777
778    class RestrictionsResultReceiver extends BroadcastReceiver {
779
780        private static final String CUSTOM_RESTRICTIONS_INTENT = Intent.EXTRA_RESTRICTIONS_INTENT;
781        String packageName;
782        AppRestrictionsPreference preference;
783
784        RestrictionsResultReceiver(String packageName, AppRestrictionsPreference preference) {
785            super();
786            this.packageName = packageName;
787            this.preference = preference;
788        }
789
790        @Override
791        public void onReceive(Context context, Intent intent) {
792            Bundle results = getResultExtras(true);
793            final ArrayList<RestrictionEntry> restrictions = results.getParcelableArrayList(
794                    Intent.EXTRA_RESTRICTIONS_LIST);
795            Intent restrictionsIntent = (Intent) results.getParcelable(CUSTOM_RESTRICTIONS_INTENT);
796            if (restrictions != null && restrictionsIntent == null) {
797                onRestrictionsReceived(preference, packageName, restrictions);
798                mUserManager.setApplicationRestrictions(packageName,
799                        RestrictionUtils.restrictionsToBundle(restrictions), mUser);
800            } else if (restrictionsIntent != null) {
801                final Intent customIntent = restrictionsIntent;
802                if (restrictions != null) {
803                    customIntent.putExtra(Intent.EXTRA_RESTRICTIONS_BUNDLE,
804                            RestrictionUtils.restrictionsToBundle(restrictions));
805                }
806                Preference p = new Preference(context);
807                p.setTitle(R.string.app_restrictions_custom_label);
808                p.setOnPreferenceClickListener(new OnPreferenceClickListener() {
809                    @Override
810                    public boolean onPreferenceClick(Preference preference) {
811                        int requestCode = generateCustomActivityRequestCode(
812                                RestrictionsResultReceiver.this.preference);
813                        AppRestrictionsFragment.this.startActivityForResult(
814                                customIntent, requestCode);
815                        return false;
816                    }
817                });
818                p.setPersistent(false);
819                p.setOrder(preference.getOrder() + 1);
820                preference.childPreferences.add(p);
821                mAppList.addPreference(p);
822                preference.setRestrictions(restrictions);
823            }
824        }
825    }
826
827    private void onRestrictionsReceived(AppRestrictionsPreference preference, String packageName,
828            ArrayList<RestrictionEntry> restrictions) {
829        // Non-custom-activity case - expand the restrictions in-place
830        final Context context = preference.getContext();
831        int count = 1;
832        for (RestrictionEntry entry : restrictions) {
833            Preference p = null;
834            switch (entry.getType()) {
835            case RestrictionEntry.TYPE_BOOLEAN:
836                p = new CheckBoxPreference(context);
837                p.setTitle(entry.getTitle());
838                p.setSummary(entry.getDescription());
839                ((CheckBoxPreference)p).setChecked(entry.getSelectedState());
840                break;
841            case RestrictionEntry.TYPE_CHOICE:
842            case RestrictionEntry.TYPE_CHOICE_LEVEL:
843                p = new ListPreference(context);
844                p.setTitle(entry.getTitle());
845                String value = entry.getSelectedString();
846                if (value == null) {
847                    value = entry.getDescription();
848                }
849                p.setSummary(findInArray(entry.getChoiceEntries(), entry.getChoiceValues(),
850                        value));
851                ((ListPreference)p).setEntryValues(entry.getChoiceValues());
852                ((ListPreference)p).setEntries(entry.getChoiceEntries());
853                ((ListPreference)p).setValue(value);
854                ((ListPreference)p).setDialogTitle(entry.getTitle());
855                break;
856            case RestrictionEntry.TYPE_MULTI_SELECT:
857                p = new MultiSelectListPreference(context);
858                p.setTitle(entry.getTitle());
859                ((MultiSelectListPreference)p).setEntryValues(entry.getChoiceValues());
860                ((MultiSelectListPreference)p).setEntries(entry.getChoiceEntries());
861                HashSet<String> set = new HashSet<String>();
862                for (String s : entry.getAllSelectedStrings()) {
863                    set.add(s);
864                }
865                ((MultiSelectListPreference)p).setValues(set);
866                ((MultiSelectListPreference)p).setDialogTitle(entry.getTitle());
867                break;
868            case RestrictionEntry.TYPE_NULL:
869            default:
870            }
871            if (p != null) {
872                p.setPersistent(false);
873                p.setOrder(preference.getOrder() + count);
874                // Store the restrictions key string as a key for the preference
875                p.setKey(preference.getKey().substring(PKG_PREFIX.length()) + DELIMITER
876                        + entry.getKey());
877                mAppList.addPreference(p);
878                p.setOnPreferenceChangeListener(AppRestrictionsFragment.this);
879                preference.childPreferences.add(p);
880                count++;
881            }
882        }
883        preference.setRestrictions(restrictions);
884        if (count == 1 // No visible restrictions
885                && preference.isImmutable()
886                && preference.isChecked()) {
887            // Special case of required app with no visible restrictions. Remove it
888            mAppList.removePreference(preference);
889        }
890    }
891
892    /**
893     * Generates a request code that is stored in a map to retrieve the associated
894     * AppRestrictionsPreference.
895     * @param preference
896     * @return
897     */
898    private int generateCustomActivityRequestCode(AppRestrictionsPreference preference) {
899        mCustomRequestCode++;
900        mCustomRequestMap.put(mCustomRequestCode, preference);
901        return mCustomRequestCode;
902    }
903
904    @Override
905    public void onActivityResult(int requestCode, int resultCode, Intent data) {
906        super.onActivityResult(requestCode, resultCode, data);
907
908        AppRestrictionsPreference pref = mCustomRequestMap.get(requestCode);
909        if (pref == null) {
910            Log.w(TAG, "Unknown requestCode " + requestCode);
911            return;
912        }
913
914        if (resultCode == Activity.RESULT_OK) {
915            String packageName = pref.getKey().substring(PKG_PREFIX.length());
916            ArrayList<RestrictionEntry> list =
917                    data.getParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS_LIST);
918            Bundle bundle = data.getBundleExtra(Intent.EXTRA_RESTRICTIONS_BUNDLE);
919            if (list != null) {
920                // If there's a valid result, persist it to the user manager.
921                pref.setRestrictions(list);
922                mUserManager.setApplicationRestrictions(packageName,
923                        RestrictionUtils.restrictionsToBundle(list), mUser);
924            } else if (bundle != null) {
925                // If there's a valid result, persist it to the user manager.
926                mUserManager.setApplicationRestrictions(packageName, bundle, mUser);
927            }
928            toggleAppPanel(pref);
929        }
930        // Remove request from the map
931        mCustomRequestMap.remove(requestCode);
932    }
933
934    private String findInArray(String[] choiceEntries, String[] choiceValues,
935            String selectedString) {
936        for (int i = 0; i < choiceValues.length; i++) {
937            if (choiceValues[i].equals(selectedString)) {
938                return choiceEntries[i];
939            }
940        }
941        return selectedString;
942    }
943
944    @Override
945    public boolean onPreferenceClick(Preference preference) {
946        if (preference.getKey().startsWith(PKG_PREFIX)) {
947            AppRestrictionsPreference arp = (AppRestrictionsPreference) preference;
948            if (!arp.isImmutable()) {
949                arp.setChecked(!arp.isChecked());
950                mSelectedPackages.put(arp.getKey().substring(PKG_PREFIX.length()), arp.isChecked());
951                updateAllEntries(arp.getKey(), arp.isChecked());
952                mAppListChanged = true;
953            }
954            return true;
955        }
956        return false;
957    }
958
959}
960