FolderPickerActivity.java 23 KB

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