1 /*
2  * Copyright (C) 2015 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 
17 package com.android.messaging.ui.conversation;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.net.Uri;
23 import android.os.Bundle;
24 import android.text.TextUtils;
25 
26 import com.android.messaging.Factory;
27 import com.android.messaging.R;
28 import com.android.messaging.datamodel.DataModel;
29 import com.android.messaging.datamodel.binding.Binding;
30 import com.android.messaging.datamodel.binding.BindingBase;
31 import com.android.messaging.datamodel.data.LaunchConversationData;
32 import com.android.messaging.ui.UIIntents;
33 import com.android.messaging.util.ContentType;
34 import com.android.messaging.util.LogUtil;
35 import com.android.messaging.util.UiUtils;
36 import com.android.messaging.util.UriUtil;
37 
38 import java.io.UnsupportedEncodingException;
39 import java.net.URLDecoder;
40 
41 /**
42  * Launches ConversationActivity for sending a message to, or viewing messages from, a specific
43  * recipient.
44  * <p>
45  * (This activity should be marked noHistory="true" in AndroidManifest.xml)
46  */
47 public class LaunchConversationActivity extends Activity implements
48         LaunchConversationData.LaunchConversationDataListener {
49     static final String SMS_BODY = "sms_body";
50     static final String ADDRESS = "address";
51     final Binding<LaunchConversationData> mBinding = BindingBase.createBinding(this);
52     String mSmsBody;
53 
54     @Override
onCreate(final Bundle savedInstanceState)55     protected void onCreate(final Bundle savedInstanceState) {
56         super.onCreate(savedInstanceState);
57         if (UiUtils.redirectToPermissionCheckIfNeeded(this)) {
58             return;
59         }
60 
61         final Intent intent = getIntent();
62         final String action = intent.getAction();
63         if (Intent.ACTION_SENDTO.equals(action) || Intent.ACTION_VIEW.equals(action)) {
64             String[] recipients = null;
65             final String commaSeparatedRecipients =
66                     UriUtil.parseRecipientsFromSmsMmsUri(intent.getData());
67             if (commaSeparatedRecipients != null) {
68                 recipients = commaSeparatedRecipients.split(",");
69             }
70             final boolean haveAddress = !TextUtils.isEmpty(intent.getStringExtra(ADDRESS));
71             final boolean haveEmail = !TextUtils.isEmpty(intent.getStringExtra(Intent.EXTRA_EMAIL));
72             if (recipients == null && (haveAddress || haveEmail)) {
73                 if (haveAddress) {
74                     recipients = new String[] { intent.getStringExtra(ADDRESS) };
75                 } else {
76                     recipients = new String[] { intent.getStringExtra(Intent.EXTRA_EMAIL) };
77                 }
78             }
79             mSmsBody = intent.getStringExtra(SMS_BODY);
80             if (TextUtils.isEmpty(mSmsBody)) {
81                 // Used by intents sent from the web YouTube (and perhaps others).
82                 mSmsBody = getBody(intent.getData());
83                 if (TextUtils.isEmpty(mSmsBody)) {
84                     // If that fails, try yet another method apps use to share text
85                     if (ContentType.TEXT_PLAIN.equals(intent.getType())) {
86                         mSmsBody = intent.getStringExtra(Intent.EXTRA_TEXT);
87                     }
88                 }
89             }
90             if (recipients != null) {
91                 mBinding.bind(DataModel.get().createLaunchConversationData(this));
92                 mBinding.getData().getOrCreateConversation(mBinding, recipients);
93             } else {
94                 // No recipients were specified in the intent.
95                 // Start a new conversation with contact picker. The new conversation will be
96                 // primed with the (optional) message in mSmsBody.
97                 onGetOrCreateNewConversation(null);
98             }
99         } else {
100             LogUtil.w(LogUtil.BUGLE_TAG, "Unsupported conversation intent action : " + action);
101         }
102         // As of M, activities without a visible window must finish before onResume completes.
103         finish();
104     }
105 
getBody(final Uri uri)106     private String getBody(final Uri uri) {
107         if (uri == null) {
108             return null;
109         }
110         String urlStr = uri.getSchemeSpecificPart();
111         if (!urlStr.contains("?")) {
112             return null;
113         }
114         urlStr = urlStr.substring(urlStr.indexOf('?') + 1);
115         final String[] params = urlStr.split("&");
116         for (final String p : params) {
117             if (p.startsWith("body=")) {
118                 try {
119                     return URLDecoder.decode(p.substring(5), "UTF-8");
120                 } catch (final UnsupportedEncodingException e) {
121                     // Invalid URL, ignore
122                 }
123             }
124         }
125         return null;
126     }
127 
128     @Override
onGetOrCreateNewConversation(final String conversationId)129     public void onGetOrCreateNewConversation(final String conversationId) {
130         final Context context = Factory.get().getApplicationContext();
131         UIIntents.get().launchConversationActivityWithParentStack(context, conversationId,
132                 mSmsBody);
133     }
134 
135     @Override
onGetOrCreateNewConversationFailed()136     public void onGetOrCreateNewConversationFailed() {
137         UiUtils.showToastAtBottom(R.string.conversation_creation_failure);
138     }
139 }
140