Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | package com.android.systemui.statusbar.policy; |
| 17 | |
| 18 | import android.os.Handler; |
| 19 | import android.os.Looper; |
| 20 | import android.os.Message; |
| 21 | import android.telephony.SubscriptionInfo; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 22 | |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 23 | import com.android.internal.annotations.VisibleForTesting; |
Jason Monk | 46dbfb4 | 2016-02-25 14:59:20 -0500 | [diff] [blame] | 24 | import com.android.systemui.statusbar.policy.NetworkController.EmergencyListener; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 25 | import com.android.systemui.statusbar.policy.NetworkController.IconState; |
| 26 | import com.android.systemui.statusbar.policy.NetworkController.SignalCallback; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.List; |
| 30 | |
| 31 | |
| 32 | /** |
| 33 | * Implements network listeners and forwards the calls along onto other listeners but on |
| 34 | * the current or specified Looper. |
| 35 | */ |
| 36 | public class CallbackHandler extends Handler implements EmergencyListener, SignalCallback { |
| 37 | private static final int MSG_EMERGENCE_CHANGED = 0; |
| 38 | private static final int MSG_SUBS_CHANGED = 1; |
| 39 | private static final int MSG_NO_SIM_VISIBLE_CHANGED = 2; |
| 40 | private static final int MSG_ETHERNET_CHANGED = 3; |
| 41 | private static final int MSG_AIRPLANE_MODE_CHANGED = 4; |
| 42 | private static final int MSG_MOBILE_DATA_ENABLED_CHANGED = 5; |
| 43 | private static final int MSG_ADD_REMOVE_EMERGENCY = 6; |
| 44 | private static final int MSG_ADD_REMOVE_SIGNAL = 7; |
| 45 | |
| 46 | // All the callbacks. |
| 47 | private final ArrayList<EmergencyListener> mEmergencyListeners = new ArrayList<>(); |
| 48 | private final ArrayList<SignalCallback> mSignalCallbacks = new ArrayList<>(); |
| 49 | |
| 50 | public CallbackHandler() { |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 51 | super(Looper.getMainLooper()); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @VisibleForTesting |
| 55 | CallbackHandler(Looper looper) { |
| 56 | super(looper); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | @SuppressWarnings("unchecked") |
| 61 | public void handleMessage(Message msg) { |
| 62 | switch (msg.what) { |
| 63 | case MSG_EMERGENCE_CHANGED: |
| 64 | for (EmergencyListener listener : mEmergencyListeners) { |
| 65 | listener.setEmergencyCallsOnly(msg.arg1 != 0); |
| 66 | } |
| 67 | break; |
| 68 | case MSG_SUBS_CHANGED: |
| 69 | for (SignalCallback signalCluster : mSignalCallbacks) { |
| 70 | signalCluster.setSubs((List<SubscriptionInfo>) msg.obj); |
| 71 | } |
| 72 | break; |
| 73 | case MSG_NO_SIM_VISIBLE_CHANGED: |
| 74 | for (SignalCallback signalCluster : mSignalCallbacks) { |
Jason Monk | 1ff7766 | 2017-09-29 11:17:05 -0400 | [diff] [blame] | 75 | signalCluster.setNoSims(msg.arg1 != 0, msg.arg2 != 0); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 76 | } |
| 77 | break; |
| 78 | case MSG_ETHERNET_CHANGED: |
| 79 | for (SignalCallback signalCluster : mSignalCallbacks) { |
| 80 | signalCluster.setEthernetIndicators((IconState) msg.obj); |
| 81 | } |
| 82 | break; |
| 83 | case MSG_AIRPLANE_MODE_CHANGED: |
| 84 | for (SignalCallback signalCluster : mSignalCallbacks) { |
| 85 | signalCluster.setIsAirplaneMode((IconState) msg.obj); |
| 86 | } |
| 87 | break; |
| 88 | case MSG_MOBILE_DATA_ENABLED_CHANGED: |
| 89 | for (SignalCallback signalCluster : mSignalCallbacks) { |
| 90 | signalCluster.setMobileDataEnabled(msg.arg1 != 0); |
| 91 | } |
| 92 | break; |
| 93 | case MSG_ADD_REMOVE_EMERGENCY: |
| 94 | if (msg.arg1 != 0) { |
| 95 | mEmergencyListeners.add((EmergencyListener) msg.obj); |
| 96 | } else { |
| 97 | mEmergencyListeners.remove((EmergencyListener) msg.obj); |
| 98 | } |
| 99 | break; |
| 100 | case MSG_ADD_REMOVE_SIGNAL: |
| 101 | if (msg.arg1 != 0) { |
| 102 | mSignalCallbacks.add((SignalCallback) msg.obj); |
| 103 | } else { |
| 104 | mSignalCallbacks.remove((SignalCallback) msg.obj); |
| 105 | } |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void setWifiIndicators(final boolean enabled, final IconState statusIcon, |
| 112 | final IconState qsIcon, final boolean activityIn, final boolean activityOut, |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 113 | final String description, boolean isTransient, String secondaryLabel) { |
Evan Laird | 83c87e5 | 2019-09-24 19:14:05 -0400 | [diff] [blame] | 114 | post(() -> { |
| 115 | for (SignalCallback callback : mSignalCallbacks) { |
| 116 | callback.setWifiIndicators(enabled, statusIcon, qsIcon, activityIn, activityOut, |
| 117 | description, isTransient, secondaryLabel); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 118 | } |
| 119 | }); |
| 120 | } |
| 121 | |
| 122 | @Override |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 123 | public void setMobileDataIndicators(final IconState statusIcon, final IconState qsIcon, |
Evan Laird | 83c87e5 | 2019-09-24 19:14:05 -0400 | [diff] [blame] | 124 | final int statusType, final int qsType, final boolean activityIn, |
| 125 | final boolean activityOut, final CharSequence typeContentDescription, |
| 126 | CharSequence typeContentDescriptionHtml, final CharSequence description, |
| 127 | final boolean isWide, final int subId, boolean roaming) { |
| 128 | post(() -> { |
| 129 | for (SignalCallback signalCluster : mSignalCallbacks) { |
| 130 | signalCluster.setMobileDataIndicators(statusIcon, qsIcon, statusType, qsType, |
| 131 | activityIn, activityOut, typeContentDescription, |
| 132 | typeContentDescriptionHtml, description, isWide, subId, roaming); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 133 | } |
| 134 | }); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void setSubs(List<SubscriptionInfo> subs) { |
| 139 | obtainMessage(MSG_SUBS_CHANGED, subs).sendToTarget(); |
| 140 | } |
| 141 | |
| 142 | @Override |
Jason Monk | 1ff7766 | 2017-09-29 11:17:05 -0400 | [diff] [blame] | 143 | public void setNoSims(boolean show, boolean simDetected) { |
| 144 | obtainMessage(MSG_NO_SIM_VISIBLE_CHANGED, show ? 1 : 0, simDetected ? 1 : 0).sendToTarget(); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void setMobileDataEnabled(boolean enabled) { |
| 149 | obtainMessage(MSG_MOBILE_DATA_ENABLED_CHANGED, enabled ? 1 : 0, 0).sendToTarget(); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void setEmergencyCallsOnly(boolean emergencyOnly) { |
| 154 | obtainMessage(MSG_EMERGENCE_CHANGED, emergencyOnly ? 1 : 0, 0).sendToTarget(); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void setEthernetIndicators(IconState icon) { |
| 159 | obtainMessage(MSG_ETHERNET_CHANGED, icon).sendToTarget();; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void setIsAirplaneMode(IconState icon) { |
| 164 | obtainMessage(MSG_AIRPLANE_MODE_CHANGED, icon).sendToTarget();; |
| 165 | } |
| 166 | |
| 167 | public void setListening(EmergencyListener listener, boolean listening) { |
| 168 | obtainMessage(MSG_ADD_REMOVE_EMERGENCY, listening ? 1 : 0, 0, listener).sendToTarget(); |
| 169 | } |
| 170 | |
| 171 | public void setListening(SignalCallback listener, boolean listening) { |
| 172 | obtainMessage(MSG_ADD_REMOVE_SIGNAL, listening ? 1 : 0, 0, listener).sendToTarget(); |
| 173 | } |
| 174 | |
| 175 | } |