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