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_REMOTE_PATH
= "REMOTE_PATH";
39 public static final String EXTRA_FILE_SIZE
= "FILE_SIZE";
40 private static final String TAG
= "FileDownloader";
42 private NotificationManager mNotificationMngr
;
43 private Looper mServiceLooper
;
44 private ServiceHandler mServiceHandler
;
45 private Account mAccount
;
46 private String mFilePath
;
47 private String mRemotePath
;
48 private int mLastPercent
;
49 private long mTotalDownloadSize
;
50 private long mCurrentDownlodSize
;
51 private Notification mNotification
;
53 private final class ServiceHandler
extends Handler
{
54 public ServiceHandler(Looper looper
) {
59 public void handleMessage(Message msg
) {
66 public void onCreate() {
68 mNotificationMngr
= (NotificationManager
) getSystemService(NOTIFICATION_SERVICE
);
69 HandlerThread thread
= new HandlerThread("FileDownladerThread",
70 Process
.THREAD_PRIORITY_BACKGROUND
);
72 mServiceLooper
= thread
.getLooper();
73 mServiceHandler
= new ServiceHandler(mServiceLooper
);
77 public IBinder
onBind(Intent arg0
) {
82 public int onStartCommand(Intent intent
, int flags
, int startId
) {
83 if (!intent
.hasExtra(EXTRA_ACCOUNT
)
84 && !intent
.hasExtra(EXTRA_FILE_PATH
)) {
85 Log
.e(TAG
, "Not enough information provided in intent");
88 mAccount
= intent
.getParcelableExtra(EXTRA_ACCOUNT
);
89 mFilePath
= intent
.getStringExtra(EXTRA_FILE_PATH
);
90 mRemotePath
= intent
.getStringExtra(EXTRA_REMOTE_PATH
);
91 Message msg
= mServiceHandler
.obtainMessage();
93 mServiceHandler
.sendMessage(msg
);
94 mCurrentDownlodSize
= mLastPercent
= 0;
95 mTotalDownloadSize
= intent
.getLongExtra(EXTRA_FILE_SIZE
, -1);
97 return START_NOT_STICKY
;
100 void downloadFile() {
101 AccountManager am
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
102 String oc_base_url
= am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_BASE_URL
);
103 OwnCloudVersion ocv
= new OwnCloudVersion(am
104 .getUserData(mAccount
, AccountAuthenticator
.KEY_OC_VERSION
));
105 String webdav_path
= AccountUtils
.getWebdavPath(ocv
);
106 Uri oc_url
= Uri
.parse(oc_base_url
+webdav_path
);
108 WebdavClient wdc
= new WebdavClient(Uri
.parse(oc_base_url
+ webdav_path
));
110 String username
= mAccount
.name
.split("@")[0];
111 String password
= "";
113 password
= am
.blockingGetAuthToken(mAccount
,
114 AccountAuthenticator
.AUTH_TOKEN_TYPE
, true
);
115 } catch (Exception e
) {
120 wdc
.setCredentials(username
, password
);
121 wdc
.allowSelfsignedCertificates();
122 wdc
.setDataTransferProgressListener(this);
124 mNotification
= new Notification(R
.drawable
.icon
, "Downloading file", System
.currentTimeMillis());
126 mNotification
.flags
|= Notification
.FLAG_ONGOING_EVENT
;
127 mNotification
.contentView
= new RemoteViews(getApplicationContext().getPackageName(), R
.layout
.progressbar_layout
);
128 mNotification
.contentView
.setProgressBar(R
.id
.status_progress
, 100, 0, mTotalDownloadSize
== -1);
129 mNotification
.contentView
.setImageViewResource(R
.id
.status_icon
, R
.drawable
.icon
);
130 // dvelasco ; contentIntent MUST be assigned to avoid app crashes in versions previous to Android 4.x ;
131 // BUT an empty Intent is not a very elegant solution; something smart should happen when a user 'clicks' on a download in the notification bar
132 mNotification
.contentIntent
= PendingIntent
.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent
.FLAG_UPDATE_CURRENT
);
134 mNotificationMngr
.notify(1, mNotification
);
136 File sdCard
= Environment
.getExternalStorageDirectory();
137 File file
= new File(sdCard
.getAbsolutePath() + "/owncloud/" + mAccount
.name
+ mFilePath
);
139 file
.getParentFile().mkdirs();
140 file
.createNewFile();
141 } catch (IOException e
) {
145 Log
.e(TAG
, file
.getAbsolutePath() + " " + oc_url
.toString());
146 Log
.e(TAG
, mFilePath
+"");
147 if (wdc
.downloadFile(mRemotePath
, file
)) {
148 ContentValues cv
= new ContentValues();
149 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getAbsolutePath());
150 getContentResolver().update(
151 ProviderTableMeta
.CONTENT_URI
,
153 ProviderTableMeta
.FILE_NAME
+ "=? AND "
154 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
156 mFilePath
.substring(mFilePath
.lastIndexOf('/') + 1),
159 mNotificationMngr
.cancel(1);
160 Intent end
= new Intent(DOWNLOAD_FINISH_MESSAGE
);
161 end
.putExtra(EXTRA_FILE_PATH
, file
.getAbsolutePath());
166 public void transferProgress(long progressRate
) {
167 mCurrentDownlodSize
+= progressRate
;
168 int percent
= (int)(100.0*((double)mCurrentDownlodSize
)/((double)mTotalDownloadSize
));
169 if (percent
!= mLastPercent
) {
170 mNotification
.contentView
.setProgressBar(R
.id
.status_progress
, 100, (int)(100*mCurrentDownlodSize
/mTotalDownloadSize
), mTotalDownloadSize
== -1);
171 mNotification
.contentView
.setTextViewText(R
.id
.status_text
, percent
+"%");
172 mNotificationMngr
.notify(1, mNotification
);
175 mLastPercent
= percent
;