| /* |
| * Copyright (C) 2011 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.android.server.telecom; |
| |
| import android.app.Activity; |
| import android.app.PendingIntent; |
| import android.content.BroadcastReceiver; |
| import android.content.Context; |
| import android.content.Intent; |
| import android.content.IntentFilter; |
| import android.content.SharedPreferences; |
| import android.os.Handler; |
| import android.telecom.Connection; |
| import android.telecom.Log; |
| import android.telecom.Logging.Session; |
| import android.telephony.PhoneNumberUtils; |
| import android.telephony.SmsManager; |
| import android.telephony.SubscriptionManager; |
| import android.text.BidiFormatter; |
| import android.text.Spannable; |
| import android.text.SpannableString; |
| import android.text.TextUtils; |
| import android.widget.Toast; |
| |
| import com.android.server.telecom.ui.UiConstants; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| import java.util.concurrent.CompletableFuture; |
| import java.util.concurrent.Executor; |
| |
| /** |
| * Helper class to manage the "Respond via Message" feature for incoming calls. |
| */ |
| public class RespondViaSmsManager extends CallsManagerListenerBase { |
| private static final String ACTION_MESSAGE_SENT = "com.android.server.telecom.MESSAGE_SENT"; |
| |
| private static final class MessageSentReceiver extends BroadcastReceiver { |
| private final String mContactName; |
| private final int mNumMessageParts; |
| private int mNumMessagesSent = 0; |
| MessageSentReceiver(String contactName, int numMessageParts) { |
| mContactName = contactName; |
| mNumMessageParts = numMessageParts; |
| } |
| |
| @Override |
| public void onReceive(Context context, Intent intent) { |
| if (getResultCode() == Activity.RESULT_OK) { |
| mNumMessagesSent++; |
| if (mNumMessagesSent == mNumMessageParts) { |
| showMessageResultToast(mContactName, context, true); |
| context.unregisterReceiver(this); |
| } |
| } else { |
| context.unregisterReceiver(this); |
| showMessageResultToast(mContactName, context, false); |
| Log.w(RespondViaSmsManager.class.getSimpleName(), |
| "Message failed with error %s", getResultCode()); |
| } |
| } |
| } |
| |
| private final CallsManager mCallsManager; |
| private final TelecomSystem.SyncRoot mLock; |
| private final Executor mAsyncExecutor; |
| |
| public RespondViaSmsManager(CallsManager callsManager, TelecomSystem.SyncRoot lock, |
| Executor asyncExecutor) { |
| mCallsManager = callsManager; |
| mLock = lock; |
| mAsyncExecutor = asyncExecutor; |
| |
| BroadcastReceiver receiver = new BroadcastReceiver() { |
| @Override |
| public void onReceive(Context context, Intent intent) { |
| if (QuickResponseUtils.ACTION_UPDATE_CANNED_TEXT_MESSAGES.equals( |
| intent.getAction())) { |
| Log.i(RespondViaSmsManager.this, "Received canned text messages update"); |
| updateCannedTextMessages(intent, context); |
| } |
| } |
| }; |
| IntentFilter intentFilter = |
| new IntentFilter(QuickResponseUtils.ACTION_UPDATE_CANNED_TEXT_MESSAGES); |
| mCallsManager.getContext().registerReceiver(receiver, intentFilter, |
| UiConstants.TELECOM_UI_ACCESS_PERMISSION, null, |
| Context.RECEIVER_EXPORTED); |
| } |
| |
| private void updateCannedTextMessages(Intent intent, Context context) { |
| SharedPreferences prefs = context.getSharedPreferences( |
| QuickResponseUtils.SHARED_PREFERENCES_NAME, |
| Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS); |
| SharedPreferences.Editor editor = prefs.edit(); |
| |
| if (intent.hasExtra(QuickResponseUtils.EXTRA_CANNED_RESPONSE_1)) { |
| editor.putString(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_1, |
| intent.getStringExtra(QuickResponseUtils.EXTRA_CANNED_RESPONSE_1)); |
| } else { |
| editor.remove(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_1); |
| } |
| if (intent.hasExtra(QuickResponseUtils.EXTRA_CANNED_RESPONSE_2)) { |
| editor.putString(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_2, |
| intent.getStringExtra(QuickResponseUtils.EXTRA_CANNED_RESPONSE_2)); |
| } else { |
| editor.remove(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_2); |
| } |
| if (intent.hasExtra(QuickResponseUtils.EXTRA_CANNED_RESPONSE_3)) { |
| editor.putString(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_3, |
| intent.getStringExtra(QuickResponseUtils.EXTRA_CANNED_RESPONSE_3)); |
| } else { |
| editor.remove(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_3); |
| } |
| if (intent.hasExtra(QuickResponseUtils.EXTRA_CANNED_RESPONSE_4)) { |
| editor.putString(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_4, |
| intent.getStringExtra(QuickResponseUtils.EXTRA_CANNED_RESPONSE_4)); |
| } else { |
| editor.remove(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_4); |
| } |
| editor.commit(); |
| } |
| |
| /** |
| * Read the (customizable) canned responses from SharedPreferences, |
| * or from defaults if the user has never actually brought up |
| * the Settings UI. |
| * |
| * The interface of this method is asynchronous since it does disk I/O. |
| * |
| * @param response An object to receive an async reply, which will be called from |
| * the main thread. |
| * @param context The context. |
| */ |
| public void loadCannedTextMessages(final CallsManager.Response<Void, List<String>> response, |
| final Context context) { |
| CompletableFuture<List<String>> cannedTextMessages = new CompletableFuture<>(); |
| Session s = Log.createSubsession(); |
| mAsyncExecutor.execute(() -> { |
| try { |
| Log.continueSession(s, "RVSM.lCTM.e"); |
| cannedTextMessages.complete(loadCannedTextMessages(context)); |
| } finally { |
| Log.endSession(); |
| } |
| }); |
| cannedTextMessages.whenCompleteAsync((result, exception) -> { |
| if (exception != null) { |
| Log.e(RespondViaSmsManager.class.getSimpleName(), exception, |
| "loadCannedTextMessages failed"); |
| response.onError(null, -1, exception.toString()); |
| } else { |
| response.onResult(null, result); |
| } |
| }, new LoggedHandlerExecutor(new Handler(context.getMainLooper()), "RVSM.lCTM.c", mLock)); |
| } |
| |
| private List<String> loadCannedTextMessages(final Context context) { |
| Log.d(RespondViaSmsManager.this, "loadCannedTextMessages() starting"); |
| // This function guarantees that QuickResponses will be in our |
| // SharedPreferences with the proper values considering there may be |
| // old QuickResponses in Telephony pre L. |
| QuickResponseUtils.maybeMigrateLegacyQuickResponses(context); |
| |
| final SharedPreferences prefs = context.getSharedPreferences( |
| QuickResponseUtils.SHARED_PREFERENCES_NAME, |
| Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS); |
| |
| final ArrayList<String> textMessages = new ArrayList<>( |
| QuickResponseUtils.NUM_CANNED_RESPONSES); |
| |
| // Where the user has changed a quick response back to the same text as the |
| // original text, clear the shared pref. This ensures we always load the resource |
| // in the current active language. |
| QuickResponseUtils.maybeResetQuickResponses(context, prefs); |
| |
| // Note the default values here must agree with the corresponding |
| // android:defaultValue attributes in respond_via_sms_settings.xml. |
| textMessages.add(0, prefs.getString(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_1, |
| TelecomResourceId.getString(context, "respond_via_sms_canned_response_1"))); |
| textMessages.add(1, prefs.getString(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_2, |
| TelecomResourceId.getString(context, "respond_via_sms_canned_response_2"))); |
| textMessages.add(2, prefs.getString(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_3, |
| TelecomResourceId.getString(context, "respond_via_sms_canned_response_3"))); |
| textMessages.add(3, prefs.getString(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_4, |
| TelecomResourceId.getString(context, "respond_via_sms_canned_response_4"))); |
| |
| Log.d(RespondViaSmsManager.this, |
| "loadCannedResponses() completed, found responses: %s", |
| textMessages.toString()); |
| return textMessages; |
| } |
| |
| @Override |
| public void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage) { |
| if (rejectWithMessage |
| && call.getHandle() != null |
| && !call.can(Connection.CAPABILITY_CAN_SEND_RESPONSE_VIA_CONNECTION)) { |
| int subId = mCallsManager.getPhoneAccountRegistrar().getSubscriptionIdForPhoneAccount( |
| call.getTargetPhoneAccount()); |
| rejectCallWithMessage(call.getContext(), call.getHandle().getSchemeSpecificPart(), |
| textMessage, subId, call.getName()); |
| } |
| } |
| |
| private static void showMessageResultToast(final String phoneNumber, |
| final Context context, boolean success) { |
| // ...and show a brief confirmation to the user (since |
| // otherwise it's hard to be sure that anything actually |
| // happened.) |
| final String formatString = TelecomResourceId.getString(context, success |
| ? "respond_via_sms_confirmation_format" |
| : "respond_via_sms_failure_format"); |
| final BidiFormatter phoneNumberFormatter = BidiFormatter.getInstance(); |
| final String confirmationMsg = String.format(formatString, |
| phoneNumberFormatter.unicodeWrap(phoneNumber)); |
| int startingPosition = confirmationMsg.indexOf(phoneNumber); |
| int endingPosition = startingPosition + phoneNumber.length(); |
| |
| Spannable styledConfirmationMsg = new SpannableString(confirmationMsg); |
| PhoneNumberUtils.addTtsSpan(styledConfirmationMsg, startingPosition, endingPosition); |
| Toast.makeText(context, styledConfirmationMsg, |
| Toast.LENGTH_LONG).show(); |
| |
| // TODO: If the device is locked, this toast won't actually ever |
| // be visible! (That's because we're about to dismiss the call |
| // screen, which means that the device will return to the |
| // keyguard. But toasts aren't visible on top of the keyguard.) |
| // Possible fixes: |
| // (1) Is it possible to allow a specific Toast to be visible |
| // on top of the keyguard? |
| // (2) Artificially delay the dismissCallScreen() call by 3 |
| // seconds to allow the toast to be seen? |
| // (3) Don't use a toast at all; instead use a transient state |
| // of the InCallScreen (perhaps via the InCallUiState |
| // progressIndication feature), and have that state be |
| // visible for 3 seconds before calling dismissCallScreen(). |
| } |
| |
| /** |
| * Reject the call with the specified message. If message is null this call is ignored. |
| */ |
| private void rejectCallWithMessage(Context context, String phoneNumber, String textMessage, |
| int subId, String contactName) { |
| if (TextUtils.isEmpty(textMessage)) { |
| Log.w(RespondViaSmsManager.this, "Couldn't send SMS message: empty text message. "); |
| return; |
| } |
| if (!SubscriptionManager.isValidSubscriptionId(subId)) { |
| Log.w(RespondViaSmsManager.this, "Couldn't send SMS message: Invalid SubId: " + |
| subId); |
| return; |
| } |
| Session s = Log.createSubsession(); |
| mAsyncExecutor.execute(() -> { |
| try { |
| Log.continueSession(s, "RVSM.rCWM.e"); |
| sendTextMessage(context, phoneNumber, textMessage, subId, contactName); |
| } finally { |
| Log.endSession(); |
| } |
| }); |
| } |
| |
| private void sendTextMessage(Context context, String phoneNumber, String textMessage, |
| int subId, String contactName) { |
| SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId); |
| try { |
| ArrayList<String> messageParts = smsManager.divideMessage(textMessage); |
| ArrayList<PendingIntent> sentIntents = new ArrayList<>(messageParts.size()); |
| for (int i = 0; i < messageParts.size(); i++) { |
| Intent intent = new Intent(ACTION_MESSAGE_SENT); |
| PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, intent, |
| PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE); |
| sentIntents.add(pendingIntent); |
| } |
| |
| MessageSentReceiver receiver = new MessageSentReceiver( |
| !TextUtils.isEmpty(contactName) ? contactName : phoneNumber, |
| messageParts.size()); |
| IntentFilter messageSentFilter = new IntentFilter(ACTION_MESSAGE_SENT); |
| messageSentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
| context.registerReceiver(receiver, messageSentFilter, Context.RECEIVER_NOT_EXPORTED); |
| smsManager.sendMultipartTextMessage(phoneNumber, null, messageParts, |
| sentIntents/*sentIntent*/, null /*deliveryIntent*/, context.getOpPackageName(), |
| context.getAttributionTag()); |
| } catch (IllegalArgumentException e) { |
| Log.w(RespondViaSmsManager.this, "Couldn't send SMS message: " + |
| e.getMessage()); |
| } |
| } |
| } |