UploadFilesActivity.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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.content.SharedPreferences;
  25. import android.os.AsyncTask;
  26. import android.os.Bundle;
  27. import android.os.Environment;
  28. import android.preference.PreferenceManager;
  29. import android.support.v4.app.DialogFragment;
  30. import android.support.v7.app.ActionBar;
  31. import android.view.Menu;
  32. import android.view.MenuItem;
  33. import android.view.View;
  34. import android.view.View.OnClickListener;
  35. import android.view.ViewGroup;
  36. import android.widget.ArrayAdapter;
  37. import android.widget.Button;
  38. import android.widget.RadioButton;
  39. import android.widget.TextView;
  40. import com.owncloud.android.R;
  41. import com.owncloud.android.files.services.FileUploader;
  42. import com.owncloud.android.lib.common.utils.Log_OC;
  43. import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
  44. import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
  45. import com.owncloud.android.ui.dialog.IndeterminateProgressDialog;
  46. import com.owncloud.android.ui.fragment.LocalFileListFragment;
  47. import com.owncloud.android.utils.FileStorageUtils;
  48. import java.io.File;
  49. /**
  50. * Displays local files and let the user choose what of them wants to upload
  51. * to the current ownCloud account.
  52. */
  53. public class UploadFilesActivity extends FileActivity implements
  54. LocalFileListFragment.ContainerActivity, ActionBar.OnNavigationListener,
  55. OnClickListener, ConfirmationDialogFragmentListener {
  56. private ArrayAdapter<String> mDirectories;
  57. private File mCurrentDir = null;
  58. private boolean mSelectAll = false;
  59. private LocalFileListFragment mFileListFragment;
  60. private Button mCancelBtn;
  61. private Button mUploadBtn;
  62. private Account mAccountOnCreation;
  63. private DialogFragment mCurrentDialog;
  64. private Menu mOptionsMenu;
  65. public static final String EXTRA_CHOSEN_FILES =
  66. UploadFilesActivity.class.getCanonicalName() + ".EXTRA_CHOSEN_FILES";
  67. public static final int RESULT_OK_AND_MOVE = RESULT_FIRST_USER;
  68. private static final String KEY_DIRECTORY_PATH =
  69. UploadFilesActivity.class.getCanonicalName() + ".KEY_DIRECTORY_PATH";
  70. private static final String KEY_ALL_SELECTED =
  71. UploadFilesActivity.class.getCanonicalName() + ".KEY_ALL_SELECTED";
  72. private static final String TAG = "UploadFilesActivity";
  73. private static final String WAIT_DIALOG_TAG = "WAIT";
  74. private static final String QUERY_TO_MOVE_DIALOG_TAG = "QUERY_TO_MOVE";
  75. private RadioButton mRadioBtnCopyFiles;
  76. private RadioButton mRadioBtnMoveFiles;
  77. @Override
  78. public void onCreate(Bundle savedInstanceState) {
  79. Log_OC.d(TAG, "onCreate() start");
  80. super.onCreate(savedInstanceState);
  81. if(savedInstanceState != null) {
  82. mCurrentDir = new File(savedInstanceState.getString(
  83. UploadFilesActivity.KEY_DIRECTORY_PATH));
  84. mSelectAll = savedInstanceState.getBoolean(
  85. UploadFilesActivity.KEY_ALL_SELECTED, false);
  86. } else {
  87. mCurrentDir = Environment.getExternalStorageDirectory();
  88. }
  89. mAccountOnCreation = getAccount();
  90. /// USER INTERFACE
  91. // Drop-down navigation
  92. mDirectories = new CustomArrayAdapter<String>(this,
  93. R.layout.support_simple_spinner_dropdown_item);
  94. File currDir = mCurrentDir;
  95. while(currDir != null && currDir.getParentFile() != null) {
  96. mDirectories.add(currDir.getName());
  97. currDir = currDir.getParentFile();
  98. }
  99. mDirectories.add(File.separator);
  100. // Inflate and set the layout view
  101. setContentView(R.layout.upload_files_layout);
  102. mFileListFragment = (LocalFileListFragment)
  103. getSupportFragmentManager().findFragmentById(R.id.local_files_list);
  104. // Set input controllers
  105. mCancelBtn = (Button) findViewById(R.id.upload_files_btn_cancel);
  106. mCancelBtn.setOnClickListener(this);
  107. mUploadBtn = (Button) findViewById(R.id.upload_files_btn_upload);
  108. mUploadBtn.setOnClickListener(this);
  109. SharedPreferences appPreferences = PreferenceManager
  110. .getDefaultSharedPreferences(getApplicationContext());
  111. Integer localBehaviour = appPreferences.getInt("prefs_uploader_behaviour", FileUploader.LOCAL_BEHAVIOUR_COPY);
  112. mRadioBtnMoveFiles = (RadioButton) findViewById(R.id.upload_radio_move);
  113. if (localBehaviour == FileUploader.LOCAL_BEHAVIOUR_MOVE){
  114. mRadioBtnMoveFiles.setChecked(true);
  115. }
  116. mRadioBtnCopyFiles = (RadioButton) findViewById(R.id.upload_radio_copy);
  117. if (localBehaviour == FileUploader.LOCAL_BEHAVIOUR_COPY){
  118. mRadioBtnCopyFiles.setChecked(true);
  119. }
  120. // setup the toolbar
  121. setupToolbar();
  122. // Action bar setup
  123. ActionBar actionBar = getSupportActionBar();
  124. actionBar.setHomeButtonEnabled(true); // mandatory since Android ICS, according to the
  125. // official documentation
  126. actionBar.setDisplayHomeAsUpEnabled(mCurrentDir != null && mCurrentDir.getName() != null);
  127. actionBar.setDisplayShowTitleEnabled(false);
  128. actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
  129. actionBar.setListNavigationCallbacks(mDirectories, this);
  130. // wait dialog
  131. if (mCurrentDialog != null) {
  132. mCurrentDialog.dismiss();
  133. mCurrentDialog = null;
  134. }
  135. Log_OC.d(TAG, "onCreate() end");
  136. }
  137. /**
  138. * Helper to launch the UploadFilesActivity for which you would like a result when it finished.
  139. * Your onActivityResult() method will be called with the given requestCode.
  140. *
  141. * @param activity the activity which should call the upload activity for a result
  142. * @param account the account for which the upload activity is called
  143. * @param requestCode If >= 0, this code will be returned in onActivityResult()
  144. */
  145. public static void startUploadActivityForResult(Activity activity, Account account, int requestCode) {
  146. Intent action = new Intent(activity, UploadFilesActivity.class);
  147. action.putExtra(EXTRA_ACCOUNT, (account));
  148. activity.startActivityForResult(action, requestCode);
  149. }
  150. @Override
  151. public boolean onCreateOptionsMenu(Menu menu) {
  152. mOptionsMenu = menu;
  153. getMenuInflater().inflate(R.menu.upload_files_picker, menu);
  154. MenuItem selectAll = menu.findItem(R.id.action_select_all);
  155. setSelectAllMenuItem(selectAll, mSelectAll);
  156. return super.onCreateOptionsMenu(menu);
  157. }
  158. @Override
  159. public boolean onOptionsItemSelected(MenuItem item) {
  160. boolean retval = true;
  161. switch (item.getItemId()) {
  162. case android.R.id.home: {
  163. if(mCurrentDir != null && mCurrentDir.getParentFile() != null){
  164. onBackPressed();
  165. }
  166. break;
  167. }
  168. case R.id.action_select_all: {
  169. item.setChecked(!item.isChecked());
  170. mSelectAll = item.isChecked();
  171. setSelectAllMenuItem(item, mSelectAll);
  172. mFileListFragment.selectAllFiles(item.isChecked());
  173. break;
  174. }
  175. default:
  176. retval = super.onOptionsItemSelected(item);
  177. }
  178. return retval;
  179. }
  180. @Override
  181. public boolean onNavigationItemSelected(int itemPosition, long itemId) {
  182. int i = itemPosition;
  183. while (i-- != 0) {
  184. onBackPressed();
  185. }
  186. // the next operation triggers a new call to this method, but it's necessary to
  187. // ensure that the name exposed in the action bar is the current directory when the
  188. // user selected it in the navigation list
  189. if (itemPosition != 0)
  190. getSupportActionBar().setSelectedNavigationItem(0);
  191. return true;
  192. }
  193. @Override
  194. public void onBackPressed() {
  195. if (mDirectories.getCount() <= 1) {
  196. finish();
  197. return;
  198. }
  199. popDirname();
  200. mFileListFragment.onNavigateUp();
  201. mCurrentDir = mFileListFragment.getCurrentDirectory();
  202. if(mCurrentDir.getParentFile() == null){
  203. ActionBar actionBar = getSupportActionBar();
  204. actionBar.setDisplayHomeAsUpEnabled(false);
  205. }
  206. // invalidate checked state when navigating directories
  207. setSelectAllMenuItem(mOptionsMenu.findItem(R.id.action_select_all), false);
  208. }
  209. @Override
  210. protected void onSaveInstanceState(Bundle outState) {
  211. // responsibility of restore is preferred in onCreate() before than in
  212. // onRestoreInstanceState when there are Fragments involved
  213. Log_OC.d(TAG, "onSaveInstanceState() start");
  214. super.onSaveInstanceState(outState);
  215. outState.putString(UploadFilesActivity.KEY_DIRECTORY_PATH, mCurrentDir.getAbsolutePath());
  216. outState.putBoolean(UploadFilesActivity.KEY_ALL_SELECTED,
  217. mOptionsMenu.findItem(R.id.action_select_all).isChecked());
  218. Log_OC.d(TAG, "onSaveInstanceState() end");
  219. }
  220. /**
  221. * Pushes a directory to the drop down list
  222. * @param directory to push
  223. * @throws IllegalArgumentException If the {@link File#isDirectory()} returns false.
  224. */
  225. public void pushDirname(File directory) {
  226. if(!directory.isDirectory()){
  227. throw new IllegalArgumentException("Only directories may be pushed!");
  228. }
  229. mDirectories.insert(directory.getName(), 0);
  230. mCurrentDir = directory;
  231. }
  232. /**
  233. * Pops a directory name from the drop down list
  234. * @return True, unless the stack is empty
  235. */
  236. public boolean popDirname() {
  237. mDirectories.remove(mDirectories.getItem(0));
  238. return !mDirectories.isEmpty();
  239. }
  240. private void setSelectAllMenuItem(MenuItem selectAll, boolean checked) {
  241. selectAll.setChecked(checked);
  242. if(checked) {
  243. selectAll.setIcon(R.drawable.ic_select_none);
  244. } else {
  245. selectAll.setIcon(R.drawable.ic_select_all);
  246. }
  247. }
  248. // Custom array adapter to override text colors
  249. private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
  250. public CustomArrayAdapter(UploadFilesActivity ctx, int view) {
  251. super(ctx, view);
  252. }
  253. public View getView(int position, View convertView, ViewGroup parent) {
  254. View v = super.getView(position, convertView, parent);
  255. ((TextView) v).setTextColor(getResources().getColorStateList(
  256. android.R.color.white));
  257. return v;
  258. }
  259. public View getDropDownView(int position, View convertView,
  260. ViewGroup parent) {
  261. View v = super.getDropDownView(position, convertView, parent);
  262. ((TextView) v).setTextColor(getResources().getColorStateList(
  263. android.R.color.white));
  264. return v;
  265. }
  266. }
  267. /**
  268. * {@inheritDoc}
  269. */
  270. @Override
  271. public void onDirectoryClick(File directory) {
  272. // invalidate checked state when navigating directories
  273. MenuItem selectAll = mOptionsMenu.findItem(R.id.action_select_all);
  274. setSelectAllMenuItem(selectAll, false);
  275. pushDirname(directory);
  276. ActionBar actionBar = getSupportActionBar();
  277. actionBar.setDisplayHomeAsUpEnabled(true);
  278. }
  279. /**
  280. * {@inheritDoc}
  281. */
  282. @Override
  283. public void onFileClick(File file) {
  284. // nothing to do
  285. }
  286. /**
  287. * {@inheritDoc}
  288. */
  289. @Override
  290. public File getInitialDirectory() {
  291. return mCurrentDir;
  292. }
  293. /**
  294. * Performs corresponding action when user presses 'Cancel' or 'Upload' button
  295. *
  296. * TODO Make here the real request to the Upload service ; will require to receive the account and
  297. * target folder where the upload must be done in the received intent.
  298. */
  299. @Override
  300. public void onClick(View v) {
  301. if (v.getId() == R.id.upload_files_btn_cancel) {
  302. setResult(RESULT_CANCELED);
  303. finish();
  304. } else if (v.getId() == R.id.upload_files_btn_upload) {
  305. new CheckAvailableSpaceTask().execute();
  306. }
  307. }
  308. /**
  309. * Asynchronous task checking if there is space enough to copy all the files chosen
  310. * to upload into the ownCloud local folder.
  311. *
  312. * Maybe an AsyncTask is not strictly necessary, but who really knows.
  313. */
  314. private class CheckAvailableSpaceTask extends AsyncTask<Void, Void, Boolean> {
  315. /**
  316. * Updates the UI before trying the movement
  317. */
  318. @Override
  319. protected void onPreExecute () {
  320. /// progress dialog and disable 'Move' button
  321. mCurrentDialog = IndeterminateProgressDialog.newInstance(R.string.wait_a_moment, false);
  322. mCurrentDialog.show(getSupportFragmentManager(), WAIT_DIALOG_TAG);
  323. }
  324. /**
  325. * Checks the available space
  326. *
  327. * @return 'True' if there is space enough.
  328. */
  329. @Override
  330. protected Boolean doInBackground(Void... params) {
  331. String[] checkedFilePaths = mFileListFragment.getCheckedFilePaths();
  332. long total = 0;
  333. for (int i=0; checkedFilePaths != null && i < checkedFilePaths.length ; i++) {
  334. String localPath = checkedFilePaths[i];
  335. File localFile = new File(localPath);
  336. total += localFile.length();
  337. }
  338. return (new Boolean(FileStorageUtils.getUsableSpace(mAccountOnCreation.name) >= total));
  339. }
  340. /**
  341. * Updates the activity UI after the check of space is done.
  342. *
  343. * If there is not space enough. shows a new dialog to query the user if wants to move the
  344. * files instead of copy them.
  345. *
  346. * @param result 'True' when there is space enough to copy all the selected files.
  347. */
  348. @Override
  349. protected void onPostExecute(Boolean result) {
  350. mCurrentDialog.dismiss();
  351. mCurrentDialog = null;
  352. if (result) {
  353. // return the list of selected files (success)
  354. Intent data = new Intent();
  355. data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
  356. SharedPreferences.Editor appPreferencesEditor = PreferenceManager
  357. .getDefaultSharedPreferences(getApplicationContext()).edit();
  358. if (mRadioBtnMoveFiles.isChecked()){
  359. setResult(RESULT_OK_AND_MOVE, data);
  360. appPreferencesEditor.putInt("prefs_uploader_behaviour",
  361. FileUploader.LOCAL_BEHAVIOUR_MOVE);
  362. } else {
  363. setResult(RESULT_OK, data);
  364. appPreferencesEditor.putInt("prefs_uploader_behaviour",
  365. FileUploader.LOCAL_BEHAVIOUR_COPY);
  366. }
  367. appPreferencesEditor.apply();
  368. finish();
  369. } else {
  370. // show a dialog to query the user if wants to move the selected files
  371. // to the ownCloud folder instead of copying
  372. String[] args = {getString(R.string.app_name)};
  373. ConfirmationDialogFragment dialog = ConfirmationDialogFragment.newInstance(
  374. R.string.upload_query_move_foreign_files, args, 0, R.string.common_yes, -1,
  375. R.string.common_no
  376. );
  377. dialog.setOnConfirmationListener(UploadFilesActivity.this);
  378. dialog.show(getSupportFragmentManager(), QUERY_TO_MOVE_DIALOG_TAG);
  379. }
  380. }
  381. }
  382. @Override
  383. public void onConfirmation(String callerTag) {
  384. Log_OC.d(TAG, "Positive button in dialog was clicked; dialog tag is " + callerTag);
  385. if (callerTag.equals(QUERY_TO_MOVE_DIALOG_TAG)) {
  386. // return the list of selected files to the caller activity (success),
  387. // signaling that they should be moved to the ownCloud folder, instead of copied
  388. Intent data = new Intent();
  389. data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
  390. setResult(RESULT_OK_AND_MOVE, data);
  391. finish();
  392. }
  393. }
  394. @Override
  395. public void onNeutral(String callerTag) {
  396. Log_OC.d(TAG, "Phantom neutral button in dialog was clicked; dialog tag is " + callerTag);
  397. }
  398. @Override
  399. public void onCancel(String callerTag) {
  400. /// nothing to do; don't finish, let the user change the selection
  401. Log_OC.d(TAG, "Negative button in dialog was clicked; dialog tag is " + callerTag);
  402. }
  403. @Override
  404. protected void onAccountSet(boolean stateWasRecovered) {
  405. super.onAccountSet(stateWasRecovered);
  406. if (getAccount() != null) {
  407. if (!mAccountOnCreation.equals(getAccount())) {
  408. setResult(RESULT_CANCELED);
  409. finish();
  410. }
  411. } else {
  412. setResult(RESULT_CANCELED);
  413. finish();
  414. }
  415. }
  416. }