Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)
AppRestrictions / src / com.example.android.apprestrictions /

MainActivity.java

       
        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.apprestrictions;
       
       
        18
       
       
       
       
        19
       
       
        import android.app.Activity;
       
       
        20
       
       
        import android.content.Context;
       
       
        21
       
       
        import android.content.SharedPreferences;
       
       
        22
       
       
        import android.os.Bundle;
       
       
        23
       
       
        import android.os.UserManager;
       
       
        24
       
       
        import android.preference.PreferenceManager;
       
       
        25
       
       
        import android.view.View;
       
       
        26
       
       
        import android.widget.CheckBox;
       
       
        27
       
       
        import android.widget.TextView;
       
       
        28
       
       
       
       
        29
       
       
        /**
       
       
        30
       
       
        * This is the main user interface of the App Restrictions sample app.  It demonstrates the use
       
       
        31
       
       
        * of the App Restriction feature, which is available on Android 4.3 and above tablet devices
       
       
        32
       
       
        * with the multiuser feature.
       
       
        33
       
       
        *
       
       
        34
       
       
        * When launched under the primary User account, you can toggle between standard app restriction
       
       
        35
       
       
        * types and custom.  When launched under a restricted profile, this activity displays app
       
       
        36
       
       
        * restriction settings, if available.
       
       
        37
       
       
        *
       
       
        38
       
       
        * Follow these steps to exercise the feature:
       
       
        39
       
       
        * 1. If this is the primary user, go to Settings > Users.
       
       
        40
       
       
        * 2. Create a restricted profile, if one doesn't exist already.
       
       
        41
       
       
        * 3. Open the profile settings, locate the sample app, and tap the app restriction settings
       
       
        42
       
       
        *    icon. Configure app restrictions for the app.
       
       
        43
       
       
        * 4. In the lock screen, switch to the user's restricted profile, launch this sample app,
       
       
        44
       
       
        *    and see the configured app restrictions displayed.
       
       
        45
       
       
        */
       
       
        46
       
       
        public class MainActivity extends Activity {
       
       
        47
       
       
        private Bundle mRestrictionsBundle;
       
       
        48
       
       
       
       
        49
       
       
        // Checkbox to indicate whether custom or standard app restriction types are selected.
       
       
        50
       
       
        private CheckBox mCustomConfig;
       
       
        51
       
       
       
       
        52
       
       
        public static final String CUSTOM_CONFIG_KEY = "custom_config";
       
       
        53
       
       
       
       
        54
       
       
        private TextView mMultiEntryValue;
       
       
        55
       
       
        private TextView mChoiceEntryValue;
       
       
        56
       
       
        private TextView mBooleanEntryValue;
       
       
        57
       
       
       
       
        58
       
       
        @Override
       
       
        59
       
       
        protected void onCreate(Bundle savedInstanceState) {
       
       
        60
       
       
        super.onCreate(savedInstanceState);
       
       
        61
       
       
       
       
        62
       
       
        // Sets up  user interface elements.
       
       
        63
       
       
        setContentView(R.layout.main);
       
       
        64
       
       
       
       
        65
       
       
        mCustomConfig = (CheckBox) findViewById(R.id.custom_app_limits);
       
       
        66
       
       
        final boolean customChecked =
       
       
        67
       
       
        PreferenceManager.getDefaultSharedPreferences(this).getBoolean(
       
       
        68
       
       
        CUSTOM_CONFIG_KEY, false);
       
       
        69
       
       
        if (customChecked) mCustomConfig.setChecked(true);
       
       
        70
       
       
       
       
        71
       
       
        mMultiEntryValue = (TextView) findViewById(R.id.multi_entry_id);
       
       
        72
       
       
        mChoiceEntryValue = (TextView) findViewById(R.id.choice_entry_id);
       
       
        73
       
       
        mBooleanEntryValue = (TextView) findViewById(R.id.boolean_entry_id);
       
       
        74
       
       
        }
       
       
        75
       
       
       
       
        76
       
       
        @Override
       
       
        77
       
       
        protected void onResume() {
       
       
        78
       
       
        super.onResume();
       
       
        79
       
       
       
       
        80
       
       
        // If app restrictions are set for this package, when launched from a restricted profile,
       
       
        81
       
       
        // the settings are available in the returned Bundle as key/value pairs.
       
       
        82
       
       
        mRestrictionsBundle =
       
       
        83
       
       
        ((UserManager) getSystemService(Context.USER_SERVICE))
       
       
        84
       
       
        .getApplicationRestrictions(getPackageName());
       
       
        85
       
       
        if (mRestrictionsBundle == null) {
       
       
        86
       
       
        mRestrictionsBundle = new Bundle();
       
       
        87
       
       
        }
       
       
        88
       
       
       
       
        89
       
       
        // Reads and displays values from a boolean type restriction entry, if available.
       
       
        90
       
       
        // An app can utilize these settings to restrict its content under a restricted profile.
       
       
        91
       
       
        final String booleanRestrictionValue =
       
       
        92
       
       
        mRestrictionsBundle.containsKey(GetRestrictionsReceiver.KEY_BOOLEAN) ?
       
       
        93
       
       
        mRestrictionsBundle.getBoolean(GetRestrictionsReceiver.KEY_BOOLEAN) + "":
       
       
        94
       
       
        getString(R.string.na);
       
       
        95
       
       
        mBooleanEntryValue.setText(booleanRestrictionValue);
       
       
        96
       
       
       
       
        97
       
       
        // Reads and displays values from a single choice restriction entry, if available.
       
       
        98
       
       
        final String singleChoiceRestrictionValue =
       
       
        99
       
       
        mRestrictionsBundle.containsKey(GetRestrictionsReceiver.KEY_CHOICE) ?
       
       
        100
       
       
        mRestrictionsBundle.getString(GetRestrictionsReceiver.KEY_CHOICE) :
       
       
        101
       
       
        getString(R.string.na);
       
       
        102
       
       
        mChoiceEntryValue.setText(singleChoiceRestrictionValue);
       
       
        103
       
       
       
       
        104
       
       
        // Reads and displays values from a multi-select restriction entry, if available.
       
       
        105
       
       
        final String[] multiSelectValues =
       
       
        106
       
       
        mRestrictionsBundle.getStringArray(GetRestrictionsReceiver.KEY_MULTI_SELECT);
       
       
        107
       
       
        if (multiSelectValues == null || multiSelectValues.length == 0) {
       
       
        108
       
       
        mMultiEntryValue.setText(getString(R.string.na));
       
       
        109
       
       
        } else {
       
       
        110
       
       
        String tempValue = "";
       
       
        111
       
       
        for (String value : multiSelectValues) {
       
       
        112
       
       
        tempValue = tempValue + value + " ";
       
       
        113
       
       
        }
       
       
        114
       
       
        mMultiEntryValue.setText(tempValue);
       
       
        115
       
       
        }
       
       
        116
       
       
        }
       
       
        117
       
       
       
       
        118
       
       
        /**
       
       
        119
       
       
        * Saves custom app restriction to the shared preference.
       
       
        120
       
       
        *
       
       
        121
       
       
        * This flag is used by {@code GetRestrictionsReceiver} to determine if a custom app
       
       
        122
       
       
        * restriction activity should be used.
       
       
        123
       
       
        *
       
       
        124
       
       
        * @param view
       
       
        125
       
       
        */
       
       
        126
       
       
        public void onCustomClicked(View view) {
       
       
        127
       
       
        final SharedPreferences.Editor editor =
       
       
        128
       
       
        PreferenceManager.getDefaultSharedPreferences(this).edit();
       
       
        129
       
       
        editor.putBoolean(CUSTOM_CONFIG_KEY, mCustomConfig.isChecked()).commit();
       
       
        130
       
       
        }
       
       
        131
       
       
        }