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
) {
44 public void handleMessage(Message msg
) {
51 public void onCreate() {
53 nm
= (NotificationManager
)getSystemService(NOTIFICATION_SERVICE
);
54 HandlerThread thread
= new HandlerThread("FileDownladerThread", Process
.THREAD_PRIORITY_BACKGROUND
);
56 mServiceLooper
= thread
.getLooper();
57 mServiceHandler
= new ServiceHandler(mServiceLooper
);
61 public IBinder
onBind(Intent arg0
) {
66 public int onStartCommand(Intent intent
, int flags
, int startId
) {
67 if (!intent
.hasExtra(EXTRA_ACCOUNT
) && !intent
.hasExtra(EXTRA_FILE_PATH
)) {
68 Log
.e(TAG
, "Not enough information provided in intent");
71 mAccount
= intent
.getParcelableExtra(EXTRA_ACCOUNT
);
72 mFilePath
= intent
.getStringExtra(EXTRA_FILE_PATH
);
73 Message msg
= mServiceHandler
.obtainMessage();
75 mServiceHandler
.sendMessage(msg
);
77 return START_NOT_STICKY
;
81 AccountManager am
= (AccountManager
)getSystemService(ACCOUNT_SERVICE
);
82 Uri oc_url
= Uri
.parse(am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_URL
));
84 WebdavClient wdc
= new WebdavClient(oc_url
);
86 String username
= mAccount
.name
.split("@")[0];
89 password
= am
.blockingGetAuthToken(mAccount
, AccountAuthenticator
.AUTH_TOKEN_TYPE
, true
);
90 } catch (Exception e
) {
94 wdc
.setCredentials(username
, password
);
95 wdc
.allowUnsignedCertificates();
97 Notification n
= new Notification(R
.drawable
.icon
, "Downloading file", System
.currentTimeMillis());
98 PendingIntent pi
= PendingIntent
.getActivity(this, 1, new Intent(this, FileDisplayActivity
.class), 0);
99 n
.setLatestEventInfo(this, "Downloading file", "Downloading file " + mFilePath
, pi
);
102 File sdCard
= Environment
.getExternalStorageDirectory();
103 File dir
= new File (sdCard
.getAbsolutePath() + "/owncloud");
105 File file
= new File(dir
, mFilePath
.replace('/', '.'));
107 Log
.e(TAG
, file
.getAbsolutePath() + " " + oc_url
.toString());
108 wdc
.downloadFile(mFilePath
, file
);
109 ContentValues cv
= new ContentValues();
110 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getAbsolutePath());
111 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
,
113 ProviderTableMeta
.FILE_NAME
+"=? AND "+ProviderTableMeta
.FILE_ACCOUNT_OWNER
+"=?",
114 new String
[]{mFilePath
.substring(mFilePath
.lastIndexOf('/')+1), mAccount
.name
});
116 Intent end
= new Intent(DOWNLOAD_FINISH_MESSAGE
);