1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.systemui.qs.tiles;
import static com.android.systemui.Prefs.Key.QS_HAS_TURNED_OFF_MOBILE_DATA;
import android.annotation.NonNull;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Looper;
import android.os.UserHandle;
import android.provider.Settings;
import android.service.quicksettings.Tile;
import android.telephony.SubscriptionManager;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.widget.Switch;
import androidx.annotation.Nullable;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settingslib.net.DataUsageController;
import com.android.systemui.Prefs;
import com.android.systemui.R;
import com.android.systemui.dagger.qualifiers.Background;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.plugins.ActivityStarter;
import com.android.systemui.plugins.FalsingManager;
import com.android.systemui.plugins.qs.DetailAdapter;
import com.android.systemui.plugins.qs.QSIconView;
import com.android.systemui.plugins.qs.QSTile.SignalState;
import com.android.systemui.plugins.statusbar.StatusBarStateController;
import com.android.systemui.qs.QSHost;
import com.android.systemui.qs.SignalTileView;
import com.android.systemui.qs.logging.QSLogger;
import com.android.systemui.qs.tileimpl.QSTileImpl;
import com.android.systemui.statusbar.connectivity.IconState;
import com.android.systemui.statusbar.connectivity.MobileDataIndicators;
import com.android.systemui.statusbar.connectivity.NetworkController;
import com.android.systemui.statusbar.connectivity.SignalCallback;
import com.android.systemui.statusbar.phone.SystemUIDialog;
import javax.inject.Inject;
/** Quick settings tile: Cellular **/
public class CellularTile extends QSTileImpl<SignalState> {
private static final String ENABLE_SETTINGS_DATA_PLAN = "enable.settings.data.plan";
private final NetworkController mController;
private final DataUsageController mDataController;
private final CellularDetailAdapter mDetailAdapter;
private final CellSignalCallback mSignalCallback = new CellSignalCallback();
@Inject
public CellularTile(
QSHost host,
@Background Looper backgroundLooper,
@Main Handler mainHandler,
FalsingManager falsingManager,
MetricsLogger metricsLogger,
StatusBarStateController statusBarStateController,
ActivityStarter activityStarter,
QSLogger qsLogger,
NetworkController networkController
) {
super(host, backgroundLooper, mainHandler, falsingManager, metricsLogger,
statusBarStateController, activityStarter, qsLogger);
mController = networkController;
mDataController = mController.getMobileDataController();
mDetailAdapter = new CellularDetailAdapter();
mController.observe(getLifecycle(), mSignalCallback);
}
@Override
public SignalState newTileState() {
return new SignalState();
}
@Override
public QSIconView createTileView(Context context) {
return new SignalTileView(context);
}
@Override
public DetailAdapter getDetailAdapter() {
return mDetailAdapter;
}
@Override
public Intent getLongClickIntent() {
if (getState().state == Tile.STATE_UNAVAILABLE) {
return new Intent(Settings.ACTION_WIRELESS_SETTINGS);
}
return getCellularSettingIntent();
}
@Override
protected void handleClick(@Nullable View view) {
if (getState().state == Tile.STATE_UNAVAILABLE) {
return;
}
if (mDataController.isMobileDataEnabled()) {
maybeShowDisableDialog();
} else {
mDataController.setMobileDataEnabled(true);
}
}
private void maybeShowDisableDialog() {
if (Prefs.getBoolean(mContext, QS_HAS_TURNED_OFF_MOBILE_DATA, false)) {
// Directly turn off mobile data if the user has seen the dialog before.
mDataController.setMobileDataEnabled(false);
return;
}
String carrierName = mController.getMobileDataNetworkName();
boolean isInService = mController.isMobileDataNetworkInService();
if (TextUtils.isEmpty(carrierName) || !isInService) {
carrierName = mContext.getString(R.string.mobile_data_disable_message_default_carrier);
}
AlertDialog dialog = new Builder(mContext)
.setTitle(R.string.mobile_data_disable_title)
.setMessage(mContext.getString(R.string.mobile_data_disable_message, carrierName))
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(
com.android.internal.R.string.alert_windows_notification_turn_off_action,
(d, w) -> {
mDataController.setMobileDataEnabled(false);
Prefs.putBoolean(mContext, QS_HAS_TURNED_OFF_MOBILE_DATA, true);
})
.create();
dialog.getWindow().setType(LayoutParams.TYPE_KEYGUARD_DIALOG);
SystemUIDialog.setShowForAllUsers(dialog, true);
SystemUIDialog.registerDismissListener(dialog);
SystemUIDialog.setWindowOnTop(dialog);
dialog.show();
}
@Override
protected void handleSecondaryClick(@Nullable View view) {
if (mDataController.isMobileDataSupported()) {
showDetail(true);
} else {
mActivityStarter
.postStartActivityDismissingKeyguard(getCellularSettingIntent(),0 /* delay */);
}
}
@Override
public CharSequence getTileLabel() {
return mContext.getString(R.string.quick_settings_cellular_detail_title);
}
@Override
protected void handleUpdateState(SignalState state, Object arg) {
CallbackInfo cb = (CallbackInfo) arg;
if (cb == null) {
cb = mSignalCallback.mInfo;
}
final Resources r = mContext.getResources();
state.label = r.getString(R.string.mobile_data);
boolean mobileDataEnabled = mDataController.isMobileDataSupported()
&& mDataController.isMobileDataEnabled();
state.value = mobileDataEnabled;
state.activityIn = mobileDataEnabled && cb.activityIn;
state.activityOut = mobileDataEnabled && cb.activityOut;
state.expandedAccessibilityClassName = Switch.class.getName();
if (cb.noSim) {
state.icon = ResourceIcon.get(R.drawable.ic_qs_no_sim);
} else {
state.icon = ResourceIcon.get(R.drawable.ic_swap_vert);
}
if (cb.noSim) {
state.state = Tile.STATE_UNAVAILABLE;
state.secondaryLabel = r.getString(R.string.keyguard_missing_sim_message_short);
} else if (cb.airplaneModeEnabled) {
state.state = Tile.STATE_UNAVAILABLE;
state.secondaryLabel = r.getString(R.string.status_bar_airplane);
} else if (mobileDataEnabled) {
state.state = Tile.STATE_ACTIVE;
state.secondaryLabel = appendMobileDataType(
// Only show carrier name if there are more than 1 subscription
cb.multipleSubs ? cb.dataSubscriptionName : "",
getMobileDataContentName(cb));
} else {
state.state = Tile.STATE_INACTIVE;
state.secondaryLabel = r.getString(R.string.cell_data_off);
}
state.contentDescription = state.label;
if (state.state == Tile.STATE_INACTIVE) {
// This information is appended later by converting the Tile.STATE_INACTIVE state.
state.stateDescription = "";
} else {
state.stateDescription = state.secondaryLabel;
}
}
private CharSequence appendMobileDataType(CharSequence current, CharSequence dataType) {
if (TextUtils.isEmpty(dataType)) {
return Html.fromHtml(current.toString(), 0);
}
if (TextUtils.isEmpty(current)) {
return Html.fromHtml(dataType.toString(), 0);
}
String concat = mContext.getString(R.string.mobile_carrier_text_format, current, dataType);
return Html.fromHtml(concat, 0);
}
private CharSequence getMobileDataContentName(CallbackInfo cb) {
if (cb.roaming && !TextUtils.isEmpty(cb.dataContentDescription)) {
String roaming = mContext.getString(R.string.data_connection_roaming);
String dataDescription = cb.dataContentDescription.toString();
return mContext.getString(R.string.mobile_data_text_format, roaming, dataDescription);
}
if (cb.roaming) {
return mContext.getString(R.string.data_connection_roaming);
}
return cb.dataContentDescription;
}
@Override
public int getMetricsCategory() {
return MetricsEvent.QS_CELLULAR;
}
@Override
public boolean isAvailable() {
return mController.hasMobileDataFeature()
&& mHost.getUserContext().getUserId() == UserHandle.USER_SYSTEM;
}
private static final class CallbackInfo {
boolean airplaneModeEnabled;
CharSequence dataSubscriptionName;
CharSequence dataContentDescription;
boolean activityIn;
boolean activityOut;
boolean noSim;
boolean roaming;
boolean multipleSubs;
}
private final class CellSignalCallback implements SignalCallback {
private final CallbackInfo mInfo = new CallbackInfo();
@Override
public void setMobileDataIndicators(@NonNull MobileDataIndicators indicators) {
if (indicators.qsIcon == null) {
// Not data sim, don't display.
return;
}
mInfo.dataSubscriptionName = mController.getMobileDataNetworkName();
mInfo.dataContentDescription = indicators.qsDescription != null
? indicators.typeContentDescriptionHtml : null;
mInfo.activityIn = indicators.activityIn;
mInfo.activityOut = indicators.activityOut;
mInfo.roaming = indicators.roaming;
mInfo.multipleSubs = mController.getNumberSubscriptions() > 1;
refreshState(mInfo);
}
@Override
public void setNoSims(boolean show, boolean simDetected) {
mInfo.noSim = show;
refreshState(mInfo);
}
@Override
public void setIsAirplaneMode(@NonNull IconState icon) {
mInfo.airplaneModeEnabled = icon.visible;
refreshState(mInfo);
}
@Override
public void setMobileDataEnabled(boolean enabled) {
mDetailAdapter.setMobileDataEnabled(enabled);
refreshState(mInfo);
}
}
static Intent getCellularSettingIntent() {
Intent intent = new Intent(Settings.ACTION_NETWORK_OPERATOR_SETTINGS);
int dataSub = SubscriptionManager.getDefaultDataSubscriptionId();
if (dataSub != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
intent.putExtra(Settings.EXTRA_SUB_ID,
SubscriptionManager.getDefaultDataSubscriptionId());
}
return intent;
}
private final class CellularDetailAdapter implements DetailAdapter {
@Override
public CharSequence getTitle() {
return mContext.getString(R.string.quick_settings_cellular_detail_title);
}
@Override
public Boolean getToggleState() {
return mDataController.isMobileDataSupported()
? mDataController.isMobileDataEnabled()
: null;
}
@Override
public Intent getSettingsIntent() {
return getCellularSettingIntent();
}
@Override
public void setToggleState(boolean state) {
MetricsLogger.action(mContext, MetricsEvent.QS_CELLULAR_TOGGLE, state);
mDataController.setMobileDataEnabled(state);
}
@Override
public int getMetricsCategory() {
return MetricsEvent.QS_DATAUSAGEDETAIL;
}
@Override
public View createDetailView(Context context, View convertView, ViewGroup parent) {
final DataUsageDetailView v = (DataUsageDetailView) (convertView != null
? convertView
: LayoutInflater.from(mContext).inflate(R.layout.data_usage, parent, false));
final DataUsageController.DataUsageInfo info = mDataController.getDataUsageInfo();
if (info == null) return v;
v.bind(info);
v.findViewById(R.id.roaming_text).setVisibility(mSignalCallback.mInfo.roaming
? View.VISIBLE : View.INVISIBLE);
return v;
}
public void setMobileDataEnabled(boolean enabled) {
fireToggleStateChanged(enabled);
}
}
}
|