downloader made background thread

This commit is contained in:
Bartek Przybylski 2012-02-06 18:01:40 +01:00
parent ca5455e88d
commit 7d93f30346

View file

@ -10,49 +10,85 @@ import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Binder;
import android.os.Environment; import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder; import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.util.Log; import android.util.Log;
import eu.alefzero.owncloud.authenticator.AccountAuthenticator; import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
import eu.alefzero.owncloud.ui.activity.FileDisplayActivity; import eu.alefzero.owncloud.ui.activity.FileDisplayActivity;
import eu.alefzero.webdav.WebdavClient; import eu.alefzero.webdav.WebdavClient;
public class FileDownloader extends Service { public class FileDownloader extends Service {
static final String EXTRA_ACCOUNT = "ACCOUNT"; public static final String EXTRA_ACCOUNT = "ACCOUNT";
static final String EXTRA_FILE_PATH = "FILE_PATH"; public static final String EXTRA_FILE_PATH = "FILE_PATH";
static final String TAG = "OC_FileDownloader"; private static final String TAG = "OC_FileDownloader";
NotificationManager nm; private NotificationManager nm;
private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
private Account mAccount;
private String mFilePath;
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
downloadFile();
stopSelf(msg.arg1);
}
}
@Override
public void onCreate() {
super.onCreate();
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
HandlerThread thread = new HandlerThread("FileDownladerThread", Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
@Override @Override
public IBinder onBind(Intent arg0) { public IBinder onBind(Intent arg0) {
return null; return null;
} }
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
if (!intent.hasExtra(EXTRA_ACCOUNT) && !intent.hasExtra(EXTRA_FILE_PATH)) { if (!intent.hasExtra(EXTRA_ACCOUNT) && !intent.hasExtra(EXTRA_FILE_PATH)) {
Log.e(TAG, "Not enough information provided in intent"); Log.e(TAG, "Not enough information provided in intent");
return START_NOT_STICKY; return START_STICKY;
} }
mAccount = intent.getParcelableExtra(EXTRA_ACCOUNT);
mFilePath = intent.getStringExtra(EXTRA_FILE_PATH);
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
mServiceHandler.sendMessage(msg);
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Account account = intent.getParcelableExtra(EXTRA_ACCOUNT); return START_NOT_STICKY;
String file_path = intent.getStringExtra(EXTRA_FILE_PATH); }
void downloadFile() {
AccountManager am = (AccountManager)getSystemService(ACCOUNT_SERVICE); AccountManager am = (AccountManager)getSystemService(ACCOUNT_SERVICE);
Uri oc_url = Uri.parse(am.getUserData(account, AccountAuthenticator.KEY_OC_URL)); Uri oc_url = Uri.parse(am.getUserData(mAccount, AccountAuthenticator.KEY_OC_URL));
WebdavClient wdc = new WebdavClient(oc_url); WebdavClient wdc = new WebdavClient(oc_url);
String username = account.name.split("@")[0]; String username = mAccount.name.split("@")[0];
String password = ""; String password = "";
try { try {
password = am.blockingGetAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE, true); password = am.blockingGetAuthToken(mAccount, AccountAuthenticator.AUTH_TOKEN_TYPE, true);
} catch (Exception e) { } catch (Exception e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
return START_NOT_STICKY;
} }
wdc.setCredentials(username, password); wdc.setCredentials(username, password);
@ -60,18 +96,16 @@ public class FileDownloader extends Service {
Notification n = new Notification(R.drawable.icon, "Downloading file", System.currentTimeMillis()); Notification n = new Notification(R.drawable.icon, "Downloading file", System.currentTimeMillis());
PendingIntent pi = PendingIntent.getActivity(this, 1, new Intent(this, FileDisplayActivity.class), 0); PendingIntent pi = PendingIntent.getActivity(this, 1, new Intent(this, FileDisplayActivity.class), 0);
n.setLatestEventInfo(this, "A", "B", pi); n.setLatestEventInfo(this, "Downloading file", "Downloading file " + mFilePath, pi);
nm.notify(1, n); nm.notify(1, n);
File sdCard = Environment.getExternalStorageDirectory(); File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/owncloud"); File dir = new File (sdCard.getAbsolutePath() + "/owncloud");
dir.mkdirs(); dir.mkdirs();
File file = new File(dir, file_path.replace('/', '.')); File file = new File(dir, mFilePath.replace('/', '.'));
wdc.downloadFile(file_path, file); wdc.downloadFile(mFilePath, file);
nm.cancel(1);
return START_NOT_STICKY;
} }
} }