- moved rotateImage() to BitmapUtils
[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.Button;
33 import android.widget.TextView;
34
35 import com.actionbarsherlock.app.ActionBar;
36 import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
37 import com.actionbarsherlock.view.MenuItem;
38 import com.owncloud.android.R;
39 import com.owncloud.android.lib.common.utils.Log_OC;
40 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
41 import com.owncloud.android.ui.dialog.IndeterminateProgressDialog;
42 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
43 import com.owncloud.android.ui.fragment.LocalFileListFragment;
44 import com.owncloud.android.utils.DisplayUtils;
45 import com.owncloud.android.utils.FileStorageUtils;
46
47
48 /**
49 * Displays local files and let the user choose what of them wants to upload
50 * to the current ownCloud account
51 *
52 * @author David A. Velasco
53 *
54 */
55
56 public class UploadFilesActivity extends FileActivity implements
57 LocalFileListFragment.ContainerActivity, OnNavigationListener, OnClickListener, ConfirmationDialogFragmentListener {
58
59 private ArrayAdapter<String> mDirectories;
60 private File mCurrentDir = null;
61 private LocalFileListFragment mFileListFragment;
62 private Button mCancelBtn;
63 private Button mUploadBtn;
64 private Account mAccountOnCreation;
65 private DialogFragment mCurrentDialog;
66
67 public static final String EXTRA_CHOSEN_FILES = UploadFilesActivity.class.getCanonicalName() + ".EXTRA_CHOSEN_FILES";
68
69 public static final int RESULT_OK_AND_MOVE = RESULT_FIRST_USER;
70
71 private static final String KEY_DIRECTORY_PATH = UploadFilesActivity.class.getCanonicalName() + ".KEY_DIRECTORY_PATH";
72 private static final String TAG = "UploadFilesActivity";
73 private static final String WAIT_DIALOG_TAG = "WAIT";
74 private static final String QUERY_TO_MOVE_DIALOG_TAG = "QUERY_TO_MOVE";
75
76
77 @Override
78 public void onCreate(Bundle savedInstanceState) {
79 Log_OC.d(TAG, "onCreate() start");
80 super.onCreate(savedInstanceState);
81
82 if(savedInstanceState != null) {
83 mCurrentDir = new File(savedInstanceState.getString(UploadFilesActivity.KEY_DIRECTORY_PATH));
84 } else {
85 mCurrentDir = Environment.getExternalStorageDirectory();
86 }
87
88 mAccountOnCreation = getAccount();
89
90 /// USER INTERFACE
91
92 // Drop-down navigation
93 mDirectories = new CustomArrayAdapter<String>(this, R.layout.sherlock_spinner_dropdown_item);
94 File currDir = mCurrentDir;
95 while(currDir != null && currDir.getParentFile() != null) {
96 mDirectories.add(currDir.getName());
97 currDir = currDir.getParentFile();
98 }
99 mDirectories.add(File.separator);
100
101 // Inflate and set the layout view
102 setContentView(R.layout.upload_files_layout);
103 mFileListFragment = (LocalFileListFragment) getSupportFragmentManager().findFragmentById(R.id.local_files_list);
104
105
106 // Set input controllers
107 mCancelBtn = (Button) findViewById(R.id.upload_files_btn_cancel);
108 mCancelBtn.setOnClickListener(this);
109 mUploadBtn = (Button) findViewById(R.id.upload_files_btn_upload);
110 mUploadBtn.setOnClickListener(this);
111
112
113 // Action bar setup
114 ActionBar actionBar = getSupportActionBar();
115 actionBar.setIcon(DisplayUtils.getSeasonalIconId());
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_OC.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_OC.d(TAG, "onSaveInstanceState() start");
185 super.onSaveInstanceState(outState);
186 outState.putString(UploadFilesActivity.KEY_DIRECTORY_PATH, mCurrentDir.getAbsolutePath());
187 Log_OC.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(mAccountOnCreation.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 dialog.show(getSupportFragmentManager(), QUERY_TO_MOVE_DIALOG_TAG);
351 }
352 }
353 }
354
355 @Override
356 public void onConfirmation(String callerTag) {
357 Log_OC.d(TAG, "Positive button in dialog was clicked; dialog tag is " + callerTag);
358 if (callerTag.equals(QUERY_TO_MOVE_DIALOG_TAG)) {
359 // return the list of selected files to the caller activity (success), signaling that they should be moved to the ownCloud folder, instead of copied
360 Intent data = new Intent();
361 data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
362 setResult(RESULT_OK_AND_MOVE, data);
363 finish();
364 }
365 }
366
367
368 @Override
369 public void onNeutral(String callerTag) {
370 Log_OC.d(TAG, "Phantom neutral button in dialog was clicked; dialog tag is " + callerTag);
371 }
372
373
374 @Override
375 public void onCancel(String callerTag) {
376 /// nothing to do; don't finish, let the user change the selection
377 Log_OC.d(TAG, "Negative button in dialog was clicked; dialog tag is " + callerTag);
378 }
379
380
381 @Override
382 protected void onAccountSet(boolean stateWasRecovered) {
383 super.onAccountSet(stateWasRecovered);
384 if (getAccount() != null) {
385 if (!mAccountOnCreation.equals(getAccount())) {
386 setResult(RESULT_CANCELED);
387 finish();
388 }
389
390 } else {
391 setResult(RESULT_CANCELED);
392 finish();
393 }
394 }
395
396
397 }