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