1 package eu
.alefzero
.owncloud
.files
.services
;
4 import java
.io
.IOException
;
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 android
.widget
.RemoteViews
;
24 import eu
.alefzero
.owncloud
.AccountUtils
;
25 import eu
.alefzero
.owncloud
.R
;
26 import eu
.alefzero
.owncloud
.R
.drawable
;
27 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
28 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
29 import eu
.alefzero
.owncloud
.files
.interfaces
.OnDatatransferProgressListener
;
30 import eu
.alefzero
.owncloud
.ui
.activity
.FileDisplayActivity
;
31 import eu
.alefzero
.owncloud
.utils
.OwnCloudVersion
;
32 import eu
.alefzero
.webdav
.WebdavClient
;
34 public class FileDownloader
extends Service
implements OnDatatransferProgressListener
{
35 public static final String DOWNLOAD_FINISH_MESSAGE
= "DOWNLOAD_FINISH";
36 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
37 public static final String EXTRA_FILE_PATH
= "FILE_PATH";
38 public static final String EXTRA_FILE_SIZE
= "FILE_SIZE";
39 private static final String TAG
= "FileDownloader";
41 private NotificationManager mNotificationMngr
;
42 private Looper mServiceLooper
;
43 private ServiceHandler mServiceHandler
;
44 private Account mAccount
;
45 private String mFilePath
;
46 private int mLastPercent
;
47 private long mTotalDownloadSize
;
48 private long mCurrentDownlodSize
;
49 private Notification mNotification
;
51 private final class ServiceHandler
extends Handler
{
52 public ServiceHandler(Looper looper
) {
57 public void handleMessage(Message msg
) {
64 public void onCreate() {
66 mNotificationMngr
= (NotificationManager
) getSystemService(NOTIFICATION_SERVICE
);
67 HandlerThread thread
= new HandlerThread("FileDownladerThread",
68 Process
.THREAD_PRIORITY_BACKGROUND
);
70 mServiceLooper
= thread
.getLooper();
71 mServiceHandler
= new ServiceHandler(mServiceLooper
);
75 public IBinder
onBind(Intent arg0
) {
80 public int onStartCommand(Intent intent
, int flags
, int startId
) {
81 if (!intent
.hasExtra(EXTRA_ACCOUNT
)
82 && !intent
.hasExtra(EXTRA_FILE_PATH
)) {
83 Log
.e(TAG
, "Not enough information provided in intent");
86 mAccount
= intent
.getParcelableExtra(EXTRA_ACCOUNT
);
87 mFilePath
= intent
.getStringExtra(EXTRA_FILE_PATH
);
88 Message msg
= mServiceHandler
.obtainMessage();
90 mServiceHandler
.sendMessage(msg
);
91 mCurrentDownlodSize
= mLastPercent
= 0;
92 mTotalDownloadSize
= intent
.getLongExtra(EXTRA_FILE_SIZE
, -1);
94 return START_NOT_STICKY
;
98 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
99 String oc_base_url
= am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_BASE_URL
);
100 OwnCloudVersion ocv
= new OwnCloudVersion(am
101 .getUserData(mAccount
, AccountAuthenticator
.KEY_OC_VERSION
));
102 String webdav_path
= AccountUtils
.getWebdavPath(ocv
);
103 Uri oc_url
= Uri
.parse(oc_base_url
+webdav_path
);
105 WebdavClient wdc
= new WebdavClient(Uri
.parse(oc_base_url
+ webdav_path
));
107 String username
= mAccount
.name
.split("@")[0];
108 String password
= "";
110 password
= am
.blockingGetAuthToken(mAccount
,
111 AccountAuthenticator
.AUTH_TOKEN_TYPE
, true
);
112 } catch (Exception e
) {
117 wdc
.setCredentials(username
, password
);
118 wdc
.allowSelfsignedCertificates();
119 wdc
.setDataTransferProgressListener(this);
121 mNotification
= new Notification(R
.drawable
.icon
, "Downloading file", System
.currentTimeMillis());
123 mNotification
.flags
|= Notification
.FLAG_ONGOING_EVENT
;
124 mNotification
.contentView
= new RemoteViews(getApplicationContext().getPackageName(), R
.layout
.progressbar_layout
);
125 mNotification
.contentView
.setProgressBar(R
.id
.status_progress
, 100, 0, mTotalDownloadSize
== -1);
126 mNotification
.contentView
.setImageViewResource(R
.id
.status_icon
, R
.drawable
.icon
);
128 mNotificationMngr
.notify(1, mNotification
);
130 File sdCard
= Environment
.getExternalStorageDirectory();
131 File file
= new File(sdCard
.getAbsolutePath() + "/owncloud/" + mAccount
.name
+ mFilePath
);
133 file
.getParentFile().mkdirs();
134 file
.createNewFile();
135 } catch (IOException e
) {
139 Log
.e(TAG
, file
.getAbsolutePath() + " " + oc_url
.toString());
140 Log
.e(TAG
, mFilePath
+"");
141 if (wdc
.downloadFile(mFilePath
, file
)) {
142 ContentValues cv
= new ContentValues();
143 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getAbsolutePath());
144 getContentResolver().update(
145 ProviderTableMeta
.CONTENT_URI
,
147 ProviderTableMeta
.FILE_NAME
+ "=? AND "
148 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
150 mFilePath
.substring(mFilePath
.lastIndexOf('/') + 1),
153 mNotificationMngr
.cancel(1);
154 Intent end
= new Intent(DOWNLOAD_FINISH_MESSAGE
);
155 end
.putExtra(EXTRA_FILE_PATH
, file
.getAbsolutePath());
160 public void transferProgress(long progressRate
) {
161 mCurrentDownlodSize
+= progressRate
;
162 int percent
= (int)(100.0*((double)mCurrentDownlodSize
)/((double)mTotalDownloadSize
));
163 if (percent
!= mLastPercent
) {
164 mNotification
.contentView
.setProgressBar(R
.id
.status_progress
, 100, (int)(100*mCurrentDownlodSize
/mTotalDownloadSize
), mTotalDownloadSize
== -1);
165 mNotification
.contentView
.setTextViewText(R
.id
.status_text
, percent
+"%");
166 mNotificationMngr
.notify(1, mNotification
);
169 mLastPercent
= percent
;