Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 17 | package com.android.server.telecom; |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 18 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 19 | // TODO: Needed for move to system service: import com.android.internal.R; |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 20 | import com.android.internal.os.SomeArgs; |
| 21 | import com.android.internal.telephony.SmsApplication; |
| 22 | |
| 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 25 | import android.content.SharedPreferences; |
| 26 | import android.content.res.Resources; |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 27 | import android.os.Handler; |
Ihab Awad | e6dbc9d | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 28 | import android.os.Looper; |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 29 | import android.os.Message; |
Bryce Lee | 45e8eb4 | 2016-07-27 16:57:21 -0700 | [diff] [blame] | 30 | import android.telecom.Connection; |
Brad Ebinger | 953e1af | 2016-10-05 15:45:22 -0700 | [diff] [blame] | 31 | import android.telecom.Log; |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 32 | import android.telecom.Response; |
Hall Liu | ab914a9 | 2016-03-21 15:06:51 -0700 | [diff] [blame] | 33 | import android.telephony.PhoneNumberUtils; |
Brad Ebinger | 2124116 | 2017-05-25 15:35:20 -0700 | [diff] [blame] | 34 | import android.telephony.SmsManager; |
Nancy Chen | 6ae4c99 | 2014-11-06 12:54:39 -0800 | [diff] [blame] | 35 | import android.telephony.SubscriptionManager; |
Hall Liu | ab914a9 | 2016-03-21 15:06:51 -0700 | [diff] [blame] | 36 | import android.text.Spannable; |
| 37 | import android.text.SpannableString; |
Srikanth Chintala | 3802dab | 2017-03-03 18:29:04 +0530 | [diff] [blame] | 38 | import android.text.TextUtils; |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 39 | import android.widget.Toast; |
| 40 | |
| 41 | import java.util.ArrayList; |
| 42 | import java.util.List; |
| 43 | |
| 44 | /** |
| 45 | * Helper class to manage the "Respond via Message" feature for incoming calls. |
| 46 | */ |
| 47 | public class RespondViaSmsManager extends CallsManagerListenerBase { |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 48 | private static final int MSG_SHOW_SENT_TOAST = 2; |
| 49 | |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 50 | private final CallsManager mCallsManager; |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 51 | private final TelecomSystem.SyncRoot mLock; |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 52 | |
Ihab Awad | e6dbc9d | 2015-03-26 10:33:44 -0700 | [diff] [blame] | 53 | private final Handler mHandler = new Handler(Looper.getMainLooper()) { |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 54 | @Override |
| 55 | public void handleMessage(Message msg) { |
| 56 | switch (msg.what) { |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 57 | case MSG_SHOW_SENT_TOAST: { |
| 58 | SomeArgs args = (SomeArgs) msg.obj; |
| 59 | try { |
| 60 | String toastMessage = (String) args.arg1; |
| 61 | Context context = (Context) args.arg2; |
| 62 | showMessageSentToast(toastMessage, context); |
| 63 | } finally { |
| 64 | args.recycle(); |
| 65 | } |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 66 | break; |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 67 | } |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | }; |
| 71 | |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 72 | public RespondViaSmsManager(CallsManager callsManager, TelecomSystem.SyncRoot lock) { |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 73 | mCallsManager = callsManager; |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 74 | mLock = lock; |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 75 | } |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Read the (customizable) canned responses from SharedPreferences, |
| 79 | * or from defaults if the user has never actually brought up |
| 80 | * the Settings UI. |
| 81 | * |
| 82 | * The interface of this method is asynchronous since it does disk I/O. |
| 83 | * |
| 84 | * @param response An object to receive an async reply, which will be called from |
| 85 | * the main thread. |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 86 | * @param context The context. |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 87 | */ |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 88 | public void loadCannedTextMessages(final Response<Void, List<String>> response, |
| 89 | final Context context) { |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 90 | new Thread() { |
| 91 | @Override |
| 92 | public void run() { |
| 93 | Log.d(RespondViaSmsManager.this, "loadCannedResponses() starting"); |
Anthony Lee | c67cd75 | 2014-09-02 23:43:44 -0700 | [diff] [blame] | 94 | |
| 95 | // This function guarantees that QuickResponses will be in our |
| 96 | // SharedPreferences with the proper values considering there may be |
| 97 | // old QuickResponses in Telephony pre L. |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 98 | QuickResponseUtils.maybeMigrateLegacyQuickResponses(context); |
Anthony Lee | c67cd75 | 2014-09-02 23:43:44 -0700 | [diff] [blame] | 99 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 100 | final SharedPreferences prefs = context.getSharedPreferences( |
Santos Cordon | 4ba27e9 | 2014-12-22 15:40:38 -0800 | [diff] [blame] | 101 | QuickResponseUtils.SHARED_PREFERENCES_NAME, |
| 102 | Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 103 | final Resources res = context.getResources(); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 104 | |
Anthony Lee | c67cd75 | 2014-09-02 23:43:44 -0700 | [diff] [blame] | 105 | final ArrayList<String> textMessages = new ArrayList<>( |
| 106 | QuickResponseUtils.NUM_CANNED_RESPONSES); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 107 | |
| 108 | // Note the default values here must agree with the corresponding |
| 109 | // android:defaultValue attributes in respond_via_sms_settings.xml. |
Anthony Lee | c67cd75 | 2014-09-02 23:43:44 -0700 | [diff] [blame] | 110 | textMessages.add(0, prefs.getString(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_1, |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 111 | res.getString(R.string.respond_via_sms_canned_response_1))); |
Anthony Lee | c67cd75 | 2014-09-02 23:43:44 -0700 | [diff] [blame] | 112 | textMessages.add(1, prefs.getString(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_2, |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 113 | res.getString(R.string.respond_via_sms_canned_response_2))); |
Anthony Lee | c67cd75 | 2014-09-02 23:43:44 -0700 | [diff] [blame] | 114 | textMessages.add(2, prefs.getString(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_3, |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 115 | res.getString(R.string.respond_via_sms_canned_response_3))); |
Anthony Lee | c67cd75 | 2014-09-02 23:43:44 -0700 | [diff] [blame] | 116 | textMessages.add(3, prefs.getString(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_4, |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 117 | res.getString(R.string.respond_via_sms_canned_response_4))); |
| 118 | |
| 119 | Log.d(RespondViaSmsManager.this, |
| 120 | "loadCannedResponses() completed, found responses: %s", |
| 121 | textMessages.toString()); |
| 122 | |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 123 | synchronized (mLock) { |
| 124 | response.onResult(null, textMessages); |
| 125 | } |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 126 | } |
| 127 | }.start(); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage) { |
Bryce Lee | ddd966e | 2015-08-28 16:33:50 -0700 | [diff] [blame] | 132 | if (rejectWithMessage |
| 133 | && call.getHandle() != null |
Bryce Lee | 45e8eb4 | 2016-07-27 16:57:21 -0700 | [diff] [blame] | 134 | && !call.can(Connection.CAPABILITY_CAN_SEND_RESPONSE_VIA_CONNECTION)) { |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 135 | int subId = mCallsManager.getPhoneAccountRegistrar().getSubscriptionIdForPhoneAccount( |
Nancy Chen | 6ae4c99 | 2014-11-06 12:54:39 -0800 | [diff] [blame] | 136 | call.getTargetPhoneAccount()); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 137 | rejectCallWithMessage(call.getContext(), call.getHandle().getSchemeSpecificPart(), |
Srikanth Chintala | 3802dab | 2017-03-03 18:29:04 +0530 | [diff] [blame] | 138 | textMessage, subId, call.getName()); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 142 | private void showMessageSentToast(final String phoneNumber, final Context context) { |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 143 | // ...and show a brief confirmation to the user (since |
| 144 | // otherwise it's hard to be sure that anything actually |
| 145 | // happened.) |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 146 | final Resources res = context.getResources(); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 147 | final String formatString = res.getString( |
| 148 | R.string.respond_via_sms_confirmation_format); |
| 149 | final String confirmationMsg = String.format(formatString, phoneNumber); |
Hall Liu | ab914a9 | 2016-03-21 15:06:51 -0700 | [diff] [blame] | 150 | int startingPosition = confirmationMsg.indexOf(phoneNumber); |
| 151 | int endingPosition = startingPosition + phoneNumber.length(); |
| 152 | |
| 153 | Spannable styledConfirmationMsg = new SpannableString(confirmationMsg); |
| 154 | PhoneNumberUtils.addTtsSpan(styledConfirmationMsg, startingPosition, endingPosition); |
| 155 | Toast.makeText(context, styledConfirmationMsg, |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 156 | Toast.LENGTH_LONG).show(); |
| 157 | |
| 158 | // TODO: If the device is locked, this toast won't actually ever |
| 159 | // be visible! (That's because we're about to dismiss the call |
| 160 | // screen, which means that the device will return to the |
| 161 | // keyguard. But toasts aren't visible on top of the keyguard.) |
| 162 | // Possible fixes: |
| 163 | // (1) Is it possible to allow a specific Toast to be visible |
| 164 | // on top of the keyguard? |
| 165 | // (2) Artificially delay the dismissCallScreen() call by 3 |
| 166 | // seconds to allow the toast to be seen? |
| 167 | // (3) Don't use a toast at all; instead use a transient state |
| 168 | // of the InCallScreen (perhaps via the InCallUiState |
| 169 | // progressIndication feature), and have that state be |
| 170 | // visible for 3 seconds before calling dismissCallScreen(). |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Reject the call with the specified message. If message is null this call is ignored. |
| 175 | */ |
Nancy Chen | 6ae4c99 | 2014-11-06 12:54:39 -0800 | [diff] [blame] | 176 | private void rejectCallWithMessage(Context context, String phoneNumber, String textMessage, |
Srikanth Chintala | 3802dab | 2017-03-03 18:29:04 +0530 | [diff] [blame] | 177 | int subId, String contactName) { |
Brad Ebinger | 2124116 | 2017-05-25 15:35:20 -0700 | [diff] [blame] | 178 | if (TextUtils.isEmpty(textMessage)) { |
| 179 | Log.w(RespondViaSmsManager.this, "Couldn't send SMS message: empty text message. "); |
| 180 | return; |
| 181 | } |
| 182 | if (!SubscriptionManager.isValidSubscriptionId(subId)) { |
| 183 | Log.w(RespondViaSmsManager.this, "Couldn't send SMS message: Invalid SubId: " + |
| 184 | subId); |
| 185 | return; |
| 186 | } |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 187 | |
Brad Ebinger | 2124116 | 2017-05-25 15:35:20 -0700 | [diff] [blame] | 188 | SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId); |
| 189 | try { |
| 190 | smsManager.sendTextMessage(phoneNumber, null, textMessage, null /*sentIntent*/, |
| 191 | null /*deliveryIntent*/); |
| 192 | |
| 193 | SomeArgs args = SomeArgs.obtain(); |
| 194 | args.arg1 = !TextUtils.isEmpty(contactName) ? contactName : phoneNumber; |
| 195 | args.arg2 = context; |
| 196 | mHandler.obtainMessage(MSG_SHOW_SENT_TOAST, args).sendToTarget(); |
| 197 | } catch (IllegalArgumentException e) { |
| 198 | Log.w(RespondViaSmsManager.this, "Couldn't send SMS message: " + |
| 199 | e.getMessage()); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | } |