1 /* 2 * Copyright (C) 2019 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.example.android.sharingshortcuts; 18 19 import android.app.Activity; 20 import android.content.Intent; 21 import android.os.Build; 22 import android.os.Bundle; 23 import android.view.View; 24 import android.widget.TextView; 25 import android.widget.Toast; 26 27 /** 28 * Provides the UI for sharing a text with a {@link Contact}. 29 */ 30 public class SendMessageActivity extends Activity { 31 32 /** 33 * The request code for {@link SelectContactActivity}. This is used when the user doesn't 34 * select any of Direct Share icons. 35 */ 36 private static final int REQUEST_SELECT_CONTACT = 1; 37 38 /** 39 * The text to share. 40 */ 41 private String mBody; 42 43 /** 44 * The ID of the contact to share the text with. 45 */ 46 private int mContactId; 47 48 // View references. 49 private TextView mTextContactName; 50 private TextView mTextMessageBody; 51 52 @Override 53 protected void onCreate(Bundle savedInstanceState) { 54 super.onCreate(savedInstanceState); 55 setContentView(R.layout.activity_send_message); 56 setTitle(R.string.sending_message); 57 // View references. 58 mTextContactName = findViewById(R.id.contact_name); 59 mTextMessageBody = findViewById(R.id.message_body); 60 // Handle the share Intent. 61 boolean handled = handleIntent(getIntent()); 62 if (!handled) { 63 finish(); 64 return; 65 } 66 // Bind event handlers. 67 findViewById(R.id.send).setOnClickListener(mOnClickListener); 68 // Set up the UI. 69 prepareUi(); 70 // The contact ID will not be passed on when the user clicks on the app icon rather than any 71 // of the Direct Share icons. In this case, we show another dialog for selecting a contact. 72 if (mContactId == Contact.INVALID_ID) { 73 selectContact(); 74 } 75 } 76 77 @Override 78 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 79 switch (requestCode) { 80 case REQUEST_SELECT_CONTACT: 81 if (resultCode == RESULT_OK) { 82 mContactId = data.getIntExtra(Contact.ID, Contact.INVALID_ID); 83 } 84 // Give up sharing the send_message if the user didn't choose a contact. 85 if (mContactId == Contact.INVALID_ID) { 86 finish(); 87 return; 88 } 89 prepareUi(); 90 break; 91 default: 92 super.onActivityResult(requestCode, resultCode, data); 93 } 94 } 95 96 /** 97 * Handles the passed {@link Intent}. This method can only handle intents for sharing a plain 98 * text. {@link #mBody} and {@link #mContactId} are modified accordingly. 99 * 100 * @param intent The {@link Intent}. 101 * @return true if the {@code intent} is handled properly. 102 */ 103 private boolean handleIntent(Intent intent) { 104 if (Intent.ACTION_SEND.equals(intent.getAction()) 105 && "text/plain".equals(intent.getType())) { 106 mBody = intent.getStringExtra(Intent.EXTRA_TEXT); 107 // The intent comes from Direct share 108 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P 109 && intent.hasExtra(Intent.EXTRA_SHORTCUT_ID)) { 110 String shortcutId = intent.getStringExtra(Intent.EXTRA_SHORTCUT_ID); 111 mContactId = Integer.valueOf(shortcutId); 112 } else { 113 // The text was shared and the user chose our app 114 mContactId = Contact.INVALID_ID; 115 } 116 return true; 117 } 118 return false; 119 } 120 121 /** 122 * Sets up the UI. 123 */ 124 private void prepareUi() { 125 if (mContactId != Contact.INVALID_ID) { 126 Contact contact = Contact.byId(mContactId); 127 ContactViewBinder.bind(contact, mTextContactName); 128 } 129 mTextMessageBody.setText(mBody); 130 } 131 132 /** 133 * Delegates selection of a {@Contact} to {@link SelectContactActivity}. 134 */ 135 private void selectContact() { 136 Intent intent = new Intent(this, SelectContactActivity.class); 137 intent.setAction(SelectContactActivity.ACTION_SELECT_CONTACT); 138 startActivityForResult(intent, REQUEST_SELECT_CONTACT); 139 } 140 141 private View.OnClickListener mOnClickListener = new View.OnClickListener() { 142 @Override 143 public void onClick(View view) { 144 switch (view.getId()) { 145 case R.id.send: 146 send(); 147 break; 148 } 149 } 150 }; 151 152 /** 153 * Pretends to send the text to the contact. This only shows a dummy message. 154 */ 155 private void send() { 156 Toast.makeText(this, 157 getString(R.string.message_sent, mBody, Contact.byId(mContactId).getName()), 158 Toast.LENGTH_SHORT).show(); 159 finish(); 160 } 161 162 } 163