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
;
23 import android
.accounts
.Account
;
24 import android
.content
.Intent
;
25 import android
.content
.SharedPreferences
;
26 import android
.os
.AsyncTask
;
27 import android
.os
.Bundle
;
28 import android
.os
.Environment
;
29 import android
.preference
.PreferenceManager
;
30 import android
.support
.v4
.app
.DialogFragment
;
31 import android
.support
.v7
.app
.ActionBar
;
32 import android
.view
.MenuItem
;
33 import android
.view
.View
;
34 import android
.view
.View
.OnClickListener
;
35 import android
.view
.ViewGroup
;
36 import android
.widget
.ArrayAdapter
;
37 import android
.widget
.Button
;
38 import android
.widget
.RadioButton
;
39 import android
.widget
.TextView
;
41 import com
.owncloud
.android
.R
;
42 import com
.owncloud
.android
.files
.services
.FileUploader
;
43 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
44 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
45 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
;
46 import com
.owncloud
.android
.ui
.dialog
.IndeterminateProgressDialog
;
47 import com
.owncloud
.android
.ui
.fragment
.LocalFileListFragment
;
48 import com
.owncloud
.android
.utils
.FileStorageUtils
;
54 * Displays local files and let the user choose what of them wants to upload
55 * to the current ownCloud account
58 public class UploadFilesActivity
extends FileActivity
implements
59 LocalFileListFragment
.ContainerActivity
, ActionBar
.OnNavigationListener
,
60 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
=
71 UploadFilesActivity
.class.getCanonicalName() + ".EXTRA_CHOSEN_FILES";
73 public static final int RESULT_OK_AND_MOVE
= RESULT_FIRST_USER
;
75 private static final String KEY_DIRECTORY_PATH
=
76 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 private RadioButton mRadioBtnCopyFiles
;
81 private RadioButton mRadioBtnMoveFiles
;
85 public void onCreate(Bundle savedInstanceState
) {
86 Log_OC
.d(TAG
, "onCreate() start");
87 super.onCreate(savedInstanceState
);
89 if(savedInstanceState
!= null
) {
90 mCurrentDir
= new File(savedInstanceState
.getString(
91 UploadFilesActivity
.KEY_DIRECTORY_PATH
));
93 mCurrentDir
= Environment
.getExternalStorageDirectory();
96 mAccountOnCreation
= getAccount();
100 // Drop-down navigation
101 mDirectories
= new CustomArrayAdapter
<String
>(this,
102 R
.layout
.support_simple_spinner_dropdown_item
);
103 File currDir
= mCurrentDir
;
104 while(currDir
!= null
&& currDir
.getParentFile() != null
) {
105 mDirectories
.add(currDir
.getName());
106 currDir
= currDir
.getParentFile();
108 mDirectories
.add(File
.separator
);
110 // Inflate and set the layout view
111 setContentView(R
.layout
.upload_files_layout
);
112 mFileListFragment
= (LocalFileListFragment
)
113 getSupportFragmentManager().findFragmentById(R
.id
.local_files_list
);
116 // Set input controllers
117 mCancelBtn
= (Button
) findViewById(R
.id
.upload_files_btn_cancel
);
118 mCancelBtn
.setOnClickListener(this);
119 mUploadBtn
= (Button
) findViewById(R
.id
.upload_files_btn_upload
);
120 mUploadBtn
.setOnClickListener(this);
122 SharedPreferences appPreferences
= PreferenceManager
123 .getDefaultSharedPreferences(getApplicationContext());
125 Integer localBehaviour
= appPreferences
.getInt("prefs_uploader_behaviour", FileUploader
.LOCAL_BEHAVIOUR_COPY
);
127 mRadioBtnMoveFiles
= (RadioButton
) findViewById(R
.id
.upload_radio_move
);
128 if (localBehaviour
== FileUploader
.LOCAL_BEHAVIOUR_MOVE
){
129 mRadioBtnMoveFiles
.setChecked(true
);
132 mRadioBtnCopyFiles
= (RadioButton
) findViewById(R
.id
.upload_radio_copy
);
133 if (localBehaviour
== FileUploader
.LOCAL_BEHAVIOUR_COPY
){
134 mRadioBtnCopyFiles
.setChecked(true
);
139 ActionBar actionBar
= getSupportActionBar();
140 actionBar
.setHomeButtonEnabled(true
); // mandatory since Android ICS, according to the
141 // official documentation
142 actionBar
.setDisplayHomeAsUpEnabled(mCurrentDir
!= null
&& mCurrentDir
.getName() != null
);
143 actionBar
.setDisplayShowTitleEnabled(false
);
144 actionBar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
145 actionBar
.setListNavigationCallbacks(mDirectories
, this);
148 if (mCurrentDialog
!= null
) {
149 mCurrentDialog
.dismiss();
150 mCurrentDialog
= null
;
153 Log_OC
.d(TAG
, "onCreate() end");
158 public boolean onOptionsItemSelected(MenuItem item
) {
159 boolean retval
= true
;
160 switch (item
.getItemId()) {
161 case android
.R
.id
.home
: {
162 if(mCurrentDir
!= null
&& mCurrentDir
.getParentFile() != null
){
168 retval
= super.onOptionsItemSelected(item
);
175 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
176 int i
= itemPosition
;
180 // the next operation triggers a new call to this method, but it's necessary to
181 // ensure that the name exposed in the action bar is the current directory when the
182 // user selected it in the navigation list
183 if (itemPosition
!= 0)
184 getSupportActionBar().setSelectedNavigationItem(0);
190 public void onBackPressed() {
191 if (mDirectories
.getCount() <= 1) {
196 mFileListFragment
.onNavigateUp();
197 mCurrentDir
= mFileListFragment
.getCurrentDirectory();
199 if(mCurrentDir
.getParentFile() == null
){
200 ActionBar actionBar
= getSupportActionBar();
201 actionBar
.setDisplayHomeAsUpEnabled(false
);
207 protected void onSaveInstanceState(Bundle outState
) {
208 // responsibility of restore is preferred in onCreate() before than in
209 // onRestoreInstanceState when there are Fragments involved
210 Log_OC
.d(TAG
, "onSaveInstanceState() start");
211 super.onSaveInstanceState(outState
);
212 outState
.putString(UploadFilesActivity
.KEY_DIRECTORY_PATH
, mCurrentDir
.getAbsolutePath());
213 Log_OC
.d(TAG
, "onSaveInstanceState() end");
218 * Pushes a directory to the drop down list
219 * @param directory to push
220 * @throws IllegalArgumentException If the {@link File#isDirectory()} returns false.
222 public void pushDirname(File directory
) {
223 if(!directory
.isDirectory()){
224 throw new IllegalArgumentException("Only directories may be pushed!");
226 mDirectories
.insert(directory
.getName(), 0);
227 mCurrentDir
= directory
;
231 * Pops a directory name from the drop down list
232 * @return True, unless the stack is empty
234 public boolean popDirname() {
235 mDirectories
.remove(mDirectories
.getItem(0));
236 return !mDirectories
.isEmpty();
240 // Custom array adapter to override text colors
241 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
243 public CustomArrayAdapter(UploadFilesActivity ctx
, int view
) {
247 public View
getView(int position
, View convertView
, ViewGroup parent
) {
248 View v
= super.getView(position
, convertView
, parent
);
250 ((TextView
) v
).setTextColor(getResources().getColorStateList(
251 android
.R
.color
.white
));
255 public View
getDropDownView(int position
, View convertView
,
257 View v
= super.getDropDownView(position
, convertView
, parent
);
259 ((TextView
) v
).setTextColor(getResources().getColorStateList(
260 android
.R
.color
.white
));
271 public void onDirectoryClick(File directory
) {
272 pushDirname(directory
);
273 ActionBar actionBar
= getSupportActionBar();
274 actionBar
.setDisplayHomeAsUpEnabled(true
);
282 public void onFileClick(File file
) {
290 public File
getInitialDirectory() {
296 * Performs corresponding action when user presses 'Cancel' or 'Upload' button
298 * TODO Make here the real request to the Upload service ; will require to receive the account and
299 * target folder where the upload must be done in the received intent.
302 public void onClick(View v
) {
303 if (v
.getId() == R
.id
.upload_files_btn_cancel
) {
304 setResult(RESULT_CANCELED
);
307 } else if (v
.getId() == R
.id
.upload_files_btn_upload
) {
308 new CheckAvailableSpaceTask().execute();
314 * Asynchronous task checking if there is space enough to copy all the files chosen
315 * to upload into the ownCloud local folder.
317 * Maybe an AsyncTask is not strictly necessary, but who really knows.
319 private class CheckAvailableSpaceTask
extends AsyncTask
<Void
, Void
, Boolean
> {
322 * Updates the UI before trying the movement
325 protected void onPreExecute () {
326 /// progress dialog and disable 'Move' button
327 mCurrentDialog
= IndeterminateProgressDialog
.newInstance(R
.string
.wait_a_moment
, false
);
328 mCurrentDialog
.show(getSupportFragmentManager(), WAIT_DIALOG_TAG
);
333 * Checks the available space
335 * @return 'True' if there is space enough.
338 protected Boolean
doInBackground(Void
... params
) {
339 String
[] checkedFilePaths
= mFileListFragment
.getCheckedFilePaths();
341 for (int i
=0; checkedFilePaths
!= null
&& i
< checkedFilePaths
.length
; i
++) {
342 String localPath
= checkedFilePaths
[i
];
343 File localFile
= new File(localPath
);
344 total
+= localFile
.length();
346 return (new Boolean(FileStorageUtils
.getUsableSpace(mAccountOnCreation
.name
) >= total
));
350 * Updates the activity UI after the check of space is done.
352 * If there is not space enough. shows a new dialog to query the user if wants to move the files instead
355 * @param result 'True' when there is space enough to copy all the selected files.
358 protected void onPostExecute(Boolean result
) {
359 mCurrentDialog
.dismiss();
360 mCurrentDialog
= null
;
363 // return the list of selected files (success)
364 Intent data
= new Intent();
365 data
.putExtra(EXTRA_CHOSEN_FILES
, mFileListFragment
.getCheckedFilePaths());
367 SharedPreferences
.Editor appPreferencesEditor
= PreferenceManager
368 .getDefaultSharedPreferences(getApplicationContext()).edit();
371 if (mRadioBtnMoveFiles
.isChecked()){
372 setResult(RESULT_OK_AND_MOVE
, data
);
373 appPreferencesEditor
.putInt("prefs_uploader_behaviour",
374 FileUploader
.LOCAL_BEHAVIOUR_MOVE
);
376 setResult(RESULT_OK
, data
);
377 appPreferencesEditor
.putInt("prefs_uploader_behaviour",
378 FileUploader
.LOCAL_BEHAVIOUR_COPY
);
380 appPreferencesEditor
.apply();
383 // show a dialog to query the user if wants to move the selected files
384 // to the ownCloud folder instead of copying
385 String
[] args
= {getString(R
.string
.app_name
)};
386 ConfirmationDialogFragment dialog
= ConfirmationDialogFragment
.newInstance(
387 R
.string
.upload_query_move_foreign_files
, args
, R
.string
.common_yes
, -1, R
.string
.common_no
389 dialog
.setOnConfirmationListener(UploadFilesActivity
.this);
390 dialog
.show(getSupportFragmentManager(), QUERY_TO_MOVE_DIALOG_TAG
);
396 public void onConfirmation(String callerTag
) {
397 Log_OC
.d(TAG
, "Positive button in dialog was clicked; dialog tag is " + callerTag
);
398 if (callerTag
.equals(QUERY_TO_MOVE_DIALOG_TAG
)) {
399 // return the list of selected files to the caller activity (success),
400 // signaling that they should be moved to the ownCloud folder, instead of copied
401 Intent data
= new Intent();
402 data
.putExtra(EXTRA_CHOSEN_FILES
, mFileListFragment
.getCheckedFilePaths());
403 setResult(RESULT_OK_AND_MOVE
, data
);
410 public void onNeutral(String callerTag
) {
411 Log_OC
.d(TAG
, "Phantom neutral button in dialog was clicked; dialog tag is " + callerTag
);
416 public void onCancel(String callerTag
) {
417 /// nothing to do; don't finish, let the user change the selection
418 Log_OC
.d(TAG
, "Negative button in dialog was clicked; dialog tag is " + callerTag
);
423 protected void onAccountSet(boolean stateWasRecovered
) {
424 super.onAccountSet(stateWasRecovered
);
425 if (getAccount() != null
) {
426 if (!mAccountOnCreation
.equals(getAccount())) {
427 setResult(RESULT_CANCELED
);
432 setResult(RESULT_CANCELED
);