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