Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2601cd91 authored by Amith Yamasani's avatar Amith Yamasani
Browse files

Implement proper settings restrictions

Settings restrictions for limited users translate to user
restrictions specified in UserManager. Added required strings.
TODO: Add NFC restriction.

Change-Id: If1f81319131855f5dc1b27fe5bd54a4fef616d7f
parent 732c80ff
Loading
Loading
Loading
Loading
+0 −6
Original line number Diff line number Diff line
@@ -1602,11 +1602,5 @@
            </intent-filter>
        </receiver>

        <receiver android:name=".users.RestrictionsReceiver">
            <intent-filter>
                <action android:name="android.intent.action.GET_RESTRICTION_ENTRIES"/>
            </intent-filter>
        </receiver>

    </application>
</manifest>
+1 −1
Original line number Diff line number Diff line
@@ -4440,7 +4440,7 @@
    <!-- Restrictions summary for allowing NFC transfers (phone) [CHAR LIMIT=100] -->
    <string name="restriction_nfc_enable_summary" product="default">Allow data exchange when the phone touches another device</string>
    <!-- Restrictions title for allowing location sharing [CHAR LIMIT=35] -->
    <string name="restriction_location_enable_title">NFC</string>
    <string name="restriction_location_enable_title">Location access</string>
    <!-- Restrictions summary for allowing location sharing [CHAR LIMIT=100] -->
    <string name="restriction_location_enable_summary" >Let apps use your location information</string>

+0 −10
Original line number Diff line number Diff line
@@ -57,7 +57,6 @@ import com.android.settings.accounts.AuthenticatorHelper;
import com.android.settings.accounts.ManageAccountsSettings;
import com.android.settings.bluetooth.BluetoothEnabler;
import com.android.settings.bluetooth.BluetoothSettings;
import com.android.settings.users.RestrictionsReceiver;
import com.android.settings.wfd.WifiDisplaySettings;
import com.android.settings.wifi.WifiEnabler;
import com.android.settings.wifi.WifiSettings;
@@ -457,15 +456,6 @@ public class Settings extends PreferenceActivity
                if (!showDev) {
                    target.remove(i);
                }
            } else if (id == R.id.application_settings) {
                if (mAppRestrictions != null) {
                    for (RestrictionEntry entry : mAppRestrictions) {
                        if (entry.getKey().equals(RestrictionsReceiver.KEY_ENABLE_APPS)
                                && !entry.getSelectedState()) {
                            target.remove(i);
                        }
                    }
                }
            } else if (id == R.id.account_add) {
                if (um.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
                    target.remove(i);
+83 −65
Original line number Diff line number Diff line
@@ -228,7 +228,7 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
        final List<ResolveInfo> existingApps = pm.queryIntentActivitiesAsUser(launcherIntent,
                0, mUser.getIdentifier());
        int i = 0;
        if (receivers != null && receivers.size() > 0) {
        if (mApps != null && mApps.size() > 0) {
            for (ResolveInfo app : mApps) {
                if (app.activityInfo == null || app.activityInfo.packageName == null) continue;
                String packageName = app.activityInfo.packageName;
@@ -238,7 +238,8 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
                p.setIcon(icon);
                p.setTitle(label);
                p.setKey(PKG_PREFIX + packageName);
                p.setSettingsEnabled(hasPackage(receivers, packageName));
                p.setSettingsEnabled(hasPackage(receivers, packageName)
                        || packageName.equals(getActivity().getPackageName()));
                p.setPersistent(false);
                p.setOnPreferenceChangeListener(this);
                p.setOnPreferenceClickListener(this);
@@ -346,8 +347,12 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
                        default:
                            continue;
                        }
                        if (packageName.equals(getActivity().getPackageName())) {
                            RestrictionUtils.setRestrictions(getActivity(), restrictions, mUser);
                        } else {
                            mUserManager.setApplicationRestrictions(packageName, restrictions,
                                    mUser);
                        }
                        break;
                    }
                }
@@ -371,6 +376,12 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
                preference.childPreferences.clear();
            } else {
                String packageName = preference.getKey().substring(PKG_PREFIX.length());
                if (packageName.equals(getActivity().getPackageName())) {
                    // Settings, fake it by using user restrictions
                    ArrayList<RestrictionEntry> restrictions = RestrictionUtils.getRestrictions(
                            getActivity(), mUser);
                    onRestrictionsReceived(preference, packageName, restrictions);
                } else {
                    List<RestrictionEntry> oldEntries =
                            mUserManager.getApplicationRestrictions(packageName, mUser);
                    Intent intent = new Intent(Intent.ACTION_GET_RESTRICTION_ENTRIES);
@@ -381,6 +392,7 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
                            new RestrictionsResultReceiver(packageName, preference),
                            null, Activity.RESULT_OK, null, null);
                }
            }
            preference.panelOpen = !preference.panelOpen;
        }
    }
