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