UploadFilesActivity.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. import static com.owncloud.android.utils.DisplayUtils.openSortingOrderDialogFragment;
  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, Injectable {
  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. ThemeUtils.tintBackButton(actionBar, 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. private void fillDirectoryDropdown() {
  190. File currentDir = mCurrentDir;
  191. while (currentDir != null && currentDir.getParentFile() != null) {
  192. mDirectories.add(currentDir.getName());
  193. currentDir = currentDir.getParentFile();
  194. }
  195. mDirectories.add(File.separator);
  196. }
  197. /**
  198. * Helper to launch the UploadFilesActivity for which you would like a result when it finished.
  199. * Your onActivityResult() method will be called with the given requestCode.
  200. *
  201. * @param activity the activity which should call the upload activity for a result
  202. * @param account the account for which the upload activity is called
  203. * @param requestCode If >= 0, this code will be returned in onActivityResult()
  204. */
  205. public static void startUploadActivityForResult(Activity activity, Account account, int requestCode) {
  206. Intent action = new Intent(activity, UploadFilesActivity.class);
  207. action.putExtra(EXTRA_ACCOUNT, account);
  208. action.putExtra(REQUEST_CODE_KEY, requestCode);
  209. activity.startActivityForResult(action, requestCode);
  210. }
  211. @Override
  212. public boolean onCreateOptionsMenu(Menu menu) {
  213. mOptionsMenu = menu;
  214. getMenuInflater().inflate(R.menu.activity_upload_files, menu);
  215. if(!mLocalFolderPickerMode) {
  216. MenuItem selectAll = menu.findItem(R.id.action_select_all);
  217. setSelectAllMenuItem(selectAll, mSelectAll);
  218. }
  219. MenuItem switchView = menu.findItem(R.id.action_switch_view);
  220. switchView.setTitle(isGridView() ? R.string.action_switch_list_view : R.string.action_switch_grid_view);
  221. int fontColor = ThemeUtils.fontColor(this);
  222. final MenuItem item = menu.findItem(R.id.action_search);
  223. mSearchView = (SearchView) MenuItemCompat.getActionView(item);
  224. EditText editText = mSearchView.findViewById(androidx.appcompat.R.id.search_src_text);
  225. editText.setHintTextColor(fontColor);
  226. editText.setTextColor(fontColor);
  227. ImageView searchClose = mSearchView.findViewById(androidx.appcompat.R.id.search_close_btn);
  228. searchClose.setColorFilter(fontColor);
  229. ThemeUtils.tintDrawable(menu.findItem(R.id.action_choose_storage_path).getIcon(), 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. openSortingOrderDialogFragment(getSupportFragmentManager(),
  251. preferences.getSortOrderByType(FileSortOrder.Type.uploadFilesView));
  252. break;
  253. }
  254. case R.id.action_switch_view: {
  255. if (isGridView()) {
  256. item.setTitle(getString(R.string.action_switch_grid_view));
  257. item.setIcon(R.drawable.ic_view_module);
  258. mFileListFragment.switchToListView();
  259. } else {
  260. item.setTitle(getApplicationContext().getString(R.string.action_switch_list_view));
  261. item.setIcon(R.drawable.ic_view_list);
  262. mFileListFragment.switchToGridView();
  263. }
  264. break;
  265. }
  266. case R.id.action_choose_storage_path: {
  267. showLocalStoragePathPickerDialog();
  268. break;
  269. }
  270. default:
  271. retval = super.onOptionsItemSelected(item);
  272. break;
  273. }
  274. return retval;
  275. }
  276. private void showLocalStoragePathPickerDialog() {
  277. FragmentManager fm = getSupportFragmentManager();
  278. FragmentTransaction ft = fm.beginTransaction();
  279. ft.addToBackStack(null);
  280. dialog = LocalStoragePathPickerDialogFragment.newInstance();
  281. dialog.show(ft, LocalStoragePathPickerDialogFragment.LOCAL_STORAGE_PATH_PICKER_FRAGMENT);
  282. }
  283. @Override
  284. public void onSortingOrderChosen(FileSortOrder selection) {
  285. mFileListFragment.sortFiles(selection);
  286. }
  287. @Override
  288. public boolean onNavigationItemSelected(int itemPosition, long itemId) {
  289. int i = itemPosition;
  290. while (i-- != 0) {
  291. onBackPressed();
  292. }
  293. // the next operation triggers a new call to this method, but it's necessary to
  294. // ensure that the name exposed in the action bar is the current directory when the
  295. // user selected it in the navigation list
  296. if (itemPosition != 0) {
  297. getSupportActionBar().setSelectedNavigationItem(0);
  298. }
  299. return true;
  300. }
  301. private boolean isSearchOpen() {
  302. if (mSearchView == null) {
  303. return false;
  304. } else {
  305. View mSearchEditFrame = mSearchView.findViewById(androidx.appcompat.R.id.search_edit_frame);
  306. return mSearchEditFrame != null && mSearchEditFrame.getVisibility() == View.VISIBLE;
  307. }
  308. }
  309. @Override
  310. public void onBackPressed() {
  311. if (isSearchOpen() && mSearchView != null) {
  312. mSearchView.setQuery("", false);
  313. mFileListFragment.onClose();
  314. mSearchView.onActionViewCollapsed();
  315. setDrawerIndicatorEnabled(isDrawerIndicatorAvailable());
  316. } else {
  317. if (mDirectories.getCount() <= SINGLE_DIR) {
  318. finish();
  319. return;
  320. }
  321. File parentFolder = mCurrentDir.getParentFile();
  322. if (!parentFolder.canRead()) {
  323. showLocalStoragePathPickerDialog();
  324. return;
  325. }
  326. popDirname();
  327. mFileListFragment.onNavigateUp();
  328. mCurrentDir = mFileListFragment.getCurrentDirectory();
  329. checkWritableFolder(mCurrentDir);
  330. if (mCurrentDir.getParentFile() == null) {
  331. ActionBar actionBar = getSupportActionBar();
  332. if (actionBar != null) {
  333. actionBar.setDisplayHomeAsUpEnabled(false);
  334. }
  335. }
  336. // invalidate checked state when navigating directories
  337. if (!mLocalFolderPickerMode) {
  338. setSelectAllMenuItem(mOptionsMenu.findItem(R.id.action_select_all), false);
  339. }
  340. }
  341. }
  342. @Override
  343. protected void onSaveInstanceState(@NonNull Bundle outState) {
  344. // responsibility of restore is preferred in onCreate() before than in
  345. // onRestoreInstanceState when there are Fragments involved
  346. Log_OC.d(TAG, "onSaveInstanceState() start");
  347. super.onSaveInstanceState(outState);
  348. outState.putString(UploadFilesActivity.KEY_DIRECTORY_PATH, mCurrentDir.getAbsolutePath());
  349. if (mOptionsMenu != null && mOptionsMenu.findItem(R.id.action_select_all) != null) {
  350. outState.putBoolean(UploadFilesActivity.KEY_ALL_SELECTED,
  351. mOptionsMenu.findItem(R.id.action_select_all).isChecked());
  352. } else {
  353. outState.putBoolean(UploadFilesActivity.KEY_ALL_SELECTED, false);
  354. }
  355. Log_OC.d(TAG, "onSaveInstanceState() end");
  356. }
  357. /**
  358. * Pushes a directory to the drop down list
  359. * @param directory to push
  360. * @throws IllegalArgumentException If the {@link File#isDirectory()} returns false.
  361. */
  362. public void pushDirname(File directory) {
  363. if(!directory.isDirectory()){
  364. throw new IllegalArgumentException("Only directories may be pushed!");
  365. }
  366. mDirectories.insert(directory.getName(), 0);
  367. mCurrentDir = directory;
  368. checkWritableFolder(mCurrentDir);
  369. }
  370. /**
  371. * Pops a directory name from the drop down list
  372. * @return True, unless the stack is empty
  373. */
  374. public boolean popDirname() {
  375. mDirectories.remove(mDirectories.getItem(0));
  376. return !mDirectories.isEmpty();
  377. }
  378. private void setSelectAllMenuItem(MenuItem selectAll, boolean checked) {
  379. selectAll.setChecked(checked);
  380. if(checked) {
  381. selectAll.setIcon(R.drawable.ic_select_none);
  382. } else {
  383. selectAll.setIcon(ThemeUtils.tintDrawable(R.drawable.ic_select_all, ThemeUtils.primaryColor(this)));
  384. }
  385. }
  386. @Override
  387. public void onCheckAvailableSpaceStart() {
  388. if (requestCode == FileDisplayActivity.REQUEST_CODE__SELECT_FILES_FROM_FILE_SYSTEM) {
  389. mCurrentDialog = IndeterminateProgressDialog.newInstance(R.string.wait_a_moment, false);
  390. mCurrentDialog.show(getSupportFragmentManager(), WAIT_DIALOG_TAG);
  391. }
  392. }
  393. /**
  394. * Updates the activity UI after the check of space is done. If there is not space enough. shows a new dialog to
  395. * query the user if wants to move the files instead of copy them.
  396. *
  397. * @param hasEnoughSpaceAvailable 'True' when there is space enough to copy all the selected files.
  398. */
  399. @Override
  400. public void onCheckAvailableSpaceFinish(boolean hasEnoughSpaceAvailable, String... filesToUpload) {
  401. if (mCurrentDialog != null) {
  402. mCurrentDialog.dismiss();
  403. mCurrentDialog = null;
  404. }
  405. if (hasEnoughSpaceAvailable) {
  406. // return the list of files (success)
  407. Intent data = new Intent();
  408. if (requestCode == FileDisplayActivity.REQUEST_CODE__UPLOAD_FROM_CAMERA) {
  409. data.putExtra(EXTRA_CHOSEN_FILES, new String[]{filesToUpload[0]});
  410. setResult(RESULT_OK_AND_MOVE, data);
  411. preferences.setUploaderBehaviour(FileUploader.LOCAL_BEHAVIOUR_MOVE);
  412. } else {
  413. data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
  414. // set result code
  415. switch (mBehaviourSpinner.getSelectedItemPosition()) {
  416. case 0: // move to nextcloud folder
  417. setResult(RESULT_OK_AND_MOVE, data);
  418. break;
  419. case 1: // only upload
  420. setResult(RESULT_OK_AND_DO_NOTHING, data);
  421. break;
  422. case 2: // upload and delete from source
  423. setResult(RESULT_OK_AND_DELETE, data);
  424. break;
  425. }
  426. // store behaviour
  427. preferences.setUploaderBehaviour(mBehaviourSpinner.getSelectedItemPosition());
  428. }
  429. finish();
  430. } else {
  431. // show a dialog to query the user if wants to move the selected files
  432. // to the ownCloud folder instead of copying
  433. String[] args = {getString(R.string.app_name)};
  434. ConfirmationDialogFragment dialog = ConfirmationDialogFragment.newInstance(
  435. R.string.upload_query_move_foreign_files, args, 0, R.string.common_yes, -1,
  436. R.string.common_no
  437. );
  438. dialog.setOnConfirmationListener(this);
  439. dialog.show(getSupportFragmentManager(), QUERY_TO_MOVE_DIALOG_TAG);
  440. }
  441. }
  442. @Override
  443. public void chosenPath(String path) {
  444. if (getListOfFilesFragment() instanceof LocalFileListFragment) {
  445. File file = new File(path);
  446. ((LocalFileListFragment) getListOfFilesFragment()).listDirectory(file);
  447. onDirectoryClick(file);
  448. mCurrentDir = new File(path);
  449. mDirectories.clear();
  450. fillDirectoryDropdown();
  451. }
  452. }
  453. /**
  454. * Custom array adapter to override text colors
  455. */
  456. private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
  457. public CustomArrayAdapter(UploadFilesActivity ctx, int view) {
  458. super(ctx, view);
  459. }
  460. @SuppressLint("RestrictedApi")
  461. public @NonNull View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
  462. View v = super.getView(position, convertView, parent);
  463. int color = ThemeUtils.fontColor(getContext());
  464. ColorStateList colorStateList = ColorStateList.valueOf(color);
  465. ((AppCompatSpinner) parent).setSupportBackgroundTintList(colorStateList);
  466. ((TextView) v).setTextColor(colorStateList);
  467. return v;
  468. }
  469. public View getDropDownView(int position, View convertView, @NonNull ViewGroup parent) {
  470. View v = super.getDropDownView(position, convertView, parent);
  471. ((TextView) v).setTextColor(getResources().getColorStateList(
  472. android.R.color.white));
  473. return v;
  474. }
  475. }
  476. /**
  477. * {@inheritDoc}
  478. */
  479. @Override
  480. public void onDirectoryClick(File directory) {
  481. if(!mLocalFolderPickerMode) {
  482. // invalidate checked state when navigating directories
  483. MenuItem selectAll = mOptionsMenu.findItem(R.id.action_select_all);
  484. setSelectAllMenuItem(selectAll, false);
  485. }
  486. pushDirname(directory);
  487. ActionBar actionBar = getSupportActionBar();
  488. actionBar.setDisplayHomeAsUpEnabled(true);
  489. }
  490. private void checkWritableFolder(File folder) {
  491. boolean canWriteIntoFolder = folder.canWrite();
  492. mBehaviourSpinner.setEnabled(canWriteIntoFolder);
  493. TextView textView = findViewById(R.id.upload_files_upload_files_behaviour_text);
  494. if (canWriteIntoFolder) {
  495. textView.setText(getString(R.string.uploader_upload_files_behaviour));
  496. int localBehaviour = preferences.getUploaderBehaviour();
  497. mBehaviourSpinner.setSelection(localBehaviour);
  498. } else {
  499. mBehaviourSpinner.setSelection(1);
  500. textView.setText(new StringBuilder().append(getString(R.string.uploader_upload_files_behaviour))
  501. .append(" ")
  502. .append(getString(R.string.uploader_upload_files_behaviour_not_writable))
  503. .toString());
  504. }
  505. }
  506. /**
  507. * {@inheritDoc}
  508. */
  509. @Override
  510. public void onFileClick(File file) {
  511. // nothing to do
  512. }
  513. /**
  514. * {@inheritDoc}
  515. */
  516. @Override
  517. public File getInitialDirectory() {
  518. return mCurrentDir;
  519. }
  520. /**
  521. * {@inheritDoc}
  522. */
  523. @Override
  524. public boolean isFolderPickerMode() {
  525. return mLocalFolderPickerMode;
  526. }
  527. /**
  528. * Performs corresponding action when user presses 'Cancel' or 'Upload' button
  529. *
  530. * TODO Make here the real request to the Upload service ; will require to receive the account and
  531. * target folder where the upload must be done in the received intent.
  532. */
  533. @Override
  534. public void onClick(View v) {
  535. if (v.getId() == R.id.upload_files_btn_cancel) {
  536. setResult(RESULT_CANCELED);
  537. finish();
  538. } else if (v.getId() == R.id.upload_files_btn_upload) {
  539. if(mCurrentDir != null) {
  540. preferences.setUploadFromLocalLastPath(mCurrentDir.getAbsolutePath());
  541. }
  542. if (mLocalFolderPickerMode) {
  543. Intent data = new Intent();
  544. if (mCurrentDir != null) {
  545. data.putExtra(EXTRA_CHOSEN_FILES, mCurrentDir.getAbsolutePath());
  546. }
  547. setResult(RESULT_OK, data);
  548. finish();
  549. } else {
  550. new CheckAvailableSpaceTask(this, mFileListFragment.getCheckedFilePaths())
  551. .execute(mBehaviourSpinner.getSelectedItemPosition() == 0);
  552. }
  553. }
  554. }
  555. @Override
  556. public void onConfirmation(String callerTag) {
  557. Log_OC.d(TAG, "Positive button in dialog was clicked; dialog tag is " + callerTag);
  558. if (QUERY_TO_MOVE_DIALOG_TAG.equals(callerTag)) {
  559. // return the list of selected files to the caller activity (success),
  560. // signaling that they should be moved to the ownCloud folder, instead of copied
  561. Intent data = new Intent();
  562. data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
  563. setResult(RESULT_OK_AND_MOVE, data);
  564. finish();
  565. }
  566. }
  567. @Override
  568. public void onNeutral(String callerTag) {
  569. Log_OC.d(TAG, "Phantom neutral button in dialog was clicked; dialog tag is " + callerTag);
  570. }
  571. @Override
  572. public void onCancel(String callerTag) {
  573. /// nothing to do; don't finish, let the user change the selection
  574. Log_OC.d(TAG, "Negative button in dialog was clicked; dialog tag is " + callerTag);
  575. }
  576. @Override
  577. protected void onStart() {
  578. super.onStart();
  579. if (getAccount() != null) {
  580. if (!mAccountOnCreation.equals(getAccount())) {
  581. setResult(RESULT_CANCELED);
  582. finish();
  583. }
  584. } else {
  585. setResult(RESULT_CANCELED);
  586. finish();
  587. }
  588. }
  589. private boolean isGridView() {
  590. return getListOfFilesFragment().isGridEnabled();
  591. }
  592. private ExtendedListFragment getListOfFilesFragment() {
  593. if (mFileListFragment == null) {
  594. Log_OC.e(TAG, "Access to unexisting list of files fragment!!");
  595. }
  596. return mFileListFragment;
  597. }
  598. @Override
  599. protected void onStop() {
  600. if (dialog != null) {
  601. dialog.dismissAllowingStateLoss();
  602. }
  603. super.onStop();
  604. }
  605. }