Use the new OemLockService to get OEM lock state.

Previously, the PersistentDataBlockService but that is only one possible
implementation of the OEM lock. The new service abstracts the
implementation so is the API that should be used.

Test: Manual
Bug: 34766843
Change-Id: I5f9cb94996f84c4c082d152f05cd8aef566edc66
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 9f266b6..66068eb 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -73,6 +73,7 @@
     <uses-permission android:name="android.permission.MANAGE_DEVICE_ADMINS" />
     <uses-permission android:name="android.permission.READ_SEARCH_INDEXABLES" />
     <uses-permission android:name="android.permission.OEM_UNLOCK_STATE" />
+    <uses-permission android:name="android.permission.MANAGE_USER_OEM_UNLOCK_STATE" />
     <uses-permission android:name="android.permission.OVERRIDE_WIFI_CONFIG" />
     <uses-permission android:name="android.permission.USE_FINGERPRINT" />
     <uses-permission android:name="android.permission.MANAGE_FINGERPRINT" />
diff --git a/src/com/android/settings/MasterClearConfirm.java b/src/com/android/settings/MasterClearConfirm.java
index 153a1aa..7a85dbe 100644
--- a/src/com/android/settings/MasterClearConfirm.java
+++ b/src/com/android/settings/MasterClearConfirm.java
@@ -25,6 +25,7 @@
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.provider.Settings;
+import android.service.oemlock.OemLockManager;
 import android.service.persistentdata.PersistentDataBlockManager;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -67,12 +68,14 @@
 
             final PersistentDataBlockManager pdbManager = (PersistentDataBlockManager)
                     getActivity().getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
+            final OemLockManager oemLockManager = (OemLockManager)
+                    getActivity().getSystemService(Context.OEM_LOCK_SERVICE);
 