@@ -404,7 +416,36 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
                    Intent.EXTRA_RESTRICTIONS);
            Intent restrictionsIntent = (Intent) results.getParcelable(CUSTOM_RESTRICTIONS_INTENT);
            if (restrictions != null && restrictionsIntent == null) {
                onRestrictionsReceived(preference, packageName, restrictions);
                mUserManager.setApplicationRestrictions(packageName, restrictions, mUser);
            } else if (restrictionsIntent != null) {
                final Intent customIntent = restrictionsIntent;
                customIntent.putParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS, restrictions);
                Preference p = new Preference(context);
                p.setTitle(R.string.app_restrictions_custom_label);
                p.setOnPreferenceClickListener(new OnPreferenceClickListener() {
                    @Override
                    public boolean onPreferenceClick(Preference preference) {
                        int requestCode = generateCustomActivityRequestCode(
                                RestrictionsResultReceiver.this.preference);
                        AppRestrictionsFragment.this.startActivityForResult(
                                customIntent, requestCode);
                        return false;
                    }
                });
                p.setPersistent(false);
                p.setOrder(preference.getOrder() + 1);
                preference.childPreferences.add(p);
                mAppList.addPreference(p);
                preference.setRestrictions(restrictions);
            }
        }
    }

    private void onRestrictionsReceived(AppRestrictionsPreference preference, String packageName,
            ArrayList<RestrictionEntry> restrictions) {
        // Non-custom-activity case - expand the restrictions in-place
        final Context context = preference.getContext();
        int count = 1;
        for (RestrictionEntry entry : restrictions) {
            Preference p = null;
@@ -456,29 +497,6 @@ public class AppRestrictionsFragment extends SettingsPreferenceFragment implemen
            }
        }
        preference.setRestrictions(restrictions);
                mUserManager.setApplicationRestrictions(packageName, restrictions, mUser);
            } else if (restrictionsIntent != null) {
                final Intent customIntent = restrictionsIntent;
                customIntent.putParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS, restrictions);
                Preference p = new Preference(context);
                p.setTitle(R.string.app_restrictions_custom_label);
                p.setOnPreferenceClickListener(new OnPreferenceClickListener() {
                    @Override
                    public boolean onPreferenceClick(Preference preference) {
                        int requestCode = generateCustomActivityRequestCode(
                                RestrictionsResultReceiver.this.preference);
                        AppRestrictionsFragment.this.startActivityForResult(
                                customIntent, requestCode);
                        return false;
                    }
                });
                p.setPersistent(false);
                p.setOrder(preference.getOrder() + 1);
                preference.childPreferences.add(p);
                mAppList.addPreference(p);
                preference.setRestrictions(restrictions);
            }
        }
    }

    /**
+93 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.users;

import android.content.Context;
import android.content.RestrictionEntry;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings.Secure;

import com.android.settings.R;

import java.util.ArrayList;

public class RestrictionUtils {

    public static final String [] sRestrictionKeys = {
        UserManager.DISALLOW_CONFIG_WIFI,
        UserManager.DISALLOW_CONFIG_BLUETOOTH,
        UserManager.DISALLOW_SHARE_LOCATION,
        UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES
    };

    public static final int [] sRestrictionTitles = {
        R.string.restriction_wifi_config_title,
        R.string.restriction_bluetooth_config_title,
        R.string.restriction_location_enable_title,
        R.string.install_applications
    };

    public static final int [] sRestrictionDescriptions = {
        R.string.restriction_wifi_config_summary,
        R.string.restriction_bluetooth_config_summary,
        R.string.restriction_location_enable_summary,
        R.string.install_unknown_applications
    };

    /**
     * Returns the current user restrictions in the form of application
     * restriction entries.
     * @return list of RestrictionEntry objects with user-visible text.
     */
    public static ArrayList<RestrictionEntry> getRestrictions(Context context, UserHandle user) {
        Resources res = context.getResources();
        ArrayList<RestrictionEntry> entries = new ArrayList<RestrictionEntry>();
        UserManager um = UserManager.get(context);
        Bundle userRestrictions = um.getUserRestrictions(user);

        for (int i = 0; i < sRestrictionKeys.length; i++) {
            RestrictionEntry entry = new RestrictionEntry(
                    sRestrictionKeys[i],
                    !userRestrictions.getBoolean(sRestrictionKeys[i], false));
            entry.setTitle(res.getString(sRestrictionTitles[i]));
            entry.setDescription(res.getString(sRestrictionDescriptions[i]));
            entry.setType(RestrictionEntry.TYPE_BOOLEAN);
            entries.add(entry);
        }

        return entries;
    }

    public static void setRestrictions(Context context, ArrayList<RestrictionEntry> entries,
            UserHandle user) {
        UserManager um = UserManager.get(context);
        Bundle userRestrictions = um.getUserRestrictions(user);

        for (RestrictionEntry entry : entries) {
            userRestrictions.putBoolean(entry.getKey(), !entry.getSelectedState());
            if (entry.getKey().equals(UserManager.DISALLOW_SHARE_LOCATION)
                    && !entry.getSelectedState()) {
                Secure.putStringForUser(context.getContentResolver(),
                        Secure.LOCATION_PROVIDERS_ALLOWED, "", user.getIdentifier());
            }
        }
        um.setUserRestrictions(userRestrictions, user);
    }
}
Loading