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 androidx.annotation.NonNull;
20 
21 import com.android.settings.Utils;
22 import com.android.settings.widget.SwitchBar;
23 import com.android.settingslib.core.lifecycle.Lifecycle;
24 import com.android.settingslib.core.lifecycle.LifecycleObserver;
25 import com.android.settingslib.core.lifecycle.events.OnStart;
26 import com.android.settingslib.core.lifecycle.events.OnStop;
27 import com.android.settingslib.development.DevelopmentSettingsEnabler;
28 
29 public class DevelopmentSwitchBarController implements LifecycleObserver, OnStart, OnStop {
30 
31     private final SwitchBar mSwitchBar;
32     private final boolean mIsAvailable;
33     private final DevelopmentSettingsDashboardFragment mSettings;
34 
DevelopmentSwitchBarController(@onNull DevelopmentSettingsDashboardFragment settings, SwitchBar switchBar, boolean isAvailable, Lifecycle lifecycle)35     public DevelopmentSwitchBarController(@NonNull DevelopmentSettingsDashboardFragment settings,
36             SwitchBar switchBar, boolean isAvailable, Lifecycle lifecycle) {
37         mSwitchBar = switchBar;
38         mIsAvailable = isAvailable && !Utils.isMonkeyRunning();
39         mSettings = settings;
40 
41         if (mIsAvailable) {
42             lifecycle.addObserver(this);
43         } else {
44             mSwitchBar.setEnabled(false);
45         }
46     }
47 
48     @Override
onStart()49     public void onStart() {
50         final boolean developmentEnabledState = DevelopmentSettingsEnabler
51                 .isDevelopmentSettingsEnabled(mSettings.getContext());
52         mSwitchBar.setChecked(developmentEnabledState);
53         mSwitchBar.addOnSwitchChangeListener(mSettings);
54     }
55 
56     @Override
onStop()57     public void onStop() {
58         mSwitchBar.removeOnSwitchChangeListener(mSettings);
59     }
60 }
61