2 * Copyright (C) 2012 Andrew Neal Licensed under the Apache License, Version 2.0
3 * (the "License"); you may not use this file except in compliance with the
4 * License. You may obtain a copy of the License at
5 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
6 * or agreed to in writing, software distributed under the License is
7 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8 * KIND, either express or implied. See the License for the specific language
9 * governing permissions and limitations under the License.
12 package com.cyngn.eleven.menu;
14 import android.app.AlertDialog;
15 import android.app.Dialog;
16 import android.content.Context;
17 import android.content.DialogInterface;
18 import android.content.DialogInterface.OnClickListener;
19 import android.os.Bundle;
20 import android.support.v4.app.DialogFragment;
21 import android.text.Editable;
22 import android.text.InputType;
23 import android.text.TextWatcher;
24 import android.view.inputmethod.InputMethodManager;
25 import android.widget.Button;
26 import android.widget.EditText;
28 import com.cyngn.eleven.R;
29 import com.cyngn.eleven.utils.MusicUtils;
32 * A simple base class for the playlist dialogs.
34 * @author Andrew Neal (andrewdneal@gmail.com)
36 public abstract class BasePlaylistDialog extends DialogFragment {
38 /* The actual dialog */
39 protected AlertDialog mPlaylistDialog;
41 /* Used to make new playlist names */
42 protected EditText mPlaylist;
44 /* The dialog save button */
45 protected Button mSaveButton;
47 /* The dialog prompt */
48 protected String mPrompt;
50 /* The default edit text text */
51 protected String mDefaultname;
57 public Dialog onCreateDialog(final Bundle savedInstanceState) {
58 // Initialize the alert dialog
59 mPlaylistDialog = new AlertDialog.Builder(getActivity()).create();
60 // Initialize the edit text
61 mPlaylist = new EditText(getActivity());
62 // To show the "done" button on the soft keyboard
63 mPlaylist.setSingleLine(true);
65 mPlaylist.setInputType(mPlaylist.getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
66 | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
67 // Set the save button action
68 mPlaylistDialog.setButton(Dialog.BUTTON_POSITIVE, getString(R.string.save),
69 new OnClickListener() {
72 public void onClick(final DialogInterface dialog, final int which) {
78 // Set the cancel button action
79 mPlaylistDialog.setButton(Dialog.BUTTON_NEGATIVE, getString(R.string.cancel),
80 new OnClickListener() {
83 public void onClick(final DialogInterface dialog, final int which) {
90 mPlaylist.post(new Runnable() {
94 // Open up the soft keyboard
96 // Request focus to the edit text
97 mPlaylist.requestFocus();
98 // Select the playlist name
99 mPlaylist.selectAll();
103 initObjects(savedInstanceState);
104 mPlaylistDialog.setTitle(mPrompt);
105 mPlaylistDialog.setView(mPlaylist);
106 mPlaylist.setText(mDefaultname);
107 mPlaylist.setSelection(mDefaultname.length());
108 mPlaylist.addTextChangedListener(mTextWatcher);
109 mPlaylistDialog.show();
110 return mPlaylistDialog;
114 * Opens the soft keyboard
116 protected void openKeyboard() {
117 final InputMethodManager mInputMethodManager = (InputMethodManager)getActivity()
118 .getSystemService(Context.INPUT_METHOD_SERVICE);
119 mInputMethodManager.toggleSoftInputFromWindow(mPlaylist.getApplicationWindowToken(),
120 InputMethodManager.SHOW_FORCED, 0);
124 * Closes the soft keyboard
126 protected void closeKeyboard() {
127 final InputMethodManager mInputMethodManager = (InputMethodManager)getActivity()
128 .getSystemService(Context.INPUT_METHOD_SERVICE);
129 mInputMethodManager.hideSoftInputFromWindow(mPlaylist.getWindowToken(), 0);
133 * Simple {@link TextWatcher}
135 private final TextWatcher mTextWatcher = new TextWatcher() {
141 public void onTextChanged(final CharSequence s, final int start, final int before,
143 onTextChangedListener();
150 public void afterTextChanged(final Editable s) {
158 public void beforeTextChanged(final CharSequence s, final int start, final int count,
165 * Initializes the prompt and default name
167 public abstract void initObjects(Bundle savedInstanceState);
170 * Called when the save button of our {@link AlertDialog} is pressed
172 public abstract void onSaveClick();
175 * Called in our {@link TextWatcher} during a text change
177 public abstract void onTextChangedListener();