UploadFilesActivity.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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.EditText;
  37. import android.widget.ImageView;
  38. import android.widget.Spinner;
  39. import android.widget.TextView;
  40. import com.google.android.material.button.MaterialButton;
  41. import com.nextcloud.client.di.Injectable;
  42. import com.nextcloud.client.preferences.AppPreferences;
  43. import com.owncloud.android.R;
  44. import com.owncloud.android.files.services.FileUploader;
  45. import com.owncloud.android.lib.common.utils.Log_OC;
  46. import com.owncloud.android.ui.adapter.StoragePathAdapter;
  47. import com.owncloud.android.ui.asynctasks.CheckAvailableSpaceTask;
  48. import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
  49. import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
  50. import com.owncloud.android.ui.dialog.IndeterminateProgressDialog;
  51. import com.owncloud.android.ui.dialog.LocalStoragePathPickerDialogFragment;
  52. import com.owncloud.android.ui.dialog.SortingOrderDialogFragment;
  53. import com.owncloud.android.ui.fragment.ExtendedListFragment;
  54. import com.owncloud.android.ui.fragment.LocalFileListFragment;
  55. import com.owncloud.android.utils.FileSortOrder;
  56. import com.owncloud.android.utils.ThemeUtils;
  57. import java.io.File;
  58. import java.util.ArrayList;
  59. import java.util.List;
  60. import javax.inject.Inject;
  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.FragmentManager;
  69. import androidx.fragment.app.FragmentTransaction;
  70. /**
  71. * Displays local files and let the user choose what of them wants to upload
  72. * to the current ownCloud account.
  73. */
  74. public class UploadFilesActivity extends FileActivity implements
  75. LocalFileListFragment.ContainerActivity, ActionBar.OnNavigationListener,
  76. OnClickListener, ConfirmationDialogFragmentListener, SortingOrderDialogFragment.OnSortingOrderListener,
  77. CheckAvailableSpaceTask.CheckAvailableSpaceListener, StoragePathAdapter.StoragePathAdapterListener, Injectable {
  78. private static final String SORT_ORDER_DIALOG_TAG = "SORT_ORDER_DIALOG";
  79. private static final int SINGLE_DIR = 1;
  80. private ArrayAdapter<String> mDirectories;
  81. private File mCurrentDir;
  82. private boolean mSelectAll;
  83. private boolean mLocalFolderPickerMode;
  84. private LocalFileListFragment mFileListFragment;
  85. protected MaterialButton mUploadBtn;
  86. private Spinner mBehaviourSpinner;
  87. private Account mAccountOnCreation;
  88. private DialogFragment mCurrentDialog;
  89. private Menu mOptionsMenu;
  90. private SearchView mSearchView;
  91. public static final String EXTRA_CHOSEN_FILES =
  92. UploadFilesActivity.class.getCanonicalName() + ".EXTRA_CHOSEN_FILES";
  93. public static final String EXTRA_ACTION = UploadFilesActivity.class.getCanonicalName() + ".EXTRA_ACTION";
  94. public final static String KEY_LOCAL_FOLDER_PICKER_MODE = UploadFilesActivity.class.getCanonicalName()
  95. + ".LOCAL_FOLDER_PICKER_MODE";
  96. public static final int RESULT_OK_AND_MOVE = RESULT_FIRST_USER;
  97. public static final int RESULT_OK_AND_DO_NOTHING = 2;
  98. public static final int RESULT_OK_AND_DELETE = 3;
  99. public static final String KEY_DIRECTORY_PATH =
  100. UploadFilesActivity.class.getCanonicalName() + ".KEY_DIRECTORY_PATH";
  101. private static final String KEY_ALL_SELECTED =
  102. UploadFilesActivity.class.getCanonicalName() + ".KEY_ALL_SELECTED";
  103. private static final String TAG = "UploadFilesActivity";
  104. private static final String WAIT_DIALOG_TAG = "WAIT";
  105. private static final String QUERY_TO_MOVE_DIALOG_TAG = "QUERY_TO_MOVE";
  106. public static final String REQUEST_CODE_KEY = "requestCode";
  107. @Inject AppPreferences preferences;
  108. private int requestCode;
  109. private LocalStoragePathPickerDialogFragment dialog;
  110. @Override
  111. public void onCreate(Bundle savedInstanceState) {
  112. Log_OC.d(TAG, "onCreate() start");
  113. super.onCreate(savedInstanceState);
  114. Bundle extras = getIntent().getExtras();
  115. if (extras != null) {
  116. mLocalFolderPickerMode = extras.getBoolean(KEY_LOCAL_FOLDER_PICKER_MODE, false);
  117. requestCode = (int) extras.get(REQUEST_CODE_KEY);
  118. }
  119. if (savedInstanceState != null) {
  120. mCurrentDir = new File(savedInstanceState.getString(UploadFilesActivity.KEY_DIRECTORY_PATH, Environment
  121. .getExternalStorageDirectory().getAbsolutePath()));
  122. mSelectAll = savedInstanceState.getBoolean(UploadFilesActivity.KEY_ALL_SELECTED, false);
  123. } else {
  124. String lastUploadFrom = preferences.getUploadFromLocalLastPath();
  125. if (!lastUploadFrom.isEmpty()) {
  126. mCurrentDir = new File(lastUploadFrom);
  127. while (!mCurrentDir.exists()) {
  128. mCurrentDir = mCurrentDir.getParentFile();
  129. }
  130. } else {
  131. mCurrentDir = Environment.getExternalStorageDirectory();
  132. }
  133. }
  134. mAccountOnCreation = getAccount();
  135. /// USER INTERFACE
  136. // Drop-down navigation
  137. mDirectories = new CustomArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item);
  138. fillDirectoryDropdown();
  139. // Inflate and set the layout view
  140. setContentView(R.layout.upload_files_layout);
  141. if (mLocalFolderPickerMode) {
  142. findViewById(R.id.upload_options).setVisibility(View.GONE);
  143. ((MaterialButton) findViewById(R.id.upload_files_btn_upload))
  144. .setText(R.string.uploader_btn_alternative_text);
  145. }
  146. mFileListFragment = (LocalFileListFragment) getSupportFragmentManager().findFragmentById(R.id.local_files_list);
  147. // Set input controllers
  148. MaterialButton mCancelButton = findViewById(R.id.upload_files_btn_cancel);
  149. mCancelButton.setTextColor(ThemeUtils.primaryColor(this, true));
  150. mCancelButton.setOnClickListener(this);
  151. mUploadBtn = findViewById(R.id.upload_files_btn_upload);
  152. mUploadBtn.setBackgroundTintMode(PorterDuff.Mode.SRC_ATOP);
  153. mUploadBtn.setBackgroundTintList(ColorStateList.valueOf(ThemeUtils.primaryColor(this, true)));
  154. mUploadBtn.setTextColor(ThemeUtils.fontColor(this, false));
  155. mUploadBtn.setOnClickListener(this);
  156. int localBehaviour = preferences.getUploaderBehaviour();
  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. if (actionBar != null) {
  174. actionBar.setHomeButtonEnabled(true); // mandatory since Android ICS, according to the official documentation
  175. actionBar.setDisplayHomeAsUpEnabled(mCurrentDir != null && mCurrentDir.getName() != null);
  176. actionBar.setDisplayShowTitleEnabled(false);
  177. actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
  178. actionBar.setListNavigationCallbacks(mDirectories, this);
  179. Drawable backArrow = getResources().getDrawable(R.drawable.ic_arrow_back);
  180. actionBar.setHomeAsUpIndicator(ThemeUtils.tintDrawable(backArrow, ThemeUtils.fontAppbarColor(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.activity_upload_files, 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. ThemeUtils.tintDrawable(menu.findItem(R.id.action_choose_storage_path).getIcon(), fontColor);
  231. return super.onCreateOptionsMenu(menu);
  232. }
  233. @Override
  234. public boolean onOptionsItemSelected(MenuItem item) {
  235. boolean retval = true;
  236. switch (item.getItemId()) {
  237. case android.R.id.home: {
  238. if(mCurrentDir != null && mCurrentDir.getParentFile() != null){
  239. onBackPressed();
  240. }
  241. break;
  242. }
  243. case R.id.action_select_all: {
  244. item.setChecked(!item.isChecked());
  245. mSelectAll = item.isChecked();
  246. setSelectAllMenuItem(item, mSelectAll);
  247. mFileListFragment.selectAllFiles(item.isChecked());
  248. break;
  249. }
  250. case R.id.action_sort: {
  251. FragmentManager fm = getSupportFragmentManager();
  252. FragmentTransaction ft = fm.beginTransaction();
  253. ft.addToBackStack(null);
  254. SortingOrderDialogFragment mSortingOrderDialogFragment = SortingOrderDialogFragment.newInstance(
  255. preferences.getSortOrderByType(FileSortOrder.Type.uploadFilesView));
  256. mSortingOrderDialogFragment.show(ft, SORT_ORDER_DIALOG_TAG);
  257. break;
  258. }
  259. case R.id.action_switch_view: {
  260. if (isGridView()) {
  261. item.setTitle(getString(R.string.action_switch_grid_view));
  262. item.setIcon(R.drawable.ic_view_module);
  263. mFileListFragment.switchToListView();
  264. } else {
  265. item.setTitle(getApplicationContext().getString(R.string.action_switch_list_view));
  266. item.setIcon(R.drawable.ic_view_list);
  267. mFileListFragment.switchToGridView();
  268. }
  269. break;
  270. }
  271. case R.id.action_choose_storage_path: {
  272. showLocalStoragePathPickerDialog();
  273. break;
  274. }
  275. default:
  276. retval = super.onOptionsItemSelected(item);
  277. break;
  278. }
  279. return retval;
  280. }
  281. private void showLocalStoragePathPickerDialog() {
  282. FragmentManager fm = getSupportFragmentManager();
  283. FragmentTransaction ft = fm.beginTransaction();
  284. ft.addToBackStack(null);
  285. dialog = LocalStoragePathPickerDialogFragment.newInstance();
  286. dialog.show(ft, LocalStoragePathPickerDialogFragment.LOCAL_STORAGE_PATH_PICKER_FRAGMENT);
  287. }
  288. @Override
  289. public void onSortingOrderChosen(FileSortOrder selection) {
  290. mFileListFragment.sortFiles(selection);
  291. }
  292. @Override
  293. public boolean onNavigationItemSelected(int itemPosition, long itemId) {
  294. int i = itemPosition;
  295. while (i-- != 0) {
  296. onBackPressed();
  297. }
  298. // the next operation triggers a new call to this method, but it's necessary to
  299. // ensure that the name exposed in the action bar is the current directory when the
  300. // user selected it in the navigation list
  301. if (itemPosition != 0) {
  302. getSupportActionBar().setSelectedNavigationItem(0);
  303. }
  304. return true;
  305. }
  306. private boolean isSearchOpen() {
  307. if (mSearchView == null) {
  308. return false;
  309. } else {
  310. View mSearchEditFrame = mSearchView.findViewById(androidx.appcompat.R.id.search_edit_frame);
  311. return mSearchEditFrame != null && mSearchEditFrame.getVisibility() == View.VISIBLE;
  312. }
  313. }
  314. @Override
  315. public void onBackPressed() {
  316. if (isSearchOpen() && mSearchView != null) {
  317. mSearchView.setQuery("", false);
  318. mFileListFragment.onClose();
  319. mSearchView.onActionViewCollapsed();
  320. setDrawerIndicatorEnabled(isDrawerIndicatorAvailable());
  321. } else {
  322. if (mDirectories.getCount() <= SINGLE_DIR) {
  323. finish();
  324. return;
  325. }
  326. File parentFolder = mCurrentDir.getParentFile();
  327. if (!parentFolder.canRead()) {
  328. showLocalStoragePathPickerDialog();
  329. return;
  330. }
  331. popDirname();
  332. mFileListFragment.onNavigateUp();
  333. mCurrentDir = mFileListFragment.getCurrentDirectory();
  334. checkWritableFolder(mCurrentDir);
  335. if (mCurrentDir.getParentFile() == null) {
  336. ActionBar actionBar = getSupportActionBar();
  337. if (actionBar != null) {
  338. actionBar.setDisplayHomeAsUpEnabled(false);
  339. }
  340. }
  341. // invalidate checked state when navigating directories
  342. if (!mLocalFolderPickerMode) {
  343. setSelectAllMenuItem(mOptionsMenu.findItem(R.id.action_select_all), false);
  344. }
  345. }
  346. }
  347. @Override
  348. protected void onSaveInstanceState(@NonNull Bundle outState) {
  349. // responsibility of restore is preferred in onCreate() before than in
  350. // onRestoreInstanceState when there are Fragments involved
  351. Log_OC.d(TAG, "onSaveInstanceState() start");
  352. super.onSaveInstanceState(outState);
  353. outState.putString(UploadFilesActivity.KEY_DIRECTORY_PATH, mCurrentDir.getAbsolutePath());
  354. if (mOptionsMenu != null && mOptionsMenu.findItem(R.id.action_select_all) != null) {
  355. outState.putBoolean(UploadFilesActivity.KEY_ALL_SELECTED,
  356. mOptionsMenu.findItem(R.id.action_select_all).isChecked());
  357. } else {
  358. outState.putBoolean(UploadFilesActivity.KEY_ALL_SELECTED, false);
  359. }
  360. Log_OC.d(TAG, "onSaveInstanceState() end");
  361. }
  362. /**
  363. * Pushes a directory to the drop down list
  364. * @param directory to push
  365. * @throws IllegalArgumentException If the {@link File#isDirectory()} returns false.
  366. */
  367. public void pushDirname(File directory) {
  368. if(!directory.isDirectory()){
  369. throw new IllegalArgumentException("Only directories may be pushed!");
  370. }
  371. mDirectories.insert(directory.getName(), 0);
  372. mCurrentDir = directory;
  373. checkWritableFolder(mCurrentDir);
  374. }
  375. /**
  376. * Pops a directory name from the drop down list
  377. * @return True, unless the stack is empty
  378. */
  379. public boolean popDirname() {
  380. mDirectories.remove(mDirectories.getItem(0));
  381. return !mDirectories.isEmpty();
  382. }
  383. private void setSelectAllMenuItem(MenuItem selectAll, boolean checked) {
  384. selectAll.setChecked(checked);
  385. if(checked) {
  386. selectAll.setIcon(R.drawable.ic_select_none);
  387. } else {
  388. selectAll.setIcon(ThemeUtils.tintDrawable(R.drawable.ic_select_all, ThemeUtils.primaryColor(this)));
  389. }
  390. }
  391. @Override
  392. public void onCheckAvailableSpaceStart() {
  393. if (requestCode == FileDisplayActivity.REQUEST_CODE__SELECT_FILES_FROM_FILE_SYSTEM) {
  394. mCurrentDialog = IndeterminateProgressDialog.newInstance(R.string.wait_a_moment, false);
  395. mCurrentDialog.show(getSupportFragmentManager(), WAIT_DIALOG_TAG);
  396. }
  397. }
  398. /**
  399. * Updates the activity UI after the check of space is done. If there is not space enough. shows a new dialog to
  400. * query the user if wants to move the files instead of copy them.
  401. *
  402. * @param hasEnoughSpaceAvailable 'True' when there is space enough to copy all the selected files.
  403. */
  404. @Override
  405. public void onCheckAvailableSpaceFinish(boolean hasEnoughSpaceAvailable, String... filesToUpload) {
  406. if (mCurrentDialog != null) {
  407. mCurrentDialog.dismiss();
  408. mCurrentDialog = null;
  409. }
  410. if (hasEnoughSpaceAvailable) {
  411. // return the list of files (success)
  412. Intent data = new Intent();
  413. if (requestCode == FileDisplayActivity.REQUEST_CODE__UPLOAD_FROM_CAMERA) {
  414. data.putExtra(EXTRA_CHOSEN_FILES, new String[]{filesToUpload[0]});
  415. setResult(RESULT_OK_AND_MOVE, data);
  416. preferences.setUploaderBehaviour(FileUploader.LOCAL_BEHAVIOUR_MOVE);
  417. } else {
  418. data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
  419. // set result code
  420. switch (mBehaviourSpinner.getSelectedItemPosition()) {
  421. case 0: // move to nextcloud folder
  422. setResult(RESULT_OK_AND_MOVE, data);
  423. break;
  424. case 1: // only upload
  425. setResult(RESULT_OK_AND_DO_NOTHING, data);
  426. break;
  427. case 2: // upload and delete from source
  428. setResult(RESULT_OK_AND_DELETE, data);
  429. break;
  430. }
  431. // store behaviour
  432. preferences.setUploaderBehaviour(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 = preferences.getUploaderBehaviour();
  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 onStart() {
  583. super.onStart();
  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. if (mFileListFragment == null) {
  599. Log_OC.e(TAG, "Access to unexisting list of files fragment!!");
  600. }
  601. return mFileListFragment;
  602. }
  603. @Override
  604. protected void onStop() {
  605. if (dialog != null) {
  606. dialog.dismissAllowingStateLoss();
  607. }
  608. super.onStop();
  609. }
  610. }