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