diff options
author | Yuichi Araki <yaraki@google.com> | 2014-12-01 21:05:48 +0900 |
---|---|---|
committer | Yuichi Araki <yaraki@google.com> | 2014-12-09 13:16:29 +0900 |
commit | 2b431b5552d50c696bd80e9bf84c45509f727cc2 (patch) | |
tree | fa776d6a6c908dc0bf55dd108a44039c27e121dd /admin/AppRestrictionSchema | |
parent | bc036ecdf44cd03163c206096172299f3940b057 (diff) | |
download | android-2b431b5552d50c696bd80e9bf84c45509f727cc2.tar.gz |
AppRestrictionSchema, Enforcer: More restrictions
Bug: 18509464
Change-Id: I82e56aa4f79ea1d5c00d807489ddcfa5fd328061
Diffstat (limited to 'admin/AppRestrictionSchema')
7 files changed, 300 insertions, 47 deletions
diff --git a/admin/AppRestrictionSchema/Application/src/main/java/com/example/android/apprestrictionschema/AppRestrictionSchemaFragment.java b/admin/AppRestrictionSchema/Application/src/main/java/com/example/android/apprestrictionschema/AppRestrictionSchemaFragment.java index 76f024f3..7b8dba83 100644 --- a/admin/AppRestrictionSchema/Application/src/main/java/com/example/android/apprestrictionschema/AppRestrictionSchemaFragment.java +++ b/admin/AppRestrictionSchema/Application/src/main/java/com/example/android/apprestrictionschema/AppRestrictionSchemaFragment.java @@ -17,10 +17,12 @@ package com.example.android.apprestrictionschema; import android.content.Context; +import android.content.RestrictionEntry; import android.content.RestrictionsManager; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; +import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -30,6 +32,8 @@ import android.widget.Toast; import com.example.android.common.logger.Log; +import java.util.List; + /** * Pressing the button on this fragment pops up a simple Toast message. The button is enabled or * disabled according to the restrictions set by device/profile owner. You can use the @@ -40,9 +44,21 @@ public class AppRestrictionSchemaFragment extends Fragment implements View.OnCli // Tag for the logger private static final String TAG = "AppRestrictionSchemaFragment"; + private static final String KEY_CAN_SAY_HELLO = "can_say_hello"; + private static final String KEY_MESSAGE = "message"; + private static final String KEY_NUMBER = "number"; + private static final String KEY_RANK = "rank"; + private static final String KEY_APPROVALS = "approvals"; + + // Message to show when the button is clicked (String restriction) + private String mMessage; + // UI Components private TextView mTextSayHello; private Button mButtonSayHello; + private TextView mTextNumber; + private TextView mTextRank; + private TextView mTextApprovals; @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @@ -54,48 +70,103 @@ public class AppRestrictionSchemaFragment extends Fragment implements View.OnCli public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { mTextSayHello = (TextView) view.findViewById(R.id.say_hello_explanation); mButtonSayHello = (Button) view.findViewById(R.id.say_hello); + mTextNumber = (TextView) view.findViewById(R.id.your_number); + mTextRank = (TextView) view.findViewById(R.id.your_rank); + mTextApprovals = (TextView) view.findViewById(R.id.approvals_you_have); mButtonSayHello.setOnClickListener(this); } @Override public void onResume() { super.onResume(); - // Update the UI according to the configured restrictions - RestrictionsManager restrictionsManager = + resolveRestrictions(); + } + + private void resolveRestrictions() { + RestrictionsManager manager = (RestrictionsManager) getActivity().getSystemService(Context.RESTRICTIONS_SERVICE); - Bundle restrictions = restrictionsManager.getApplicationRestrictions(); - updateUI(restrictions); + Bundle restrictions = manager.getApplicationRestrictions(); + List<RestrictionEntry> entries = manager.getManifestRestrictions(getActivity().getApplicationContext().getPackageName()); + for (RestrictionEntry entry : entries) { + String key = entry.getKey(); + Log.d(TAG, "key: " + key); + if (key.equals(KEY_CAN_SAY_HELLO)) { + updateCanSayHello(entry, restrictions); + } else if (key.equals(KEY_MESSAGE)) { + updateMessage(entry, restrictions); + } else if (key.equals(KEY_NUMBER)) { + updateNumber(entry, restrictions); + } else if (key.equals(KEY_RANK)) { + updateRank(entry, restrictions); + } else if (key.equals(KEY_APPROVALS)) { + updateApprovals(entry, restrictions); + } + } + } + + private void updateCanSayHello(RestrictionEntry entry, Bundle restrictions) { + boolean canSayHello; + if (restrictions == null || !restrictions.containsKey(KEY_CAN_SAY_HELLO)) { + canSayHello = entry.getSelectedState(); + } else { + canSayHello = restrictions.getBoolean(KEY_CAN_SAY_HELLO); + } + mTextSayHello.setText(canSayHello ? + R.string.explanation_can_say_hello_true : + R.string.explanation_can_say_hello_false); + mButtonSayHello.setEnabled(canSayHello); } - private void updateUI(Bundle restrictions) { - if (canSayHello(restrictions)) { - mTextSayHello.setText(R.string.explanation_can_say_hello_true); - mButtonSayHello.setEnabled(true); + private void updateMessage(RestrictionEntry entry, Bundle restrictions) { + if (restrictions == null || !restrictions.containsKey(KEY_MESSAGE)) { + mMessage = entry.getSelectedString(); } else { - mTextSayHello.setText(R.string.explanation_can_say_hello_false); - mButtonSayHello.setEnabled(false); + mMessage = restrictions.getString(KEY_MESSAGE); } } - /** - * Returns the current status of the restriction. - * - * @param restrictions The application restrictions - * @return True if the app is allowed to say hello - */ - private boolean canSayHello(Bundle restrictions) { - final boolean defaultValue = false; - boolean canSayHello = restrictions == null ? defaultValue : - restrictions.getBoolean("can_say_hello", defaultValue); - Log.d(TAG, "canSayHello: " + canSayHello); - return canSayHello; + private void updateNumber(RestrictionEntry entry, Bundle restrictions) { + int number; + if (restrictions == null || !restrictions.containsKey(KEY_NUMBER)) { + number = entry.getIntValue(); + } else { + number = restrictions.getInt(KEY_NUMBER); + } + mTextNumber.setText(getString(R.string.your_number, number)); + } + + private void updateRank(RestrictionEntry entry, Bundle restrictions) { + String rank; + if (restrictions == null || !restrictions.containsKey(KEY_RANK)) { + rank = entry.getSelectedString(); + } else { + rank = restrictions.getString(KEY_RANK); + } + mTextRank.setText(getString(R.string.your_rank, rank)); + } + + private void updateApprovals(RestrictionEntry entry, Bundle restrictions) { + String[] approvals; + if (restrictions == null || !restrictions.containsKey(KEY_APPROVALS)) { + approvals = entry.getAllSelectedStrings(); + } else { + approvals = restrictions.getStringArray(KEY_APPROVALS); + } + String text; + if (approvals == null || approvals.length == 0) { + text = getString(R.string.none); + } else { + text = TextUtils.join(", ", approvals); + } + mTextApprovals.setText(getString(R.string.approvals_you_have, text)); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.say_hello: { - Toast.makeText(getActivity(), R.string.message_hello, Toast.LENGTH_SHORT).show(); + Toast.makeText(getActivity(), getString(R.string.message, mMessage), + Toast.LENGTH_SHORT).show(); break; } } diff --git a/admin/AppRestrictionSchema/Application/src/main/res/layout/fragment_app_restriction_schema.xml b/admin/AppRestrictionSchema/Application/src/main/res/layout/fragment_app_restriction_schema.xml index fc5e23dc..18ca0a4d 100644 --- a/admin/AppRestrictionSchema/Application/src/main/res/layout/fragment_app_restriction_schema.xml +++ b/admin/AppRestrictionSchema/Application/src/main/res/layout/fragment_app_restriction_schema.xml @@ -14,24 +14,60 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> -<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" - android:layout_width="match_parent" - android:layout_height="match_parent" - android:orientation="vertical" - android:padding="@dimen/margin_medium"> - - <TextView - android:id="@+id/say_hello_explanation" + + +<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent"> + + <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" - android:text="@string/explanation_can_say_hello_true" - android:textAppearance="?android:attr/textAppearanceMedium" /> + android:orientation="vertical" + android:padding="@dimen/margin_medium"> - <Button - android:id="@+id/say_hello" - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:layout_marginStart="@dimen/margin_medium" - android:text="@string/action_can_say_hello" /> + <TextView + android:id="@+id/say_hello_explanation" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:textAppearance="?android:attr/textAppearanceMedium" + tools:text="@string/explanation_can_say_hello_true"/> + + <Button + android:id="@+id/say_hello" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginStart="@dimen/margin_medium" + android:text="@string/action_can_say_hello"/> + + <include layout="@layout/separator"/> + + <TextView + android:id="@+id/your_number" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:textAppearance="?android:attr/textAppearanceMedium" + tools:text="@string/your_number"/> + + <include layout="@layout/separator"/> + + <TextView + android:id="@+id/your_rank" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:textAppearance="?android:attr/textAppearanceMedium" + tools:text="@string/your_rank"/> + + <include layout="@layout/separator"/> + + <TextView + android:id="@+id/approvals_you_have" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:textAppearance="?android:attr/textAppearanceMedium" + tools:text="@string/approvals_you_have"/> + + </LinearLayout> -</LinearLayout> +</ScrollView> diff --git a/admin/AppRestrictionSchema/Application/src/main/res/layout/separator.xml b/admin/AppRestrictionSchema/Application/src/main/res/layout/separator.xml new file mode 100644 index 00000000..6927d801 --- /dev/null +++ b/admin/AppRestrictionSchema/Application/src/main/res/layout/separator.xml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +Copyright 2014 The Android Open Source Project + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> +<View xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="1dp" + android:layout_marginBottom="@dimen/margin_medium" + android:layout_marginTop="@dimen/margin_medium" + android:background="#9000"/> diff --git a/admin/AppRestrictionSchema/Application/src/main/res/values/restriction_values.xml b/admin/AppRestrictionSchema/Application/src/main/res/values/restriction_values.xml new file mode 100644 index 00000000..558d097f --- /dev/null +++ b/admin/AppRestrictionSchema/Application/src/main/res/values/restriction_values.xml @@ -0,0 +1,77 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +Copyright 2014 The Android Open Source Project + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> +<resources> + + <!-- Bool restriction --> + <string name="title_can_say_hello">Can say hello</string> + <string name="description_can_say_hello">Whether the app can say hello to the user</string> + <bool name="default_can_say_hello">false</bool> + + <!-- String restriction --> + <string name="title_message">Message</string> + <string name="description_message">A message string to show</string> + <string name="default_message">Hello!</string> + + <!-- Integer restriction --> + <string name="title_number">Number</string> + <string name="description_number">Sample integer value</string> + <integer name="default_number">32582657</integer> + + <!-- Choice restriction --> + <string name="title_rank">Rank</string> + <string name="description_rank">Rank of the user</string> + <string-array name="entries_rank"> + <item>Apprentice</item> + <item>Intermediate</item> + <item>Master</item> + </string-array> + <string name="entry_value_rank_apprentice">apprentice</string> + <string name="entry_value_rank_intermediate">intermediate</string> + <string name="entry_value_rank_master">master</string> + <string-array name="entry_values_rank"> + <item>@string/entry_value_rank_apprentice</item> + <item>@string/entry_value_rank_intermediate</item> + <item>@string/entry_value_rank_master</item> + </string-array> + <string name="default_rank">@string/entry_value_rank_apprentice</string> + + <!-- Multi-select restriction --> + <string name="title_approvals">Approvals</string> + <string name="description_approvals">Approvals</string> + <string-array name="entries_approvals"> + <item>Read</item> + <item>Write</item> + <item>Execute</item> + </string-array> + <string name="entry_value_approvals_read">read</string> + <string name="entry_value_approvals_write">write</string> + <string name="entry_value_approvals_execute">execute</string> + <string-array name="entry_values_approvals"> + <item>@string/entry_value_approvals_read</item> + <item>@string/entry_value_approvals_write</item> + <item>@string/entry_value_approvals_execute</item> + </string-array> + <string-array name="default_approvals"> + <!-- Empty --> + </string-array> + + <!-- Hidden restriction --> + <string name="title_secret_code">Secret code</string> + <string name="description_secret_code">This restriction is hidden and will not be shown to the administrator.</string> + <string name="default_secret_code">(Hidden restriction must have some default value)</string> + +</resources> diff --git a/admin/AppRestrictionSchema/Application/src/main/res/values/strings.xml b/admin/AppRestrictionSchema/Application/src/main/res/values/strings.xml index b8ef110b..6dce123f 100644 --- a/admin/AppRestrictionSchema/Application/src/main/res/values/strings.xml +++ b/admin/AppRestrictionSchema/Application/src/main/res/values/strings.xml @@ -16,11 +16,14 @@ limitations under the License. --> <resources> - <string name="title_can_say_hello">Can say hello</string> - <string name="description_can_say_hello">Whether the app can say hello to the user</string> <string name="explanation_can_say_hello_true">I can say hello to you.</string> <string name="explanation_can_say_hello_false">I am restricted from saying hello to you.</string> <string name="action_can_say_hello">Say hello</string> - <string name="message_hello">Hello!</string> + <string name="message">All I can say is \"%s\".</string> -</resources>
\ No newline at end of file + <string name="your_number">Your number: %d</string> + <string name="your_rank">Your rank: %s</string> + <string name="approvals_you_have">Approvals you have: %s</string> + <string name="none">none</string> + +</resources> diff --git a/admin/AppRestrictionSchema/Application/src/main/res/xml/app_restrictions.xml b/admin/AppRestrictionSchema/Application/src/main/res/xml/app_restrictions.xml index 409527fc..9e47f458 100644 --- a/admin/AppRestrictionSchema/Application/src/main/res/xml/app_restrictions.xml +++ b/admin/AppRestrictionSchema/Application/src/main/res/xml/app_restrictions.xml @@ -16,11 +16,55 @@ limitations under the License. --> <restrictions xmlns:android="http://schemas.android.com/apk/res/android"> + <!-- + Refer to the javadoc of RestrictionsManager for detail of this file. + https://developer.android.com/reference/android/content/RestrictionsManager.html + --> + <restriction - android:defaultValue="false" + android:defaultValue="@bool/default_can_say_hello" android:description="@string/description_can_say_hello" android:key="can_say_hello" android:restrictionType="bool" - android:title="@string/title_can_say_hello" /> + android:title="@string/title_can_say_hello"/> + + <restriction + android:defaultValue="@string/default_message" + android:description="@string/description_message" + android:key="message" + android:restrictionType="string" + android:title="@string/title_message"/> + + <restriction + android:defaultValue="@integer/default_number" + android:description="@string/description_number" + android:key="number" + android:restrictionType="integer" + android:title="@string/title_number"/> + + <restriction + android:defaultValue="@string/default_rank" + android:description="@string/description_rank" + android:entries="@array/entries_rank" + android:entryValues="@array/entry_values_rank" + android:key="rank" + android:restrictionType="choice" + android:title="@string/title_rank"/> + + <restriction + android:defaultValue="@array/default_approvals" + android:description="@string/description_approvals" + android:entries="@array/entries_approvals" + android:entryValues="@array/entry_values_approvals" + android:key="approvals" + android:restrictionType="multi-select" + android:title="@string/title_approvals"/> + + <restriction + android:defaultValue="@string/default_secret_code" + android:description="@string/description_secret_code" + android:key="secret_code" + android:restrictionType="hidden" + android:title="@string/title_secret_code"/> </restrictions> diff --git a/admin/AppRestrictionSchema/gradle/wrapper/gradle-wrapper.properties b/admin/AppRestrictionSchema/gradle/wrapper/gradle-wrapper.properties index 0c71e760..fb79885e 100644 --- a/admin/AppRestrictionSchema/gradle/wrapper/gradle-wrapper.properties +++ b/admin/AppRestrictionSchema/gradle/wrapper/gradle-wrapper.properties @@ -1,4 +1,4 @@ -#Wed Apr 10 15:27:10 PDT 2013 +#Mon Dec 01 11:44:58 JST 2014 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME |