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