Browse Source

Refactored handle of user response in confirmation dialog for file removal to avoid duplication

David A. Velasco 11 years ago
parent
commit
21d3282c8a

+ 3 - 0
src/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -1293,6 +1293,9 @@ OnSslUntrustedCertListener, EditNameDialogListener {
             OCFile removedFile = operation.getFile();
             FileFragment second = getSecondFragment();
             if (second != null && removedFile.equals(second.getFile())) {
+                if (second instanceof PreviewMediaFragment) {
+                    ((PreviewMediaFragment)second).stopPreview(true);
+                }
                 cleanSecondFragment();
             }
             if (getStorageManager().getFileById(removedFile.getParentId()).equals(getCurrentDir())) {

+ 116 - 0
src/com/owncloud/android/ui/dialog/RemoveFileDialogFragment.java

@@ -0,0 +1,116 @@
+/* ownCloud Android client application
+ *   Copyright (C) 2012 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;
+
+/**
+ *  Dialog requiring confirmation before removing a given OCFile.  
+ * 
+ *  Triggers the removal according to the user response. 
+ */
+import com.owncloud.android.R;
+import com.owncloud.android.datamodel.FileDataStorageManager;
+import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.ui.activity.ComponentsGetter;
+import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
+
+import android.app.Dialog;
+import android.os.Bundle;
+
+public class RemoveFileDialogFragment extends ConfirmationDialogFragment 
+implements ConfirmationDialogFragmentListener {
+
+    private static final String ARG_TARGET_FILE = "TARGET_FILE";
+
+    /**
+     * 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.
+     */
+    public static RemoveFileDialogFragment newInstance(OCFile file) {
+        RemoveFileDialogFragment frag = new RemoveFileDialogFragment();
+        Bundle args = new Bundle();
+        
+        int messageStringId = R.string.confirmation_remove_alert;
+        
+        int posBtn = R.string.confirmation_remove_remote;
+        int neuBtn = -1;
+        if (file.isFolder()) {
+            messageStringId = R.string.confirmation_remove_folder_alert;
+            posBtn = R.string.confirmation_remove_remote_and_local;
+            neuBtn = R.string.confirmation_remove_folder_local;
+        } else if (file.isDown()) {
+            posBtn = R.string.confirmation_remove_remote_and_local;
+            neuBtn = R.string.confirmation_remove_local;
+        }
+        
+        
+        args.putInt(ARG_CONF_RESOURCE_ID, messageStringId);
+        args.putStringArray(ARG_CONF_ARGUMENTS, new String[]{file.getFileName()});
+        args.putInt(ARG_POSITIVE_BTN_RES, posBtn);
+        args.putInt(ARG_NEUTRAL_BTN_RES, neuBtn);
+        args.putInt(ARG_NEGATIVE_BTN_RES, R.string.common_cancel);
+        args.putParcelable(ARG_TARGET_FILE, file);
+        frag.setArguments(args);
+        
+        frag.setOnConfirmationListener(frag);
+        
+        return frag;
+    }
+
+    private OCFile mTargetFile;
+    
+    @Override
+    public Dialog onCreateDialog(Bundle savedInstanceState) {
+        Dialog dialog = super.onCreateDialog(savedInstanceState);
+        mTargetFile = getArguments().getParcelable(ARG_TARGET_FILE);
+        return dialog;
+    }    
+
+    /**
+     * Performs the removal of the target file, both locally and in the server.
+     */
+    @Override
+    public void onConfirmation(String callerTag) {
+        ComponentsGetter cg = (ComponentsGetter)getSherlockActivity();
+        FileDataStorageManager storageManager = cg.getStorageManager();
+        if (storageManager.getFileById(mTargetFile.getFileId()) != null) {
+            cg.getFileOperationsHelper().removeFile(mTargetFile, false);
+        }
+    }
+    
+    /**
+     * Performs the removal of the local copy of the taget file
+     */
+    @Override
+    public void onNeutral(String callerTag) {
+        ((ComponentsGetter)getSherlockActivity()).getFileOperationsHelper()
+            .removeFile(mTargetFile, true);
+    }
+
+    @Override
+    public void onCancel(String callerTag) {
+        // nothing to do here
+    }
+    
+}

+ 14 - 52
src/com/owncloud/android/ui/fragment/FileDetailFragment.java

@@ -44,8 +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.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.utils.DisplayUtils;
 import com.owncloud.android.utils.Log_OC;
@@ -58,8 +58,7 @@ import com.owncloud.android.utils.Log_OC;
  * @author David A. Velasco
  */
 public class FileDetailFragment extends FileFragment implements
-        OnClickListener, 
-        ConfirmationDialogFragment.ConfirmationDialogFragmentListener, EditNameDialogListener {
+        OnClickListener, EditNameDialogListener {
 
     private int mLayout;
     private View mView;
@@ -101,7 +100,7 @@ public class FileDetailFragment extends FileFragment implements
     
     
     @Override
-    public void onCreate(Bundle savedInstanceState) {
+    public void onActivityCreated(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setHasOptionsMenu(true);
     }
@@ -178,13 +177,15 @@ public class FileDetailFragment extends FileFragment implements
     public void onPrepareOptionsMenu (Menu menu) {
         super.onPrepareOptionsMenu(menu);
         
-        FileMenuFilter mf = new FileMenuFilter(
-            getFile(),
-            mContainerActivity.getStorageManager().getAccount(),
-            mContainerActivity,
-            getSherlockActivity()
-        );
-        mf.filter(menu);
+        if (mContainerActivity.getStorageManager() != null) {
+            FileMenuFilter mf = new FileMenuFilter(
+                getFile(),
+                mContainerActivity.getStorageManager().getAccount(),
+                mContainerActivity,
+                getSherlockActivity()
+            );
+            mf.filter(menu);
+        }
         
         // additional restriction for this fragment 
         MenuItem item = menu.findItem(R.id.action_see_details);
@@ -214,7 +215,8 @@ public class FileDetailFragment extends FileFragment implements
                 return true;
             }
             case R.id.action_remove_file: {
-                showDialogToRemoveFile();
+                RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(getFile());
+                dialog.show(getFragmentManager(), FTAG_CONFIRMATION);
                 return true;
             }
             case R.id.action_rename_file: {
@@ -287,19 +289,6 @@ public class FileDetailFragment extends FileFragment implements
         }
     }
 
-    private void showDialogToRemoveFile() {
-        OCFile file = getFile();
-        ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(
-                R.string.confirmation_remove_alert,
-                new String[]{file.getFileName()},
-                file.isDown() ? R.string.confirmation_remove_remote_and_local : R.string.confirmation_remove_remote,
-                file.isDown() ? R.string.confirmation_remove_local : -1,
-                R.string.common_cancel);
-        confDialog.setOnConfirmationListener(this);
-        confDialog.show(getFragmentManager(), FTAG_CONFIRMATION);
-    }
-
-
     private void showDialogToRenameFile() {
         OCFile file = getFile();
         String fileName = file.getFileName();
@@ -308,33 +297,6 @@ public class FileDetailFragment extends FileFragment implements
         EditNameDialog dialog = EditNameDialog.newInstance(getString(R.string.rename_dialog_title), fileName, 0, selectionEnd, this);
         dialog.show(getFragmentManager(), FTAG_RENAME_FILE);
     }
-
-    
-    @Override
-    public void onConfirmation(String callerTag) {
-        OCFile file = getFile();
-        if (callerTag.equals(FTAG_CONFIRMATION)) {
-            if (mContainerActivity.getStorageManager().getFileById(file.getFileId()) != null) {
-                mContainerActivity.getFileOperationsHelper().removeFile(file, false);
-            }
-        }
-    }
-    
-    @Override
-    public void onNeutral(String callerTag) {
-        OCFile file = getFile();
-        mContainerActivity.getFileOperationsHelper().removeFile(file, true);
-        //if (file.getStoragePath() != null) {
-        //    file.setStoragePath(null);
-        //    updateFileDetails(file, mAccount);
-        //}
-    }
-    
-    @Override
-    public void onCancel(String callerTag) {
-        Log_OC.d(TAG, "REMOVAL CANCELED");
-    }
-    
     
     /**
      * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.

+ 13 - 51
src/com/owncloud/android/ui/fragment/OCFileListFragment.java

@@ -28,7 +28,7 @@ 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.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
+import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
 import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener;
 import com.owncloud.android.ui.preview.PreviewImageFragment;
 import com.owncloud.android.ui.preview.PreviewMediaFragment;
@@ -53,7 +53,7 @@ import android.widget.AdapterView.AdapterContextMenuInfo;
  * @author David A. Velasco
  */
 public class OCFileListFragment extends ExtendedListFragment 
-implements EditNameDialogListener, ConfirmationDialogFragmentListener {
+implements EditNameDialogListener {
     
     private static final String TAG = OCFileListFragment.class.getSimpleName();
 
@@ -295,13 +295,15 @@ implements EditNameDialogListener, ConfirmationDialogFragmentListener {
         AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
         OCFile targetFile = (OCFile) mAdapter.getItem(info.position);
         
-        FileMenuFilter mf = new FileMenuFilter(
-            targetFile,
-            mContainerActivity.getStorageManager().getAccount(),
-            mContainerActivity,
-            getSherlockActivity()
-        );
-        mf.filter(menu);
+        if (mContainerActivity.getStorageManager() != null) {
+            FileMenuFilter mf = new FileMenuFilter(
+                targetFile,
+                mContainerActivity.getStorageManager().getAccount(),
+                mContainerActivity,
+                getSherlockActivity()
+            );
+            mf.filter(menu);
+        }
         
         /// additional restrictions for this fragment 
         // TODO allow in the future 'open with' for previewable files
@@ -350,25 +352,8 @@ implements EditNameDialogListener, ConfirmationDialogFragmentListener {
                 return true;
             }
             case R.id.action_remove_file: {
-                int messageStringId = R.string.confirmation_remove_alert;
-                int posBtnStringId = R.string.confirmation_remove_remote;
-                int neuBtnStringId = -1;
-                if (mTargetFile.isFolder()) {
-                    messageStringId = R.string.confirmation_remove_folder_alert;
-                    posBtnStringId = R.string.confirmation_remove_remote_and_local;
-                    neuBtnStringId = R.string.confirmation_remove_folder_local;
-                } else if (mTargetFile.isDown()) {
-                    posBtnStringId = R.string.confirmation_remove_remote_and_local;
-                    neuBtnStringId = R.string.confirmation_remove_local;
-                }
-                ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(
-                        messageStringId,
-                        new String[]{mTargetFile.getFileName()},
-                        posBtnStringId,
-                        neuBtnStringId,
-                        R.string.common_cancel);
-                confDialog.setOnConfirmationListener(this);
-                confDialog.show(getFragmentManager(), FileDetailFragment.FTAG_CONFIRMATION);
+                RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(mTargetFile);
+                dialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
                 return true;
             }
             case R.id.action_download_file: 
@@ -464,28 +449,5 @@ implements EditNameDialogListener, ConfirmationDialogFragmentListener {
             mContainerActivity.getFileOperationsHelper().renameFile(mTargetFile, newFilename);
         }
     }
-
     
-    @Override
-    public void onConfirmation(String callerTag) {
-        if (callerTag.equals(FileDetailFragment.FTAG_CONFIRMATION)) {
-            FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
-            if (storageManager.getFileById(mTargetFile.getFileId()) != null) {
-                mContainerActivity.getFileOperationsHelper().removeFile(mTargetFile, false);
-            }
-        }
-    }
-    
-    @Override
-    public void onNeutral(String callerTag) {
-        mContainerActivity.getFileOperationsHelper().removeFile(mTargetFile, true);
-        //listDirectory();
-        //mContainerActivity.onTransferStateChanged(mTargetFile, false, false);
-    }
-    
-    @Override
-    public void onCancel(String callerTag) {
-        Log_OC.d(TAG, "REMOVAL CANCELED");
-    }
-
 }

+ 13 - 61
src/com/owncloud/android/ui/preview/PreviewImageFragment.java

@@ -42,11 +42,11 @@ import com.actionbarsherlock.view.Menu;
 import com.actionbarsherlock.view.MenuInflater;
 import com.actionbarsherlock.view.MenuItem;
 import com.owncloud.android.R;
-import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.files.FileMenuFilter;
 import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
+import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
 import com.owncloud.android.ui.fragment.FileFragment;
 import com.owncloud.android.utils.Log_OC;
 
@@ -60,8 +60,7 @@ import com.owncloud.android.utils.Log_OC;
  * 
  * @author David A. Velasco
  */
-public class PreviewImageFragment extends FileFragment implements 
-ConfirmationDialogFragment.ConfirmationDialogFragmentListener {
+public class PreviewImageFragment extends FileFragment {
     public static final String EXTRA_FILE = "FILE";
     public static final String EXTRA_ACCOUNT = "ACCOUNT";
 
@@ -226,13 +225,15 @@ ConfirmationDialogFragment.ConfirmationDialogFragmentListener {
     public void onPrepareOptionsMenu(Menu menu) {
         super.onPrepareOptionsMenu(menu);
         
-        FileMenuFilter mf = new FileMenuFilter(
-            getFile(),
-            mContainerActivity.getStorageManager().getAccount(),
-            mContainerActivity,
-            getSherlockActivity()
-        );
-        mf.filter(menu);
+        if (mContainerActivity.getStorageManager() != null) {
+            FileMenuFilter mf = new FileMenuFilter(
+                getFile(),
+                mContainerActivity.getStorageManager().getAccount(),
+                mContainerActivity,
+                getSherlockActivity()
+            );
+            mf.filter(menu);
+        }
         
         // additional restriction for this fragment 
         // TODO allow renaming in PreviewImageFragment
@@ -264,7 +265,8 @@ ConfirmationDialogFragment.ConfirmationDialogFragmentListener {
                 return true;
             }
             case R.id.action_remove_file: {
-                removeFile();
+                RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(getFile());
+                dialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
                 return true;
             }
             case R.id.action_see_details: {
@@ -321,56 +323,6 @@ ConfirmationDialogFragment.ConfirmationDialogFragmentListener {
     }
     
     
-    /**
-     * Starts a the removal of the previewed file.
-     * 
-     * Shows a confirmation dialog. The action continues in {@link #onConfirmation(String)} , {@link #onNeutral(String)} or {@link #onCancel(String)},
-     * depending upon the user selection in the dialog. 
-     */
-    private void removeFile() {
-        ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(
-                R.string.confirmation_remove_alert,
-                new String[]{getFile().getFileName()},
-                R.string.confirmation_remove_remote_and_local,
-                R.string.confirmation_remove_local,
-                R.string.common_cancel);
-        confDialog.setOnConfirmationListener(this);
-        confDialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
-    }
-
-    
-    /**
-     * Performs the removal of the previewed file, both locally and in the server.
-     */
-    @Override
-    public void onConfirmation(String callerTag) {
-        FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
-        if (storageManager.getFileById(getFile().getFileId()) != null) {   // check that the file is still there;
-            mContainerActivity.getFileOperationsHelper().removeFile(getFile(), false);
-        }
-    }
-    
-    
-    /**
-     * Removes the file from local storage
-     */
-    @Override
-    public void onNeutral(String callerTag) {
-        OCFile file = getFile();
-        mContainerActivity.getFileOperationsHelper().removeFile(file, true);
-        //mContainerActivity.getStorageManager().removeFile(file, false, true);    // TODO perform in background task / new thread
-        //finish();
-    }
-    
-    /**
-     * User cancelled the removal action.
-     */
-    @Override
-    public void onCancel(String callerTag) {
-        // nothing to do here
-    }
-    
-
     private class BitmapLoader extends AsyncTask<String, Void, Bitmap> {
 
         /**

+ 14 - 65
src/com/owncloud/android/ui/preview/PreviewMediaFragment.java

@@ -45,7 +45,6 @@ import com.actionbarsherlock.view.Menu;
 import com.actionbarsherlock.view.MenuInflater;
 import com.actionbarsherlock.view.MenuItem;
 import com.owncloud.android.R;
-import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.files.FileMenuFilter;
 import com.owncloud.android.media.MediaControlView;
@@ -53,6 +52,7 @@ import com.owncloud.android.media.MediaService;
 import com.owncloud.android.media.MediaServiceBinder;
 import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
+import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
 import com.owncloud.android.ui.fragment.FileFragment;
 import com.owncloud.android.utils.Log_OC;
 
@@ -67,8 +67,7 @@ import com.owncloud.android.utils.Log_OC;
  * @author David A. Velasco
  */
 public class PreviewMediaFragment extends FileFragment implements
-        OnTouchListener,  
-        ConfirmationDialogFragment.ConfirmationDialogFragmentListener  {
+        OnTouchListener {
 
     public static final String EXTRA_FILE = "FILE";
     public static final String EXTRA_ACCOUNT = "ACCOUNT";
@@ -257,13 +256,15 @@ public class PreviewMediaFragment extends FileFragment implements
     public void onPrepareOptionsMenu(Menu menu) {
         super.onPrepareOptionsMenu(menu);
         
-        FileMenuFilter mf = new FileMenuFilter(
-            getFile(),
-            mContainerActivity.getStorageManager().getAccount(),
-            mContainerActivity,
-            getSherlockActivity()
-        );
-        mf.filter(menu);
+        if (mContainerActivity.getStorageManager() != null) {
+            FileMenuFilter mf = new FileMenuFilter(
+                getFile(),
+                mContainerActivity.getStorageManager().getAccount(),
+                mContainerActivity,
+                getSherlockActivity()
+            );
+            mf.filter(menu);
+        }
 
         // additional restriction for this fragment 
         // TODO allow renaming in PreviewImageFragment
@@ -296,7 +297,8 @@ public class PreviewMediaFragment extends FileFragment implements
                 return true;
             }
             case R.id.action_remove_file: {
-                removeFile();
+                RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(getFile());
+                dialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
                 return true;
             }
             case R.id.action_see_details: {
@@ -591,59 +593,6 @@ public class PreviewMediaFragment extends FileFragment implements
         finish();
     }
     
-    /**
-     * Starts a the removal of the previewed file.
-     * 
-     * Shows a confirmation dialog. The action continues in {@link #onConfirmation(String)} , {@link #onNeutral(String)} or {@link #onCancel(String)},
-     * depending upon the user selection in the dialog. 
-     */
-    private void removeFile() {
-        ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(
-                R.string.confirmation_remove_alert,
-                new String[]{getFile().getFileName()},
-                R.string.confirmation_remove_remote_and_local,
-                R.string.confirmation_remove_local,
-                R.string.common_cancel);
-        confDialog.setOnConfirmationListener(this);
-        confDialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
-    }
-
-    
-    /**
-     * Performs the removal of the previewed file, both locally and in the server.
-     */
-    @Override
-    public void onConfirmation(String callerTag) {
-        OCFile file = getFile();
-        FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
-        if (storageManager.getFileById(file.getFileId()) != null) {   // check that the file is still there;
-            stopPreview(true);
-            mContainerActivity.getFileOperationsHelper().removeFile(file, false);
-        }
-    }
-    
-    
-    /**
-     * Removes the file from local storage
-     */
-    @Override
-    public void onNeutral(String callerTag) {
-        OCFile file = getFile();
-        stopPreview(true);
-        //mContainerActivity.getStorageManager().removeFile(file, false, true);    // TODO perform in background task / new thread
-        mContainerActivity.getFileOperationsHelper().removeFile(file, true);
-        //finish();
-    }
-    
-    /**
-     * User cancelled the removal action.
-     */
-    @Override
-    public void onCancel(String callerTag) {
-        // nothing to do here
-    }
-    
-
     /**
      * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment} to be previewed.
      * 
@@ -655,7 +604,7 @@ public class PreviewMediaFragment extends FileFragment implements
     }
     
 
-    private void stopPreview(boolean stopAudio) {
+    public void stopPreview(boolean stopAudio) {
         OCFile file = getFile();
         if (file.isAudio() && stopAudio) {
             mMediaServiceBinder.pause();