1d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/* 2d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Copyright (C) 2015 The Android Open Source Project 3d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * 4d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Licensed under the Apache License, Version 2.0 (the "License"); 5d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * you may not use this file except in compliance with the License. 6d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * You may obtain a copy of the License at 7d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * 8d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * http://www.apache.org/licenses/LICENSE-2.0 9d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * 10d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Unless required by applicable law or agreed to in writing, software 11d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * distributed under the License is distributed on an "AS IS" BASIS, 12d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * See the License for the specific language governing permissions and 14d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * limitations under the License. 15d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */ 16d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpackage com.android.messaging.datamodel; 18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.app.IntentService; 20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.Intent; 21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.net.Uri; 22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.os.Bundle; 23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.support.v4.app.RemoteInput; 24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.telephony.TelephonyManager; 25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.text.TextUtils; 26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.action.InsertNewMessageAction; 28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.action.UpdateMessageNotificationAction; 29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.MessageData; 30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.ParticipantData; 31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.sms.MmsUtils; 32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.ui.UIIntents; 33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.ui.conversationlist.ConversationListActivity; 34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.LogUtil; 35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/** 37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Respond to a special intent and send an SMS message without the user's intervention, unless 38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * the intent extra "showUI" is true. 39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */ 40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class NoConfirmationSmsSendService extends IntentService { 41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private static final String TAG = LogUtil.BUGLE_TAG; 42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private static final String EXTRA_SUBSCRIPTION = "subscription"; 44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public static final String EXTRA_SELF_ID = "self_id"; 45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public NoConfirmationSmsSendService() { 47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Class name will be the thread name. 48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd super(NoConfirmationSmsSendService.class.getName()); 49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Intent should be redelivered if the process gets killed before completing the job. 51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd setIntentRedelivery(true); 52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd @Override 55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd protected void onHandleIntent(final Intent intent) { 56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) { 57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd LogUtil.v(TAG, "NoConfirmationSmsSendService onHandleIntent"); 58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final String action = intent.getAction(); 61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (!TelephonyManager.ACTION_RESPOND_VIA_MESSAGE.equals(action)) { 62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) { 63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd LogUtil.v(TAG, "NoConfirmationSmsSendService onHandleIntent wrong action: " + 64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd action); 65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return; 67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final Bundle extras = intent.getExtras(); 69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (extras == null) { 70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) { 71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd LogUtil.v(TAG, "Called to send SMS but no extras"); 72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return; 74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Get all possible extras from intent 77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final String conversationId = 78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd intent.getStringExtra(UIIntents.UI_INTENT_EXTRA_CONVERSATION_ID); 79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final String selfId = intent.getStringExtra(EXTRA_SELF_ID); 80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final boolean requiresMms = intent.getBooleanExtra(UIIntents.UI_INTENT_EXTRA_REQUIRES_MMS, 81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd false); 82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final String message = getText(intent, Intent.EXTRA_TEXT); 83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final String subject = getText(intent, Intent.EXTRA_SUBJECT); 84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final int subId = extras.getInt(EXTRA_SUBSCRIPTION, ParticipantData.DEFAULT_SELF_SUB_ID); 85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final Uri intentUri = intent.getData(); 87d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final String recipients = intentUri != null ? MmsUtils.getSmsRecipients(intentUri) : null; 88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (TextUtils.isEmpty(recipients) && TextUtils.isEmpty(conversationId)) { 90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) { 91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd LogUtil.v(TAG, "Both conversationId and recipient(s) cannot be empty"); 92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return; 94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (extras.getBoolean("showUI", false)) { 97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd startActivity(new Intent(this, ConversationListActivity.class)); 98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } else { 99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (TextUtils.isEmpty(message)) { 100d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) { 101d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd LogUtil.v(TAG, "Message cannot be empty"); 102d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 103d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return; 104d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 105d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 106d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // TODO: it's possible that a long message would require sending it via mms, 107d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // but we're not testing for that here and we're sending the message as an sms. 108d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 109d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (TextUtils.isEmpty(conversationId)) { 110d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd InsertNewMessageAction.insertNewMessage(subId, recipients, message, subject); 111d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } else { 112d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd MessageData messageData = null; 113d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (requiresMms) { 114d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) { 115d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd LogUtil.v(TAG, "Auto-sending MMS message in conversation: " + 116d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd conversationId); 117d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 118d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd messageData = MessageData.createDraftMmsMessage(conversationId, selfId, message, 119d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd subject); 120d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } else { 121d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) { 122d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd LogUtil.v(TAG, "Auto-sending SMS message in conversation: " + 123d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd conversationId); 124d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 125d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd messageData = MessageData.createDraftSmsMessage(conversationId, selfId, 126d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd message); 127d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 128d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd InsertNewMessageAction.insertNewMessage(messageData); 129d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 130d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd UpdateMessageNotificationAction.updateMessageNotification(); 131d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 132d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 133d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 134d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private String getText(final Intent intent, final String textType) { 135d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final String message = intent.getStringExtra(textType); 136d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (message == null) { 137d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final Bundle remoteInput = RemoteInput.getResultsFromIntent(intent); 138d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (remoteInput != null) { 139d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final CharSequence extra = remoteInput.getCharSequence(textType); 140d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (extra != null) { 141d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return extra.toString(); 142d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 143d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 144d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 145d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return message; 146d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 147d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 148d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd} 149