1 /* ownCloud Android client application
3 * @author David A. Velasco
4 * Copyright (C) 2012-2013 ownCloud Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package com
.owncloud
.android
.ui
.activity
;
24 import android
.accounts
.Account
;
25 import android
.content
.Intent
;
26 import android
.os
.AsyncTask
;
27 import android
.os
.Bundle
;
28 import android
.os
.Environment
;
29 import android
.support
.v4
.app
.DialogFragment
;
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
;
37 import com
.actionbarsherlock
.app
.ActionBar
;
38 import com
.actionbarsherlock
.app
.ActionBar
.OnNavigationListener
;
39 import com
.actionbarsherlock
.view
.MenuItem
;
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
;
51 * Displays local files and let the user choose what of them wants to upload
52 * to the current ownCloud account
55 public class UploadFilesActivity
extends FileActivity
implements
56 LocalFileListFragment
.ContainerActivity
, OnNavigationListener
, OnClickListener
, ConfirmationDialogFragmentListener
{
58 private ArrayAdapter
<String
> mDirectories
;
59 private File mCurrentDir
= null
;
60 private LocalFileListFragment mFileListFragment
;
61 private Button mCancelBtn
;
62 private Button mUploadBtn
;
63 private Account mAccountOnCreation
;
64 private DialogFragment mCurrentDialog
;
66 public static final String EXTRA_CHOSEN_FILES
= UploadFilesActivity
.class.getCanonicalName() + ".EXTRA_CHOSEN_FILES";
68 public static final int RESULT_OK_AND_MOVE
= RESULT_FIRST_USER
;
70 private static final String KEY_DIRECTORY_PATH
= UploadFilesActivity
.class.getCanonicalName() + ".KEY_DIRECTORY_PATH";
71 private static final String TAG
= "UploadFilesActivity";
72 private static final String WAIT_DIALOG_TAG
= "WAIT";
73 private static final String QUERY_TO_MOVE_DIALOG_TAG
= "QUERY_TO_MOVE";
77 public void onCreate(Bundle savedInstanceState
) {
78 Log_OC
.d(TAG
, "onCreate() start");
79 super.onCreate(savedInstanceState
);
81 if(savedInstanceState
!= null
) {
82 mCurrentDir
= new File(savedInstanceState
.getString(UploadFilesActivity
.KEY_DIRECTORY_PATH
));
84 mCurrentDir
= Environment
.getExternalStorageDirectory();
87 mAccountOnCreation
= getAccount();
91 // Drop-down navigation
92 mDirectories
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
);
93 File currDir
= mCurrentDir
;
94 while(currDir
!= null
&& currDir
.getParentFile() != null
) {
95 mDirectories
.add(currDir
.getName());
96 currDir
= currDir
.getParentFile();
98 mDirectories
.add(File
.separator
);
100 // Inflate and set the layout view
101 setContentView(R
.layout
.upload_files_layout
);
102 mFileListFragment
= (LocalFileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.local_files_list
);
105 // Set input controllers
106 mCancelBtn
= (Button
) findViewById(R
.id
.upload_files_btn_cancel
);
107 mCancelBtn
.setOnClickListener(this);
108 mUploadBtn
= (Button
) findViewById(R
.id
.upload_files_btn_upload
);
109 mUploadBtn
.setOnClickListener(this);
113 ActionBar actionBar
= getSupportActionBar();
114 actionBar
.setIcon(DisplayUtils
.getSeasonalIconId());
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);
122 if (mCurrentDialog
!= null
) {
123 mCurrentDialog
.dismiss();
124 mCurrentDialog
= null
;
127 Log_OC
.d(TAG
, "onCreate() end");
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
){
142 retval
= super.onOptionsItemSelected(item
);
149 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
150 int i
= itemPosition
;
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);
164 public void onBackPressed() {
165 if (mDirectories
.getCount() <= 1) {
170 mFileListFragment
.onNavigateUp();
171 mCurrentDir
= mFileListFragment
.getCurrentDirectory();
173 if(mCurrentDir
.getParentFile() == null
){
174 ActionBar actionBar
= getSupportActionBar();
175 actionBar
.setDisplayHomeAsUpEnabled(false
);
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");
191 * Pushes a directory to the drop down list
192 * @param directory to push
193 * @throws IllegalArgumentException If the {@link File#isDirectory()} returns false.
195 public void pushDirname(File directory
) {
196 if(!directory
.isDirectory()){
197 throw new IllegalArgumentException("Only directories may be pushed!");
199 mDirectories
.insert(directory
.getName(), 0);
200 mCurrentDir
= directory
;
204 * Pops a directory name from the drop down list
205 * @return True, unless the stack is empty
207 public boolean popDirname() {
208 mDirectories
.remove(mDirectories
.getItem(0));
209 return !mDirectories
.isEmpty();
213 // Custom array adapter to override text colors
214 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
216 public CustomArrayAdapter(UploadFilesActivity ctx
, int view
) {
220 public View
getView(int position
, View convertView
, ViewGroup parent
) {
221 View v
= super.getView(position
, convertView
, parent
);
223 ((TextView
) v
).setTextColor(getResources().getColorStateList(
224 android
.R
.color
.white
));
228 public View
getDropDownView(int position
, View convertView
,
230 View v
= super.getDropDownView(position
, convertView
, parent
);
232 ((TextView
) v
).setTextColor(getResources().getColorStateList(
233 android
.R
.color
.white
));
244 public void onDirectoryClick(File directory
) {
245 pushDirname(directory
);
246 ActionBar actionBar
= getSupportActionBar();
247 actionBar
.setDisplayHomeAsUpEnabled(true
);
255 public void onFileClick(File file
) {
263 public File
getInitialDirectory() {
269 * Performs corresponding action when user presses 'Cancel' or 'Upload' button
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.
275 public void onClick(View v
) {
276 if (v
.getId() == R
.id
.upload_files_btn_cancel
) {
277 setResult(RESULT_CANCELED
);
280 } else if (v
.getId() == R
.id
.upload_files_btn_upload
) {
281 new CheckAvailableSpaceTask().execute();
287 * Asynchronous task checking if there is space enough to copy all the files chosen
288 * to upload into the ownCloud local folder.
290 * Maybe an AsyncTask is not strictly necessary, but who really knows.
292 private class CheckAvailableSpaceTask
extends AsyncTask
<Void
, Void
, Boolean
> {
295 * Updates the UI before trying the movement
298 protected void onPreExecute () {
299 /// progress dialog and disable 'Move' button
300 mCurrentDialog
= IndeterminateProgressDialog
.newInstance(R
.string
.wait_a_moment
, false
);
301 mCurrentDialog
.show(getSupportFragmentManager(), WAIT_DIALOG_TAG
);
306 * Checks the available space
308 * @return 'True' if there is space enough.
311 protected Boolean
doInBackground(Void
... params
) {
312 String
[] checkedFilePaths
= mFileListFragment
.getCheckedFilePaths();
314 for (int i
=0; checkedFilePaths
!= null
&& i
< checkedFilePaths
.length
; i
++) {
315 String localPath
= checkedFilePaths
[i
];
316 File localFile
= new File(localPath
);
317 total
+= localFile
.length();
319 return (FileStorageUtils
.getUsableSpace(mAccountOnCreation
.name
) >= total
);
323 * Updates the activity UI after the check of space is done.
325 * If there is not space enough. shows a new dialog to query the user if wants to move the files instead
328 * @param result 'True' when there is space enough to copy all the selected files.
331 protected void onPostExecute(Boolean result
) {
332 mCurrentDialog
.dismiss();
333 mCurrentDialog
= null
;
336 // return the list of selected files (success)
337 Intent data
= new Intent();
338 data
.putExtra(EXTRA_CHOSEN_FILES
, mFileListFragment
.getCheckedFilePaths());
339 setResult(RESULT_OK
, data
);
343 // show a dialog to query the user if wants to move the selected files to the ownCloud folder instead of copying
344 String
[] args
= {getString(R
.string
.app_name
)};
345 ConfirmationDialogFragment dialog
= ConfirmationDialogFragment
.newInstance(R
.string
.upload_query_move_foreign_files
, args
, R
.string
.common_yes
, -1, R
.string
.common_no
);
346 dialog
.setOnConfirmationListener(UploadFilesActivity
.this);
347 dialog
.show(getSupportFragmentManager(), QUERY_TO_MOVE_DIALOG_TAG
);
353 public void onConfirmation(String callerTag
) {
354 Log_OC
.d(TAG
, "Positive button in dialog was clicked; dialog tag is " + callerTag
);
355 if (callerTag
.equals(QUERY_TO_MOVE_DIALOG_TAG
)) {
356 // return the list of selected files to the caller activity (success), signaling that they should be moved to the ownCloud folder, instead of copied
357 Intent data
= new Intent();
358 data
.putExtra(EXTRA_CHOSEN_FILES
, mFileListFragment
.getCheckedFilePaths());
359 setResult(RESULT_OK_AND_MOVE
, data
);
366 public void onNeutral(String callerTag
) {
367 Log_OC
.d(TAG
, "Phantom neutral button in dialog was clicked; dialog tag is " + callerTag
);
372 public void onCancel(String callerTag
) {
373 /// nothing to do; don't finish, let the user change the selection
374 Log_OC
.d(TAG
, "Negative button in dialog was clicked; dialog tag is " + callerTag
);
379 protected void onAccountSet(boolean stateWasRecovered
) {
380 super.onAccountSet(stateWasRecovered
);
381 if (getAccount() != null
) {
382 if (!mAccountOnCreation
.equals(getAccount())) {
383 setResult(RESULT_CANCELED
);
388 setResult(RESULT_CANCELED
);