FolderPickerActivity.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * Copyright (C) 2016 ownCloud Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2,
  8. * as published by the Free Software Foundation.
  9. *
  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.
  14. *
  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/>.
  17. *
  18. */
  19. package com.owncloud.android.ui.activity;
  20. import android.accounts.Account;
  21. import android.accounts.AuthenticatorException;
  22. import android.app.Activity;
  23. import android.content.BroadcastReceiver;
  24. import android.content.Context;
  25. import android.content.Intent;
  26. import android.content.IntentFilter;
  27. import android.content.res.Resources.NotFoundException;
  28. import android.graphics.PorterDuff;
  29. import android.os.Bundle;
  30. import android.os.Parcelable;
  31. import android.support.v4.app.Fragment;
  32. import android.support.v4.app.FragmentTransaction;
  33. import android.support.v7.app.ActionBar;
  34. import android.util.Log;
  35. import android.view.Menu;
  36. import android.view.MenuInflater;
  37. import android.view.MenuItem;
  38. import android.view.View;
  39. import android.view.View.OnClickListener;
  40. import android.widget.Button;
  41. import com.owncloud.android.R;
  42. import com.owncloud.android.datamodel.OCFile;
  43. import com.owncloud.android.lib.common.operations.RemoteOperation;
  44. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  45. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  46. import com.owncloud.android.lib.common.utils.Log_OC;
  47. import com.owncloud.android.operations.CreateFolderOperation;
  48. import com.owncloud.android.operations.RefreshFolderOperation;
  49. import com.owncloud.android.syncadapter.FileSyncAdapter;
  50. import com.owncloud.android.ui.dialog.CreateFolderDialogFragment;
  51. import com.owncloud.android.ui.fragment.FileFragment;
  52. import com.owncloud.android.ui.fragment.OCFileListFragment;
  53. import com.owncloud.android.utils.DataHolderUtil;
  54. import com.owncloud.android.utils.DisplayUtils;
  55. import com.owncloud.android.utils.ErrorMessageAdapter;
  56. import com.owncloud.android.utils.ThemeUtils;
  57. import java.util.ArrayList;
  58. public class FolderPickerActivity extends FileActivity implements FileFragment.ContainerActivity,
  59. OnClickListener, OnEnforceableRefreshListener {
  60. public static final String EXTRA_FOLDER = FolderPickerActivity.class.getCanonicalName() + ".EXTRA_FOLDER";
  61. public static final String EXTRA_FILES = FolderPickerActivity.class.getCanonicalName() + ".EXTRA_FILES";
  62. public static final String EXTRA_ACTION = FolderPickerActivity.class.getCanonicalName() + ".EXTRA_ACTION";
  63. public static final String EXTRA_CURRENT_FOLDER = FolderPickerActivity.class.getCanonicalName() +
  64. ".EXTRA_CURRENT_FOLDER";
  65. public static final String MOVE = "MOVE";
  66. public static final String COPY = "COPY";
  67. private SyncBroadcastReceiver mSyncBroadcastReceiver;
  68. private static final String TAG = FolderPickerActivity.class.getSimpleName();
  69. private static final String SCREEN_NAME = "Choose upload folder";
  70. private static final String TAG_LIST_OF_FOLDERS = "LIST_OF_FOLDERS";
  71. private boolean mSyncInProgress = false;
  72. private boolean mSearchOnlyFolders = false;
  73. private boolean mDoNotEnterEncryptedFolder = false;
  74. protected Button mCancelBtn;
  75. protected Button mChooseBtn;
  76. private String caption;
  77. @Override
  78. protected void onCreate(Bundle savedInstanceState) {
  79. Log_OC.d(TAG, "onCreate() start");
  80. super.onCreate(savedInstanceState);
  81. setContentView(R.layout.files_folder_picker);
  82. // sets callback listeners for UI elements
  83. initControls();
  84. // Action bar setup
  85. setupToolbar();
  86. if (getIntent().getStringExtra(EXTRA_ACTION) != null) {
  87. switch (getIntent().getStringExtra(EXTRA_ACTION)) {
  88. case MOVE:
  89. caption = getResources().getText(R.string.move_to).toString();
  90. mSearchOnlyFolders = true;
  91. mDoNotEnterEncryptedFolder = true;
  92. break;
  93. case COPY:
  94. caption = getResources().getText(R.string.copy_to).toString();
  95. mSearchOnlyFolders = true;
  96. mDoNotEnterEncryptedFolder = true;
  97. break;
  98. default:
  99. caption = ThemeUtils.getDefaultDisplayNameForRootFolder(this);
  100. break;
  101. }
  102. } else {
  103. caption = ThemeUtils.getDefaultDisplayNameForRootFolder(this);
  104. }
  105. if (getIntent().getParcelableExtra(EXTRA_CURRENT_FOLDER) != null) {
  106. setFile(getIntent().getParcelableExtra(EXTRA_CURRENT_FOLDER));
  107. }
  108. if (savedInstanceState == null) {
  109. createFragments();
  110. }
  111. if (getSupportActionBar() != null) {
  112. getSupportActionBar().setDisplayShowTitleEnabled(true);
  113. getSupportActionBar().setTitle(caption);
  114. }
  115. setIndeterminate(mSyncInProgress);
  116. // always AFTER setContentView(...) ; to work around bug in its implementation
  117. // sets message for empty list of folders
  118. setBackgroundText();
  119. Log_OC.d(TAG, "onCreate() end");
  120. }
  121. @Override
  122. protected void onStart() {
  123. super.onStart();
  124. }
  125. /**
  126. * Called when the ownCloud {@link Account} associated to the Activity was just updated.
  127. */
  128. @Override
  129. protected void onAccountSet(boolean stateWasRecovered) {
  130. super.onAccountSet(stateWasRecovered);
  131. if (getAccount() != null) {
  132. updateFileFromDB();
  133. OCFile folder = getFile();
  134. if (folder == null || !folder.isFolder()) {
  135. // fall back to root folder
  136. setFile(getStorageManager().getFileByPath(OCFile.ROOT_PATH));
  137. folder = getFile();
  138. }
  139. if (!stateWasRecovered) {
  140. OCFileListFragment listOfFolders = getListOfFilesFragment();
  141. listOfFolders.listDirectory(folder, false, false);
  142. startSyncFolderOperation(folder, false);
  143. }
  144. updateNavigationElementsInActionBar();
  145. }
  146. }
  147. private Activity getActivity() {
  148. return this;
  149. }
  150. private void createFragments() {
  151. OCFileListFragment listOfFiles = new OCFileListFragment();
  152. Bundle args = new Bundle();
  153. args.putBoolean(OCFileListFragment.ARG_ONLY_FOLDERS_CLICKABLE, true);
  154. args.putBoolean(OCFileListFragment.ARG_HIDE_FAB, true);
  155. args.putBoolean(OCFileListFragment.ARG_HIDE_ITEM_OPTIONS, true);
  156. args.putBoolean(OCFileListFragment.ARG_SEARCH_ONLY_FOLDER, mSearchOnlyFolders);
  157. listOfFiles.setArguments(args);
  158. FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  159. transaction.add(R.id.fragment_container, listOfFiles, TAG_LIST_OF_FOLDERS);
  160. transaction.commit();
  161. }
  162. /**
  163. * Show a text message on screen view for notifying user if content is
  164. * loading or folder is empty
  165. */
  166. private void setBackgroundText() {
  167. OCFileListFragment listFragment = getListOfFilesFragment();
  168. if (listFragment != null) {
  169. if (!mSyncInProgress) {
  170. listFragment.setMessageForEmptyList(
  171. R.string.file_list_empty_headline,
  172. R.string.file_list_empty_moving,
  173. R.drawable.ic_list_empty_create_folder,
  174. true
  175. );
  176. } else {
  177. listFragment.setEmptyListLoadingMessage();
  178. }
  179. } else {
  180. Log.e(TAG, "OCFileListFragment is null");
  181. }
  182. }
  183. protected OCFileListFragment getListOfFilesFragment() {
  184. Fragment listOfFiles = getSupportFragmentManager().findFragmentByTag(FolderPickerActivity.TAG_LIST_OF_FOLDERS);
  185. if (listOfFiles != null) {
  186. return (OCFileListFragment)listOfFiles;
  187. }
  188. Log_OC.e(TAG, "Access to non existing list of files fragment!!");
  189. return null;
  190. }
  191. /**
  192. * {@inheritDoc}
  193. *
  194. * Updates action bar and second fragment, if in dual pane mode.
  195. */
  196. @Override
  197. public void onBrowsedDownTo(OCFile directory) {
  198. setFile(directory);
  199. updateNavigationElementsInActionBar();
  200. // Sync Folder
  201. startSyncFolderOperation(directory, false);
  202. }
  203. @Override
  204. public void onSavedCertificate() {
  205. startSyncFolderOperation(getCurrentDir(), false);
  206. }
  207. public void startSyncFolderOperation(OCFile folder, boolean ignoreETag) {
  208. long currentSyncTime = System.currentTimeMillis();
  209. mSyncInProgress = true;
  210. // perform folder synchronization
  211. RemoteOperation refreshFolderOperation = new RefreshFolderOperation(folder, currentSyncTime, false,
  212. getFileOperationsHelper().isSharedSupported(), ignoreETag, getStorageManager(), getAccount(),
  213. getApplicationContext());
  214. refreshFolderOperation.execute(getAccount(), this, null, null);
  215. setIndeterminate(true);
  216. setBackgroundText();
  217. }
  218. @Override
  219. protected void onResume() {
  220. super.onResume();
  221. Log_OC.e(TAG, "onResume() start");
  222. // refresh list of files
  223. refreshListOfFilesFragment(false);
  224. // Listen for sync messages
  225. IntentFilter syncIntentFilter = new IntentFilter(FileSyncAdapter.EVENT_FULL_SYNC_START);
  226. syncIntentFilter.addAction(FileSyncAdapter.EVENT_FULL_SYNC_END);
  227. syncIntentFilter.addAction(FileSyncAdapter.EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED);
  228. syncIntentFilter.addAction(RefreshFolderOperation.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED);
  229. syncIntentFilter.addAction(RefreshFolderOperation.EVENT_SINGLE_FOLDER_SHARES_SYNCED);
  230. mSyncBroadcastReceiver = new SyncBroadcastReceiver();
  231. registerReceiver(mSyncBroadcastReceiver, syncIntentFilter);
  232. Log_OC.d(TAG, "onResume() end");
  233. }
  234. @Override
  235. protected void onPause() {
  236. Log_OC.e(TAG, "onPause() start");
  237. if (mSyncBroadcastReceiver != null) {
  238. unregisterReceiver(mSyncBroadcastReceiver);
  239. //LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncBroadcastReceiver);
  240. mSyncBroadcastReceiver = null;
  241. }
  242. Log_OC.d(TAG, "onPause() end");
  243. super.onPause();
  244. }
  245. @Override
  246. public boolean onCreateOptionsMenu(Menu menu) {
  247. MenuInflater inflater = getMenuInflater();
  248. inflater.inflate(R.menu.main_menu, menu);
  249. menu.findItem(R.id.action_switch_view).setVisible(false);
  250. menu.findItem(R.id.action_sync_account).setVisible(false);
  251. menu.findItem(R.id.action_select_all).setVisible(false);
  252. menu.findItem(R.id.action_sort).setVisible(false);
  253. return true;
  254. }
  255. @Override
  256. public boolean onOptionsItemSelected(MenuItem item) {
  257. boolean retval = true;
  258. switch (item.getItemId()) {
  259. case R.id.action_create_dir: {
  260. CreateFolderDialogFragment dialog =
  261. CreateFolderDialogFragment.newInstance(getCurrentFolder());
  262. dialog.show(
  263. getSupportFragmentManager(),
  264. CreateFolderDialogFragment.CREATE_FOLDER_FRAGMENT
  265. );
  266. break;
  267. }
  268. case android.R.id.home: {
  269. OCFile currentDir = getCurrentFolder();
  270. if(currentDir != null && currentDir.getParentId() != 0) {
  271. onBackPressed();
  272. }
  273. break;
  274. }
  275. default:
  276. retval = super.onOptionsItemSelected(item);
  277. break;
  278. }
  279. return retval;
  280. }
  281. protected OCFile getCurrentFolder() {
  282. OCFile file = getFile();
  283. if (file != null) {
  284. if (file.isFolder()) {
  285. return file;
  286. } else if (getStorageManager() != null) {
  287. String parentPath = file.getRemotePath().substring(0, file.getRemotePath().lastIndexOf(file.getFileName()));
  288. return getStorageManager().getFileByPath(parentPath);
  289. }
  290. }
  291. return null;
  292. }
  293. public void refreshListOfFilesFragment(boolean fromSearch) {
  294. OCFileListFragment fileListFragment = getListOfFilesFragment();
  295. if (fileListFragment != null) {
  296. fileListFragment.listDirectory(false, fromSearch);
  297. }
  298. }
  299. public void browseToRoot() {
  300. OCFileListFragment listOfFiles = getListOfFilesFragment();
  301. if (listOfFiles != null) { // should never be null, indeed
  302. OCFile root = getStorageManager().getFileByPath(OCFile.ROOT_PATH);
  303. listOfFiles.listDirectory(root, false, false);
  304. setFile(listOfFiles.getCurrentFile());
  305. updateNavigationElementsInActionBar();
  306. startSyncFolderOperation(root, false);
  307. }
  308. }
  309. @Override
  310. public void onBackPressed() {
  311. OCFileListFragment listOfFiles = getListOfFilesFragment();
  312. if (listOfFiles != null) { // should never be null, indeed
  313. int levelsUp = listOfFiles.onBrowseUp();
  314. if (levelsUp == 0) {
  315. finish();
  316. return;
  317. }
  318. setFile(listOfFiles.getCurrentFile());
  319. updateNavigationElementsInActionBar();
  320. }
  321. }
  322. protected void updateNavigationElementsInActionBar() {
  323. OCFile currentDir = getCurrentFolder();
  324. ActionBar actionBar = getSupportActionBar();
  325. if (actionBar != null) {
  326. boolean atRoot = (currentDir == null || currentDir.getParentId() == 0);
  327. actionBar.setDisplayHomeAsUpEnabled(!atRoot);
  328. actionBar.setHomeButtonEnabled(!atRoot);
  329. actionBar.setTitle(atRoot ? caption : currentDir.getFileName());
  330. }
  331. }
  332. /**
  333. * Set per-view controllers
  334. */
  335. private void initControls(){
  336. mCancelBtn = findViewById(R.id.folder_picker_btn_cancel);
  337. mCancelBtn.setOnClickListener(this);
  338. mChooseBtn = findViewById(R.id.folder_picker_btn_choose);
  339. mChooseBtn.getBackground().setColorFilter(ThemeUtils.primaryColor(this), PorterDuff.Mode.SRC_ATOP);
  340. mChooseBtn.setOnClickListener(this);
  341. }
  342. @Override
  343. public void onClick(View v) {
  344. if (v.equals(mCancelBtn)) {
  345. finish();
  346. } else if (v.equals(mChooseBtn)) {
  347. Intent i = getIntent();
  348. ArrayList<Parcelable> targetFiles = i.getParcelableArrayListExtra(FolderPickerActivity.EXTRA_FILES);
  349. Intent data = new Intent();
  350. data.putExtra(EXTRA_FOLDER, getCurrentFolder());
  351. data.putParcelableArrayListExtra(EXTRA_FILES, targetFiles);
  352. setResult(RESULT_OK, data);
  353. finish();
  354. }
  355. }
  356. @Override
  357. public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
  358. super.onRemoteOperationFinish(operation, result);
  359. if (operation instanceof CreateFolderOperation) {
  360. onCreateFolderOperationFinish((CreateFolderOperation) operation, result);
  361. }
  362. }
  363. /**
  364. * Updates the view associated to the activity after the finish of an operation trying
  365. * to create a new folder.
  366. *
  367. * @param operation Creation operation performed.
  368. * @param result Result of the creation.
  369. */
  370. private void onCreateFolderOperationFinish(
  371. CreateFolderOperation operation, RemoteOperationResult result
  372. ) {
  373. if (result.isSuccess()) {
  374. refreshListOfFilesFragment(false);
  375. } else {
  376. try {
  377. DisplayUtils.showSnackMessage(
  378. this,ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources())
  379. );
  380. } catch (NotFoundException e) {
  381. Log_OC.e(TAG, "Error while trying to show fail message " , e);
  382. }
  383. }
  384. }
  385. private class SyncBroadcastReceiver extends BroadcastReceiver {
  386. /**
  387. * {@link BroadcastReceiver} to enable syncing feedback in UI
  388. */
  389. @Override
  390. public void onReceive(Context context, Intent intent) {
  391. try {
  392. String event = intent.getAction();
  393. Log_OC.d(TAG, "Received broadcast " + event);
  394. String accountName = intent.getStringExtra(FileSyncAdapter.EXTRA_ACCOUNT_NAME);
  395. String syncFolderRemotePath = intent.getStringExtra(FileSyncAdapter.EXTRA_FOLDER_PATH);
  396. RemoteOperationResult syncResult = (RemoteOperationResult)
  397. DataHolderUtil.getInstance().retrieve(intent.getStringExtra(FileSyncAdapter.EXTRA_RESULT));
  398. boolean sameAccount = (getAccount() != null && accountName.equals(getAccount().name)
  399. && getStorageManager() != null);
  400. if (sameAccount) {
  401. if (FileSyncAdapter.EVENT_FULL_SYNC_START.equals(event)) {
  402. mSyncInProgress = true;
  403. } else {
  404. OCFile currentFile = (getFile() == null) ? null :
  405. getStorageManager().getFileByPath(getFile().getRemotePath());
  406. OCFile currentDir = (getCurrentFolder() == null) ? null :
  407. getStorageManager().getFileByPath(getCurrentFolder().getRemotePath());
  408. if (currentDir == null) {
  409. // current folder was removed from the server
  410. DisplayUtils.showSnackMessage(getActivity(), R.string.sync_current_folder_was_removed,
  411. getCurrentFolder().getFileName());
  412. browseToRoot();
  413. } else {
  414. if (currentFile == null && !getFile().isFolder()) {
  415. // currently selected file was removed in the server, and now we know it
  416. currentFile = currentDir;
  417. }
  418. if (currentDir.getRemotePath().equals(syncFolderRemotePath)) {
  419. OCFileListFragment fileListFragment = getListOfFilesFragment();
  420. if (fileListFragment != null) {
  421. fileListFragment.listDirectory(currentDir, false, false);
  422. }
  423. }
  424. setFile(currentFile);
  425. }
  426. mSyncInProgress = (!FileSyncAdapter.EVENT_FULL_SYNC_END.equals(event) &&
  427. !RefreshFolderOperation.EVENT_SINGLE_FOLDER_SHARES_SYNCED.equals(event));
  428. if (RefreshFolderOperation.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED.equals(event) &&
  429. /// TODO refactor and make common
  430. syncResult != null && !syncResult.isSuccess()) {
  431. if (ResultCode.UNAUTHORIZED.equals(syncResult.getCode()) || (syncResult.isException()
  432. && syncResult.getException() instanceof AuthenticatorException)) {
  433. requestCredentialsUpdate(context);
  434. } else if (RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED
  435. .equals(syncResult.getCode())) {
  436. showUntrustedCertDialog(syncResult);
  437. }
  438. }
  439. }
  440. removeStickyBroadcast(intent);
  441. DataHolderUtil.getInstance().delete(intent.getStringExtra(FileSyncAdapter.EXTRA_RESULT));
  442. Log_OC.d(TAG, "Setting progress visibility to " + mSyncInProgress);
  443. setIndeterminate(mSyncInProgress);
  444. setBackgroundText();
  445. }
  446. } catch (RuntimeException e) {
  447. // avoid app crashes after changing the serial id of RemoteOperationResult
  448. // in owncloud library with broadcast notifications pending to process
  449. removeStickyBroadcast(intent);
  450. DataHolderUtil.getInstance().delete(intent.getStringExtra(FileSyncAdapter.EXTRA_RESULT));
  451. }
  452. }
  453. }
  454. /**
  455. * Shows the information of the {@link OCFile} received as a
  456. * parameter in the second fragment.
  457. *
  458. * @param file {@link OCFile} whose details will be shown
  459. */
  460. @Override
  461. public void showDetails(OCFile file) {
  462. // not used at the moment
  463. }
  464. /**
  465. * {@inheritDoc}
  466. */
  467. @Override
  468. public void onTransferStateChanged(OCFile file, boolean downloading, boolean uploading) {
  469. // not used at the moment
  470. }
  471. @Override
  472. public void onRefresh() {
  473. refreshList(true);
  474. }
  475. @Override
  476. public void onRefresh(boolean enforced) {
  477. refreshList(enforced);
  478. }
  479. private void refreshList(boolean ignoreETag) {
  480. OCFileListFragment listOfFiles = getListOfFilesFragment();
  481. if (listOfFiles != null) {
  482. OCFile folder = listOfFiles.getCurrentFile();
  483. if (folder != null) {
  484. startSyncFolderOperation(folder, ignoreETag);
  485. }
  486. }
  487. }
  488. public boolean isDoNotEnterEncryptedFolder() {
  489. return mDoNotEnterEncryptedFolder;
  490. }
  491. }