2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 package com
.owncloud
.android
.ui
.activity
;
25 import android
.accounts
.Account
;
26 import android
.content
.Intent
;
27 import android
.os
.AsyncTask
;
28 import android
.os
.Bundle
;
29 import android
.os
.Environment
;
30 import android
.support
.v4
.app
.DialogFragment
;
31 import android
.view
.View
;
32 import android
.view
.View
.OnClickListener
;
33 import android
.view
.ViewGroup
;
34 import android
.widget
.ArrayAdapter
;
35 import android
.widget
.Button
;
36 import android
.widget
.TextView
;
38 import com
.actionbarsherlock
.app
.ActionBar
;
39 import com
.actionbarsherlock
.app
.ActionBar
.OnNavigationListener
;
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
56 public class UploadFilesActivity
extends FileActivity
implements
57 LocalFileListFragment
.ContainerActivity
, OnNavigationListener
, OnClickListener
, ConfirmationDialogFragmentListener
{
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 mAccountOnCreation
;
65 private DialogFragment mCurrentDialog
;
67 public static final String EXTRA_CHOSEN_FILES
=
68 UploadFilesActivity
.class.getCanonicalName() + ".EXTRA_CHOSEN_FILES";
70 public static final int RESULT_OK_AND_MOVE
= RESULT_FIRST_USER
;
72 private static final String KEY_DIRECTORY_PATH
=
73 UploadFilesActivity
.class.getCanonicalName() + ".KEY_DIRECTORY_PATH";
74 private static final String TAG
= "UploadFilesActivity";
75 private static final String WAIT_DIALOG_TAG
= "WAIT";
76 private static final String QUERY_TO_MOVE_DIALOG_TAG
= "QUERY_TO_MOVE";
80 public void onCreate(Bundle savedInstanceState
) {
81 Log_OC
.d(TAG
, "onCreate() start");
82 super.onCreate(savedInstanceState
);
84 if(savedInstanceState
!= null
) {
85 mCurrentDir
= new File(savedInstanceState
.getString(UploadFilesActivity
.KEY_DIRECTORY_PATH
));
87 mCurrentDir
= Environment
.getExternalStorageDirectory();
90 mAccountOnCreation
= getAccount();
94 // Drop-down navigation
95 mDirectories
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
);
96 File currDir
= mCurrentDir
;
97 while(currDir
!= null
&& currDir
.getParentFile() != null
) {
98 mDirectories
.add(currDir
.getName());
99 currDir
= currDir
.getParentFile();
101 mDirectories
.add(File
.separator
);
103 // Inflate and set the layout view
104 setContentView(R
.layout
.upload_files_layout
);
105 mFileListFragment
= (LocalFileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.local_files_list
);
108 // Set input controllers
109 mCancelBtn
= (Button
) findViewById(R
.id
.upload_files_btn_cancel
);
110 mCancelBtn
.setOnClickListener(this);
111 mUploadBtn
= (Button
) findViewById(R
.id
.upload_files_btn_upload
);
112 mUploadBtn
.setOnClickListener(this);
116 ActionBar actionBar
= getSupportActionBar();
117 actionBar
.setIcon(DisplayUtils
.getSeasonalIconId());
118 actionBar
.setHomeButtonEnabled(true
); // mandatory since Android ICS, according to the official documentation
119 actionBar
.setDisplayHomeAsUpEnabled(mCurrentDir
!= null
&& mCurrentDir
.getName() != null
);
120 actionBar
.setDisplayShowTitleEnabled(false
);
121 actionBar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
122 actionBar
.setListNavigationCallbacks(mDirectories
, this);
125 if (mCurrentDialog
!= null
) {
126 mCurrentDialog
.dismiss();
127 mCurrentDialog
= null
;
130 Log_OC
.d(TAG
, "onCreate() end");
135 public boolean onOptionsItemSelected(MenuItem item
) {
136 boolean retval
= true
;
137 switch (item
.getItemId()) {
138 case android
.R
.id
.home
: {
139 if(mCurrentDir
!= null
&& mCurrentDir
.getParentFile() != null
){
145 retval
= super.onOptionsItemSelected(item
);
152 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
153 int i
= itemPosition
;
157 // the next operation triggers a new call to this method, but it's necessary to
158 // ensure that the name exposed in the action bar is the current directory when the
159 // user selected it in the navigation list
160 if (itemPosition
!= 0)
161 getSupportActionBar().setSelectedNavigationItem(0);
167 public void onBackPressed() {
168 if (mDirectories
.getCount() <= 1) {
173 mFileListFragment
.onNavigateUp();
174 mCurrentDir
= mFileListFragment
.getCurrentDirectory();
176 if(mCurrentDir
.getParentFile() == null
){
177 ActionBar actionBar
= getSupportActionBar();
178 actionBar
.setDisplayHomeAsUpEnabled(false
);
184 protected void onSaveInstanceState(Bundle outState
) {
185 // responsibility of restore is preferred in onCreate() before than in
186 // 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");
195 * Pushes a directory to the drop down list
196 * @param directory to push
197 * @throws IllegalArgumentException If the {@link File#isDirectory()} returns false.
199 public void pushDirname(File directory
) {
200 if(!directory
.isDirectory()){
201 throw new IllegalArgumentException("Only directories may be pushed!");
203 mDirectories
.insert(directory
.getName(), 0);
204 mCurrentDir
= directory
;
208 * Pops a directory name from the drop down list
209 * @return True, unless the stack is empty
211 public boolean popDirname() {
212 mDirectories
.remove(mDirectories
.getItem(0));
213 return !mDirectories
.isEmpty();
217 // Custom array adapter to override text colors
218 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
220 public CustomArrayAdapter(UploadFilesActivity ctx
, int view
) {
224 public View
getView(int position
, View convertView
, ViewGroup parent
) {
225 View v
= super.getView(position
, convertView
, parent
);
227 ((TextView
) v
).setTextColor(getResources().getColorStateList(
228 android
.R
.color
.white
));
232 public View
getDropDownView(int position
, View convertView
,
234 View v
= super.getDropDownView(position
, convertView
, parent
);
236 ((TextView
) v
).setTextColor(getResources().getColorStateList(
237 android
.R
.color
.white
));
248 public void onDirectoryClick(File directory
) {
249 pushDirname(directory
);
250 ActionBar actionBar
= getSupportActionBar();
251 actionBar
.setDisplayHomeAsUpEnabled(true
);
259 public void onFileClick(File file
) {
267 public File
getInitialDirectory() {
273 * Performs corresponding action when user presses 'Cancel' or 'Upload' button
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.
279 public void onClick(View v
) {
280 if (v
.getId() == R
.id
.upload_files_btn_cancel
) {
281 setResult(RESULT_CANCELED
);
284 } else if (v
.getId() == R
.id
.upload_files_btn_upload
) {
285 new CheckAvailableSpaceTask().execute();
291 * Asynchronous task checking if there is space enough to copy all the files chosen
292 * to upload into the ownCloud local folder.
294 * Maybe an AsyncTask is not strictly necessary, but who really knows.
296 private class CheckAvailableSpaceTask
extends AsyncTask
<Void
, Void
, Boolean
> {
299 * Updates the UI before trying the movement
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
);
310 * Checks the available space
312 * @return 'True' if there is space enough.
315 protected Boolean
doInBackground(Void
... params
) {
316 String
[] checkedFilePaths
= mFileListFragment
.getCheckedFilePaths();
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();
323 return (new Boolean(FileStorageUtils
.getUsableSpace(mAccountOnCreation
.name
) >= total
));
327 * Updates the activity UI after the check of space is done.
329 * If there is not space enough. shows a new dialog to query the user if wants to move the files instead
332 * @param result 'True' when there is space enough to copy all the selected files.
335 protected void onPostExecute(Boolean result
) {
336 mCurrentDialog
.dismiss();
337 mCurrentDialog
= null
;
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
);
347 // show a dialog to query the user if wants to move the selected files
348 // to the ownCloud folder instead of copying
349 String
[] args
= {getString(R
.string
.app_name
)};
350 ConfirmationDialogFragment dialog
= ConfirmationDialogFragment
.newInstance(
351 R
.string
.upload_query_move_foreign_files
, args
, R
.string
.common_yes
, -1, R
.string
.common_no
353 dialog
.setOnConfirmationListener(UploadFilesActivity
.this);
354 dialog
.show(getSupportFragmentManager(), QUERY_TO_MOVE_DIALOG_TAG
);
360 public void onConfirmation(String callerTag
) {
361 Log_OC
.d(TAG
, "Positive button in dialog was clicked; dialog tag is " + callerTag
);
362 if (callerTag
.equals(QUERY_TO_MOVE_DIALOG_TAG
)) {
363 // return the list of selected files to the caller activity (success),
364 // signaling that they should be moved to the ownCloud folder, instead of copied
365 Intent data
= new Intent();
366 data
.putExtra(EXTRA_CHOSEN_FILES
, mFileListFragment
.getCheckedFilePaths());
367 setResult(RESULT_OK_AND_MOVE
, data
);
374 public void onNeutral(String callerTag
) {
375 Log_OC
.d(TAG
, "Phantom neutral button in dialog was clicked; dialog tag is " + callerTag
);
380 public void onCancel(String callerTag
) {
381 /// nothing to do; don't finish, let the user change the selection
382 Log_OC
.d(TAG
, "Negative button in dialog was clicked; dialog tag is " + callerTag
);
387 protected void onAccountSet(boolean stateWasRecovered
) {
388 super.onAccountSet(stateWasRecovered
);
389 if (getAccount() != null
) {
390 if (!mAccountOnCreation
.equals(getAccount())) {
391 setResult(RESULT_CANCELED
);
396 setResult(RESULT_CANCELED
);