1 /* 2 * Copyright (C) 2023 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.factory; 18 19 import android.content.Context; 20 import android.net.TetheringManager; 21 import android.net.wifi.WifiManager; 22 import android.util.Log; 23 24 import androidx.annotation.NonNull; 25 import androidx.annotation.Nullable; 26 import androidx.lifecycle.ViewModelProvider; 27 import androidx.lifecycle.ViewModelStoreOwner; 28 29 import com.android.settings.wifi.details.WifiNetworkDetailsViewModel; 30 import com.android.settings.wifi.repository.SharedConnectivityRepository; 31 import com.android.settings.wifi.repository.WifiHotspotRepository; 32 import com.android.settings.wifi.tether.WifiHotspotSecurityViewModel; 33 import com.android.settings.wifi.tether.WifiHotspotSpeedViewModel; 34 import com.android.settings.wifi.tether.WifiTetherViewModel; 35 36 import org.jetbrains.annotations.NotNull; 37 38 /** 39 * Wi-Fi Feature Provider 40 */ 41 public class WifiFeatureProvider { 42 private static final String TAG = "WifiFeatureProvider"; 43 44 private final Context mAppContext; 45 private WifiManager mWifiManager; 46 private TetheringManager mTetheringManager; 47 private WifiVerboseLogging mWifiVerboseLogging; 48 private WifiHotspotRepository mWifiHotspotRepository; 49 private SharedConnectivityRepository mSharedConnectivityRepository; 50 WifiFeatureProvider(@onNull Context appContext)51 public WifiFeatureProvider(@NonNull Context appContext) { 52 mAppContext = appContext; 53 } 54 55 /** 56 * Gets WifiManager 57 */ getWifiManager()58 public WifiManager getWifiManager() { 59 if (mWifiManager == null) { 60 mWifiManager = mAppContext.getSystemService(WifiManager.class); 61 } 62 return mWifiManager; 63 } 64 65 /** 66 * Gets TetheringManager 67 */ getTetheringManager()68 public TetheringManager getTetheringManager() { 69 if (mTetheringManager == null) { 70 mTetheringManager = mAppContext.getSystemService(TetheringManager.class); 71 verboseLog(TAG, "getTetheringManager():" + mTetheringManager); 72 } 73 return mTetheringManager; 74 } 75 76 /** 77 * Gets WifiVerboseLogging 78 */ getWifiVerboseLogging()79 public WifiVerboseLogging getWifiVerboseLogging() { 80 if (mWifiVerboseLogging == null) { 81 mWifiVerboseLogging = new WifiVerboseLogging(mAppContext, getWifiManager()); 82 } 83 return mWifiVerboseLogging; 84 } 85 86 /** 87 * Gets WifiHotspotRepository 88 */ getWifiHotspotRepository()89 public WifiHotspotRepository getWifiHotspotRepository() { 90 if (mWifiHotspotRepository == null) { 91 mWifiHotspotRepository = new WifiHotspotRepository(mAppContext, getWifiManager(), 92 getTetheringManager()); 93 verboseLog(TAG, "getWifiHotspotRepository():" + mWifiHotspotRepository); 94 } 95 return mWifiHotspotRepository; 96 } 97 98 /** 99 * Gets SharedConnectivityRepository 100 */ getSharedConnectivityRepository()101 public SharedConnectivityRepository getSharedConnectivityRepository() { 102 if (mSharedConnectivityRepository == null) { 103 mSharedConnectivityRepository = new SharedConnectivityRepository(mAppContext); 104 verboseLog(TAG, "getSharedConnectivityRepository():" + mSharedConnectivityRepository); 105 } 106 return mSharedConnectivityRepository; 107 } 108 109 /** 110 * Gets WifiTetherViewModel 111 */ getWifiTetherViewModel(@otNull ViewModelStoreOwner owner)112 public WifiTetherViewModel getWifiTetherViewModel(@NotNull ViewModelStoreOwner owner) { 113 return new ViewModelProvider(owner).get(WifiTetherViewModel.class); 114 } 115 116 /** 117 * Gets WifiHotspotSecurityViewModel 118 */ getWifiHotspotSecurityViewModel( @otNull ViewModelStoreOwner owner)119 public WifiHotspotSecurityViewModel getWifiHotspotSecurityViewModel( 120 @NotNull ViewModelStoreOwner owner) { 121 WifiHotspotSecurityViewModel viewModel = 122 new ViewModelProvider(owner).get(WifiHotspotSecurityViewModel.class); 123 verboseLog(TAG, "getWifiHotspotSecurityViewModel():" + viewModel); 124 return viewModel; 125 } 126 127 /** 128 * Gets WifiHotspotSpeedViewModel 129 */ getWifiHotspotSpeedViewModel( @otNull ViewModelStoreOwner owner)130 public WifiHotspotSpeedViewModel getWifiHotspotSpeedViewModel( 131 @NotNull ViewModelStoreOwner owner) { 132 WifiHotspotSpeedViewModel viewModel = 133 new ViewModelProvider(owner).get(WifiHotspotSpeedViewModel.class); 134 verboseLog(TAG, "getWifiHotspotSpeedViewModel():" + viewModel); 135 return viewModel; 136 } 137 138 /** 139 * Gets WifiNetworkDetailsViewModel 140 */ getWifiNetworkDetailsViewModel( @otNull ViewModelStoreOwner owner)141 public WifiNetworkDetailsViewModel getWifiNetworkDetailsViewModel( 142 @NotNull ViewModelStoreOwner owner) { 143 WifiNetworkDetailsViewModel viewModel = 144 new ViewModelProvider(owner).get(WifiNetworkDetailsViewModel.class); 145 verboseLog(TAG, "getWifiNetworkDetailsViewModel():" + viewModel); 146 return viewModel; 147 } 148 149 /** 150 * Send a {@link Log#VERBOSE} log message. 151 * 152 * @param tag Used to identify the source of a log message. It usually identifies 153 * the class or activity where the log call occurs. 154 * @param msg The message you would like logged. 155 */ verboseLog(@ullable String tag, @NonNull String msg)156 public void verboseLog(@Nullable String tag, @NonNull String msg) { 157 getWifiVerboseLogging().log(tag, msg); 158 } 159 } 160 161