1 package eu
.alefzero
.owncloud
.files
.services
;
6 import eu
.alefzero
.owncloud
.AccountUtils
;
7 import eu
.alefzero
.owncloud
.R
;
8 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
9 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
10 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
11 import eu
.alefzero
.owncloud
.files
.interfaces
.OnDatatransferProgressListener
;
12 import eu
.alefzero
.owncloud
.utils
.OwnCloudVersion
;
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
.PendingIntent
;
19 import android
.app
.Service
;
20 import android
.content
.Intent
;
21 import android
.net
.Uri
;
22 import android
.os
.Handler
;
23 import android
.os
.HandlerThread
;
24 import android
.os
.IBinder
;
25 import android
.os
.Looper
;
26 import android
.os
.Message
;
27 import android
.os
.Process
;
28 import android
.util
.Log
;
29 import android
.webkit
.MimeTypeMap
;
30 import android
.widget
.RemoteViews
;
31 import android
.widget
.Toast
;
33 public class FileUploader
extends Service
implements OnDatatransferProgressListener
{
35 public static final String UPLOAD_FINISH_MESSAGE
= "UPLOAD_FINISH";
36 public static final String EXTRA_PARENT_DIR_ID
= "PARENT_DIR_ID";
38 public static final String KEY_LOCAL_FILE
= "LOCAL_FILE";
39 public static final String KEY_REMOTE_FILE
= "REMOTE_FILE";
40 public static final String KEY_ACCOUNT
= "ACCOUNT";
41 public static final String KEY_UPLOAD_TYPE
= "UPLOAD_TYPE";
43 public static final int UPLOAD_SINGLE_FILE
= 0;
44 public static final int UPLOAD_MULTIPLE_FILES
= 1;
46 private static final String TAG
= "FileUploader";
47 private NotificationManager mNotificationManager
;
48 private Looper mServiceLooper
;
49 private ServiceHandler mServiceHandler
;
50 private AccountManager mAccountManager
;
51 private Account mAccount
;
52 private String
[] mLocalPaths
, mRemotePaths
;
53 private int mUploadType
;
54 private Notification mNotification
;
55 private int mTotalDataToSend
, mSendData
;
56 private int mCurrentIndexUpload
, mPreviousPercent
;
57 private int mSuccessCounter
;
60 public IBinder
onBind(Intent arg0
) {
64 private final class ServiceHandler
extends Handler
{
65 public ServiceHandler(Looper looper
) {
70 public void handleMessage(Message msg
) {
77 public void onCreate() {
79 mNotificationManager
= (NotificationManager
) getSystemService(NOTIFICATION_SERVICE
);
80 HandlerThread thread
= new HandlerThread("FileUploaderThread",
81 Process
.THREAD_PRIORITY_BACKGROUND
);
83 mServiceLooper
= thread
.getLooper();
84 mServiceHandler
= new ServiceHandler(mServiceLooper
);
85 mAccountManager
= AccountManager
.get(this);
89 public int onStartCommand(Intent intent
, int flags
, int startId
) {
90 if (!intent
.hasExtra(KEY_ACCOUNT
) && !intent
.hasExtra(KEY_UPLOAD_TYPE
)) {
91 Log
.e(TAG
, "Not enought data in intent provided");
92 return Service
.START_NOT_STICKY
;
94 mAccount
= intent
.getParcelableExtra(KEY_ACCOUNT
);
95 mUploadType
= intent
.getIntExtra(KEY_UPLOAD_TYPE
, -1);
96 if (mUploadType
== -1) {
97 Log
.e(TAG
, "Incorrect upload type provided");
98 return Service
.START_NOT_STICKY
;
100 if (mUploadType
== UPLOAD_SINGLE_FILE
) {
101 mLocalPaths
= new String
[] { intent
.getStringExtra(KEY_LOCAL_FILE
) };
102 mRemotePaths
= new String
[] { intent
103 .getStringExtra(KEY_REMOTE_FILE
) };
104 } else { // mUploadType == UPLOAD_MULTIPLE_FILES
105 mLocalPaths
= intent
.getStringArrayExtra(KEY_LOCAL_FILE
);
106 mRemotePaths
= intent
.getStringArrayExtra(KEY_REMOTE_FILE
);
109 for (int i
= 0; i
< mRemotePaths
.length
; ++i
)
110 mRemotePaths
[i
] = mRemotePaths
[i
].replace(' ', '+');
112 if (mLocalPaths
.length
!= mRemotePaths
.length
) {
113 Log
.e(TAG
, "Remote paths and local paths are not equal!");
114 return Service
.START_NOT_STICKY
;
117 Message msg
= mServiceHandler
.obtainMessage();
119 mServiceHandler
.sendMessage(msg
);
121 return Service
.START_NOT_STICKY
;
126 if (mSuccessCounter
== mLocalPaths
.length
) {
127 message
= getString(R
.string
.uploader_upload_succeed
);
129 message
= getString(R
.string
.uploader_upload_failed
);
130 if (mLocalPaths
.length
> 1)
131 message
+= " (" + mSuccessCounter
+ " / " + mLocalPaths
.length
+ getString(R
.string
.uploader_files_uploaded_suffix
) + ")";
132 Toast
.makeText(this, "Upload could not be completed", Toast
.LENGTH_SHORT
).show();
134 Toast
.makeText(this, message
, Toast
.LENGTH_SHORT
).show();
137 public void uploadFile() {
138 String baseUrl
= mAccountManager
.getUserData(mAccount
,
139 AccountAuthenticator
.KEY_OC_BASE_URL
), ocVerStr
= mAccountManager
140 .getUserData(mAccount
, AccountAuthenticator
.KEY_OC_VERSION
);
141 OwnCloudVersion ocVer
= new OwnCloudVersion(ocVerStr
);
142 String webdav_path
= AccountUtils
.getWebdavPath(ocVer
);
143 Uri ocUri
= Uri
.parse(baseUrl
+ webdav_path
);
144 String username
= mAccount
.name
.substring(0,
145 mAccount
.name
.lastIndexOf('@'));
146 String password
= mAccountManager
.getPassword(mAccount
);
147 FileDataStorageManager storageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
149 mTotalDataToSend
= mSendData
= mPreviousPercent
= 0;
151 mNotification
= new Notification(
152 eu
.alefzero
.owncloud
.R
.drawable
.icon
, "Uploading...",
153 System
.currentTimeMillis());
154 mNotification
.flags
|= Notification
.FLAG_ONGOING_EVENT
;
155 mNotification
.contentView
= new RemoteViews(getApplicationContext().getPackageName(), R
.layout
.progressbar_layout
);
156 mNotification
.contentView
.setProgressBar(R
.id
.status_progress
, 100, 0, false
);
157 mNotification
.contentView
.setImageViewResource(R
.id
.status_icon
, R
.drawable
.icon
);
158 // dvelasco ; contentIntent MUST be assigned to avoid app crashes in versions previous to Android 4.x ;
159 // BUT an empty Intent is not a very elegant solution; something smart should happen when a user 'clicks' on an upload in the notification bar
160 mNotification
.contentIntent
= PendingIntent
.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent
.FLAG_UPDATE_CURRENT
);
162 mNotificationManager
.notify(42, mNotification
);
164 WebdavClient wc
= new WebdavClient(ocUri
);
165 wc
.allowSelfsignedCertificates();
166 wc
.setDataTransferProgressListener(this);
167 wc
.setCredentials(username
, password
);
169 for (int i
= 0; i
< mLocalPaths
.length
; ++i
) {
170 File f
= new File(mLocalPaths
[i
]);
171 mTotalDataToSend
+= f
.length();
174 Log
.d(TAG
, "Will upload " + mTotalDataToSend
+ " bytes, with " + mLocalPaths
.length
+ " files");
178 for (int i
= 0; i
< mLocalPaths
.length
; ++i
) {
182 mimeType
= MimeTypeMap
.getSingleton()
183 .getMimeTypeFromExtension(
184 mLocalPaths
[i
].substring(mLocalPaths
[i
]
185 .lastIndexOf('.') + 1));
186 } catch (IndexOutOfBoundsException e
) {
187 Log
.e(TAG
, "Trying to find out MIME type of a file without extension: " + mLocalPaths
[i
]);
188 mimeType
= "application/octet-stream";
191 mCurrentIndexUpload
= i
;
192 if (wc
.putFile(mLocalPaths
[i
], mRemotePaths
[i
], mimeType
)) {
194 OCFile new_file
= new OCFile(mRemotePaths
[i
]);
195 new_file
.setMimetype(mimeType
);
196 new_file
.setFileLength(new File(mLocalPaths
[i
]).length());
197 new_file
.setModificationTimestamp(System
.currentTimeMillis());
198 new_file
.setLastSyncDate(0);
199 new_file
.setStoragePath(mLocalPaths
[i
]);
200 File f
= new File(mRemotePaths
[i
]);
201 long parentDirId
= storageManager
.getFileByPath(f
.getParent().endsWith("/")?f
.getParent():f
.getParent()+"/").getFileId();
202 new_file
.setParentId(parentDirId
);
203 storageManager
.saveFile(new_file
);
205 Intent end
= new Intent(UPLOAD_FINISH_MESSAGE
);
206 end
.putExtra(EXTRA_PARENT_DIR_ID
, parentDirId
);
211 mNotificationManager
.cancel(42);
216 public void transferProgress(long progressRate
) {
217 mSendData
+= progressRate
;
218 int percent
= (int)(100*((double)mSendData
)/((double)mTotalDataToSend
));
219 if (percent
!= mPreviousPercent
) {
220 String text
= String
.format("%d%% Uploading %s file", percent
, new File(mLocalPaths
[mCurrentIndexUpload
]).getName());
221 mNotification
.contentView
.setProgressBar(R
.id
.status_progress
, 100, percent
, false
);
222 mNotification
.contentView
.setTextViewText(R
.id
.status_text
, text
);
223 mNotificationManager
.notify(42, mNotification
);
225 mPreviousPercent
= percent
;