OSDN Git Service

Eleven: rebrand step 1: update paths
[android-x86/packages-apps-Eleven.git] / src / org / lineageos / eleven / menu / BasePlaylistDialog.java
1 /*
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.
12  */
13
14 package com.cyanogenmod.eleven.menu;
15
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;
28
29 import com.cyanogenmod.eleven.R;
30 import com.cyanogenmod.eleven.utils.MusicUtils;
31
32 /**
33  * A simple base class for the playlist dialogs.
34  *
35  * @author Andrew Neal (andrewdneal@gmail.com)
36  */
37 public abstract class BasePlaylistDialog extends DialogFragment {
38
39     /* The actual dialog */
40     protected AlertDialog mPlaylistDialog;
41
42     /* Used to make new playlist names */
43     protected EditText mPlaylist;
44
45     /* The dialog save button */
46     protected Button mSaveButton;
47
48     /* The dialog prompt */
49     protected String mPrompt;
50
51     /* The default edit text text */
52     protected String mDefaultname;
53
54     /**
55      * {@inheritDoc}
56      */
57     @Override
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);
65         // All caps
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() {
71
72                     @Override
73                     public void onClick(final DialogInterface dialog, final int which) {
74                         onSaveClick();
75                         MusicUtils.refresh();
76                         dialog.dismiss();
77                     }
78                 });
79         // Set the cancel button action
80         mPlaylistDialog.setButton(Dialog.BUTTON_NEGATIVE, getString(R.string.cancel),
81                 new OnClickListener() {
82
83                     @Override
84                     public void onClick(final DialogInterface dialog, final int which) {
85                         MusicUtils.refresh();
86                         dialog.dismiss();
87                     }
88                 });
89
90         mPlaylist.post(new Runnable() {
91
92             @Override
93             public void run() {
94                 // Request focus to the edit text
95                 mPlaylist.requestFocus();
96                 // Select the playlist name
97                 mPlaylist.selectAll();
98             };
99         });
100
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;
111     }
112
113     /**
114      * Simple {@link TextWatcher}
115      */
116     private final TextWatcher mTextWatcher = new TextWatcher() {
117
118         /**
119          * {@inheritDoc}
120          */
121         @Override
122         public void onTextChanged(final CharSequence s, final int start, final int before,
123                 final int count) {
124             onTextChangedListener();
125         }
126
127         /**
128          * {@inheritDoc}
129          */
130         @Override
131         public void afterTextChanged(final Editable s) {
132             /* Nothing to do */
133         }
134
135         /**
136          * {@inheritDoc}
137          */
138         @Override
139         public void beforeTextChanged(final CharSequence s, final int start, final int count,
140                 final int after) {
141             /* Nothing to do */
142         }
143     };
144
145     /**
146      * Initializes the prompt and default name
147      */
148     public abstract void initObjects(Bundle savedInstanceState);
149
150     /**
151      * Called when the save button of our {@link AlertDialog} is pressed
152      */
153     public abstract void onSaveClick();
154
155     /**
156      * Called in our {@link TextWatcher} during a text change
157      */
158     public abstract void onTextChangedListener();
159
160 }