1 package eu
.alefzero
.owncloud
;
5 import android
.accounts
.Account
;
6 import android
.accounts
.AccountManager
;
7 import android
.app
.Notification
;
8 import android
.app
.NotificationManager
;
9 import android
.app
.PendingIntent
;
10 import android
.app
.Service
;
11 import android
.content
.Intent
;
12 import android
.net
.Uri
;
13 import android
.os
.Environment
;
14 import android
.os
.IBinder
;
15 import android
.util
.Log
;
16 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
18 public class FileDownloader
extends Service
{
19 static final String EXTRA_ACCOUNT
= "ACCOUNT";
20 static final String EXTRA_FILE_PATH
= "FILE_PATH";
21 static final String TAG
= "OC_FileDownloader";
23 NotificationManager nm
;
26 public IBinder
onBind(Intent arg0
) {
31 public int onStartCommand(Intent intent
, int flags
, int startId
) {
32 if (!intent
.hasExtra(EXTRA_ACCOUNT
) && !intent
.hasExtra(EXTRA_FILE_PATH
)) {
33 Log
.e(TAG
, "Not enough information provided in intent");
34 return START_NOT_STICKY
;
37 nm
= (NotificationManager
)getSystemService(NOTIFICATION_SERVICE
);
39 Account account
= intent
.getParcelableExtra(EXTRA_ACCOUNT
);
40 String file_path
= intent
.getStringExtra(EXTRA_FILE_PATH
);
41 AccountManager am
= (AccountManager
)getSystemService(ACCOUNT_SERVICE
);
42 Uri oc_url
= Uri
.parse(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_URL
));
44 WebdavClient wdc
= new WebdavClient(oc_url
);
46 String username
= account
.name
.split("@")[0];
49 password
= am
.blockingGetAuthToken(account
, AccountAuthenticator
.AUTH_TOKEN_TYPE
, true
);
50 } catch (Exception e
) {
51 // TODO Auto-generated catch block
53 return START_NOT_STICKY
;
56 wdc
.setCredentials(username
, password
);
57 wdc
.allowUnsignedCertificates();
59 Notification n
= new Notification(R
.drawable
.icon
, "Downloading file", System
.currentTimeMillis());
60 PendingIntent pi
= PendingIntent
.getActivity(this, 1, new Intent(this, MainScreen
.class), 0);
61 n
.setLatestEventInfo(this, "A", "B", pi
);
64 File sdCard
= Environment
.getExternalStorageDirectory();
65 File dir
= new File (sdCard
.getAbsolutePath() + "/owncloud");
67 File file
= new File(dir
, file_path
.replace('/', '.'));
69 wdc
.downloadFile(file_path
, file
);
71 return START_NOT_STICKY
;