1 /* ownCloud Android client application 
   2  *   Copyright (C) 2012-2013 ownCloud Inc. 
   4  *   This program is free software: you can redistribute it and/or modify 
   5  *   it under the terms of the GNU General Public License version 2, 
   6  *   as published by the Free Software Foundation. 
   8  *   This program is distributed in the hope that it will be useful, 
   9  *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
  10  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  11  *   GNU General Public License for more details. 
  13  *   You should have received a copy of the GNU General Public License 
  14  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. 
  18 package com
.owncloud
.android
.ui
.activity
; 
  22 import android
.accounts
.Account
; 
  23 import android
.content
.Intent
; 
  24 import android
.os
.AsyncTask
; 
  25 import android
.os
.Bundle
; 
  26 import android
.os
.Environment
; 
  27 import android
.support
.v4
.app
.DialogFragment
; 
  28 import android
.view
.View
; 
  29 import android
.view
.View
.OnClickListener
; 
  30 import android
.view
.ViewGroup
; 
  31 import android
.widget
.ArrayAdapter
; 
  32 import android
.widget
.Button
; 
  33 import android
.widget
.TextView
; 
  35 import com
.actionbarsherlock
.app
.ActionBar
; 
  36 import com
.actionbarsherlock
.app
.ActionBar
.OnNavigationListener
; 
  37 import com
.actionbarsherlock
.view
.MenuItem
; 
  38 import com
.owncloud
.android
.R
; 
  39 import com
.owncloud
.android
.ui
.dialog
.IndeterminateProgressDialog
; 
  40 import com
.owncloud
.android
.ui
.fragment
.ConfirmationDialogFragment
; 
  41 import com
.owncloud
.android
.ui
.fragment
.LocalFileListFragment
; 
  42 import com
.owncloud
.android
.ui
.fragment
.ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
; 
  43 import com
.owncloud
.android
.utils
.DisplayUtils
; 
  44 import com
.owncloud
.android
.utils
.FileStorageUtils
; 
  45 import com
.owncloud
.android
.utils
.Log_OC
; 
  49  * Displays local files and let the user choose what of them wants to upload 
  50  * to the current ownCloud account 
  52  * @author David A. Velasco 
  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 
= UploadFilesActivity
.class.getCanonicalName() + ".EXTRA_CHOSEN_FILES"; 
  69     public static final int RESULT_OK_AND_MOVE 
= RESULT_FIRST_USER
;  
  71     private static final String KEY_DIRECTORY_PATH 
= UploadFilesActivity
.class.getCanonicalName() + ".KEY_DIRECTORY_PATH"; 
  72     private static final String TAG 
= "UploadFilesActivity"; 
  73     private static final String WAIT_DIALOG_TAG 
= "WAIT"; 
  74     private static final String QUERY_TO_MOVE_DIALOG_TAG 
= "QUERY_TO_MOVE"; 
  78     public void onCreate(Bundle savedInstanceState
) { 
  79         Log_OC
.d(TAG
, "onCreate() start"); 
  80         super.onCreate(savedInstanceState
); 
  82         if(savedInstanceState 
!= null
) { 
  83             mCurrentDir 
= new File(savedInstanceState
.getString(UploadFilesActivity
.KEY_DIRECTORY_PATH
)); 
  85             mCurrentDir 
= Environment
.getExternalStorageDirectory(); 
  88         mAccountOnCreation 
= getAccount(); 
  92         // Drop-down navigation  
  93         mDirectories 
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
); 
  94         File currDir 
= mCurrentDir
; 
  95         while(currDir 
!= null 
&& currDir
.getParentFile() != null
) { 
  96             mDirectories
.add(currDir
.getName()); 
  97             currDir 
= currDir
.getParentFile(); 
  99         mDirectories
.add(File
.separator
); 
 101         // Inflate and set the layout view 
 102         setContentView(R
.layout
.upload_files_layout
); 
 103         mFileListFragment 
= (LocalFileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.local_files_list
); 
 106         // Set input controllers 
 107         mCancelBtn 
= (Button
) findViewById(R
.id
.upload_files_btn_cancel
); 
 108         mCancelBtn
.setOnClickListener(this); 
 109         mUploadBtn 
= (Button
) findViewById(R
.id
.upload_files_btn_upload
); 
 110         mUploadBtn
.setOnClickListener(this); 
 114         ActionBar actionBar 
= getSupportActionBar(); 
 115         actionBar
.setIcon(DisplayUtils
.getSeasonalIconId()); 
 116         actionBar
.setHomeButtonEnabled(true
);   // mandatory since Android ICS, according to the official documentation 
 117         actionBar
.setDisplayHomeAsUpEnabled(mCurrentDir 
!= null 
&& mCurrentDir
.getName() != null
); 
 118         actionBar
.setDisplayShowTitleEnabled(false
); 
 119         actionBar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
); 
 120         actionBar
.setListNavigationCallbacks(mDirectories
, this); 
 123         if (mCurrentDialog 
!= null
) { 
 124             mCurrentDialog
.dismiss(); 
 125             mCurrentDialog 
= null
; 
 128         Log_OC
.d(TAG
, "onCreate() end"); 
 133     public boolean onOptionsItemSelected(MenuItem item
) { 
 134         boolean retval 
= true
; 
 135         switch (item
.getItemId()) { 
 136             case android
.R
.id
.home
: { 
 137                 if(mCurrentDir 
!= null 
&& mCurrentDir
.getParentFile() != null
){ 
 143                 retval 
= super.onOptionsItemSelected(item
); 
 150     public boolean onNavigationItemSelected(int itemPosition
, long itemId
) { 
 151         int i 
= itemPosition
; 
 155         // the next operation triggers a new call to this method, but it's necessary to  
 156         // ensure that the name exposed in the action bar is the current directory when the  
 157         // user selected it in the navigation list 
 158         if (itemPosition 
!= 0) 
 159             getSupportActionBar().setSelectedNavigationItem(0); 
 165     public void onBackPressed() { 
 166         if (mDirectories
.getCount() <= 1) { 
 171         mFileListFragment
.onNavigateUp(); 
 172         mCurrentDir 
= mFileListFragment
.getCurrentDirectory(); 
 174         if(mCurrentDir
.getParentFile() == null
){ 
 175             ActionBar actionBar 
= getSupportActionBar();  
 176             actionBar
.setDisplayHomeAsUpEnabled(false
); 
 182     protected void onSaveInstanceState(Bundle outState
) { 
 183         // responsibility of restore is preferred in onCreate() before than in onRestoreInstanceState when there are Fragments involved 
 184         Log_OC
.d(TAG
, "onSaveInstanceState() start"); 
 185         super.onSaveInstanceState(outState
); 
 186         outState
.putString(UploadFilesActivity
.KEY_DIRECTORY_PATH
, mCurrentDir
.getAbsolutePath()); 
 187         Log_OC
.d(TAG
, "onSaveInstanceState() end"); 
 192      * Pushes a directory to the drop down list 
 193      * @param directory to push 
 194      * @throws IllegalArgumentException If the {@link File#isDirectory()} returns false. 
 196     public void pushDirname(File directory
) { 
 197         if(!directory
.isDirectory()){ 
 198             throw new IllegalArgumentException("Only directories may be pushed!"); 
 200         mDirectories
.insert(directory
.getName(), 0); 
 201         mCurrentDir 
= directory
; 
 205      * Pops a directory name from the drop down list 
 206      * @return True, unless the stack is empty 
 208     public boolean popDirname() { 
 209         mDirectories
.remove(mDirectories
.getItem(0)); 
 210         return !mDirectories
.isEmpty(); 
 214     // Custom array adapter to override text colors 
 215     private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> { 
 217         public CustomArrayAdapter(UploadFilesActivity ctx
, int view
) { 
 221         public View 
getView(int position
, View convertView
, ViewGroup parent
) { 
 222             View v 
= super.getView(position
, convertView
, parent
); 
 224             ((TextView
) v
).setTextColor(getResources().getColorStateList( 
 225                     android
.R
.color
.white
)); 
 229         public View 
getDropDownView(int position
, View convertView
, 
 231             View v 
= super.getDropDownView(position
, convertView
, parent
); 
 233             ((TextView
) v
).setTextColor(getResources().getColorStateList( 
 234                     android
.R
.color
.white
)); 
 245     public void onDirectoryClick(File directory
) { 
 246         pushDirname(directory
); 
 247         ActionBar actionBar 
= getSupportActionBar(); 
 248         actionBar
.setDisplayHomeAsUpEnabled(true
); 
 256     public void onFileClick(File file
) { 
 264     public File 
getInitialDirectory() { 
 270      * Performs corresponding action when user presses 'Cancel' or 'Upload' button 
 272      * TODO Make here the real request to the Upload service ; will require to receive the account and  
 273      * target folder where the upload must be done in the received intent. 
 276     public void onClick(View v
) { 
 277         if (v
.getId() == R
.id
.upload_files_btn_cancel
) { 
 278             setResult(RESULT_CANCELED
); 
 281         } else if (v
.getId() == R
.id
.upload_files_btn_upload
) { 
 282             new CheckAvailableSpaceTask().execute(); 
 288      * Asynchronous task checking if there is space enough to copy all the files chosen 
 289      * to upload into the ownCloud local folder. 
 291      * Maybe an AsyncTask is not strictly necessary, but who really knows. 
 293      * @author David A. Velasco 
 295     private class CheckAvailableSpaceTask 
extends AsyncTask
<Void
, Void
, Boolean
> { 
 298          * Updates the UI before trying the movement 
 301         protected void onPreExecute () { 
 302             /// progress dialog and disable 'Move' button 
 303             mCurrentDialog 
= IndeterminateProgressDialog
.newInstance(R
.string
.wait_a_moment
, false
); 
 304             mCurrentDialog
.show(getSupportFragmentManager(), WAIT_DIALOG_TAG
); 
 309          * Checks the available space 
 311          * @return     'True' if there is space enough. 
 314         protected Boolean 
doInBackground(Void
... params
) { 
 315             String
[] checkedFilePaths 
= mFileListFragment
.getCheckedFilePaths(); 
 317             for (int i
=0; checkedFilePaths 
!= null 
&& i 
< checkedFilePaths
.length 
; i
++) { 
 318                 String localPath 
= checkedFilePaths
[i
]; 
 319                 File localFile 
= new File(localPath
); 
 320                 total 
+= localFile
.length(); 
 322             return (FileStorageUtils
.getUsableSpace(mAccountOnCreation
.name
) >= total
); 
 326          * Updates the activity UI after the check of space is done. 
 328          * If there is not space enough. shows a new dialog to query the user if wants to move the files instead 
 331          * @param result        'True' when there is space enough to copy all the selected files. 
 334         protected void onPostExecute(Boolean result
) { 
 335             mCurrentDialog
.dismiss(); 
 336             mCurrentDialog 
= null
; 
 339                 // return the list of selected files (success) 
 340                 Intent data 
= new Intent(); 
 341                 data
.putExtra(EXTRA_CHOSEN_FILES
, mFileListFragment
.getCheckedFilePaths()); 
 342                 setResult(RESULT_OK
, data
); 
 346                 // show a dialog to query the user if wants to move the selected files to the ownCloud folder instead of copying 
 347                 String
[] args 
= {getString(R
.string
.app_name
)}; 
 348                 ConfirmationDialogFragment dialog 
= ConfirmationDialogFragment
.newInstance(R
.string
.upload_query_move_foreign_files
, args
, R
.string
.common_yes
, -1, R
.string
.common_no
); 
 349                 dialog
.setOnConfirmationListener(UploadFilesActivity
.this); 
 350                 dialog
.show(getSupportFragmentManager(), QUERY_TO_MOVE_DIALOG_TAG
); 
 356     public void onConfirmation(String callerTag
) { 
 357         Log_OC
.d(TAG
, "Positive button in dialog was clicked; dialog tag is " + callerTag
); 
 358         if (callerTag
.equals(QUERY_TO_MOVE_DIALOG_TAG
)) { 
 359             // return the list of selected files to the caller activity (success), signaling that they should be moved to the ownCloud folder, instead of copied 
 360             Intent data 
= new Intent(); 
 361             data
.putExtra(EXTRA_CHOSEN_FILES
, mFileListFragment
.getCheckedFilePaths()); 
 362             setResult(RESULT_OK_AND_MOVE
, data
); 
 369     public void onNeutral(String callerTag
) { 
 370         Log_OC
.d(TAG
, "Phantom neutral button in dialog was clicked; dialog tag is " + callerTag
); 
 375     public void onCancel(String callerTag
) { 
 376         /// nothing to do; don't finish, let the user change the selection 
 377         Log_OC
.d(TAG
, "Negative button in dialog was clicked; dialog tag is " + callerTag
); 
 382     protected void onAccountSet(boolean stateWasRecovered
) { 
 383         if (getAccount() != null
) { 
 384             if (!mAccountOnCreation
.equals(getAccount())) { 
 385                 setResult(RESULT_CANCELED
); 
 390             Log_OC
.wtf(TAG
, "onAccountChanged was called with NULL account associated!"); 
 391             setResult(RESULT_CANCELED
);