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