UploadFilesActivity.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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.annotation.SuppressLint;
  23. import android.app.Activity;
  24. import android.content.Intent;
  25. import android.os.Bundle;
  26. import android.os.Environment;
  27. import android.view.Menu;
  28. import android.view.MenuItem;
  29. import android.view.View;
  30. import android.view.View.OnClickListener;
  31. import android.widget.AdapterView;
  32. import android.widget.ArrayAdapter;
  33. import android.widget.Spinner;
  34. import android.widget.TextView;
  35. import com.google.android.material.button.MaterialButton;
  36. import com.nextcloud.client.di.Injectable;
  37. import com.nextcloud.client.preferences.AppPreferences;
  38. import com.owncloud.android.R;
  39. import com.owncloud.android.files.services.FileUploader;
  40. import com.owncloud.android.lib.common.utils.Log_OC;
  41. import com.owncloud.android.ui.adapter.StoragePathAdapter;
  42. import com.owncloud.android.ui.asynctasks.CheckAvailableSpaceTask;
  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.dialog.LocalStoragePathPickerDialogFragment;
  47. import com.owncloud.android.ui.dialog.SortingOrderDialogFragment;
  48. import com.owncloud.android.ui.fragment.ExtendedListFragment;
  49. import com.owncloud.android.ui.fragment.LocalFileListFragment;
  50. import com.owncloud.android.utils.FileSortOrder;
  51. import com.owncloud.android.utils.theme.ThemeButtonUtils;
  52. import com.owncloud.android.utils.theme.ThemeColorUtils;
  53. import com.owncloud.android.utils.theme.ThemeDrawableUtils;
  54. import com.owncloud.android.utils.theme.ThemeToolbarUtils;
  55. import com.owncloud.android.utils.theme.ThemeUtils;
  56. import java.io.File;
  57. import java.util.ArrayList;
  58. import java.util.List;
  59. import javax.inject.Inject;
  60. import androidx.annotation.NonNull;
  61. import androidx.appcompat.app.ActionBar;
  62. import androidx.appcompat.widget.SearchView;
  63. import androidx.core.view.MenuItemCompat;
  64. import androidx.fragment.app.DialogFragment;
  65. import androidx.fragment.app.FragmentManager;
  66. import androidx.fragment.app.FragmentTransaction;
  67. import static com.owncloud.android.ui.activity.FileActivity.EXTRA_ACCOUNT;
  68. /**
  69. * Displays local files and let the user choose what of them wants to upload to the current ownCloud account.
  70. */
  71. public class UploadFilesActivity extends DrawerActivity implements LocalFileListFragment.ContainerActivity,
  72. OnClickListener, ConfirmationDialogFragmentListener, SortingOrderDialogFragment.OnSortingOrderListener,
  73. CheckAvailableSpaceTask.CheckAvailableSpaceListener, StoragePathAdapter.StoragePathAdapterListener, Injectable {
  74. private static final String KEY_ALL_SELECTED = UploadFilesActivity.class.getCanonicalName() + ".KEY_ALL_SELECTED";
  75. public final static String KEY_LOCAL_FOLDER_PICKER_MODE = UploadFilesActivity.class.getCanonicalName() + ".LOCAL_FOLDER_PICKER_MODE";
  76. public static final String LOCAL_BASE_PATH = UploadFilesActivity.class.getCanonicalName() + ".LOCAL_BASE_PATH";
  77. public static final String EXTRA_CHOSEN_FILES = UploadFilesActivity.class.getCanonicalName() + ".EXTRA_CHOSEN_FILES";
  78. public static final String KEY_DIRECTORY_PATH = UploadFilesActivity.class.getCanonicalName() + ".KEY_DIRECTORY_PATH";
  79. private static final int SINGLE_DIR = 1;
  80. public static final int RESULT_OK_AND_DELETE = 3;
  81. public static final int RESULT_OK_AND_DO_NOTHING = 2;
  82. public static final int RESULT_OK_AND_MOVE = RESULT_FIRST_USER;
  83. public static final String REQUEST_CODE_KEY = "requestCode";
  84. private static final String QUERY_TO_MOVE_DIALOG_TAG = "QUERY_TO_MOVE";
  85. private static final String TAG = "UploadFilesActivity";
  86. private static final String WAIT_DIALOG_TAG = "WAIT";
  87. @Inject AppPreferences preferences;
  88. private Account mAccountOnCreation;
  89. private ArrayAdapter<String> mDirectories;
  90. private boolean mLocalFolderPickerMode;
  91. private boolean mSelectAll;
  92. private DialogFragment mCurrentDialog;
  93. private File mCurrentDir;
  94. private int requestCode;
  95. private LocalFileListFragment mFileListFragment;
  96. private LocalStoragePathPickerDialogFragment dialog;
  97. private Menu mOptionsMenu;
  98. private SearchView mSearchView;
  99. private Spinner mBehaviourSpinner;
  100. private MaterialButton uploadButton;
  101. /**
  102. * Helper to launch the UploadFilesActivity for which you would like a result when it finished. Your
  103. * onActivityResult() method will be called with the given requestCode.
  104. *
  105. * @param activity the activity which should call the upload activity for a result
  106. * @param account the account for which the upload activity is called
  107. * @param requestCode If >= 0, this code will be returned in onActivityResult()
  108. */
  109. public static void startUploadActivityForResult(Activity activity, Account account, int requestCode) {
  110. Intent action = new Intent(activity, UploadFilesActivity.class);
  111. action.putExtra(EXTRA_ACCOUNT, account);
  112. action.putExtra(REQUEST_CODE_KEY, requestCode);
  113. activity.startActivityForResult(action, requestCode);
  114. }
  115. @Override
  116. @SuppressLint("WrongViewCast") // wrong error on finding local_files_list
  117. public void onCreate(Bundle savedInstanceState) {
  118. Log_OC.d(TAG, "onCreate() start");
  119. super.onCreate(savedInstanceState);
  120. Bundle extras = getIntent().getExtras();
  121. if (extras != null) {
  122. mLocalFolderPickerMode = extras.getBoolean(KEY_LOCAL_FOLDER_PICKER_MODE, false);
  123. requestCode = (int) extras.get(REQUEST_CODE_KEY);
  124. }
  125. if (savedInstanceState != null) {
  126. mCurrentDir = new File(savedInstanceState.getString(UploadFilesActivity.KEY_DIRECTORY_PATH,
  127. Environment.getExternalStorageDirectory().getAbsolutePath()));
  128. mSelectAll = savedInstanceState.getBoolean(UploadFilesActivity.KEY_ALL_SELECTED, false);
  129. } else {
  130. String lastUploadFrom = preferences.getUploadFromLocalLastPath();
  131. if (!lastUploadFrom.isEmpty()) {
  132. mCurrentDir = new File(lastUploadFrom);
  133. while (!mCurrentDir.exists()) {
  134. mCurrentDir = mCurrentDir.getParentFile();
  135. }
  136. } else {
  137. mCurrentDir = Environment.getExternalStorageDirectory();
  138. }
  139. }
  140. mAccountOnCreation = getAccount();
  141. /// USER INTERFACE
  142. // Drop-down navigation
  143. mDirectories = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item);
  144. mDirectories.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  145. fillDirectoryDropdown();
  146. // Inflate and set the layout view
  147. setContentView(R.layout.upload_files_layout);
  148. if (mLocalFolderPickerMode) {
  149. findViewById(R.id.upload_options).setVisibility(View.GONE);
  150. ((MaterialButton) findViewById(R.id.upload_files_btn_upload)).setText(R.string.uploader_btn_alternative_text);
  151. }
  152. mFileListFragment = (LocalFileListFragment) getSupportFragmentManager().findFragmentByTag("local_files_list");
  153. // Set input controllers
  154. MaterialButton cancelButton = findViewById(R.id.upload_files_btn_cancel);
  155. cancelButton.setTextColor(ThemeColorUtils.primaryColor(this, true));
  156. cancelButton.setOnClickListener(this);
  157. uploadButton = findViewById(R.id.upload_files_btn_upload);
  158. ThemeButtonUtils.colorPrimaryButton(uploadButton, this);
  159. uploadButton.setOnClickListener(this);
  160. uploadButton.setEnabled(false);
  161. int localBehaviour = preferences.getUploaderBehaviour();
  162. // file upload spinner
  163. mBehaviourSpinner = findViewById(R.id.upload_files_spinner_behaviour);
  164. List<String> behaviours = new ArrayList<>();
  165. behaviours.add(getString(R.string.uploader_upload_files_behaviour_move_to_nextcloud_folder,
  166. ThemeUtils.getDefaultDisplayNameForRootFolder(this)));
  167. behaviours.add(getString(R.string.uploader_upload_files_behaviour_only_upload));
  168. behaviours.add(getString(R.string.uploader_upload_files_behaviour_upload_and_delete_from_source));
  169. ArrayAdapter<String> behaviourAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item,
  170. behaviours);
  171. behaviourAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  172. mBehaviourSpinner.setAdapter(behaviourAdapter);
  173. mBehaviourSpinner.setSelection(localBehaviour);
  174. // setup the toolbar
  175. setupToolbar();
  176. findViewById(R.id.sort_list_button_group).setVisibility(View.VISIBLE);
  177. findViewById(R.id.switch_grid_view_button).setVisibility(View.GONE);
  178. // Action bar setup
  179. ActionBar actionBar = getSupportActionBar();
  180. if (actionBar != null) {
  181. actionBar.setHomeButtonEnabled(true); // mandatory since Android ICS, according to the official documentation
  182. actionBar.setDisplayHomeAsUpEnabled(mCurrentDir != null);
  183. actionBar.setDisplayShowTitleEnabled(false);
  184. ThemeToolbarUtils.tintBackButton(actionBar, this);
  185. }
  186. showToolbarSpinner();
  187. mToolbarSpinner.setAdapter(mDirectories);
  188. mToolbarSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  189. @Override
  190. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  191. int i = position;
  192. while (i-- != 0) {
  193. onBackPressed();
  194. }
  195. // the next operation triggers a new call to this method, but it's necessary to
  196. // ensure that the name exposed in the action bar is the current directory when the
  197. // user selected it in the navigation list
  198. if (position != 0) {
  199. mToolbarSpinner.setSelection(0);
  200. }
  201. }
  202. @Override
  203. public void onNothingSelected(AdapterView<?> parent) {
  204. // no action
  205. }
  206. });
  207. // wait dialog
  208. if (mCurrentDialog != null) {
  209. mCurrentDialog.dismiss();
  210. mCurrentDialog = null;
  211. }
  212. checkWritableFolder(mCurrentDir);
  213. Log_OC.d(TAG, "onCreate() end");
  214. }
  215. public void showToolbarSpinner() {
  216. mToolbarSpinner.setVisibility(View.VISIBLE);
  217. }
  218. private void fillDirectoryDropdown() {
  219. File currentDir = mCurrentDir;
  220. while (currentDir != null && currentDir.getParentFile() != null) {
  221. mDirectories.add(currentDir.getName());
  222. currentDir = currentDir.getParentFile();
  223. }
  224. mDirectories.add(File.separator);
  225. }
  226. @Override
  227. public boolean onCreateOptionsMenu(Menu menu) {
  228. mOptionsMenu = menu;
  229. getMenuInflater().inflate(R.menu.activity_upload_files, menu);
  230. if (!mLocalFolderPickerMode) {
  231. MenuItem selectAll = menu.findItem(R.id.action_select_all);
  232. setSelectAllMenuItem(selectAll, mSelectAll);
  233. }
  234. int fontColor = ThemeColorUtils.appBarPrimaryFontColor(this);
  235. final MenuItem item = menu.findItem(R.id.action_search);
  236. mSearchView = (SearchView) MenuItemCompat.getActionView(item);
  237. ThemeToolbarUtils.themeSearchView(mSearchView, this);
  238. ThemeDrawableUtils.tintDrawable(menu.findItem(R.id.action_choose_storage_path).getIcon(), fontColor);
  239. mSearchView.setOnSearchClickListener(v -> mToolbarSpinner.setVisibility(View.GONE));
  240. return super.onCreateOptionsMenu(menu);
  241. }
  242. @Override
  243. public boolean onOptionsItemSelected(MenuItem item) {
  244. boolean retval = true;
  245. switch (item.getItemId()) {
  246. case android.R.id.home: {
  247. if (mCurrentDir != null && mCurrentDir.getParentFile() != null) {
  248. onBackPressed();
  249. }
  250. break;
  251. }
  252. case R.id.action_select_all: {
  253. item.setChecked(!item.isChecked());
  254. mSelectAll = item.isChecked();
  255. setSelectAllMenuItem(item, mSelectAll);
  256. mFileListFragment.selectAllFiles(item.isChecked());
  257. break;
  258. }
  259. case R.id.action_choose_storage_path: {
  260. showLocalStoragePathPickerDialog();
  261. break;
  262. }
  263. default:
  264. retval = super.onOptionsItemSelected(item);
  265. break;
  266. }
  267. return retval;
  268. }
  269. private void showLocalStoragePathPickerDialog() {
  270. FragmentManager fm = getSupportFragmentManager();
  271. FragmentTransaction ft = fm.beginTransaction();
  272. ft.addToBackStack(null);
  273. dialog = LocalStoragePathPickerDialogFragment.newInstance();
  274. dialog.show(ft, LocalStoragePathPickerDialogFragment.LOCAL_STORAGE_PATH_PICKER_FRAGMENT);
  275. }
  276. @Override
  277. public void onSortingOrderChosen(FileSortOrder selection) {
  278. preferences.setSortOrder(FileSortOrder.Type.uploadFilesView, selection);
  279. mFileListFragment.sortFiles(selection);
  280. }
  281. private boolean isSearchOpen() {
  282. if (mSearchView == null) {
  283. return false;
  284. } else {
  285. View mSearchEditFrame = mSearchView.findViewById(androidx.appcompat.R.id.search_edit_frame);
  286. return mSearchEditFrame != null && mSearchEditFrame.getVisibility() == View.VISIBLE;
  287. }
  288. }
  289. @Override
  290. public void onBackPressed() {
  291. if (isSearchOpen() && mSearchView != null) {
  292. mSearchView.setQuery("", false);
  293. mFileListFragment.onClose();
  294. mSearchView.onActionViewCollapsed();
  295. setDrawerIndicatorEnabled(isDrawerIndicatorAvailable());
  296. } else {
  297. if (mDirectories.getCount() <= SINGLE_DIR) {
  298. finish();
  299. return;
  300. }
  301. File parentFolder = mCurrentDir.getParentFile();
  302. if (!parentFolder.canRead()) {
  303. showLocalStoragePathPickerDialog();
  304. return;
  305. }
  306. popDirname();
  307. mFileListFragment.onNavigateUp();
  308. mCurrentDir = mFileListFragment.getCurrentDirectory();
  309. checkWritableFolder(mCurrentDir);
  310. if (mCurrentDir.getParentFile() == null) {
  311. ActionBar actionBar = getSupportActionBar();
  312. if (actionBar != null) {
  313. actionBar.setDisplayHomeAsUpEnabled(false);
  314. }
  315. }
  316. // invalidate checked state when navigating directories
  317. if (!mLocalFolderPickerMode) {
  318. setSelectAllMenuItem(mOptionsMenu.findItem(R.id.action_select_all), false);
  319. }
  320. }
  321. }
  322. @Override
  323. protected void onSaveInstanceState(@NonNull Bundle outState) {
  324. // responsibility of restore is preferred in onCreate() before than in
  325. // onRestoreInstanceState when there are Fragments involved
  326. Log_OC.d(TAG, "onSaveInstanceState() start");
  327. super.onSaveInstanceState(outState);
  328. outState.putString(UploadFilesActivity.KEY_DIRECTORY_PATH, mCurrentDir.getAbsolutePath());
  329. if (mOptionsMenu != null && mOptionsMenu.findItem(R.id.action_select_all) != null) {
  330. outState.putBoolean(UploadFilesActivity.KEY_ALL_SELECTED, mOptionsMenu.findItem(R.id.action_select_all).isChecked());
  331. } else {
  332. outState.putBoolean(UploadFilesActivity.KEY_ALL_SELECTED, false);
  333. }
  334. Log_OC.d(TAG, "onSaveInstanceState() end");
  335. }
  336. /**
  337. * Pushes a directory to the drop down list
  338. *
  339. * @param directory to push
  340. * @throws IllegalArgumentException If the {@link File#isDirectory()} returns false.
  341. */
  342. public void pushDirname(File directory) {
  343. if (!directory.isDirectory()) {
  344. throw new IllegalArgumentException("Only directories may be pushed!");
  345. }
  346. mDirectories.insert(directory.getName(), 0);
  347. mCurrentDir = directory;
  348. checkWritableFolder(mCurrentDir);
  349. }
  350. /**
  351. * Pops a directory name from the drop down list
  352. *
  353. * @return True, unless the stack is empty
  354. */
  355. public boolean popDirname() {
  356. mDirectories.remove(mDirectories.getItem(0));
  357. return !mDirectories.isEmpty();
  358. }
  359. private void setSelectAllMenuItem(MenuItem selectAll, boolean checked) {
  360. selectAll.setChecked(checked);
  361. if (checked) {
  362. selectAll.setIcon(R.drawable.ic_select_none);
  363. } else {
  364. selectAll.setIcon(
  365. ThemeDrawableUtils.tintDrawable(R.drawable.ic_select_all, ThemeColorUtils.primaryColor(this)));
  366. }
  367. }
  368. @Override
  369. public void onCheckAvailableSpaceStart() {
  370. if (requestCode == FileDisplayActivity.REQUEST_CODE__SELECT_FILES_FROM_FILE_SYSTEM) {
  371. mCurrentDialog = IndeterminateProgressDialog.newInstance(R.string.wait_a_moment, false);
  372. mCurrentDialog.show(getSupportFragmentManager(), WAIT_DIALOG_TAG);
  373. }
  374. }
  375. /**
  376. * Updates the activity UI after the check of space is done. If there is not space enough. shows a new dialog to
  377. * query the user if wants to move the files instead of copy them.
  378. *
  379. * @param hasEnoughSpaceAvailable 'True' when there is space enough to copy all the selected files.
  380. */
  381. @Override
  382. public void onCheckAvailableSpaceFinish(boolean hasEnoughSpaceAvailable, String... filesToUpload) {
  383. if (mCurrentDialog != null) {
  384. mCurrentDialog.dismiss();
  385. mCurrentDialog = null;
  386. }
  387. if (hasEnoughSpaceAvailable) {
  388. // return the list of files (success)
  389. Intent data = new Intent();
  390. if (requestCode == FileDisplayActivity.REQUEST_CODE__UPLOAD_FROM_CAMERA) {
  391. data.putExtra(EXTRA_CHOSEN_FILES, new String[]{filesToUpload[0]});
  392. setResult(RESULT_OK_AND_DELETE, data);
  393. preferences.setUploaderBehaviour(FileUploader.LOCAL_BEHAVIOUR_DELETE);
  394. } else {
  395. data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
  396. data.putExtra(LOCAL_BASE_PATH, mCurrentDir.getAbsolutePath());
  397. // set result code
  398. switch (mBehaviourSpinner.getSelectedItemPosition()) {
  399. case 0: // move to nextcloud folder
  400. setResult(RESULT_OK_AND_MOVE, data);
  401. break;
  402. case 1: // only upload
  403. setResult(RESULT_OK_AND_DO_NOTHING, data);
  404. break;
  405. case 2: // upload and delete from source
  406. setResult(RESULT_OK_AND_DELETE, data);
  407. break;
  408. default:
  409. // do nothing
  410. break;
  411. }
  412. // store behaviour
  413. preferences.setUploaderBehaviour(mBehaviourSpinner.getSelectedItemPosition());
  414. }
  415. finish();
  416. } else {
  417. // show a dialog to query the user if wants to move the selected files
  418. // to the ownCloud folder instead of copying
  419. String[] args = {getString(R.string.app_name)};
  420. ConfirmationDialogFragment dialog = ConfirmationDialogFragment.newInstance(
  421. R.string.upload_query_move_foreign_files, args, 0, R.string.common_yes, -1,
  422. R.string.common_no
  423. );
  424. dialog.setOnConfirmationListener(this);
  425. dialog.show(getSupportFragmentManager(), QUERY_TO_MOVE_DIALOG_TAG);
  426. }
  427. }
  428. @Override
  429. public void chosenPath(String path) {
  430. if (getListOfFilesFragment() instanceof LocalFileListFragment) {
  431. File file = new File(path);
  432. ((LocalFileListFragment) getListOfFilesFragment()).listDirectory(file);
  433. onDirectoryClick(file);
  434. mCurrentDir = new File(path);
  435. mDirectories.clear();
  436. fillDirectoryDropdown();
  437. }
  438. }
  439. /**
  440. * {@inheritDoc}
  441. */
  442. @Override
  443. public void onDirectoryClick(File directory) {
  444. if (!mLocalFolderPickerMode) {
  445. // invalidate checked state when navigating directories
  446. MenuItem selectAll = mOptionsMenu.findItem(R.id.action_select_all);
  447. setSelectAllMenuItem(selectAll, false);
  448. }
  449. pushDirname(directory);
  450. ActionBar actionBar = getSupportActionBar();
  451. actionBar.setDisplayHomeAsUpEnabled(true);
  452. }
  453. private void checkWritableFolder(File folder) {
  454. boolean canWriteIntoFolder = folder.canWrite();
  455. mBehaviourSpinner.setEnabled(canWriteIntoFolder);
  456. TextView textView = findViewById(R.id.upload_files_upload_files_behaviour_text);
  457. if (canWriteIntoFolder) {
  458. textView.setText(getString(R.string.uploader_upload_files_behaviour));
  459. int localBehaviour = preferences.getUploaderBehaviour();
  460. mBehaviourSpinner.setSelection(localBehaviour);
  461. } else {
  462. mBehaviourSpinner.setSelection(1);
  463. textView.setText(new StringBuilder().append(getString(R.string.uploader_upload_files_behaviour))
  464. .append(' ')
  465. .append(getString(R.string.uploader_upload_files_behaviour_not_writable))
  466. .toString());
  467. }
  468. }
  469. /**
  470. * {@inheritDoc}
  471. */
  472. @Override
  473. public void onFileClick(File file) {
  474. uploadButton.setEnabled(mFileListFragment.getCheckedFilesCount() > 0);
  475. }
  476. /**
  477. * {@inheritDoc}
  478. */
  479. @Override
  480. public File getInitialDirectory() {
  481. return mCurrentDir;
  482. }
  483. /**
  484. * {@inheritDoc}
  485. */
  486. @Override
  487. public boolean isFolderPickerMode() {
  488. return mLocalFolderPickerMode;
  489. }
  490. /**
  491. * Performs corresponding action when user presses 'Cancel' or 'Upload' button
  492. * <p>
  493. * TODO Make here the real request to the Upload service ; will require to receive the account and target folder
  494. * where the upload must be done in the received intent.
  495. */
  496. @Override
  497. public void onClick(View v) {
  498. if (v.getId() == R.id.upload_files_btn_cancel) {
  499. setResult(RESULT_CANCELED);
  500. finish();
  501. } else if (v.getId() == R.id.upload_files_btn_upload) {
  502. if (mCurrentDir != null) {
  503. preferences.setUploadFromLocalLastPath(mCurrentDir.getAbsolutePath());
  504. }
  505. if (mLocalFolderPickerMode) {
  506. Intent data = new Intent();
  507. if (mCurrentDir != null) {
  508. data.putExtra(EXTRA_CHOSEN_FILES, mCurrentDir.getAbsolutePath());
  509. }
  510. setResult(RESULT_OK, data);
  511. finish();
  512. } else {
  513. new CheckAvailableSpaceTask(this, mFileListFragment.getCheckedFilePaths())
  514. .execute(mBehaviourSpinner.getSelectedItemPosition() == 0);
  515. }
  516. }
  517. }
  518. @Override
  519. public void onConfirmation(String callerTag) {
  520. Log_OC.d(TAG, "Positive button in dialog was clicked; dialog tag is " + callerTag);
  521. if (QUERY_TO_MOVE_DIALOG_TAG.equals(callerTag)) {
  522. // return the list of selected files to the caller activity (success),
  523. // signaling that they should be moved to the ownCloud folder, instead of copied
  524. Intent data = new Intent();
  525. data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
  526. setResult(RESULT_OK_AND_MOVE, data);
  527. finish();
  528. }
  529. }
  530. @Override
  531. public void onNeutral(String callerTag) {
  532. Log_OC.d(TAG, "Phantom neutral button in dialog was clicked; dialog tag is " + callerTag);
  533. }
  534. @Override
  535. public void onCancel(String callerTag) {
  536. /// nothing to do; don't finish, let the user change the selection
  537. Log_OC.d(TAG, "Negative button in dialog was clicked; dialog tag is " + callerTag);
  538. }
  539. @Override
  540. protected void onStart() {
  541. super.onStart();
  542. if (getAccount() != null) {
  543. if (!mAccountOnCreation.equals(getAccount())) {
  544. setResult(RESULT_CANCELED);
  545. finish();
  546. }
  547. } else {
  548. setResult(RESULT_CANCELED);
  549. finish();
  550. }
  551. }
  552. private boolean isGridView() {
  553. return getListOfFilesFragment().isGridEnabled();
  554. }
  555. private ExtendedListFragment getListOfFilesFragment() {
  556. if (mFileListFragment == null) {
  557. Log_OC.e(TAG, "Access to unexisting list of files fragment!!");
  558. }
  559. return mFileListFragment;
  560. }
  561. @Override
  562. protected void onStop() {
  563. if (dialog != null) {
  564. dialog.dismissAllowingStateLoss();
  565. }
  566. super.onStop();
  567. }
  568. }