1/**
2 * Copyright (c) 2013, Google Inc.
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
17package com.android.email.activity;
18
19import android.text.Editable;
20import android.text.SpannableStringBuilder;
21import android.view.Menu;
22import android.view.MenuItem;
23import android.widget.EditText;
24
25import com.android.email.R;
26import com.android.mail.compose.ComposeActivity;
27import com.android.mail.utils.LogUtils;
28
29public class ComposeActivityEmail extends ComposeActivity
30        implements InsertQuickResponseDialog.Callback {
31    static final String insertQuickResponseDialogTag = "insertQuickResponseDialog";
32    @Override
33    public boolean onCreateOptionsMenu(Menu menu) {
34        final boolean superCreated = super.onCreateOptionsMenu(menu);
35        if (mReplyFromAccount != null) {
36            getMenuInflater().inflate(R.menu.email_compose_menu_extras, menu);
37            return true;
38        } else {
39            LogUtils.d(LogUtils.TAG, "mReplyFromAccount is null, not adding Quick Response menu");
40            return superCreated;
41        }
42    }
43
44    @Override
45    public boolean onOptionsItemSelected(MenuItem item) {
46        if (item.getItemId() == R.id.insert_quick_response_menu_item) {
47            InsertQuickResponseDialog dialog = InsertQuickResponseDialog.newInstance(null,
48                    mReplyFromAccount.account);
49            dialog.show(getFragmentManager(), insertQuickResponseDialogTag);
50        }
51        return super.onOptionsItemSelected(item);
52    }
53
54    public void onQuickResponseSelected(CharSequence quickResponse) {
55        final int selEnd = mBodyView.getSelectionEnd();
56        final int selStart = mBodyView.getSelectionStart();
57
58        if (selEnd >= 0 && selStart >= 0) {
59            final SpannableStringBuilder messageBody =
60                    new SpannableStringBuilder(mBodyView.getText());
61            final int replaceStart = selStart < selEnd ? selStart : selEnd;
62            final int replaceEnd = selStart < selEnd ? selEnd : selStart;
63            messageBody.replace(replaceStart, replaceEnd, quickResponse);
64            mBodyView.setText(messageBody);
65            mBodyView.setSelection(replaceStart + quickResponse.length());
66        } else {
67            mBodyView.append(quickResponse);
68            mBodyView.setSelection(mBodyView.getText().length());
69        }
70    }
71}
72