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
.owncloud
.utils
.OwnCloudVersion
;
26 import eu
.alefzero
.webdav
.WebdavClient
;
28 public class FileDownloader
extends Service
{
29 public static final String DOWNLOAD_FINISH_MESSAGE
= "DOWNLOAD_FINISH";
30 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
31 public static final String EXTRA_FILE_PATH
= "FILE_PATH";
32 private static final String TAG
= "FileDownloader";
34 private NotificationManager nm
;
35 private Looper mServiceLooper
;
36 private ServiceHandler mServiceHandler
;
37 private Account mAccount
;
38 private String mFilePath
;
40 private final class ServiceHandler
extends Handler
{
41 public ServiceHandler(Looper looper
) {
46 public void handleMessage(Message msg
) {
53 public void onCreate() {
55 nm
= (NotificationManager
) getSystemService(NOTIFICATION_SERVICE
);
56 HandlerThread thread
= new HandlerThread("FileDownladerThread",
57 Process
.THREAD_PRIORITY_BACKGROUND
);
59 mServiceLooper
= thread
.getLooper();
60 mServiceHandler
= new ServiceHandler(mServiceLooper
);
64 public IBinder
onBind(Intent arg0
) {
69 public int onStartCommand(Intent intent
, int flags
, int startId
) {
70 if (!intent
.hasExtra(EXTRA_ACCOUNT
)
71 && !intent
.hasExtra(EXTRA_FILE_PATH
)) {
72 Log
.e(TAG
, "Not enough information provided in intent");
75 mAccount
= intent
.getParcelableExtra(EXTRA_ACCOUNT
);
76 mFilePath
= intent
.getStringExtra(EXTRA_FILE_PATH
);
77 Message msg
= mServiceHandler
.obtainMessage();
79 mServiceHandler
.sendMessage(msg
);
81 return START_NOT_STICKY
;
85 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
86 String oc_base_url
= am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_BASE_URL
);
87 OwnCloudVersion ocv
= new OwnCloudVersion(am
88 .getUserData(mAccount
, AccountAuthenticator
.KEY_OC_VERSION
));
89 String webdav_path
= AccountUtils
.getWebdavPath(ocv
);
90 Uri oc_url
= Uri
.parse(oc_base_url
+webdav_path
);
92 WebdavClient wdc
= new WebdavClient(Uri
.parse(oc_base_url
+ webdav_path
));
94 String username
= mAccount
.name
.split("@")[0];
97 password
= am
.blockingGetAuthToken(mAccount
,
98 AccountAuthenticator
.AUTH_TOKEN_TYPE
, true
);
99 } catch (Exception e
) {
104 wdc
.setCredentials(username
, password
);
105 wdc
.allowUnsignedCertificates();
107 Notification n
= new Notification(R
.drawable
.icon
, "Downloading file",
108 System
.currentTimeMillis());
109 PendingIntent pi
= PendingIntent
.getActivity(this, 1, new Intent(this,
110 FileDisplayActivity
.class), 0);
111 n
.setLatestEventInfo(this, "Downloading file", "Downloading file "
115 File sdCard
= Environment
.getExternalStorageDirectory();
116 File dir
= new File(sdCard
.getAbsolutePath() + "/owncloud");
118 File file
= new File(dir
, mFilePath
.replace('/', '.'));
120 Log
.e(TAG
, file
.getAbsolutePath() + " " + oc_url
.toString());
121 Log
.e(TAG
, mFilePath
+"");
122 if (wdc
.downloadFile(mFilePath
, file
)) {
123 ContentValues cv
= new ContentValues();
124 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getAbsolutePath());
125 getContentResolver().update(
126 ProviderTableMeta
.CONTENT_URI
,
128 ProviderTableMeta
.FILE_NAME
+ "=? AND "
129 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
131 mFilePath
.substring(mFilePath
.lastIndexOf('/') + 1),
135 Intent end
= new Intent(DOWNLOAD_FINISH_MESSAGE
);