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
.ContentValues
;
12 import android
.content
.Intent
;
13 import android
.net
.Uri
;
14 import android
.os
.Environment
;
15 import android
.os
.Handler
;
16 import android
.os
.HandlerThread
;
17 import android
.os
.IBinder
;
18 import android
.os
.Looper
;
19 import android
.os
.Message
;
20 import android
.os
.Process
;
21 import android
.util
.Log
;
22 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
23 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
24 import eu
.alefzero
.owncloud
.ui
.activity
.FileDisplayActivity
;
25 import eu
.alefzero
.webdav
.WebdavClient
;
27 public class FileDownloader
extends Service
{
28 public static final String DOWNLOAD_FINISH_MESSAGE
= "DOWNLOAD_FINISH";
29 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
30 public static final String EXTRA_FILE_PATH
= "FILE_PATH";
31 private static final String TAG
= "FileDownloader";
33 private NotificationManager nm
;
34 private Looper mServiceLooper
;
35 private ServiceHandler mServiceHandler
;
36 private Account mAccount
;
37 private String mFilePath
;
39 private final class ServiceHandler
extends Handler
{
40 public ServiceHandler(Looper looper
) {
45 public void handleMessage(Message msg
) {
52 public void onCreate() {
54 nm
= (NotificationManager
) getSystemService(NOTIFICATION_SERVICE
);
55 HandlerThread thread
= new HandlerThread("FileDownladerThread",
56 Process
.THREAD_PRIORITY_BACKGROUND
);
58 mServiceLooper
= thread
.getLooper();
59 mServiceHandler
= new ServiceHandler(mServiceLooper
);
63 public IBinder
onBind(Intent arg0
) {
68 public int onStartCommand(Intent intent
, int flags
, int startId
) {
69 if (!intent
.hasExtra(EXTRA_ACCOUNT
)
70 && !intent
.hasExtra(EXTRA_FILE_PATH
)) {
71 Log
.e(TAG
, "Not enough information provided in intent");
74 mAccount
= intent
.getParcelableExtra(EXTRA_ACCOUNT
);
75 mFilePath
= intent
.getStringExtra(EXTRA_FILE_PATH
);
76 Message msg
= mServiceHandler
.obtainMessage();
78 mServiceHandler
.sendMessage(msg
);
80 return START_NOT_STICKY
;
84 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
85 Uri oc_url
= Uri
.parse(am
.getUserData(mAccount
,
86 AccountAuthenticator
.KEY_OC_URL
));
88 WebdavClient wdc
= new WebdavClient(oc_url
);
90 String username
= mAccount
.name
.split("@")[0];
93 password
= am
.blockingGetAuthToken(mAccount
,
94 AccountAuthenticator
.AUTH_TOKEN_TYPE
, true
);
95 } catch (Exception e
) {
99 wdc
.setCredentials(username
, password
);
100 wdc
.allowUnsignedCertificates();
102 Notification n
= new Notification(R
.drawable
.icon
, "Downloading file",
103 System
.currentTimeMillis());
104 PendingIntent pi
= PendingIntent
.getActivity(this, 1, new Intent(this,
105 FileDisplayActivity
.class), 0);
106 n
.setLatestEventInfo(this, "Downloading file", "Downloading file "
110 File sdCard
= Environment
.getExternalStorageDirectory();
111 File dir
= new File(sdCard
.getAbsolutePath() + "/owncloud");
113 File file
= new File(dir
, mFilePath
.replace('/', '.'));
115 Log
.e(TAG
, file
.getAbsolutePath() + " " + oc_url
.toString());
116 wdc
.downloadFile(mFilePath
, file
);
117 ContentValues cv
= new ContentValues();
118 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getAbsolutePath());
119 getContentResolver().update(
120 ProviderTableMeta
.CONTENT_URI
,
122 ProviderTableMeta
.FILE_NAME
+ "=? AND "
123 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
125 mFilePath
.substring(mFilePath
.lastIndexOf('/') + 1),
128 Intent end
= new Intent(DOWNLOAD_FINISH_MESSAGE
);