Home | History | Annotate | Download | only in users
      1 /*
      2  * Copyright (C) 2014 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.tv.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.tv.settings.R;
     28 
     29 import java.util.ArrayList;
     30 
     31 public class RestrictionUtils {
     32 
     33     public static final String [] sRestrictionKeys = {
     34         UserManager.DISALLOW_SHARE_LOCATION,
     35     };
     36 
     37     public static final int [] sRestrictionTitles = {
     38         R.string.restriction_location_enable_title,
     39     };
     40 
     41     public static final int [] sRestrictionDescriptions = {
     42         R.string.restriction_location_enable_summary,
     43     };
     44 
     45     /**
     46      * Returns the current user restrictions in the form of application
     47      * restriction entries.
     48      * @return list of RestrictionEntry objects with user-visible text.
     49      */
     50     public static ArrayList<RestrictionEntry> getRestrictions(Context context, UserHandle user) {
     51         Resources res = context.getResources();
     52         ArrayList<RestrictionEntry> entries = new ArrayList<RestrictionEntry>();
     53         UserManager um = UserManager.get(context);
     54         Bundle userRestrictions = um.getUserRestrictions(user);
     55 
     56         for (int i = 0; i < sRestrictionKeys.length; i++) {
     57             RestrictionEntry entry = new RestrictionEntry(
     58                     sRestrictionKeys[i],
     59                     !userRestrictions.getBoolean(sRestrictionKeys[i], false));
     60             entry.setTitle(res.getString(sRestrictionTitles[i]));
     61             entry.setDescription(res.getString(sRestrictionDescriptions[i]));
     62             entry.setType(RestrictionEntry.TYPE_BOOLEAN);
     63             entries.add(entry);
     64         }
     65 
     66         return entries;
     67     }
     68 
     69     public static void setRestrictions(Context context, ArrayList<RestrictionEntry> entries,
     70             UserHandle user) {
     71         UserManager um = UserManager.get(context);
     72         Bundle userRestrictions = um.getUserRestrictions(user);
     73 
     74         for (RestrictionEntry entry : entries) {
     75             userRestrictions.putBoolean(entry.getKey(), !entry.getSelectedState());
     76             if (entry.getKey().equals(UserManager.DISALLOW_SHARE_LOCATION)
     77                     && !entry.getSelectedState()) {
     78                 Secure.putIntForUser(context.getContentResolver(),
     79                         Secure.LOCATION_MODE, Secure.LOCATION_MODE_OFF, user.getIdentifier());
     80             }
     81         }
     82         um.setUserRestrictions(userRestrictions, user);
     83     }
     84 
     85     public static Bundle restrictionsToBundle(ArrayList<RestrictionEntry> entries) {
     86         final Bundle bundle = new Bundle();
     87         for (RestrictionEntry entry : entries) {
     88             if (entry.getType() == RestrictionEntry.TYPE_BOOLEAN) {
     89                 bundle.putBoolean(entry.getKey(), entry.getSelectedState());
     90             } else if (entry.getType() == RestrictionEntry.TYPE_MULTI_SELECT) {
     91                 bundle.putStringArray(entry.getKey(), entry.getAllSelectedStrings());
     92             } else {
     93                 bundle.putString(entry.getKey(), entry.getSelectedString());
     94             }
     95         }
     96         return bundle;
     97     }
     98 }
     99