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