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