Refactoring: Added comments to every class, as well as a copyright
[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.activity.FileDisplayActivity;
18 import eu.alefzero.webdav.WebdavClient;
19
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";
24
25 NotificationManager nm;
26
27 @Override
28 public IBinder onBind(Intent arg0) {
29 return null;
30 }
31
32 @Override
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;
37 }
38
39 nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
40
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));
45
46 WebdavClient wdc = new WebdavClient(oc_url);
47
48 String username = account.name.split("@")[0];
49 String password = "";
50 try {
51 password = am.blockingGetAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE, true);
52 } catch (Exception e) {
53 // TODO Auto-generated catch block
54 e.printStackTrace();
55 return START_NOT_STICKY;
56 }
57
58 wdc.setCredentials(username, password);
59 wdc.allowUnsignedCertificates();
60
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);
64 nm.notify(1, n);
65
66 File sdCard = Environment.getExternalStorageDirectory();
67 File dir = new File (sdCard.getAbsolutePath() + "/owncloud");
68 dir.mkdirs();
69 File file = new File(dir, file_path.replace('/', '.'));
70
71 wdc.downloadFile(file_path, file);
72
73 return START_NOT_STICKY;
74 }
75
76
77 }