Home | History | Annotate | Download | only in users
      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 
     17 package com.android.settings.users;
     18 
     19 import android.content.Context;
     20 import android.content.RestrictionEntry;
     21 import android.content.res.Resources;
     22 import android.os.Bundle;
     23 import android.os.UserHandle;
     24 import android.os.UserManager;
     25 import android.provider.Settings.Secure;
     26 
     27 import com.android.settings.R;
     28 
     29 import java.util.ArrayList;
     30 
     31 public class RestrictionUtils {
     32 
     33     public static final String [] sRestrictionKeys = {
     34 //        UserManager.DISALLOW_CONFIG_WIFI,
     35 //        UserManager.DISALLOW_CONFIG_BLUETOOTH,
     36         UserManager.DISALLOW_SHARE_LOCATION,
     37 //        UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES
     38     };
     39 
     40     public static final int [] sRestrictionTitles = {
     41 //        R.string.restriction_wifi_config_title,
     42 //        R.string.restriction_bluetooth_config_title,
     43         R.string.restriction_location_enable_title,
     44 //        R.string.install_applications
     45     };
     46 
     47     public static final int [] sRestrictionDescriptions = {
     48 //        R.string.restriction_wifi_config_summary,
     49 //        R.string.restriction_bluetooth_config_summary,
     50         R.string.restriction_location_enable_summary,
     51 //        R.string.install_unknown_applications
     52     };
     53 
     54     /**
     55      * Returns the current user restrictions in the form of application
     56      * restriction entries.
     57      * @return list of RestrictionEntry objects with user-visible text.
     58      */
     59     public static ArrayList<RestrictionEntry> getRestrictions(Context context, UserHandle user) {
     60         Resources res = context.getResources();
     61         ArrayList<RestrictionEntry> entries = new ArrayList<RestrictionEntry>();
     62         UserManager um = UserManager.get(context);
     63         Bundle userRestrictions = um.getUserRestrictions(user);
     64 
     65         for (int i = 0; i < sRestrictionKeys.length; i++) {
     66             RestrictionEntry entry = new RestrictionEntry(
     67                     sRestrictionKeys[i],
     68                     !userRestrictions.getBoolean(sRestrictionKeys[i], false));
     69             entry.setTitle(res.getString(sRestrictionTitles[i]));
     70             entry.setDescription(res.getString(sRestrictionDescriptions[i]));
     71             entry.setType(RestrictionEntry.TYPE_BOOLEAN);
     72             entries.add(entry);
     73         }
     74 
     75         return entries;
     76     }
     77 
     78     public static void setRestrictions(Context context, ArrayList<RestrictionEntry> entries,
     79             UserHandle user) {
     80         UserManager um = UserManager.get(context);
     81         Bundle userRestrictions = um.getUserRestrictions(user);
     82 
     83         for (RestrictionEntry entry : entries) {
     84             userRestrictions.putBoolean(entry.getKey(), !entry.getSelectedState());
     85             if (entry.getKey().equals(UserManager.DISALLOW_SHARE_LOCATION)
     86                     && !entry.getSelectedState()) {
     87                 Secure.putStringForUser(context.getContentResolver(),
     88                         Secure.LOCATION_PROVIDERS_ALLOWED, "", user.getIdentifier());
     89             }
     90         }
     91         um.setUserRestrictions(userRestrictions, user);
     92     }
     93 
     94     public static Bundle restrictionsToBundle(ArrayList<RestrictionEntry> entries) {
     95         final Bundle bundle = new Bundle();
     96         for (RestrictionEntry entry : entries) {
     97             if (entry.getType() == RestrictionEntry.TYPE_BOOLEAN) {
     98                 bundle.putBoolean(entry.getKey(), entry.getSelectedState());
     99             } else if (entry.getType() == RestrictionEntry.TYPE_MULTI_SELECT) {
    100                 bundle.putStringArray(entry.getKey(), entry.getAllSelectedStrings());
    101             } else {
    102                 bundle.putString(entry.getKey(), entry.getSelectedString());
    103             }
    104         }
    105         return bundle;
    106     }
    107 }
    108