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
.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
.TextView
;
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
, ActionBar
.OnNavigationListener
,
57 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(
86 UploadFilesActivity
.KEY_DIRECTORY_PATH
));
88 mCurrentDir
= Environment
.getExternalStorageDirectory();
91 mAccountOnCreation
= getAccount();
95 // Drop-down navigation
96 mDirectories
= new CustomArrayAdapter
<String
>(this,
97 R
.layout
.support_simple_spinner_dropdown_item
);
98 File currDir
= mCurrentDir
;
99 while(currDir
!= null
&& currDir
.getParentFile() != null
) {
100 mDirectories
.add(currDir
.getName());
101 currDir
= currDir
.getParentFile();
103 mDirectories
.add(File
.separator
);
105 // Inflate and set the layout view
106 setContentView(R
.layout
.upload_files_layout
);
107 mFileListFragment
= (LocalFileListFragment
)
108 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
122 // official documentation
123 actionBar
.setDisplayHomeAsUpEnabled(mCurrentDir
!= null
&& mCurrentDir
.getName() != null
);
124 actionBar
.setDisplayShowTitleEnabled(false
);
125 actionBar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
126 actionBar
.setListNavigationCallbacks(mDirectories
, this);
129 if (mCurrentDialog
!= null
) {
130 mCurrentDialog
.dismiss();
131 mCurrentDialog
= null
;
134 Log_OC
.d(TAG
, "onCreate() end");
139 public boolean onOptionsItemSelected(MenuItem item
) {
140 boolean retval
= true
;
141 switch (item
.getItemId()) {
142 case android
.R
.id
.home
: {
143 if(mCurrentDir
!= null
&& mCurrentDir
.getParentFile() != null
){
149 retval
= super.onOptionsItemSelected(item
);
156 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
157 int i
= itemPosition
;
161 // the next operation triggers a new call to this method, but it's necessary to
162 // ensure that the name exposed in the action bar is the current directory when the
163 // user selected it in the navigation list
164 if (itemPosition
!= 0)
165 getSupportActionBar().setSelectedNavigationItem(0);
171 public void onBackPressed() {
172 if (mDirectories
.getCount() <= 1) {
177 mFileListFragment
.onNavigateUp();
178 mCurrentDir
= mFileListFragment
.getCurrentDirectory();
180 if(mCurrentDir
.getParentFile() == null
){
181 ActionBar actionBar
= getSupportActionBar();
182 actionBar
.setDisplayHomeAsUpEnabled(false
);
188 protected void onSaveInstanceState(Bundle outState
) {
189 // responsibility of restore is preferred in onCreate() before than in
190 // onRestoreInstanceState when there are Fragments involved
191 Log_OC
.d(TAG
, "onSaveInstanceState() start");
192 super.onSaveInstanceState(outState
);
193 outState
.putString(UploadFilesActivity
.KEY_DIRECTORY_PATH
, mCurrentDir
.getAbsolutePath());
194 Log_OC
.d(TAG
, "onSaveInstanceState() end");
199 * Pushes a directory to the drop down list
200 * @param directory to push
201 * @throws IllegalArgumentException If the {@link File#isDirectory()} returns false.
203 public void pushDirname(File directory
) {
204 if(!directory
.isDirectory()){
205 throw new IllegalArgumentException("Only directories may be pushed!");
207 mDirectories
.insert(directory
.getName(), 0);
208 mCurrentDir
= directory
;
212 * Pops a directory name from the drop down list
213 * @return True, unless the stack is empty
215 public boolean popDirname() {
216 mDirectories
.remove(mDirectories
.getItem(0));
217 return !mDirectories
.isEmpty();
221 // Custom array adapter to override text colors
222 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
224 public CustomArrayAdapter(UploadFilesActivity ctx
, int view
) {
228 public View
getView(int position
, View convertView
, ViewGroup parent
) {
229 View v
= super.getView(position
, convertView
, parent
);
231 ((TextView
) v
).setTextColor(getResources().getColorStateList(
232 android
.R
.color
.white
));
236 public View
getDropDownView(int position
, View convertView
,
238 View v
= super.getDropDownView(position
, convertView
, parent
);
240 ((TextView
) v
).setTextColor(getResources().getColorStateList(
241 android
.R
.color
.white
));
252 public void onDirectoryClick(File directory
) {
253 pushDirname(directory
);
254 ActionBar actionBar
= getSupportActionBar();
255 actionBar
.setDisplayHomeAsUpEnabled(true
);
263 public void onFileClick(File file
) {
271 public File
getInitialDirectory() {
277 * Performs corresponding action when user presses 'Cancel' or 'Upload' button
279 * TODO Make here the real request to the Upload service ; will require to receive the account and
280 * target folder where the upload must be done in the received intent.
283 public void onClick(View v
) {
284 if (v
.getId() == R
.id
.upload_files_btn_cancel
) {
285 setResult(RESULT_CANCELED
);
288 } else if (v
.getId() == R
.id
.upload_files_btn_upload
) {
289 new CheckAvailableSpaceTask().execute();
295 * Asynchronous task checking if there is space enough to copy all the files chosen
296 * to upload into the ownCloud local folder.
298 * Maybe an AsyncTask is not strictly necessary, but who really knows.
300 private class CheckAvailableSpaceTask
extends AsyncTask
<Void
, Void
, Boolean
> {
303 * Updates the UI before trying the movement
306 protected void onPreExecute () {
307 /// progress dialog and disable 'Move' button
308 mCurrentDialog
= IndeterminateProgressDialog
.newInstance(R
.string
.wait_a_moment
, false
);
309 mCurrentDialog
.show(getSupportFragmentManager(), WAIT_DIALOG_TAG
);
314 * Checks the available space
316 * @return 'True' if there is space enough.
319 protected Boolean
doInBackground(Void
... params
) {
320 String
[] checkedFilePaths
= mFileListFragment
.getCheckedFilePaths();
322 for (int i
=0; checkedFilePaths
!= null
&& i
< checkedFilePaths
.length
; i
++) {
323 String localPath
= checkedFilePaths
[i
];
324 File localFile
= new File(localPath
);
325 total
+= localFile
.length();
327 return (new Boolean(FileStorageUtils
.getUsableSpace(mAccountOnCreation
.name
) >= total
));
331 * Updates the activity UI after the check of space is done.
333 * If there is not space enough. shows a new dialog to query the user if wants to move the files instead
336 * @param result 'True' when there is space enough to copy all the selected files.
339 protected void onPostExecute(Boolean result
) {
340 mCurrentDialog
.dismiss();
341 mCurrentDialog
= null
;
344 // return the list of selected files (success)
345 Intent data
= new Intent();
346 data
.putExtra(EXTRA_CHOSEN_FILES
, mFileListFragment
.getCheckedFilePaths());
347 setResult(RESULT_OK
, data
);
351 // show a dialog to query the user if wants to move the selected files
352 // to the ownCloud folder instead of copying
353 String
[] args
= {getString(R
.string
.app_name
)};
354 ConfirmationDialogFragment dialog
= ConfirmationDialogFragment
.newInstance(
355 R
.string
.upload_query_move_foreign_files
, args
, R
.string
.common_yes
, -1, R
.string
.common_no
357 dialog
.setOnConfirmationListener(UploadFilesActivity
.this);
358 dialog
.show(getSupportFragmentManager(), QUERY_TO_MOVE_DIALOG_TAG
);
364 public void onConfirmation(String callerTag
) {
365 Log_OC
.d(TAG
, "Positive button in dialog was clicked; dialog tag is " + callerTag
);
366 if (callerTag
.equals(QUERY_TO_MOVE_DIALOG_TAG
)) {
367 // return the list of selected files to the caller activity (success),
368 // signaling that they should be moved to the ownCloud folder, instead of copied
369 Intent data
= new Intent();
370 data
.putExtra(EXTRA_CHOSEN_FILES
, mFileListFragment
.getCheckedFilePaths());
371 setResult(RESULT_OK_AND_MOVE
, data
);
378 public void onNeutral(String callerTag
) {
379 Log_OC
.d(TAG
, "Phantom neutral button in dialog was clicked; dialog tag is " + callerTag
);
384 public void onCancel(String callerTag
) {
385 /// nothing to do; don't finish, let the user change the selection
386 Log_OC
.d(TAG
, "Negative button in dialog was clicked; dialog tag is " + callerTag
);
391 protected void onAccountSet(boolean stateWasRecovered
) {
392 super.onAccountSet(stateWasRecovered
);
393 if (getAccount() != null
) {
394 if (!mAccountOnCreation
.equals(getAccount())) {
395 setResult(RESULT_CANCELED
);
400 setResult(RESULT_CANCELED
);