blob: 7e682555a8428308785ad3ac96bd7e85710dff48 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
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
17package com.android.phone;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.app.PendingIntent;
22import android.app.Service;
23import android.content.BroadcastReceiver;
24import android.content.Context;
25import android.content.Intent;
26import android.content.IntentFilter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.os.AsyncResult;
28import android.os.Binder;
29import android.os.CountDownTimer;
30import android.os.Handler;
31import android.os.IBinder;
32import android.os.Message;
Inseob Kim14bb3d02018-12-13 17:11:34 +090033import android.sysprop.TelephonyProperties;
Daniel Brightd24bee42020-01-09 22:11:15 -080034import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.util.Log;
36
Santos Cordon7d4ddf62013-07-10 11:58:08 -070037import com.android.internal.telephony.Phone;
38import com.android.internal.telephony.PhoneConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import com.android.internal.telephony.TelephonyIntents;
fionaxu8b7620d2017-05-01 16:22:17 -070040import com.android.internal.telephony.util.NotificationChannelController;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041
Brad Ebingercd3601b2017-05-09 15:27:27 -070042import java.text.SimpleDateFormat;
43
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044/**
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 */
50public class EmergencyCallbackModeService extends Service {
51
52 // Default Emergency Callback Mode timeout value
Inseob Kim14bb3d02018-12-13 17:11:34 +090053 private static final long DEFAULT_ECM_EXIT_TIMER_VALUE = 300000L;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054 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 Kuntade73a6a2014-10-15 18:45:56 +053076 Phone phoneInEcm = PhoneGlobals.getInstance().getPhoneInEcm();
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077 // Check if it is CDMA phone
Sandeep Kuntade73a6a2014-10-15 18:45:56 +053078 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 Cordon7d4ddf62013-07-10 11:58:08 -070081 stopSelf();
Sandeep Kuntade73a6a2014-10-15 18:45:56 +053082 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070083 }
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 Kuntade73a6a2014-10-15 18:45:56 +053094 mPhone = phoneInEcm;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095 mPhone.registerForEcmTimerReset(mHandler, ECM_TIMER_RESET, null);
96
97 startTimerNotification();
98 }
99
100 @Override
101 public void onDestroy() {
Sandeep Kunta55a6ae82015-12-14 17:49:02 +0530102 if (mPhone != null) {
103 // Unregister receiver
104 unregisterReceiver(mEcmReceiver);
105 // Unregister ECM timer reset notification
106 mPhone.unregisterForEcmTimerReset(mHandler);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700107
Sandeep Kunta55a6ae82015-12-14 17:49:02 +0530108 // Cancel the notification and timer
109 mNotificationManager.cancel(R.string.phone_in_ecm_notification_title);
110 mTimer.cancel();
111 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700112 }
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)) {
Daniel Brightd24bee42020-01-09 22:11:15 -0800123 if (!intent.getBooleanExtra(TelephonyManager.EXTRA_PHONE_IN_ECM_STATE, false)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124 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
Inseob Kim14bb3d02018-12-13 17:11:34 +0900142 long ecmTimeout = TelephonyProperties.ecm_exit_timer().orElse(DEFAULT_ECM_EXIT_TIMER_VALUE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700143
144 // Show the notification
145 showNotification(ecmTimeout);
146
147 // Start countdown timer for the notification updates
Yorke Lee34a72cb2014-10-12 13:17:04 -0700148 if (mTimer != null) {
149 mTimer.cancel();
150 } else {
151 mTimer = new CountDownTimer(ecmTimeout, 1000) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700152
Yorke Lee34a72cb2014-10-12 13:17:04 -0700153 @Override
154 public void onTick(long millisUntilFinished) {
155 mTimeLeft = millisUntilFinished;
Yorke Lee34a72cb2014-10-12 13:17:04 -0700156 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700157
Yorke Lee34a72cb2014-10-12 13:17:04 -0700158 @Override
159 public void onFinish() {
160 //Do nothing
161 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700162
Yorke Lee34a72cb2014-10-12 13:17:04 -0700163 };
164 }
165 mTimer.start();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700166 }
167
168 /**
169 * Shows notification for Emergency Callback Mode
170 */
171 private void showNotification(long millisUntilFinished) {
Amit Mahajan6a2f8752016-07-25 11:38:10 -0700172 Phone imsPhone = mPhone.getImsPhone();
173 boolean isInEcm = mPhone.isInEcm() || (imsPhone != null && imsPhone.isInEcm());
Yorke Lee34a72cb2014-10-12 13:17:04 -0700174 if (!isInEcm) {
175 Log.i(LOG_TAG, "Asked to show notification but not in ECM mode");
176 if (mTimer != null) {
177 mTimer.cancel();
178 }
179 return;
180 }
fionaxu8b7620d2017-05-01 16:22:17 -0700181 final Notification.Builder builder = new Notification.Builder(getApplicationContext());
Tyler Gunn625eb0b2014-08-27 20:37:32 -0700182 builder.setOngoing(true);
183 builder.setPriority(Notification.PRIORITY_HIGH);
184 builder.setSmallIcon(R.drawable.ic_emergency_callback_mode);
185 builder.setTicker(getText(R.string.phone_entered_ecm_text));
186 builder.setContentTitle(getText(R.string.phone_in_ecm_notification_title));
187 builder.setColor(getResources().getColor(R.color.dialer_theme_color));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700188
189 // PendingIntent to launch Emergency Callback Mode Exit activity if the user selects
190 // this notification
191 PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
192 new Intent(EmergencyCallbackModeExitDialog.ACTION_SHOW_ECM_EXIT_DIALOG), 0);
Tyler Gunn625eb0b2014-08-27 20:37:32 -0700193 builder.setContentIntent(contentIntent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700194
195 // Format notification string
196 String text = null;
197 if(mInEmergencyCall) {
198 text = getText(R.string.phone_in_ecm_call_notification_text).toString();
199 } else {
Brad Ebingercd3601b2017-05-09 15:27:27 -0700200 // Calculate the time in ms when the notification will be finished.
201 long finishedCountMs = millisUntilFinished + System.currentTimeMillis();
202 builder.setShowWhen(true);
203 builder.setChronometerCountDown(true);
204 builder.setUsesChronometer(true);
205 builder.setWhen(finishedCountMs);
206
207 String completeTime = SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT).format(
208 finishedCountMs);
209 text = getResources().getString(R.string.phone_in_ecm_notification_complete_time,
210 completeTime);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700211 }
Tyler Gunn625eb0b2014-08-27 20:37:32 -0700212 builder.setContentText(text);
fionaxu8b7620d2017-05-01 16:22:17 -0700213 builder.setChannelId(NotificationChannelController.CHANNEL_ID_ALERT);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700214
215 // Show notification
Tyler Gunn625eb0b2014-08-27 20:37:32 -0700216 mNotificationManager.notify(R.string.phone_in_ecm_notification_title, builder.build());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700217 }
218
219 /**
220 * Handle ECM_TIMER_RESET notification
221 */
222 private void resetEcmTimer(AsyncResult r) {
223 boolean isTimerCanceled = ((Boolean)r.result).booleanValue();
224
225 if (isTimerCanceled) {
226 mInEmergencyCall = true;
227 mTimer.cancel();
228 showNotification(0);
229 } else {
230 mInEmergencyCall = false;
231 startTimerNotification();
232 }
233 }
234
235 @Override
236 public IBinder onBind(Intent intent) {
237 return mBinder;
238 }
239
240 // This is the object that receives interactions from clients.
241 private final IBinder mBinder = new LocalBinder();
242
243 /**
244 * Class for clients to access
245 */
246 public class LocalBinder extends Binder {
247 EmergencyCallbackModeService getService() {
248 return EmergencyCallbackModeService.this;
249 }
250 }
251
252 /**
253 * Returns Emergency Callback Mode timeout value
254 */
255 public long getEmergencyCallbackModeTimeout() {
256 return mTimeLeft;
257 }
258
259 /**
260 * Returns Emergency Callback Mode call state
261 */
262 public boolean getEmergencyCallbackModeCallState() {
263 return mInEmergencyCall;
264 }
265}