Created new dialog classes for file renaming and folder creations

This commit is contained in:
David A. Velasco 2014-05-16 14:57:19 +02:00
parent 161a72b350
commit dfdc713a15
8 changed files with 276 additions and 249 deletions

View file

@ -193,6 +193,7 @@
<string name="sync_file_nothing_to_do_msg">File contents already synchronized</string>
<string name="create_dir_fail_msg">Folder could not be created</string>
<string name="filename_forbidden_characters">Forbidden characters: / \\ &lt; &gt; : " | ? *</string>
<string name="filename_empty">File name cannot be empty</string>
<string name="wait_a_moment">Wait a moment</string>
<string name="filedisplay_unexpected_bad_get_content">"Unexpected problem ; please select the file from a different app"</string>
<string name="filedisplay_no_file_selected">No file was selected</string>

View file

@ -79,9 +79,8 @@ import com.owncloud.android.operations.SynchronizeFileOperation;
import com.owncloud.android.operations.SynchronizeFolderOperation;
import com.owncloud.android.operations.UnshareLinkOperation;
import com.owncloud.android.syncadapter.FileSyncAdapter;
import com.owncloud.android.ui.dialog.EditNameDialog;
import com.owncloud.android.ui.dialog.CreateFolderDialogFragment;
import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;
import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener;
import com.owncloud.android.ui.dialog.SslUntrustedCertDialog.OnSslUntrustedCertListener;
import com.owncloud.android.ui.fragment.FileDetailFragment;
import com.owncloud.android.ui.fragment.FileFragment;
@ -102,8 +101,7 @@ import com.owncloud.android.utils.Log_OC;
*/
public class FileDisplayActivity extends HookActivity implements
FileFragment.ContainerActivity, OnNavigationListener,
OnSslUntrustedCertListener, EditNameDialogListener {
FileFragment.ContainerActivity, OnNavigationListener, OnSslUntrustedCertListener {
private ArrayAdapter<String> mDirectories;
@ -453,7 +451,8 @@ OnSslUntrustedCertListener, EditNameDialogListener {
boolean retval = true;
switch (item.getItemId()) {
case R.id.action_create_dir: {
EditNameDialog dialog = EditNameDialog.newInstance(getString(R.string.uploader_info_dirname), "", -1, -1, this);
CreateFolderDialogFragment dialog =
CreateFolderDialogFragment.newInstance(getCurrentDir());
dialog.show(getSupportFragmentManager(), "createdirdialog");
break;
}
@ -1439,21 +1438,6 @@ OnSslUntrustedCertListener, EditNameDialogListener {
}
public void onDismiss(EditNameDialog dialog) {
if (dialog.getResult()) {
String newDirectoryName = dialog.getNewFilename().trim();
Log_OC.d(TAG, "'create directory' dialog dismissed with new name " + newDirectoryName);
if (newDirectoryName.length() > 0) {
String path = getCurrentDir().getRemotePath();
// Create directory
path += newDirectoryName + OCFile.PATH_SEPARATOR;
getFileOperationsHelper().createFolder(path, false);
}
}
}
private void requestForDownload() {
Account account = getAccount();
if (!mDownloaderBinder.isDownloading(account, mWaitingToPreview)) {

View file

@ -0,0 +1,122 @@
/* ownCloud Android client application
* Copyright (C) 2014 ownCloud Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.owncloud.android.ui.dialog;
import com.actionbarsherlock.app.SherlockDialogFragment;
import com.owncloud.android.R;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.lib.resources.files.FileUtils;
import com.owncloud.android.ui.activity.ComponentsGetter;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* Dialog to input the name for a new folder to create.
*
* Triggers the folder creation when name is confirmed.
*
* @author David A. Velasco
*/
public class CreateFolderDialogFragment
extends SherlockDialogFragment implements DialogInterface.OnClickListener {
private static final String ARG_PARENT_FOLDER = "PARENT_FOLDER";
/**
* Public factory method to create new CreateFolderDialogFragment instances.
*
* @param file File to remove.
* @return Dialog ready to show.
*/
public static CreateFolderDialogFragment newInstance(OCFile parentFolder) {
CreateFolderDialogFragment frag = new CreateFolderDialogFragment();
Bundle args = new Bundle();
args.putParcelable(ARG_PARENT_FOLDER, parentFolder);
frag.setArguments(args);
return frag;
}
private OCFile mParentFolder;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
mParentFolder = getArguments().getParcelable(ARG_PARENT_FOLDER);
// Inflate the layout for the dialog
LayoutInflater inflater = getSherlockActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.edit_box_dialog, null);
// Setup layout
EditText inputText = ((EditText)v.findViewById(R.id.user_input));
inputText.setText("");
inputText.requestFocus();
// Build the dialog
AlertDialog.Builder builder = new AlertDialog.Builder(getSherlockActivity());
builder.setView(v)
.setPositiveButton(R.string.common_ok, this)
.setNegativeButton(R.string.common_cancel, this)
.setTitle(R.string.uploader_info_dirname);
Dialog d = builder.create();
d.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return d;
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == AlertDialog.BUTTON_POSITIVE) {
String newFolderName =
((TextView)(getDialog().findViewById(R.id.user_input)))
.getText().toString().trim();
if (newFolderName.length() <= 0) {
Toast.makeText(
getSherlockActivity(),
R.string.filename_empty,
Toast.LENGTH_LONG).show();
return;
}
if (!FileUtils.isValidName(newFolderName)) {
Toast.makeText(
getSherlockActivity(),
R.string.filename_forbidden_characters,
Toast.LENGTH_LONG).show();
return;
}
String path = mParentFolder.getRemotePath();
path += newFolderName + OCFile.PATH_SEPARATOR;
((ComponentsGetter)getSherlockActivity()).
getFileOperationsHelper().createFolder(path, false);
}
}
}

View file

@ -1,178 +0,0 @@
/* ownCloud Android client application
* Copyright (C) 2011 Bartek Przybylski
* Copyright (C) 2012-2013 ownCloud Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.owncloud.android.ui.dialog;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockDialogFragment;
import com.owncloud.android.R;
import com.owncloud.android.lib.resources.files.FileUtils;
/**
* Dialog to request the user to input a name, optionally initialized with a former name.
*
* @author Bartek Przybylski
* @author David A. Velasco
*/
public class EditNameDialog extends SherlockDialogFragment implements DialogInterface.OnClickListener {
public static final String TAG = EditNameDialog.class.getSimpleName();
protected static final String ARG_TITLE = "TITLE";
protected static final String ARG_NAME = "NAME";
protected static final String ARG_SELECTION_START = "SELECTION_START";
protected static final String ARG_SELECTION_END = "SELECTION_END";
private String mNewFilename;
private boolean mResult;
private EditNameDialogListener mListener;
/**
* Public factory method to get dialog instances.
*
* @param title Text to show as title in the dialog.
* @param name Optional text to include in the text input field when the dialog is shown.
* @param listener Instance to notify when the dialog is dismissed.
* @param selectionStart Index to the first character to be selected in the input field; negative value for none
* @param selectionEnd Index to the last character to be selected in the input field; negative value for none
* @return New dialog instance, ready to show.
*/
static public EditNameDialog newInstance(String title, String name, int selectionStart, int selectionEnd, EditNameDialogListener listener) {
EditNameDialog f = new EditNameDialog();
Bundle args = new Bundle();
args.putString(ARG_TITLE, title);
args.putString(ARG_NAME, name);
args.putInt(ARG_SELECTION_START, selectionStart);
args.putInt(ARG_SELECTION_END, selectionEnd);
f.setArguments(args);
f.setOnDismissListener(listener);
return f;
}
/**
* {@inheritDoc}
*/
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String currentName = getArguments().getString(ARG_NAME);
if (currentName == null)
currentName = "";
String title = getArguments().getString(ARG_TITLE);
// Inflate the layout for the dialog
LayoutInflater inflater = getSherlockActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.edit_box_dialog, null); // null parent view because it will go in the dialog layout
EditText inputText = ((EditText)v.findViewById(R.id.user_input));
inputText.setText(currentName);
// Set it to the dialog
AlertDialog.Builder builder = new AlertDialog.Builder(getSherlockActivity());
builder.setView(v)
.setPositiveButton(R.string.common_ok, this)
.setNegativeButton(R.string.common_cancel, this);
if (title != null) {
builder.setTitle(title);
}
mResult = false;
Dialog d = builder.create();
inputText.requestFocus();
int selectionStart = getArguments().getInt(ARG_SELECTION_START, -1);
int selectionEnd = getArguments().getInt(ARG_SELECTION_END, -1);
if (selectionStart >= 0 && selectionEnd >= 0) {
inputText.setSelection(Math.min(selectionStart, selectionEnd), Math.max(selectionStart, selectionEnd));
}
d.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return d;
}
/**
* Performs the corresponding action when a dialog button is clicked.
*
* Saves the text in the input field to be accessed through {@link #getNewFilename()} when the positive
* button is clicked.
*
* Notify the current listener in any case.
*/
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case AlertDialog.BUTTON_POSITIVE: {
mNewFilename = ((TextView)(getDialog().findViewById(R.id.user_input))).getText().toString();
if (!FileUtils.isValidName(mNewFilename)) {
Toast.makeText(getSherlockActivity(), R.string.filename_forbidden_characters, Toast.LENGTH_LONG).show();
return;
}
mResult = true;
}
case AlertDialog.BUTTON_NEGATIVE: { // fall through
dismiss();
if (mListener != null)
mListener.onDismiss(this);
}
}
}
protected void setOnDismissListener(EditNameDialogListener listener) {
mListener = listener;
}
/**
* Returns the text in the input field after the user clicked the positive button.
*
* @return Text in the input field.
*/
public String getNewFilename() {
return mNewFilename;
}
/**
*
* @return True when the user clicked the positive button.
*/
public boolean getResult() {
return mResult;
}
/**
* Interface to receive a notification when any button in the dialog is clicked.
*/
public interface EditNameDialogListener {
public void onDismiss(EditNameDialog dialog);
}
}

