1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.ui
.activity
;
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
;
35 import com
.actionbarsherlock
.app
.ActionBar
;
36 import com
.actionbarsherlock
.app
.ActionBar
.OnNavigationListener
;
37 import com
.actionbarsherlock
.internal
.view
.menu
.ActionMenuItemView
;
38 import com
.actionbarsherlock
.view
.Menu
;
39 import com
.actionbarsherlock
.view
.MenuInflater
;
40 import com
.actionbarsherlock
.view
.MenuItem
;
41 import com
.owncloud
.android
.R
;
42 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
43 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
44 import com
.owncloud
.android
.ui
.dialog
.IndeterminateProgressDialog
;
45 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
;
46 import com
.owncloud
.android
.ui
.fragment
.LocalFileListFragment
;
47 import com
.owncloud
.android
.utils
.DisplayUtils
;
48 import com
.owncloud
.android
.utils
.FileStorageUtils
;
52 * Displays local files and let the user choose what of them wants to upload
53 * to the current ownCloud account
55 * @author David A. Velasco
59 public class UploadFilesActivity
extends FileActivity
implements
60 LocalFileListFragment
.ContainerActivity
, OnNavigationListener
, OnClickListener
, ConfirmationDialogFragmentListener
{
62 private ArrayAdapter
<String
> mDirectories
;
63 private File mCurrentDir
= null
;
64 private LocalFileListFragment mFileListFragment
;
65 private Button mCancelBtn
;
66 private Button mUploadBtn
;
67 private Account mAccountOnCreation
;
68 private DialogFragment mCurrentDialog
;
70 public static final String EXTRA_CHOSEN_FILES
= UploadFilesActivity
.class.getCanonicalName() + ".EXTRA_CHOSEN_FILES";
72 public static final int RESULT_OK_AND_MOVE
= RESULT_FIRST_USER
;
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";
79 private boolean selectAllToggled
= false
;
83 public void onCreate(Bundle savedInstanceState
) {
84 Log_OC
.d(TAG
, "onCreate() start");
85 super.onCreate(savedInstanceState
);
87 if(savedInstanceState
!= null
) {
88 mCurrentDir
= new File(savedInstanceState
.getString(UploadFilesActivity
.KEY_DIRECTORY_PATH
));
90 mCurrentDir
= Environment
.getExternalStorageDirectory();
93 mAccountOnCreation
= getAccount();
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();
104 mDirectories
.add(File
.separator
);
106 // Inflate and set the layout view
107 setContentView(R
.layout
.upload_files_layout
);
108 mFileListFragment
= (LocalFileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.local_files_list
);
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);
119 ActionBar actionBar
= getSupportActionBar();
120 actionBar
.setIcon(DisplayUtils
.getSeasonalIconId());
121 actionBar
.setHomeButtonEnabled(true
); // mandatory since Android ICS, according to the 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);
129 if (mCurrentDialog
!= null
) {
130 mCurrentDialog
.dismiss();
131 mCurrentDialog
= null
;
134 Log_OC
.d(TAG
, "onCreate() end");
137 public boolean onCreateOptionsMenu(Menu menu
) {
138 // Inflate the menu items for use in the action bar
139 MenuInflater inflater
= getSherlock().getMenuInflater();
140 inflater
.inflate(R
.menu
.file_select_all
, menu
);
146 public boolean onOptionsItemSelected(MenuItem item
) {
147 boolean retval
= true
;
148 switch (item
.getItemId()) {
149 case android
.R
.id
.home
: {
150 if(mCurrentDir
!= null
&& mCurrentDir
.getParentFile() != null
){
155 case R
.id
.actionbar_select_all
:{
156 if(selectAllToggled
){
157 toggleOffSelectAll();
159 toggleOnSelectAll(item
);
164 retval
= super.onOptionsItemSelected(item
);
168 public void toggleOffSelectAll(MenuItem item
){
169 selectAllToggled
= false
;
170 item
.setIcon(android
.R
.drawable
.checkbox_off_background
);
171 mFileListFragment
.deselectAll();
173 public void toggleOffSelectAll(){
174 MenuItem item
= menu
.findItem(R
.id
.actionbar_select_all
);
175 toggleOffSelectAll(item
);
177 public void toggleOnSelectAll(MenuItem item
){
178 selectAllToggled
= true
;
179 item
.setIcon(android
.R
.drawable
.checkbox_on_background
);
180 mFileListFragment
.selectAll();
184 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
185 int i
= itemPosition
;
189 // the next operation triggers a new call to this method, but it's necessary to
190 // ensure that the name exposed in the action bar is the current directory when the
191 // user selected it in the navigation list
192 if (itemPosition
!= 0)
193 getSupportActionBar().setSelectedNavigationItem(0);
199 public void onBackPressed() {
200 if (mDirectories
.getCount() <= 1) {
205 mFileListFragment
.onNavigateUp();
206 mCurrentDir
= mFileListFragment
.getCurrentDirectory();
208 if(mCurrentDir
.getParentFile() == null
){
209 ActionBar actionBar
= getSupportActionBar();
210 actionBar
.setDisplayHomeAsUpEnabled(false
);
212 toggleOffSelectAll();
217 protected void onSaveInstanceState(Bundle outState
) {
218 // responsibility of restore is preferred in onCreate() before than in onRestoreInstanceState when there are Fragments involved
219 Log_OC
.d(TAG
, "onSaveInstanceState() start");
220 super.onSaveInstanceState(outState
);
221 outState
.putString(UploadFilesActivity
.KEY_DIRECTORY_PATH
, mCurrentDir
.getAbsolutePath());
222 Log_OC
.d(TAG
, "onSaveInstanceState() end");
227 * Pushes a directory to the drop down list
228 * @param directory to push
229 * @throws IllegalArgumentException If the {@link File#isDirectory()} returns false.
231 public void pushDirname(File directory
) {
232 if(!directory
.isDirectory()){
233 throw new IllegalArgumentException("Only directories may be pushed!");
235 mDirectories
.insert(directory
.getName(), 0);
236 mCurrentDir
= directory
;
240 * Pops a directory name from the drop down list
241 * @return True, unless the stack is empty
243 public boolean popDirname() {
244 mDirectories
.remove(mDirectories
.getItem(0));
245 return !mDirectories
.isEmpty();
249 // Custom array adapter to override text colors
250 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
252 public CustomArrayAdapter(UploadFilesActivity ctx
, int view
) {
256 public View
getView(int position
, View convertView
, ViewGroup parent
) {
257 View v
= super.getView(position
, convertView
, parent
);
259 ((TextView
) v
).setTextColor(getResources().getColorStateList(
260 android
.R
.color
.white
));
264 public View
getDropDownView(int position
, View convertView
,
266 View v
= super.getDropDownView(position
, convertView
, parent
);
268 ((TextView
) v
).setTextColor(getResources().getColorStateList(
269 android
.R
.color
.white
));
280 public void onDirectoryClick(File directory
) {
281 pushDirname(directory
);
282 ActionBar actionBar
= getSupportActionBar();
283 actionBar
.setDisplayHomeAsUpEnabled(true
);
284 toggleOffSelectAll();
292 public void onFileClick(File file
) {
300 public File
getInitialDirectory() {
306 * Performs corresponding action when user presses 'Cancel' or 'Upload' button
308 * TODO Make here the real request to the Upload service ; will require to receive the account and
309 * target folder where the upload must be done in the received intent.
312 public void onClick(View v
) {
313 if (v
.getId() == R
.id
.upload_files_btn_cancel
) {
314 setResult(RESULT_CANCELED
);
317 } else if (v
.getId() == R
.id
.upload_files_btn_upload
) {
318 new CheckAvailableSpaceTask().execute();
324 * Asynchronous task checking if there is space enough to copy all the files chosen
325 * to upload into the ownCloud local folder.
327 * Maybe an AsyncTask is not strictly necessary, but who really knows.
329 * @author David A. Velasco
331 private class CheckAvailableSpaceTask
extends AsyncTask
<Void
, Void
, Boolean
> {
334 * Updates the UI before trying the movement
337 protected void onPreExecute () {
338 /// progress dialog and disable 'Move' button
339 mCurrentDialog
= IndeterminateProgressDialog
.newInstance(R
.string
.wait_a_moment
, false
);
340 mCurrentDialog
.show(getSupportFragmentManager(), WAIT_DIALOG_TAG
);
345 * Checks the available space
347 * @return 'True' if there is space enough.
350 protected Boolean
doInBackground(Void
... params
) {
351 String
[] checkedFilePaths
= mFileListFragment
.getCheckedFilePaths();
353 for (int i
=0; checkedFilePaths
!= null
&& i
< checkedFilePaths
.length
; i
++) {
354 String localPath
= checkedFilePaths
[i
];
355 File localFile
= new File(localPath
);
356 total
+= localFile
.length();
358 return (FileStorageUtils
.getUsableSpace(mAccountOnCreation
.name
) >= total
);
362 * Updates the activity UI after the check of space is done.
364 * If there is not space enough. shows a new dialog to query the user if wants to move the files instead
367 * @param result 'True' when there is space enough to copy all the selected files.
370 protected void onPostExecute(Boolean result
) {
371 mCurrentDialog
.dismiss();
372 mCurrentDialog
= null
;
375 // return the list of selected files (success)
376 Intent data
= new Intent();
377 data
.putExtra(EXTRA_CHOSEN_FILES
, mFileListFragment
.getCheckedFilePaths());
378 setResult(RESULT_OK
, data
);
382 // show a dialog to query the user if wants to move the selected files to the ownCloud folder instead of copying
383 String
[] args
= {getString(R
.string
.app_name
)};
384 ConfirmationDialogFragment dialog
= ConfirmationDialogFragment
.newInstance(R
.string
.upload_query_move_foreign_files
, args
, R
.string
.common_yes
, -1, R
.string
.common_no
);
385 dialog
.setOnConfirmationListener(UploadFilesActivity
.this);
386 dialog
.show(getSupportFragmentManager(), QUERY_TO_MOVE_DIALOG_TAG
);
392 public void onConfirmation(String callerTag
) {
393 Log_OC
.d(TAG
, "Positive button in dialog was clicked; dialog tag is " + callerTag
);
394 if (callerTag
.equals(QUERY_TO_MOVE_DIALOG_TAG
)) {
395 // return the list of selected files to the caller activity (success), signaling that they should be moved to the ownCloud folder, instead of copied
396 Intent data
= new Intent();
397 data
.putExtra(EXTRA_CHOSEN_FILES
, mFileListFragment
.getCheckedFilePaths());
398 setResult(RESULT_OK_AND_MOVE
, data
);
405 public void onNeutral(String callerTag
) {
406 Log_OC
.d(TAG
, "Phantom neutral button in dialog was clicked; dialog tag is " + callerTag
);
411 public void onCancel(String callerTag
) {
412 /// nothing to do; don't finish, let the user change the selection
413 Log_OC
.d(TAG
, "Negative button in dialog was clicked; dialog tag is " + callerTag
);
418 protected void onAccountSet(boolean stateWasRecovered
) {
419 super.onAccountSet(stateWasRecovered
);
420 if (getAccount() != null
) {
421 if (!mAccountOnCreation
.equals(getAccount())) {
422 setResult(RESULT_CANCELED
);
427 setResult(RESULT_CANCELED
);