UploadFilesActivity.java 27 KB

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