1 package eu
.alefzero
.owncloud
.files
.services
; 
   5 import eu
.alefzero
.owncloud
.AccountUtils
; 
   6 import eu
.alefzero
.owncloud
.R
; 
   7 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
; 
   8 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
; 
   9 import eu
.alefzero
.owncloud
.datamodel
.OCFile
; 
  10 import eu
.alefzero
.owncloud
.files
.interfaces
.OnDatatransferProgressListener
; 
  11 import eu
.alefzero
.owncloud
.utils
.OwnCloudVersion
; 
  12 import eu
.alefzero
.webdav
.OnUploadProgressListener
; 
  13 import eu
.alefzero
.webdav
.WebdavClient
; 
  14 import android
.accounts
.Account
; 
  15 import android
.accounts
.AccountManager
; 
  16 import android
.app
.Notification
; 
  17 import android
.app
.NotificationManager
; 
  18 import android
.app
.Service
; 
  19 import android
.content
.Intent
; 
  20 import android
.net
.Uri
; 
  21 import android
.os
.Handler
; 
  22 import android
.os
.HandlerThread
; 
  23 import android
.os
.IBinder
; 
  24 import android
.os
.Looper
; 
  25 import android
.os
.Message
; 
  26 import android
.os
.Process
; 
  27 import android
.util
.Log
; 
  28 import android
.webkit
.MimeTypeMap
; 
  29 import android
.widget
.RemoteViews
; 
  30 import android
.widget
.Toast
; 
  32 public class FileUploader 
