blob: e1f7fb34dc446b1ce17b17561d0d461dc238bfee [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
38import com.android.internal.telephony.cdma.CDMAPhone;
39import com.android.internal.telephony.Phone;
40import com.android.internal.telephony.PhoneConstants;
41import com.android.internal.telephony.PhoneFactory;
42import com.android.internal.telephony.TelephonyIntents;
43import com.android.internal.telephony.TelephonyProperties;
44
45/**
46 * Application service that inserts/removes Emergency Callback Mode notification and
47 * updates Emergency Callback Mode countdown clock in the notification
48 *
49 * @see EmergencyCallbackModeExitDialog
50 */
51public class EmergencyCallbackModeService extends Service {
52
53 // Default Emergency Callback Mode timeout value
54 private static final int DEFAULT_ECM_EXIT_TIMER_VALUE = 300000;
55 private static final String LOG_TAG = "EmergencyCallbackModeService";
56
57 private NotificationManager mNotificationManager = null;
58 private CountDownTimer mTimer = null;
59 private long mTimeLeft = 0;
60 private Phone mPhone = null;
61 private boolean mInEmergencyCall = false;
62
63 private static final int ECM_TIMER_RESET = 1;
64
65 private Handler mHandler = new Handler () {
66 public void handleMessage(Message msg) {
67 switch (msg.what) {
68 case ECM_TIMER_RESET:
69 resetEcmTimer((AsyncResult) msg.obj);
70 break;
71 }
72 }
73 };
74
75 @Override
76 public void onCreate() {
77 // Check if it is CDMA phone
Uma Maheswari Ramalingamf16de202014-07-31 16:15:27 -070078 if ((PhoneFactory.getDefaultPhone().getPhoneType() != PhoneConstants.PHONE_TYPE_CDMA)
79 && (PhoneFactory.getDefaultPhone().getImsPhone() == null)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070080 Log.e(LOG_TAG, "Error! Emergency Callback Mode not supported for " +
81 PhoneFactory.getDefaultPhone().getPhoneName() + " phones");
82 stopSelf();
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
94 mPhone = PhoneFactory.getDefaultPhone();
95 mPhone.registerForEcmTimerReset(mHandler, ECM_TIMER_RESET, null);
96
97 startTimerNotification();
98 }
99
100 @Override
101 public void onDestroy() {
102 // Unregister receiver
103 unregisterReceiver(mEcmReceiver);
104 // Unregister ECM timer reset notification
105 mPhone.unregisterForEcmTimerReset(mHandler);
106
107 // Cancel the notification and timer
108 mNotificationManager.cancel(R.string.phone_in_ecm_notification_title);
109 mTimer.cancel();
110 }
111
112 /**
113 * Listens for Emergency Callback Mode intents
114 */
115 private BroadcastReceiver mEcmReceiver = new BroadcastReceiver() {
116 @Override
117 public void onReceive(Context context, Intent intent) {
118 // Stop the service when phone exits Emergency Callback Mode
119 if (intent.getAction().equals(
120 TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
121 if (intent.getBooleanExtra("phoneinECMState", false) == false) {
122 stopSelf();
123 }
124 }
125 // Show dialog box
126 else if (intent.getAction().equals(
127 TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS)) {
128 context.startActivity(
129 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS)
130 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
131 }
132 }
133 };
134
135 /**
136 * Start timer notification for Emergency Callback Mode
137 */
138 private void startTimerNotification() {
139 // Get Emergency Callback Mode timeout value
140 long ecmTimeout = SystemProperties.getLong(
141 TelephonyProperties.PROPERTY_ECM_EXIT_TIMER, DEFAULT_ECM_EXIT_TIMER_VALUE);
142
143 // Show the notification
144 showNotification(ecmTimeout);
145
146 // Start countdown timer for the notification updates
147 mTimer = new CountDownTimer(ecmTimeout, 1000) {
148
149 @Override
150 public void onTick(long millisUntilFinished) {
151 mTimeLeft = millisUntilFinished;
152 EmergencyCallbackModeService.this.showNotification(millisUntilFinished);
153 }
154
155 @Override
156 public void onFinish() {
157 //Do nothing
158 }
159
160 }.start();
161 }
162
163 /**
164 * Shows notification for Emergency Callback Mode
165 */
166 private void showNotification(long millisUntilFinished) {
Tyler Gunn625eb0b2014-08-27 20:37:32 -0700167 final Notification.Builder builder = new Notification.Builder(getApplicationContext());
168 builder.setOngoing(true);
169 builder.setPriority(Notification.PRIORITY_HIGH);
170 builder.setSmallIcon(R.drawable.ic_emergency_callback_mode);
171 builder.setTicker(getText(R.string.phone_entered_ecm_text));
172 builder.setContentTitle(getText(R.string.phone_in_ecm_notification_title));
173 builder.setColor(getResources().getColor(R.color.dialer_theme_color));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700174
175 // PendingIntent to launch Emergency Callback Mode Exit activity if the user selects
176 // this notification
177 PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
178 new Intent(EmergencyCallbackModeExitDialog.ACTION_SHOW_ECM_EXIT_DIALOG), 0);
Tyler Gunn625eb0b2014-08-27 20:37:32 -0700179 builder.setContentIntent(contentIntent);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700180
181 // Format notification string
182 String text = null;
183 if(mInEmergencyCall) {
184 text = getText(R.string.phone_in_ecm_call_notification_text).toString();
185 } else {
186 int minutes = (int)(millisUntilFinished / 60000);
187 String time = String.format("%d:%02d", minutes, (millisUntilFinished % 60000) / 1000);
188 text = String.format(getResources().getQuantityText(
189 R.plurals.phone_in_ecm_notification_time, minutes).toString(), time);
190 }
Tyler Gunn625eb0b2014-08-27 20:37:32 -0700191 builder.setContentText(text);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700192
193 // Show notification
Tyler Gunn625eb0b2014-08-27 20:37:32 -0700194 mNotificationManager.notify(R.string.phone_in_ecm_notification_title, builder.build());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700195 }
196
197 /**
198 * Handle ECM_TIMER_RESET notification
199 */
200 private void resetEcmTimer(AsyncResult r) {
201 boolean isTimerCanceled = ((Boolean)r.result).booleanValue();
202
203 if (isTimerCanceled) {
204 mInEmergencyCall = true;
205 mTimer.cancel();
206 showNotification(0);
207 } else {
208 mInEmergencyCall = false;
209 startTimerNotification();
210 }
211 }
212
213 @Override
214 public IBinder onBind(Intent intent) {
215 return mBinder;
216 }
217
218 // This is the object that receives interactions from clients.
219 private final IBinder mBinder = new LocalBinder();
220
221 /**
222 * Class for clients to access
223 */
224 public class LocalBinder extends Binder {
225 EmergencyCallbackModeService getService() {
226 return EmergencyCallbackModeService.this;
227 }
228 }
229
230 /**
231 * Returns Emergency Callback Mode timeout value
232 */
233 public long getEmergencyCallbackModeTimeout() {
234 return mTimeLeft;
235 }
236
237 /**
238 * Returns Emergency Callback Mode call state
239 */
240 public boolean getEmergencyCallbackModeCallState() {
241 return mInEmergencyCall;
242 }
243}