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_PROFILE = "profile"; 55 private static final String KEY_PROFILE_NAME = "name"; 56 private static final String KEY_PROFILE_AGE = "age"; 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 // UI Components 67 private TextView mTextSayHello; 68 private Button mButtonSayHello; 69 private TextView mTextNumber; 70 private TextView mTextRank; 71 private TextView mTextApprovals; 72 private TextView mTextProfile; 73 private TextView mTextItems; 74 75 @Override 76 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 77 @Nullable Bundle savedInstanceState) { 78 return inflater.inflate(R.layout.fragment_app_restriction_schema, container, false); 79 } 80 81 @Override 82 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 83 mTextSayHello = (TextView) view.findViewById(R.id.say_hello_explanation); 84 mButtonSayHello = (Button) view.findViewById(R.id.say_hello); 85 mTextNumber = (TextView) view.findViewById(R.id.your_number); 86 mTextRank = (TextView) view.findViewById(R.id.your_rank); 87 mTextApprovals = (TextView) view.findViewById(R.id.approvals_you_have); 88 View bundleSeparator = view.findViewById(R.id.bundle_separator); 89 mTextProfile = (TextView) view.findViewById(R.id.your_profile); 90 View bundleArraySeparator = view.findViewById(R.id.bundle_array_separator); 91 mTextItems = (TextView) view.findViewById(R.id.your_items); 92 mButtonSayHello.setOnClickListener(this); 93 if (BUNDLE_SUPPORTED) { 94 bundleSeparator.setVisibility(View.VISIBLE); 95 mTextProfile.setVisibility(View.VISIBLE); 96 bundleArraySeparator.setVisibility(View.VISIBLE); 97 mTextItems.setVisibility(View.VISIBLE); 98 } else { 99 bundleSeparator.setVisibility(View.GONE); 100 mTextProfile.setVisibility(View.GONE); 101 bundleArraySeparator.setVisibility(View.GONE); 102 mTextItems.setVisibility(View.GONE); 103 } 104 } 105 106 @Override 107 public void onResume() { 108 super.onResume(); 109 resolveRestrictions(); 110 } 111 112 private void resolveRestrictions() { 113 RestrictionsManager manager = 114 (RestrictionsManager) getActivity().getSystemService(Context.RESTRICTIONS_SERVICE); 115 Bundle restrictions = manager.getApplicationRestrictions(); 116 List<RestrictionEntry> entries = manager.getManifestRestrictions( 117 getActivity().getApplicationContext().getPackageName()); 118 for (RestrictionEntry entry : entries) { 119 String key = entry.getKey(); 120 Log.d(TAG, "key: " + key); 121 if (key.equals(KEY_CAN_SAY_HELLO)) { 122 updateCanSayHello(entry, restrictions); 123 } else if (key.equals(KEY_MESSAGE)) { 124 updateMessage(entry, restrictions); 125 } else if (key.equals(KEY_NUMBER)) { 126 updateNumber(entry, restrictions); 127 } else if (key.equals(KEY_RANK)) { 128 updateRank(entry, restrictions); 129 } else if (key.equals(KEY_APPROVALS)) { 130 updateApprovals(entry, restrictions); 131 } else if (key.equals(KEY_PROFILE)) { 132 updateProfile(entry, restrictions); 133 } else if (key.equals(KEY_ITEMS)) { 134 updateItems(entry, restrictions); 135 } 136 } 137 } 138 139 private void updateCanSayHello(RestrictionEntry entry, Bundle restrictions) { 140 boolean canSayHello; 141 if (restrictions == null || !restrictions.containsKey(KEY_CAN_SAY_HELLO)) { 142 canSayHello = entry.getSelectedState(); 143 } else { 144 canSayHello = restrictions.getBoolean(KEY_CAN_SAY_HELLO); 145 } 146 mTextSayHello.setText(canSayHello ? 147 R.string.explanation_can_say_hello_true : 148 R.string.explanation_can_say_hello_false); 149 mButtonSayHello.setEnabled(canSayHello); 150 } 151 152 private void updateMessage(RestrictionEntry entry, Bundle restrictions) { 153 if (restrictions == null || !restrictions.containsKey(KEY_MESSAGE)) { 154 mMessage = entry.getSelectedString(); 155 } else { 156 mMessage = restrictions.getString(KEY_MESSAGE); 157 } 158 } 159 160 private void updateNumber(RestrictionEntry entry, Bundle restrictions) { 161 int number; 162 if (restrictions == null || !restrictions.containsKey(KEY_NUMBER)) { 163 number = entry.getIntValue(); 164 } else { 165 number = restrictions.getInt(KEY_NUMBER); 166 } 167 mTextNumber.setText(getString(R.string.your_number, number)); 168 } 169 170 private void updateRank(RestrictionEntry entry, Bundle restrictions) { 171 String rank; 172 if (restrictions == null || !restrictions.containsKey(KEY_RANK)) { 173 rank = entry.getSelectedString(); 174 } else { 175 rank = restrictions.getString(KEY_RANK); 176 } 177 mTextRank.setText(getString(R.string.your_rank, rank)); 178 } 179 180 private void updateApprovals(RestrictionEntry entry, Bundle restrictions) { 181 String[] approvals; 182 if (restrictions == null || !restrictions.containsKey(KEY_APPROVALS)) { 183 approvals = entry.getAllSelectedStrings(); 184 } else { 185 approvals = restrictions.getStringArray(KEY_APPROVALS); 186 } 187 String text; 188 if (approvals == null || approvals.length == 0) { 189 text = getString(R.string.none); 190 } else { 191 text = TextUtils.join(", ", approvals); 192 } 193 mTextApprovals.setText(getString(R.string.approvals_you_have, text)); 194 } 195 196 private void updateProfile(RestrictionEntry entry, Bundle restrictions) { 197 if (!BUNDLE_SUPPORTED) { 198 return; 199 } 200 String name = null; 201 int age = 0; 202 if (restrictions == null || !restrictions.containsKey(KEY_PROFILE)) { 203 RestrictionEntry[] entries = entry.getRestrictions(); 204 for (RestrictionEntry profileEntry : entries) { 205 String key = profileEntry.getKey(); 206 if (key.equals(KEY_PROFILE_NAME)) { 207 name = profileEntry.getSelectedString(); 208 } else if (key.equals(KEY_PROFILE_AGE)) { 209 age = profileEntry.getIntValue(); 210 } 211 } 212 } else { 213 Bundle profile = restrictions.getBundle(KEY_PROFILE); 214 if (profile != null) { 215 name = profile.getString(KEY_PROFILE_NAME); 216 age = profile.getInt(KEY_PROFILE_AGE); 217 } 218 } 219 mTextProfile.setText(getString(R.string.your_profile, name, age)); 220 } 221 222 private void updateItems(RestrictionEntry entry, Bundle restrictions) { 223 if (!BUNDLE_SUPPORTED) { 224 return; 225 } 226 StringBuilder builder = new StringBuilder(); 227 if (restrictions != null) { 228 Parcelable[] parcelables = restrictions.getParcelableArray(KEY_ITEMS); 229 if (parcelables != null && parcelables.length > 0) { 230 Bundle[] items = new Bundle[parcelables.length]; 231 for (int i = 0; i < parcelables.length; i++) { 232 items[i] = (Bundle) parcelables[i]; 233 } 234 boolean first = true; 235 for (Bundle item : items) { 236 if (!item.containsKey(KEY_ITEM_KEY) || !item.containsKey(KEY_ITEM_VALUE)) { 237 continue; 238 } 239 if (first) { 240 first = false; 241 } else { 242 builder.append(", "); 243 } 244 builder.append(item.getString(KEY_ITEM_KEY)); 245 builder.append(":"); 246 builder.append(item.getString(KEY_ITEM_VALUE)); 247 } 248 } else { 249 builder.append(getString(R.string.none)); 250 } 251 } else { 252 builder.append(getString(R.string.none)); 253 } 254 mTextItems.setText(getString(R.string.your_items, builder)); 255 } 256 257 @Override 258 public void onClick(View view) { 259 switch (view.getId()) { 260 case R.id.say_hello: { 261 Toast.makeText(getActivity(), getString(R.string.message, mMessage), 262 Toast.LENGTH_SHORT).show(); 263 break; 264 } 265 } 266 } 267 268 } 269