Home | History | Annotate | Download | only in com.example.android.apprestrictionschema
      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.example.android.apprestrictionschema;
     18 
     19 import android.content.Context;
     20 import android.content.RestrictionEntry;
     21 import android.content.RestrictionsManager;
     22 import android.os.Bundle;
     23 import android.support.annotation.Nullable;
     24 import android.support.v4.app.Fragment;
     25 import android.text.TextUtils;
     26 import android.view.LayoutInflater;
     27 import android.view.View;
     28 import android.view.ViewGroup;
     29 import android.widget.Button;
     30 import android.widget.TextView;
     31 import android.widget.Toast;
     32 
     33 import com.example.android.common.logger.Log;
     34 
     35 import java.util.List;
     36 
     37 /**
     38  * Pressing the button on this fragment pops up a simple Toast message. The button is enabled or
     39  * disabled according to the restrictions set by device/profile owner. You can use the
     40  * AppRestrictionEnforcer sample as a profile owner for this.
     41  */
     42 public class AppRestrictionSchemaFragment extends Fragment implements View.OnClickListener {
     43 
     44     // Tag for the logger
     45     private static final String TAG = "AppRestrictionSchemaFragment";
     46 
     47     private static final String KEY_CAN_SAY_HELLO = "can_say_hello";
     48     private static final String KEY_MESSAGE = "message";
     49     private static final String KEY_NUMBER = "number";
     50     private static final String KEY_RANK = "rank";
     51     private static final String KEY_APPROVALS = "approvals";
     52 
     53     // Message to show when the button is clicked (String restriction)
     54     private String mMessage;
     55 
     56     // UI Components
     57     private TextView mTextSayHello;
     58     private Button mButtonSayHello;
     59     private TextView mTextNumber;
     60     private TextView mTextRank;
     61     private TextView mTextApprovals;
     62 
     63     @Override
     64     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
     65                              @Nullable Bundle savedInstanceState) {
     66         return inflater.inflate(R.layout.fragment_app_restriction_schema, container, false);
     67     }
     68 
     69     @Override
     70     public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
     71         mTextSayHello = (TextView) view.findViewById(R.id.say_hello_explanation);
     72         mButtonSayHello = (Button) view.findViewById(R.id.say_hello);
     73         mTextNumber = (TextView) view.findViewById(R.id.your_number);
     74         mTextRank = (TextView) view.findViewById(R.id.your_rank);
     75         mTextApprovals = (TextView) view.findViewById(R.id.approvals_you_have);
     76         mButtonSayHello.setOnClickListener(this);
     77     }
     78 
     79     @Override
     80     public void onResume() {
     81         super.onResume();
     82         resolveRestrictions();
     83     }
     84 
     85     private void resolveRestrictions() {
     86         RestrictionsManager manager =
     87                 (RestrictionsManager) getActivity().getSystemService(Context.RESTRICTIONS_SERVICE);
     88         Bundle restrictions = manager.getApplicationRestrictions();
     89         List<RestrictionEntry> entries = manager.getManifestRestrictions(getActivity().getApplicationContext().getPackageName());
     90         for (RestrictionEntry entry : entries) {
     91             String key = entry.getKey();
     92             Log.d(TAG, "key: " + key);
     93             if (key.equals(KEY_CAN_SAY_HELLO)) {
     94                 updateCanSayHello(entry, restrictions);
     95             } else if (key.equals(KEY_MESSAGE)) {
     96                 updateMessage(entry, restrictions);
     97             } else if (key.equals(KEY_NUMBER)) {
     98                 updateNumber(entry, restrictions);
     99             } else if (key.equals(KEY_RANK)) {
    100                 updateRank(entry, restrictions);
    101             } else if (key.equals(KEY_APPROVALS)) {
    102                 updateApprovals(entry, restrictions);
    103             }
    104         }
    105     }
    106 
    107     private void updateCanSayHello(RestrictionEntry entry, Bundle restrictions) {
    108         boolean canSayHello;
    109         if (restrictions == null || !restrictions.containsKey(KEY_CAN_SAY_HELLO)) {
    110             canSayHello = entry.getSelectedState();
    111         } else {
    112             canSayHello = restrictions.getBoolean(KEY_CAN_SAY_HELLO);
    113         }
    114         mTextSayHello.setText(canSayHello ?
    115                 R.string.explanation_can_say_hello_true :
    116                 R.string.explanation_can_say_hello_false);
    117         mButtonSayHello.setEnabled(canSayHello);
    118     }
    119 
    120     private void updateMessage(RestrictionEntry entry, Bundle restrictions) {
    121         if (restrictions == null || !restrictions.containsKey(KEY_MESSAGE)) {
    122             mMessage = entry.getSelectedString();
    123         } else {
    124             mMessage = restrictions.getString(KEY_MESSAGE);
    125         }
    126     }
    127 
    128     private void updateNumber(RestrictionEntry entry, Bundle restrictions) {
    129         int number;
    130         if (restrictions == null || !restrictions.containsKey(KEY_NUMBER)) {
    131             number = entry.getIntValue();
    132         } else {
    133             number = restrictions.getInt(KEY_NUMBER);
    134         }
    135         mTextNumber.setText(getString(R.string.your_number, number));
    136     }
    137 
    138     private void updateRank(RestrictionEntry entry, Bundle restrictions) {
    139         String rank;
    140         if (restrictions == null || !restrictions.containsKey(KEY_RANK)) {
    141             rank = entry.getSelectedString();
    142         } else {
    143             rank = restrictions.getString(KEY_RANK);
    144         }
    145         mTextRank.setText(getString(R.string.your_rank, rank));
    146     }
    147 
    148     private void updateApprovals(RestrictionEntry entry, Bundle restrictions) {
    149         String[] approvals;
    150         if (restrictions == null || !restrictions.containsKey(KEY_APPROVALS)) {
    151             approvals = entry.getAllSelectedStrings();
    152         } else {
    153             approvals = restrictions.getStringArray(KEY_APPROVALS);
    154         }
    155         String text;
    156         if (approvals == null || approvals.length == 0) {
    157             text = getString(R.string.none);
    158         } else {
    159             text = TextUtils.join(", ", approvals);
    160         }
    161         mTextApprovals.setText(getString(R.string.approvals_you_have, text));
    162     }
    163 
    164     @Override
    165     public void onClick(View view) {
    166         switch (view.getId()) {
    167             case R.id.say_hello: {
    168                 Toast.makeText(getActivity(), getString(R.string.message, mMessage),
    169                         Toast.LENGTH_SHORT).show();
    170                 break;
    171             }
    172         }
    173     }
    174 
    175 }
    176