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 DefaultHttpClient client
= new DefaultHttpClient();
66 Log
.d(TAG
, oc_url
.toString());
67 HttpGet query
= new HttpGet(oc_url
+ file_path
);
68 query
.setHeader("Content-type", "text/xml");
69 query
.setHeader("User-Agent", "Android-ownCloud");
71 BasicHttpContext httpContext
= new BasicHttpContext();
72 BasicScheme basicAuth
= new BasicScheme();
73 httpContext
.setAttribute("preemptive-auth", basicAuth
);
75 String username
= account
.name
.split("@")[0];
78 password
= am
.blockingGetAuthToken(account
, AccountAuthenticator
.AUTH_TOKEN_TYPE
, true
);
79 } catch (OperationCanceledException e1
) {
80 // TODO Auto-generated catch block
82 } catch (AuthenticatorException e1
) {
83 // TODO Auto-generated catch block
85 } catch (IOException e1
) {
86 // TODO Auto-generated catch block
89 if (am
.getUserData(account
, AccountAuthenticator
.KEY_OC_URL
) == null
) {
93 client
.getCredentialsProvider().setCredentials(
94 new AuthScope(oc_url
.getHost(), oc_url
.getPort()==-1?
80:oc_url
.getPort()),
95 new UsernamePasswordCredentials(username
, password
)
98 HttpHost host
= new HttpHost(oc_url
.getHost(), oc_url
.getPort()==-1?
80:oc_url
.getPort());
100 Notification n
= new Notification(R
.drawable
.icon
, "Downloading file", System
.currentTimeMillis());
101 PendingIntent pi
= PendingIntent
.getActivity(this, 1, new Intent(this, OwnCloudMainScreen
.class), 0);
102 n
.setLatestEventInfo(this, "A", "B", pi
);
105 HttpResponse response
= null
;
107 response
= client
.execute(host
, query
, httpContext
);
108 } catch (ClientProtocolException e
) {
109 // TODO Auto-generated catch block
111 } catch (IOException e
) {
112 // TODO Auto-generated catch block
116 File sdCard
= Environment
.getExternalStorageDirectory();
117 File dir
= new File (sdCard
.getAbsolutePath() + "/owncloud");
119 File file
= new File(dir
, "filename");
122 FileOutputStream f
= new FileOutputStream(file
);
123 byte[] b
= new byte[(int)response
.getEntity().getContentLength()];
124 response
.getEntity().getContent().read(b
);
126 } catch (FileNotFoundException e
) {
127 // TODO Auto-generated catch block
129 } catch (IllegalStateException e
) {
130 // TODO Auto-generated catch block
132 } catch (IOException e
) {
133 // TODO Auto-generated catch block
138 return START_NOT_STICKY
;