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.content.SharedPreferences; 21 import android.provider.Settings; 22 23 import com.android.settings.core.lifecycle.Lifecycle; 24 import com.android.settings.core.lifecycle.LifecycleObserver; 25 import com.android.settings.core.lifecycle.events.OnResume; 26 27 public class DevelopmentSettingsEnabler implements LifecycleObserver, OnResume { 28 29 private final Context mContext; 30 private final SharedPreferences mDevelopmentPreferences; 31 private boolean mLastEnabledState; 32 DevelopmentSettingsEnabler(Context context, Lifecycle lifecycle)33 public DevelopmentSettingsEnabler(Context context, Lifecycle lifecycle) { 34 mContext = context; 35 mDevelopmentPreferences = context.getSharedPreferences(DevelopmentSettings.PREF_FILE, 36 Context.MODE_PRIVATE); 37 if (lifecycle != null) { 38 lifecycle.addObserver(this); 39 } 40 } 41 42 @Override onResume()43 public void onResume() { 44 mLastEnabledState = Settings.Global.getInt(mContext.getContentResolver(), 45 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0; 46 } 47 enableDevelopmentSettings(Context context, SharedPreferences prefs)48 public static boolean enableDevelopmentSettings(Context context, SharedPreferences prefs) { 49 prefs.edit() 50 .putBoolean(DevelopmentSettings.PREF_SHOW, true) 51 .commit(); 52 return Settings.Global.putInt(context.getContentResolver(), 53 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1); 54 } 55 getLastEnabledState()56 public boolean getLastEnabledState() { 57 return mLastEnabledState; 58 } 59 enableDevelopmentSettings()60 public void enableDevelopmentSettings() { 61 mLastEnabledState = enableDevelopmentSettings(mContext, mDevelopmentPreferences); 62 } 63 disableDevelopmentSettings()64 public void disableDevelopmentSettings() { 65 mDevelopmentPreferences.edit() 66 .putBoolean(DevelopmentSettings.PREF_SHOW, false) 67 .commit(); 68 Settings.Global.putInt(mContext.getContentResolver(), 69 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0); 70 mLastEnabledState = false; 71 } 72 } 73