1 /* 2 * Copyright (C) 2017 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.android.settings.development; 18 19 import android.content.Context; 20 import android.os.UserHandle; 21 import android.os.UserManager; 22 import android.service.persistentdata.PersistentDataBlockManager; 23 import android.util.Log; 24 25 public class OemUnlockUtils { 26 private static final String TAG = "OemUnlockUtils"; 27 28 /** 29 * Returns whether or not this device is able to be OEM unlocked. 30 */ isOemUnlockEnabled(Context context)31 static boolean isOemUnlockEnabled(Context context) { 32 PersistentDataBlockManager manager = (PersistentDataBlockManager) 33 context.getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE); 34 return manager.getOemUnlockEnabled(); 35 } 36 37 /** 38 * Allows enabling or disabling OEM unlock on this device. OEM unlocked 39 * devices allow users to flash other OSes to them. 40 */ setOemUnlockEnabled(Context context, boolean enabled)41 static void setOemUnlockEnabled(Context context, boolean enabled) { 42 try { 43 PersistentDataBlockManager manager = (PersistentDataBlockManager) 44 context.getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE); 45 manager.setOemUnlockEnabled(enabled); 46 } catch (SecurityException e) { 47 Log.e(TAG, "Fail to set oem unlock.", e); 48 } 49 } 50 51 /** 52 * Returns {@code true} if OEM unlock is disallowed by user restriction 53 * {@link UserManager#DISALLOW_FACTORY_RESET} or {@link UserManager#DISALLOW_OEM_UNLOCK}. 54 * Otherwise, returns {@code false}. 55 */ isOemUnlockAllowed(UserManager um)56 static boolean isOemUnlockAllowed(UserManager um) { 57 final UserHandle userHandle = UserHandle.of(UserHandle.myUserId()); 58 return !(um.hasBaseUserRestriction(UserManager.DISALLOW_OEM_UNLOCK, userHandle) 59 || um.hasBaseUserRestriction(UserManager.DISALLOW_FACTORY_RESET, userHandle)); 60 } 61 } 62