summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/restrictions/RestrictionProvider.java
blob: 26b9a446f2f98f0324d6119a0443a6fa03255d37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.restrictions;

import org.mozilla.gecko.AppConstants;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.RestrictionEntry;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.Map;

/**
 * Broadcast receiver providing supported restrictions to the system.
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class RestrictionProvider extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, final Intent intent) {
        if (AppConstants.Versions.preJBMR2) {
            // This broadcast does not make any sense prior to Jelly Bean MR2.
            return;
        }

        final PendingResult result = goAsync();

        new Thread() {
            @Override
            public void run() {
                final Bundle oldRestrictions = intent.getBundleExtra(Intent.EXTRA_RESTRICTIONS_BUNDLE);
                RestrictionCache.migrateRestrictionsIfNeeded(oldRestrictions);

                final Bundle extras = new Bundle();

                ArrayList<RestrictionEntry> entries = initRestrictions(context, oldRestrictions);
                extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS_LIST, entries);

                result.setResult(Activity.RESULT_OK, null, extras);
                result.finish();
            }
        }.start();
    }

    private ArrayList<RestrictionEntry> initRestrictions(Context context, Bundle oldRestrictions) {
        ArrayList<RestrictionEntry> entries = new ArrayList<RestrictionEntry>();

        final Map<Restrictable, Boolean> configuration = RestrictedProfileConfiguration.getConfiguration();

        for (Restrictable restrictable : configuration.keySet()) {
            if (RestrictedProfileConfiguration.shouldHide(restrictable)) {
                continue;
            }

            RestrictionEntry entry = createRestrictionEntryWithDefaultValue(context, restrictable,
                    oldRestrictions.getBoolean(restrictable.name, configuration.get(restrictable)));
            entries.add(entry);
        }

        return entries;
    }

    private RestrictionEntry createRestrictionEntryWithDefaultValue(Context context, Restrictable restrictable, boolean defaultValue) {
        RestrictionEntry entry = new RestrictionEntry(restrictable.name, defaultValue);

        entry.setTitle(restrictable.getTitle(context));

        final String description = restrictable.getDescription(context);
        if (!TextUtils.isEmpty(description)) {
            entry.setDescription(description);
        }

        return entry;
    }
}