aboutsummaryrefslogtreecommitdiff
path: root/content/DirectShare/Application/src/main/java/com/example/android/directshare/SendMessageActivity.java
blob: 39826d4f9e558f0480728abc8a5836807164223a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Copyright (C) 2015 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.example.android.directshare;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Provides the UI for sharing a text with a {@link Contact}.
 */
public class SendMessageActivity extends Activity {

    /**
     * The request code for {@link SelectContactActivity}. This is used when the user doesn't select
     * any of Direct Share icons.
     */
    private static final int REQUEST_SELECT_CONTACT = 1;

    /**
     * The text to share.
     */
    private String mBody;

    /**
     * The ID of the contact to share the text with.
     */
    private int mContactId;

    // View references.
    private TextView mTextContactName;
    private TextView mTextMessageBody;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.send_message);
        setTitle(R.string.sending_message);
        // View references.
        mTextContactName = (TextView) findViewById(R.id.contact_name);
        mTextMessageBody = (TextView) findViewById(R.id.message_body);
        // Resolve the share Intent.
        boolean resolved = resolveIntent(getIntent());
        if (!resolved) {
            finish();
            return;
        }
        // Bind event handlers.
        findViewById(R.id.send).setOnClickListener(mOnClickListener);
        // Set up the UI.
        prepareUi();
        // The contact ID will not be passed on when the user clicks on the app icon rather than any
        // of the Direct Share icons. In this case, we show another dialog for selecting a contact.
        if (mContactId == Contact.INVALID_ID) {
            selectContact();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_SELECT_CONTACT:
                if (resultCode == RESULT_OK) {
                    mContactId = data.getIntExtra(Contact.ID, Contact.INVALID_ID);
                }
                // Give up sharing the send_message if the user didn't choose a contact.
                if (mContactId == Contact.INVALID_ID) {
                    finish();
                    return;
                }
                prepareUi();
                break;
            default:
                super.onActivityResult(requestCode, resultCode, data);
        }
    }

    /**
     * Resolves the passed {@link Intent}. This method can only resolve intents for sharing a plain
     * text. {@link #mBody} and {@link #mContactId} are modified accordingly.
     *
     * @param intent The {@link Intent}.
     * @return True if the {@code intent} is resolved properly.
     */
    private boolean resolveIntent(Intent intent) {
        if (Intent.ACTION_SEND.equals(intent.getAction()) &&
                "text/plain".equals(intent.getType())) {
            mBody = intent.getStringExtra(Intent.EXTRA_TEXT);
            mContactId = intent.getIntExtra(Contact.ID, Contact.INVALID_ID);
            return true;
        }
        return false;
    }

    /**
     * Sets up the UI.
     */
    private void prepareUi() {
        if (mContactId != Contact.INVALID_ID) {
            Contact contact = Contact.byId(mContactId);
            ContactViewBinder.bind(contact, mTextContactName);
        }
        mTextMessageBody.setText(mBody);
    }

    /**
     * Delegates selection of a {@Contact} to {@link SelectContactActivity}.
     */
    private void selectContact() {
        Intent intent = new Intent(this, SelectContactActivity.class);
        intent.setAction(SelectContactActivity.ACTION_SELECT_CONTACT);
        startActivityForResult(intent, REQUEST_SELECT_CONTACT);
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.send:
                    send();
                    break;
            }
        }
    };

    /**
     * Pretends to send the text to the contact. This only shows a placeholder message.
     */
    private void send() {
        Toast.makeText(this,
                getString(R.string.message_sent, mBody, Contact.byId(mContactId).getName()),
                Toast.LENGTH_SHORT).show();
        finish();
    }

}