1 /* 2 * Copyright (C) 2022 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.helper; 18 19 import android.annotation.TestApi; 20 import android.content.Context; 21 import android.net.ConnectivityManager; 22 import android.net.wifi.WifiManager; 23 import android.os.Handler; 24 import android.os.Looper; 25 import android.util.Log; 26 27 import androidx.annotation.GuardedBy; 28 import androidx.annotation.NonNull; 29 import androidx.annotation.VisibleForTesting; 30 import androidx.lifecycle.Lifecycle; 31 32 import com.android.wifitrackerlib.SavedNetworkTracker; 33 34 import java.util.List; 35 import java.util.Map; 36 import java.util.concurrent.ConcurrentHashMap; 37 38 /** 39 * Helper for saved Wi-Fi networks tracker from WifiTrackerLib. 40 */ 41 public class SavedWifiHelper extends WifiTrackerBase { 42 private static final String TAG = "SavedWifiHelper"; 43 44 private static final Object sInstanceLock = new Object(); 45 @TestApi 46 @GuardedBy("sInstanceLock") 47 private static Map<Context, SavedWifiHelper> sTestInstances; 48 49 protected SavedNetworkTracker mSavedNetworkTracker; 50 51 /** 52 * Static method to create a SavedWifiHelper class. 53 * 54 * @param context The Context this is associated with. 55 * @param lifecycle The lifecycle this is associated with. 56 * @return an instance of {@link SavedWifiHelper} object. 57 */ getInstance(@onNull Context context, @NonNull Lifecycle lifecycle)58 public static SavedWifiHelper getInstance(@NonNull Context context, 59 @NonNull Lifecycle lifecycle) { 60 synchronized (sInstanceLock) { 61 if (sTestInstances != null && sTestInstances.containsKey(context)) { 62 SavedWifiHelper testInstance = sTestInstances.get(context); 63 Log.w(TAG, "The context owner use a test instance:" + testInstance); 64 return testInstance; 65 } 66 return new SavedWifiHelper(context, lifecycle); 67 } 68 } 69 70 /** 71 * A convenience method to set pre-prepared instance or mock(SavedWifiHelper.class) for 72 * testing. 73 * 74 * @param context The Context this is associated with. 75 * @param instance of {@link SavedWifiHelper} object. 76 * @hide 77 */ 78 @TestApi 79 @VisibleForTesting setTestInstance(@onNull Context context, SavedWifiHelper instance)80 public static void setTestInstance(@NonNull Context context, SavedWifiHelper instance) { 81 synchronized (sInstanceLock) { 82 if (sTestInstances == null) sTestInstances = new ConcurrentHashMap<>(); 83 Log.w(TAG, "Set a test instance by context:" + context); 84 sTestInstances.put(context, instance); 85 } 86 } 87 SavedWifiHelper(@onNull Context context, @NonNull Lifecycle lifecycle)88 public SavedWifiHelper(@NonNull Context context, @NonNull Lifecycle lifecycle) { 89 this(context, lifecycle, null); 90 } 91 92 @VisibleForTesting SavedWifiHelper(@onNull Context context, @NonNull Lifecycle lifecycle, SavedNetworkTracker saveNetworkTracker)93 protected SavedWifiHelper(@NonNull Context context, @NonNull Lifecycle lifecycle, 94 SavedNetworkTracker saveNetworkTracker) { 95 super(lifecycle); 96 mSavedNetworkTracker = (saveNetworkTracker != null) ? saveNetworkTracker 97 : createSavedNetworkTracker(context, lifecycle); 98 } 99 100 @VisibleForTesting createSavedNetworkTracker(@onNull Context context, @NonNull Lifecycle lifecycle)101 protected SavedNetworkTracker createSavedNetworkTracker(@NonNull Context context, 102 @NonNull Lifecycle lifecycle) { 103 return new SavedNetworkTracker(lifecycle, context.getApplicationContext(), 104 context.getApplicationContext().getSystemService(WifiManager.class), 105 context.getApplicationContext().getSystemService(ConnectivityManager.class), 106 new Handler(Looper.getMainLooper()), 107 getWorkerThreadHandler(), 108 ELAPSED_REALTIME_CLOCK, 109 MAX_SCAN_AGE_MILLIS, 110 SCAN_INTERVAL_MILLIS, 111 null /* SavedNetworkTrackerCallback */); 112 } 113 114 @Override getTag()115 protected String getTag() { 116 return TAG; 117 } 118 getSavedNetworkTracker()119 public SavedNetworkTracker getSavedNetworkTracker() { 120 return mSavedNetworkTracker; 121 } 122 123 /** 124 * Returns true when the certificate is being used by a saved network or network suggestion. 125 */ isCertificateInUse(String certAlias)126 public boolean isCertificateInUse(String certAlias) { 127 return mSavedNetworkTracker.isCertificateRequired(certAlias); 128 } 129 130 /** 131 * Returns a list of network names which is using the certificate alias. 132 * 133 * @return a list of network names. 134 */ getCertificateNetworkNames(String certAlias)135 public List<String> getCertificateNetworkNames(String certAlias) { 136 return mSavedNetworkTracker.getCertificateRequesterNames(certAlias); 137 } 138 } 139