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
) {
59 public void handleMessage(Message msg
) {
66 public void onCreate() {
68 mNotificationManager
= (NotificationManager
) getSystemService(NOTIFICATION_SERVICE
);
69 HandlerThread thread
= new HandlerThread("FileUploaderThread",
70 Process
.THREAD_PRIORITY_BACKGROUND
);
72 mServiceLooper
= thread
.getLooper();
73 mServiceHandler
= new ServiceHandler(mServiceLooper
);
74 mAccountManager
= AccountManager
.get(this);
78 public int onStartCommand(Intent intent
, int flags
, int startId
) {
79 if (!intent
.hasExtra(KEY_ACCOUNT
) && !intent
.hasExtra(KEY_UPLOAD_TYPE
)) {
80 Log
.e(TAG
, "Not enought data in intent provided");
81 return Service
.START_NOT_STICKY
;
83 mAccount
= intent
.getParcelableExtra(KEY_ACCOUNT
);
84 mUploadType
= intent
.getIntExtra(KEY_UPLOAD_TYPE
, -1);
85 if (mUploadType
== -1) {
86 Log
.e(TAG
, "Incorrect upload type provided");
87 return Service
.START_NOT_STICKY
;
89 if (mUploadType
== UPLOAD_SINGLE_FILE
) {
90 mLocalPaths
= new String
[] { intent
.getStringExtra(KEY_LOCAL_FILE
) };
91 mRemotePaths
= new String
[] { intent
92 .getStringExtra(KEY_REMOTE_FILE
) };
93 } else { // mUploadType == UPLOAD_MULTIPLE_FILES
94 mLocalPaths
= intent
.getStringArrayExtra(KEY_LOCAL_FILE
);
95 mRemotePaths
= intent
.getStringArrayExtra(KEY_REMOTE_FILE
);
98 for (int i
= 0; i
< mRemotePaths
.length
; ++i
)
99 mRemotePaths
[i
] = mRemotePaths
[i
].replace(' ', '+');
101 if (mLocalPaths
.length
!= mRemotePaths
.length
) {
102 Log
.e(TAG
, "Remote paths and local paths are not equal!");
103 return Service
.START_NOT_STICKY
;
106 Message msg
= mServiceHandler
.obtainMessage();
108 mServiceHandler
.sendMessage(msg
);
110 return Service
.START_NOT_STICKY
;
115 Toast
.makeText(this, "Upload successfull", Toast
.LENGTH_SHORT
)
118 Toast
.makeText(this, "No i kupa", Toast
.LENGTH_SHORT
).show();
122 public void uploadFile() {
123 String baseUrl
= mAccountManager
.getUserData(mAccount
,
124 AccountAuthenticator
.KEY_OC_BASE_URL
), ocVerStr
= mAccountManager
125 .getUserData(mAccount
, AccountAuthenticator
.KEY_OC_VERSION
);
126 OwnCloudVersion ocVer
= new OwnCloudVersion(ocVerStr
);
127 String webdav_path
= AccountUtils
.getWebdavPath(ocVer
);
128 Uri ocUri
= Uri
.parse(baseUrl
+ webdav_path
);
129 String username
= mAccount
.name
.substring(0,
130 mAccount
.name
.lastIndexOf('@'));
131 String password
= mAccountManager
.getPassword(mAccount
);
132 Notification notification
= new Notification(
133 eu
.alefzero
.owncloud
.R
.drawable
.icon
, "Uploading...",
134 System
.currentTimeMillis());
135 notification
.flags
|= Notification
.FLAG_ONGOING_EVENT
;
136 notification
.contentView
= new RemoteViews(getApplicationContext()
137 .getPackageName(), R
.layout
.progressbar_layout
);
138 notification
.contentView
.setProgressBar(R
.id
.status_progress
,
139 mLocalPaths
.length
- 1, 0, false
);
140 notification
.contentView
.setImageViewResource(R
.id
.status_icon
,
143 mNotificationManager
.notify(42, notification
);
145 WebdavClient wc
= new WebdavClient(ocUri
);
146 wc
.setCredentials(username
, password
);
148 for (int i
= 0; i
< mLocalPaths
.length
; ++i
) {
149 String mimeType
= MimeTypeMap
.getSingleton()
150 .getMimeTypeFromExtension(
151 mLocalPaths
[i
].substring(mLocalPaths
[i
]
152 .lastIndexOf('.') + 1));
154 if (wc
.putFile(mLocalPaths
[i
], mRemotePaths
[i
], mimeType
)) {
157 notification
.contentView
.setProgressBar(R
.id
.status_progress
,
158 mLocalPaths
.length
- 1, i
+ 1, false
);
160 mNotificationManager
.notify(42, notification
);
162 // notification.contentView.setProgressBar(R.id.status_progress,
163 // mLocalPaths.length-1, mLocalPaths.length-1, false);
164 mNotificationManager
.cancel(42);