1 package eu
.alefzero
.owncloud
;
4 import java
.net
.URLEncoder
;
6 import android
.accounts
.Account
;
7 import android
.accounts
.AccountManager
;
8 import android
.app
.Notification
;
9 import android
.app
.NotificationManager
;
10 import android
.app
.PendingIntent
;
11 import android
.app
.Service
;
12 import android
.content
.ContentValues
;
13 import android
.content
.Intent
;
14 import android
.net
.Uri
;
15 import android
.os
.Environment
;
16 import android
.os
.Handler
;
17 import android
.os
.HandlerThread
;
18 import android
.os
.IBinder
;
19 import android
.os
.Looper
;
20 import android
.os
.Message
;
21 import android
.os
.Process
;
22 import android
.util
.Log
;
23 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
24 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
25 import eu
.alefzero
.owncloud
.ui
.activity
.FileDisplayActivity
;
26 import eu
.alefzero
.owncloud
.utils
.OwnCloudVersion
;
27 import eu
.alefzero
.webdav
.WebdavClient
;
29 public class FileDownloader
extends Service
{
30 public static final String DOWNLOAD_FINISH_MESSAGE
= "DOWNLOAD_FINISH";
31 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
32 public static final String EXTRA_FILE_PATH
= "FILE_PATH";
33 private static final String TAG
= "FileDownloader";
35 private NotificationManager nm
;
36 private Looper mServiceLooper
;
37 private ServiceHandler mServiceHandler
;
38 private Account mAccount
;
39 private String mFilePath
;
41 private final class ServiceHandler
extends Handler
{
42 public ServiceHandler(Looper looper
) {
47 public void handleMessage(Message msg
) {
54 public void onCreate() {
56 nm
= (NotificationManager
) getSystemService(NOTIFICATION_SERVICE
);
57 HandlerThread thread
= new HandlerThread("FileDownladerThread",
58 Process
.THREAD_PRIORITY_BACKGROUND
);
60 mServiceLooper
= thread
.getLooper();
61 mServiceHandler
= new ServiceHandler(mServiceLooper
);
65 public IBinder
onBind(Intent arg0
) {
70 public int onStartCommand(Intent intent
, int flags
, int startId
) {
71 if (!intent
.hasExtra(EXTRA_ACCOUNT
)
72 && !intent
.hasExtra(EXTRA_FILE_PATH
)) {
73 Log
.e(TAG
, "Not enough information provided in intent");
76 mAccount
= intent
.getParcelableExtra(EXTRA_ACCOUNT
);
77 mFilePath
= intent
.getStringExtra(EXTRA_FILE_PATH
);
78 Message msg
= mServiceHandler
.obtainMessage();
80 mServiceHandler
.sendMessage(msg
);
82 return START_NOT_STICKY
;
86 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
87 String oc_base_url
= am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_BASE_URL
);
88 OwnCloudVersion ocv
= new OwnCloudVersion(am
89 .getUserData(mAccount
, AccountAuthenticator
.KEY_OC_VERSION
));
90 String webdav_path
= AccountUtils
.getWebdavPath(ocv
);
91 Uri oc_url
= Uri
.parse(oc_base_url
+webdav_path
);
93 WebdavClient wdc
= new WebdavClient(Uri
.parse(oc_base_url
+ webdav_path
));
95 String username
= mAccount
.name
.split("@")[0];
98 password
= am
.blockingGetAuthToken(mAccount
,
99 AccountAuthenticator
.AUTH_TOKEN_TYPE
, true
);
100 } catch (Exception e
) {
105 wdc
.setCredentials(username
, password
);
106 wdc
.allowUnsignedCertificates();
108 Notification n
= new Notification(R
.drawable
.icon
, "Downloading file",
109 System
.currentTimeMillis());
110 PendingIntent pi
= PendingIntent
.getActivity(this, 1, new Intent(this,
111 FileDisplayActivity
.class), 0);
112 n
.setLatestEventInfo(this, "Downloading file", "Downloading file "
116 File sdCard
= Environment
.getExternalStorageDirectory();
117 File dir
= new File(sdCard
.getAbsolutePath() + "/owncloud");
119 File file
= new File(dir
, mFilePath
.replace('/', '.'));
121 Log
.e(TAG
, file
.getAbsolutePath() + " " + oc_url
.toString());
122 Log
.e(TAG
, mFilePath
+"");
123 if (wdc
.downloadFile(mFilePath
, file
)) {
124 ContentValues cv
= new ContentValues();
125 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getAbsolutePath());
126 getContentResolver().update(
127 ProviderTableMeta
.CONTENT_URI
,
129 ProviderTableMeta
.FILE_NAME
+ "=? AND "
130 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
132 mFilePath
.substring(mFilePath
.lastIndexOf('/') + 1),
136 Intent end
= new Intent(DOWNLOAD_FINISH_MESSAGE
);