1 package eu
.alefzero
.owncloud
.files
.services
;
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 android
.widget
.RemoteViews
;
23 import eu
.alefzero
.owncloud
.AccountUtils
;
24 import eu
.alefzero
.owncloud
.R
;
25 import eu
.alefzero
.owncloud
.R
.drawable
;
26 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
27 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
28 import eu
.alefzero
.owncloud
.files
.interfaces
.OnDatatransferProgressListener
;
29 import eu
.alefzero
.owncloud
.ui
.activity
.FileDisplayActivity
;
30 import eu
.alefzero
.owncloud
.utils
.OwnCloudVersion
;
31 import eu
.alefzero
.webdav
.WebdavClient
;
33 public class FileDownloader
extends Service
implements OnDatatransferProgressListener
{
34 public static final String DOWNLOAD_FINISH_MESSAGE
= "DOWNLOAD_FINISH";
35 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
36 public static final String EXTRA_FILE_PATH
= "FILE_PATH";
37 public static final String EXTRA_FILE_SIZE
= "FILE_SIZE";
38 private static final String TAG
= "FileDownloader";
40 private NotificationManager mNotificationMngr
;
41 private Looper mServiceLooper
;
42 private ServiceHandler mServiceHandler
;
43 private Account mAccount
;
44 private String mFilePath
;
45 private int mLastPercent
;
46 private long mTotalDownloadSize
;
47 private long mCurrentDownlodSize
;
48 private Notification mNotification
;
50 private final class ServiceHandler
extends Handler
{
51 public ServiceHandler(Looper looper
) {
56 public void handleMessage(Message msg
) {
63 public void onCreate() {
65 mNotificationMngr
= (NotificationManager
) getSystemService(NOTIFICATION_SERVICE
);
66 HandlerThread thread
= new HandlerThread("FileDownladerThread",
67 Process
.THREAD_PRIORITY_BACKGROUND
);
69 mServiceLooper
= thread
.getLooper();
70 mServiceHandler
= new ServiceHandler(mServiceLooper
);
74 public IBinder
onBind(Intent arg0
) {
79 public int onStartCommand(Intent intent
, int flags
, int startId
) {
80 if (!intent
.hasExtra(EXTRA_ACCOUNT
)
81 && !intent
.hasExtra(EXTRA_FILE_PATH
)) {
82 Log
.e(TAG
, "Not enough information provided in intent");
85 mAccount
= intent
.getParcelableExtra(EXTRA_ACCOUNT
);
86 mFilePath
= intent
.getStringExtra(EXTRA_FILE_PATH
);
87 Message msg
= mServiceHandler
.obtainMessage();
89 mServiceHandler
.sendMessage(msg
);
90 mCurrentDownlodSize
= mLastPercent
= 0;
91 mTotalDownloadSize
= intent
.getLongExtra(EXTRA_FILE_SIZE
, -1);
93 return START_NOT_STICKY
;
97 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
98 String oc_base_url
= am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_BASE_URL
);
99 OwnCloudVersion ocv
= new OwnCloudVersion(am
100 .getUserData(mAccount
, AccountAuthenticator
.KEY_OC_VERSION
));
101 String webdav_path
= AccountUtils
.getWebdavPath(ocv
);
102 Uri oc_url
= Uri
.parse(oc_base_url
+webdav_path
);
104 WebdavClient wdc
= new WebdavClient(Uri
.parse(oc_base_url
+ webdav_path
));
106 String username
= mAccount
.name
.split("@")[0];
107 String password
= "";
109 password
= am
.blockingGetAuthToken(mAccount
,
110 AccountAuthenticator
.AUTH_TOKEN_TYPE
, true
);
111 } catch (Exception e
) {
116 wdc
.setCredentials(username
, password
);
117 wdc
.allowSelfsignedCertificates();
118 wdc
.setDataTransferProgressListener(this);
120 mNotification
= new Notification(R
.drawable
.icon
, "Downloading file", System
.currentTimeMillis());
122 mNotification
.flags
|= Notification
.FLAG_ONGOING_EVENT
;
123 mNotification
.contentView
= new RemoteViews(getApplicationContext().getPackageName(), R
.layout
.progressbar_layout
);
124 mNotification
.contentView
.setProgressBar(R
.id
.status_progress
, 100, 0, mTotalDownloadSize
== -1);
125 mNotification
.contentView
.setImageViewResource(R
.id
.status_icon
, R
.drawable
.icon
);
127 mNotificationMngr
.notify(1, mNotification
);
129 File sdCard
= Environment
.getExternalStorageDirectory();
130 File dir
= new File(sdCard
.getAbsolutePath() + "/owncloud");
132 File file
= new File(dir
, mFilePath
.replace('/', '.'));
134 Log
.e(TAG
, file
.getAbsolutePath() + " " + oc_url
.toString());
135 Log
.e(TAG
, mFilePath
+"");
136 if (wdc
.downloadFile(mFilePath
, file
)) {
137 ContentValues cv
= new ContentValues();
138 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getAbsolutePath());
139 getContentResolver().update(
140 ProviderTableMeta
.CONTENT_URI
,
142 ProviderTableMeta
.FILE_NAME
+ "=? AND "
143 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
145 mFilePath
.substring(mFilePath
.lastIndexOf('/') + 1),
148 mNotificationMngr
.cancel(1);
149 Intent end
= new Intent(DOWNLOAD_FINISH_MESSAGE
);
154 public void transferProgress(long progressRate
) {
155 mCurrentDownlodSize
+= progressRate
;
156 int percent
= (int)(100.0*((double)mCurrentDownlodSize
)/((double)mTotalDownloadSize
));
157 if (percent
!= mLastPercent
) {
158 mNotification
.contentView
.setProgressBar(R
.id
.status_progress
, 100, (int)(100*mCurrentDownlodSize
/mTotalDownloadSize
), mTotalDownloadSize
== -1);
159 mNotification
.contentView
.setTextViewText(R
.id
.status_text
, percent
+"%");
160 mNotificationMngr
.notify(1, mNotification
);
163 mLastPercent
= percent
;