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
.Binder
;
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
.DbHandler
;
25 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
26 import eu
.alefzero
.owncloud
.ui
.activity
.FileDisplayActivity
;
27 import eu
.alefzero
.webdav
.WebdavClient
;
29 public class FileDownloader
extends Service
{
30 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
31 public static final String EXTRA_FILE_PATH
= "FILE_PATH";
32 private static final String TAG
= "OC_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
) {
45 public void handleMessage(Message msg
) {
52 public void onCreate() {
54 nm
= (NotificationManager
)getSystemService(NOTIFICATION_SERVICE
);
55 HandlerThread thread
= new HandlerThread("FileDownladerThread", Process
.THREAD_PRIORITY_BACKGROUND
);
57 mServiceLooper
= thread
.getLooper();
58 mServiceHandler
= new ServiceHandler(mServiceLooper
);
62 public IBinder
onBind(Intent arg0
) {
67 public int onStartCommand(Intent intent
, int flags
, int startId
) {
68 if (!intent
.hasExtra(EXTRA_ACCOUNT
) && !intent
.hasExtra(EXTRA_FILE_PATH
)) {
69 Log
.e(TAG
, "Not enough information provided in intent");
72 mAccount
= intent
.getParcelableExtra(EXTRA_ACCOUNT
);
73 mFilePath
= intent
.getStringExtra(EXTRA_FILE_PATH
);
74 Message msg
= mServiceHandler
.obtainMessage();
76 mServiceHandler
.sendMessage(msg
);
78 return START_NOT_STICKY
;
82 AccountManager am
= (AccountManager
)getSystemService(ACCOUNT_SERVICE
);
83 Uri oc_url
= Uri
.parse(am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_URL
));
85 WebdavClient wdc
= new WebdavClient(oc_url
);
87 String username
= mAccount
.name
.split("@")[0];
90 password
= am
.blockingGetAuthToken(mAccount
, AccountAuthenticator
.AUTH_TOKEN_TYPE
, true
);
91 } catch (Exception e
) {
95 wdc
.setCredentials(username
, password
);
96 wdc
.allowUnsignedCertificates();
98 Notification n
= new Notification(R
.drawable
.icon
, "Downloading file", System
.currentTimeMillis());
99 PendingIntent pi
= PendingIntent
.getActivity(this, 1, new Intent(this, FileDisplayActivity
.class), 0);
100 n
.setLatestEventInfo(this, "Downloading file", "Downloading file " + mFilePath
, pi
);
103 File sdCard
= Environment
.getExternalStorageDirectory();
104 File dir
= new File (sdCard
.getAbsolutePath() + "/owncloud");
106 File file
= new File(dir
, mFilePath
.replace('/', '.'));
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
});