1 /* 2 * Copyright (C) 2019 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.car.settings.wifi.preferences; 18 19 import android.app.Service; 20 import android.car.drivingstate.CarUxRestrictions; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.location.LocationManager; 24 import android.provider.Settings; 25 import android.text.SpannableString; 26 import android.text.Spanned; 27 import android.text.style.URLSpan; 28 import android.widget.Toast; 29 30 import androidx.preference.TwoStatePreference; 31 32 import com.android.car.settings.R; 33 import com.android.car.settings.common.FragmentController; 34 import com.android.car.settings.common.Logger; 35 import com.android.car.settings.common.PreferenceController; 36 37 /** Business logic to allow auto-enabling of wifi near saved networks. */ 38 public class WifiWakeupTogglePreferenceController extends 39 PreferenceController<TwoStatePreference> implements 40 ConfirmEnableWifiScanningDialogFragment.WifiScanningEnabledListener { 41 42 private static final Logger LOG = new Logger(WifiWakeupTogglePreferenceController.class); 43 private final LocationManager mLocationManager; 44 WifiWakeupTogglePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)45 public WifiWakeupTogglePreferenceController(Context context, String preferenceKey, 46 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 47 super(context, preferenceKey, fragmentController, uxRestrictions); 48 mLocationManager = (LocationManager) context.getSystemService(Service.LOCATION_SERVICE); 49 } 50 51 @Override getPreferenceType()52 protected Class<TwoStatePreference> getPreferenceType() { 53 return TwoStatePreference.class; 54 } 55 56 @Override onCreateInternal()57 protected void onCreateInternal() { 58 ConfirmEnableWifiScanningDialogFragment dialogFragment = 59 (ConfirmEnableWifiScanningDialogFragment) getFragmentController().findDialogByTag( 60 ConfirmEnableWifiScanningDialogFragment.TAG); 61 if (dialogFragment != null) { 62 dialogFragment.setWifiScanningEnabledListener(this); 63 } 64 } 65 66 @Override updateState(TwoStatePreference preference)67 protected void updateState(TwoStatePreference preference) { 68 preference.setChecked(getWifiWakeupEnabled() 69 && getWifiScanningEnabled() 70 && mLocationManager.isLocationEnabled()); 71 if (!mLocationManager.isLocationEnabled()) { 72 preference.setSummary(getNoLocationSummary()); 73 } else { 74 preference.setSummary(R.string.wifi_wakeup_summary); 75 } 76 } 77 78 @Override handlePreferenceClicked(TwoStatePreference preference)79 protected boolean handlePreferenceClicked(TwoStatePreference preference) { 80 if (!mLocationManager.isLocationEnabled()) { 81 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 82 getContext().startActivity(intent); 83 } else if (getWifiWakeupEnabled()) { 84 setWifiWakeupEnabled(false); 85 } else if (!getWifiScanningEnabled()) { 86 showScanningDialog(); 87 } else { 88 setWifiWakeupEnabled(true); 89 } 90 91 refreshUi(); 92 return true; 93 } 94 getWifiWakeupEnabled()95 private boolean getWifiWakeupEnabled() { 96 return Settings.Global.getInt(getContext().getContentResolver(), 97 Settings.Global.WIFI_WAKEUP_ENABLED, 0) == 1; 98 } 99 setWifiWakeupEnabled(boolean enabled)100 private void setWifiWakeupEnabled(boolean enabled) { 101 Settings.Global.putInt(getContext().getContentResolver(), 102 Settings.Global.WIFI_WAKEUP_ENABLED, 103 enabled ? 1 : 0); 104 } 105 getWifiScanningEnabled()106 private boolean getWifiScanningEnabled() { 107 return Settings.Global.getInt(getContext().getContentResolver(), 108 Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0) == 1; 109 } 110 enableWifiScanning()111 private void enableWifiScanning() { 112 Settings.Global.putInt(getContext().getContentResolver(), 113 Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 1); 114 Toast.makeText( 115 getContext(), 116 getContext().getString(R.string.wifi_settings_scanning_required_enabled), 117 Toast.LENGTH_SHORT).show(); 118 } 119 getNoLocationSummary()120 private CharSequence getNoLocationSummary() { 121 String highlightedWord = "link"; 122 CharSequence locationText = getContext() 123 .getText(R.string.wifi_wakeup_summary_no_location); 124 125 SpannableString msg = new SpannableString(locationText); 126 int startIndex = locationText.toString().indexOf(highlightedWord); 127 if (startIndex < 0) { 128 LOG.e("Cannot create URL span"); 129 return locationText; 130 } 131 msg.setSpan(new URLSpan((String) null), startIndex, startIndex + highlightedWord.length(), 132 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 133 return msg; 134 } 135 showScanningDialog()136 private void showScanningDialog() { 137 ConfirmEnableWifiScanningDialogFragment dialogFragment = 138 new ConfirmEnableWifiScanningDialogFragment(); 139 dialogFragment.setWifiScanningEnabledListener(this); 140 getFragmentController().showDialog(dialogFragment, 141 ConfirmEnableWifiScanningDialogFragment.TAG); 142 } 143 144 @Override onWifiScanningEnabled()145 public void onWifiScanningEnabled() { 146 enableWifiScanning(); 147 if (mLocationManager.isLocationEnabled()) { 148 setWifiWakeupEnabled(true); 149 } 150 refreshUi(); 151 } 152 } 153