UploadFilesActivity.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-2013 ownCloud Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. package com.owncloud.android.ui.activity;
  18. import java.io.File;
  19. import android.accounts.Account;
  20. import android.content.Intent;
  21. import android.os.AsyncTask;
  22. import android.os.Bundle;
  23. import android.os.Environment;
  24. import android.support.v4.app.DialogFragment;
  25. import android.view.View;
  26. import android.view.View.OnClickListener;
  27. import android.view.ViewGroup;
  28. import android.widget.ArrayAdapter;
  29. import android.widget.Button;
  30. import android.widget.TextView;
  31. import com.actionbarsherlock.app.ActionBar;
  32. import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
  33. import com.actionbarsherlock.view.MenuItem;
  34. import com.owncloud.android.R;
  35. import com.owncloud.android.ui.dialog.IndeterminateProgressDialog;
  36. import com.owncloud.android.ui.fragment.ConfirmationDialogFragment;
  37. import com.owncloud.android.ui.fragment.LocalFileListFragment;
  38. import com.owncloud.android.ui.fragment.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
  39. import com.owncloud.android.utils.DisplayUtils;
  40. import com.owncloud.android.utils.FileStorageUtils;
  41. import com.owncloud.android.utils.Log_OC;
  42. /**
  43. * Displays local files and let the user choose what of them wants to upload
  44. * to the current ownCloud account
  45. *
  46. * @author David A. Velasco
  47. *
  48. */
  49. public class UploadFilesActivity extends FileActivity implements
  50. LocalFileListFragment.ContainerActivity, OnNavigationListener, OnClickListener, ConfirmationDialogFragmentListener {
  51. private ArrayAdapter<String> mDirectories;
  52. private File mCurrentDir = null;
  53. private LocalFileListFragment mFileListFragment;
  54. private Button mCancelBtn;
  55. private Button mUploadBtn;
  56. private Account mAccountOnCreation;
  57. private DialogFragment mCurrentDialog;
  58. public static final String EXTRA_CHOSEN_FILES = UploadFilesActivity.class.getCanonicalName() + ".EXTRA_CHOSEN_FILES";
  59. public static final int RESULT_OK_AND_MOVE = RESULT_FIRST_USER;
  60. private static final String KEY_DIRECTORY_PATH = UploadFilesActivity.class.getCanonicalName() + ".KEY_DIRECTORY_PATH";
  61. private static final String TAG = "UploadFilesActivity";
  62. private static final String WAIT_DIALOG_TAG = "WAIT";
  63. private static final String QUERY_TO_MOVE_DIALOG_TAG = "QUERY_TO_MOVE";
  64. @Override
  65. public void onCreate(Bundle savedInstanceState) {
  66. Log_OC.d(TAG, "onCreate() start");
  67. super.onCreate(savedInstanceState);
  68. if(savedInstanceState != null) {
  69. mCurrentDir = new File(savedInstanceState.getString(UploadFilesActivity.KEY_DIRECTORY_PATH));
  70. } else {
  71. mCurrentDir = Environment.getExternalStorageDirectory();
  72. }
  73. mAccountOnCreation = getAccount();
  74. /// USER INTERFACE
  75. // Drop-down navigation
  76. mDirectories = new CustomArrayAdapter<String>(this, R.layout.sherlock_spinner_dropdown_item);
  77. File currDir = mCurrentDir;
  78. while(currDir != null && currDir.getParentFile() != null) {
  79. mDirectories.add(currDir.getName());
  80. currDir = currDir.getParentFile();
  81. }
  82. mDirectories.add(File.separator);
  83. // Inflate and set the layout view
  84. setContentView(R.layout.upload_files_layout);
  85. mFileListFragment = (LocalFileListFragment) getSupportFragmentManager().findFragmentById(R.id.local_files_list);
  86. // Set input controllers
  87. mCancelBtn = (Button) findViewById(R.id.upload_files_btn_cancel);
  88. mCancelBtn.setOnClickListener(this);
  89. mUploadBtn = (Button) findViewById(R.id.upload_files_btn_upload);
  90. mUploadBtn.setOnClickListener(this);
  91. // Action bar setup
  92. ActionBar actionBar = getSupportActionBar();
  93. actionBar.setIcon(DisplayUtils.getSeasonalIconId());
  94. actionBar.setHomeButtonEnabled(true); // mandatory since Android ICS, according to the official documentation
  95. actionBar.setDisplayHomeAsUpEnabled(mCurrentDir != null && mCurrentDir.getName() != null);
  96. actionBar.setDisplayShowTitleEnabled(false);
  97. actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
  98. actionBar.setListNavigationCallbacks(mDirectories, this);
  99. // wait dialog
  100. if (mCurrentDialog != null) {
  101. mCurrentDialog.dismiss();
  102. mCurrentDialog = null;
  103. }
  104. Log_OC.d(TAG, "onCreate() end");
  105. }
  106. @Override
  107. public boolean onOptionsItemSelected(MenuItem item) {
  108. boolean retval = true;
  109. switch (item.getItemId()) {
  110. case android.R.id.home: {
  111. if(mCurrentDir != null && mCurrentDir.getParentFile() != null){
  112. onBackPressed();
  113. }
  114. break;
  115. }
  116. default:
  117. retval = super.onOptionsItemSelected(item);
  118. }
  119. return retval;
  120. }
  121. @Override
  122. public boolean onNavigationItemSelected(int itemPosition, long itemId) {
  123. int i = itemPosition;
  124. while (i-- != 0) {
  125. onBackPressed();
  126. }
  127. // the next operation triggers a new call to this method, but it's necessary to
  128. // ensure that the name exposed in the action bar is the current directory when the
  129. // user selected it in the navigation list
  130. if (itemPosition != 0)
  131. getSupportActionBar().setSelectedNavigationItem(0);
  132. return true;
  133. }
  134. @Override
  135. public void onBackPressed() {
  136. if (mDirectories.getCount() <= 1) {
  137. finish();
  138. return;
  139. }
  140. popDirname();
  141. mFileListFragment.onNavigateUp();
  142. mCurrentDir = mFileListFragment.getCurrentDirectory();
  143. if(mCurrentDir.getParentFile() == null){
  144. ActionBar actionBar = getSupportActionBar();
  145. actionBar.setDisplayHomeAsUpEnabled(false);
  146. }
  147. }
  148. @Override
  149. protected void onSaveInstanceState(Bundle outState) {
  150. // responsibility of restore is preferred in onCreate() before than in onRestoreInstanceState when there are Fragments involved
  151. Log_OC.d(TAG, "onSaveInstanceState() start");
  152. super.onSaveInstanceState(outState);
  153. outState.putString(UploadFilesActivity.KEY_DIRECTORY_PATH, mCurrentDir.getAbsolutePath());
  154. Log_OC.d(TAG, "onSaveInstanceState() end");
  155. }
  156. /**
  157. * Pushes a directory to the drop down list
  158. * @param directory to push
  159. * @throws IllegalArgumentException If the {@link File#isDirectory()} returns false.
  160. */
  161. public void pushDirname(File directory) {
  162. if(!directory.isDirectory()){
  163. throw new IllegalArgumentException("Only directories may be pushed!");
  164. }
  165. mDirectories.insert(directory.getName(), 0);
  166. mCurrentDir = directory;
  167. }
  168. /**
  169. * Pops a directory name from the drop down list
  170. * @return True, unless the stack is empty
  171. */
  172. public boolean popDirname() {
  173. mDirectories.remove(mDirectories.getItem(0));
  174. return !mDirectories.isEmpty();
  175. }
  176. // Custom array adapter to override text colors
  177. private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
  178. public CustomArrayAdapter(UploadFilesActivity ctx, int view) {
  179. super(ctx, view);
  180. }
  181. public View getView(int position, View convertView, ViewGroup parent) {
  182. View v = super.getView(position, convertView, parent);
  183. ((TextView) v).setTextColor(getResources().getColorStateList(
  184. android.R.color.white));
  185. return v;
  186. }
  187. public View getDropDownView(int position, View convertView,
  188. ViewGroup parent) {
  189. View v = super.getDropDownView(position, convertView, parent);
  190. ((TextView) v).setTextColor(getResources().getColorStateList(
  191. android.R.color.white));
  192. return v;
  193. }
  194. }
  195. /**
  196. * {@inheritDoc}
  197. */
  198. @Override
  199. public void onDirectoryClick(File directory) {
  200. pushDirname(directory);
  201. ActionBar actionBar = getSupportActionBar();
  202. actionBar.setDisplayHomeAsUpEnabled(true);
  203. }
  204. /**
  205. * {@inheritDoc}
  206. */
  207. @Override
  208. public void onFileClick(File file) {
  209. // nothing to do
  210. }
  211. /**
  212. * {@inheritDoc}
  213. */
  214. @Override
  215. public File getInitialDirectory() {
  216. return mCurrentDir;
  217. }
  218. /**
  219. * Performs corresponding action when user presses 'Cancel' or 'Upload' button
  220. *
  221. * TODO Make here the real request to the Upload service ; will require to receive the account and
  222. * target folder where the upload must be done in the received intent.
  223. */
  224. @Override
  225. public void onClick(View v) {
  226. if (v.getId() == R.id.upload_files_btn_cancel) {
  227. setResult(RESULT_CANCELED);
  228. finish();
  229. } else if (v.getId() == R.id.upload_files_btn_upload) {
  230. new CheckAvailableSpaceTask().execute();
  231. }
  232. }
  233. /**
  234. * Asynchronous task checking if there is space enough to copy all the files chosen
  235. * to upload into the ownCloud local folder.
  236. *
  237. * Maybe an AsyncTask is not strictly necessary, but who really knows.
  238. *
  239. * @author David A. Velasco
  240. */
  241. private class CheckAvailableSpaceTask extends AsyncTask<Void, Void, Boolean> {
  242. /**
  243. * Updates the UI before trying the movement
  244. */
  245. @Override
  246. protected void onPreExecute () {
  247. /// progress dialog and disable 'Move' button
  248. mCurrentDialog = IndeterminateProgressDialog.newInstance(R.string.wait_a_moment, false);
  249. mCurrentDialog.show(getSupportFragmentManager(), WAIT_DIALOG_TAG);
  250. }
  251. /**
  252. * Checks the available space
  253. *
  254. * @return 'True' if there is space enough.
  255. */
  256. @Override
  257. protected Boolean doInBackground(Void... params) {
  258. String[] checkedFilePaths = mFileListFragment.getCheckedFilePaths();
  259. long total = 0;
  260. for (int i=0; checkedFilePaths != null && i < checkedFilePaths.length ; i++) {
  261. String localPath = checkedFilePaths[i];
  262. File localFile = new File(localPath);
  263. total += localFile.length();
  264. }
  265. return (FileStorageUtils.getUsableSpace(mAccountOnCreation.name) >= total);
  266. }
  267. /**
  268. * Updates the activity UI after the check of space is done.
  269. *
  270. * If there is not space enough. shows a new dialog to query the user if wants to move the files instead
  271. * of copy them.
  272. *
  273. * @param result 'True' when there is space enough to copy all the selected files.
  274. */
  275. @Override
  276. protected void onPostExecute(Boolean result) {
  277. mCurrentDialog.dismiss();
  278. mCurrentDialog = null;
  279. if (result) {
  280. // return the list of selected files (success)
  281. Intent data = new Intent();
  282. data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
  283. setResult(RESULT_OK, data);
  284. finish();
  285. } else {
  286. // show a dialog to query the user if wants to move the selected files to the ownCloud folder instead of copying
  287. String[] args = {getString(R.string.app_name)};
  288. ConfirmationDialogFragment dialog = ConfirmationDialogFragment.newInstance(R.string.upload_query_move_foreign_files, args, R.string.common_yes, -1, R.string.common_no);
  289. dialog.setOnConfirmationListener(UploadFilesActivity.this);
  290. dialog.show(getSupportFragmentManager(), QUERY_TO_MOVE_DIALOG_TAG);
  291. }
  292. }
  293. }
  294. @Override
  295. public void onConfirmation(String callerTag) {
  296. Log_OC.d(TAG, "Positive button in dialog was clicked; dialog tag is " + callerTag);
  297. if (callerTag.equals(QUERY_TO_MOVE_DIALOG_TAG)) {
  298. // return the list of selected files to the caller activity (success), signaling that they should be moved to the ownCloud folder, instead of copied
  299. Intent data = new Intent();
  300. data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
  301. setResult(RESULT_OK_AND_MOVE, data);
  302. finish();
  303. }
  304. }
  305. @Override
  306. public void onNeutral(String callerTag) {
  307. Log_OC.d(TAG, "Phantom neutral button in dialog was clicked; dialog tag is " + callerTag);
  308. }
  309. @Override
  310. public void onCancel(String callerTag) {
  311. /// nothing to do; don't finish, let the user change the selection
  312. Log_OC.d(TAG, "Negative button in dialog was clicked; dialog tag is " + callerTag);
  313. }
  314. @Override
  315. protected void onAccountSet(boolean stateWasRecovered) {
  316. super.onAccountSet(stateWasRecovered);
  317. if (getAccount() != null) {
  318. if (!mAccountOnCreation.equals(getAccount())) {
  319. setResult(RESULT_CANCELED);
  320. finish();
  321. }
  322. } else {
  323. setResult(RESULT_CANCELED);
  324. finish();
  325. }
  326. }
  327. }