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.wifi; 18 19 import static com.android.settings.wifi.ConfigureWifiSettings.WIFI_WAKEUP_REQUEST_CODE; 20 21 import android.app.Fragment; 22 import android.app.Service; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.location.LocationManager; 26 import android.provider.Settings; 27 import android.support.annotation.VisibleForTesting; 28 import android.support.v14.preference.SwitchPreference; 29 import android.support.v7.preference.Preference; 30 import android.support.v7.preference.PreferenceScreen; 31 import android.text.TextUtils; 32 33 import com.android.settings.R; 34 import com.android.settings.core.PreferenceControllerMixin; 35 import com.android.settings.dashboard.DashboardFragment; 36 import com.android.settings.utils.AnnotationSpan; 37 import com.android.settingslib.core.AbstractPreferenceController; 38 39 /** 40 * {@link PreferenceControllerMixin} that controls whether the Wi-Fi Wakeup feature should be 41 * enabled. 42 */ 43 public class WifiWakeupPreferenceController extends AbstractPreferenceController { 44 45 private static final String TAG = "WifiWakeupPrefController"; 46 private static final String KEY_ENABLE_WIFI_WAKEUP = "enable_wifi_wakeup"; 47 48 private final Fragment mFragment; 49 50 @VisibleForTesting 51 SwitchPreference mPreference; 52 @VisibleForTesting 53 LocationManager mLocationManager; 54 WifiWakeupPreferenceController(Context context, DashboardFragment fragment)55 public WifiWakeupPreferenceController(Context context, DashboardFragment fragment) { 56 super(context); 57 mFragment = fragment; 58 mLocationManager = (LocationManager) context.getSystemService(Service.LOCATION_SERVICE); 59 } 60 61 @Override displayPreference(PreferenceScreen screen)62 public void displayPreference(PreferenceScreen screen) { 63 super.displayPreference(screen); 64 mPreference = (SwitchPreference) screen.findPreference(KEY_ENABLE_WIFI_WAKEUP); 65 updateState(mPreference); 66 } 67 68 @Override isAvailable()69 public boolean isAvailable() { 70 return true; 71 } 72 73 @Override handlePreferenceTreeClick(Preference preference)74 public boolean handlePreferenceTreeClick(Preference preference) { 75 if (!TextUtils.equals(preference.getKey(), KEY_ENABLE_WIFI_WAKEUP)) { 76 return false; 77 } 78 if (!(preference instanceof SwitchPreference)) { 79 return false; 80 } 81 82 if (!mLocationManager.isLocationEnabled()) { 83 final Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 84 mFragment.startActivity(intent); 85 } else if (getWifiWakeupEnabled()) { 86 setWifiWakeupEnabled(false); 87 } else if (!getWifiScanningEnabled()) { 88 showScanningDialog(); 89 } else { 90 setWifiWakeupEnabled(true); 91 } 92 93 updateState(mPreference); 94 return true; 95 } 96 97 @Override getPreferenceKey()98 public String getPreferenceKey() { 99 return KEY_ENABLE_WIFI_WAKEUP; 100 } 101 102 @Override updateState(Preference preference)103 public void updateState(Preference preference) { 104 if (!(preference instanceof SwitchPreference)) { 105 return; 106 } 107 final SwitchPreference enableWifiWakeup = (SwitchPreference) preference; 108 109 enableWifiWakeup.setChecked(getWifiWakeupEnabled() 110 && getWifiScanningEnabled() 111 && mLocationManager.isLocationEnabled()); 112 if (!mLocationManager.isLocationEnabled()) { 113 preference.setSummary(getNoLocationSummary()); 114 } else { 115 preference.setSummary(R.string.wifi_wakeup_summary); 116 } 117 } 118 getNoLocationSummary()119 @VisibleForTesting CharSequence getNoLocationSummary() { 120 AnnotationSpan.LinkInfo linkInfo = new AnnotationSpan.LinkInfo("link", null); 121 CharSequence locationText = mContext.getText(R.string.wifi_wakeup_summary_no_location); 122 return AnnotationSpan.linkify(locationText, linkInfo); 123 } 124 onActivityResult(int requestCode, int resultCode)125 public void onActivityResult(int requestCode, int resultCode) { 126 if (requestCode != WIFI_WAKEUP_REQUEST_CODE) { 127 return; 128 } 129 if (mLocationManager.isLocationEnabled()) { 130 setWifiWakeupEnabled(true); 131 } 132 updateState(mPreference); 133 } 134 getWifiScanningEnabled()135 private boolean getWifiScanningEnabled() { 136 return Settings.Global.getInt(mContext.getContentResolver(), 137 Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0) == 1; 138 } 139 showScanningDialog()140 private void showScanningDialog() { 141 final WifiScanningRequiredFragment dialogFragment = 142 WifiScanningRequiredFragment.newInstance(); 143 dialogFragment.setTargetFragment(mFragment, WIFI_WAKEUP_REQUEST_CODE /* requestCode */); 144 dialogFragment.show(mFragment.getFragmentManager(), TAG); 145 } 146 getWifiWakeupEnabled()147 private boolean getWifiWakeupEnabled() { 148 return Settings.Global.getInt(mContext.getContentResolver(), 149 Settings.Global.WIFI_WAKEUP_ENABLED, 0) == 1; 150 } 151 setWifiWakeupEnabled(boolean enabled)152 private void setWifiWakeupEnabled(boolean enabled) { 153 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.WIFI_WAKEUP_ENABLED, 154 enabled ? 1 : 0); 155 } 156 } 157