UploadFilesActivity.java 14 KB

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