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
.os
.AsyncTask
;
26 import android
.os
.Bundle
;
27 import android
.os
.Environment
;
28 import android
.support
.v4
.app
.DialogFragment
;
29 import android
.support
.v7
.app
.ActionBar
;
30 import android
.view
.MenuItem
;
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
.owncloud
.android
.R
;
39 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
40 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
41 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
;
42 import com
.owncloud
.android
.ui
.dialog
.IndeterminateProgressDialog
;
43 import com
.owncloud
.android
.ui
.fragment
.LocalFileListFragment
;
44 import com
.owncloud
.android
.utils
.FileStorageUtils
;
50 * Displays local files and let the user choose what of them wants to upload
51 * to the current ownCloud account
54 public class UploadFilesActivity
extends FileActivity
implements
55 LocalFileListFragment
.ContainerActivity
, ActionBar
.OnNavigationListener
,
56 OnClickListener
, ConfirmationDialogFragmentListener
{
58 private ArrayAdapter
<String
> mDirectories
;
59 private File mCurrentDir
= null
;
60 protected LocalFileListFragment mFileListFragment
;
61 protected Button mCancelBtn
;
62 protected Button mUploadBtn
;
63 protected Account mAccountOnCreation
;
64 protected DialogFragment mCurrentDialog
;
66 public static final String EXTRA_CHOSEN_FILES
=
67 UploadFilesActivity
.class.getCanonicalName() + ".EXTRA_CHOSEN_FILES";
69 public static final int RESULT_OK_AND_MOVE
= RESULT_FIRST_USER
;
71 public static final String KEY_DIRECTORY_PATH
=
72 UploadFilesActivity
.class.getCanonicalName() + ".KEY_DIRECTORY_PATH";
73 private static final String TAG
= "UploadFilesActivity";
74 private static final String WAIT_DIALOG_TAG
= "WAIT";
75 private static final String QUERY_TO_MOVE_DIALOG_TAG
= "QUERY_TO_MOVE";
79 public void onCreate(Bundle savedInstanceState
) {
80 Log_OC
.d(TAG
, "onCreate() start");
81 super.onCreate(savedInstanceState
);
83 if(savedInstanceState
!= null
) {
84 mCurrentDir
= new File(savedInstanceState
.getString(KEY_DIRECTORY_PATH
));
85 } else if (getIntent() != null
&& getIntent().hasExtra(KEY_DIRECTORY_PATH
)) {
86 mCurrentDir
= new File(getIntent().getStringExtra(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
.setHomeButtonEnabled(true
); // mandatory since Android ICS, according to the
121 // 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);
128 if (mCurrentDialog
!= null
) {
129 mCurrentDialog
.dismiss();
130 mCurrentDialog
= null
;
133 Log_OC
.d(TAG
, "onCreate() end");
138 public boolean onOptionsItemSelected(MenuItem item
) {
139 boolean retval
= true
;
140 switch (item
.getItemId()) {
141 case android
.R
.id
.home
: {
142 if(mCurrentDir
!= null
&& mCurrentDir
.getParentFile() != null
){
148 retval
= super.onOptionsItemSelected(item
);
155 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
156 int i
= itemPosition
;
160 // the next operation triggers a new call to this method, but it's necessary to
161 // ensure that the name exposed in the action bar is the current directory when the
162 // user selected it in the navigation list
163 if (itemPosition
!= 0)
164 getSupportActionBar().setSelectedNavigationItem(0);
170 public void onBackPressed() {
171 if (mDirectories
.getCount() <= 1) {
176 mFileListFragment
.onNavigateUp();
177 mCurrentDir
= mFileListFragment
.getCurrentDirectory();
179 if(mCurrentDir
.getParentFile() == null
){
180 ActionBar actionBar
= getSupportActionBar();
181 actionBar
.setDisplayHomeAsUpEnabled(false
);
187 protected void onSaveInstanceState(Bundle outState
) {
188 // responsibility of restore is preferred in onCreate() before than in
189 // onRestoreInstanceState when there are Fragments involved
190 Log_OC
.d(TAG
, "onSaveInstanceState() start");
191 super.onSaveInstanceState(outState
);
192 outState
.putString(UploadFilesActivity
.KEY_DIRECTORY_PATH
, mCurrentDir
.getAbsolutePath());
193 Log_OC
.d(TAG
, "onSaveInstanceState() end");
198 * Pushes a directory to the drop down list
199 * @param directory to push
200 * @throws IllegalArgumentException If the {@link File#isDirectory()} returns false.
202 public void pushDirname(File directory
) {
203 if(!directory
.isDirectory()){
204 throw new IllegalArgumentException("Only directories may be pushed!");
206 mDirectories
.insert(directory
.getName(), 0);
207 mCurrentDir
= directory
;
211 * Pops a directory name from the drop down list
212 * @return True, unless the stack is empty
214 public boolean popDirname() {
215 mDirectories
.remove(mDirectories
.getItem(0));
216 return !mDirectories
.isEmpty();
220 // Custom array adapter to override text colors
221 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
223 public CustomArrayAdapter(UploadFilesActivity ctx
, int view
) {
227 public View
getView(int position
, View convertView
, ViewGroup parent
) {
228 View v
= super.getView(position
, convertView
, parent
);
230 ((TextView
) v
).setTextColor(getResources().getColorStateList(
231 android
.R
.color
.white
));
235 public View
getDropDownView(int position
, View convertView
,
237 View v
= super.getDropDownView(position
, convertView
, parent
);
239 ((TextView
) v
).setTextColor(getResources().getColorStateList(
240 android
.R
.color
.white
));
251 public void onDirectoryClick(File directory
) {
252 pushDirname(directory
);
253 ActionBar actionBar
= getSupportActionBar();
254 actionBar
.setDisplayHomeAsUpEnabled(true
);
262 public void onFileClick(File file
) {
270 public File
getInitialDirectory() {
276 * Performs corresponding action when user presses 'Cancel' or 'Upload' button
278 * TODO Make here the real request to the Upload service ; will require to receive the account and
279 * target folder where the upload must be done in the received intent.
282 public void onClick(View v
) {
283 if (v
.getId() == R
.id
.upload_files_btn_cancel
) {
284 setResult(RESULT_CANCELED
);
287 } else if (v
.getId() == R
.id
.upload_files_btn_upload
) {
288 new CheckAvailableSpaceTask().execute();
294 * Asynchronous task checking if there is space enough to copy all the files chosen
295 * to upload into the ownCloud local folder.
297 * Maybe an AsyncTask is not strictly necessary, but who really knows.
299 private class CheckAvailableSpaceTask
extends AsyncTask
<Void
, Void
, Boolean
> {
302 * Updates the UI before trying the movement
305 protected void onPreExecute () {
306 /// progress dialog and disable 'Move' button
307 mCurrentDialog
= IndeterminateProgressDialog
.newInstance(R
.string
.wait_a_moment
, false
);
308 mCurrentDialog
.show(getSupportFragmentManager(), WAIT_DIALOG_TAG
);
313 * Checks the available space
315 * @return 'True' if there is space enough.
318 protected Boolean
doInBackground(Void
... params
) {
319 String
[] checkedFilePaths
= mFileListFragment
.getCheckedFilePaths();
321 for (int i
=0; checkedFilePaths
!= null
&& i
< checkedFilePaths
.length
; i
++) {
322 String localPath
= checkedFilePaths
[i
];
323 File localFile
= new File(localPath
);
324 total
+= localFile
.length();
326 return (new Boolean(FileStorageUtils
.getUsableSpace(mAccountOnCreation
.name
) >= total
));
330 * Updates the activity UI after the check of space is done.
332 * If there is not space enough. shows a new dialog to query the user if wants to move the files instead
335 * @param result 'True' when there is space enough to copy all the selected files.
338 protected void onPostExecute(Boolean result
) {
339 mCurrentDialog
.dismiss();
340 mCurrentDialog
= null
;
343 // return the list of selected files (success)
344 Intent data
= new Intent();
345 data
.putExtra(EXTRA_CHOSEN_FILES
, mFileListFragment
.getCheckedFilePaths());
346 setResult(RESULT_OK
, data
);
350 // show a dialog to query the user if wants to move the selected files
351 // to the ownCloud folder instead of copying
352 String
[] args
= {getString(R
.string
.app_name
)};
353 ConfirmationDialogFragment dialog
= ConfirmationDialogFragment
.newInstance(
354 R
.string
.upload_query_move_foreign_files
, args
, R
.string
.common_yes
, -1, R
.string
.common_no
356 dialog
.setOnConfirmationListener(UploadFilesActivity
.this);
357 dialog
.show(getSupportFragmentManager(), QUERY_TO_MOVE_DIALOG_TAG
);
363 public void onConfirmation(String callerTag
) {
364 Log_OC
.d(TAG
, "Positive button in dialog was clicked; dialog tag is " + callerTag
);
365 if (callerTag
.equals(QUERY_TO_MOVE_DIALOG_TAG
)) {
366 // return the list of selected files to the caller activity (success),
367 // signaling that they should be moved to the ownCloud folder, instead of copied
368 Intent data
= new Intent();
369 data
.putExtra(EXTRA_CHOSEN_FILES
, mFileListFragment
.getCheckedFilePaths());
370 setResult(RESULT_OK_AND_MOVE
, data
);
377 public void onNeutral(String callerTag
) {
378 Log_OC
.d(TAG
, "Phantom neutral button in dialog was clicked; dialog tag is " + callerTag
);
383 public void onCancel(String callerTag
) {
384 /// nothing to do; don't finish, let the user change the selection
385 Log_OC
.d(TAG
, "Negative button in dialog was clicked; dialog tag is " + callerTag
);
390 protected void onAccountSet(boolean stateWasRecovered
) {
391 super.onAccountSet(stateWasRecovered
);
392 if (getAccount() != null
) {
393 if (!mAccountOnCreation
.equals(getAccount())) {
394 setResult(RESULT_CANCELED
);
399 setResult(RESULT_CANCELED
);