extends Service 
implements OnDatatransferProgressListener 
{ 
  34     public static final String KEY_LOCAL_FILE 
= "LOCAL_FILE"; 
  35     public static final String KEY_REMOTE_FILE 
= "REMOTE_FILE"; 
  36     public static final String KEY_ACCOUNT 
= "ACCOUNT"; 
  37     public static final String KEY_UPLOAD_TYPE 
= "UPLOAD_TYPE"; 
  39     public static final int UPLOAD_SINGLE_FILE 
= 0; 
  40     public static final int UPLOAD_MULTIPLE_FILES 
= 1; 
  42     private static final String TAG 
= "FileUploader"; 
  43     private NotificationManager mNotificationManager
; 
  44     private Looper mServiceLooper
; 
  45     private ServiceHandler mServiceHandler
; 
  46     private AccountManager mAccountManager
; 
  47     private Account mAccount
; 
  48     private String
[] mLocalPaths
, mRemotePaths
; 
  49     private boolean mResult
; 
  50     private int mUploadType
; 
  51     private Notification mNotification
; 
  52     private int mTotalDataToSend
, mSendData
; 
  53     private int mCurrentIndexUpload
, mPreviousPercent
; 
  56     public IBinder 
onBind(Intent arg0
) { 
  60     private final class ServiceHandler 
extends Handler 
{ 
  61         public ServiceHandler(Looper looper
) { 
  66         public void handleMessage(Message msg
) { 
  73     public void onCreate() { 
  75         mNotificationManager 
= (NotificationManager
) getSystemService(NOTIFICATION_SERVICE
); 
  76         HandlerThread thread 
= new HandlerThread("FileUploaderThread", 
  77                 Process
.THREAD_PRIORITY_BACKGROUND
); 
  79         mServiceLooper 
= thread
.getLooper(); 
  80         mServiceHandler 
= new ServiceHandler(mServiceLooper
); 
  81         mAccountManager 
= AccountManager
.get(this); 
  85     public int onStartCommand(Intent intent
, int flags
, int startId
) { 
  86         if (!intent
.hasExtra(KEY_ACCOUNT
) && !intent
.hasExtra(KEY_UPLOAD_TYPE
)) { 
  87             Log
.e(TAG
, "Not enought data in intent provided"); 
  88             return Service
.START_NOT_STICKY
; 
  90         mAccount 
= intent
.getParcelableExtra(KEY_ACCOUNT
); 
  91         mUploadType 
= intent
.getIntExtra(KEY_UPLOAD_TYPE
, -1); 
  92         if (mUploadType 
== -1) { 
  93             Log
.e(TAG
, "Incorrect upload type provided"); 
  94             return Service
.START_NOT_STICKY
; 
  96         if (mUploadType 
== UPLOAD_SINGLE_FILE
) { 
  97             mLocalPaths 
= new String
[] { intent
.getStringExtra(KEY_LOCAL_FILE
) }; 
  98             mRemotePaths 
= new String
[] { intent
 
  99                     .getStringExtra(KEY_REMOTE_FILE
) }; 
 100         } else { // mUploadType == UPLOAD_MULTIPLE_FILES 
 101             mLocalPaths 
= intent
.getStringArrayExtra(KEY_LOCAL_FILE
); 
 102             mRemotePaths 
= intent
.getStringArrayExtra(KEY_REMOTE_FILE
); 
 105         for (int i 
= 0; i 
< mRemotePaths
.length
; ++i
) 
 106             mRemotePaths
[i
] = mRemotePaths
[i
].replace(' ', '+'); 
 108         if (mLocalPaths
.length 
!= mRemotePaths
.length
) { 
 109             Log
.e(TAG
, "Remote paths and local paths are not equal!"); 
 110             return Service
.START_NOT_STICKY
; 
 113         Message msg 
= mServiceHandler
.obtainMessage(); 
 115         mServiceHandler
.sendMessage(msg
); 
 117         return Service
.START_NOT_STICKY
; 
 122             Toast
.makeText(this, "Upload successfull", Toast
.LENGTH_SHORT
) 
 125             Toast
.makeText(this, "No i kupa", Toast
.LENGTH_SHORT
).show(); 
 129     public void uploadFile() { 
 130         String baseUrl 
= mAccountManager
.getUserData(mAccount
, 
 131                 AccountAuthenticator
.KEY_OC_BASE_URL
), ocVerStr 
= mAccountManager
 
 132                 .getUserData(mAccount
, AccountAuthenticator
.KEY_OC_VERSION
); 
 133         OwnCloudVersion ocVer 
= new OwnCloudVersion(ocVerStr
); 
 134         String webdav_path 
= AccountUtils
.getWebdavPath(ocVer
); 
 135         Uri ocUri 
= Uri
.parse(baseUrl 
+ webdav_path
); 
 136         String username 
= mAccount
.name
.substring(0, 
 137                 mAccount
.name
.lastIndexOf('@')); 
 138         String password 
= mAccountManager
.getPassword(mAccount
); 
 139         FileDataStorageManager storageManager 
= new FileDataStorageManager(mAccount
, getContentResolver()); 
 141         mTotalDataToSend 
= mSendData 
= mPreviousPercent 
= 0; 
 143         mNotification 
= new Notification( 
 144                 eu
.alefzero
.owncloud
.R
.drawable
.icon
, "Uploading...", 
 145                 System
.currentTimeMillis()); 
 146         mNotification
.flags 
|= Notification
.FLAG_ONGOING_EVENT
; 
 147         mNotification
.contentView 
= new RemoteViews(getApplicationContext().getPackageName(), R
.layout
.progressbar_layout
); 
 148         mNotification
.contentView
.setProgressBar(R
.id
.status_progress
, 100, 0, false
); 
 149         mNotification
.contentView
.setImageViewResource(R
.id
.status_icon
, R
.drawable
.icon
); 
 151         mNotificationManager
.notify(42, mNotification
); 
 153         WebdavClient wc 
= new WebdavClient(ocUri
); 
 154         wc
.allowSelfsignedCertificates(); 
 155         wc
.setDataTransferProgressListener(this); 
 156         wc
.setCredentials(username
, password
); 
 158         for (int i 
= 0; i 
< mLocalPaths
.length
; ++i
) { 
 159             File f 
= new File(mLocalPaths
[i
]); 
 160             mTotalDataToSend 
+= f
.length(); 
 163         Log
.d(TAG
, "Will upload " + mTotalDataToSend 
+ " bytes, with " + mLocalPaths
.length 
+ " files"); 
 165         for (int i 
= 0; i 
< mLocalPaths
.length
; ++i
) { 
 166             String mimeType 
= MimeTypeMap
.getSingleton() 
 167                     .getMimeTypeFromExtension( 
 168                             mLocalPaths
[i
].substring(mLocalPaths
[i
] 
 169                                     .lastIndexOf('.') + 1)); 
 171             mCurrentIndexUpload 
= i
; 
 172             if (wc
.putFile(mLocalPaths
[i
], mRemotePaths
[i
], mimeType
)) { 
 174                 OCFile new_file 
= new OCFile(mRemotePaths
[i
]); 
 175                 new_file
.setMimetype(mimeType
); 
 176                 new_file
.setFileLength(new File(mLocalPaths
[i
]).length()); 
 177                 new_file
.setModificationTimestamp(System
.currentTimeMillis()); 
 178                 new_file
.setLastSyncDate(0); 
 179                 new_file
.setStoragePath(mLocalPaths
[i
]); 
 180                 new_file
.setParentId(storageManager
.getFileByPath(mRemotePaths
[i
].substring(0, mRemotePaths
[i
].lastIndexOf('/')+1)).getFileId()); 
 181                 storageManager
.saveFile(new_file
); 
 184         // notification.contentView.setProgressBar(R.id.status_progress, 
 185         // mLocalPaths.length-1, mLocalPaths.length-1, false); 
 186         mNotificationManager
.cancel(42); 
 191     public void transferProgress(long progressRate
) { 
 192         mSendData 
+= progressRate
; 
 193         int percent 
= (int)(100*((double)mSendData
)/((double)mTotalDataToSend
)); 
 194         if (percent 
!= mPreviousPercent
) { 
 195             String text 
= String
.format("%d%% Uploading %s file", percent
, new File(mLocalPaths
[mCurrentIndexUpload
]).getName()); 
 196             mNotification
.contentView
.setProgressBar(R
.id
.status_progress
, 100, percent
, false
); 
 197             mNotification
.contentView
.setTextViewText(R
.id
.status_text
, text
); 
 198             mNotificationManager
.notify(42, mNotification
); 
 200         mPreviousPercent 
= percent
;