1 package eu
.alefzero
.owncloud
;
4 import java
.io
.FileNotFoundException
;
5 import java
.io
.FileOutputStream
;
6 import java
.io
.IOException
;
7 import java
.io
.InputStreamReader
;
8 import java
.net
.UnknownHostException
;
11 import org
.apache
.http
.HttpHost
;
12 import org
.apache
.http
.HttpResponse
;
13 import org
.apache
.http
.auth
.AuthScope
;
14 import org
.apache
.http
.auth
.UsernamePasswordCredentials
;
15 import org
.apache
.http
.client
.ClientProtocolException
;
16 import org
.apache
.http
.client
.methods
.HttpGet
;
17 import org
.apache
.http
.impl
.auth
.BasicScheme
;
18 import org
.apache
.http
.impl
.client
.DefaultHttpClient
;
19 import org
.apache
.http
.protocol
.BasicHttpContext
;
21 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
22 import eu
.alefzero
.webdav
.HttpPropFind
;
24 import android
.accounts
.Account
;
25 import android
.accounts
.AccountManager
;
26 import android
.accounts
.AuthenticatorException
;
27 import android
.accounts
.OperationCanceledException
;
28 import android
.app
.Notification
;
29 import android
.app
.NotificationManager
;
30 import android
.app
.PendingIntent
;
31 import android
.app
.Service
;
32 import android
.content
.Intent
;
33 import android
.net
.Uri
;
34 import android
.os
.Environment
;
35 import android
.os
.IBinder
;
36 import android
.util
.Log
;
37 import android
.widget
.FrameLayout
;
39 public class FileDownloader
extends Service
{
40 static final String EXTRA_ACCOUNT
= "ACCOUNT";
41 static final String EXTRA_FILE_PATH
= "FILE_PATH";
42 static final String TAG
= "OC_FileDownloader";
44 NotificationManager nm
;
47 public IBinder
onBind(Intent arg0
) {
52 public int onStartCommand(Intent intent
, int flags
, int startId
) {
53 if (!intent
.hasExtra(EXTRA_ACCOUNT
) && !intent
.hasExtra(EXTRA_FILE_PATH
)) {
54 Log
.e(TAG
, "Not enough information provided in intent");
55 return START_NOT_STICKY
;
58 nm
= (NotificationManager
)getSystemService(NOTIFICATION_SERVICE
);
60 Account account
= intent
.getParcelableExtra(EXTRA_ACCOUNT
);
61 String file_path
= intent
.getStringExtra(EXTRA_FILE_PATH
);
62 AccountManager am
= (AccountManager
)getSystemService(ACCOUNT_SERVICE
);
63 Uri oc_url
= Uri
.parse(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_URL
));
65 WebdavClient wdc
= new WebdavClient(oc_url
);
67 String username
= account
.name
.split("@")[0];
70 password
= am
.blockingGetAuthToken(account
, AccountAuthenticator
.AUTH_TOKEN_TYPE
, true
);
71 } catch (Exception e
) {
72 // TODO Auto-generated catch block
74 return START_NOT_STICKY
;
77 wdc
.setCredentials(username
, password
);
78 wdc
.allowUnsignedCertificates();
80 Notification n
= new Notification(R
.drawable
.icon
, "Downloading file", System
.currentTimeMillis());
81 PendingIntent pi
= PendingIntent
.getActivity(this, 1, new Intent(this, OwnCloudMainScreen
.class), 0);
82 n
.setLatestEventInfo(this, "A", "B", pi
);
85 File sdCard
= Environment
.getExternalStorageDirectory();
86 File dir
= new File (sdCard
.getAbsolutePath() + "/owncloud");
88 File file
= new File(dir
, "filename");
90 wdc
.downloadFile(file_path
, file
);
92 return START_NOT_STICKY
;