1 /*
2  * Copyright (C) 2017 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.widget;
18 
19 import android.os.Bundle;
20 import android.support.annotation.Nullable;
21 import android.support.v4.app.Fragment;
22 import android.text.Editable;
23 import android.text.InputFilter;
24 import android.text.TextUtils;
25 import android.text.TextWatcher;
26 import android.view.KeyEvent;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.View.OnClickListener;
30 import android.view.ViewGroup;
31 import android.widget.EditText;
32 import android.widget.ImageView;
33 import android.widget.LinearLayout;
34 import android.widget.TextView;
35 import android.widget.TextView.OnEditorActionListener;
36 import com.android.dialer.common.Assert;
37 import com.android.dialer.common.FragmentUtils;
38 
39 /** Fragment used to compose call with message fragment. */
40 public class MessageFragment extends Fragment
41     implements OnClickListener, TextWatcher, OnEditorActionListener {
42   private static final String CHAR_LIMIT_KEY = "char_limit";
43   private static final String SHOW_SEND_ICON_KEY = "show_send_icon";
44   private static final String MESSAGE_LIST_KEY = "message_list";
45 
46   public static final int NO_CHAR_LIMIT = -1;
47 
48   private EditText customMessage;
49   private ImageView sendMessage;
50   private View sendMessageContainer;
51   private TextView remainingChar;
52   private int charLimit;
53 
newInstance(Builder builder)54   private static MessageFragment newInstance(Builder builder) {
55     MessageFragment fragment = new MessageFragment();
56     Bundle args = new Bundle();
57     args.putInt(CHAR_LIMIT_KEY, builder.charLimit);
58     args.putBoolean(SHOW_SEND_ICON_KEY, builder.showSendIcon);
59     args.putStringArray(MESSAGE_LIST_KEY, builder.messages);
60     fragment.setArguments(args);
61     return fragment;
62   }
63 
64   @Nullable
getMessage()65   public String getMessage() {
66     return customMessage == null ? null : customMessage.getText().toString();
67   }
68 
69   @Nullable
70   @Override
onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)71   public View onCreateView(
72       LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
73     View view = inflater.inflate(R.layout.fragment_message, container, false);
74 
75     sendMessage = view.findViewById(R.id.send_message);
76     sendMessageContainer = view.findViewById(R.id.count_and_send_container);
77     if (getArguments().getBoolean(SHOW_SEND_ICON_KEY, false)) {
78       sendMessage.setVisibility(View.VISIBLE);
79       sendMessage.setEnabled(false);
80       sendMessageContainer.setOnClickListener(this);
81     }
82 
83     customMessage = view.findViewById(R.id.custom_message);
84     customMessage.addTextChangedListener(this);
85     customMessage.setOnEditorActionListener(this);
86     charLimit = getArguments().getInt(CHAR_LIMIT_KEY, NO_CHAR_LIMIT);
87     if (charLimit != NO_CHAR_LIMIT) {
88       remainingChar = view.findViewById(R.id.remaining_characters);
89       remainingChar.setVisibility(View.VISIBLE);
90       remainingChar = view.findViewById(R.id.remaining_characters);
91       remainingChar.setText(Integer.toString(charLimit));
92       customMessage.setFilters(new InputFilter[] {new InputFilter.LengthFilter(charLimit)});
93     }
94 
95     LinearLayout messageContainer = view.findViewById(R.id.message_container);
96     for (String message : getArguments().getStringArray(MESSAGE_LIST_KEY)) {
97       TextView textView = (TextView) inflater.inflate(R.layout.selectable_text_view, null);
98       textView.setOnClickListener(this);
99       textView.setText(message);
100       messageContainer.addView(textView);
101     }
102     return view;
103   }
104 
105   @Override
onClick(View view)106   public void onClick(View view) {
107     if (view == sendMessageContainer) {
108       if (!TextUtils.isEmpty(customMessage.getText())) {
109         getListener().onMessageFragmentSendMessage(customMessage.getText().toString());
110       }
111     } else if (view.getId() == R.id.selectable_text_view) {
112       customMessage.setText(((TextView) view).getText());
113       customMessage.setSelection(customMessage.getText().length());
114     } else {
115       Assert.fail("Unknown view clicked");
116     }
117   }
118 
119   @Override
beforeTextChanged(CharSequence s, int start, int count, int after)120   public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
121 
122   @Override
onTextChanged(CharSequence s, int start, int before, int count)123   public void onTextChanged(CharSequence s, int start, int before, int count) {
124     sendMessage.setEnabled(s.length() > 0);
125   }
126 
127   @Override
afterTextChanged(Editable s)128   public void afterTextChanged(Editable s) {
129     if (charLimit != NO_CHAR_LIMIT) {
130       remainingChar.setText(Integer.toString(charLimit - s.length()));
131     }
132     getListener().onMessageFragmentAfterTextChange(s.toString());
133   }
134 
135   @Override
onEditorAction(TextView v, int actionId, KeyEvent event)136   public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
137     if (!TextUtils.isEmpty(getMessage())) {
138       getListener().onMessageFragmentSendMessage(getMessage());
139     }
140     return true;
141   }
142 
getListener()143   private Listener getListener() {
144     return FragmentUtils.getParentUnsafe(this, Listener.class);
145   }
146 
builder()147   public static Builder builder() {
148     return new Builder();
149   }
150 
151   /** Builder for {@link MessageFragment}. */
152   public static class Builder {
153     private String[] messages;
154     private boolean showSendIcon;
155     private int charLimit = NO_CHAR_LIMIT;
156 
157     /**
158      * @throws NullPointerException if message is null
159      * @throws IllegalArgumentException if messages.length is outside the range [1,3].
160      */
setMessages(String... messages)161     public Builder setMessages(String... messages) {
162       // Since we only allow up to 3 messages, crash if more are set.
163       Assert.checkArgument(messages.length > 0 && messages.length <= 3);
164       this.messages = messages;
165       return this;
166     }
167 
showSendIcon()168     public Builder showSendIcon() {
169       showSendIcon = true;
170       return this;
171     }
172 
setCharLimit(int charLimit)173     public Builder setCharLimit(int charLimit) {
174       this.charLimit = charLimit;
175       return this;
176     }
177 
build()178     public MessageFragment build() {
179       return MessageFragment.newInstance(this);
180     }
181   }
182 
183   /** Interface for parent activity to implement to listen for important events. */
184   public interface Listener {
onMessageFragmentSendMessage(String message)185     void onMessageFragmentSendMessage(String message);
186 
onMessageFragmentAfterTextChange(String message)187     void onMessageFragmentAfterTextChange(String message);
188   }
189 }
190