-            if (pdbManager != null && !pdbManager.getOemUnlockEnabled() &&
+            if (pdbManager != null && !oemLockManager.isOemUnlockAllowed() &&
                     Utils.isDeviceProvisioned(getActivity())) {
-                // if OEM unlock is enabled, this will be wiped during FR process. If disabled, it
-                // will be wiped here, unless the device is still being provisioned, in which case
-                // the persistent data block will be preserved.
+                // if OEM unlock is allowed, the persistent data block will be wiped during FR
+                // process. If disabled, it will be wiped here, unless the device is still being
+                // provisioned, in which case the persistent data block will be preserved.
                 new AsyncTask<Void, Void, Void>() {
                     int mOldOrientation;
                     ProgressDialog mProgressDialog;
diff --git a/src/com/android/settings/development/DevelopmentSettings.java b/src/com/android/settings/development/DevelopmentSettings.java
index bfb9f0f..376005c 100644
--- a/src/com/android/settings/development/DevelopmentSettings.java
+++ b/src/com/android/settings/development/DevelopmentSettings.java
@@ -59,7 +59,7 @@
 import android.os.storage.IStorageManager;
 import android.provider.SearchIndexableResource;
 import android.provider.Settings;
-import android.service.persistentdata.PersistentDataBlockManager;
+import android.service.oemlock.OemLockManager;
 import android.support.annotation.VisibleForTesting;
 import android.support.v14.preference.SwitchPreference;
 import android.support.v7.preference.ListPreference;
@@ -238,7 +238,6 @@
     private static final int RESULT_DEBUG_APP = 1000;
     private static final int RESULT_MOCK_LOCATION_APP = 1001;
 
-    private static final String PERSISTENT_DATA_BLOCK_PROP = "ro.frp.pst";
     private static final String FLASH_LOCKED_PROP = "ro.boot.flash.locked";
 
     private static final String SHORTCUT_MANAGER_RESET_KEY = "reset_shortcut_manager_throttling";
@@ -252,7 +251,7 @@
     private IWebViewUpdateService mWebViewUpdateService;
     private UserManager mUm;
     private WifiManager mWifiManager;
-    private PersistentDataBlockManager mOemUnlockManager;
+    private OemLockManager mOemLockManager;
     private TelephonyManager mTelephonyManager;
 
     private SwitchBar mSwitchBar;
@@ -383,8 +382,7 @@
         mBackupManager = IBackupManager.Stub.asInterface(
                 ServiceManager.getService(Context.BACKUP_SERVICE));
         mWebViewUpdateService = WebViewFactory.getUpdateService();
-        mOemUnlockManager = (PersistentDataBlockManager) getActivity()
-                .getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
+        mOemLockManager = (OemLockManager) getSystemService(Context.OEM_LOCK_SERVICE);
         mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
 
         mUm = (UserManager) getSystemService(Context.USER_SERVICE);
@@ -431,7 +429,7 @@
         mKeepScreenOn = (RestrictedSwitchPreference) findAndInitSwitchPref(KEEP_SCREEN_ON);
         mBtHciSnoopLog = findAndInitSwitchPref(BT_HCI_SNOOP_LOG);
         mEnableOemUnlock = (RestrictedSwitchPreference) findAndInitSwitchPref(ENABLE_OEM_UNLOCK);
-        if (!showEnableOemUnlockPreference()) {
+        if (!showEnableOemUnlockPreference(getActivity())) {
             removePreference(mEnableOemUnlock);
             mEnableOemUnlock = null;
         }
@@ -1070,18 +1068,17 @@
                 Settings.Global.PACKAGE_VERIFIER_SETTING_VISIBLE, 1) > 0;
     }
 
-    private static boolean showEnableOemUnlockPreference() {
-        return !SystemProperties.get(PERSISTENT_DATA_BLOCK_PROP).equals("");
+    private static boolean showEnableOemUnlockPreference(Context context) {
+        return context.getSystemService(Context.OEM_LOCK_SERVICE) != null;
     }
 
     private boolean enableOemUnlockPreference() {
-        return !isBootloaderUnlocked() && OemUnlockUtils.isOemUnlockAllowed(mUm);
+        return !isBootloaderUnlocked() && mOemLockManager.canUserAllowOemUnlock();
     }
 
     private void updateOemUnlockOptions() {
         if (mEnableOemUnlock != null) {
-            updateSwitchPreference(mEnableOemUnlock,
-                    OemUnlockUtils.isOemUnlockEnabled(getActivity()));
+            updateSwitchPreference(mEnableOemUnlock, mOemLockManager.isOemUnlockAllowed());
             updateOemUnlockSettingDescription();
             // Showing mEnableOemUnlock preference as device has persistent data block.
             mEnableOemUnlock.setDisabledByAdmin(null);
@@ -2339,7 +2336,7 @@
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 if (which == DialogInterface.BUTTON_POSITIVE) {
-                    OemUnlockUtils.setOemUnlockEnabled(getActivity(), true);
+                    mOemLockManager.setOemUnlockAllowedByUser(true);
                 }
             }
         };
@@ -2410,7 +2407,7 @@
                 if (mEnableOemUnlock.isChecked()) {
                     confirmEnableOemUnlock();
                 } else {
-                    OemUnlockUtils.setOemUnlockEnabled(getActivity(), false);
+                    mOemLockManager.setOemUnlockAllowedByUser(false);
                 }
             }
         } else {
@@ -2480,7 +2477,7 @@
                     confirmEnableOemUnlock();
                 }
             } else {
-                OemUnlockUtils.setOemUnlockEnabled(getActivity(), false);
+                mOemLockManager.setOemUnlockAllowedByUser(false);
             }
         } else if (preference == mMockLocationAppPref) {
             Intent intent = new Intent(getActivity(), AppPicker.class);
@@ -2821,7 +2818,7 @@
                     }
 
                     final List<String> keys = new ArrayList<String>();
-                    if (!showEnableOemUnlockPreference()) {
+                    if (!showEnableOemUnlockPreference(context)) {
                         keys.add(ENABLE_OEM_UNLOCK);
                     }
                     return keys;
@@ -2849,11 +2846,10 @@
                 oemUnlockSummary = R.string.oem_unlock_enable_disabled_summary_bootloader_unlocked;
             } else if (isSimLockedDevice()) {
                 oemUnlockSummary = R.string.oem_unlock_enable_disabled_summary_sim_locked_device;
-            } else if (!OemUnlockUtils.isOemUnlockAllowed(mUm)) {
-                // If the device isn't SIM-locked but OEM unlock is disabled by the system via the
-                // user restriction, this means either some other carrier restriction is in place or
-                // the device hasn't been able to confirm which restrictions (SIM-lock or otherwise)
-                // apply.
+            } else if (!mOemLockManager.canUserAllowOemUnlock()) {
+                // If the device isn't SIM-locked but OEM unlock is disallowed by some party, this
+                // means either some other carrier restriction is in place or the device hasn't been
+                // able to confirm which restrictions (SIM-lock or otherwise) apply.
                 oemUnlockSummary =
                         R.string.oem_unlock_enable_disabled_summary_connectivity_or_locked;
             }