View file

@ -1,6 +1,5 @@
/* ownCloud Android client application
* Copyright (C) 2012 Bartek Przybylski
* Copyright (C) 2012-2013 ownCloud Inc.
* Copyright (C) 2014 ownCloud Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
@ -22,6 +21,8 @@ package com.owncloud.android.ui.dialog;
* Dialog requiring confirmation before removing a given OCFile.
*
* Triggers the removal according to the user response.
*
* @author David A. Velasco
*/
import com.owncloud.android.R;
import com.owncloud.android.datamodel.FileDataStorageManager;
@ -38,14 +39,10 @@ implements ConfirmationDialogFragmentListener {
private static final String ARG_TARGET_FILE = "TARGET_FILE";
/**
* Public factory method to create new RemoveFIleDialogFragment instances.
* Public factory method to create new RemoveFileDialogFragment instances.
*
* @param string_id Resource id for a message to show in the dialog.
* @param arguments Arguments to complete the message, if it's a format string.
* @param posBtn Resource id for the text of the positive button.
* @param neuBtn Resource id for the text of the neutral button.
* @param negBtn Resource id for the text of the negative button.
* @return Dialog ready to show.
* @param file File to remove.
* @return Dialog ready to show.
*/
public static RemoveFileDialogFragment newInstance(OCFile file) {
RemoveFileDialogFragment frag = new RemoveFileDialogFragment();

View file

@ -0,0 +1,136 @@
/* ownCloud Android client application
* Copyright (C) 2014 ownCloud Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.owncloud.android.ui.dialog;
/**
* Dialog to input a new name for an {@link OCFile} being renamed.
*
* Triggers the rename operation.
*/
import com.actionbarsherlock.app.SherlockDialogFragment;
import com.owncloud.android.R;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.lib.resources.files.FileUtils;
import com.owncloud.android.ui.activity.ComponentsGetter;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* Dialog to input a new name for a file or folder to rename.
*
* Triggers the rename operation when name is confirmed.
*
* @author David A. Velasco
*/
public class RenameFileDialogFragment
extends SherlockDialogFragment implements DialogInterface.OnClickListener {
private static final String ARG_TARGET_FILE = "TARGET_FILE";
/**
* Public factory method to create new RenameFileDialogFragment instances.
*
* @param file File to rename.
* @return Dialog ready to show.
*/
public static RenameFileDialogFragment newInstance(OCFile file) {
RenameFileDialogFragment frag = new RenameFileDialogFragment();
Bundle args = new Bundle();
args.putParcelable(ARG_TARGET_FILE, file);
frag.setArguments(args);
return frag;
}
private OCFile mTargetFile;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
mTargetFile = getArguments().getParcelable(ARG_TARGET_FILE);
// Inflate the layout for the dialog
LayoutInflater inflater = getSherlockActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.edit_box_dialog, null);
// Setup layout
String currentName = mTargetFile.getFileName();
EditText inputText = ((EditText)v.findViewById(R.id.user_input));
inputText.setText(currentName);
int selectionStart = 0;
int extensionStart = mTargetFile.isFolder() ? -1 : currentName.lastIndexOf(".");
int selectionEnd = (extensionStart >= 0) ? extensionStart : currentName.length();
if (selectionStart >= 0 && selectionEnd >= 0) {
inputText.setSelection(
Math.min(selectionStart, selectionEnd),
Math.max(selectionStart, selectionEnd));
}
inputText.requestFocus();
// Build the dialog
AlertDialog.Builder builder = new AlertDialog.Builder(getSherlockActivity());
builder.setView(v)
.setPositiveButton(R.string.common_ok, this)
.setNegativeButton(R.string.common_cancel, this)
.setTitle(R.string.rename_dialog_title);
Dialog d = builder.create();
d.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return d;
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == AlertDialog.BUTTON_POSITIVE) {
String newFileName =
((TextView)(getDialog().findViewById(R.id.user_input)))
.getText().toString().trim();
if (newFileName.length() <= 0) {
Toast.makeText(
getSherlockActivity(),
R.string.filename_empty,
Toast.LENGTH_LONG).show();
return;
}
if (!FileUtils.isValidName(newFileName)) {
Toast.makeText(
getSherlockActivity(),
R.string.filename_forbidden_characters,
Toast.LENGTH_LONG).show();
return;
}
((ComponentsGetter)getSherlockActivity()).
getFileOperationsHelper().renameFile(mTargetFile, newFileName);
}
}
}

