Home | History | Annotate | Download | only in callcomposer
      1 /*
      2  * Copyright (C) 2016 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.android.dialer.callcomposer;
     18 
     19 import android.os.Bundle;
     20 import android.support.annotation.Nullable;
     21 import android.text.Editable;
     22 import android.text.InputFilter;
     23 import android.text.TextUtils;
     24 import android.text.TextWatcher;
     25 import android.view.KeyEvent;
     26 import android.view.LayoutInflater;
     27 import android.view.View;
     28 import android.view.View.OnClickListener;
     29 import android.view.ViewGroup;
     30 import android.widget.EditText;
     31 import android.widget.TextView;
     32 import android.widget.TextView.OnEditorActionListener;
     33 
     34 /** Fragment used to compose call with message fragment. */
     35 public class MessageComposerFragment extends CallComposerFragment
     36     implements OnClickListener, TextWatcher, OnEditorActionListener {
     37   private static final String CHAR_LIMIT_KEY = "char_limit";
     38 
     39   public static final int NO_CHAR_LIMIT = -1;
     40 
     41   private EditText customMessage;
     42   private int charLimit;
     43 
     44   public static MessageComposerFragment newInstance(int charLimit) {
     45     MessageComposerFragment fragment = new MessageComposerFragment();
     46     Bundle args = new Bundle();
     47     args.putInt(CHAR_LIMIT_KEY, charLimit);
     48     fragment.setArguments(args);
     49     return fragment;
     50   }
     51 
     52   @Nullable
     53   public String getMessage() {
     54     return customMessage == null ? null : customMessage.getText().toString();
     55   }
     56 
     57   @Nullable
     58   @Override
     59   public View onCreateView(
     60       LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     61     charLimit = getArguments().getInt(CHAR_LIMIT_KEY, NO_CHAR_LIMIT);
     62 
     63     View view = inflater.inflate(R.layout.fragment_message_composer, container, false);
     64     TextView urgent = (TextView) view.findViewById(R.id.message_urgent);
     65     customMessage = (EditText) view.findViewById(R.id.custom_message);
     66 
     67     urgent.setOnClickListener(this);
     68     customMessage.addTextChangedListener(this);
     69     customMessage.setOnEditorActionListener(this);
     70     if (charLimit != NO_CHAR_LIMIT) {
     71       TextView remainingChar = (TextView) view.findViewById(R.id.remaining_characters);
     72       remainingChar.setText("" + charLimit);
     73       customMessage.setFilters(new InputFilter[] {new InputFilter.LengthFilter(charLimit)});
     74       customMessage.addTextChangedListener(
     75           new TextWatcher() {
     76             @Override
     77 
     78             public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
     79 
     80             @Override
     81             public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
     82 
     83             @Override
     84             public void afterTextChanged(Editable editable) {
     85               remainingChar.setText("" + (charLimit - editable.length()));
     86             }
     87           });
     88     }
     89     view.findViewById(R.id.message_chat).setOnClickListener(this);
     90     view.findViewById(R.id.message_question).setOnClickListener(this);
     91     return view;
     92   }
     93 
     94   @Override
     95   public void onClick(View view) {
     96     customMessage.setText(((TextView) view).getText());
     97     customMessage.setSelection(customMessage.getText().length());
     98   }
     99 
    100   @Override
    101   public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    102 
    103   @Override
    104   public void onTextChanged(CharSequence s, int start, int before, int count) {}
    105 
    106   @Override
    107   public void afterTextChanged(Editable s) {
    108     getListener().composeCall(this);
    109   }
    110 
    111   @Override
    112   public boolean shouldHide() {
    113     return TextUtils.isEmpty(getMessage());
    114   }
    115 
    116   @Override
    117   public void clearComposer() {
    118     customMessage.getText().clear();
    119   }
    120 
    121   @Override
    122   public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    123     if (getMessage() == null) {
    124       return false;
    125     }
    126     getListener().sendAndCall();
    127     return true;
    128   }
    129 }
    130