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