blob: f06ae0c51a998979e2ac922d29ba2f1eb5f00ac9 [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;
27import android.content.res.Resources;
Tyler Gunn625eb0b2014-08-27 20:37:32 -070028import android.graphics.BitmapFactory;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.os.AsyncResult;
30import android.os.Binder;
31import android.os.CountDownTimer;
32import android.os.Handler;
33import android.os.IBinder;
34import android.os.Message;
35import android.os.SystemProperties;
36import android.util.Log;
37
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import com.android.internal.telephony.Phone;
39import com.android.internal.telephony.PhoneConstants;
40import com.android.internal.telephony.PhoneFactory;
41import com.android.internal.telephony.TelephonyIntents;
42import com.android.internal.telephony.TelephonyProperties;
43
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 */
50public 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() {
76 // Check if it is CDMA phone
Uma Maheswari Ramalingamf16de202014-07-31 16:15:27 -070077 if ((PhoneFactory.getDefaultPhone().getPhoneType() != PhoneConstants.PHONE_TYPE_CDMA)
78 && (PhoneFactory.getDefaultPhone().getImsPhone() == null)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070079 Log.e(LOG_TAG, "Error! Emergency Callback Mode not supported for " +
80 PhoneFactory.getDefaultPhone().getPhoneName() + " phones");
81 stopSelf();
82 }
83
84 // Register receiver for intents
85 IntentFilter filter = new IntentFilter();
86 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
87 filter.addAction(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS);
88 registerReceiver(mEcmReceiver, filter);
89
90 mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
91
92 // Register ECM timer reset notfication
93 mPhone = PhoneFactory.getDefaultPhone();
94 mPhone.registerForEcmTimerReset(mHandler, ECM_TIMER_RESET, null);
95
96 startTimerNotification();
97 }
98
99 @Override
100 public void onDestroy() {
Sandeep Kunta55a6ae82015-12-14 17:49:02 +0530101 if (mPhone != null) {
102 // Unregister receiver
103 unregisterReceiver(mEcmReceiver);
104 // Unregister ECM timer reset notification
105 mPhone.unregisterForEcmTimerReset(mHandler);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700106
Sandeep Kunta55a6ae82015-12-14 17:49:02 +0530107 // Cancel the notification and timer
108 mNotificationManager.cancel(R.string.phone_in_ecm_notification_title);
109 mTimer.cancel();
110 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700111 }
112
113 /**
114 * Listens for Emergency Callback Mode intents
115 */
116 private BroadcastReceiver mEcmReceiver = new BroadcastReceiver() {
117 @Override
118 public void onReceive(Context context, Intent intent) {
119 // Stop the service when phone exits Emergency Callback Mode
120 if (intent.getAction().equals(
121 TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
122 if (intent.getBooleanExtra("phoneinECMState", false) == false) {
123 stopSelf();
124 }
125 }
126 // Show dialog box
127 else if (intent.getAction().equals(
128 TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS)) {
129 context.startActivity(
130 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS)
131 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
132 }
133 }
134 };
135
136 /**
137 * Start timer notification for Emergency Callback Mode
138 */
139 private void startTimerNotification() {
140 // Get Emergency Callback Mode timeout value
141 long ecmTimeout = SystemProperties.getLong(
142 TelephonyProperties.PROPERTY_ECM_EXIT_TIMER, DEFAULT_ECM_EXIT_TIMER_VALUE);
143
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;
156 EmergencyCallbackModeService.this.showNotification(millisUntilFinished);
157 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700158
Yorke Lee34a72cb2014-10-12 13:17:04 -0700159 @Override
160 public void onFinish() {
161 //Do nothing
162 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700163
Yorke Lee34a72cb2014-10-12 13:17:04 -0700164 };
165 }
166 mTimer.start();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700167 }
168
169 /**
170 * Shows notification for Emergency Callback Mode
171 */
172 private void showNotification(long millisUntilFinished) {
Yorke Lee34a72cb2014-10-12 13:17:04 -0700173 final boolean isInEcm = Boolean.parseBoolean(
174 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE));
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 }
Tyler Gunn625eb0b2014-08-27 20:37:32 -0700182 final Notification.Builder builder = new Notification.Builder(getApplicationContext());
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 Cordon7d4ddf62013-07-10 11:58:08 -0700189
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 Gunn625eb0b2014-08-27 20:37:32 -0700194 builder.setContentIntent(contentIntent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700195
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 {
201 int minutes = (int)(millisUntilFinished / 60000);
202 String time = String.format("%d:%02d", minutes, (millisUntilFinished % 60000) / 1000);
203 text = String.format(getResources().getQuantityText(
204 R.plurals.phone_in_ecm_notification_time, minutes).toString(), time);
205 }
Tyler Gunn625eb0b2014-08-27 20:37:32 -0700206 builder.setContentText(text);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700207
208 // Show notification
Tyler Gunn625eb0b2014-08-27 20:37:32 -0700209 mNotificationManager.notify(R.string.phone_in_ecm_notification_title, builder.build());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700210 }
211
212 /**
213 * Handle ECM_TIMER_RESET notification
214 */
215 private void resetEcmTimer(AsyncResult r) {
216 boolean isTimerCanceled = ((Boolean)r.result).booleanValue();
217
218 if (isTimerCanceled) {
219 mInEmergencyCall = true;
220 mTimer.cancel();
221 showNotification(0);
222 } else {
223 mInEmergencyCall = false;
224 startTimerNotification();
225 }
226 }
227
228 @Override
229 public IBinder onBind(Intent intent) {
230 return mBinder;
231 }
232
233 // This is the object that receives interactions from clients.
234 private final IBinder mBinder = new LocalBinder();
235
236 /**
237 * Class for clients to access
238 */
239 public class LocalBinder extends Binder {
240 EmergencyCallbackModeService getService() {
241 return EmergencyCallbackModeService.this;
242 }
243 }
244
245 /**
246 * Returns Emergency Callback Mode timeout value
247 */
248 public long getEmergencyCallbackModeTimeout() {
249 return mTimeLeft;
250 }
251
252 /**
253 * Returns Emergency Callback Mode call state
254 */
255 public boolean getEmergencyCallbackModeCallState() {
256 return mInEmergencyCall;
257 }
258}