UploadFilesActivity.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2015 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.ui.activity;
  21. import android.accounts.Account;
  22. import android.app.Activity;
  23. import android.content.Intent;
  24. import android.os.AsyncTask;
  25. import android.os.Bundle;
  26. import android.os.Environment;
  27. import android.support.v4.app.DialogFragment;
  28. import android.support.v4.app.Fragment;
  29. import android.support.v4.app.FragmentManager;
  30. import android.support.v4.app.FragmentTransaction;
  31. import android.support.v7.app.ActionBar;
  32. import android.view.Menu;
  33. import android.view.MenuItem;
  34. import android.view.View;
  35. import android.view.View.OnClickListener;
  36. import android.view.ViewGroup;
  37. import android.widget.ArrayAdapter;
  38. import android.widget.Button;
  39. import android.widget.Spinner;
  40. import android.widget.TextView;
  41. import com.owncloud.android.MainApp;
  42. import com.owncloud.android.R;
  43. import com.owncloud.android.db.PreferenceManager;
  44. import com.owncloud.android.lib.common.utils.Log_OC;
  45. import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
  46. import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
  47. import com.owncloud.android.ui.dialog.IndeterminateProgressDialog;
  48. import com.owncloud.android.ui.dialog.SortingOrderDialogFragment;
  49. import com.owncloud.android.ui.fragment.ExtendedListFragment;
  50. import com.owncloud.android.ui.fragment.LocalFileListFragment;
  51. import com.owncloud.android.utils.FileStorageUtils;
  52. import java.io.File;
  53. import static com.owncloud.android.db.PreferenceManager.getSortAscending;
  54. import static com.owncloud.android.db.PreferenceManager.getSortOrder;
  55. /**
  56. * Displays local files and let the user choose what of them wants to upload
  57. * to the current ownCloud account.
  58. */
  59. public class UploadFilesActivity extends FileActivity implements
  60. LocalFileListFragment.ContainerActivity, ActionBar.OnNavigationListener,
  61. OnClickListener, ConfirmationDialogFragmentListener, SortingOrderDialogFragment.OnSortingOrderListener {
  62. private static final String SORT_ORDER_DIALOG_TAG = "SORT_ORDER_DIALOG";
  63. private ArrayAdapter<String> mDirectories;
  64. private File mCurrentDir = null;
  65. private boolean mSelectAll = false;
  66. private LocalFileListFragment mFileListFragment;
  67. private Button mCancelBtn;
  68. protected Button mUploadBtn;
  69. private Spinner mBehaviourSpinner;
  70. private Account mAccountOnCreation;
  71. private DialogFragment mCurrentDialog;
  72. private Menu mOptionsMenu;
  73. private static final String SCREEN_NAME = "Choose local files to upload";
  74. public static final String EXTRA_CHOSEN_FILES =
  75. UploadFilesActivity.class.getCanonicalName() + ".EXTRA_CHOSEN_FILES";
  76. public static final int RESULT_OK_AND_MOVE = RESULT_FIRST_USER;
  77. public static final int RESULT_OK_AND_DO_NOTHING = 2;
  78. public static final int RESULT_OK_AND_DELETE = 3;
  79. public static final String KEY_DIRECTORY_PATH =
  80. UploadFilesActivity.class.getCanonicalName() + ".KEY_DIRECTORY_PATH";
  81. private static final String KEY_ALL_SELECTED =
  82. UploadFilesActivity.class.getCanonicalName() + ".KEY_ALL_SELECTED";
  83. private static final String TAG = "UploadFilesActivity";
  84. private static final String WAIT_DIALOG_TAG = "WAIT";
  85. private static final String QUERY_TO_MOVE_DIALOG_TAG = "QUERY_TO_MOVE";
  86. @Override
  87. public void onCreate(Bundle savedInstanceState) {
  88. Log_OC.d(TAG, "onCreate() start");
  89. super.onCreate(savedInstanceState);
  90. if(savedInstanceState != null) {
  91. mCurrentDir = new File(savedInstanceState.getString(UploadFilesActivity.KEY_DIRECTORY_PATH, Environment
  92. .getExternalStorageDirectory().getAbsolutePath()));
  93. mSelectAll = savedInstanceState.getBoolean(UploadFilesActivity.KEY_ALL_SELECTED, false);
  94. } else {
  95. mCurrentDir = Environment.getExternalStorageDirectory();
  96. }
  97. mAccountOnCreation = getAccount();
  98. /// USER INTERFACE
  99. // Drop-down navigation
  100. mDirectories = new CustomArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item);
  101. File currDir = mCurrentDir;
  102. while(currDir != null && currDir.getParentFile() != null) {
  103. mDirectories.add(currDir.getName());
  104. currDir = currDir.getParentFile();
  105. }
  106. mDirectories.add(File.separator);
  107. // Inflate and set the layout view
  108. setContentView(R.layout.upload_files_layout);
  109. mFileListFragment = (LocalFileListFragment) getSupportFragmentManager().findFragmentById(R.id.local_files_list);
  110. // Set input controllers
  111. mCancelBtn = (Button) findViewById(R.id.upload_files_btn_cancel);
  112. mCancelBtn.setOnClickListener(this);
  113. mUploadBtn = (Button) findViewById(R.id.upload_files_btn_upload);
  114. mUploadBtn.setOnClickListener(this);
  115. int localBehaviour = PreferenceManager.getUploaderBehaviour(this);
  116. // file upload spinner
  117. mBehaviourSpinner = (Spinner) findViewById(R.id.upload_files_spinner_behaviour);
  118. ArrayAdapter<CharSequence> behaviourAdapter = ArrayAdapter.createFromResource(this,
  119. R.array.upload_files_behaviour, android.R.layout.simple_spinner_item);
  120. behaviourAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  121. mBehaviourSpinner.setAdapter(behaviourAdapter);
  122. mBehaviourSpinner.setSelection(localBehaviour);
  123. // setup the toolbar
  124. setupToolbar();
  125. // Action bar setup
  126. ActionBar actionBar = getSupportActionBar();
  127. actionBar.setHomeButtonEnabled(true); // mandatory since Android ICS, according to the
  128. // official documentation
  129. actionBar.setDisplayHomeAsUpEnabled(mCurrentDir != null && mCurrentDir.getName() != null);
  130. actionBar.setDisplayShowTitleEnabled(false);
  131. actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
  132. actionBar.setListNavigationCallbacks(mDirectories, this);
  133. // wait dialog
  134. if (mCurrentDialog != null) {
  135. mCurrentDialog.dismiss();
  136. mCurrentDialog = null;
  137. }
  138. Log_OC.d(TAG, "onCreate() end");
  139. }
  140. /**
  141. * Helper to launch the UploadFilesActivity for which you would like a result when it finished.
  142. * Your onActivityResult() method will be called with the given requestCode.
  143. *
  144. * @param activity the activity which should call the upload activity for a result
  145. * @param account the account for which the upload activity is called
  146. * @param requestCode If >= 0, this code will be returned in onActivityResult()
  147. */
  148. public static void startUploadActivityForResult(Activity activity, Account account, int requestCode) {
  149. Intent action = new Intent(activity, UploadFilesActivity.class);
  150. action.putExtra(EXTRA_ACCOUNT, (account));
  151. activity.startActivityForResult(action, requestCode);
  152. }
  153. @Override
  154. public boolean onCreateOptionsMenu(Menu menu) {
  155. mOptionsMenu = menu;
  156. getMenuInflater().inflate(R.menu.upload_files_picker, menu);
  157. MenuItem selectAll = menu.findItem(R.id.action_select_all);
  158. setSelectAllMenuItem(selectAll, mSelectAll);
  159. MenuItem switchView = menu.findItem(R.id.action_switch_view);
  160. switchView.setTitle(isGridView() ? R.string.action_switch_list_view : R.string.action_switch_grid_view);
  161. return super.onCreateOptionsMenu(menu);
  162. }
  163. @Override
  164. public boolean onOptionsItemSelected(MenuItem item) {
  165. boolean retval = true;
  166. switch (item.getItemId()) {
  167. case android.R.id.home: {
  168. if(mCurrentDir != null && mCurrentDir.getParentFile() != null){
  169. onBackPressed();
  170. }
  171. break;
  172. }
  173. case R.id.action_select_all: {
  174. item.setChecked(!item.isChecked());
  175. mSelectAll = item.isChecked();
  176. setSelectAllMenuItem(item, mSelectAll);
  177. mFileListFragment.selectAllFiles(item.isChecked());
  178. break;
  179. }
  180. case R.id.action_sort: {
  181. // Read sorting order, default to sort by name ascending
  182. Integer sortOrder = PreferenceManager.getSortOrder(this);
  183. FragmentManager fm = getSupportFragmentManager();
  184. FragmentTransaction ft = fm.beginTransaction();
  185. ft.addToBackStack(null);
  186. SortingOrderDialogFragment mSortingOrderDialogFragment = SortingOrderDialogFragment.newInstance(
  187. getSortOrder(this),
  188. getSortAscending(this)
  189. );
  190. mSortingOrderDialogFragment.show(ft, SORT_ORDER_DIALOG_TAG);
  191. break;
  192. }
  193. case R.id.action_switch_view: {
  194. if (isGridView()) {
  195. item.setTitle(getString(R.string.action_switch_grid_view));
  196. item.setIcon(R.drawable.ic_view_module);
  197. mFileListFragment.switchToListView();
  198. } else {
  199. item.setTitle(getApplicationContext().getString(R.string.action_switch_list_view));
  200. item.setIcon(R.drawable.ic_view_list);
  201. mFileListFragment.switchToGridView();
  202. }
  203. return true;
  204. }
  205. default:
  206. retval = super.onOptionsItemSelected(item);
  207. }
  208. return retval;
  209. }
  210. @Override
  211. public void onSortingOrderChosen(int selection) {
  212. switch (selection) {
  213. case SortingOrderDialogFragment.BY_NAME_ASC:
  214. mFileListFragment.sortByName(true);
  215. break;
  216. case SortingOrderDialogFragment.BY_NAME_DESC:
  217. mFileListFragment.sortByName(false);
  218. break;
  219. case SortingOrderDialogFragment.BY_MODIFICATION_DATE_ASC:
  220. mFileListFragment.sortByDate(true);
  221. break;
  222. case SortingOrderDialogFragment.BY_MODIFICATION_DATE_DESC:
  223. mFileListFragment.sortByDate(false);
  224. break;
  225. case SortingOrderDialogFragment.BY_SIZE_ASC:
  226. mFileListFragment.sortBySize(true);
  227. break;
  228. case SortingOrderDialogFragment.BY_SIZE_DESC:
  229. mFileListFragment.sortBySize(false);
  230. break;
  231. default: // defaulting to alphabetical-ascending
  232. Log_OC.w(TAG, "Unknown sort order, defaulting to alphabetical-ascending!");
  233. mFileListFragment.sortByName(true);
  234. break;
  235. }
  236. }
  237. @Override
  238. public boolean onNavigationItemSelected(int itemPosition, long itemId) {
  239. int i = itemPosition;
  240. while (i-- != 0) {
  241. onBackPressed();
  242. }
  243. // the next operation triggers a new call to this method, but it's necessary to
  244. // ensure that the name exposed in the action bar is the current directory when the
  245. // user selected it in the navigation list
  246. if (itemPosition != 0) {
  247. getSupportActionBar().setSelectedNavigationItem(0);
  248. }
  249. return true;
  250. }
  251. @Override
  252. public void onBackPressed() {
  253. if (mDirectories.getCount() <= 1) {
  254. finish();
  255. return;
  256. }
  257. popDirname();
  258. mFileListFragment.onNavigateUp();
  259. mCurrentDir = mFileListFragment.getCurrentDirectory();
  260. if(mCurrentDir.getParentFile() == null){
  261. ActionBar actionBar = getSupportActionBar();
  262. actionBar.setDisplayHomeAsUpEnabled(false);
  263. }
  264. // invalidate checked state when navigating directories
  265. setSelectAllMenuItem(mOptionsMenu.findItem(R.id.action_select_all), false);
  266. }
  267. @Override
  268. protected void onSaveInstanceState(Bundle outState) {
  269. // responsibility of restore is preferred in onCreate() before than in
  270. // onRestoreInstanceState when there are Fragments involved
  271. Log_OC.d(TAG, "onSaveInstanceState() start");
  272. super.onSaveInstanceState(outState);
  273. outState.putString(UploadFilesActivity.KEY_DIRECTORY_PATH, mCurrentDir.getAbsolutePath());
  274. outState.putBoolean(UploadFilesActivity.KEY_ALL_SELECTED,
  275. mOptionsMenu.findItem(R.id.action_select_all).isChecked());
  276. Log_OC.d(TAG, "onSaveInstanceState() end");
  277. }
  278. @Override
  279. protected void onResume() {
  280. super.onResume();
  281. MainApp.getFirebaseAnalyticsInstance().setCurrentScreen(this, SCREEN_NAME, TAG);
  282. }
  283. /**
  284. * Pushes a directory to the drop down list
  285. * @param directory to push
  286. * @throws IllegalArgumentException If the {@link File#isDirectory()} returns false.
  287. */
  288. public void pushDirname(File directory) {
  289. if(!directory.isDirectory()){
  290. throw new IllegalArgumentException("Only directories may be pushed!");
  291. }
  292. mDirectories.insert(directory.getName(), 0);
  293. mCurrentDir = directory;
  294. }
  295. /**
  296. * Pops a directory name from the drop down list
  297. * @return True, unless the stack is empty
  298. */
  299. public boolean popDirname() {
  300. mDirectories.remove(mDirectories.getItem(0));
  301. return !mDirectories.isEmpty();
  302. }
  303. private void setSelectAllMenuItem(MenuItem selectAll, boolean checked) {
  304. selectAll.setChecked(checked);
  305. if(checked) {
  306. selectAll.setIcon(R.drawable.ic_select_none);
  307. } else {
  308. selectAll.setIcon(R.drawable.ic_select_all);
  309. }
  310. }
  311. // Custom array adapter to override text colors
  312. private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
  313. public CustomArrayAdapter(UploadFilesActivity ctx, int view) {
  314. super(ctx, view);
  315. }
  316. public View getView(int position, View convertView, ViewGroup parent) {
  317. View v = super.getView(position, convertView, parent);
  318. ((TextView) v).setTextColor(getResources().getColorStateList(
  319. android.R.color.white));
  320. return v;
  321. }
  322. public View getDropDownView(int position, View convertView,
  323. ViewGroup parent) {
  324. View v = super.getDropDownView(position, convertView, parent);
  325. ((TextView) v).setTextColor(getResources().getColorStateList(
  326. android.R.color.white));
  327. return v;
  328. }
  329. }
  330. /**
  331. * {@inheritDoc}
  332. */
  333. @Override
  334. public void onDirectoryClick(File directory) {
  335. // invalidate checked state when navigating directories
  336. MenuItem selectAll = mOptionsMenu.findItem(R.id.action_select_all);
  337. setSelectAllMenuItem(selectAll, false);
  338. pushDirname(directory);
  339. ActionBar actionBar = getSupportActionBar();
  340. actionBar.setDisplayHomeAsUpEnabled(true);
  341. }
  342. /**
  343. * {@inheritDoc}
  344. */
  345. @Override
  346. public void onFileClick(File file) {
  347. // nothing to do
  348. }
  349. /**
  350. * {@inheritDoc}
  351. */
  352. @Override
  353. public File getInitialDirectory() {
  354. return mCurrentDir;
  355. }
  356. /**
  357. * Performs corresponding action when user presses 'Cancel' or 'Upload' button
  358. *
  359. * TODO Make here the real request to the Upload service ; will require to receive the account and
  360. * target folder where the upload must be done in the received intent.
  361. */
  362. @Override
  363. public void onClick(View v) {
  364. if (v.getId() == R.id.upload_files_btn_cancel) {
  365. setResult(RESULT_CANCELED);
  366. finish();
  367. } else if (v.getId() == R.id.upload_files_btn_upload) {
  368. new CheckAvailableSpaceTask().execute(mBehaviourSpinner.getSelectedItemPosition()==0);
  369. }
  370. }
  371. /**
  372. * Asynchronous task checking if there is space enough to copy all the files chosen
  373. * to upload into the ownCloud local folder.
  374. *
  375. * Maybe an AsyncTask is not strictly necessary, but who really knows.
  376. */
  377. private class CheckAvailableSpaceTask extends AsyncTask<Boolean, Void, Boolean> {
  378. /**
  379. * Updates the UI before trying the movement
  380. */
  381. @Override
  382. protected void onPreExecute () {
  383. /// progress dialog and disable 'Move' button
  384. mCurrentDialog = IndeterminateProgressDialog.newInstance(R.string.wait_a_moment, false);
  385. mCurrentDialog.show(getSupportFragmentManager(), WAIT_DIALOG_TAG);
  386. }
  387. /**
  388. * Checks the available space.
  389. *
  390. * @param params boolean flag if storage calculation should be done.
  391. * @return 'True' if there is space enough or doesn't have to be calculated
  392. */
  393. @Override
  394. protected Boolean doInBackground(Boolean... params) {
  395. if(params[0]) {
  396. String[] checkedFilePaths = mFileListFragment.getCheckedFilePaths();
  397. long total = 0;
  398. for (int i = 0; checkedFilePaths != null && i < checkedFilePaths.length; i++) {
  399. String localPath = checkedFilePaths[i];
  400. File localFile = new File(localPath);
  401. total += localFile.length();
  402. }
  403. return FileStorageUtils.getUsableSpace(mAccountOnCreation.name) >= total;
  404. }
  405. return true;
  406. }
  407. /**
  408. * Updates the activity UI after the check of space is done.
  409. *
  410. * If there is not space enough. shows a new dialog to query the user if wants to move the
  411. * files instead of copy them.
  412. *
  413. * @param result 'True' when there is space enough to copy all the selected files.
  414. */
  415. @Override
  416. protected void onPostExecute(Boolean result) {
  417. if(mCurrentDialog != null) {
  418. mCurrentDialog.dismiss();
  419. mCurrentDialog = null;
  420. }
  421. if (result) {
  422. // return the list of selected files (success)
  423. Intent data = new Intent();
  424. data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
  425. // set result code
  426. switch (mBehaviourSpinner.getSelectedItemPosition()) {
  427. case 0: // move to nextcloud folder
  428. setResult(RESULT_OK_AND_MOVE, data);
  429. break;
  430. case 1: // only upload
  431. setResult(RESULT_OK_AND_DO_NOTHING, data);
  432. break;
  433. case 2: // upload and delete from source
  434. setResult(RESULT_OK_AND_DELETE, data);
  435. break;
  436. }
  437. // store behaviour
  438. PreferenceManager.setUploaderBehaviour(getApplicationContext(),
  439. mBehaviourSpinner.getSelectedItemPosition());
  440. finish();
  441. } else {
  442. // show a dialog to query the user if wants to move the selected files
  443. // to the ownCloud folder instead of copying
  444. String[] args = {getString(R.string.app_name)};
  445. ConfirmationDialogFragment dialog = ConfirmationDialogFragment.newInstance(
  446. R.string.upload_query_move_foreign_files, args, 0, R.string.common_yes, -1,
  447. R.string.common_no
  448. );
  449. dialog.setOnConfirmationListener(UploadFilesActivity.this);
  450. dialog.show(getSupportFragmentManager(), QUERY_TO_MOVE_DIALOG_TAG);
  451. }
  452. }
  453. }
  454. @Override
  455. public void onConfirmation(String callerTag) {
  456. Log_OC.d(TAG, "Positive button in dialog was clicked; dialog tag is " + callerTag);
  457. if (callerTag.equals(QUERY_TO_MOVE_DIALOG_TAG)) {
  458. // return the list of selected files to the caller activity (success),
  459. // signaling that they should be moved to the ownCloud folder, instead of copied
  460. Intent data = new Intent();
  461. data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
  462. setResult(RESULT_OK_AND_MOVE, data);
  463. finish();
  464. }
  465. }
  466. @Override
  467. public void onNeutral(String callerTag) {
  468. Log_OC.d(TAG, "Phantom neutral button in dialog was clicked; dialog tag is " + callerTag);
  469. }
  470. @Override
  471. public void onCancel(String callerTag) {
  472. /// nothing to do; don't finish, let the user change the selection
  473. Log_OC.d(TAG, "Negative button in dialog was clicked; dialog tag is " + callerTag);
  474. }
  475. @Override
  476. protected void onAccountSet(boolean stateWasRecovered) {
  477. super.onAccountSet(stateWasRecovered);
  478. if (getAccount() != null) {
  479. if (!mAccountOnCreation.equals(getAccount())) {
  480. setResult(RESULT_CANCELED);
  481. finish();
  482. }
  483. } else {
  484. setResult(RESULT_CANCELED);
  485. finish();
  486. }
  487. }
  488. private boolean isGridView() {
  489. return getListOfFilesFragment().isGridEnabled();
  490. }
  491. private ExtendedListFragment getListOfFilesFragment() {
  492. Fragment listOfFiles = mFileListFragment;
  493. if (listOfFiles != null) {
  494. return (ExtendedListFragment) listOfFiles;
  495. }
  496. Log_OC.e(TAG, "Access to unexisting list of files fragment!!");
  497. return null;
  498. }
  499. }