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.example.android.applimits; 18 19 import android.app.Activity; 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.RestrictionEntry; 24 import android.content.BroadcastReceiver.PendingResult; 25 import android.content.res.Resources; 26 import android.os.Bundle; 27 import android.util.Log; 28 29 import java.util.ArrayList; 30 import java.util.List; 31 32 public class GetRestrictionsReceiver extends BroadcastReceiver { 33 private static final String TAG = "AppLimits$GetRestrictionsReceiver"; 34 35 static final String KEY_CUSTOM = "custom_or_not"; 36 static final String KEY_CHOICE = "choice"; 37 static final String KEY_MULTI_SELECT = "multi"; 38 39 @Override onReceive(final Context context, Intent intent)40 public void onReceive(final Context context, Intent intent) { 41 final PendingResult result = goAsync(); 42 final Bundle oldRestrictions = 43 intent.getBundleExtra(Intent.EXTRA_RESTRICTIONS_BUNDLE); 44 Log.i(TAG, "oldRestrictions = " + oldRestrictions); 45 new Thread() { 46 public void run() { 47 createRestrictions(context, result, oldRestrictions); 48 } 49 }.start(); 50 } 51 populateCustomEntry(Resources res, RestrictionEntry entry)52 public static void populateCustomEntry(Resources res, RestrictionEntry entry) { 53 entry.setType(RestrictionEntry.TYPE_BOOLEAN); 54 entry.setTitle(res.getString(R.string.custom_or_not_title)); 55 } 56 populateChoiceEntry(Resources res, RestrictionEntry reSingleChoice)57 public static void populateChoiceEntry(Resources res, RestrictionEntry reSingleChoice) { 58 String[] choiceEntries = res.getStringArray(R.array.choice_entry_entries); 59 String[] choiceValues = res.getStringArray(R.array.choice_entry_values); 60 if (reSingleChoice.getSelectedString() == null) { 61 reSingleChoice.setSelectedString(choiceValues[0]); 62 } 63 reSingleChoice.setTitle(res.getString(R.string.choice_entry_title)); 64 reSingleChoice.setChoiceEntries(choiceEntries); 65 reSingleChoice.setChoiceValues(choiceValues); 66 reSingleChoice.setType(RestrictionEntry.TYPE_CHOICE); 67 } 68 populateMultiEntry(Resources res, RestrictionEntry reMultiSelect)69 public static void populateMultiEntry(Resources res, RestrictionEntry reMultiSelect) { 70 String[] multiEntries = res.getStringArray(R.array.multi_entry_entries); 71 String[] multiValues = res.getStringArray(R.array.multi_entry_values); 72 if (reMultiSelect.getAllSelectedStrings() == null) { 73 reMultiSelect.setAllSelectedStrings(new String[0]); 74 } 75 reMultiSelect.setTitle(res.getString(R.string.multi_entry_title)); 76 reMultiSelect.setChoiceEntries(multiEntries); 77 reMultiSelect.setChoiceValues(multiValues); 78 reMultiSelect.setType(RestrictionEntry.TYPE_MULTI_SELECT); 79 } 80 initRestrictions(Context context)81 private ArrayList<RestrictionEntry> initRestrictions(Context context) { 82 ArrayList<RestrictionEntry> newRestrictions = new ArrayList<RestrictionEntry>(); 83 Resources res = context.getResources(); 84 85 RestrictionEntry reCustomOrNot = new RestrictionEntry(KEY_CUSTOM, false); 86 populateCustomEntry(res, reCustomOrNot); 87 newRestrictions.add(reCustomOrNot); 88 89 RestrictionEntry reSingleChoice = new RestrictionEntry(KEY_CHOICE, (String) null); 90 populateChoiceEntry(res, reSingleChoice); 91 newRestrictions.add(reSingleChoice); 92 93 RestrictionEntry reMultiSelect = new RestrictionEntry(KEY_MULTI_SELECT, (String[]) null); 94 populateMultiEntry(res, reMultiSelect); 95 newRestrictions.add(reMultiSelect); 96 97 return newRestrictions; 98 } 99 createRestrictions(Context context, PendingResult result, Bundle old)100 private void createRestrictions(Context context, PendingResult result, Bundle old) { 101 Resources res = context.getResources(); 102 103 ArrayList<RestrictionEntry> newEntries = initRestrictions(context); 104 // If this is the first time, create the default restrictions entries and return them. 105 if (old == null) { 106 Bundle extras = new Bundle(); 107 extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS_LIST, newEntries); 108 result.setResult(Activity.RESULT_OK, null, extras); 109 result.finish(); 110 return; 111 } 112 113 boolean custom = old.getBoolean(KEY_CUSTOM, false); 114 for (RestrictionEntry entry : newEntries) { 115 final String key = entry.getKey(); 116 if (KEY_CUSTOM.equals(key)) { 117 entry.setSelectedState(custom); 118 } else if (KEY_CHOICE.equals(key)) { 119 if (old.containsKey(KEY_CHOICE)) { 120 entry.setSelectedString(old.getString(KEY_CHOICE)); 121 } 122 } else if (KEY_MULTI_SELECT.equals(key)) { 123 if (old.containsKey(KEY_MULTI_SELECT)) { 124 entry.setAllSelectedStrings(old.getStringArray(key)); 125 } 126 } 127 } 128 129 Bundle extras = new Bundle(); 130 if (custom) { 131 Intent customIntent = new Intent(); 132 customIntent.setClass(context, CustomRestrictionsActivity.class); 133 extras.putParcelable(Intent.EXTRA_RESTRICTIONS_INTENT, customIntent); 134 } 135 extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS_LIST, newEntries); 136 result.setResult(Activity.RESULT_OK, null, extras); 137 result.finish(); 138 } 139 } 140