|
| 1 | +package com.android.systemui.qs.tiles; |
| 2 | + |
| 3 | +import android.content.BroadcastReceiver; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.Intent; |
| 6 | +import android.content.IntentFilter; |
| 7 | +import android.os.AsyncTask; |
| 8 | +import android.os.Handler; |
| 9 | +import android.os.Looper; |
| 10 | +import android.os.SystemProperties; |
| 11 | +import android.provider.Settings; |
| 12 | +import android.telephony.PhoneStateListener; |
| 13 | +import android.telephony.SubscriptionInfo; |
| 14 | +import android.telephony.SubscriptionManager; |
| 15 | +import android.telephony.TelephonyManager; |
| 16 | +import android.text.TextUtils; |
| 17 | +import android.util.Log; |
| 18 | + |
| 19 | +import androidx.annotation.Nullable; |
| 20 | + |
| 21 | +import com.android.internal.logging.MetricsLogger; |
| 22 | + |
| 23 | +import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
| 24 | +import com.android.internal.telephony.IccCardConstants; |
| 25 | +import com.android.internal.telephony.TelephonyIntents; |
| 26 | +import com.android.systemui.animation.Expandable; |
| 27 | +import com.android.systemui.dagger.qualifiers.Background; |
| 28 | +import com.android.systemui.dagger.qualifiers.Main; |
| 29 | +import com.android.systemui.plugins.ActivityStarter; |
| 30 | +import com.android.systemui.plugins.FalsingManager; |
| 31 | +import com.android.systemui.plugins.qs.QSTile.BooleanState; |
| 32 | +import com.android.systemui.plugins.statusbar.StatusBarStateController; |
| 33 | +import com.android.systemui.qs.QsEventLogger; |
| 34 | +import com.android.systemui.qs.QSHost; |
| 35 | +import com.android.systemui.qs.logging.QSLogger; |
| 36 | +import com.android.systemui.qs.pipeline.domain.interactor.PanelInteractor; |
| 37 | +import com.android.systemui.qs.tileimpl.QSTileImpl; |
| 38 | +import com.android.systemui.qs.tileimpl.QSTileImpl.ResourceIcon; |
| 39 | +import com.android.systemui.res.R; |
| 40 | + |
| 41 | +import java.util.List; |
| 42 | + |
| 43 | +import javax.inject.Inject; |
| 44 | + |
| 45 | +public class DataSwitchTile extends QSTileImpl<BooleanState> { |
| 46 | + |
| 47 | + public static final String TILE_SPEC = "dataswitch"; |
| 48 | + |
| 49 | + private boolean mCanSwitch = true; |
| 50 | + private boolean mRegistered = false; |
| 51 | + private int mSimCount = 0; |
| 52 | + BroadcastReceiver mSimReceiver = new BroadcastReceiver() { |
| 53 | + public void onReceive(Context context, Intent intent) { |
| 54 | + Log.d(TAG, "mSimReceiver:onReceive"); |
| 55 | + refreshState(); |
| 56 | + } |
| 57 | + }; |
| 58 | + private final MyCallStateListener mPhoneStateListener; |
| 59 | + private final SubscriptionManager mSubscriptionManager; |
| 60 | + private final TelephonyManager mTelephonyManager; |
| 61 | + private final PanelInteractor mPanelInteractor; |
| 62 | + |
| 63 | + class MyCallStateListener extends PhoneStateListener { |
| 64 | + MyCallStateListener() { |
| 65 | + } |
| 66 | + |
| 67 | + public void onCallStateChanged(int state, String arg1) { |
| 68 | + mCanSwitch = mTelephonyManager.getCallState() == 0; |
| 69 | + refreshState(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Inject |
| 74 | + public DataSwitchTile( |
| 75 | + QSHost host, |
| 76 | + QsEventLogger uiEventLogger, |
| 77 | + @Background Looper backgroundLooper, |
| 78 | + @Main Handler mainHandler, |
| 79 | + FalsingManager falsingManager, |
| 80 | + MetricsLogger metricsLogger, |
| 81 | + StatusBarStateController statusBarStateController, |
| 82 | + ActivityStarter activityStarter, |
| 83 | + QSLogger qsLogger, |
| 84 | + PanelInteractor panelInteractor |
| 85 | + ) { |
| 86 | + super(host, uiEventLogger, backgroundLooper, mainHandler, falsingManager, metricsLogger, |
| 87 | + statusBarStateController, activityStarter, qsLogger); |
| 88 | + mSubscriptionManager = SubscriptionManager.from(host.getContext()); |
| 89 | + mTelephonyManager = TelephonyManager.from(host.getContext()); |
| 90 | + mPhoneStateListener = new MyCallStateListener(); |
| 91 | + mPanelInteractor = panelInteractor; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public boolean isAvailable() { |
| 96 | + int count = TelephonyManager.getDefault().getPhoneCount(); |
| 97 | + Log.d(TAG, "phoneCount: " + count); |
| 98 | + return count >= 2; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public BooleanState newTileState() { |
| 103 | + return new BooleanState(); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void handleSetListening(boolean listening) { |
| 108 | + if (listening) { |
| 109 | + if (!mRegistered) { |
| 110 | + IntentFilter filter = new IntentFilter(); |
| 111 | + filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
| 112 | + mContext.registerReceiver(mSimReceiver, filter, Context.RECEIVER_NOT_EXPORTED); |
| 113 | + mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); |
| 114 | + mRegistered = true; |
| 115 | + } |
| 116 | + refreshState(); |
| 117 | + } else if (mRegistered) { |
| 118 | + mContext.unregisterReceiver(mSimReceiver); |
| 119 | + mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE); |
| 120 | + mRegistered = false; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private void updateSimCount() { |
| 125 | + String simState = SystemProperties.get("gsm.sim.state"); |
| 126 | + Log.d(TAG, "DataSwitchTile:updateSimCount:simState=" + simState); |
| 127 | + mSimCount = 0; |
| 128 | + try { |
| 129 | + String[] sims = TextUtils.split(simState, ","); |
| 130 | + for (String sim : sims) { |
| 131 | + if (!sim.isEmpty() |
| 132 | + && !sim.equalsIgnoreCase(IccCardConstants.INTENT_VALUE_ICC_ABSENT) |
| 133 | + && !sim.equalsIgnoreCase(IccCardConstants.INTENT_VALUE_ICC_NOT_READY)) { |
| 134 | + mSimCount++; |
| 135 | + } |
| 136 | + } |
| 137 | + } catch (Exception e) { |
| 138 | + Log.e(TAG, "Error to parse sim state"); |
| 139 | + } |
| 140 | + Log.d(TAG, "DataSwitchTile:updateSimCount:mSimCount=" + mSimCount); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + protected void handleClick(@Nullable Expandable expandable) { |
| 145 | + if (!mCanSwitch) { |
| 146 | + Log.d(TAG, "Call state=" + mTelephonyManager.getCallState()); |
| 147 | + } else if (mSimCount == 0) { |
| 148 | + Log.d(TAG, "handleClick:no sim card"); |
| 149 | + } else if (mSimCount == 1) { |
| 150 | + Log.d(TAG, "handleClick:only one sim card"); |
| 151 | + } else { |
| 152 | + AsyncTask.execute(() -> { |
| 153 | + toggleMobileDataEnabled(); |
| 154 | + refreshState(); |
| 155 | + }); |
| 156 | + mPanelInteractor.collapsePanels(); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public Intent getLongClickIntent() { |
| 162 | + return new Intent(Settings.ACTION_NETWORK_OPERATOR_SETTINGS); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public CharSequence getTileLabel() { |
| 167 | + return mContext.getString(R.string.qs_data_switch_label); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + protected void handleUpdateState(BooleanState state, Object arg) { |
| 172 | + boolean activeSIMZero; |
| 173 | + if (arg == null) { |
| 174 | + int defaultPhoneId = mSubscriptionManager.getPhoneId( |
| 175 | + mSubscriptionManager.getDefaultDataSubscriptionId()); |
| 176 | + Log.d(TAG, "default data phone id=" + defaultPhoneId); |
| 177 | + activeSIMZero = defaultPhoneId == 0; |
| 178 | + } else { |
| 179 | + activeSIMZero = (Boolean) arg; |
| 180 | + } |
| 181 | + updateSimCount(); |
| 182 | + switch (mSimCount) { |
| 183 | + case 0: |
| 184 | + state.icon = ResourceIcon.get(R.drawable.ic_qs_data_switch_0); |
| 185 | + state.value = false; |
| 186 | + state.secondaryLabel = mContext.getString(R.string.tile_unavailable); |
| 187 | + break; |
| 188 | + case 1: |
| 189 | + state.icon = ResourceIcon.get(activeSIMZero |
| 190 | + ? R.drawable.ic_qs_data_switch_1 |
| 191 | + : R.drawable.ic_qs_data_switch_2); |
| 192 | + state.value = false; |
| 193 | + state.secondaryLabel = mContext.getString(R.string.tile_unavailable); |
| 194 | + break; |
| 195 | + case 2: |
| 196 | + state.icon = ResourceIcon.get(activeSIMZero |
| 197 | + ? R.drawable.ic_qs_data_switch_1 |
| 198 | + : R.drawable.ic_qs_data_switch_2); |
| 199 | + state.value = true; |
| 200 | + state.secondaryLabel = getActiveSlotName(); |
| 201 | + break; |
| 202 | + default: |
| 203 | + state.icon = ResourceIcon.get(R.drawable.ic_qs_data_switch_1); |
| 204 | + state.value = false; |
| 205 | + state.secondaryLabel = mContext.getString(R.string.tile_unavailable); |
| 206 | + break; |
| 207 | + } |
| 208 | + if (mSimCount < 2) { |
| 209 | + state.state = 0; |
| 210 | + } else if (!mCanSwitch) { |
| 211 | + state.state = 0; |
| 212 | + Log.d(TAG, "call state isn't idle, set to unavailable."); |
| 213 | + } else { |
| 214 | + state.state = state.value ? 2 : 1; |
| 215 | + } |
| 216 | + |
| 217 | + state.label = mContext.getString(R.string.qs_data_switch_label); |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * Set whether to enable data for {@code subId}, also whether to disable data for other |
| 222 | + * subscription |
| 223 | + */ |
| 224 | + private void toggleMobileDataEnabled() { |
| 225 | + TelephonyManager telephonyManager; |
| 226 | + boolean dataEnabled = false; |
| 227 | + boolean foundActive = false; |
| 228 | + int subId; |
| 229 | + List<SubscriptionInfo> subInfoList = |
| 230 | + mSubscriptionManager.getActiveSubscriptionInfoList(true); |
| 231 | + if (subInfoList != null) { |
| 232 | + for (SubscriptionInfo subInfo : subInfoList) { |
| 233 | + subId = subInfo.getSubscriptionId(); |
| 234 | + telephonyManager = |
| 235 | + mTelephonyManager.createForSubscriptionId(subId); |
| 236 | + dataEnabled = telephonyManager.getDataEnabled(); |
| 237 | + if (subInfo.isOpportunistic() && dataEnabled) { |
| 238 | + // We never disable mobile data for opportunistic subscriptions. |
| 239 | + continue; |
| 240 | + } else { |
| 241 | + dataEnabled = !dataEnabled && !foundActive; |
| 242 | + telephonyManager.setDataEnabled(dataEnabled); |
| 243 | + if (dataEnabled) mSubscriptionManager.setDefaultDataSubId(subId); |
| 244 | + // Indicate we found sim with active data, disable data on remaining sim. |
| 245 | + if (!foundActive) foundActive = dataEnabled; |
| 246 | + } |
| 247 | + Log.d(TAG, "Changed subID " + subId + " to " |
| 248 | + + !dataEnabled); |
| 249 | + } |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + private String getActiveSlotName() { |
| 254 | + TelephonyManager telephonyManager; |
| 255 | + String mInitialState = mContext.getString(R.string.tile_unavailable); |
| 256 | + List<SubscriptionInfo> subInfoList = |
| 257 | + mSubscriptionManager.getActiveSubscriptionInfoList(true); |
| 258 | + if (subInfoList != null) { |
| 259 | + for (SubscriptionInfo subInfo : subInfoList) { |
| 260 | + telephonyManager = |
| 261 | + mTelephonyManager.createForSubscriptionId(subInfo.getSubscriptionId()); |
| 262 | + if (telephonyManager.getDataEnabled()) { |
| 263 | + // Active SIM found |
| 264 | + return subInfo.getDisplayName().toString(); |
| 265 | + } |
| 266 | + } |
| 267 | + } |
| 268 | + return mInitialState; |
| 269 | + } |
| 270 | +} |
0 commit comments