UploadFilesActivity.java 27 KB

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