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