Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2548)

Unified Diff: chrome/android/java_staging/src/org/chromium/chrome/browser/policy/providers/AppRestrictionsProvider.java

Issue 1141283003: Upstream oodles of Chrome for Android code into Chromium. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: final patch? Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java_staging/src/org/chromium/chrome/browser/policy/providers/AppRestrictionsProvider.java
diff --git a/chrome/android/java_staging/src/org/chromium/chrome/browser/policy/providers/AppRestrictionsProvider.java b/chrome/android/java_staging/src/org/chromium/chrome/browser/policy/providers/AppRestrictionsProvider.java
new file mode 100644
index 0000000000000000000000000000000000000000..b5c45c8574611cc3ebf995c35ee496c849a9213c
--- /dev/null
+++ b/chrome/android/java_staging/src/org/chromium/chrome/browser/policy/providers/AppRestrictionsProvider.java
@@ -0,0 +1,107 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chrome.browser.policy.providers;
+
+import android.annotation.TargetApi;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.os.AsyncTask;
+import android.os.Build;
+import android.os.Bundle;
+import android.os.UserManager;
+
+import org.chromium.chrome.browser.policy.PolicyProvider;
+
+/**
+ * Policy provider for Android's App Restriction Schema.
+ */
+public final class AppRestrictionsProvider extends PolicyProvider {
+ private final BroadcastReceiver mAppRestrictionsChangedReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ refresh();
+ }
+ };
+ private final UserManager mUserManager;
+
+ /**
+ * Register to receive the intent for App Restrictions.
+ */
+ public AppRestrictionsProvider(Context context) {
+ super(context);
+ mUserManager = getUserManager();
+ }
+
+ @Override
+ @TargetApi(Build.VERSION_CODES.LOLLIPOP)
+ protected void startListeningForPolicyChanges() {
+ if (!isChangeIntentSupported()) return;
+ mContext.registerReceiver(mAppRestrictionsChangedReceiver,
+ new IntentFilter(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED));
+ }
+
+ @Override
+ public void refresh() {
+ if (!isRestrictionsSupported()) {
+ notifySettingsAvailable(new Bundle());
+ return;
+ }
+
+ new AsyncTask<Void, Void, Bundle>() {
+ @Override
+ protected Bundle doInBackground(Void... params) {
+ return getApplicationRestrictions();
+ }
+
+ @Override
+ protected void onPostExecute(Bundle result) {
+ notifySettingsAvailable(result);
+ }
+ }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
+ }
+
+ @Override
+ public void destroy() {
+ if (isChangeIntentSupported()) {
+ mContext.unregisterReceiver(mAppRestrictionsChangedReceiver);
+ }
+ super.destroy();
+ }
+
+ /**
+ * getApplicationRestrictions method of UserManger was introduced in JELLY_BEAN_MR2.
+ */
+ private boolean isRestrictionsSupported() {
+ return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2;
+ }
+
+ /**
+ * Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED was introduced in LOLLIPOP.
+ */
+ private boolean isChangeIntentSupported() {
+ return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
+ }
+
+ /**
+ * Wrap access to the Android UserManager to allow being swapped out in environments where it
+ * is not available yet.
+ */
+ @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
+ private Bundle getApplicationRestrictions() {
+ return mUserManager.getApplicationRestrictions(mContext.getPackageName());
+ }
+
+ /**
+ * Wrap access to the Android UserManager to allow being swapped out in environments where it
+ * is not available yet.
+ */
+ @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
+ private UserManager getUserManager() {
+ if (!isRestrictionsSupported()) return null;
+ return (UserManager) mContext.getSystemService(Context.USER_SERVICE);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698