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