FolderPickerActivity.java 21 KB

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