Merged PR 1187
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / UploadFilesActivity.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 package com.owncloud.android.ui.activity;
22
23 import android.accounts.Account;
24 import android.content.Intent;
25 import android.os.AsyncTask;
26 import android.os.Bundle;
27 import android.os.Environment;
28 import android.support.v4.app.DialogFragment;
29 import android.support.v7.app.ActionBar;
30 import android.view.MenuItem;
31 import android.view.View;
32 import android.view.View.OnClickListener;
33 import android.view.ViewGroup;
34 import android.widget.ArrayAdapter;
35 import android.widget.Button;
36 import android.widget.TextView;
37
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.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
42 import com.owncloud.android.ui.dialog.IndeterminateProgressDialog;
43 import com.owncloud.android.ui.fragment.LocalFileListFragment;
44 import com.owncloud.android.utils.FileStorageUtils;
45
46 import java.io.File;
47
48
49 /**
50 * Displays local files and let the user choose what of them wants to upload
51 * to the current ownCloud account
52 */
53
54 public class UploadFilesActivity extends FileActivity implements
55 LocalFileListFragment.ContainerActivity, ActionBar.OnNavigationListener,
56 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 =
67 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 =
72 UploadFilesActivity.class.getCanonicalName() + ".KEY_DIRECTORY_PATH";
73 private static final String TAG = "UploadFilesActivity";
74 private static final String WAIT_DIALOG_TAG = "WAIT";
75 private static final String QUERY_TO_MOVE_DIALOG_TAG = "QUERY_TO_MOVE";
76
77
78 @Override
79 public void onCreate(Bundle savedInstanceState) {
80 Log_OC.d(TAG, "onCreate() start");
81 super.onCreate(savedInstanceState);
82
83 if(savedInstanceState != null) {
84 mCurrentDir = new File(savedInstanceState.getString(
85 UploadFilesActivity.KEY_DIRECTORY_PATH));
86 } else {
87 mCurrentDir = Environment.getExternalStorageDirectory();
88 }
89
90 mAccountOnCreation = getAccount();
91
92 /// USER INTERFACE
93
94 // Drop-down navigation
95 mDirectories = new CustomArrayAdapter<String>(this,
96 R.layout.support_simple_spinner_dropdown_item);
97 File currDir = mCurrentDir;
98 while(currDir != null && currDir.getParentFile() != null) {
99 mDirectories.add(currDir.getName());
100 currDir = currDir.getParentFile();
101 }
102 mDirectories.add(File.separator);
103
104 // Inflate and set the layout view
105 setContentView(R.layout.upload_files_layout);
106 mFileListFragment = (LocalFileListFragment)
107 getSupportFragmentManager().findFragmentById(R.id.local_files_list);
108
109
110 // Set input controllers
111 mCancelBtn = (Button) findViewById(R.id.upload_files_btn_cancel);
112 mCancelBtn.setOnClickListener(this);
113 mUploadBtn = (Button) findViewById(R.id.upload_files_btn_upload);
114 mUploadBtn.setOnClickListener(this);
115
116
117 // Action bar setup
118 ActionBar actionBar = getSupportActionBar();
119 actionBar.setHomeButtonEnabled(true); // mandatory since Android ICS, according to the
120 // 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
188 // onRestoreInstanceState when there are Fragments involved
189 Log_OC.d(TAG, "onSaveInstanceState() start");
190 super.onSaveInstanceState(outState);
191 outState.putString(UploadFilesActivity.KEY_DIRECTORY_PATH, mCurrentDir.getAbsolutePath());
192 Log_OC.d(TAG, "onSaveInstanceState() end");
193 }
194
195
196 /**
197 * Pushes a directory to the drop down list
198 * @param directory to push
199 * @throws IllegalArgumentException If the {@link File#isDirectory()} returns false.
200 */
201 public void pushDirname(File directory) {
202 if(!directory.isDirectory()){
203 throw new IllegalArgumentException("Only directories may be pushed!");
204 }
205 mDirectories.insert(directory.getName(), 0);
206 mCurrentDir = directory;
207 }
208
209 /**
210 * Pops a directory name from the drop down list
211 * @return True, unless the stack is empty
212 */
213 public boolean popDirname() {
214 mDirectories.remove(mDirectories.getItem(0));
215 return !mDirectories.isEmpty();
216 }
217
218
219 // Custom array adapter to override text colors
220 private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
221
222 public CustomArrayAdapter(UploadFilesActivity ctx, int view) {
223 super(ctx, view);
224 }
225
226 public View getView(int position, View convertView, ViewGroup parent) {
227 View v = super.getView(position, convertView, parent);
228
229 ((TextView) v).setTextColor(getResources().getColorStateList(
230 android.R.color.white));
231 return v;
232 }
233
234 public View getDropDownView(int position, View convertView,
235 ViewGroup parent) {
236 View v = super.getDropDownView(position, convertView, parent);
237
238 ((TextView) v).setTextColor(getResources().getColorStateList(
239 android.R.color.white));
240
241 return v;
242 }
243
244 }
245
246 /**
247 * {@inheritDoc}
248 */
249 @Override
250 public void onDirectoryClick(File directory) {
251 pushDirname(directory);
252 ActionBar actionBar = getSupportActionBar();
253 actionBar.setDisplayHomeAsUpEnabled(true);
254 }
255
256
257 /**
258 * {@inheritDoc}
259 */
260 @Override
261 public void onFileClick(File file) {
262 // nothing to do
263 }
264
265 /**
266 * {@inheritDoc}
267 */
268 @Override
269 public File getInitialDirectory() {
270 return mCurrentDir;
271 }
272
273
274 /**
275 * Performs corresponding action when user presses 'Cancel' or 'Upload' button
276 *
277 * TODO Make here the real request to the Upload service ; will require to receive the account and
278 * target folder where the upload must be done in the received intent.
279 */
280 @Override
281 public void onClick(View v) {
282 if (v.getId() == R.id.upload_files_btn_cancel) {
283 setResult(RESULT_CANCELED);
284 finish();
285
286 } else if (v.getId() == R.id.upload_files_btn_upload) {
287 new CheckAvailableSpaceTask().execute();
288 }
289 }
290
291
292 /**
293 * Asynchronous task checking if there is space enough to copy all the files chosen
294 * to upload into the ownCloud local folder.
295 *
296 * Maybe an AsyncTask is not strictly necessary, but who really knows.
297 */
298 private class CheckAvailableSpaceTask extends AsyncTask<Void, Void, Boolean> {
299
300 /**
301 * Updates the UI before trying the movement
302 */
303 @Override
304 protected void onPreExecute () {
305 /// progress dialog and disable 'Move' button
306 mCurrentDialog = IndeterminateProgressDialog.newInstance(R.string.wait_a_moment, false);
307 mCurrentDialog.show(getSupportFragmentManager(), WAIT_DIALOG_TAG);
308 }
309
310
311 /**
312 * Checks the available space
313 *
314 * @return 'True' if there is space enough.
315 */
316 @Override
317 protected Boolean doInBackground(Void... params) {
318 String[] checkedFilePaths = mFileListFragment.getCheckedFilePaths();
319 long total = 0;
320 for (int i=0; checkedFilePaths != null && i < checkedFilePaths.length ; i++) {
321 String localPath = checkedFilePaths[i];
322 File localFile = new File(localPath);
323 total += localFile.length();
324 }
325 return (new Boolean(FileStorageUtils.getUsableSpace(mAccountOnCreation.name) >= total));
326 }
327
328 /**
329 * Updates the activity UI after the check of space is done.
330 *
331 * If there is not space enough. shows a new dialog to query the user if wants to move the files instead
332 * of copy them.
333 *
334 * @param result 'True' when there is space enough to copy all the selected files.
335 */
336 @Override
337 protected void onPostExecute(Boolean result) {
338 mCurrentDialog.dismiss();
339 mCurrentDialog = null;
340
341 if (result) {
342 // return the list of selected files (success)
343 Intent data = new Intent();
344 data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
345 setResult(RESULT_OK, data);
346 finish();
347
348 } else {
349 // show a dialog to query the user if wants to move the selected files
350 // to the ownCloud folder instead of copying
351 String[] args = {getString(R.string.app_name)};
352 ConfirmationDialogFragment dialog = ConfirmationDialogFragment.newInstance(
353 R.string.upload_query_move_foreign_files, args, R.string.common_yes, -1, R.string.common_no
354 );
355 dialog.setOnConfirmationListener(UploadFilesActivity.this);
356 dialog.show(getSupportFragmentManager(), QUERY_TO_MOVE_DIALOG_TAG);
357 }
358 }
359 }
360
361 @Override
362 public void onConfirmation(String callerTag) {
363 Log_OC.d(TAG, "Positive button in dialog was clicked; dialog tag is " + callerTag);
364 if (callerTag.equals(QUERY_TO_MOVE_DIALOG_TAG)) {
365 // return the list of selected files to the caller activity (success),
366 // signaling that they should be moved to the ownCloud folder, instead of copied
367 Intent data = new Intent();
368 data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
369 setResult(RESULT_OK_AND_MOVE, data);
370 finish();
371 }
372 }
373
374
375 @Override
376 public void onNeutral(String callerTag) {
377 Log_OC.d(TAG, "Phantom neutral button in dialog was clicked; dialog tag is " + callerTag);
378 }
379
380
381 @Override
382 public void onCancel(String callerTag) {
383 /// nothing to do; don't finish, let the user change the selection
384 Log_OC.d(TAG, "Negative button in dialog was clicked; dialog tag is " + callerTag);
385 }
386
387
388 @Override
389 protected void onAccountSet(boolean stateWasRecovered) {
390 super.onAccountSet(stateWasRecovered);
391 if (getAccount() != null) {
392 if (!mAccountOnCreation.equals(getAccount())) {
393 setResult(RESULT_CANCELED);
394 finish();
395 }
396
397 } else {
398 setResult(RESULT_CANCELED);
399 finish();
400 }
401 }
402
403
404 }