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.network; 17 18 import static android.provider.Settings.Secure.ADAPTIVE_CONNECTIVITY_MOBILE_NETWORK_ENABLED; 19 import static android.provider.Settings.Secure.ADAPTIVE_CONNECTIVITY_WIFI_ENABLED; 20 21 import android.app.settings.SettingsEnums; 22 import android.content.Context; 23 import android.net.wifi.WifiManager; 24 import android.os.Bundle; 25 import android.provider.Settings; 26 import android.util.Log; 27 import androidx.annotation.NonNull; 28 import androidx.annotation.Nullable; 29 import androidx.preference.SwitchPreferenceCompat; 30 import com.android.settings.R; 31 import com.android.settings.dashboard.DashboardFragment; 32 import com.android.settings.flags.Flags; 33 import com.android.settings.search.BaseSearchIndexProvider; 34 import com.android.settingslib.search.SearchIndexable; 35 36 /** Adaptive connectivity is a feature which automatically manages network connections. */ 37 @SearchIndexable 38 public class AdaptiveConnectivitySettings extends DashboardFragment { 39 private static final String TAG = "AdaptiveConnectivitySettings"; 40 41 @Override getMetricsCategory()42 public int getMetricsCategory() { 43 return SettingsEnums.ADAPTIVE_CONNECTIVITY_CATEGORY; 44 } 45 46 @Override getLogTag()47 protected String getLogTag() { 48 return TAG; 49 } 50 51 @Override getPreferenceScreenResId()52 protected int getPreferenceScreenResId() { 53 return R.xml.adaptive_connectivity_settings; 54 } 55 56 public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 57 new BaseSearchIndexProvider(R.xml.adaptive_connectivity_settings); 58 59 @Override getPreferenceScreenBindingKey(@onNull Context context)60 public @Nullable String getPreferenceScreenBindingKey(@NonNull Context context) { 61 return AdaptiveConnectivityScreen.KEY; 62 } 63 64 @Override onCreatePreferences(@onNull Bundle savedInstanceState, @NonNull String rootKey)65 public void onCreatePreferences(@NonNull Bundle savedInstanceState, @NonNull String rootKey) { 66 Log.i("Settings", "onCreatePreferences"); 67 super.onCreatePreferences(savedInstanceState, rootKey); 68 if (Flags.enableNestedToggleSwitches() && !isCatalystEnabled()) { 69 setupSwitchPreferenceCompat(ADAPTIVE_CONNECTIVITY_WIFI_ENABLED); 70 setupSwitchPreferenceCompat(ADAPTIVE_CONNECTIVITY_MOBILE_NETWORK_ENABLED); 71 } 72 } 73 setupSwitchPreferenceCompat(String key)74 private void setupSwitchPreferenceCompat(String key) { 75 SwitchPreferenceCompat switchPreference = findPreference(key); 76 if (switchPreference != null) { 77 switchPreference.setOnPreferenceChangeListener( 78 (preference, newValue) -> { 79 boolean isChecked = (Boolean) newValue; 80 Settings.Secure.putInt(getContentResolver(), key, isChecked ? 1 : 0); 81 if (preference.getKey().equals(ADAPTIVE_CONNECTIVITY_WIFI_ENABLED)) { 82 getSystemService(WifiManager.class).setWifiScoringEnabled(isChecked); 83 } 84 return true; 85 }); 86 switchPreference.setVisible(true); 87 } 88 } 89 } 90