1 package eu
.alefzero
.owncloud
.files
.services
;
3 import java
.net
.URLEncoder
;
5 import eu
.alefzero
.owncloud
.AccountUtils
;
6 import eu
.alefzero
.owncloud
.R
;
7 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
8 import eu
.alefzero
.owncloud
.utils
.OwnCloudVersion
;
9 import eu
.alefzero
.webdav
.WebdavClient
;
10 import android
.accounts
.Account
;
11 import android
.accounts
.AccountManager
;
12 import android
.app
.Notification
;
13 import android
.app
.NotificationManager
;
14 import android
.app
.Service
;
15 import android
.content
.Intent
;
16 import android
.net
.Uri
;
17 import android
.os
.Handler
;
18 import android
.os
.HandlerThread
;
19 import android
.os
.IBinder
;
20 import android
.os
.Looper
;
21 import android
.os
.Message
;
22 import android
.os
.Process
;
23 import android
.util
.Log
;
24 import android
.webkit
.MimeTypeMap
;
25 import android
.widget
.RemoteViews
;
26 import android
.widget
.Toast
;
28 public class FileUploader
extends Service
{
30 public static final String KEY_LOCAL_FILE
= "LOCAL_FILE";
31 public static final String KEY_REMOTE_FILE
= "REMOTE_FILE";
32 public static final String KEY_ACCOUNT
= "ACCOUNT";
33 public static final String KEY_UPLOAD_TYPE
= "UPLOAD_TYPE";
35 public static final int UPLOAD_SINGLE_FILE
= 0;
36 public static final int UPLOAD_MULTIPLE_FILES
= 1;
38 private static final String TAG
= "FileUploader";
39 private NotificationManager mNotificationManager
;
40 private Looper mServiceLooper
;
41 private ServiceHandler mServiceHandler
;
42 private AccountManager mAccountManager
;
43 private Account mAccount
;
44 private String
[] mLocalPaths
, mRemotePaths
;
45 private boolean mResult
;
46 private int mUploadType
;
49 public IBinder
onBind(Intent arg0
) {
53 private final class ServiceHandler
extends Handler
{
54 public ServiceHandler(Looper looper
) {
58 public void handleMessage(Message msg
) {
65 public void onCreate() {
67 mNotificationManager
= (NotificationManager
) getSystemService(NOTIFICATION_SERVICE
);
68 HandlerThread thread
= new HandlerThread("FileUploaderThread", Process
.THREAD_PRIORITY_BACKGROUND
);
70 mServiceLooper
= thread
.getLooper();
71 mServiceHandler
= new ServiceHandler(mServiceLooper
);
72 mAccountManager
= AccountManager
.get(this);
76 public int onStartCommand(Intent intent
, int flags
, int startId
) {
77 if (!intent
.hasExtra(KEY_ACCOUNT
) && !intent
.hasExtra(KEY_UPLOAD_TYPE
)) {
78 Log
.e(TAG
, "Not enought data in intent provided");
79 return Service
.START_NOT_STICKY
;
81 mAccount
= intent
.getParcelableExtra(KEY_ACCOUNT
);
82 mUploadType
= intent
.getIntExtra(KEY_UPLOAD_TYPE
, -1);
83 if (mUploadType
== -1) {
84 Log
.e(TAG
, "Incorrect upload type provided");
85 return Service
.START_NOT_STICKY
;
87 if (mUploadType
== UPLOAD_SINGLE_FILE
) {
88 mLocalPaths
= new String
[] { intent
.getStringExtra(KEY_LOCAL_FILE
) };
89 mRemotePaths
= new String
[] { intent
.getStringExtra(KEY_REMOTE_FILE
) };
90 } else { // mUploadType == UPLOAD_MULTIPLE_FILES
91 mLocalPaths
= intent
.getStringArrayExtra(KEY_LOCAL_FILE
);
92 mRemotePaths
= intent
.getStringArrayExtra(KEY_REMOTE_FILE
);
95 for (int i
= 0; i
< mRemotePaths
.length
; ++i
)
96 mRemotePaths
[i
] = mRemotePaths
[i
].replace(' ', '+');
98 if (mLocalPaths
.length
!= mRemotePaths
.length
) {
99 Log
.e(TAG
, "Remote paths and local paths are not equal!");
100 return Service
.START_NOT_STICKY
;
103 Message msg
= mServiceHandler
.obtainMessage();
105 mServiceHandler
.sendMessage(msg
);
107 return Service
.START_NOT_STICKY
;
112 Toast
.makeText(this, "Upload successfull", Toast
.LENGTH_SHORT
).show();
114 Toast
.makeText(this, "No i kupa", Toast
.LENGTH_SHORT
).show();
118 public void uploadFile() {
119 String baseUrl
= mAccountManager
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_BASE_URL
),
120 ocVerStr
= mAccountManager
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_VERSION
);
121 OwnCloudVersion ocVer
= new OwnCloudVersion(ocVerStr
);
122 String webdav_path
= AccountUtils
.getWebdavPath(ocVer
);
123 Uri ocUri
= Uri
.parse(baseUrl
+ webdav_path
);
124 String username
= mAccount
.name
.substring(0, mAccount
.name
.lastIndexOf('@'));
125 String password
= mAccountManager
.getPassword(mAccount
);
126 Notification notification
= new Notification(eu
.alefzero
.owncloud
.R
.drawable
.icon
,
127 "Uploading...", System
.currentTimeMillis());
128 notification
.flags
|= Notification
.FLAG_ONGOING_EVENT
;
129 notification
.contentView
= new RemoteViews(getApplicationContext().getPackageName(),
130 R
.layout
.progressbar_layout
);
131 notification
.contentView
.setProgressBar(R
.id
.status_progress
, mLocalPaths
.length
-1, 0, false
);
132 notification
.contentView
.setImageViewResource(R
.id
.status_icon
, R
.drawable
.icon
);
134 mNotificationManager
.notify(42, notification
);
136 WebdavClient wc
= new WebdavClient(ocUri
);
137 wc
.setCredentials(username
, password
);
139 for (int i
= 0; i
< mLocalPaths
.length
; ++i
) {
140 String mimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(mLocalPaths
[i
].substring(mLocalPaths
[i
].lastIndexOf('.')+1));
142 if (wc
.putFile(mLocalPaths
[i
], mRemotePaths
[i
], mimeType
)) {
145 notification
.contentView
.setProgressBar(R
.id
.status_progress
, mLocalPaths
.length
-1, i
+1, false
);
147 mNotificationManager
.notify(42, notification
);
149 //notification.contentView.setProgressBar(R.id.status_progress, mLocalPaths.length-1, mLocalPaths.length-1, false);
150 mNotificationManager
.cancel(42);