ConflictsResolveActivity protected against access to non-existing account
[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.net.Uri;
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.view.View;
30 import android.view.View.OnClickListener;
31 import android.view.ViewGroup;
32 import android.widget.ArrayAdapter;
33 import android.widget.Button;
34 import android.widget.MediaController;
35 import android.widget.TextView;
36
37 import com.actionbarsherlock.app.ActionBar;
38 import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
39 import com.actionbarsherlock.app.SherlockFragmentActivity;
40 import com.actionbarsherlock.view.MenuItem;
41 import com.owncloud.android.datamodel.FileDataStorageManager;
42 import com.owncloud.android.datamodel.OCFile;
43 import com.owncloud.android.ui.dialog.IndeterminateProgressDialog;
44 import com.owncloud.android.ui.fragment.ConfirmationDialogFragment;
45 import com.owncloud.android.ui.fragment.LocalFileListFragment;
46 import com.owncloud.android.ui.fragment.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
47 import com.owncloud.android.utils.FileStorageUtils;
48
49 import com.owncloud.android.AccountUtils;
50 import com.owncloud.android.Log_OC;
51 import com.owncloud.android.R;
52
53 /**
54 * Displays local files and let the user choose what of them wants to upload
55 * to the current ownCloud account
56 *
57 * @author David A. Velasco
58 *
59 */
60
61 public class UploadFilesActivity extends FileActivity implements
62 LocalFileListFragment.ContainerActivity, OnNavigationListener, OnClickListener, ConfirmationDialogFragmentListener {
63
64 private ArrayAdapter<String> mDirectories;
65 private File mCurrentDir = null;
66 private LocalFileListFragment mFileListFragment;
67 private Button mCancelBtn;
68 private Button mUploadBtn;
69 private Account mAccountOnCreation;
70 private DialogFragment mCurrentDialog;
71
72 public static final String EXTRA_CHOSEN_FILES = UploadFilesActivity.class.getCanonicalName() + ".EXTRA_CHOSEN_FILES";
73
74 public static final int RESULT_OK_AND_MOVE = RESULT_FIRST_USER;
75
76 private static final String KEY_DIRECTORY_PATH = UploadFilesActivity.class.getCanonicalName() + ".KEY_DIRECTORY_PATH";
77 private static final String TAG = "UploadFilesActivity";
78 private static final String WAIT_DIALOG_TAG = "WAIT";
79 private static final String QUERY_TO_MOVE_DIALOG_TAG = "QUERY_TO_MOVE";
80
81
82 @Override
83 public void onCreate(Bundle savedInstanceState) {
84 Log_OC.d(TAG, "onCreate() start");
85 super.onCreate(savedInstanceState);
86
87 if(savedInstanceState != null) {
88 mCurrentDir = new File(savedInstanceState.getString(UploadFilesActivity.KEY_DIRECTORY_PATH));
89 } else {
90 mCurrentDir = Environment.getExternalStorageDirectory();
91 }
92
93 mAccountOnCreation = getAccount();
94
95 /// USER INTERFACE
96
97 // Drop-down navigation
98 mDirectories = new CustomArrayAdapter<String>(this, R.layout.sherlock_spinner_dropdown_item);
99 File currDir = mCurrentDir;
100 while(currDir != null && currDir.getParentFile() != null) {
101 mDirectories.add(currDir.getName());
102 currDir = currDir.getParentFile();
103 }
104 mDirectories.add(File.separator);
105
106 // Inflate and set the layout view
107 setContentView(R.layout.upload_files_layout);
108 mFileListFragment = (LocalFileListFragment) 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 // Action bar setup
118 ActionBar actionBar = getSupportActionBar();
119 actionBar.setHomeButtonEnabled(true); // mandatory since Android ICS, according to the official documentation
120 actionBar.setDisplayHomeAsUpEnabled(mCurrentDir != null && mCurrentDir.getName() != null);
121 actionBar.setDisplayShowTitleEnabled(false);
122 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
123 actionBar.setListNavigationCallbacks(mDirectories, this);
124
125 // wait dialog
126 if (mCurrentDialog != null) {
127 mCurrentDialog.dismiss();
128 mCurrentDialog = null;
129 }
130
131 Log_OC.d(TAG, "onCreate() end");
132 }
133
134
135 @Override
136 public boolean onOptionsItemSelected(MenuItem item) {
137 boolean retval = true;
138 switch (item.getItemId()) {
139 case android.R.id.home: {
140 if(mCurrentDir != null && mCurrentDir.getParentFile() != null){
141 onBackPressed();
142 }
143 break;
144 }
145 default:
146 retval = super.onOptionsItemSelected(item);
147 }
148 return retval;
149 }
150
151
152 @Override
153 public boolean onNavigationItemSelected(int itemPosition, long itemId) {
154 int i = itemPosition;
155 while (i-- != 0) {
156 onBackPressed();
157 }
158 // the next operation triggers a new call to this method, but it's necessary to
159 // ensure that the name exposed in the action bar is the current directory when the
160 // user selected it in the navigation list
161 if (itemPosition != 0)
162 getSupportActionBar().setSelectedNavigationItem(0);
163 return true;
164 }
165
166
167 @Override
168 public void onBackPressed() {
169 if (mDirectories.getCount() <= 1) {
170 finish();
171 return;
172 }
173 popDirname();
174 mFileListFragment.onNavigateUp();
175 mCurrentDir = mFileListFragment.getCurrentDirectory();
176
177 if(mCurrentDir.getParentFile() == null){
178 ActionBar actionBar = getSupportActionBar();
179 actionBar.setDisplayHomeAsUpEnabled(false);
180 }
181 }
182
183
184 @Override
185 protected void onSaveInstanceState(Bundle outState) {
186 // responsibility of restore is preferred in onCreate() before than in 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 * @author David A. Velasco
297 */
298 private class CheckAvailableSpaceTask extends AsyncTask<Void, Void, Boolean> {
299
300 /**
301 * Updates the UI before trying the movement
302 */
303 @Override
304 protected void onPreExecute () {
305 /// progress dialog and disable 'Move' button
306 mCurrentDialog = IndeterminateProgressDialog.newInstance(R.string.wait_a_moment, false);
307 mCurrentDialog.show(getSupportFragmentManager(), WAIT_DIALOG_TAG);
308 }
309
310
311 /**
312 * Checks the available space
313 *
314 * @return 'True' if there is space enough.
315 */
316 @Override
317 protected Boolean doInBackground(Void... params) {
318 String[] checkedFilePaths = mFileListFragment.getCheckedFilePaths();
319 long total = 0;
320 for (int i=0; checkedFilePaths != null && i < checkedFilePaths.length ; i++) {
321 String localPath = checkedFilePaths[i];
322 File localFile = new File(localPath);
323 total += localFile.length();
324 }
325 return (FileStorageUtils.getUsableSpace(mAccountOnCreation.name) >= total);
326 }
327
328 /**
329 * Updates the activity UI after the check of space is done.
330 *
331 * If there is not space enough. shows a new dialog to query the user if wants to move the files instead
332 * of copy them.
333 *
334 * @param result 'True' when there is space enough to copy all the selected files.
335 */
336 @Override
337 protected void onPostExecute(Boolean result) {
338 mCurrentDialog.dismiss();
339 mCurrentDialog = null;
340
341 if (result) {
342 // return the list of selected files (success)
343 Intent data = new Intent();
344 data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
345 setResult(RESULT_OK, data);
346 finish();
347
348 } else {
349 // show a dialog to query the user if wants to move the selected files to the ownCloud folder instead of copying
350 String[] args = {getString(R.string.app_name)};
351 ConfirmationDialogFragment dialog = ConfirmationDialogFragment.newInstance(R.string.upload_query_move_foreign_files, args, R.string.common_yes, -1, R.string.common_no);
352 dialog.setOnConfirmationListener(UploadFilesActivity.this);
353 dialog.show(getSupportFragmentManager(), QUERY_TO_MOVE_DIALOG_TAG);
354 }
355 }
356 }
357
358 @Override
359 public void onConfirmation(String callerTag) {
360 Log_OC.d(TAG, "Positive button in dialog was clicked; dialog tag is " + callerTag);
361 if (callerTag.equals(QUERY_TO_MOVE_DIALOG_TAG)) {
362 // return the list of selected files to the caller activity (success), signaling that they should be moved to the ownCloud folder, instead of copied
363 Intent data = new Intent();
364 data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
365 setResult(RESULT_OK_AND_MOVE, data);
366 finish();
367 }
368 }
369
370
371 @Override
372 public void onNeutral(String callerTag) {
373 Log_OC.d(TAG, "Phantom neutral button in dialog was clicked; dialog tag is " + callerTag);
374 }
375
376
377 @Override
378 public void onCancel(String callerTag) {
379 /// nothing to do; don't finish, let the user change the selection
380 Log_OC.d(TAG, "Negative button in dialog was clicked; dialog tag is " + callerTag);
381 }
382
383
384 @Override
385 protected void onAccountSet(boolean stateWasRecovered) {
386 if (getAccount() != null) {
387 if (!mAccountOnCreation.equals(getAccount())) {
388 setResult(RESULT_CANCELED);
389 finish();
390 }
391
392 } else {
393 Log_OC.wtf(TAG, "onAccountChanged was called with NULL account associated!");
394 setResult(RESULT_CANCELED);
395 finish();
396 }
397 }
398
399
400 }