UploadFilesActivity.java 27 KB

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