• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * 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.systemui.statusbar.pipeline.wifi.data.repository.demo
18 
19 import android.net.wifi.WifiManager
20 import android.os.Bundle
21 import com.android.systemui.dagger.SysUISingleton
22 import com.android.systemui.dagger.qualifiers.Application
23 import com.android.systemui.demomode.DemoMode.COMMAND_NETWORK
24 import com.android.systemui.demomode.DemoModeController
25 import com.android.systemui.statusbar.pipeline.mobile.data.repository.MobileConnectionRepository.Companion.DEFAULT_NUM_LEVELS
26 import com.android.systemui.statusbar.pipeline.wifi.data.repository.demo.model.FakeWifiEventModel
27 import javax.inject.Inject
28 import kotlinx.coroutines.CoroutineScope
29 import kotlinx.coroutines.flow.SharingStarted
30 import kotlinx.coroutines.flow.map
31 import kotlinx.coroutines.flow.shareIn
32 
33 /** Data source to map between demo mode commands and inputs into [DemoWifiRepository]'s flows */
34 @SysUISingleton
35 class DemoModeWifiDataSource
36 @Inject
37 constructor(
38     demoModeController: DemoModeController,
39     @Application scope: CoroutineScope,
40 ) {
41     private val demoCommandStream = demoModeController.demoFlowForCommand(COMMAND_NETWORK)
42     private val _wifiCommands = demoCommandStream.map { args -> args.toWifiEvent() }
43     val wifiEvents = _wifiCommands.shareIn(scope, SharingStarted.WhileSubscribed())
44 
45     private fun Bundle.toWifiEvent(): FakeWifiEventModel? {
46         val wifi = getString("wifi") ?: return null
47         return when (wifi) {
48             "show" -> activeWifiEvent()
49             "carriermerged" -> carrierMergedWifiEvent()
50             else -> FakeWifiEventModel.WifiDisabled
51         }
52     }
53 
54     private fun Bundle.activeWifiEvent(): FakeWifiEventModel.Wifi {
55         val level = getString("level")?.toInt()
56         val activity = getString("activity").toActivity()
57         val ssid = getString("ssid")
58         val validated = getString("fully").toBoolean()
59 
60         return FakeWifiEventModel.Wifi(
61             level = level,
62             activity = activity,
63             ssid = ssid,
64             validated = validated,
65         )
66     }
67 
68     private fun Bundle.carrierMergedWifiEvent(): FakeWifiEventModel.CarrierMerged {
69         val subId = getString("slot")?.toInt() ?: DEFAULT_CARRIER_MERGED_SUB_ID
70         val level = getString("level")?.toInt() ?: 0
71         val numberOfLevels = getString("numlevels")?.toInt() ?: DEFAULT_NUM_LEVELS
72         val activity = getString("activity").toActivity()
73 
74         return FakeWifiEventModel.CarrierMerged(subId, level, numberOfLevels, activity)
75     }
76 
77     private fun String?.toActivity(): Int =
78         when (this) {
79             "inout" -> WifiManager.TrafficStateCallback.DATA_ACTIVITY_INOUT
80             "in" -> WifiManager.TrafficStateCallback.DATA_ACTIVITY_IN
81             "out" -> WifiManager.TrafficStateCallback.DATA_ACTIVITY_OUT
82             else -> WifiManager.TrafficStateCallback.DATA_ACTIVITY_NONE
83         }
84 
85     companion object {
86         const val DEFAULT_CARRIER_MERGED_SUB_ID = 10
87     }
88 }
89