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