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.incallui.answer.impl; 18 19 import android.app.AlertDialog; 20 import android.app.Dialog; 21 import android.content.DialogInterface; 22 import android.content.DialogInterface.OnCancelListener; 23 import android.content.DialogInterface.OnShowListener; 24 import android.os.Bundle; 25 import android.support.annotation.NonNull; 26 import android.support.v7.app.AppCompatDialogFragment; 27 import android.text.Editable; 28 import android.text.TextWatcher; 29 import android.view.View; 30 import android.view.WindowManager.LayoutParams; 31 import android.widget.Button; 32 import android.widget.EditText; 33 import com.android.dialer.common.FragmentUtils; 34 35 /** 36 * Shows the dialog for users to enter a custom message when rejecting a call with an SMS message. 37 */ 38 public class CreateCustomSmsDialogFragment extends AppCompatDialogFragment { 39 40 private static final String ARG_ENTERED_TEXT = "enteredText"; 41 42 private EditText editText; 43 newInstance()44 public static CreateCustomSmsDialogFragment newInstance() { 45 return new CreateCustomSmsDialogFragment(); 46 } 47 48 @NonNull 49 @Override onCreateDialog(Bundle savedInstanceState)50 public Dialog onCreateDialog(Bundle savedInstanceState) { 51 super.onCreateDialog(savedInstanceState); 52 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 53 View view = View.inflate(builder.getContext(), R.layout.fragment_custom_sms_dialog, null); 54 editText = (EditText) view.findViewById(R.id.custom_sms_input); 55 if (savedInstanceState != null) { 56 editText.setText(savedInstanceState.getCharSequence(ARG_ENTERED_TEXT)); 57 } 58 builder 59 .setCancelable(true) 60 .setView(view) 61 .setPositiveButton( 62 R.string.call_incoming_custom_message_send, 63 new DialogInterface.OnClickListener() { 64 @Override 65 public void onClick(DialogInterface dialogInterface, int i) { 66 FragmentUtils.getParentUnsafe( 67 CreateCustomSmsDialogFragment.this, CreateCustomSmsHolder.class) 68 .customSmsCreated(editText.getText().toString().trim()); 69 dismiss(); 70 } 71 }) 72 .setNegativeButton( 73 R.string.call_incoming_custom_message_cancel, 74 new DialogInterface.OnClickListener() { 75 @Override 76 public void onClick(DialogInterface dialogInterface, int i) { 77 dismiss(); 78 } 79 }) 80 .setOnCancelListener( 81 new OnCancelListener() { 82 @Override 83 public void onCancel(DialogInterface dialogInterface) { 84 dismiss(); 85 } 86 }) 87 .setTitle(R.string.call_incoming_respond_via_sms_custom_message); 88 final AlertDialog customMessagePopup = builder.create(); 89 customMessagePopup.setOnShowListener( 90 new OnShowListener() { 91 @Override 92 public void onShow(DialogInterface dialogInterface) { 93 ((AlertDialog) dialogInterface) 94 .getButton(AlertDialog.BUTTON_POSITIVE) 95 .setEnabled(false); 96 } 97 }); 98 99 editText.addTextChangedListener( 100 new TextWatcher() { 101 @Override 102 public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {} 103 104 @Override 105 public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {} 106 107 @Override 108 public void afterTextChanged(Editable editable) { 109 Button sendButton = customMessagePopup.getButton(DialogInterface.BUTTON_POSITIVE); 110 sendButton.setEnabled(editable != null && editable.toString().trim().length() != 0); 111 } 112 }); 113 customMessagePopup.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 114 customMessagePopup.getWindow().addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED); 115 return customMessagePopup; 116 } 117 118 @Override onSaveInstanceState(@onNull Bundle outState)119 public void onSaveInstanceState(@NonNull Bundle outState) { 120 super.onSaveInstanceState(outState); 121 outState.putCharSequence(ARG_ENTERED_TEXT, editText.getText()); 122 } 123 124 @Override onDismiss(DialogInterface dialogInterface)125 public void onDismiss(DialogInterface dialogInterface) { 126 super.onDismiss(dialogInterface); 127 FragmentUtils.getParentUnsafe(this, CreateCustomSmsHolder.class).customSmsDismissed(); 128 } 129 130 /** Call back for {@link CreateCustomSmsDialogFragment} */ 131 public interface CreateCustomSmsHolder { 132 customSmsCreated(@onNull CharSequence text)133 void customSmsCreated(@NonNull CharSequence text); 134 customSmsDismissed()135 void customSmsDismissed(); 136 } 137 } 138