1 /* ownCloud Android client application 
   2  *   Copyright (C) 2011  Bartek Przybylski 
   4  *   This program is free software: you can redistribute it and/or modify 
   5  *   it under the terms of the GNU General Public License as published by 
   6  *   the Free Software Foundation, either version 3 of the License, or 
   7  *   (at your option) any later version. 
   9  *   This program is distributed in the hope that it will be useful, 
  10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
  11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  12  *   GNU General Public License for more details. 
  14  *   You should have received a copy of the GNU General Public License 
  15  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. 
  19 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
.util
.Log
; 
  30 import android
.view
.View
; 
  31 import android
.view
.View
.OnClickListener
; 
  32 import android
.view
.ViewGroup
; 
  33 import android
.widget
.ArrayAdapter
; 
  34 import android
.widget
.Button
; 
  35 import android
.widget
.TextView
; 
  37 import com
.actionbarsherlock
.app
.ActionBar
; 
  38 import com
.actionbarsherlock
.app
.ActionBar
.OnNavigationListener
; 
  39 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
; 
  40 import com
.actionbarsherlock
.view
.MenuItem
; 
  41 import com
.owncloud
.android
.ui
.dialog
.IndeterminateProgressDialog
; 
  42 import com
.owncloud
.android
.ui
.fragment
.ConfirmationDialogFragment
; 
  43 import com
.owncloud
.android
.ui
.fragment
.LocalFileListFragment
; 
  44 import com
.owncloud
.android
.ui
.fragment
.ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
; 
  45 import com
.owncloud
.android
.utils
.FileStorageUtils
; 
  47 import com
.owncloud
.android
.R
; 
  50  * Displays local files and let the user choose what of them wants to upload 
  51  * to the current ownCloud account 
  53  * @author David A. Velasco 
  57 public class UploadFilesActivity 
extends SherlockFragmentActivity 
implements 
  58     LocalFileListFragment
.ContainerActivity
, OnNavigationListener
, OnClickListener
, ConfirmationDialogFragmentListener 
{ 
  60     private ArrayAdapter
<String
> mDirectories
; 
  61     private File mCurrentDir 
= null
; 
  62     private LocalFileListFragment mFileListFragment
; 
  63     private Button mCancelBtn
; 
  64     private Button mUploadBtn
; 
  65     private Account mAccount
; 
  66     private DialogFragment mCurrentDialog
; 
  68     public static final String EXTRA_ACCOUNT 
= UploadFilesActivity
.class.getCanonicalName() + ".EXTRA_ACCOUNT"; 
  69     public static final String EXTRA_CHOSEN_FILES 
= UploadFilesActivity
.class.getCanonicalName() + ".EXTRA_CHOSEN_FILES"; 
  71     public static final int RESULT_OK_AND_MOVE 
= RESULT_FIRST_USER
;  
  73     private static final String KEY_DIRECTORY_PATH 
= 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
.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         mAccount 
= getIntent().getParcelableExtra(EXTRA_ACCOUNT
); 
  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); 
 115         ActionBar actionBar 
= getSupportActionBar(); 
 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
.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 
= 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
.d(TAG
, "onSaveInstanceState() start"); 
 185         super.onSaveInstanceState(outState
); 
 186         outState
.putString(UploadFilesActivity
.KEY_DIRECTORY_PATH
, mCurrentDir
.getAbsolutePath()); 
 187         Log
.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(mAccount
.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                 mCurrentDialog 
= dialog
; 
 351                 mCurrentDialog
.show(getSupportFragmentManager(), QUERY_TO_MOVE_DIALOG_TAG
); 
 357     public void onConfirmation(String callerTag
) { 
 358         Log
.d(TAG
, "Positive button in dialog was clicked; dialog tag is " + callerTag
); 
 359         if (callerTag
.equals(QUERY_TO_MOVE_DIALOG_TAG
)) { 
 360             // return the list of selected files to the caller activity (success), signaling that they should be moved to the ownCloud folder, instead of copied 
 361             Intent data 
= new Intent(); 
 362             data
.putExtra(EXTRA_CHOSEN_FILES
, mFileListFragment
.getCheckedFilePaths()); 
 363             setResult(RESULT_OK_AND_MOVE
, data
); 
 366         mCurrentDialog
.dismiss(); 
 367         mCurrentDialog 
= null
; 
 372     public void onNeutral(String callerTag
) { 
 373         Log
.d(TAG
, "Phantom neutral button in dialog was clicked; dialog tag is " + callerTag
); 
 374         mCurrentDialog
.dismiss(); 
 375         mCurrentDialog 
= null
; 
 380     public void onCancel(String callerTag
) { 
 381         /// nothing to do; don't finish, let the user change the selection 
 382         Log
.d(TAG
, "Negative button in dialog was clicked; dialog tag is " + callerTag
); 
 383         mCurrentDialog
.dismiss(); 
 384         mCurrentDialog 
= null
;