X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/dc984ec5bf840f8ea95467d1e9da501b645a7fbc..d5625e6cccd85dfb0f9e66a2da8f844341c96e2b:/src/com/owncloud/android/ui/activity/MoveActivity.java diff --git a/src/com/owncloud/android/ui/activity/MoveActivity.java b/src/com/owncloud/android/ui/activity/MoveActivity.java index 5d431c07..2d6824c8 100644 --- a/src/com/owncloud/android/ui/activity/MoveActivity.java +++ b/src/com/owncloud/android/ui/activity/MoveActivity.java @@ -1,3 +1,20 @@ +/* ownCloud Android client application + * Copyright (C) 2012-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 . + * + */ + package com.owncloud.android.ui.activity; import java.io.IOException; @@ -10,20 +27,18 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.res.Resources.NotFoundException; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; +import android.support.v4.widget.SwipeRefreshLayout; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; -import android.view.ViewGroup; -import android.widget.ArrayAdapter; import android.widget.Button; -import android.widget.TextView; import android.widget.Toast; import com.actionbarsherlock.app.ActionBar; -import com.actionbarsherlock.app.ActionBar.OnNavigationListener; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem; @@ -38,29 +53,29 @@ import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundExce import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; +import com.owncloud.android.operations.CreateFolderOperation; import com.owncloud.android.operations.SynchronizeFolderOperation; -import com.owncloud.android.services.observer.FileObserverService; import com.owncloud.android.syncadapter.FileSyncAdapter; import com.owncloud.android.ui.dialog.CreateFolderDialogFragment; import com.owncloud.android.ui.fragment.FileFragment; -import com.owncloud.android.ui.fragment.MoveFileListFragment; +import com.owncloud.android.ui.fragment.OCFileListFragment; import com.owncloud.android.utils.DisplayUtils; -import com.owncloud.android.utils.Log_OC; +import com.owncloud.android.utils.ErrorMessageAdapter; +import com.owncloud.android.lib.common.utils.Log_OC; public class MoveActivity extends HookActivity implements FileFragment.ContainerActivity, - OnNavigationListener, OnClickListener{ - - private ArrayAdapter mDirectories; - - private SyncBroadcastReceiver mSyncBroadcastReceiver; + OnClickListener, SwipeRefreshLayout.OnRefreshListener { - public static final int DIALOG_SHORT_WAIT = 0; + public static final String EXTRA_CURRENT_FOLDER = UploadFilesActivity.class.getCanonicalName() + ".EXTRA_CURRENT_FOLDER"; + public static final String EXTRA_TARGET_FILE = UploadFilesActivity.class.getCanonicalName() + "EXTRA_TARGET_FILE"; + + public static final int RESULT_OK_AND_MOVE = 1; - public static final String ACTION_DETAILS = "com.owncloud.android.ui.activity.action.DETAILS"; + private SyncBroadcastReceiver mSyncBroadcastReceiver; private static final String TAG = MoveActivity.class.getSimpleName(); - private static final String TAG_LIST_OF_FILES = "LIST_OF_FILES"; + private static final String TAG_LIST_OF_FOLDERS = "LIST_OF_FOLDERS"; private boolean mSyncInProgress = false; @@ -73,39 +88,78 @@ public class MoveActivity extends HookActivity implements FileFragment.Container Log_OC.d(TAG, "onCreate() start"); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); - super.onCreate(savedInstanceState); // this calls onAccountChanged() when ownCloud Account is valid - - /// grant that FileObserverService is watching favourite files - if (savedInstanceState == null) { - Intent initObserversIntent = FileObserverService.makeInitIntent(this); - startService(initObserversIntent); - } - - /// USER INTERFACE + super.onCreate(savedInstanceState); - // Inflate and set the layout view - setContentView(R.layout.files_move); + setContentView(R.layout.files_move); + if (savedInstanceState == null) { - createMinFragments(); + createFragments(); } + // sets callback listeners for UI elements initControls(); // Action bar setup - mDirectories = new CustomArrayAdapter(this, R.layout.sherlock_spinner_dropdown_item); - getSupportActionBar().setHomeButtonEnabled(true); // mandatory since Android ICS, according to the official documentation - setSupportProgressBarIndeterminateVisibility(mSyncInProgress /*|| mRefreshSharesInProgress*/); // always AFTER setContentView(...) ; to work around bug in its implementation + ActionBar actionBar = getSupportActionBar(); + actionBar.setDisplayShowTitleEnabled(true); + actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); + setSupportProgressBarIndeterminateVisibility(mSyncInProgress); + // always AFTER setContentView(...) ; to work around bug in its implementation + // sets message for empty list of folders setBackgroundText(); Log_OC.d(TAG, "onCreate() end"); } - private void createMinFragments() { - MoveFileListFragment listOfFiles = new MoveFileListFragment(); + @Override + protected void onStart() { + super.onStart(); + getSupportActionBar().setIcon(DisplayUtils.getSeasonalIconId()); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + } + + /** + * Called when the ownCloud {@link Account} associated to the Activity was just updated. + */ + @Override + protected void onAccountSet(boolean stateWasRecovered) { + super.onAccountSet(stateWasRecovered); + if (getAccount() != null) { + + updateFileFromDB(); + + OCFile folder = getFile(); + if (folder == null || !folder.isFolder()) { + // fall back to root folder + setFile(getStorageManager().getFileByPath(OCFile.ROOT_PATH)); + folder = getFile(); + } + + if (!stateWasRecovered) { + OCFileListFragment listOfFolders = getListOfFilesFragment(); + listOfFolders.listDirectory(folder); + + startSyncFolderOperation(folder, false); + } + + updateNavigationElementsInActionBar(); + } + } + + private void createFragments() { + OCFileListFragment listOfFiles = new OCFileListFragment(); + Bundle args = new Bundle(); + args.putBoolean(OCFileListFragment.ARG_JUST_FOLDERS, true); + args.putBoolean(OCFileListFragment.ARG_ALLOW_CONTEXTUAL_ACTIONS, false); + listOfFiles.setArguments(args); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); - transaction.add(R.id.fragment_container, listOfFiles, TAG_LIST_OF_FILES); + transaction.add(R.id.fragment_container, listOfFiles, TAG_LIST_OF_FOLDERS); transaction.commit(); } @@ -114,64 +168,29 @@ public class MoveActivity extends HookActivity implements FileFragment.Container * loading or folder is empty */ private void setBackgroundText() { - MoveFileListFragment MoveFileListFragment = getListOfFilesFragment(); - if (MoveFileListFragment != null) { + OCFileListFragment listFragment = getListOfFilesFragment(); + if (listFragment != null) { int message = R.string.file_list_loading; if (!mSyncInProgress) { // In case folder list is empty message = R.string.file_list_empty_moving; } - MoveFileListFragment.setMessageForEmptyList(getString(message)); + listFragment.setMessageForEmptyList(getString(message)); } else { - Log.e(TAG, "MoveFileListFragment is null"); + Log.e(TAG, "OCFileListFragment is null"); } } - private MoveFileListFragment getListOfFilesFragment() { - Fragment listOfFiles = getSupportFragmentManager().findFragmentByTag(MoveActivity.TAG_LIST_OF_FILES); + private OCFileListFragment getListOfFilesFragment() { + Fragment listOfFiles = getSupportFragmentManager().findFragmentByTag(MoveActivity.TAG_LIST_OF_FOLDERS); if (listOfFiles != null) { - return (MoveFileListFragment)listOfFiles; + return (OCFileListFragment)listOfFiles; } Log_OC.wtf(TAG, "Access to unexisting list of files fragment!!"); return null; } - // Custom array adapter to override text colors - private class CustomArrayAdapter extends ArrayAdapter { - - public CustomArrayAdapter(MoveActivity ctx, int view) { - super(ctx, view); - } - - public View getView(int position, View convertView, ViewGroup parent) { - View v = super.getView(position, convertView, parent); - - ((TextView) v).setTextColor(getResources().getColorStateList( - android.R.color.white)); - - fixRoot((TextView) v ); - return v; - } - - public View getDropDownView(int position, View convertView, - ViewGroup parent) { - View v = super.getDropDownView(position, convertView, parent); - - ((TextView) v).setTextColor(getResources().getColorStateList( - android.R.color.white)); - - fixRoot((TextView) v ); - return v; - } - - private void fixRoot(TextView v) { - if (v.getText().equals(OCFile.PATH_SEPARATOR)) { - v.setText(R.string.default_display_name_for_root_folder); - } - } - - } - + /** * {@inheritDoc} * @@ -179,46 +198,15 @@ public class MoveActivity extends HookActivity implements FileFragment.Container */ @Override public void onBrowsedDownTo(OCFile directory) { - pushDirname(directory); - + setFile(directory); + updateNavigationElementsInActionBar(); // Sync Folder - startSyncFolderOperation(directory); + startSyncFolderOperation(directory, false); } - /** - * Shows the information of the {@link OCFile} received as a - * parameter in the second fragment. - * - * @param file {@link OCFile} whose details will be shown - */ - @Override - public void showDetails(OCFile file) { - - } - - /** - * {@inheritDoc} - */ - @Override - public void onTransferStateChanged(OCFile file, boolean downloading, boolean uploading) { - - } - - /** - * Pushes a directory to the drop down list - * @param directory to push - * @throws IllegalArgumentException If the {@link OCFile#isFolder()} returns false. - */ - public void pushDirname(OCFile directory) { - if(!directory.isFolder()){ - throw new IllegalArgumentException("Only directories may be pushed!"); - } - mDirectories.insert(directory.getFileName(), 0); - setFile(directory); - } - - public void startSyncFolderOperation(OCFile folder) { + + public void startSyncFolderOperation(OCFile folder, boolean ignoreETag) { long currentSyncTime = System.currentTimeMillis(); mSyncInProgress = true; @@ -228,6 +216,7 @@ public class MoveActivity extends HookActivity implements FileFragment.Container currentSyncTime, false, getFileOperationsHelper().isSharedSupported(), + ignoreETag, getStorageManager(), getAccount(), getApplicationContext() @@ -258,18 +247,20 @@ public class MoveActivity extends HookActivity implements FileFragment.Container Log_OC.d(TAG, "onResume() end"); } - - @Override - protected void onStart() { - super.onStart(); - getSupportActionBar().setIcon(DisplayUtils.getSeasonalIconId()); - } - + @Override - protected void onDestroy() { - super.onDestroy(); + protected void onPause() { + Log_OC.e(TAG, "onPause() start"); + if (mSyncBroadcastReceiver != null) { + unregisterReceiver(mSyncBroadcastReceiver); + //LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncBroadcastReceiver); + mSyncBroadcastReceiver = null; + } + + Log_OC.d(TAG, "onPause() end"); + super.onPause(); } - + @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getSherlock().getMenuInflater(); @@ -286,12 +277,15 @@ public class MoveActivity extends HookActivity implements FileFragment.Container switch (item.getItemId()) { case R.id.action_create_dir: { CreateFolderDialogFragment dialog = - CreateFolderDialogFragment.newInstance(getCurrentDir()); - dialog.show(getSupportFragmentManager(), "createdirdialog"); + CreateFolderDialogFragment.newInstance(getCurrentFolder()); + dialog.show( + getSupportFragmentManager(), + CreateFolderDialogFragment.CREATE_FOLDER_FRAGMENT + ); break; } case android.R.id.home: { - OCFile currentDir = getCurrentDir(); + OCFile currentDir = getCurrentFolder(); if(currentDir != null && currentDir.getParentId() != 0) { onBackPressed(); } @@ -303,7 +297,7 @@ public class MoveActivity extends HookActivity implements FileFragment.Container return retval; } - private OCFile getCurrentDir() { + private OCFile getCurrentFolder() { OCFile file = getFile(); if (file != null) { if (file.isFolder()) { @@ -317,12 +311,118 @@ public class MoveActivity extends HookActivity implements FileFragment.Container } protected void refreshListOfFilesFragment() { - MoveFileListFragment fileListFragment = getListOfFilesFragment(); + OCFileListFragment fileListFragment = getListOfFilesFragment(); if (fileListFragment != null) { fileListFragment.listDirectory(); } } + public void browseToRoot() { + OCFileListFragment listOfFiles = getListOfFilesFragment(); + if (listOfFiles != null) { // should never be null, indeed + OCFile root = getStorageManager().getFileByPath(OCFile.ROOT_PATH); + listOfFiles.listDirectory(root); + setFile(listOfFiles.getCurrentFile()); + updateNavigationElementsInActionBar(); + startSyncFolderOperation(root, false); + } + } + + @Override + public void onBackPressed() { + OCFileListFragment listOfFiles = getListOfFilesFragment(); + if (listOfFiles != null) { // should never be null, indeed + int levelsUp = listOfFiles.onBrowseUp(); + if (levelsUp == 0) { + finish(); + return; + } + setFile(listOfFiles.getCurrentFile()); + updateNavigationElementsInActionBar(); + } + } + + private void updateNavigationElementsInActionBar() { + ActionBar actionBar = getSupportActionBar(); + OCFile currentDir = getCurrentFolder(); + boolean atRoot = (currentDir == null || currentDir.getParentId() == 0); + actionBar.setDisplayHomeAsUpEnabled(!atRoot); + actionBar.setHomeButtonEnabled(!atRoot); + actionBar.setTitle( + atRoot + ? getString(R.string.default_display_name_for_root_folder) + : currentDir.getFileName() + ); + } + + /** + * Set per-view controllers + */ + private void initControls(){ + mCancelBtn = (Button) findViewById(R.id.move_files_btn_cancel); + mCancelBtn.setOnClickListener(this); + mChooseBtn = (Button) findViewById(R.id.move_files_btn_choose); + mChooseBtn.setOnClickListener(this); + } + + @Override + public void onClick(View v) { + if (v == mCancelBtn) { + finish(); + } else if (v == mChooseBtn) { + Intent i = getIntent(); + OCFile targetFile = (OCFile) i.getParcelableExtra(MoveActivity.EXTRA_TARGET_FILE); + + Intent data = new Intent(); + data.putExtra(EXTRA_CURRENT_FOLDER, getCurrentFolder()); + data.putExtra(EXTRA_TARGET_FILE, targetFile); + setResult(RESULT_OK_AND_MOVE, data); + finish(); + } + } + + + @Override + public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) { + super.onRemoteOperationFinish(operation, result); + + if (operation instanceof CreateFolderOperation) { + onCreateFolderOperationFinish((CreateFolderOperation)operation, result); + + } + } + + + /** + * Updates the view associated to the activity after the finish of an operation trying + * to create a new folder. + * + * @param operation Creation operation performed. + * @param result Result of the creation. + */ + private void onCreateFolderOperationFinish( + CreateFolderOperation operation, RemoteOperationResult result + ) { + + if (result.isSuccess()) { + dismissLoadingDialog(); + refreshListOfFilesFragment(); + } else { + dismissLoadingDialog(); + try { + Toast msg = Toast.makeText(MoveActivity.this, + ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()), + Toast.LENGTH_LONG); + msg.show(); + + } catch (NotFoundException e) { + Log_OC.e(TAG, "Error while trying to show fail message " , e); + } + } + } + + + private class SyncBroadcastReceiver extends BroadcastReceiver { /** @@ -345,12 +445,12 @@ public class MoveActivity extends HookActivity implements FileFragment.Container } else { OCFile currentFile = (getFile() == null) ? null : getStorageManager().getFileByPath(getFile().getRemotePath()); - OCFile currentDir = (getCurrentDir() == null) ? null : getStorageManager().getFileByPath(getCurrentDir().getRemotePath()); + OCFile currentDir = (getCurrentFolder() == null) ? null : getStorageManager().getFileByPath(getCurrentFolder().getRemotePath()); if (currentDir == null) { // current folder was removed from the server Toast.makeText( MoveActivity.this, - String.format(getString(R.string.sync_current_folder_was_removed), mDirectories.getItem(0)), + String.format(getString(R.string.sync_current_folder_was_removed), getCurrentFolder().getFileName()), Toast.LENGTH_LONG) .show(); browseToRoot(); @@ -362,7 +462,7 @@ public class MoveActivity extends HookActivity implements FileFragment.Container } if (synchFolderRemotePath != null && currentDir.getRemotePath().equals(synchFolderRemotePath)) { - MoveFileListFragment fileListFragment = getListOfFilesFragment(); + OCFileListFragment fileListFragment = getListOfFilesFragment(); if (fileListFragment != null) { fileListFragment.listDirectory(currentDir); } @@ -433,174 +533,37 @@ public class MoveActivity extends HookActivity implements FileFragment.Container } } - public void browseToRoot() { - MoveFileListFragment listOfFiles = getListOfFilesFragment(); - if (listOfFiles != null) { // should never be null, indeed - while (mDirectories.getCount() > 1) { - popDirname(); - } - OCFile root = getStorageManager().getFileByPath(OCFile.ROOT_PATH); - listOfFiles.listDirectory(root); - setFile(listOfFiles.getCurrentFile()); - startSyncFolderOperation(root); - } - } + /** - * Pops a directory name from the drop down list - * @return True, unless the stack is empty + * Shows the information of the {@link OCFile} received as a + * parameter in the second fragment. + * + * @param file {@link OCFile} whose details will be shown */ - public boolean popDirname() { - mDirectories.remove(mDirectories.getItem(0)); - return !mDirectories.isEmpty(); - } - - private void setNavigationListWithFolder(OCFile file) { - mDirectories.clear(); - OCFile fileIt = file; - String parentPath; - while(fileIt != null && fileIt.getFileName() != OCFile.ROOT_PATH) { - if (fileIt.isFolder()) { - mDirectories.add(fileIt.getFileName()); - } - // get parent from path - parentPath = fileIt.getRemotePath().substring(0, fileIt.getRemotePath().lastIndexOf(fileIt.getFileName())); - fileIt = getStorageManager().getFileByPath(parentPath); - } - mDirectories.add(OCFile.PATH_SEPARATOR); - } - @Override - public boolean onNavigationItemSelected(int itemPosition, long itemId) { - if (itemPosition != 0) { - String targetPath = ""; - for (int i=itemPosition; i < mDirectories.getCount() - 1; i++) { - targetPath = mDirectories.getItem(i) + OCFile.PATH_SEPARATOR + targetPath; - } - targetPath = OCFile.PATH_SEPARATOR + targetPath; - OCFile targetFolder = getStorageManager().getFileByPath(targetPath); - if (targetFolder != null) { - browseTo(targetFolder); - } - - // the next operation triggers a new call to this method, but it's necessary to - // ensure that the name exposed in the action bar is the current directory when the - // user selected it in the navigation list - if (getSupportActionBar().getNavigationMode() == ActionBar.NAVIGATION_MODE_LIST && itemPosition != 0) - getSupportActionBar().setSelectedNavigationItem(0); - } - return true; - } - - public void browseTo(OCFile folder) { - if (folder == null || !folder.isFolder()) { - throw new IllegalArgumentException("Trying to browse to invalid folder " + folder); - } - MoveFileListFragment listOfFiles = getListOfFilesFragment(); - if (listOfFiles != null) { - setNavigationListWithFolder(folder); - listOfFiles.listDirectory(folder); - setFile(listOfFiles.getCurrentFile()); - startSyncFolderOperation(folder); - } else { - Log_OC.e(TAG, "Unexpected null when accessing list fragment"); - } - } - - @Override - public void onBackPressed() { - MoveFileListFragment listOfFiles = getListOfFilesFragment(); - if (listOfFiles != null) { // should never be null, indeed - if (mDirectories.getCount() <= 1) { - finish(); - return; - } - int levelsUp = listOfFiles.onBrowseUp(); - for (int i=0; i < levelsUp && mDirectories.getCount() > 1 ; i++) { - popDirname(); - } - } - if (listOfFiles != null) { // should never be null, indeed - setFile(listOfFiles.getCurrentFile()); - } - } - - private void updateNavigationElementsInActionBar(OCFile chosenFile) { - ActionBar actionBar = getSupportActionBar(); - if (chosenFile == null) { - // only list of files - set for browsing through folders - OCFile currentDir = getCurrentDir(); - boolean noRoot = (currentDir != null && currentDir.getParentId() != 0); - actionBar.setDisplayHomeAsUpEnabled(noRoot); - actionBar.setDisplayShowTitleEnabled(!noRoot); - if (!noRoot) { - actionBar.setTitle(getString(R.string.default_display_name_for_root_folder)); - } - actionBar.setNavigationMode(!noRoot ? ActionBar.NAVIGATION_MODE_STANDARD : ActionBar.NAVIGATION_MODE_LIST); - actionBar.setListNavigationCallbacks(mDirectories, this); // assuming mDirectories is updated + public void showDetails(OCFile file) { - } else { - actionBar.setDisplayHomeAsUpEnabled(true); - actionBar.setDisplayShowTitleEnabled(true); - actionBar.setTitle(chosenFile.getFileName()); - actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); - } } /** - * Called when the ownCloud {@link Account} associated to the Activity was just updated. + * {@inheritDoc} */ @Override - protected void onAccountSet(boolean stateWasRecovered) { - super.onAccountSet(stateWasRecovered); - if (getAccount() != null) { - /// Check whether the 'main' OCFile handled by the Activity is contained in the current Account - OCFile file = getFile(); - // get parent from path - String parentPath = ""; - if (file != null) { - if (file.isDown() && file.getLastSyncDateForProperties() == 0) { - // upload in progress - right now, files are not inserted in the local cache until the upload is successful - // get parent from path - parentPath = file.getRemotePath().substring(0, file.getRemotePath().lastIndexOf(file.getFileName())); - if (getStorageManager().getFileByPath(parentPath) == null) - file = null; // not able to know the directory where the file is uploading - } else { - file = getStorageManager().getFileByPath(file.getRemotePath()); // currentDir = null if not in the current Account - } - } - if (file == null) { - // fall back to root folder - file = getStorageManager().getFileByPath(OCFile.ROOT_PATH); // never returns null - } - setFile(file); - setNavigationListWithFolder(file); - - if (!stateWasRecovered) { - Log_OC.e(TAG, "Initializing Fragments in onAccountChanged.."); - if (file.isFolder()) { - startSyncFolderOperation(file); - } - } else { - updateNavigationElementsInActionBar(file.isFolder() ? null : file); - } - } + public void onTransferStateChanged(OCFile file, boolean downloading, boolean uploading) { + } - /** - * Set controllers - */ - private void initControls(){ - mCancelBtn = (Button) findViewById(R.id.move_files_btn_cancel); - mCancelBtn.setOnClickListener(this); - mChooseBtn = (Button) findViewById(R.id.move_files_btn_choose); - mChooseBtn.setOnClickListener(this); - } @Override - public void onClick(View v) { - if (v == mCancelBtn) { - finish(); + public void onRefresh() { + OCFileListFragment listOfFiles = getListOfFilesFragment(); + if (listOfFiles != null) { + OCFile folder = listOfFiles.getCurrentFile(); + if (folder != null) { + startSyncFolderOperation(folder, true); + } } } + }