121e14723347782b01ff2042ef247310954faa04
   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
.Intent
; 
  12 import android
.net
.Uri
; 
  13 import android
.os
.Environment
; 
  14 import android
.os
.IBinder
; 
  15 import android
.util
.Log
; 
  16 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
; 
  17 import eu
.alefzero
.owncloud
.ui
.activity
.FileDisplayActivity
; 
  18 import eu
.alefzero
.webdav
.WebdavClient
; 
  20 public class FileDownloader 
extends Service 
{ 
  21   static final String EXTRA_ACCOUNT 
= "ACCOUNT"; 
  22   static final String EXTRA_FILE_PATH 
= "FILE_PATH"; 
  23   static final String TAG 
= "OC_FileDownloader"; 
  25   NotificationManager nm
; 
  28   public IBinder 
onBind(Intent arg0
) { 
  33   public int onStartCommand(Intent intent
, int flags
, int startId
) { 
  34     if (!intent
.hasExtra(EXTRA_ACCOUNT
) && !intent
.hasExtra(EXTRA_FILE_PATH
)) { 
  35       Log
.e(TAG
, "Not enough information provided in intent"); 
  36       return START_NOT_STICKY
; 
  39     nm 
= (NotificationManager
)getSystemService(NOTIFICATION_SERVICE
); 
  41     Account account 
= intent
.getParcelableExtra(EXTRA_ACCOUNT
); 
  42     String file_path 
= intent
.getStringExtra(EXTRA_FILE_PATH
); 
  43     AccountManager am 
= (AccountManager
)getSystemService(ACCOUNT_SERVICE
); 
  44     Uri oc_url 
= Uri
.parse(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_URL
)); 
  46     WebdavClient wdc 
= new WebdavClient(oc_url
); 
  48     String username 
= account
.name
.split("@")[0]; 
  51       password 
= am
.blockingGetAuthToken(account
, AccountAuthenticator
.AUTH_TOKEN_TYPE
, true
); 
  52     } catch (Exception e
) { 
  53       // TODO Auto-generated catch block 
  55       return START_NOT_STICKY
; 
  58     wdc
.setCredentials(username
, password
); 
  59     wdc
.allowUnsignedCertificates(); 
  61     Notification n 
= new Notification(R
.drawable
.icon
, "Downloading file", System
.currentTimeMillis()); 
  62     PendingIntent pi 
= PendingIntent
.getActivity(this, 1, new Intent(this, FileDisplayActivity
.class), 0); 
  63     n
.setLatestEventInfo(this, "A", "B", pi
); 
  66     File sdCard 
= Environment
.getExternalStorageDirectory(); 
  67     File dir 
= new File (sdCard
.getAbsolutePath() + "/owncloud"); 
  69     File file 
= new File(dir
, file_path
.replace('/', '.')); 
  71     wdc
.downloadFile(file_path
, file
); 
  73     return START_NOT_STICKY
;