@@ -2876,12 +2872,7 @@
      * Returns {@code true} if the bootloader has been unlocked. Otherwise, returns {code false}.
      */
     private boolean isBootloaderUnlocked() {
-        int flashLockState = PersistentDataBlockManager.FLASH_LOCK_UNKNOWN;
-        if (mOemUnlockManager != null) {
-            flashLockState = mOemUnlockManager.getFlashLockState();
-        }
-
-        return flashLockState == PersistentDataBlockManager.FLASH_LOCK_UNLOCKED;
+        return mOemLockManager.isDeviceOemUnlocked();
     }
 
 
diff --git a/src/com/android/settings/development/OemUnlockUtils.java b/src/com/android/settings/development/OemUnlockUtils.java
deleted file mode 100644
index cee3679..0000000
--- a/src/com/android/settings/development/OemUnlockUtils.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright (C) 2017 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.
- */
-
-package com.android.settings.development;
-
-import android.content.Context;
-import android.os.UserHandle;
-import android.os.UserManager;
-import android.service.persistentdata.PersistentDataBlockManager;
-import android.util.Log;
-
-public class OemUnlockUtils {
-    private static final String TAG = "OemUnlockUtils";
-
-    /**
-     * Returns whether or not this device is able to be OEM unlocked.
-     */
-    static boolean isOemUnlockEnabled(Context context) {
-        PersistentDataBlockManager manager = (PersistentDataBlockManager)
-                context.getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
-        return manager.getOemUnlockEnabled();
-    }
-
-    /**
-     * Allows enabling or disabling OEM unlock on this device. OEM unlocked
-     * devices allow users to flash other OSes to them.
-     */
-    static void setOemUnlockEnabled(Context context, boolean enabled) {
-        try {
-            PersistentDataBlockManager manager = (PersistentDataBlockManager)
-                    context.getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
-            manager.setOemUnlockEnabled(enabled);
-        } catch (SecurityException e) {
-            Log.e(TAG, "Fail to set oem unlock.", e);
-        }
-    }
-
-    /**
-     * Returns {@code true} if OEM unlock is disallowed by user restriction
-     * {@link UserManager#DISALLOW_FACTORY_RESET} or {@link UserManager#DISALLOW_OEM_UNLOCK}.
-     * Otherwise, returns {@code false}.
-     */
-    static boolean isOemUnlockAllowed(UserManager um) {
-        final UserHandle userHandle = UserHandle.of(UserHandle.myUserId());
-        return !(um.hasBaseUserRestriction(UserManager.DISALLOW_OEM_UNLOCK, userHandle)
-                || um.hasBaseUserRestriction(UserManager.DISALLOW_FACTORY_RESET, userHandle));
-    }
-}
diff --git a/tests/robotests/src/android/service/oemlock/OemLockManager.java b/tests/robotests/src/android/service/oemlock/OemLockManager.java
new file mode 100644
index 0000000..7c015cf
--- /dev/null
+++ b/tests/robotests/src/android/service/oemlock/OemLockManager.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+package android.service.oemlock;
+
+/**
+ * Make OemLockManager available to Robolectric.
+ */
+public class OemLockManager {
+    public void setOemUnlockAllowedByCarrier(boolean allowed, byte[] signature) {}
+
+    public boolean isOemUnlockAllowedByCarrier() {
+        return true;
+    }
+
+    public void setOemUnlockAllowedByUser(boolean allowed) {}
+
+    public boolean isOemUnlockAllowedByUser() {
+        return false;
+    }
+
+    public boolean canUserAllowOemUnlock() {
+        return true;
+    }
+    public boolean isOemUnlockAllowed() {
+        return false;
+    }
+
+    public boolean isDeviceOemUnlocked() {
+        return false;
+    }
+}