1 /*
<lambda>null2  * Copyright (C) 2024 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
18 
19 import android.content.Context
20 import android.net.wifi.WifiInfo
21 import com.android.settings.wifi.repository.SharedConnectivityRepository
22 import com.android.settings.wifi.repository.WifiPickerRepository
23 import com.android.settings.wifi.repository.WifiStatusRepository
24 import com.android.settingslib.R
25 import com.android.settingslib.wifi.WifiStatusTracker
26 import com.android.wifitrackerlib.HotspotNetworkEntry
27 import kotlinx.coroutines.Dispatchers
28 import kotlinx.coroutines.flow.Flow
29 import kotlinx.coroutines.flow.combine
30 import kotlinx.coroutines.flow.conflate
31 import kotlinx.coroutines.flow.flowOn
32 import kotlinx.coroutines.flow.map
33 
34 /** Repository that listeners to wifi callback and provide wifi summary flow to client. */
35 class WifiSummaryRepository(
36     private val context: Context,
37     private val wifiStatusRepository: WifiStatusRepository = WifiStatusRepository(context),
38     private val wifiPickerRepository: WifiPickerRepository? =
39         if (SharedConnectivityRepository.isDeviceConfigEnabled()) WifiPickerRepository(context)
40         else null,
41 ) {
42 
43     fun summaryFlow(): Flow<String> {
44         if (wifiPickerRepository == null) return wifiStatusSummaryFlow()
45         return combine(
46             wifiStatusSummaryFlow(),
47             wifiPickerRepository.connectedWifiEntryFlow(),
48         ) { wifiStatusSummary, wifiEntry ->
49             if (wifiEntry is HotspotNetworkEntry) wifiEntry.alternateSummary else wifiStatusSummary
50         }
51     }
52 
53     private fun wifiStatusSummaryFlow() =
54         wifiStatusRepository
55             .wifiStatusTrackerFlow()
56             .map { wifiStatusTracker -> wifiStatusTracker.getSummary() }
57             .conflate()
58             .flowOn(Dispatchers.Default)
59 
60     private fun WifiStatusTracker.getSummary(): String {
61         if (!enabled) return context.getString(com.android.settings.R.string.switch_off_text)
62         if (!connected) return context.getString(com.android.settings.R.string.disconnected)
63         val sanitizedSsid = WifiInfo.sanitizeSsid(ssid) ?: ""
64         if (statusLabel.isNullOrEmpty()) return sanitizedSsid
65         return context.getString(
66             R.string.preference_summary_default_combination,
67             sanitizedSsid,
68             statusLabel,
69         )
70     }
71 }
72