Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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.phone; |
| 18 | |
| 19 | import android.app.Notification; |
| 20 | import android.app.NotificationManager; |
| 21 | import android.app.PendingIntent; |
| 22 | import android.app.Service; |
| 23 | import android.content.BroadcastReceiver; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 27 | import android.os.AsyncResult; |
| 28 | import android.os.Binder; |
| 29 | import android.os.CountDownTimer; |
| 30 | import android.os.Handler; |
| 31 | import android.os.IBinder; |
| 32 | import android.os.Message; |
| 33 | import android.os.SystemProperties; |
| 34 | import android.util.Log; |
| 35 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 36 | import com.android.internal.telephony.Phone; |
| 37 | import com.android.internal.telephony.PhoneConstants; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 38 | import com.android.internal.telephony.TelephonyIntents; |
| 39 | import com.android.internal.telephony.TelephonyProperties; |
fionaxu | 8b7620d | 2017-05-01 16:22:17 -0700 | [diff] [blame] | 40 | import com.android.internal.telephony.util.NotificationChannelController; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 41 | |
Brad Ebinger | cd3601b | 2017-05-09 15:27:27 -0700 | [diff] [blame^] | 42 | import java.text.SimpleDateFormat; |
| 43 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Application service that inserts/removes Emergency Callback Mode notification and |
| 46 | * updates Emergency Callback Mode countdown clock in the notification |
| 47 | * |
| 48 | * @see EmergencyCallbackModeExitDialog |
| 49 | */ |
| 50 | public class EmergencyCallbackModeService extends Service { |
| 51 | |
| 52 | // Default Emergency Callback Mode timeout value |
| 53 | private static final int DEFAULT_ECM_EXIT_TIMER_VALUE = 300000; |
| 54 | private static final String LOG_TAG = "EmergencyCallbackModeService"; |
| 55 | |
| 56 | private NotificationManager mNotificationManager = null; |
| 57 | private CountDownTimer mTimer = null; |
| 58 | private long mTimeLeft = 0; |
| 59 | private Phone mPhone = null; |
| 60 | private boolean mInEmergencyCall = false; |
| 61 | |
| 62 | private static final int ECM_TIMER_RESET = 1; |
| 63 | |
| 64 | private Handler mHandler = new Handler () { |
| 65 | public void handleMessage(Message msg) { |
| 66 | switch (msg.what) { |
| 67 | case ECM_TIMER_RESET: |
| 68 | resetEcmTimer((AsyncResult) msg.obj); |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | @Override |
| 75 | public void onCreate() { |
Sandeep Kunta | de73a6a | 2014-10-15 18:45:56 +0530 | [diff] [blame] | 76 | Phone phoneInEcm = PhoneGlobals.getInstance().getPhoneInEcm(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 77 | // Check if it is CDMA phone |
Sandeep Kunta | de73a6a | 2014-10-15 18:45:56 +0530 | [diff] [blame] | 78 | if (phoneInEcm == null || ((phoneInEcm.getPhoneType() != PhoneConstants.PHONE_TYPE_CDMA) |
| 79 | && (phoneInEcm.getImsPhone() == null))) { |
| 80 | Log.e(LOG_TAG, "Error! Emergency Callback Mode not supported for " + phoneInEcm); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 81 | stopSelf(); |
Sandeep Kunta | de73a6a | 2014-10-15 18:45:56 +0530 | [diff] [blame] | 82 | return; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // Register receiver for intents |
| 86 | IntentFilter filter = new IntentFilter(); |
| 87 | filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED); |
| 88 | filter.addAction(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS); |
| 89 | registerReceiver(mEcmReceiver, filter); |
| 90 | |
| 91 | mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
| 92 | |
| 93 | // Register ECM timer reset notfication |
Sandeep Kunta | de73a6a | 2014-10-15 18:45:56 +0530 | [diff] [blame] | 94 | mPhone = phoneInEcm; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 95 | mPhone.registerForEcmTimerReset(mHandler, ECM_TIMER_RESET, null); |
| 96 | |
| 97 | startTimerNotification(); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void onDestroy() { |
Sandeep Kunta | 55a6ae8 | 2015-12-14 17:49:02 +0530 | [diff] [blame] | 102 | if (mPhone != null) { |
| 103 | // Unregister receiver |
| 104 | unregisterReceiver(mEcmReceiver); |
| 105 | // Unregister ECM timer reset notification |
| 106 | mPhone.unregisterForEcmTimerReset(mHandler); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 107 | |
Sandeep Kunta | 55a6ae8 | 2015-12-14 17:49:02 +0530 | [diff] [blame] | 108 | // Cancel the notification and timer |
| 109 | mNotificationManager.cancel(R.string.phone_in_ecm_notification_title); |
| 110 | mTimer.cancel(); |
| 111 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Listens for Emergency Callback Mode intents |
| 116 | */ |
| 117 | private BroadcastReceiver mEcmReceiver = new BroadcastReceiver() { |
| 118 | @Override |
| 119 | public void onReceive(Context context, Intent intent) { |
| 120 | // Stop the service when phone exits Emergency Callback Mode |
| 121 | if (intent.getAction().equals( |
| 122 | TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) { |
| 123 | if (intent.getBooleanExtra("phoneinECMState", false) == false) { |
| 124 | stopSelf(); |
| 125 | } |
| 126 | } |
| 127 | // Show dialog box |
| 128 | else if (intent.getAction().equals( |
| 129 | TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS)) { |
| 130 | context.startActivity( |
| 131 | new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS) |
| 132 | .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 133 | } |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | /** |
| 138 | * Start timer notification for Emergency Callback Mode |
| 139 | */ |
| 140 | private void startTimerNotification() { |
| 141 | // Get Emergency Callback Mode timeout value |
| 142 | long ecmTimeout = SystemProperties.getLong( |
| 143 | TelephonyProperties.PROPERTY_ECM_EXIT_TIMER, DEFAULT_ECM_EXIT_TIMER_VALUE); |
| 144 | |
| 145 | // Show the notification |
| 146 | showNotification(ecmTimeout); |
| 147 | |
| 148 | // Start countdown timer for the notification updates |
Yorke Lee | 34a72cb | 2014-10-12 13:17:04 -0700 | [diff] [blame] | 149 | if (mTimer != null) { |
| 150 | mTimer.cancel(); |
| 151 | } else { |
| 152 | mTimer = new CountDownTimer(ecmTimeout, 1000) { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 153 | |
Yorke Lee | 34a72cb | 2014-10-12 13:17:04 -0700 | [diff] [blame] | 154 | @Override |
| 155 | public void onTick(long millisUntilFinished) { |
| 156 | mTimeLeft = millisUntilFinished; |
Yorke Lee | 34a72cb | 2014-10-12 13:17:04 -0700 | [diff] [blame] | 157 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 158 | |
Yorke Lee | 34a72cb | 2014-10-12 13:17:04 -0700 | [diff] [blame] | 159 | @Override |
| 160 | public void onFinish() { |
| 161 | //Do nothing |
| 162 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 163 | |
Yorke Lee | 34a72cb | 2014-10-12 13:17:04 -0700 | [diff] [blame] | 164 | }; |
| 165 | } |
| 166 | mTimer.start(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Shows notification for Emergency Callback Mode |
| 171 | */ |
| 172 | private void showNotification(long millisUntilFinished) { |
Amit Mahajan | 6a2f875 | 2016-07-25 11:38:10 -0700 | [diff] [blame] | 173 | Phone imsPhone = mPhone.getImsPhone(); |
| 174 | boolean isInEcm = mPhone.isInEcm() || (imsPhone != null && imsPhone.isInEcm()); |
Yorke Lee | 34a72cb | 2014-10-12 13:17:04 -0700 | [diff] [blame] | 175 | if (!isInEcm) { |
| 176 | Log.i(LOG_TAG, "Asked to show notification but not in ECM mode"); |
| 177 | if (mTimer != null) { |
| 178 | mTimer.cancel(); |
| 179 | } |
| 180 | return; |
| 181 | } |
fionaxu | 8b7620d | 2017-05-01 16:22:17 -0700 | [diff] [blame] | 182 | final Notification.Builder builder = new Notification.Builder(getApplicationContext()); |
Tyler Gunn | 625eb0b | 2014-08-27 20:37:32 -0700 | [diff] [blame] | 183 | builder.setOngoing(true); |
| 184 | builder.setPriority(Notification.PRIORITY_HIGH); |
| 185 | builder.setSmallIcon(R.drawable.ic_emergency_callback_mode); |
| 186 | builder.setTicker(getText(R.string.phone_entered_ecm_text)); |
| 187 | builder.setContentTitle(getText(R.string.phone_in_ecm_notification_title)); |
| 188 | builder.setColor(getResources().getColor(R.color.dialer_theme_color)); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 189 | |
| 190 | // PendingIntent to launch Emergency Callback Mode Exit activity if the user selects |
| 191 | // this notification |
| 192 | PendingIntent contentIntent = PendingIntent.getActivity(this, 0, |
| 193 | new Intent(EmergencyCallbackModeExitDialog.ACTION_SHOW_ECM_EXIT_DIALOG), 0); |
Tyler Gunn | 625eb0b | 2014-08-27 20:37:32 -0700 | [diff] [blame] | 194 | builder.setContentIntent(contentIntent); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 195 | |
| 196 | // Format notification string |
| 197 | String text = null; |
| 198 | if(mInEmergencyCall) { |
| 199 | text = getText(R.string.phone_in_ecm_call_notification_text).toString(); |
| 200 | } else { |
Brad Ebinger | cd3601b | 2017-05-09 15:27:27 -0700 | [diff] [blame^] | 201 | // Calculate the time in ms when the notification will be finished. |
| 202 | long finishedCountMs = millisUntilFinished + System.currentTimeMillis(); |
| 203 | builder.setShowWhen(true); |
| 204 | builder.setChronometerCountDown(true); |
| 205 | builder.setUsesChronometer(true); |
| 206 | builder.setWhen(finishedCountMs); |
| 207 | |
| 208 | String completeTime = SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT).format( |
| 209 | finishedCountMs); |
| 210 | text = getResources().getString(R.string.phone_in_ecm_notification_complete_time, |
| 211 | completeTime); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 212 | } |
Tyler Gunn | 625eb0b | 2014-08-27 20:37:32 -0700 | [diff] [blame] | 213 | builder.setContentText(text); |
fionaxu | 8b7620d | 2017-05-01 16:22:17 -0700 | [diff] [blame] | 214 | builder.setChannelId(NotificationChannelController.CHANNEL_ID_ALERT); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 215 | |
| 216 | // Show notification |
Tyler Gunn | 625eb0b | 2014-08-27 20:37:32 -0700 | [diff] [blame] | 217 | mNotificationManager.notify(R.string.phone_in_ecm_notification_title, builder.build()); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Handle ECM_TIMER_RESET notification |
| 222 | */ |
| 223 | private void resetEcmTimer(AsyncResult r) { |
| 224 | boolean isTimerCanceled = ((Boolean)r.result).booleanValue(); |
| 225 | |
| 226 | if (isTimerCanceled) { |
| 227 | mInEmergencyCall = true; |
| 228 | mTimer.cancel(); |
| 229 | showNotification(0); |
| 230 | } else { |
| 231 | mInEmergencyCall = false; |
| 232 | startTimerNotification(); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public IBinder onBind(Intent intent) { |
| 238 | return mBinder; |
| 239 | } |
| 240 | |
| 241 | // This is the object that receives interactions from clients. |
| 242 | private final IBinder mBinder = new LocalBinder(); |
| 243 | |
| 244 | /** |
| 245 | * Class for clients to access |
| 246 | */ |
| 247 | public class LocalBinder extends Binder { |
| 248 | EmergencyCallbackModeService getService() { |
| 249 | return EmergencyCallbackModeService.this; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Returns Emergency Callback Mode timeout value |
| 255 | */ |
| 256 | public long getEmergencyCallbackModeTimeout() { |
| 257 | return mTimeLeft; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Returns Emergency Callback Mode call state |
| 262 | */ |
| 263 | public boolean getEmergencyCallbackModeCallState() { |
| 264 | return mInEmergencyCall; |
| 265 | } |
| 266 | } |