1 /*
2  * Copyright (C) 2018 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 
18 package com.android.settings.testutils;
19 
20 import android.content.Context;
21 import android.content.IntentFilter;
22 import android.net.Uri;
23 import android.net.wifi.WifiManager;
24 import android.provider.Settings;
25 
26 import com.android.settings.core.TogglePreferenceController;
27 import com.android.settings.slices.SliceBackgroundWorker;
28 
29 import java.io.IOException;
30 
31 public class FakeToggleController extends TogglePreferenceController {
32 
33     public static final String AVAILABILITY_KEY = "fake_toggle_availability_key";
34 
35     public static final IntentFilter INTENT_FILTER = new IntentFilter(
36             WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
37 
38     private static final String SETTING_KEY = "toggle_key";
39 
40     private static final int ON = 1;
41     private static final int OFF = 0;
42 
43     private boolean mIsAsyncUpdate = false;
44 
FakeToggleController(Context context, String preferenceKey)45     public FakeToggleController(Context context, String preferenceKey) {
46         super(context, preferenceKey);
47     }
48 
49     @Override
isChecked()50     public boolean isChecked() {
51         return Settings.System.getInt(mContext.getContentResolver(),
52                 SETTING_KEY, OFF) == ON;
53     }
54 
55     @Override
setChecked(boolean isChecked)56     public boolean setChecked(boolean isChecked) {
57         return Settings.System.putInt(mContext.getContentResolver(), SETTING_KEY,
58                 isChecked ? ON : OFF);
59     }
60 
61     @Override
getAvailabilityStatus()62     public int getAvailabilityStatus() {
63         return Settings.Global.getInt(mContext.getContentResolver(),
64                 AVAILABILITY_KEY, AVAILABLE);
65     }
66 
67     @Override
getIntentFilter()68     public IntentFilter getIntentFilter() {
69         return INTENT_FILTER;
70     }
71 
72     @Override
isSliceable()73     public boolean isSliceable() {
74         return true;
75     }
76 
77     @Override
getBackgroundWorkerClass()78     public Class<? extends SliceBackgroundWorker> getBackgroundWorkerClass() {
79         return TestWorker.class;
80     }
81 
82     @Override
hasAsyncUpdate()83     public boolean hasAsyncUpdate() {
84         return mIsAsyncUpdate;
85     }
86 
setAsyncUpdate(boolean isAsyncUpdate)87     public void setAsyncUpdate(boolean isAsyncUpdate) {
88         mIsAsyncUpdate = isAsyncUpdate;
89     }
90 
91     public static class TestWorker extends SliceBackgroundWorker<Void> {
92 
TestWorker(Context context, Uri uri)93         public TestWorker(Context context, Uri uri) {
94             super(context, uri);
95         }
96 
97         @Override
onSlicePinned()98         protected void onSlicePinned() {
99         }
100 
101         @Override
onSliceUnpinned()102         protected void onSliceUnpinned() {
103         }
104 
105         @Override
close()106         public void close() {
107         }
108     }
109 }
110