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.settings.wifi.savedaccesspoints; 18 19 20 import android.content.Context; 21 import android.net.wifi.WifiManager; 22 import android.util.Log; 23 24 import androidx.annotation.VisibleForTesting; 25 import androidx.preference.Preference; 26 import androidx.preference.PreferenceGroup; 27 import androidx.preference.PreferenceScreen; 28 29 import com.android.settings.core.BasePreferenceController; 30 import com.android.settings.utils.PreferenceGroupChildrenCache; 31 import com.android.settingslib.core.lifecycle.LifecycleObserver; 32 import com.android.settingslib.core.lifecycle.events.OnStart; 33 import com.android.settingslib.utils.ThreadUtils; 34 import com.android.settingslib.wifi.AccessPoint; 35 import com.android.settingslib.wifi.AccessPointPreference; 36 import com.android.settingslib.wifi.AccessPointPreference.UserBadgeCache; 37 import com.android.settingslib.wifi.WifiSavedConfigUtils; 38 39 import java.util.Collections; 40 import java.util.List; 41 42 /** 43 * Controller that manages a PreferenceGroup, which contains a list of subscribed access points. 44 */ 45 // TODO(b/127206629): Code refactor to avoid duplicated coding after removed feature flag. 46 public class SubscribedAccessPointsPreferenceController extends BasePreferenceController implements 47 LifecycleObserver, OnStart, Preference.OnPreferenceClickListener, 48 WifiManager.ActionListener { 49 50 private static final String TAG = "SubscribedAPPrefCtrl"; 51 52 private final WifiManager mWifiManager; 53 private final PreferenceGroupChildrenCache mChildrenCache; 54 private final UserBadgeCache mUserBadgeCache; 55 private PreferenceGroup mPreferenceGroup; 56 private SavedAccessPointsWifiSettings mHost; 57 SubscribedAccessPointsPreferenceController(Context context, String preferenceKey)58 public SubscribedAccessPointsPreferenceController(Context context, 59 String preferenceKey) { 60 super(context, preferenceKey); 61 mUserBadgeCache = new AccessPointPreference.UserBadgeCache(context.getPackageManager()); 62 mWifiManager = context.getSystemService(WifiManager.class); 63 mChildrenCache = new PreferenceGroupChildrenCache(); 64 } 65 setHost(SavedAccessPointsWifiSettings host)66 public SubscribedAccessPointsPreferenceController setHost(SavedAccessPointsWifiSettings host) { 67 mHost = host; 68 return this; 69 } 70 71 @Override getAvailabilityStatus()72 public int getAvailabilityStatus() { 73 return AVAILABLE; 74 } 75 76 @Override displayPreference(PreferenceScreen screen)77 public void displayPreference(PreferenceScreen screen) { 78 super.displayPreference(screen); 79 mPreferenceGroup = screen.findPreference(getPreferenceKey()); 80 } 81 82 @Override onStart()83 public void onStart() { 84 refreshSubscribedAccessPoints(); 85 } 86 postRefreshSubscribedAccessPoints()87 public void postRefreshSubscribedAccessPoints() { 88 ThreadUtils.postOnMainThread(() -> refreshSubscribedAccessPoints()); 89 } 90 91 @Override onPreferenceClick(Preference preference)92 public boolean onPreferenceClick(Preference preference) { 93 if (mHost != null) { 94 mHost.showWifiPage((AccessPointPreference) preference); 95 } 96 return false; 97 } 98 99 @Override onSuccess()100 public void onSuccess() { 101 postRefreshSubscribedAccessPoints(); 102 } 103 104 @Override onFailure(int reason)105 public void onFailure(int reason) { 106 postRefreshSubscribedAccessPoints(); 107 } 108 109 @VisibleForTesting refreshSubscribedAccessPoints()110 void refreshSubscribedAccessPoints() { 111 if (mPreferenceGroup == null) { 112 Log.w(TAG, "PreferenceGroup is null, skipping."); 113 return; 114 } 115 116 if (mHost != null && !mHost.isSubscriptionsFeatureEnabled()) { 117 mPreferenceGroup.setVisible(false); 118 return; 119 } 120 121 final Context prefContext = mPreferenceGroup.getContext(); 122 123 final List<AccessPoint> accessPoints = 124 WifiSavedConfigUtils.getAllConfigs(mContext, mWifiManager); 125 Collections.sort(accessPoints, SavedNetworkComparator.INSTANCE); 126 mChildrenCache.cacheRemoveAllPrefs(mPreferenceGroup); 127 128 final int accessPointsSize = accessPoints.size(); 129 for (int i = 0; i < accessPointsSize; ++i) { 130 AccessPoint ap = accessPoints.get(i); 131 if (!ap.isPasspointConfig()) { 132 continue; 133 } 134 135 final String key = ap.getKey(); 136 AccessPointPreference preference = 137 (AccessPointPreference) mChildrenCache.getCachedPreference(key); 138 if (preference == null) { 139 preference = new AccessPointPreference(ap, prefContext, mUserBadgeCache, true); 140 preference.setKey(key); 141 preference.setIcon(null); 142 preference.setOnPreferenceClickListener(this); 143 mPreferenceGroup.addPreference(preference); 144 } 145 preference.setOrder(i); 146 } 147 148 mChildrenCache.removeCachedPrefs(mPreferenceGroup); 149 150 if (mPreferenceGroup.getPreferenceCount() < 1) { 151 Log.w(TAG, "Subscribed networks activity loaded," 152 + " but there are no subscribed networks!"); 153 mPreferenceGroup.setVisible(false); 154 } else { 155 mPreferenceGroup.setVisible(true); 156 } 157 } 158 } 159