aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/filemanager/ui/dialogs/InputNameDialog.java
blob: 04140501748102fd6470da28fff305063b0c1ffc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 * Copyright (C) 2012 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cyanogenmod.filemanager.ui.dialogs;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.cyanogenmod.filemanager.R;
import com.cyanogenmod.filemanager.model.FileSystemObject;
import com.cyanogenmod.filemanager.ui.ThemeManager;
import com.cyanogenmod.filemanager.ui.ThemeManager.Theme;
import com.cyanogenmod.filemanager.util.DialogHelper;
import com.cyanogenmod.filemanager.util.FileHelper;

import java.io.File;
import java.util.List;

/**
 * A class that wraps a dialog for input a name for file, folder, ...
 */
public class InputNameDialog
    implements TextWatcher, DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {

    private final Context mContext;
    /**
     * @hide
     */
    final AlertDialog mDialog;
    private final TextView mMsg;
    /**
     * @hide
     */
    final EditText mEditText;
    /**
     * @hide
     */
    final List<FileSystemObject> mFiles;
    /**
     * @hide
     */
    final FileSystemObject mFso;
    private final boolean mAllowFsoName;

    private DialogInterface.OnCancelListener mOnCancelListener;
    private DialogInterface.OnDismissListener mOnDismissListener;
    /**
     * @hide
     */
    boolean mCancelled;

    /**
     * Constructor of <code>InputNameDialog</code>.
     *
     * @param context The current context
     * @param files The files of the current directory (used to validate the name)
     * @param dialogTitle The dialog title
     */
    public InputNameDialog(
            final Context context, List<FileSystemObject> files, String dialogTitle) {
        this(context, files, null, false, dialogTitle);
    }

    /**
     * Constructor of <code>InputNameDialog</code>.
     *
     * @param context The current context
     * @param files The files of the current directory (used to validate the name)
     * @param fso The original file system object. null if not needed.
     * @param allowFsoName If allow that the name of the fso will be returned
     * @param dialogTitle The dialog title
     */
    public InputNameDialog(
            final Context context, final List<FileSystemObject> files,
            final FileSystemObject fso, boolean allowFsoName, final String dialogTitle) {
        super();

        //Save the context
        this.mContext = context;

        //Save the files
        this.mFiles = files;
        this.mFso = fso;
        this.mAllowFsoName = allowFsoName;
        this.mCancelled = true;

        //Create the
        LayoutInflater li =
                (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = li.inflate(R.layout.input_name_dialog, null);
        TextView title = (TextView)v.findViewById(R.id.input_name_dialog_label);
        title.setText(R.string.input_name_dialog_label);
        this.mEditText = (EditText)v.findViewById(R.id.input_name_dialog_edit);
        if (this.mFso != null) {
            this.mEditText.setText(this.mFso.getName());
        } else {
            this.mEditText.setText(dialogTitle);
        }
        this.mEditText.selectAll();
        this.mEditText.addTextChangedListener(this);
        this.mMsg = (TextView)v.findViewById(R.id.input_name_dialog_message);

        // Apply the current theme
        Theme theme = ThemeManager.getCurrentTheme(context);
        theme.setBackgroundDrawable(context, v, "background_drawable"); //$NON-NLS-1$
        theme.setTextColor(context, title, "text_color"); //$NON-NLS-1$
        theme.setTextColor(context, this.mMsg, "text_color"); //$NON-NLS-1$
        this.mMsg.setCompoundDrawablesWithIntrinsicBounds(
                theme.getDrawable(this.mContext,
                        "filesystem_dialog_warning_drawable"), //$NON-NLS-1$
                null, null, null);

        //Create the dialog
        this.mDialog = DialogHelper.createDialog(
                                        context,
                                        0,
                                        dialogTitle,
                                        v);
        this.mDialog.setButton(
                DialogInterface.BUTTON_POSITIVE,
                context.getString(android.R.string.ok),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        InputNameDialog.this.mCancelled = false;
                    }
                });
        this.mDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                InputMethodManager mgr =
                        (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
                mgr.showSoftInput(InputNameDialog.this.mEditText, InputMethodManager.SHOW_IMPLICIT);
            }
        });
        this.mDialog.setButton(
                DialogInterface.BUTTON_NEGATIVE,
                context.getString(android.R.string.cancel),
                (DialogInterface.OnClickListener)null);
        this.mDialog.setOnCancelListener(this);
        this.mDialog.setOnDismissListener(this);

        // Disable accept button, because name is the same as fso
        if (this.mFso != null && !this.mAllowFsoName) {
            this.mEditText.post(new Runnable() {
                @Override
                public void run() {
                    InputNameDialog.this.mDialog.getButton(
                            DialogInterface.BUTTON_POSITIVE).setEnabled(false);
                }
            });
        } else {
            this.mEditText.post(new Runnable() {
                @Override
                public void run() {
                    checkName(InputNameDialog.this.mEditText.getText().toString());
                }
            });
        }
    }

    /**
     * Set a listener to be invoked when the dialog is cancelled.
     * <p>
     * This will only be invoked when the dialog is cancelled, if the creator
     * needs to know when it is dismissed in general, use
     * {@link #setOnDismissListener}.
     *
     * @param onCancelListener The {@link "OnCancelListener"} to use.
     */
    public void setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) {
        this.mOnCancelListener = onCancelListener;
    }

    /**
     * Set a listener to be invoked when the dialog is dismissed.
     *
     * @param onDismissListener The {@link "OnDismissListener"} to use.
     */
    public void setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) {
        this.mOnDismissListener = onDismissListener;
    }

    /**
     * Method that shows the dialog.
     */
    public void show() {
        DialogHelper.delegateDialogShow(this.mContext, this.mDialog);
    }

    /**
     * Method that returns the name that the user inputted.
     *
     * @return String The name that the user inputted
     */
    public String getName() {
        return this.mEditText.getText().toString();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        /**NON BLOCK**/
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        /**NON BLOCK**/
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void afterTextChanged(Editable s) {
        String name = s.toString().trim();
        checkName(name);
    }

    /**
     * Method that checks the input name
     * @param name
     * @hide
     */
    void checkName(String name) {
        //The name is empty
        if (name.length() == 0) {
            setMsg(
                InputNameDialog.this.mContext.getString(
                      R.string.input_name_dialog_message_empty_name), false);
            return;
        }
        // The path is invalid
        if (name.indexOf(File.separator) != -1) {
            setMsg(
                InputNameDialog.this.mContext.getString(
                      R.string.input_name_dialog_message_invalid_path_name,
                      File.separator), false);
            return;
        }
        // No allow . or ..
        if (name.compareTo(FileHelper.CURRENT_DIRECTORY) == 0 ||
                name.compareTo(FileHelper.PARENT_DIRECTORY) == 0) {
            setMsg(
                InputNameDialog.this.mContext.getString(
                        R.string.input_name_dialog_message_invalid_name), false);
            return;
        }
        // The same name
        if (this.mFso != null && !this.mAllowFsoName && name.compareTo(this.mFso.getName()) == 0) {
            setMsg(null, false);
            return;
        }
        // Name exists
        if (FileHelper.isNameExists(this.mFiles, name)) {
            setMsg(
                InputNameDialog.this.mContext.getString(
                        R.string.input_name_dialog_message_name_exists), false);
            return;
        }

        //Valid name
        setMsg(null, true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onDismiss(DialogInterface dialog) {
        if (!InputNameDialog.this.mCancelled) {
            if (this.mOnDismissListener != null) {
                this.mOnDismissListener.onDismiss(dialog);
            }
            return;
        }
        if (this.mOnCancelListener != null) {
            this.mOnCancelListener.onCancel(dialog);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onCancel(DialogInterface dialog) {
        if (this.mOnCancelListener != null) {
            this.mOnCancelListener.onCancel(dialog);
        }
    }

    /**
     * Method that shows the alert message with the validation warning.
     *
     * @param msg The message to show
     * @param activate If the positive button must be activate
     */
    private void setMsg(String msg, boolean activate) {
        if (msg == null || msg.length() == 0) {
            this.mMsg.setVisibility(View.GONE);
            this.mMsg.setText(""); //$NON-NLS-1$
        } else {
            this.mMsg.setText(msg);
            this.mMsg.setVisibility(View.VISIBLE);
        }
        this.mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(activate);
    }

}