UploadFilesActivity.java 27 KB

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