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.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.content.RestrictionEntry;
24 import android.content.RestrictionsManager;
25 import android.os.Build;
26 import android.os.Bundle;
27 import android.os.Parcelable;
28 import android.support.annotation.Nullable;
29 import android.support.v4.app.Fragment;
30 import android.text.TextUtils;
31 import android.view.LayoutInflater;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.widget.Button;
35 import android.widget.TextView;
36 import android.widget.Toast;
37 
38 import com.example.android.common.logger.Log;
39 
40 import java.util.List;
41 
42 /**
43  * Pressing the button on this fragment pops up a simple Toast message. The button is enabled or
44  * disabled according to the restrictions set by device/profile owner. You can use the
45  * AppRestrictionEnforcer sample as a profile owner for this.
46  */
47 public class AppRestrictionSchemaFragment extends Fragment implements View.OnClickListener {
48 
49     // Tag for the logger
50     private static final String TAG = "AppRestrictionSchema";
51 
52     private static final String KEY_CAN_SAY_HELLO = "can_say_hello";
53     private static final String KEY_MESSAGE = "message";
54     private static final String KEY_NUMBER = "number";
55     private static final String KEY_RANK = "rank";
56     private static final String KEY_APPROVALS = "approvals";
57     private static final String KEY_ITEMS = "items";
58     private static final String KEY_ITEM_KEY = "key";
59     private static final String KEY_ITEM_VALUE = "value";
60 
61     private static final boolean BUNDLE_SUPPORTED = Build.VERSION.SDK_INT >= 23;
62 
63     // Message to show when the button is clicked (String restriction)
64     private String mMessage;
65 
66     // Observes restriction changes
67     private BroadcastReceiver mBroadcastReceiver;
68 
69     // UI Components
70     private TextView mTextSayHello;
71     private Button mButtonSayHello;
72     private TextView mTextNumber;
73     private TextView mTextRank;
74     private TextView mTextApprovals;
75     private TextView mTextItems;
76 
77     @Override
onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)78     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
79                              @Nullable Bundle savedInstanceState) {
80         return inflater.inflate(R.layout.fragment_app_restriction_schema, container, false);
81     }
82 
83     @Override
onViewCreated(View view, @Nullable Bundle savedInstanceState)84     public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
85         mTextSayHello = (TextView) view.findViewById(R.id.say_hello_explanation);
86         mButtonSayHello = (Button) view.findViewById(R.id.say_hello);
87         mTextNumber = (TextView) view.findViewById(R.id.your_number);
88         mTextRank = (TextView) view.findViewById(R.id.your_rank);
89         mTextApprovals = (TextView) view.findViewById(R.id.approvals_you_have);
90         mTextItems = (TextView) view.findViewById(R.id.your_items);
91         mButtonSayHello.setOnClickListener(this);
92         if (BUNDLE_SUPPORTED) {
93             mTextItems.setVisibility(View.VISIBLE);
94         } else {
95             mTextItems.setVisibility(View.GONE);
96         }
97     }
98 
99     @Override
onResume()100     public void onResume() {
101         super.onResume();
102         resolveRestrictions();
103     }
104 
105     @Override
onStart()106     public void onStart() {
107         super.onStart();
108         mBroadcastReceiver = new BroadcastReceiver() {
109             @Override
110             public void onReceive(Context context, Intent intent) {
111                 resolveRestrictions();
112             }
113         };
114         getActivity().registerReceiver(mBroadcastReceiver,
115                 new IntentFilter(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED));
116     }
117 
118     @Override
onStop()119     public void onStop() {
120         super.onStop();
121         if (mBroadcastReceiver != null) {
122             getActivity().unregisterReceiver(mBroadcastReceiver);
123             mBroadcastReceiver = null;
124         }
125     }
126 
resolveRestrictions()127     private void resolveRestrictions() {
128         RestrictionsManager manager =
129                 (RestrictionsManager) getActivity().getSystemService(Context.RESTRICTIONS_SERVICE);
130         Bundle restrictions = manager.getApplicationRestrictions();
131         List<RestrictionEntry> entries = manager.getManifestRestrictions(
132                 getActivity().getApplicationContext().getPackageName());
133         for (RestrictionEntry entry : entries) {
134             String key = entry.getKey();
135             Log.d(TAG, "key: " + key);
136             if (key.equals(KEY_CAN_SAY_HELLO)) {
137                 updateCanSayHello(entry, restrictions);
138             } else if (key.equals(KEY_MESSAGE)) {
139                 updateMessage(entry, restrictions);
140             } else if (key.equals(KEY_NUMBER)) {
141                 updateNumber(entry, restrictions);
142             } else if (key.equals(KEY_RANK)) {
143                 updateRank(entry, restrictions);
144             } else if (key.equals(KEY_APPROVALS)) {
145                 updateApprovals(entry, restrictions);
146             } else if (key.equals(KEY_ITEMS)) {
147                 updateItems(restrictions);
148             }
149         }
150     }
151 
updateCanSayHello(RestrictionEntry entry, Bundle restrictions)152     private void updateCanSayHello(RestrictionEntry entry, Bundle restrictions) {
153         boolean canSayHello;
154         if (restrictions == null || !restrictions.containsKey(KEY_CAN_SAY_HELLO)) {
155             canSayHello = entry.getSelectedState();
156         } else {
157             canSayHello = restrictions.getBoolean(KEY_CAN_SAY_HELLO);
158         }
159         mTextSayHello.setText(canSayHello ?
160                 R.string.explanation_can_say_hello_true :
161                 R.string.explanation_can_say_hello_false);
162         mButtonSayHello.setEnabled(canSayHello);
163     }
164 
updateMessage(RestrictionEntry entry, Bundle restrictions)165     private void updateMessage(RestrictionEntry entry, Bundle restrictions) {
166         if (restrictions == null || !restrictions.containsKey(KEY_MESSAGE)) {
167             mMessage = entry.getSelectedString();
168         } else {
169             mMessage = restrictions.getString(KEY_MESSAGE);
170         }
171     }
172 
updateNumber(RestrictionEntry entry, Bundle restrictions)173     private void updateNumber(RestrictionEntry entry, Bundle restrictions) {
174         int number;
175         if (restrictions == null || !restrictions.containsKey(KEY_NUMBER)) {
176             number = entry.getIntValue();
177         } else {
178             number = restrictions.getInt(KEY_NUMBER);
179         }
180         mTextNumber.setText(getString(R.string.your_number, number));
181     }
182 
updateRank(RestrictionEntry entry, Bundle restrictions)183     private void updateRank(RestrictionEntry entry, Bundle restrictions) {
184         String rank;
185         if (restrictions == null || !restrictions.containsKey(KEY_RANK)) {
186             rank = entry.getSelectedString();
187         } else {
188             rank = restrictions.getString(KEY_RANK);
189         }
190         mTextRank.setText(getString(R.string.your_rank, rank));
191     }
192 
updateApprovals(RestrictionEntry entry, Bundle restrictions)193     private void updateApprovals(RestrictionEntry entry, Bundle restrictions) {
194         String[] approvals;
195         if (restrictions == null || !restrictions.containsKey(KEY_APPROVALS)) {
196             approvals = entry.getAllSelectedStrings();
197         } else {
198             approvals = restrictions.getStringArray(KEY_APPROVALS);
199         }
200         String text;
201         if (approvals == null || approvals.length == 0) {
202             text = getString(R.string.none);
203         } else {
204             text = TextUtils.join(", ", approvals);
205         }
206         mTextApprovals.setText(getString(R.string.approvals_you_have, text));
207     }
208 
updateItems(Bundle restrictions)209     private void updateItems(Bundle restrictions) {
210         if (!BUNDLE_SUPPORTED) {
211             return;
212         }
213         StringBuilder builder = new StringBuilder();
214         if (restrictions != null) {
215             Parcelable[] parcelables = restrictions.getParcelableArray(KEY_ITEMS);
216             if (parcelables != null && parcelables.length > 0) {
217                 Bundle[] items = new Bundle[parcelables.length];
218                 for (int i = 0; i < parcelables.length; i++) {
219                     items[i] = (Bundle) parcelables[i];
220                 }
221                 boolean first = true;
222                 for (Bundle item : items) {
223                     if (!item.containsKey(KEY_ITEM_KEY) || !item.containsKey(KEY_ITEM_VALUE)) {
224                         continue;
225                     }
226                     if (first) {
227                         first = false;
228                     } else {
229                         builder.append(", ");
230                     }
231                     builder.append(item.getString(KEY_ITEM_KEY));
232                     builder.append(":");
233                     builder.append(item.getString(KEY_ITEM_VALUE));
234                 }
235             } else {
236                 builder.append(getString(R.string.none));
237             }
238         } else {
239             builder.append(getString(R.string.none));
240         }
241         mTextItems.setText(getString(R.string.your_items, builder));
242     }
243 
244     @Override
onClick(View view)245     public void onClick(View view) {
246         switch (view.getId()) {
247             case R.id.say_hello: {
248                 Toast.makeText(getActivity(), getString(R.string.message, mMessage),
249                         Toast.LENGTH_SHORT).show();
250                 break;
251             }
252         }
253     }
254 
255 }
256