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