blob: b191f888aa1a2037760857b4604b0e541eace374 [file] [log] [blame]
Tony Mantler4ca9ebd2017-07-31 10:54:20 -07001/*
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
17package com.android.settingslib.development;
18
19import android.content.Context;
20import android.content.Intent;
21import android.os.Build;
Fan Zhang6da958a2018-02-07 12:45:32 -080022import android.os.UserManager;
Tony Mantler4ca9ebd2017-07-31 10:54:20 -070023import android.provider.Settings;
Fan Zhangf7802ea2018-08-28 15:15:19 -070024
Aurimas Liutikasfd52c142018-04-17 09:50:46 -070025import androidx.localbroadcastmanager.content.LocalBroadcastManager;
Tony Mantler4ca9ebd2017-07-31 10:54:20 -070026
27public class DevelopmentSettingsEnabler {
28
29 public static final String DEVELOPMENT_SETTINGS_CHANGED_ACTION =
30 "com.android.settingslib.development.DevelopmentSettingsEnabler.SETTINGS_CHANGED";
31
Fan Zhang6da958a2018-02-07 12:45:32 -080032 private DevelopmentSettingsEnabler() {
33 }
Tony Mantler4ca9ebd2017-07-31 10:54:20 -070034
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 Zhang6da958a2018-02-07 12:45:32 -080043 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
44 final boolean settingEnabled = Settings.Global.getInt(context.getContentResolver(),
Tony Mantler4ca9ebd2017-07-31 10:54:20 -070045 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED,
46 Build.TYPE.equals("eng") ? 1 : 0) != 0;
Fan Zhang6da958a2018-02-07 12:45:32 -080047 final boolean hasRestriction = um.hasUserRestriction(
48 UserManager.DISALLOW_DEBUGGING_FEATURES);
Christine Franks67e43ea2018-04-23 11:18:43 -070049 final boolean isAdmin = um.isAdminUser();
50 return isAdmin && !hasRestriction && settingEnabled;
Tony Mantler4ca9ebd2017-07-31 10:54:20 -070051 }
52}