7d3605e45950505562f24688c432f3b0c0168bba
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
;
17 import eu
.alefzero
.owncloud
.ui
.FileDisplayActivity
;
19 public class FileDownloader
extends Service
{
20 static final String EXTRA_ACCOUNT
= "ACCOUNT";
21 static final String EXTRA_FILE_PATH
= "FILE_PATH";
22 static final String TAG
= "OC_FileDownloader";
24 NotificationManager nm
;
27 public IBinder
onBind(Intent arg0
) {
32 public int onStartCommand(Intent intent
, int flags
, int startId
) {
33 if (!intent
.hasExtra(EXTRA_ACCOUNT
) && !intent
.hasExtra(EXTRA_FILE_PATH
)) {
34 Log
.e(TAG
, "Not enough information provided in intent");
35 return START_NOT_STICKY
;
38 nm
= (NotificationManager
)getSystemService(NOTIFICATION_SERVICE
);
40 Account account
= intent
.getParcelableExtra(EXTRA_ACCOUNT
);
41 String file_path
= intent
.getStringExtra(EXTRA_FILE_PATH
);
42 AccountManager am
= (AccountManager
)getSystemService(ACCOUNT_SERVICE
);
43 Uri oc_url
= Uri
.parse(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_URL
));
45 WebdavClient wdc
= new WebdavClient(oc_url
);
47 String username
= account
.name
.split("@")[0];
50 password
= am
.blockingGetAuthToken(account
, AccountAuthenticator
.AUTH_TOKEN_TYPE
, true
);
51 } catch (Exception e
) {
52 // TODO Auto-generated catch block
54 return START_NOT_STICKY
;
57 wdc
.setCredentials(username
, password
);
58 wdc
.allowUnsignedCertificates();
60 Notification n
= new Notification(R
.drawable
.icon
, "Downloading file", System
.currentTimeMillis());
61 PendingIntent pi
= PendingIntent
.getActivity(this, 1, new Intent(this, FileDisplayActivity
.class), 0);
62 n
.setLatestEventInfo(this, "A", "B", pi
);
65 File sdCard
= Environment
.getExternalStorageDirectory();
66 File dir
= new File (sdCard
.getAbsolutePath() + "/owncloud");
68 File file
= new File(dir
, file_path
.replace('/', '.'));
70 wdc
.downloadFile(file_path
, file
);
72 return START_NOT_STICKY
;