UploadFilesActivity.java 14 KB

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