View file

@ -44,9 +44,8 @@ import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
import com.owncloud.android.ui.activity.FileActivity;
import com.owncloud.android.ui.activity.FileDisplayActivity;
import com.owncloud.android.ui.dialog.EditNameDialog;
import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener;
import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
import com.owncloud.android.utils.DisplayUtils;
import com.owncloud.android.utils.Log_OC;
@ -57,8 +56,7 @@ import com.owncloud.android.utils.Log_OC;
* @author Bartek Przybylski
* @author David A. Velasco
*/
public class FileDetailFragment extends FileFragment implements
OnClickListener, EditNameDialogListener {
public class FileDetailFragment extends FileFragment implements OnClickListener {
private int mLayout;
private View mView;
@ -220,7 +218,8 @@ public class FileDetailFragment extends FileFragment implements
return true;
}
case R.id.action_rename_file: {
showDialogToRenameFile();
RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(getFile());
dialog.show(getFragmentManager(), FTAG_RENAME_FILE);
return true;
}
case R.id.action_cancel_download:
@ -289,15 +288,6 @@ public class FileDetailFragment extends FileFragment implements
}
}
private void showDialogToRenameFile() {
OCFile file = getFile();
String fileName = file.getFileName();
int extensionStart = file.isFolder() ? -1 : fileName.lastIndexOf(".");
int selectionEnd = (extensionStart >= 0) ? extensionStart : fileName.length();
EditNameDialog dialog = EditNameDialog.newInstance(getString(R.string.rename_dialog_title), fileName, 0, selectionEnd, this);
dialog.show(getFragmentManager(), FTAG_RENAME_FILE);
}
/**
* Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
*
@ -511,15 +501,6 @@ public class FileDetailFragment extends FileFragment implements
}
public void onDismiss(EditNameDialog dialog) {
if (dialog.getResult()) {
String newFilename = dialog.getNewFilename();
Log_OC.d(TAG, "name edit dialog dismissed with new name " + newFilename);
mContainerActivity.getFileOperationsHelper().renameFile(getFile(), newFilename);
}
}
public void listenForTransferProgress() {
if (mProgressListener != null) {
if (mContainerActivity.getFileDownloaderBinder() != null) {

View file

@ -27,9 +27,8 @@ import com.owncloud.android.files.FileMenuFilter;
import com.owncloud.android.ui.adapter.FileListListAdapter;
import com.owncloud.android.ui.activity.FileDisplayActivity;
import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
import com.owncloud.android.ui.dialog.EditNameDialog;
import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener;
import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
import com.owncloud.android.ui.preview.PreviewImageFragment;
import com.owncloud.android.ui.preview.PreviewMediaFragment;
import com.owncloud.android.utils.Log_OC;
@ -52,8 +51,7 @@ import android.widget.AdapterView.AdapterContextMenuInfo;
* @author masensio
* @author David A. Velasco
*/
public class OCFileListFragment extends ExtendedListFragment
implements EditNameDialogListener {
public class OCFileListFragment extends ExtendedListFragment {
private static final String TAG = OCFileListFragment.class.getSimpleName();
@ -344,10 +342,7 @@ implements EditNameDialogListener {
return true;
}
case R.id.action_rename_file: {
String fileName = mTargetFile.getFileName();
int extensionStart = mTargetFile.isFolder() ? -1 : fileName.lastIndexOf(".");
int selectionEnd = (extensionStart >= 0) ? extensionStart : fileName.length();
EditNameDialog dialog = EditNameDialog.newInstance(getString(R.string.rename_dialog_title), fileName, 0, selectionEnd, this);
RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(mTargetFile);
dialog.show(getFragmentManager(), FileDetailFragment.FTAG_RENAME_FILE);
return true;
}
@ -439,15 +434,4 @@ implements EditNameDialogListener {
}
}
@Override
public void onDismiss(EditNameDialog dialog) {
if (dialog.getResult()) {
String newFilename = dialog.getNewFilename();
Log_OC.d(TAG, "name edit dialog dismissed with new name " + newFilename);
mContainerActivity.getFileOperationsHelper().renameFile(mTargetFile, newFilename);
}
}
}