1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 package org.mozilla.gecko.restrictions;
7 
8 import org.mozilla.gecko.AppConstants;
9 
10 import android.annotation.TargetApi;
11 import android.app.Activity;
12 import android.content.BroadcastReceiver;
13 import android.content.Context;
14 import android.content.Intent;
15 import android.content.RestrictionEntry;
16 import android.os.Build;
17 import android.os.Bundle;
18 import android.text.TextUtils;
19 
20 import java.util.ArrayList;
21 import java.util.Map;
22 
23 /**
24  * Broadcast receiver providing supported restrictions to the system.
25  */
26 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
27 public class RestrictionProvider extends BroadcastReceiver {
28     @Override
onReceive(final Context context, final Intent intent)29     public void onReceive(final Context context, final Intent intent) {
30         if (AppConstants.Versions.preJBMR2) {
31             // This broadcast does not make any sense prior to Jelly Bean MR2.
32             return;
33         }
34 
35         final PendingResult result = goAsync();
36 
37         new Thread() {
38             @Override
39             public void run() {
40                 final Bundle oldRestrictions = intent.getBundleExtra(Intent.EXTRA_RESTRICTIONS_BUNDLE);
41                 final Bundle extras = new Bundle();
42                 if (oldRestrictions != null) {
43                     RestrictionCache.migrateRestrictionsIfNeeded(oldRestrictions);
44                     ArrayList<RestrictionEntry> entries = initRestrictions(context, oldRestrictions);
45                     extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS_LIST, entries);
46                 }
47 
48                 result.setResult(Activity.RESULT_OK, null, extras);
49                 result.finish();
50             }
51         }.start();
52     }
53 
initRestrictions(Context context, Bundle oldRestrictions)54     private ArrayList<RestrictionEntry> initRestrictions(Context context, Bundle oldRestrictions) {
55         ArrayList<RestrictionEntry> entries = new ArrayList<RestrictionEntry>();
56 
57         final Map<Restrictable, Boolean> configuration = RestrictedProfileConfiguration.getConfiguration();
58 
59         for (Restrictable restrictable : configuration.keySet()) {
60             if (RestrictedProfileConfiguration.shouldHide(restrictable)) {
61                 continue;
62             }
63 
64             RestrictionEntry entry = createRestrictionEntryWithDefaultValue(context, restrictable,
65                     oldRestrictions.getBoolean(restrictable.name, configuration.get(restrictable)));
66             entries.add(entry);
67         }
68 
69         return entries;
70     }
71 
createRestrictionEntryWithDefaultValue(Context context, Restrictable restrictable, boolean defaultValue)72     private RestrictionEntry createRestrictionEntryWithDefaultValue(Context context, Restrictable restrictable, boolean defaultValue) {
73         RestrictionEntry entry = new RestrictionEntry(restrictable.name, defaultValue);
74 
75         entry.setTitle(restrictable.getTitle(context));
76 
77         final String description = restrictable.getDescription(context);
78         if (!TextUtils.isEmpty(description)) {
79             entry.setDescription(description);
80         }
81 
82         return entry;
83     }
84 }
85