2 * Copyright (C) 2012 Andrew Neal
3 * Copyright (C) 2014 The CyanogenMod Project
4 * Licensed under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with the
6 * License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
8 * or agreed to in writing, software distributed under the License is
9 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10 * KIND, either express or implied. See the License for the specific language
11 * governing permissions and limitations under the License.
14 package com.cyanogenmod.eleven.menu;
16 import android.app.AlertDialog;
17 import android.app.Dialog;
18 import android.content.DialogInterface;
19 import android.content.DialogInterface.OnClickListener;
20 import android.os.Bundle;
21 import android.support.v4.app.DialogFragment;
22 import android.text.Editable;
23 import android.text.InputType;
24 import android.text.TextWatcher;
25 import android.view.WindowManager;
26 import android.widget.Button;
27 import android.widget.EditText;
29 import com.cyanogenmod.eleven.R;
30 import com.cyanogenmod.eleven.utils.MusicUtils;
33 * A simple base class for the playlist dialogs.
35 * @author Andrew Neal (andrewdneal@gmail.com)
37 public abstract class BasePlaylistDialog extends DialogFragment {
39 /* The actual dialog */
40 protected AlertDialog mPlaylistDialog;
42 /* Used to make new playlist names */
43 protected EditText mPlaylist;
45 /* The dialog save button */
46 protected Button mSaveButton;
48 /* The dialog prompt */
49 protected String mPrompt;
51 /* The default edit text text */
52 protected String mDefaultname;
58 public Dialog onCreateDialog(final Bundle savedInstanceState) {
59 // Initialize the alert dialog
60 mPlaylistDialog = new AlertDialog.Builder(getActivity()).create();
61 // Initialize the edit text
62 mPlaylist = new EditText(getActivity());
63 // To show the "done" button on the soft keyboard
64 mPlaylist.setSingleLine(true);
66 mPlaylist.setInputType(mPlaylist.getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
67 | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
68 // Set the save button action
69 mPlaylistDialog.setButton(Dialog.BUTTON_POSITIVE, getString(R.string.save),
70 new OnClickListener() {
73 public void onClick(final DialogInterface dialog, final int which) {
79 // Set the cancel button action
80 mPlaylistDialog.setButton(Dialog.BUTTON_NEGATIVE, getString(R.string.cancel),
81 new OnClickListener() {
84 public void onClick(final DialogInterface dialog, final int which) {
90 mPlaylist.post(new Runnable() {
94 // Request focus to the edit text
95 mPlaylist.requestFocus();
96 // Select the playlist name
97 mPlaylist.selectAll();
101 initObjects(savedInstanceState);
102 mPlaylistDialog.setTitle(mPrompt);
103 mPlaylistDialog.setView(mPlaylist);
104 mPlaylist.setText(mDefaultname);
105 mPlaylist.setSelection(mDefaultname.length());
106 mPlaylist.addTextChangedListener(mTextWatcher);
107 mPlaylistDialog.getWindow().setSoftInputMode(
108 WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
109 mPlaylistDialog.show();
110 return mPlaylistDialog;
114 * Simple {@link TextWatcher}
116 private final TextWatcher mTextWatcher = new TextWatcher() {
122 public void onTextChanged(final CharSequence s, final int start, final int before,
124 onTextChangedListener();
131 public void afterTextChanged(final Editable s) {
139 public void beforeTextChanged(final CharSequence s, final int start, final int count,
146 * Initializes the prompt and default name
148 public abstract void initObjects(Bundle savedInstanceState);
151 * Called when the save button of our {@link AlertDialog} is pressed
153 public abstract void onSaveClick();
156 * Called in our {@link TextWatcher} during a text change
158 public abstract void onTextChangedListener();