Tony Mantler | 4ca9ebd | 2017-07-31 10:54:20 -0700 | [diff] [blame] | 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.settingslib.development; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.Intent; |
| 21 | import android.os.Build; |
Fan Zhang | 6da958a | 2018-02-07 12:45:32 -0800 | [diff] [blame] | 22 | import android.os.UserManager; |
Tony Mantler | 4ca9ebd | 2017-07-31 10:54:20 -0700 | [diff] [blame] | 23 | import android.provider.Settings; |
Fan Zhang | f7802ea | 2018-08-28 15:15:19 -0700 | [diff] [blame] | 24 | |
Aurimas Liutikas | fd52c14 | 2018-04-17 09:50:46 -0700 | [diff] [blame] | 25 | import androidx.localbroadcastmanager.content.LocalBroadcastManager; |
Tony Mantler | 4ca9ebd | 2017-07-31 10:54:20 -0700 | [diff] [blame] | 26 | |
| 27 | public class DevelopmentSettingsEnabler { |
| 28 | |
| 29 | public static final String DEVELOPMENT_SETTINGS_CHANGED_ACTION = |
| 30 | "com.android.settingslib.development.DevelopmentSettingsEnabler.SETTINGS_CHANGED"; |
| 31 | |
Fan Zhang | 6da958a | 2018-02-07 12:45:32 -0800 | [diff] [blame] | 32 | private DevelopmentSettingsEnabler() { |
| 33 | } |
Tony Mantler | 4ca9ebd | 2017-07-31 10:54:20 -0700 | [diff] [blame] | 34 | |
| 35 | public static void setDevelopmentSettingsEnabled(Context context, boolean enable) { |
| 36 | Settings.Global.putInt(context.getContentResolver(), |
| 37 | Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, enable ? 1 : 0); |
| 38 | LocalBroadcastManager.getInstance(context) |
| 39 | .sendBroadcast(new Intent(DEVELOPMENT_SETTINGS_CHANGED_ACTION)); |
| 40 | } |
| 41 | |
| 42 | public static boolean isDevelopmentSettingsEnabled(Context context) { |
Fan Zhang | 6da958a | 2018-02-07 12:45:32 -0800 | [diff] [blame] | 43 | final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 44 | final boolean settingEnabled = Settings.Global.getInt(context.getContentResolver(), |
Tony Mantler | 4ca9ebd | 2017-07-31 10:54:20 -0700 | [diff] [blame] | 45 | Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, |
| 46 | Build.TYPE.equals("eng") ? 1 : 0) != 0; |
Fan Zhang | 6da958a | 2018-02-07 12:45:32 -0800 | [diff] [blame] | 47 | final boolean hasRestriction = um.hasUserRestriction( |
| 48 | UserManager.DISALLOW_DEBUGGING_FEATURES); |
Christine Franks | 67e43ea | 2018-04-23 11:18:43 -0700 | [diff] [blame] | 49 | final boolean isAdmin = um.isAdminUser(); |
| 50 | return isAdmin && !hasRestriction && settingEnabled; |
Tony Mantler | 4ca9ebd | 2017-07-31 10:54:20 -0700 | [diff] [blame] | 51 | } |
| 52 | } |