087b103e229869aeab17ced964ffb434ac087e0d
[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
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.ContentValues;
12 import android.content.Intent;
13 import android.net.Uri;
14 import android.os.Environment;
15 import android.os.Handler;
16 import android.os.HandlerThread;
17 import android.os.IBinder;
18 import android.os.Looper;
19 import android.os.Message;
20 import android.os.Process;
21 import android.util.Log;
22 import android.widget.RemoteViews;
23 import eu.alefzero.owncloud.AccountUtils;
24 import eu.alefzero.owncloud.R;
25 import eu.alefzero.owncloud.R.drawable;
26 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
27 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
28 import eu.alefzero.owncloud.files.interfaces.OnDatatransferProgressListener;
29 import eu.alefzero.owncloud.ui.activity.FileDisplayActivity;
30 import eu.alefzero.owncloud.utils.OwnCloudVersion;
31 import eu.alefzero.webdav.WebdavClient;
32
33 public class FileDownloader extends Service implements OnDatatransferProgressListener {
34 public static final String DOWNLOAD_FINISH_MESSAGE = "DOWNLOAD_FINISH";
35 public static final String EXTRA_ACCOUNT = "ACCOUNT";
36 public static final String EXTRA_FILE_PATH = "FILE_PATH";
37 public static final String EXTRA_FILE_SIZE = "FILE_SIZE";
38 private static final String TAG = "FileDownloader";
39
40 private NotificationManager mNotificationMngr;
41 private Looper mServiceLooper;
42 private ServiceHandler mServiceHandler;
43 private Account mAccount;
44 private String mFilePath;
45 private int mLastPercent;
46 private long mTotalDownloadSize;
47 private long mCurrentDownlodSize;
48 private Notification mNotification;
49
50 private final class ServiceHandler extends Handler {
51 public ServiceHandler(Looper looper) {
52 super(looper);
53 }
54
55 @Override
56 public void handleMessage(Message msg) {
57 downloadFile();
58 stopSelf(msg.arg1);
59 }
60 }
61
62 @Override
63 public void onCreate() {
64 super.onCreate();
65 mNotificationMngr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
66 HandlerThread thread = new HandlerThread("FileDownladerThread",
67 Process.THREAD_PRIORITY_BACKGROUND);
68 thread.start();
69 mServiceLooper = thread.getLooper();
70 mServiceHandler = new ServiceHandler(mServiceLooper);
71 }
72
73 @Override
74 public IBinder onBind(Intent arg0) {
75 return null;
76 }
77
78 @Override
79 public int onStartCommand(Intent intent, int flags, int startId) {
80 if (!intent.hasExtra(EXTRA_ACCOUNT)
81 && !intent.hasExtra(EXTRA_FILE_PATH)) {
82 Log.e(TAG, "Not enough information provided in intent");
83 return START_STICKY;
84 }
85 mAccount = intent.getParcelableExtra(EXTRA_ACCOUNT);
86 mFilePath = intent.getStringExtra(EXTRA_FILE_PATH);
87 Message msg = mServiceHandler.obtainMessage();
88 msg.arg1 = startId;
89 mServiceHandler.sendMessage(msg);
90 mCurrentDownlodSize = mLastPercent = 0;
91 mTotalDownloadSize = intent.getLongExtra(EXTRA_FILE_SIZE, -1);
92
93 return START_NOT_STICKY;
94 }
95
96 void downloadFile() {
97 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
98 String oc_base_url = am.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL);
99 OwnCloudVersion ocv = new OwnCloudVersion(am
100 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
101 String webdav_path = AccountUtils.getWebdavPath(ocv);
102 Uri oc_url = Uri.parse(oc_base_url+webdav_path);
103
104 WebdavClient wdc = new WebdavClient(Uri.parse(oc_base_url + webdav_path));
105
106 String username = mAccount.name.split("@")[0];
107 String password = "";
108 try {
109 password = am.blockingGetAuthToken(mAccount,
110 AccountAuthenticator.AUTH_TOKEN_TYPE, true);
111 } catch (Exception e) {
112 e.printStackTrace();
113 return;
114 }
115
116 wdc.setCredentials(username, password);
117 wdc.allowSelfsignedCertificates();
118 wdc.setDataTransferProgressListener(this);
119
120 mNotification = new Notification(R.drawable.icon, "Downloading file", System.currentTimeMillis());
121
122 mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
123 mNotification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.progressbar_layout);
124 mNotification.contentView.setProgressBar(R.id.status_progress, 100, 0, mTotalDownloadSize == -1);
125 mNotification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon);
126
127 mNotificationMngr.notify(1, mNotification);
128
129 File sdCard = Environment.getExternalStorageDirectory();
130 File dir = new File(sdCard.getAbsolutePath() + "/owncloud");
131 dir.mkdirs();
132 File file = new File(dir, mFilePath.replace('/', '.'));
133
134 Log.e(TAG, file.getAbsolutePath() + " " + oc_url.toString());
135 Log.e(TAG, mFilePath+"");
136 if (wdc.downloadFile(mFilePath, file)) {
137 ContentValues cv = new ContentValues();
138 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getAbsolutePath());
139 getContentResolver().update(
140 ProviderTableMeta.CONTENT_URI,
141 cv,
142 ProviderTableMeta.FILE_NAME + "=? AND "
143 + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
144 new String[] {
145 mFilePath.substring(mFilePath.lastIndexOf('/') + 1),
146 mAccount.name });
147 }
148 mNotificationMngr.cancel(1);
149 Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE);
150 sendBroadcast(end);
151 }
152
153 @Override
154 public void transferProgress(long progressRate) {
155 mCurrentDownlodSize += progressRate;
156 int percent = (int)(100.0*((double)mCurrentDownlodSize)/((double)mTotalDownloadSize));
157 if (percent != mLastPercent) {
158 mNotification.contentView.setProgressBar(R.id.status_progress, 100, (int)(100*mCurrentDownlodSize/mTotalDownloadSize), mTotalDownloadSize == -1);
159 mNotification.contentView.setTextViewText(R.id.status_text, percent+"%");
160 mNotificationMngr.notify(1, mNotification);
161 }
162
163 mLastPercent = percent;
164 }
165
166 }