LabelDialogFragment.java revision 037bf5506892b4ec9038f4a8fac32591b6ab563c
1/* 2 * Copyright (C) 2012 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 17package com.android.deskclock; 18 19import android.app.Activity; 20import android.app.Dialog; 21import android.app.DialogFragment; 22import android.content.Context; 23import android.content.DialogInterface; 24import android.content.res.ColorStateList; 25import android.graphics.Color; 26import android.os.Bundle; 27import android.support.annotation.NonNull; 28import android.support.v7.app.AlertDialog; 29import android.support.v7.widget.AppCompatEditText; 30import android.text.Editable; 31import android.text.TextUtils; 32import android.text.TextWatcher; 33import android.view.KeyEvent; 34import android.view.WindowManager; 35import android.view.inputmethod.EditorInfo; 36import android.widget.TextView; 37 38import com.android.deskclock.provider.Alarm; 39import com.android.deskclock.timer.TimerObj; 40 41/** 42 * DialogFragment to edit label. 43 */ 44public class LabelDialogFragment extends DialogFragment { 45 46 private static final String KEY_LABEL = "label"; 47 private static final String KEY_ALARM = "alarm"; 48 private static final String KEY_TIMER = "timer"; 49 private static final String KEY_TAG = "tag"; 50 51 private AppCompatEditText mLabelBox; 52 53 public static LabelDialogFragment newInstance(Alarm alarm, String label, String tag) { 54 final Bundle args = new Bundle(); 55 args.putString(KEY_LABEL, label); 56 args.putParcelable(KEY_ALARM, alarm); 57 args.putString(KEY_TAG, tag); 58 59 final LabelDialogFragment frag = new LabelDialogFragment(); 60 frag.setArguments(args); 61 return frag; 62 } 63 64 public static LabelDialogFragment newInstance(TimerObj timer, String label, String tag) { 65 final Bundle args = new Bundle(); 66 args.putString(KEY_LABEL, label); 67 args.putParcelable(KEY_TIMER, timer); 68 args.putString(KEY_TAG, tag); 69 70 final LabelDialogFragment frag = new LabelDialogFragment(); 71 frag.setArguments(args); 72 return frag; 73 } 74 75 @Override 76 public void onSaveInstanceState(@NonNull Bundle outState) { 77 super.onSaveInstanceState(outState); 78 outState.putString(KEY_LABEL, mLabelBox.getText().toString()); 79 } 80 81 @Override 82 public Dialog onCreateDialog(Bundle savedInstanceState) { 83 final Bundle bundle = getArguments(); 84 final Alarm alarm = bundle.getParcelable(KEY_ALARM); 85 final TimerObj timer = bundle.getParcelable(KEY_TIMER); 86 final String tag = bundle.getString(KEY_TAG); 87 final String label = savedInstanceState != null ? 88 savedInstanceState.getString(KEY_LABEL) : bundle.getString(KEY_LABEL); 89 90 final Context context = getActivity(); 91 final int colorAccent = Utils.obtainStyledColor(context, R.attr.colorAccent, Color.RED); 92 final int colorControlNormal = 93 Utils.obtainStyledColor(context, R.attr.colorControlNormal, Color.WHITE); 94 95 mLabelBox = new AppCompatEditText(context); 96 mLabelBox.setOnEditorActionListener(new TextView.OnEditorActionListener() { 97 @Override 98 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 99 if (actionId == EditorInfo.IME_ACTION_DONE) { 100 set(alarm, timer, tag); 101 return true; 102 } 103 return false; 104 } 105 }); 106 mLabelBox.addTextChangedListener(new TextWatcher() { 107 @Override 108 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 109 } 110 111 @Override 112 public void onTextChanged(CharSequence s, int start, int before, int count) { 113 final int color = TextUtils.isEmpty(s) ? colorControlNormal : colorAccent; 114 mLabelBox.setSupportBackgroundTintList(ColorStateList.valueOf(color)); 115 } 116 117 @Override 118 public void afterTextChanged(Editable editable) { 119 } 120 }); 121 mLabelBox.setText(label); 122 mLabelBox.selectAll(); 123 124 final int padding = getResources().getDimensionPixelSize(R.dimen.label_edittext_padding); 125 final AlertDialog alertDialog = new AlertDialog.Builder(context) 126 .setView(mLabelBox, padding, 0, padding, 0) 127 .setPositiveButton(R.string.time_picker_set, new DialogInterface.OnClickListener() { 128 @Override 129 public void onClick(DialogInterface dialog, int which) { 130 set(alarm, timer, tag); 131 } 132 }) 133 .setNegativeButton(R.string.time_picker_cancel, 134 new DialogInterface.OnClickListener() { 135 @Override 136 public void onClick(DialogInterface dialog, int which) { 137 dismiss(); 138 } 139 }) 140 .setMessage(R.string.label) 141 .create(); 142 143 alertDialog.getWindow().setSoftInputMode( 144 WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 145 return alertDialog; 146 } 147 148 private void set(Alarm alarm, TimerObj timer, String tag) { 149 String label = mLabelBox.getText().toString(); 150 if (label.trim().isEmpty()) { 151 // Don't allow user to input label with only whitespace. 152 label = ""; 153 } 154 155 if (alarm != null) { 156 set(alarm, tag, label); 157 } else if (timer != null) { 158 set(timer, tag, label); 159 } else { 160 LogUtils.e("No alarm or timer available."); 161 } 162 } 163 164 private void set(Alarm alarm, String tag, String label) { 165 final Activity activity = getActivity(); 166 if (activity instanceof AlarmLabelDialogHandler) { 167 ((AlarmLabelDialogHandler) activity).onDialogLabelSet(alarm, label, tag); 168 } else { 169 LogUtils.e("Error! Activities that use LabelDialogFragment must implement " 170 + "AlarmLabelDialogHandler or TimerLabelDialogHandler"); 171 } 172 dismiss(); 173 } 174 175 private void set(TimerObj timer, String tag, String label) { 176 final Activity activity = getActivity(); 177 if (activity instanceof TimerLabelDialogHandler){ 178 ((TimerLabelDialogHandler) activity).onDialogLabelSet(timer, label, tag); 179 } else { 180 LogUtils.e("Error! Activities that use LabelDialogFragment must implement " 181 + "AlarmLabelDialogHandler or TimerLabelDialogHandler"); 182 } 183 dismiss(); 184 } 185 186 interface AlarmLabelDialogHandler { 187 void onDialogLabelSet(Alarm alarm, String label, String tag); 188 } 189 190 interface TimerLabelDialogHandler { 191 void onDialogLabelSet(TimerObj timer, String label, String tag); 192 } 193} 194