[tx-robot] updated from transifex
[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 protected LocalFileListFragment mFileListFragment;
61 protected Button mCancelBtn;
62 protected Button mUploadBtn;
63 protected Account mAccountOnCreation;
64 protected 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 public 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(KEY_DIRECTORY_PATH));
85 } else if (getIntent() != null && getIntent().hasExtra(KEY_DIRECTORY_PATH)) {
86 mCurrentDir = new File(getIntent().getStringExtra(KEY_DIRECTORY_PATH));
87 } else {
88 mCurrentDir = Environment.getExternalStorageDirectory();
89 }
90
91 mAccountOnCreation = getAccount();
92
93 /// USER INTERFACE
94
95 // Drop-down navigation
96 mDirectories = new CustomArrayAdapter<String>(this,
97 R.layout.support_simple_spinner_dropdown_item);
98 File currDir = mCurrentDir;
99 while(currDir != null && currDir.getParentFile() != null) {
100 mDirectories.add(currDir.getName());
101 currDir = currDir.getParentFile();
102 }
103 mDirectories.add(File.separator);
104
105 // Inflate and set the layout view
106 setContentView(R.layout.upload_files_layout);
107 mFileListFragment = (LocalFileListFragment)
108 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.setHomeButtonEnabled(true); // mandatory since Android ICS, according to the
121 // 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 // wait dialog
128 if (mCurrentDialog != null) {
129 mCurrentDialog.dismiss();
130 mCurrentDialog = null;
131 }
132
133 Log_OC.d(TAG, "onCreate() end");
134 }
135
136
137 @Override
138 public boolean onOptionsItemSelected(MenuItem item) {
139 boolean retval = true;
140 switch (item.getItemId()) {
141 case android.R.id.home: {
142 if(mCurrentDir != null && mCurrentDir.getParentFile() != null){
143 onBackPressed();
144 }
145 break;
146 }
147 default:
148 retval = super.onOptionsItemSelected(item);
149 }
150 return retval;
151 }
152
153
154 @Override
155 public boolean onNavigationItemSelected(int itemPosition, long itemId) {
156 int i = itemPosition;
157 while (i-- != 0) {
158 onBackPressed();
159 }
160 // the next operation triggers a new call to this method, but it's necessary to
161 // ensure that the name exposed in the action bar is the current directory when the
162 // user selected it in the navigation list
163 if (itemPosition != 0)
164 getSupportActionBar().setSelectedNavigationItem(0);
165 return true;
166 }
167
168
169 @Override
170 public void onBackPressed() {
171 if (mDirectories.getCount() <= 1) {
172 finish();
173 return;
174 }
175 popDirname();
176 mFileListFragment.onNavigateUp();
177 mCurrentDir = mFileListFragment.getCurrentDirectory();
178
179 if(mCurrentDir.getParentFile() == null){
180 ActionBar actionBar = getSupportActionBar();
181 actionBar.setDisplayHomeAsUpEnabled(false);
182 }
183 }
184
185
186 @Override
187 protected void onSaveInstanceState(Bundle outState) {
188 // responsibility of restore is preferred in onCreate() before than in
189 // onRestoreInstanceState when there are Fragments involved
190 Log_OC.d(TAG, "onSaveInstanceState() start");
191 super.onSaveInstanceState(outState);
192 outState.putString(UploadFilesActivity.KEY_DIRECTORY_PATH, mCurrentDir.getAbsolutePath());
193 Log_OC.d(TAG, "onSaveInstanceState() end");
194 }
195
196
197 /**
198 * Pushes a directory to the drop down list
199 * @param directory to push
200 * @throws IllegalArgumentException If the {@link File#isDirectory()} returns false.
201 */
202 public void pushDirname(File directory) {
203 if(!directory.isDirectory()){
204 throw new IllegalArgumentException("Only directories may be pushed!");
205 }
206 mDirectories.insert(directory.getName(), 0);
207 mCurrentDir = directory;
208 }
209
210 /**
211 * Pops a directory name from the drop down list
212 * @return True, unless the stack is empty
213 */
214 public boolean popDirname() {
215 mDirectories.remove(mDirectories.getItem(0));
216 return !mDirectories.isEmpty();
217 }
218
219
220 // Custom array adapter to override text colors
221 private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
222
223 public CustomArrayAdapter(UploadFilesActivity ctx, int view) {
224 super(ctx, view);
225 }
226
227 public View getView(int position, View convertView, ViewGroup parent) {
228 View v = super.getView(position, convertView, parent);
229
230 ((TextView) v).setTextColor(getResources().getColorStateList(
231 android.R.color.white));
232 return v;
233 }
234
235 public View getDropDownView(int position, View convertView,
236 ViewGroup parent) {
237 View v = super.getDropDownView(position, convertView, parent);
238
239 ((TextView) v).setTextColor(getResources().getColorStateList(
240 android.R.color.white));
241
242 return v;
243 }
244
245 }
246
247 /**
248 * {@inheritDoc}
249 */
250 @Override
251 public void onDirectoryClick(File directory) {
252 pushDirname(directory);
253 ActionBar actionBar = getSupportActionBar();
254 actionBar.setDisplayHomeAsUpEnabled(true);
255 }
256
257
258 /**
259 * {@inheritDoc}
260 */
261 @Override
262 public void onFileClick(File file) {
263 // nothing to do
264 }
265
266 /**
267 * {@inheritDoc}
268 */
269 @Override
270 public File getInitialDirectory() {
271 return mCurrentDir;
272 }
273
274
275 /**
276 * Performs corresponding action when user presses 'Cancel' or 'Upload' button
277 *
278 * TODO Make here the real request to the Upload service ; will require to receive the account and
279 * target folder where the upload must be done in the received intent.
280 */
281 @Override
282 public void onClick(View v) {
283 if (v.getId() == R.id.upload_files_btn_cancel) {
284 setResult(RESULT_CANCELED);
285 finish();
286
287 } else if (v.getId() == R.id.upload_files_btn_upload) {
288 new CheckAvailableSpaceTask().execute();
289 }
290 }
291
292
293 /**
294 * Asynchronous task checking if there is space enough to copy all the files chosen
295 * to upload into the ownCloud local folder.
296 *
297 * Maybe an AsyncTask is not strictly necessary, but who really knows.
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 (new Boolean(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
351 // to the ownCloud folder instead of copying
352 String[] args = {getString(R.string.app_name)};
353 ConfirmationDialogFragment dialog = ConfirmationDialogFragment.newInstance(
354 R.string.upload_query_move_foreign_files, args, R.string.common_yes, -1, R.string.common_no
355 );
356 dialog.setOnConfirmationListener(UploadFilesActivity.this);
357 dialog.show(getSupportFragmentManager(), QUERY_TO_MOVE_DIALOG_TAG);
358 }
359 }
360 }
361
362 @Override
363 public void onConfirmation(String callerTag) {
364 Log_OC.d(TAG, "Positive button in dialog was clicked; dialog tag is " + callerTag);
365 if (callerTag.equals(QUERY_TO_MOVE_DIALOG_TAG)) {
366 // return the list of selected files to the caller activity (success),
367 // signaling that they should be moved to the ownCloud folder, instead of copied
368 Intent data = new Intent();
369 data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
370 setResult(RESULT_OK_AND_MOVE, data);
371 finish();
372 }
373 }
374
375
376 @Override
377 public void onNeutral(String callerTag) {
378 Log_OC.d(TAG, "Phantom neutral button in dialog was clicked; dialog tag is " + callerTag);
379 }
380
381
382 @Override
383 public void onCancel(String callerTag) {
384 /// nothing to do; don't finish, let the user change the selection
385 Log_OC.d(TAG, "Negative button in dialog was clicked; dialog tag is " + callerTag);
386 }
387
388
389 @Override
390 protected void onAccountSet(boolean stateWasRecovered) {
391 super.onAccountSet(stateWasRecovered);
392 if (getAccount() != null) {
393 if (!mAccountOnCreation.equals(getAccount())) {
394 setResult(RESULT_CANCELED);
395 finish();
396 }
397
398 } else {
399 setResult(RESULT_CANCELED);
400 finish();
401 }
402 }
403
404
405 }