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