1 /*
<lambda>null2  * 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.systemui.qs.tiles
18 
19 import android.content.Intent
20 import android.os.Handler
21 import android.os.Looper
22 import android.provider.Settings
23 import android.widget.Switch
24 import com.android.internal.logging.MetricsLogger
25 import com.android.systemui.animation.Expandable
26 import com.android.systemui.dagger.qualifiers.Background
27 import com.android.systemui.dagger.qualifiers.Main
28 import com.android.systemui.plugins.ActivityStarter
29 import com.android.systemui.plugins.FalsingManager
30 import com.android.systemui.plugins.qs.QSTile
31 import com.android.systemui.plugins.statusbar.StatusBarStateController
32 import com.android.systemui.qs.QSHost
33 import com.android.systemui.qs.QsEventLogger
34 import com.android.systemui.qs.logging.QSLogger
35 import com.android.systemui.qs.tileimpl.QSTileImpl
36 import com.android.systemui.qs.tiles.dialog.InternetDialogManager
37 import com.android.systemui.res.R
38 import com.android.systemui.statusbar.connectivity.AccessPointController
39 import com.android.systemui.statusbar.pipeline.shared.ui.binder.InternetTileBinder
40 import com.android.systemui.statusbar.pipeline.shared.ui.model.InternetTileModel
41 import com.android.systemui.statusbar.pipeline.shared.ui.viewmodel.InternetTileViewModel
42 import javax.inject.Inject
43 
44 class InternetTileNewImpl
45 @Inject
46 constructor(
47     host: QSHost,
48     uiEventLogger: QsEventLogger,
49     @Background backgroundLooper: Looper,
50     @Main private val mainHandler: Handler,
51     falsingManager: FalsingManager,
52     metricsLogger: MetricsLogger,
53     statusBarStateController: StatusBarStateController,
54     activityStarter: ActivityStarter,
55     qsLogger: QSLogger,
56     viewModel: InternetTileViewModel,
57     private val internetDialogManager: InternetDialogManager,
58     private val accessPointController: AccessPointController,
59 ) :
60     QSTileImpl<QSTile.BooleanState>(
61         host,
62         uiEventLogger,
63         backgroundLooper,
64         mainHandler,
65         falsingManager,
66         metricsLogger,
67         statusBarStateController,
68         activityStarter,
69         qsLogger
70     ) {
71     private var model: InternetTileModel = viewModel.tileModel.value
72 
73     init {
74         InternetTileBinder.bind(lifecycle, viewModel.tileModel) { newModel ->
75             model = newModel
76             refreshState()
77         }
78     }
79 
80     override fun getTileLabel(): CharSequence =
81         mContext.getString(R.string.quick_settings_internet_label)
82 
83     override fun newTileState(): QSTile.BooleanState {
84         return QSTile.BooleanState().also { it.forceExpandIcon = true }
85     }
86 
87     override fun handleClick(expandable: Expandable?) {
88         mainHandler.post {
89             internetDialogManager.create(
90                 aboveStatusBar = true,
91                 accessPointController.canConfigMobileData(),
92                 accessPointController.canConfigWifi(),
93                 expandable,
94             )
95         }
96     }
97 
98     override fun handleUpdateState(state: QSTile.BooleanState, arg: Any?) {
99         state.label = mContext.resources.getString(R.string.quick_settings_internet_label)
100         state.expandedAccessibilityClassName = Switch::class.java.name
101 
102         model.applyTo(state, mContext)
103     }
104 
105     override fun getLongClickIntent(): Intent = WIFI_SETTINGS
106 
107     companion object {
108         private val WIFI_SETTINGS = Intent(Settings.ACTION_WIFI_SETTINGS)
109     }
110 }
111