Home | History | Annotate | Download | only in videoeditor
      1 /*
      2  * Copyright (C) 2009 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.videoeditor;
     18 
     19 import android.app.AlertDialog;
     20 import android.content.Context;
     21 import android.content.DialogInterface;
     22 import android.content.DialogInterface.OnCancelListener;
     23 import android.content.DialogInterface.OnClickListener;
     24 import android.text.Editable;
     25 import android.text.InputFilter;
     26 import android.text.InputType;
     27 import android.text.TextWatcher;
     28 import android.view.LayoutInflater;
     29 import android.view.View;
     30 import android.view.WindowManager;
     31 import android.widget.Button;
     32 import android.widget.EditText;
     33 
     34 /**
     35  * Utility class for creating various alert dialogs.
     36  * It contains only static methods and cannot be instantiated.
     37  */
     38 public class AlertDialogs {
     39 
     40     private AlertDialogs() {}
     41 
     42     /**
     43      * Creates an alert dialog box.
     44      *
     45      * @param context The context
     46      * @param title The title string
     47      * @param iconId The icon id
     48      * @param content The content string
     49      * @param positive The positive button text
     50      * @param positiveListener The positive listener
     51      * @param negative The negative button text
     52      * @param negativeListener The negative listener
     53      * @param cancelListener The cancel listener
     54      * @param cancelable true if cancelable
     55      */
     56     public static AlertDialog createAlert(Context context, String title, int iconId,
     57             String content, String positive, OnClickListener positiveListener, String negative,
     58             OnClickListener negativeListener, OnCancelListener cancelListener,
     59             boolean cancelable) {
     60         final AlertDialog.Builder builder = new AlertDialog.Builder(context);
     61         builder.setTitle(title);
     62         if (iconId != 0) {
     63             builder.setIcon(context.getResources().getDrawable(iconId));
     64         }
     65         builder.setMessage(content);
     66         builder.setPositiveButton(positive, positiveListener);
     67         builder.setNegativeButton(negative, negativeListener);
     68         builder.setOnCancelListener(cancelListener);
     69         builder.setCancelable(cancelable);
     70 
     71         final AlertDialog dialog = builder.create();
     72         dialog.setCanceledOnTouchOutside(true);
     73         return dialog;
     74     }
     75 
     76     /**
     77      * Creates a dialog with one edit text.
     78      *
     79      * @param context The context
     80      * @param title The title of the dialog
     81      * @param text The text shown in the edit box
     82      * @param positiveButtonText The positive button text
     83      * @param positiveListener The positive button listener
     84      * @param negativeButtonText The negative button text
     85      * @param negativeListener The negative button listener
     86      * @param cancelListener The cancel listener
     87      * @param inputType Input type
     88      * @param maxChars The maximum number of characters
     89      * @param hint hint text to be shown in the edit box
     90      *
     91      * @return The created dialog
     92      */
     93     public static AlertDialog createEditDialog(Context context, String title, String text,
     94             String positiveButtonText, DialogInterface.OnClickListener positiveListener,
     95             String negativeButtonText, DialogInterface.OnClickListener negativeListener,
     96             DialogInterface.OnCancelListener cancelListener, int inputType, int maxChars,
     97             String hint) {
     98         final AlertDialog.Builder builder = new AlertDialog.Builder(context);
     99         final LayoutInflater vi = (LayoutInflater) context.getSystemService(
    100                 Context.LAYOUT_INFLATER_SERVICE);
    101         final View myView = vi.inflate(R.layout.edit_one_dialog_view, null);
    102         builder.setView(myView);
    103 
    104         // Set the text or hint if they are available.
    105         final EditText textInput = (EditText) myView.findViewById(R.id.text_1);
    106         if (title != null) builder.setTitle(title);
    107         if (text != null) {
    108             textInput.setText(text);
    109             textInput.setSelection(0, text.length());
    110         }
    111         if (hint != null) textInput.setHint(hint);
    112 
    113         if (maxChars > 0) {
    114             final InputFilter[] filters = new InputFilter[1];
    115             filters[0] = new InputFilter.LengthFilter(maxChars);
    116             textInput.setFilters(filters);
    117         }
    118 
    119         if (inputType != InputType.TYPE_NULL) {
    120             textInput.setInputType(inputType);
    121         }
    122 
    123         // Setup the positive button listener
    124         builder.setPositiveButton(positiveButtonText, positiveListener);
    125         builder.setNegativeButton(negativeButtonText, negativeListener);
    126         builder.setOnCancelListener(cancelListener);
    127         final AlertDialog dialog = builder.create();
    128         dialog.setCanceledOnTouchOutside(true);
    129         textInput.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    130 
    131             @Override
    132             public void onFocusChange(View v, boolean hasFocus) {
    133                 if (hasFocus) {
    134                     dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    135                 }
    136             }
    137         });
    138         textInput.addTextChangedListener(new TextWatcher() {
    139             Button mPositiveButton;
    140 
    141             @Override
    142             public void onTextChanged(CharSequence s, int start, int before, int count) {
    143             }
    144 
    145             @Override
    146             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    147             }
    148 
    149             @Override
    150             public void afterTextChanged(Editable s) {
    151                 if (mPositiveButton == null) {
    152                     mPositiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
    153                 }
    154                 mPositiveButton.setEnabled(s.toString().trim().length() > 0);
    155             }
    156         });
    157 
    158         return dialog;
    159     }
    160 }
    161