send intent with filepath so details view can be updated
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / files / services / FileDownloader.java
1 package eu.alefzero.owncloud.files.services;
2
3 import java.io.File;
4 import java.io.IOException;
5
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;
33
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";
40
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;
50
51 private final class ServiceHandler extends Handler {
52 public ServiceHandler(Looper looper) {
53 super(looper);
54 }
55
56 @Override
57 public void handleMessage(Message msg) {
58 downloadFile();
59 stopSelf(msg.arg1);
60 }
61 }
62
63 @Override
64 public void onCreate() {
65 super.onCreate();
66 mNotificationMngr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
67 HandlerThread thread = new HandlerThread("FileDownladerThread",
68 Process.THREAD_PRIORITY_BACKGROUND);
69 thread.start();
70 mServiceLooper = thread.getLooper();
71 mServiceHandler = new ServiceHandler(mServiceLooper);
72 }
73
74 @Override
75 public IBinder onBind(Intent arg0) {
76 return null;
77 }
78
79 @Override
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");
84 return START_STICKY;
85 }
86 mAccount = intent.getParcelableExtra(EXTRA_ACCOUNT);
87 mFilePath = intent.getStringExtra(EXTRA_FILE_PATH);
88 Message msg = mServiceHandler.obtainMessage();
89 msg.arg1 = startId;
90 mServiceHandler.sendMessage(msg);
91 mCurrentDownlodSize = mLastPercent = 0;
92 mTotalDownloadSize = intent.getLongExtra(EXTRA_FILE_SIZE, -1);
93
94 return START_NOT_STICKY;
95 }
96
97 void downloadFile() {
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);
104
105 WebdavClient wdc = new WebdavClient(Uri.parse(oc_base_url + webdav_path));
106
107 String username = mAccount.name.split("@")[0];
108 String password = "";
109 try {
110 password = am.blockingGetAuthToken(mAccount,
111 AccountAuthenticator.AUTH_TOKEN_TYPE, true);
112 } catch (Exception e) {
113 e.printStackTrace();
114 return;
115 }
116
117 wdc.setCredentials(username, password);
118 wdc.allowSelfsignedCertificates();
119 wdc.setDataTransferProgressListener(this);
120
121 mNotification = new Notification(R.drawable.icon, "Downloading file", System.currentTimeMillis());
122
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);
127
128 mNotificationMngr.notify(1, mNotification);
129
130 File sdCard = Environment.getExternalStorageDirectory();
131 File file = new File(sdCard.getAbsolutePath() + "/owncloud/" + mAccount.name + mFilePath);
132 try {
133 file.getParentFile().mkdirs();
134 file.createNewFile();
135 } catch (IOException e) {
136 e.printStackTrace();
137 }
138
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,
146 cv,
147 ProviderTableMeta.FILE_NAME + "=? AND "
148 + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
149 new String[] {
150 mFilePath.substring(mFilePath.lastIndexOf('/') + 1),
151 mAccount.name });
152 }
153 mNotificationMngr.cancel(1);
154 Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE);
155 end.putExtra(EXTRA_FILE_PATH, file.getAbsolutePath());
156 sendBroadcast(end);
157 }
158
159 @Override
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);
167 }
168
169 mLastPercent = percent;
170 }
171
172 }