7d3605e45950505562f24688c432f3b0c0168bba
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / FileDownloader.java
1 package eu.alefzero.owncloud;
2
3 import java.io.File;
4
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.FileDisplayActivity;
18
19 public class FileDownloader extends Service {
20 static final String EXTRA_ACCOUNT = "ACCOUNT";
21 static final String EXTRA_FILE_PATH = "FILE_PATH";
22 static final String TAG = "OC_FileDownloader";
23
24 NotificationManager nm;
25
26 @Override
27 public IBinder onBind(Intent arg0) {
28 return null;
29 }
30
31 @Override
32 public int onStartCommand(Intent intent, int flags, int startId) {
33 if (!intent.hasExtra(EXTRA_ACCOUNT) && !intent.hasExtra(EXTRA_FILE_PATH)) {
34 Log.e(TAG, "Not enough information provided in intent");
35 return START_NOT_STICKY;
36 }
37
38 nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
39
40 Account account = intent.getParcelableExtra(EXTRA_ACCOUNT);
41 String file_path = intent.getStringExtra(EXTRA_FILE_PATH);
42 AccountManager am = (AccountManager)getSystemService(ACCOUNT_SERVICE);
43 Uri oc_url = Uri.parse(am.getUserData(account, AccountAuthenticator.KEY_OC_URL));
44
45 WebdavClient wdc = new WebdavClient(oc_url);
46
47 String username = account.name.split("@")[0];
48 String password = "";
49 try {
50 password = am.blockingGetAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE, true);
51 } catch (Exception e) {
52 // TODO Auto-generated catch block
53 e.printStackTrace();
54 return START_NOT_STICKY;
55 }
56
57 wdc.setCredentials(username, password);
58 wdc.allowUnsignedCertificates();
59
60 Notification n = new Notification(R.drawable.icon, "Downloading file", System.currentTimeMillis());
61 PendingIntent pi = PendingIntent.getActivity(this, 1, new Intent(this, FileDisplayActivity.class), 0);
62 n.setLatestEventInfo(this, "A", "B", pi);
63 nm.notify(1, n);
64
65 File sdCard = Environment.getExternalStorageDirectory();
66 File dir = new File (sdCard.getAbsolutePath() + "/owncloud");
67 dir.mkdirs();
68 File file = new File(dir, file_path.replace('/', '.'));
69
70 wdc.downloadFile(file_path, file);
71
72 return START_NOT_STICKY;
73 }
74
75
76 }