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