734359edd00b0f708b1d84bd7ae267347f258f02
[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 BAD_DOWNLOAD_MESSAGE = "BAD_DOWNLOAD";
37 public static final String EXTRA_ACCOUNT = "ACCOUNT";
38 public static final String EXTRA_FILE_PATH = "FILE_PATH";
39 public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";
40 public static final String EXTRA_FILE_SIZE = "FILE_SIZE";
41 private static final String TAG = "FileDownloader";
42
43 private NotificationManager mNotificationMngr;
44 private Looper mServiceLooper;
45 private ServiceHandler mServiceHandler;
46 private Account mAccount;
47 private String mFilePath;
48 private String mRemotePath;
49 private int mLastPercent;
50 private long mTotalDownloadSize;
51 private long mCurrentDownlodSize;
52 private Notification mNotification;
53
54 private final class ServiceHandler extends Handler {
55 public ServiceHandler(Looper looper) {
56 super(looper);
57 }
58
59 @Override
60 public void handleMessage(Message msg) {
61 downloadFile();
62 stopSelf(msg.arg1);
63 }
64 }
65
66 @Override
67 public void onCreate() {
68 super.onCreate();
69 mNotificationMngr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
70 HandlerThread thread = new HandlerThread("FileDownladerThread",
71 Process.THREAD_PRIORITY_BACKGROUND);
72 thread.start();
73 mServiceLooper = thread.getLooper();
74 mServiceHandler = new ServiceHandler(mServiceLooper);
75 }
76
77 @Override
78 public IBinder onBind(Intent arg0) {
79 return null;
80 }
81
82 @Override
83 public int onStartCommand(Intent intent, int flags, int startId) {
84 if (!intent.hasExtra(EXTRA_ACCOUNT)
85 && !intent.hasExtra(EXTRA_FILE_PATH)) {
86 Log.e(TAG, "Not enough information provided in intent");
87 return START_STICKY;
88 }
89 mAccount = intent.getParcelableExtra(EXTRA_ACCOUNT);
90 mFilePath = intent.getStringExtra(EXTRA_FILE_PATH);
91 mRemotePath = intent.getStringExtra(EXTRA_REMOTE_PATH);
92 Message msg = mServiceHandler.obtainMessage();
93 msg.arg1 = startId;
94 mServiceHandler.sendMessage(msg);
95 mCurrentDownlodSize = mLastPercent = 0;
96 mTotalDownloadSize = intent.getLongExtra(EXTRA_FILE_SIZE, -1);
97
98 return START_NOT_STICKY;
99 }
100
101 void downloadFile() {
102 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
103 String oc_base_url = am.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL);
104 OwnCloudVersion ocv = new OwnCloudVersion(am
105 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
106 String webdav_path = AccountUtils.getWebdavPath(ocv);
107 Uri oc_url = Uri.parse(oc_base_url+webdav_path);
108
109 WebdavClient wdc = new WebdavClient(Uri.parse(oc_base_url + webdav_path));
110
111 String username = mAccount.name.split("@")[0];
112 String password = "";
113 try {
114 password = am.blockingGetAuthToken(mAccount,
115 AccountAuthenticator.AUTH_TOKEN_TYPE, true);
116 } catch (Exception e) {
117 e.printStackTrace();
118 return;
119 }
120
121 wdc.setCredentials(username, password);
122 wdc.allowSelfsignedCertificates();
123 wdc.setDataTransferProgressListener(this);
124
125 mNotification = new Notification(R.drawable.icon, "Downloading file", System.currentTimeMillis());
126
127 mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
128 mNotification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.progressbar_layout);
129 mNotification.contentView.setProgressBar(R.id.status_progress, 100, 0, mTotalDownloadSize == -1);
130 mNotification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon);
131 // dvelasco ; contentIntent MUST be assigned to avoid app crashes in versions previous to Android 4.x ;
132 // BUT an empty Intent is not a very elegant solution; something smart should happen when a user 'clicks' on a download in the notification bar
133 mNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
134
135 mNotificationMngr.notify(1, mNotification);
136
137 File sdCard = Environment.getExternalStorageDirectory();
138 File file = new File(sdCard.getAbsolutePath() + "/owncloud/" + mAccount.name + mFilePath);
139 try {
140 file.getParentFile().mkdirs();
141 file.createNewFile();
142 } catch (IOException e) {
143 e.printStackTrace();
144 }
145
146 Log.e(TAG, file.getAbsolutePath() + " " + oc_url.toString());
147 Log.e(TAG, mFilePath+"");
148 String message;
149 if (wdc.downloadFile(mRemotePath, file)) {
150 ContentValues cv = new ContentValues();
151 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getAbsolutePath());
152 getContentResolver().update(
153 ProviderTableMeta.CONTENT_URI,
154 cv,
155 ProviderTableMeta.FILE_NAME + "=? AND "
156 + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
157 new String[] {
158 mFilePath.substring(mFilePath.lastIndexOf('/') + 1),
159 mAccount.name });
160 message = DOWNLOAD_FINISH_MESSAGE;
161 } else {
162 message = BAD_DOWNLOAD_MESSAGE;
163 }
164
165 mNotificationMngr.cancel(1);
166 Intent end = new Intent(message);
167 end.putExtra(EXTRA_FILE_PATH, file.getAbsolutePath());
168 sendBroadcast(end);
169 }
170
171 @Override
172 public void transferProgress(long progressRate) {
173 mCurrentDownlodSize += progressRate;
174 int percent = (int)(100.0*((double)mCurrentDownlodSize)/((double)mTotalDownloadSize));
175 if (percent != mLastPercent) {
176 mNotification.contentView.setProgressBar(R.id.status_progress, 100, (int)(100*mCurrentDownlodSize/mTotalDownloadSize), mTotalDownloadSize == -1);
177 mNotification.contentView.setTextViewText(R.id.status_text, percent+"%");
178 mNotificationMngr.notify(1, mNotification);
179 }
180
181 mLastPercent = percent;
182 }
183
184 }