OSDN Git Service

Automatic translation import
[android-x86/packages-apps-Eleven.git] / src / com / cyngn / eleven / menu / BasePlaylistDialog.java
1 /*
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.
10  */
11
12 package com.cyngn.eleven.menu;
13
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;
27
28 import com.cyngn.eleven.R;
29 import com.cyngn.eleven.utils.MusicUtils;
30
31 /**
32  * A simple base class for the playlist dialogs.
33  * 
34  * @author Andrew Neal (andrewdneal@gmail.com)
35  */
36 public abstract class BasePlaylistDialog extends DialogFragment {
37
38     /* The actual dialog */
39     protected AlertDialog mPlaylistDialog;
40
41     /* Used to make new playlist names */
42     protected EditText mPlaylist;
43
44     /* The dialog save button */
45     protected Button mSaveButton;
46
47     /* The dialog prompt */
48     protected String mPrompt;
49
50     /* The default edit text text */
51     protected String mDefaultname;
52
53     /**
54      * {@inheritDoc}
55      */
56     @Override
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);
64         // All caps
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() {
70
71                     @Override
72                     public void onClick(final DialogInterface dialog, final int which) {
73                         onSaveClick();
74                         MusicUtils.refresh();
75                         dialog.dismiss();
76                     }
77                 });
78         // Set the cancel button action
79         mPlaylistDialog.setButton(Dialog.BUTTON_NEGATIVE, getString(R.string.cancel),
80                 new OnClickListener() {
81
82                     @Override
83                     public void onClick(final DialogInterface dialog, final int which) {
84                         closeKeyboard();
85                         MusicUtils.refresh();
86                         dialog.dismiss();
87                     }
88                 });
89
90         mPlaylist.post(new Runnable() {
91
92             @Override
93             public void run() {
94                 // Open up the soft keyboard
95                 openKeyboard();
96                 // Request focus to the edit text
97                 mPlaylist.requestFocus();
98                 // Select the playlist name
99                 mPlaylist.selectAll();
100             };
101         });
102
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;
111     }
112
113     /**
114      * Opens the soft keyboard
115      */
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);
121     }
122
123     /**
124      * Closes the soft keyboard
125      */
126     protected void closeKeyboard() {
127         final InputMethodManager mInputMethodManager = (InputMethodManager)getActivity()
128                 .getSystemService(Context.INPUT_METHOD_SERVICE);
129         mInputMethodManager.hideSoftInputFromWindow(mPlaylist.getWindowToken(), 0);
130     }
131
132     /**
133      * Simple {@link TextWatcher}
134      */
135     private final TextWatcher mTextWatcher = new TextWatcher() {
136
137         /**
138          * {@inheritDoc}
139          */
140         @Override
141         public void onTextChanged(final CharSequence s, final int start, final int before,
142                 final int count) {
143             onTextChangedListener();
144         }
145
146         /**
147          * {@inheritDoc}
148          */
149         @Override
150         public void afterTextChanged(final Editable s) {
151             /* Nothing to do */
152         }
153
154         /**
155          * {@inheritDoc}
156          */
157         @Override
158         public void beforeTextChanged(final CharSequence s, final int start, final int count,
159                 final int after) {
160             /* Nothing to do */
161         }
162     };
163
164     /**
165      * Initializes the prompt and default name
166      */
167     public abstract void initObjects(Bundle savedInstanceState);
168
169     /**
170      * Called when the save button of our {@link AlertDialog} is pressed
171      */
172     public abstract void onSaveClick();
173
174     /**
175      * Called in our {@link TextWatcher} during a text change
176      */
177     public abstract void onTextChangedListener();
178
179 }