OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.policy.providers; |
| 6 |
| 7 import android.annotation.TargetApi; |
| 8 import android.content.BroadcastReceiver; |
| 9 import android.content.Context; |
| 10 import android.content.Intent; |
| 11 import android.content.IntentFilter; |
| 12 import android.os.AsyncTask; |
| 13 import android.os.Build; |
| 14 import android.os.Bundle; |
| 15 import android.os.UserManager; |
| 16 |
| 17 import org.chromium.chrome.browser.policy.PolicyProvider; |
| 18 |
| 19 /** |
| 20 * Policy provider for Android's App Restriction Schema. |
| 21 */ |
| 22 public final class AppRestrictionsProvider extends PolicyProvider { |
| 23 private final BroadcastReceiver mAppRestrictionsChangedReceiver = new Broadc
astReceiver() { |
| 24 @Override |
| 25 public void onReceive(Context context, Intent intent) { |
| 26 refresh(); |
| 27 } |
| 28 }; |
| 29 private final UserManager mUserManager; |
| 30 |
| 31 /** |
| 32 * Register to receive the intent for App Restrictions. |
| 33 */ |
| 34 public AppRestrictionsProvider(Context context) { |
| 35 super(context); |
| 36 mUserManager = getUserManager(); |
| 37 } |
| 38 |
| 39 @Override |
| 40 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 41 protected void startListeningForPolicyChanges() { |
| 42 if (!isChangeIntentSupported()) return; |
| 43 mContext.registerReceiver(mAppRestrictionsChangedReceiver, |
| 44 new IntentFilter(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED)
); |
| 45 } |
| 46 |
| 47 @Override |
| 48 public void refresh() { |
| 49 if (!isRestrictionsSupported()) { |
| 50 notifySettingsAvailable(new Bundle()); |
| 51 return; |
| 52 } |
| 53 |
| 54 new AsyncTask<Void, Void, Bundle>() { |
| 55 @Override |
| 56 protected Bundle doInBackground(Void... params) { |
| 57 return getApplicationRestrictions(); |
| 58 } |
| 59 |
| 60 @Override |
| 61 protected void onPostExecute(Bundle result) { |
| 62 notifySettingsAvailable(result); |
| 63 } |
| 64 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
| 65 } |
| 66 |
| 67 @Override |
| 68 public void destroy() { |
| 69 if (isChangeIntentSupported()) { |
| 70 mContext.unregisterReceiver(mAppRestrictionsChangedReceiver); |
| 71 } |
| 72 super.destroy(); |
| 73 } |
| 74 |
| 75 /** |
| 76 * getApplicationRestrictions method of UserManger was introduced in JELLY_B
EAN_MR2. |
| 77 */ |
| 78 private boolean isRestrictionsSupported() { |
| 79 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2; |
| 80 } |
| 81 |
| 82 /** |
| 83 * Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED was introduced in LOLLIPOP
. |
| 84 */ |
| 85 private boolean isChangeIntentSupported() { |
| 86 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; |
| 87 } |
| 88 |
| 89 /** |
| 90 * Wrap access to the Android UserManager to allow being swapped out in envi
ronments where it |
| 91 * is not available yet. |
| 92 */ |
| 93 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) |
| 94 private Bundle getApplicationRestrictions() { |
| 95 return mUserManager.getApplicationRestrictions(mContext.getPackageName()
); |
| 96 } |
| 97 |
| 98 /** |
| 99 * Wrap access to the Android UserManager to allow being swapped out in envi
ronments where it |
| 100 * is not available yet. |
| 101 */ |
| 102 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) |
| 103 private UserManager getUserManager() { |
| 104 if (!isRestrictionsSupported()) return null; |
| 105 return (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 106 } |
| 107 } |
OLD | NEW |