select all / deselect all file in upload menu
[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.ui.dialog.ConfirmationDialogFragment;
43 import com.owncloud.android.ui.dialog.IndeterminateProgressDialog;
44 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
45 import com.owncloud.android.ui.fragment.LocalFileListFragment;
46 import com.owncloud.android.utils.DisplayUtils;
47 import com.owncloud.android.utils.FileStorageUtils;
48 import com.owncloud.android.utils.Log_OC;
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 // deselect all
158 toggleOffSelectAll();
159 }else{
160 // select alls
161 toggleOnSelectAll(item);
162 }
163 break;
164 }
165 default:
166 retval = super.onOptionsItemSelected(item);
167 }
168 return retval;
169 }
170 public void toggleOffSelectAll(MenuItem item){
171 selectAllToggled = false;
172 item.setIcon(android.R.drawable.checkbox_off_background);
173 // mFileListFragment.listDirectory()
174 mFileListFragment.deselectAll();
175 }
176 public void toggleOffSelectAll(){
177 MenuItem item = menu.findItem(R.id.actionbar_select_all);
178 toggleOffSelectAll(item);
179 }
180 public void toggleOnSelectAll(MenuItem item){
181 selectAllToggled = true;
182 item.setIcon(android.R.drawable.checkbox_on_background);
183
184 mFileListFragment.selectAll();
185 }
186
187 @Override
188 public boolean onNavigationItemSelected(int itemPosition, long itemId) {
189 int i = itemPosition;
190 while (i-- != 0) {
191 onBackPressed();
192 }
193 // the next operation triggers a new call to this method, but it's necessary to
194 // ensure that the name exposed in the action bar is the current directory when the
195 // user selected it in the navigation list
196 if (itemPosition != 0)
197 getSupportActionBar().setSelectedNavigationItem(0);
198 return true;
199 }
200
201
202 @Override
203 public void onBackPressed() {
204 if (mDirectories.getCount() <= 1) {
205 finish();
206 return;
207 }
208 popDirname();
209 mFileListFragment.onNavigateUp();
210 mCurrentDir = mFileListFragment.getCurrentDirectory();
211
212 if(mCurrentDir.getParentFile() == null){
213 ActionBar actionBar = getSupportActionBar();
214 actionBar.setDisplayHomeAsUpEnabled(false);
215 }
216 toggleOffSelectAll();
217 }
218
219
220 @Override
221 protected void onSaveInstanceState(Bundle outState) {
222 // responsibility of restore is preferred in onCreate() before than in onRestoreInstanceState when there are Fragments involved
223 Log_OC.d(TAG, "onSaveInstanceState() start");
224 super.onSaveInstanceState(outState);
225 outState.putString(UploadFilesActivity.KEY_DIRECTORY_PATH, mCurrentDir.getAbsolutePath());
226 Log_OC.d(TAG, "onSaveInstanceState() end");
227 }
228
229
230 /**
231 * Pushes a directory to the drop down list
232 * @param directory to push
233 * @throws IllegalArgumentException If the {@link File#isDirectory()} returns false.
234 */
235 public void pushDirname(File directory) {
236 if(!directory.isDirectory()){
237 throw new IllegalArgumentException("Only directories may be pushed!");
238 }
239 mDirectories.insert(directory.getName(), 0);
240 mCurrentDir = directory;
241 }
242
243 /**
244 * Pops a directory name from the drop down list
245 * @return True, unless the stack is empty
246 */
247 public boolean popDirname() {
248 mDirectories.remove(mDirectories.getItem(0));
249 return !mDirectories.isEmpty();
250 }
251
252
253 // Custom array adapter to override text colors
254 private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
255
256 public CustomArrayAdapter(UploadFilesActivity ctx, int view) {
257 super(ctx, view);
258 }
259
260 public View getView(int position, View convertView, ViewGroup parent) {
261 View v = super.getView(position, convertView, parent);
262
263 ((TextView) v).setTextColor(getResources().getColorStateList(
264 android.R.color.white));
265 return v;
266 }
267
268 public View getDropDownView(int position, View convertView,
269 ViewGroup parent) {
270 View v = super.getDropDownView(position, convertView, parent);
271
272 ((TextView) v).setTextColor(getResources().getColorStateList(
273 android.R.color.white));
274
275 return v;
276 }
277
278 }
279
280 /**
281 * {@inheritDoc}
282 */
283 @Override
284 public void onDirectoryClick(File directory) {
285 pushDirname(directory);
286 ActionBar actionBar = getSupportActionBar();
287 actionBar.setDisplayHomeAsUpEnabled(true);
288 toggleOffSelectAll();
289 }
290
291
292 /**
293 * {@inheritDoc}
294 */
295 @Override
296 public void onFileClick(File file) {
297 // nothing to do
298 }
299
300 /**
301 * {@inheritDoc}
302 */
303 @Override
304 public File getInitialDirectory() {
305 return mCurrentDir;
306 }
307
308
309 /**
310 * Performs corresponding action when user presses 'Cancel' or 'Upload' button
311 *
312 * TODO Make here the real request to the Upload service ; will require to receive the account and
313 * target folder where the upload must be done in the received intent.
314 */
315 @Override
316 public void onClick(View v) {
317 if (v.getId() == R.id.upload_files_btn_cancel) {
318 setResult(RESULT_CANCELED);
319 finish();
320
321 } else if (v.getId() == R.id.upload_files_btn_upload) {
322 new CheckAvailableSpaceTask().execute();
323 }
324 }
325
326
327 /**
328 * Asynchronous task checking if there is space enough to copy all the files chosen
329 * to upload into the ownCloud local folder.
330 *
331 * Maybe an AsyncTask is not strictly necessary, but who really knows.
332 *
333 * @author David A. Velasco
334 */
335 private class CheckAvailableSpaceTask extends AsyncTask<Void, Void, Boolean> {
336
337 /**
338 * Updates the UI before trying the movement
339 */
340 @Override
341 protected void onPreExecute () {
342 /// progress dialog and disable 'Move' button
343 mCurrentDialog = IndeterminateProgressDialog.newInstance(R.string.wait_a_moment, false);
344 mCurrentDialog.show(getSupportFragmentManager(), WAIT_DIALOG_TAG);
345 }
346
347
348 /**
349 * Checks the available space
350 *
351 * @return 'True' if there is space enough.
352 */
353 @Override
354 protected Boolean doInBackground(Void... params) {
355 String[] checkedFilePaths = mFileListFragment.getCheckedFilePaths();
356 long total = 0;
357 for (int i=0; checkedFilePaths != null && i < checkedFilePaths.length ; i++) {
358 String localPath = checkedFilePaths[i];
359 File localFile = new File(localPath);
360 total += localFile.length();
361 }
362 return (FileStorageUtils.getUsableSpace(mAccountOnCreation.name) >= total);
363 }
364
365 /**
366 * Updates the activity UI after the check of space is done.
367 *
368 * If there is not space enough. shows a new dialog to query the user if wants to move the files instead
369 * of copy them.
370 *
371 * @param result 'True' when there is space enough to copy all the selected files.
372 */
373 @Override
374 protected void onPostExecute(Boolean result) {
375 mCurrentDialog.dismiss();
376 mCurrentDialog = null;
377
378 if (result) {
379 // return the list of selected files (success)
380 Intent data = new Intent();
381 data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
382 setResult(RESULT_OK, data);
383 finish();
384
385 } else {
386 // show a dialog to query the user if wants to move the selected files to the ownCloud folder instead of copying
387 String[] args = {getString(R.string.app_name)};
388 ConfirmationDialogFragment dialog = ConfirmationDialogFragment.newInstance(R.string.upload_query_move_foreign_files, args, R.string.common_yes, -1, R.string.common_no);
389 dialog.setOnConfirmationListener(UploadFilesActivity.this);
390 dialog.show(getSupportFragmentManager(), QUERY_TO_MOVE_DIALOG_TAG);
391 }
392 }
393 }
394
395 @Override
396 public void onConfirmation(String callerTag) {
397 Log_OC.d(TAG, "Positive button in dialog was clicked; dialog tag is " + callerTag);
398 if (callerTag.equals(QUERY_TO_MOVE_DIALOG_TAG)) {
399 // return the list of selected files to the caller activity (success), signaling that they should be moved to the ownCloud folder, instead of copied
400 Intent data = new Intent();
401 data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
402 setResult(RESULT_OK_AND_MOVE, data);
403 finish();
404 }
405 }
406
407
408 @Override
409 public void onNeutral(String callerTag) {
410 Log_OC.d(TAG, "Phantom neutral button in dialog was clicked; dialog tag is " + callerTag);
411 }
412
413
414 @Override
415 public void onCancel(String callerTag) {
416 /// nothing to do; don't finish, let the user change the selection
417 Log_OC.d(TAG, "Negative button in dialog was clicked; dialog tag is " + callerTag);
418 }
419
420
421 @Override
422 protected void onAccountSet(boolean stateWasRecovered) {
423 super.onAccountSet(stateWasRecovered);
424 if (getAccount() != null) {
425 if (!mAccountOnCreation.equals(getAccount())) {
426 setResult(RESULT_CANCELED);
427 finish();
428 }
429
430 } else {
431 setResult(RESULT_CANCELED);
432 finish();
433 }
434 }
435
436
437 }