123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- /**
- * ownCloud Android client application
- *
- * Copyright (C) 2016 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.activity;
- import android.accounts.Account;
- import android.accounts.AuthenticatorException;
- 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.os.Parcelable;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentTransaction;
- import android.support.v7.app.ActionBar;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
- import com.owncloud.android.R;
- import com.owncloud.android.datamodel.OCFile;
- 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.lib.common.utils.Log_OC;
- import com.owncloud.android.operations.CreateFolderOperation;
- import com.owncloud.android.operations.RefreshFolderOperation;
- 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.OCFileListFragment;
- import com.owncloud.android.utils.ErrorMessageAdapter;
- import java.util.ArrayList;
- public class FolderPickerActivity extends FileActivity implements FileFragment.ContainerActivity,
- OnClickListener, OnEnforceableRefreshListener {
- public static final String EXTRA_FOLDER = FolderPickerActivity.class.getCanonicalName()
- + ".EXTRA_FOLDER";
- public static final String EXTRA_FILES = FolderPickerActivity.class.getCanonicalName()
- + ".EXTRA_FILES";
- public static final String EXTRA_ACTION = FolderPickerActivity.class.getCanonicalName()
- + ".EXTRA_ACTION";
- private SyncBroadcastReceiver mSyncBroadcastReceiver;
- private static final String TAG = FolderPickerActivity.class.getSimpleName();
-
- private static final String TAG_LIST_OF_FOLDERS = "LIST_OF_FOLDERS";
-
- private boolean mSyncInProgress = false;
- protected Button mCancelBtn;
- protected Button mChooseBtn;
- private String caption;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- Log_OC.d(TAG, "onCreate() start");
- super.onCreate(savedInstanceState);
- setContentView(R.layout.files_folder_picker);
-
- if (savedInstanceState == null) {
- createFragments();
- }
- // sets callback listeners for UI elements
- initControls();
- // Action bar setup
- setupToolbar();
- getSupportActionBar().setDisplayShowTitleEnabled(true);
- if (getIntent().getStringExtra(EXTRA_ACTION) != null) {
- caption = getIntent().getStringExtra(EXTRA_ACTION);
- } else {
- caption = getString(R.string.default_display_name_for_root_folder);
- };
- getSupportActionBar().setTitle(caption);
- setIndeterminate(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");
- }
- @Override
- protected void onStart() {
- super.onStart();
- }
- /**
- * 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, false);
-
- 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_HIDE_FAB, true);
- listOfFiles.setArguments(args);
- FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
- transaction.add(R.id.fragment_container, listOfFiles, TAG_LIST_OF_FOLDERS);
- transaction.commit();
- }
- /**
- * Show a text message on screen view for notifying user if content is
- * loading or folder is empty
- */
- private void setBackgroundText() {
- OCFileListFragment listFragment = getListOfFilesFragment();
- if (listFragment != null) {
- if (!mSyncInProgress) {
- listFragment.setMessageForEmptyList(
- R.string.file_list_empty_headline,
- R.string.file_list_empty_moving,
- R.drawable.ic_list_empty_create_folder
- );
- } else {
- listFragment.setEmptyListLoadingMessage();
- }
- } else {
- Log.e(TAG, "OCFileListFragment is null");
- }
- }
- protected OCFileListFragment getListOfFilesFragment() {
- Fragment listOfFiles = getSupportFragmentManager().findFragmentByTag(FolderPickerActivity.TAG_LIST_OF_FOLDERS);
- if (listOfFiles != null) {
- return (OCFileListFragment)listOfFiles;
- }
- Log_OC.e(TAG, "Access to unexisting list of files fragment!!");
- return null;
- }
-
- /**
- * {@inheritDoc}
- *
- * Updates action bar and second fragment, if in dual pane mode.
- */
- @Override
- public void onBrowsedDownTo(OCFile directory) {
- setFile(directory);
- updateNavigationElementsInActionBar();
- // Sync Folder
- startSyncFolderOperation(directory, false);
-
- }
- @Override
- public void onSavedCertificate() {
- startSyncFolderOperation(getCurrentDir(), false);
- }
-
- public void startSyncFolderOperation(OCFile folder, boolean ignoreETag) {
- long currentSyncTime = System.currentTimeMillis();
-
- mSyncInProgress = true;
-
- // perform folder synchronization
- RemoteOperation synchFolderOp = new RefreshFolderOperation( folder,
- currentSyncTime,
- false,
- getFileOperationsHelper().isSharedSupported(),
- ignoreETag,
- getStorageManager(),
- getAccount(),
- getApplicationContext()
- );
- synchFolderOp.execute(getAccount(), this, null, null);
- setIndeterminate(true);
- setBackgroundText();
- }
- @Override
- protected void onResume() {
- super.onResume();
- Log_OC.e(TAG, "onResume() start");
-
- // refresh list of files
- refreshListOfFilesFragment();
- // Listen for sync messages
- IntentFilter syncIntentFilter = new IntentFilter(FileSyncAdapter.EVENT_FULL_SYNC_START);
- syncIntentFilter.addAction(FileSyncAdapter.EVENT_FULL_SYNC_END);
- syncIntentFilter.addAction(FileSyncAdapter.EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED);
- syncIntentFilter.addAction(RefreshFolderOperation.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED);
- syncIntentFilter.addAction(RefreshFolderOperation.EVENT_SINGLE_FOLDER_SHARES_SYNCED);
- mSyncBroadcastReceiver = new SyncBroadcastReceiver();
- registerReceiver(mSyncBroadcastReceiver, syncIntentFilter);
-
- Log_OC.d(TAG, "onResume() end");
- }
-
- @Override
- 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 = getMenuInflater();
- inflater.inflate(R.menu.main_menu, menu);
- menu.findItem(R.id.action_sort).setVisible(false);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- boolean retval = true;
- switch (item.getItemId()) {
- case R.id.action_create_dir: {
- CreateFolderDialogFragment dialog =
- CreateFolderDialogFragment.newInstance(getCurrentFolder());
- dialog.show(
- getSupportFragmentManager(),
- CreateFolderDialogFragment.CREATE_FOLDER_FRAGMENT
- );
- break;
- }
- case android.R.id.home: {
- OCFile currentDir = getCurrentFolder();
- if(currentDir != null && currentDir.getParentId() != 0) {
- onBackPressed();
- }
- break;
- }
- default:
- retval = super.onOptionsItemSelected(item);
- }
- return retval;
- }
- protected OCFile getCurrentFolder() {
- OCFile file = getFile();
- if (file != null) {
- if (file.isFolder()) {
- return file;
- } else if (getStorageManager() != null) {
- String parentPath = file.getRemotePath().substring(0, file.getRemotePath().lastIndexOf(file.getFileName()));
- return getStorageManager().getFileByPath(parentPath);
- }
- }
- return null;
- }
-
- protected void refreshListOfFilesFragment() {
- OCFileListFragment fileListFragment = getListOfFilesFragment();
- if (fileListFragment != null) {
- fileListFragment.listDirectory(false);
- }
- }
- public void browseToRoot() {
- OCFileListFragment listOfFiles = getListOfFilesFragment();
- if (listOfFiles != null) { // should never be null, indeed
- OCFile root = getStorageManager().getFileByPath(OCFile.ROOT_PATH);
- listOfFiles.listDirectory(root, false);
- 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();
- }
- }
- protected void updateNavigationElementsInActionBar() {
- ActionBar actionBar = getSupportActionBar();
- OCFile currentDir = getCurrentFolder();
- boolean atRoot = (currentDir == null || currentDir.getParentId() == 0);
- actionBar.setDisplayHomeAsUpEnabled(!atRoot);
- actionBar.setHomeButtonEnabled(!atRoot);
- actionBar.setTitle(
- atRoot
- ? caption
- : currentDir.getFileName()
- );
- }
- /**
- * Set per-view controllers
- */
- private void initControls(){
- mCancelBtn = (Button) findViewById(R.id.folder_picker_btn_cancel);
- mCancelBtn.setOnClickListener(this);
- mChooseBtn = (Button) findViewById(R.id.folder_picker_btn_choose);
- mChooseBtn.setOnClickListener(this);
- }
-
- @Override
- public void onClick(View v) {
- if (v.equals(mCancelBtn)) {
- finish();
- } else if (v.equals(mChooseBtn)) {
- Intent i = getIntent();
- ArrayList<Parcelable> targetFiles = i.getParcelableArrayListExtra(FolderPickerActivity.EXTRA_FILES);
- Intent data = new Intent();
- data.putExtra(EXTRA_FOLDER, getCurrentFolder());
- data.putParcelableArrayListExtra(EXTRA_FILES, targetFiles);
- setResult(RESULT_OK, 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()) {
- refreshListOfFilesFragment();
- } else {
- try {
- Toast msg = Toast.makeText(FolderPickerActivity.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 {
- /**
- * {@link BroadcastReceiver} to enable syncing feedback in UI
- */
- @Override
- public void onReceive(Context context, Intent intent) {
- try {
- String event = intent.getAction();
- Log_OC.d(TAG, "Received broadcast " + event);
- String accountName = intent.getStringExtra(FileSyncAdapter.EXTRA_ACCOUNT_NAME);
- String synchFolderRemotePath = intent.getStringExtra(FileSyncAdapter.EXTRA_FOLDER_PATH);
- RemoteOperationResult synchResult = (RemoteOperationResult)intent.
- getSerializableExtra(FileSyncAdapter.EXTRA_RESULT);
- boolean sameAccount = (getAccount() != null &&
- accountName.equals(getAccount().name) && getStorageManager() != null);
-
- if (sameAccount) {
-
- if (FileSyncAdapter.EVENT_FULL_SYNC_START.equals(event)) {
- mSyncInProgress = true;
-
- } else {
- OCFile currentFile = (getFile() == null) ? null :
- getStorageManager().getFileByPath(getFile().getRemotePath());
- OCFile currentDir = (getCurrentFolder() == null) ? null :
- getStorageManager().getFileByPath(getCurrentFolder().getRemotePath());
-
- if (currentDir == null) {
- // current folder was removed from the server
- Toast.makeText( FolderPickerActivity.this,
- String.format(
- getString(R.string.sync_current_folder_was_removed),
- getCurrentFolder().getFileName()),
- Toast.LENGTH_LONG)
- .show();
- browseToRoot();
-
- } else {
- if (currentFile == null && !getFile().isFolder()) {
- // currently selected file was removed in the server, and now we know it
- currentFile = currentDir;
- }
- if (currentDir.getRemotePath().equals(synchFolderRemotePath)) {
- OCFileListFragment fileListFragment = getListOfFilesFragment();
- if (fileListFragment != null) {
- fileListFragment.listDirectory(currentDir, false);
- }
- }
- setFile(currentFile);
- }
-
- mSyncInProgress = (!FileSyncAdapter.EVENT_FULL_SYNC_END.equals(event) &&
- !RefreshFolderOperation.EVENT_SINGLE_FOLDER_SHARES_SYNCED.equals(event));
-
- if (RefreshFolderOperation.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED.
- equals(event) &&
- /// TODO refactor and make common
- synchResult != null && !synchResult.isSuccess()) {
- if(ResultCode.UNAUTHORIZED.equals(synchResult.getCode()) ||
- (synchResult.isException() && synchResult.getException()
- instanceof AuthenticatorException)) {
- requestCredentialsUpdate(context);
- } else if(RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED.equals(synchResult.getCode())) {
- showUntrustedCertDialog(synchResult);
- }
- }
- }
- removeStickyBroadcast(intent);
- Log_OC.d(TAG, "Setting progress visibility to " + mSyncInProgress);
- setIndeterminate(mSyncInProgress);
- setBackgroundText();
- }
-
- } catch (RuntimeException e) {
- // avoid app crashes after changing the serial id of RemoteOperationResult
- // in owncloud library with broadcast notifications pending to process
- removeStickyBroadcast(intent);
- }
- }
- }
-
- /**
- * 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) {
-
- }
- @Override
- public void onRefresh() {
- refreshList(true);
- }
- @Override
- public void onRefresh(boolean enforced) {
- refreshList(enforced);
- }
- private void refreshList(boolean ignoreETag) {
- OCFileListFragment listOfFiles = getListOfFilesFragment();
- if (listOfFiles != null) {
- OCFile folder = listOfFiles.getCurrentFile();
- if (folder != null) {
- startSyncFolderOperation(folder, ignoreETag);
- }
- }
- }
- }
|