1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package com
.owncloud
.android
.ui
.dialog
;
22 import android
.app
.AlertDialog
;
23 import android
.app
.Dialog
;
24 import android
.content
.DialogInterface
;
25 import android
.os
.Bundle
;
26 import android
.view
.LayoutInflater
;
27 import android
.view
.View
;
28 import android
.view
.WindowManager
.LayoutParams
;
29 import android
.widget
.TextView
;
31 import com
.actionbarsherlock
.app
.SherlockDialogFragment
;
32 import com
.owncloud
.android
.R
;
36 * Dialog to request the user to input a name, optionally initialized with a former name.
38 * @author Bartek Przybylski
39 * @author David A. Velasco
41 public class EditNameDialog
extends SherlockDialogFragment
implements DialogInterface
.OnClickListener
{
43 public static final String TAG
= EditNameDialog
.class.getSimpleName();
45 protected static final String ARG_TITLE
= "title";
46 protected static final String ARG_NAME
= "name";
48 private String mNewFilename
;
49 private boolean mResult
;
50 private EditNameDialogListener mListener
;
53 * Public factory method to get dialog instances.
55 * @param title Text to show as title in the dialog.
56 * @param name Optional text to include in the text input field when the dialog is shown.
57 * @param listener Instance to notify when the dialog is dismissed.
58 * @return New dialog instance, ready to show.
60 static public EditNameDialog
newInstance(String title
, String name
, EditNameDialogListener listener
) {
61 EditNameDialog f
= new EditNameDialog();
62 Bundle args
= new Bundle();
63 args
.putString(ARG_TITLE
, title
);
64 args
.putString(ARG_NAME
, name
);
66 f
.setOnDismissListener(listener
);
75 public Dialog
onCreateDialog(Bundle savedInstanceState
) {
76 String currentName
= getArguments().getString(ARG_NAME
);
77 if (currentName
== null
)
79 String title
= getArguments().getString(ARG_TITLE
);
81 // Inflate the layout for the dialog
82 LayoutInflater inflater
= getSherlockActivity().getLayoutInflater();
83 View v
= inflater
.inflate(R
.layout
.edit_box_dialog
, null
); // null parent view because it will go in the dialog layout
84 TextView inputText
= ((TextView
)v
.findViewById(R
.id
.user_input
));
85 inputText
.setText(currentName
);
87 // Set it to the dialog
88 AlertDialog
.Builder builder
= new AlertDialog
.Builder(getSherlockActivity());
90 .setPositiveButton(R
.string
.common_ok
, this)
91 .setNegativeButton(R
.string
.common_cancel
, this);
94 builder
.setTitle(title
);
99 Dialog d
= builder
.create();
101 inputText
.requestFocus();
102 d
.getWindow().setSoftInputMode(LayoutParams
.SOFT_INPUT_STATE_VISIBLE
);
108 * Performs the corresponding action when a dialog button is clicked.
110 * Saves the text in the input field to be accessed through {@link #getNewFilename()} when the positive
113 * Notify the current listener in any case.
116 public void onClick(DialogInterface dialog
, int which
) {
118 case AlertDialog
.BUTTON_POSITIVE
: {
119 mNewFilename
= ((TextView
)(getDialog().findViewById(R
.id
.user_input
))).getText().toString();
122 case AlertDialog
.BUTTON_NEGATIVE
: { // fall through
124 if (mListener
!= null
)
125 mListener
.onDismiss(this);
130 protected void setOnDismissListener(EditNameDialogListener listener
) {
131 mListener
= listener
;
135 * Returns the text in the input field after the user clicked the positive button.
137 * @return Text in the input field.
139 public String
getNewFilename() {
145 * @return True when the user clicked the positive button.
147 public boolean getResult() {
153 * Interface to receive a notification when any button in the dialog is clicked.
155 public interface EditNameDialogListener
{
156 public void onDismiss(EditNameDialog dialog
);