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