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
.utils
.OwnCloudVersion
;
9 import eu
.alefzero
.webdav
.OnUploadProgressListener
;
10 import eu
.alefzero
.webdav
.WebdavClient
;
11 import android
.accounts
.Account
;
12 import android
.accounts
.AccountManager
;
13 import android
.app
.Notification
;
14 import android
.app
.NotificationManager
;
15 import android
.app
.Service
;
16 import android
.content
.Intent
;
17 import android
.net
.Uri
;
18 import android
.os
.Handler
;
19 import android
.os
.HandlerThread
;
20 import android
.os
.IBinder
;
21 import android
.os
.Looper
;
22 import android
.os
.Message
;
23 import android
.os
.Process
;
24 import android
.util
.Log
;
25 import android
.webkit
.MimeTypeMap
;
26 import android
.widget
.RemoteViews
;
27 import android
.widget
.Toast
;
29 public class FileUploader
extends Service
implements OnUploadProgressListener
{
31 public static final String KEY_LOCAL_FILE
= "LOCAL_FILE";
32 public static final String KEY_REMOTE_FILE
= "REMOTE_FILE";
33 public static final String KEY_ACCOUNT
= "ACCOUNT";
34 public static final String KEY_UPLOAD_TYPE
= "UPLOAD_TYPE";
36 public static final int UPLOAD_SINGLE_FILE
= 0;
37 public static final int UPLOAD_MULTIPLE_FILES
= 1;
39 private static final String TAG
= "FileUploader";
40 private NotificationManager mNotificationManager
;
41 private Looper mServiceLooper
;
42 private ServiceHandler mServiceHandler
;
43 private AccountManager mAccountManager
;
44 private Account mAccount
;
45 private String
[] mLocalPaths
, mRemotePaths
;
46 private boolean mResult
;
47 private int mUploadType
;
48 private Notification mNotification
;
49 private int mTotalDataToSend
, mSendData
;
50 private int mCurrentIndexUpload
, mPreviousPercent
;
53 public IBinder
onBind(Intent arg0
) {
57 private final class ServiceHandler
extends Handler
{
58 public ServiceHandler(Looper looper
) {
63 public void handleMessage(Message msg
) {
70 public void onCreate() {
72 mNotificationManager
= (NotificationManager
) getSystemService(NOTIFICATION_SERVICE
);
73 HandlerThread thread
= new HandlerThread("FileUploaderThread",
74 Process
.THREAD_PRIORITY_BACKGROUND
);
76 mServiceLooper
= thread
.getLooper();
77 mServiceHandler
= new ServiceHandler(mServiceLooper
);
78 mAccountManager
= AccountManager
.get(this);
82 public int onStartCommand(Intent intent
, int flags
, int startId
) {
83 if (!intent
.hasExtra(KEY_ACCOUNT
) && !intent
.hasExtra(KEY_UPLOAD_TYPE
)) {
84 Log
.e(TAG
, "Not enought data in intent provided");
85 return Service
.START_NOT_STICKY
;
87 mAccount
= intent
.getParcelableExtra(KEY_ACCOUNT
);
88 mUploadType
= intent
.getIntExtra(KEY_UPLOAD_TYPE
, -1);
89 if (mUploadType
== -1) {
90 Log
.e(TAG
, "Incorrect upload type provided");
91 return Service
.START_NOT_STICKY
;
93 if (mUploadType
== UPLOAD_SINGLE_FILE
) {
94 mLocalPaths
= new String
[] { intent
.getStringExtra(KEY_LOCAL_FILE
) };
95 mRemotePaths
= new String
[] { intent
96 .getStringExtra(KEY_REMOTE_FILE
) };
97 } else { // mUploadType == UPLOAD_MULTIPLE_FILES
98 mLocalPaths
= intent
.getStringArrayExtra(KEY_LOCAL_FILE
);
99 mRemotePaths
= intent
.getStringArrayExtra(KEY_REMOTE_FILE
);
102 for (int i
= 0; i
< mRemotePaths
.length
; ++i
)
103 mRemotePaths
[i
] = mRemotePaths
[i
].replace(' ', '+');
105 if (mLocalPaths
.length
!= mRemotePaths
.length
) {
106 Log
.e(TAG
, "Remote paths and local paths are not equal!");
107 return Service
.START_NOT_STICKY
;
110 Message msg
= mServiceHandler
.obtainMessage();
112 mServiceHandler
.sendMessage(msg
);
114 return Service
.START_NOT_STICKY
;
119 Toast
.makeText(this, "Upload successfull", Toast
.LENGTH_SHORT
)
122 Toast
.makeText(this, "No i kupa", Toast
.LENGTH_SHORT
).show();
126 public void uploadFile() {
127 String baseUrl
= mAccountManager
.getUserData(mAccount
,
128 AccountAuthenticator
.KEY_OC_BASE_URL
), ocVerStr
= mAccountManager
129 .getUserData(mAccount
, AccountAuthenticator
.KEY_OC_VERSION
);
130 OwnCloudVersion ocVer
= new OwnCloudVersion(ocVerStr
);
131 String webdav_path
= AccountUtils
.getWebdavPath(ocVer
);
132 Uri ocUri
= Uri
.parse(baseUrl
+ webdav_path
);
133 String username
= mAccount
.name
.substring(0,
134 mAccount
.name
.lastIndexOf('@'));
135 String password
= mAccountManager
.getPassword(mAccount
);
137 mTotalDataToSend
= mSendData
= mPreviousPercent
= 0;
139 mNotification
= new Notification(
140 eu
.alefzero
.owncloud
.R
.drawable
.icon
, "Uploading...",
141 System
.currentTimeMillis());
142 mNotification
.flags
|= Notification
.FLAG_ONGOING_EVENT
;
143 mNotification
.contentView
= new RemoteViews(getApplicationContext().getPackageName(), R
.layout
.progressbar_layout
);
144 mNotification
.contentView
.setProgressBar(R
.id
.status_progress
, 100, 0, false
);
145 mNotification
.contentView
.setImageViewResource(R
.id
.status_icon
, R
.drawable
.icon
);
147 mNotificationManager
.notify(42, mNotification
);
149 WebdavClient wc
= new WebdavClient(ocUri
);
150 wc
.allowUnsignedCertificates();
151 wc
.setUploadListener(this);
152 wc
.setCredentials(username
, password
);
154 for (int i
= 0; i
< mLocalPaths
.length
; ++i
) {
155 File f
= new File(mLocalPaths
[i
]);
156 mTotalDataToSend
+= f
.length();
159 Log
.d(TAG
, "Will upload " + mTotalDataToSend
+ " bytes, with " + mLocalPaths
.length
+ " files");
161 for (int i
= 0; i
< mLocalPaths
.length
; ++i
) {
162 String mimeType
= MimeTypeMap
.getSingleton()
163 .getMimeTypeFromExtension(
164 mLocalPaths
[i
].substring(mLocalPaths
[i
]
165 .lastIndexOf('.') + 1));
167 mCurrentIndexUpload
= i
;
168 if (wc
.putFile(mLocalPaths
[i
], mRemotePaths
[i
], mimeType
)) {
172 // notification.contentView.setProgressBar(R.id.status_progress,
173 // mLocalPaths.length-1, mLocalPaths.length-1, false);
174 mNotificationManager
.cancel(42);
179 public void OnUploadProgress(long currentProgress
) {
180 mSendData
+= currentProgress
;
181 int percent
= (int)(100*mSendData
/mTotalDataToSend
);
182 if (percent
!= mPreviousPercent
) {
183 String text
= String
.format("%d%% Uploading %s file", percent
, new File(mLocalPaths
[mCurrentIndexUpload
]).getName());
184 mNotification
.contentView
.setProgressBar(R
.id
.status_progress
, 100, percent
, false
);
185 mNotification
.contentView
.setTextViewText(R
.id
.status_text
, text
);
186 mNotificationManager
.notify(42, mNotification
);
188 mPreviousPercent
= percent
;