Removed toast message for updating credentials when user manually accessed to the...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / UploadFilesActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 package com.owncloud.android.ui.activity;
19
20 import java.io.File;
21
22 import android.accounts.Account;
23 import android.content.Intent;
24 import android.os.AsyncTask;
25 import android.os.Bundle;
26 import android.os.Environment;
27 import android.support.v4.app.DialogFragment;
28 import android.view.View;
29 import android.view.View.OnClickListener;
30 import android.view.ViewGroup;
31 import android.widget.ArrayAdapter;
32 import android.widget.Button;
33 import android.widget.TextView;
34
35 import com.actionbarsherlock.app.ActionBar;
36 import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
37 import com.actionbarsherlock.app.SherlockFragmentActivity;
38 import com.actionbarsherlock.view.MenuItem;
39 import com.owncloud.android.ui.dialog.IndeterminateProgressDialog;
40 import com.owncloud.android.ui.fragment.ConfirmationDialogFragment;
41 import com.owncloud.android.ui.fragment.LocalFileListFragment;
42 import com.owncloud.android.ui.fragment.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
43 import com.owncloud.android.utils.FileStorageUtils;
44
45 import com.owncloud.android.Log_OC;
46 import com.owncloud.android.R;
47
48 /**
49 * Displays local files and let the user choose what of them wants to upload
50 * to the current ownCloud account
51 *
52 * @author David A. Velasco
53 *
54 */
55
56 public class UploadFilesActivity extends SherlockFragmentActivity 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 mAccount;
65 private DialogFragment mCurrentDialog;
66
67 public static final String EXTRA_ACCOUNT = UploadFilesActivity.class.getCanonicalName() + ".EXTRA_ACCOUNT";
68 public static final String EXTRA_CHOSEN_FILES = 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 = 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(UploadFilesActivity.KEY_DIRECTORY_PATH));
85 } else {
86 mCurrentDir = Environment.getExternalStorageDirectory();
87 }
88
89 mAccount = getIntent().getParcelableExtra(EXTRA_ACCOUNT);
90
91 /// USER INTERFACE
92
93 // Drop-down navigation
94 mDirectories = new CustomArrayAdapter<String>(this, R.layout.sherlock_spinner_dropdown_item);
95 File currDir = mCurrentDir;
96 while(currDir != null && currDir.getParentFile() != null) {
97 mDirectories.add(currDir.getName());
98 currDir = currDir.getParentFile();
99 }
100 mDirectories.add(File.separator);
101
102 // Inflate and set the layout view
103 setContentView(R.layout.upload_files_layout);
104 mFileListFragment = (LocalFileListFragment) getSupportFragmentManager().findFragmentById(R.id.local_files_list);
105
106
107 // Set input controllers
108 mCancelBtn = (Button) findViewById(R.id.upload_files_btn_cancel);
109 mCancelBtn.setOnClickListener(this);
110 mUploadBtn = (Button) findViewById(R.id.upload_files_btn_upload);
111 mUploadBtn.setOnClickListener(this);
112
113 // Action bar setup
114 ActionBar actionBar = getSupportActionBar();
115 actionBar.setHomeButtonEnabled(true); // mandatory since Android ICS, according to the official documentation
116 actionBar.setDisplayHomeAsUpEnabled(mCurrentDir != null && mCurrentDir.getName() != null);
117 actionBar.setDisplayShowTitleEnabled(false);
118 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
119 actionBar.setListNavigationCallbacks(mDirectories, this);
120
121 // wait dialog
122 if (mCurrentDialog != null) {
123 mCurrentDialog.dismiss();
124 mCurrentDialog = null;
125 }
126
127 Log_OC.d(TAG, "onCreate() end");
128 }
129
130
131 @Override
132 public boolean onOptionsItemSelected(MenuItem item) {
133 boolean retval = true;
134 switch (item.getItemId()) {
135 case android.R.id.home: {
136 if(mCurrentDir != null && mCurrentDir.getParentFile() != null){
137 onBackPressed();
138 }
139 break;
140 }
141 default:
142 retval = super.onOptionsItemSelected(item);
143 }
144 return retval;
145 }
146
147
148 @Override
149 public boolean onNavigationItemSelected(int itemPosition, long itemId) {
150 int i = itemPosition;
151 while (i-- != 0) {
152 onBackPressed();
153 }
154 // the next operation triggers a new call to this method, but it's necessary to
155 // ensure that the name exposed in the action bar is the current directory when the
156 // user selected it in the navigation list
157 if (itemPosition != 0)
158 getSupportActionBar().setSelectedNavigationItem(0);
159 return true;
160 }
161
162
163 @Override
164 public void onBackPressed() {
165 if (mDirectories.getCount() <= 1) {
166 finish();
167 return;
168 }
169 popDirname();
170 mFileListFragment.onNavigateUp();
171 mCurrentDir = mFileListFragment.getCurrentDirectory();
172
173 if(mCurrentDir.getParentFile() == null){
174 ActionBar actionBar = getSupportActionBar();
175 actionBar.setDisplayHomeAsUpEnabled(false);
176 }
177 }
178
179
180 @Override
181 protected void onSaveInstanceState(Bundle outState) {
182 // responsibility of restore is preferred in onCreate() before than in onRestoreInstanceState when there are Fragments involved
183 Log_OC.d(TAG, "onSaveInstanceState() start");
184 super.onSaveInstanceState(outState);
185 outState.putString(UploadFilesActivity.KEY_DIRECTORY_PATH, mCurrentDir.getAbsolutePath());
186 Log_OC.d(TAG, "onSaveInstanceState() end");
187 }
188
189
190 /**
191 * Pushes a directory to the drop down list
192 * @param directory to push
193 * @throws IllegalArgumentException If the {@link File#isDirectory()} returns false.
194 */
195 public void pushDirname(File directory) {
196 if(!directory.isDirectory()){
197 throw new IllegalArgumentException("Only directories may be pushed!");
198 }
199 mDirectories.insert(directory.getName(), 0);
200 mCurrentDir = directory;
201 }
202
203 /**
204 * Pops a directory name from the drop down list
205 * @return True, unless the stack is empty
206 */
207 public boolean popDirname() {
208 mDirectories.remove(mDirectories.getItem(0));
209 return !mDirectories.isEmpty();
210 }
211
212
213 // Custom array adapter to override text colors
214 private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
215
216 public CustomArrayAdapter(UploadFilesActivity ctx, int view) {
217 super(ctx, view);
218 }
219
220 public View getView(int position, View convertView, ViewGroup parent) {
221 View v = super.getView(position, convertView, parent);
222
223 ((TextView) v).setTextColor(getResources().getColorStateList(
224 android.R.color.white));
225 return v;
226 }
227
228 public View getDropDownView(int position, View convertView,
229 ViewGroup parent) {
230 View v = super.getDropDownView(position, convertView, parent);
231
232 ((TextView) v).setTextColor(getResources().getColorStateList(
233 android.R.color.white));
234
235 return v;
236 }
237
238 }
239
240 /**
241 * {@inheritDoc}
242 */
243 @Override
244 public void onDirectoryClick(File directory) {
245 pushDirname(directory);
246 ActionBar actionBar = getSupportActionBar();
247 actionBar.setDisplayHomeAsUpEnabled(true);
248 }
249
250
251 /**
252 * {@inheritDoc}
253 */
254 @Override
255 public void onFileClick(File file) {
256 // nothing to do
257 }
258
259 /**
260 * {@inheritDoc}
261 */
262 @Override
263 public File getInitialDirectory() {
264 return mCurrentDir;
265 }
266
267
268 /**
269 * Performs corresponding action when user presses 'Cancel' or 'Upload' button
270 *
271 * TODO Make here the real request to the Upload service ; will require to receive the account and
272 * target folder where the upload must be done in the received intent.
273 */
274 @Override
275 public void onClick(View v) {
276 if (v.getId() == R.id.upload_files_btn_cancel) {
277 setResult(RESULT_CANCELED);
278 finish();
279
280 } else if (v.getId() == R.id.upload_files_btn_upload) {
281 new CheckAvailableSpaceTask().execute();
282 }
283 }
284
285
286 /**
287 * Asynchronous task checking if there is space enough to copy all the files chosen
288 * to upload into the ownCloud local folder.
289 *
290 * Maybe an AsyncTask is not strictly necessary, but who really knows.
291 *
292 * @author David A. Velasco
293 */
294 private class CheckAvailableSpaceTask extends AsyncTask<Void, Void, Boolean> {
295
296 /**
297 * Updates the UI before trying the movement
298 */
299 @Override
300 protected void onPreExecute () {
301 /// progress dialog and disable 'Move' button
302 mCurrentDialog = IndeterminateProgressDialog.newInstance(R.string.wait_a_moment, false);
303 mCurrentDialog.show(getSupportFragmentManager(), WAIT_DIALOG_TAG);
304 }
305
306
307 /**
308 * Checks the available space
309 *
310 * @return 'True' if there is space enough.
311 */
312 @Override
313 protected Boolean doInBackground(Void... params) {
314 String[] checkedFilePaths = mFileListFragment.getCheckedFilePaths();
315 long total = 0;
316 for (int i=0; checkedFilePaths != null && i < checkedFilePaths.length ; i++) {
317 String localPath = checkedFilePaths[i];
318 File localFile = new File(localPath);
319 total += localFile.length();
320 }
321 return (FileStorageUtils.getUsableSpace(mAccount.name) >= total);
322 }
323
324 /**
325 * Updates the activity UI after the check of space is done.
326 *
327 * If there is not space enough. shows a new dialog to query the user if wants to move the files instead
328 * of copy them.
329 *
330 * @param result 'True' when there is space enough to copy all the selected files.
331 */
332 @Override
333 protected void onPostExecute(Boolean result) {
334 mCurrentDialog.dismiss();
335 mCurrentDialog = null;
336
337 if (result) {
338 // return the list of selected files (success)
339 Intent data = new Intent();
340 data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
341 setResult(RESULT_OK, data);
342 finish();
343
344 } else {
345 // show a dialog to query the user if wants to move the selected files to the ownCloud folder instead of copying
346 String[] args = {getString(R.string.app_name)};
347 ConfirmationDialogFragment dialog = ConfirmationDialogFragment.newInstance(R.string.upload_query_move_foreign_files, args, R.string.common_yes, -1, R.string.common_no);
348 dialog.setOnConfirmationListener(UploadFilesActivity.this);
349 dialog.show(getSupportFragmentManager(), QUERY_TO_MOVE_DIALOG_TAG);
350 }
351 }
352 }
353
354 @Override
355 public void onConfirmation(String callerTag) {
356 Log_OC.d(TAG, "Positive button in dialog was clicked; dialog tag is " + callerTag);
357 if (callerTag.equals(QUERY_TO_MOVE_DIALOG_TAG)) {
358 // return the list of selected files to the caller activity (success), signaling that they should be moved to the ownCloud folder, instead of copied
359 Intent data = new Intent();
360 data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
361 setResult(RESULT_OK_AND_MOVE, data);
362 finish();
363 }
364 }
365
366
367 @Override
368 public void onNeutral(String callerTag) {
369 Log_OC.d(TAG, "Phantom neutral button in dialog was clicked; dialog tag is " + callerTag);
370 }
371
372
373 @Override
374 public void onCancel(String callerTag) {
375 /// nothing to do; don't finish, let the user change the selection
376 Log_OC.d(TAG, "Negative button in dialog was clicked; dialog tag is " + callerTag);
377 }
378
379
380 }