X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/2bc41ee3315feb483e4b92873df8f7dd3bb5499c..5f61cb15552eb2fda01716078a9191b88cebf764:/src/eu/alefzero/owncloud/FileDownloader.java diff --git a/src/eu/alefzero/owncloud/FileDownloader.java b/src/eu/alefzero/owncloud/FileDownloader.java index 1631f498..1936e8ea 100644 --- a/src/eu/alefzero/owncloud/FileDownloader.java +++ b/src/eu/alefzero/owncloud/FileDownloader.java @@ -1,141 +1,139 @@ -package eu.alefzero.owncloud; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.UnknownHostException; -import java.util.Date; - -import org.apache.http.HttpHost; -import org.apache.http.HttpResponse; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.auth.BasicScheme; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.protocol.BasicHttpContext; - -import eu.alefzero.owncloud.authenticator.AccountAuthenticator; -import eu.alefzero.webdav.HttpPropFind; - -import android.accounts.Account; -import android.accounts.AccountManager; -import android.accounts.AuthenticatorException; -import android.accounts.OperationCanceledException; -import android.app.Notification; -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.app.Service; -import android.content.Intent; -import android.net.Uri; -import android.os.Environment; -import android.os.IBinder; -import android.util.Log; -import android.widget.FrameLayout; - -public class FileDownloader extends Service { - static final String EXTRA_ACCOUNT = "ACCOUNT"; - static final String EXTRA_FILE_PATH = "FILE_PATH"; - static final String TAG = "OC_FileDownloader"; - - NotificationManager nm; - - @Override - public IBinder onBind(Intent arg0) { - return null; - } - - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - if (!intent.hasExtra(EXTRA_ACCOUNT) && !intent.hasExtra(EXTRA_FILE_PATH)) { - Log.e(TAG, "Not enough information provided in intent"); - return START_NOT_STICKY; - } - - nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); - - Account account = intent.getParcelableExtra(EXTRA_ACCOUNT); - String file_path = intent.getStringExtra(EXTRA_FILE_PATH); - AccountManager am = (AccountManager)getSystemService(ACCOUNT_SERVICE); - Uri oc_url = Uri.parse(am.getUserData(account, AccountAuthenticator.KEY_OC_URL)); - - DefaultHttpClient client = new DefaultHttpClient(); - Log.d(TAG, oc_url.toString()); - HttpGet query = new HttpGet(oc_url + file_path); - query.setHeader("Content-type", "text/xml"); - query.setHeader("User-Agent", "Android-ownCloud"); - - BasicHttpContext httpContext = new BasicHttpContext(); - BasicScheme basicAuth = new BasicScheme(); - httpContext.setAttribute("preemptive-auth", basicAuth); - - String username = account.name.split("@")[0]; - String password = ""; - try { - password = am.blockingGetAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE, true); - } catch (OperationCanceledException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } catch (AuthenticatorException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } catch (IOException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } - if (am.getUserData(account, AccountAuthenticator.KEY_OC_URL) == null) { - - } - - client.getCredentialsProvider().setCredentials( - new AuthScope(oc_url.getHost(), oc_url.getPort()==-1?80:oc_url.getPort()), - new UsernamePasswordCredentials(username, password) - ); - - HttpHost host = new HttpHost(oc_url.getHost(), oc_url.getPort()==-1?80:oc_url.getPort()); - - Notification n = new Notification(R.drawable.icon, "Downloading file", System.currentTimeMillis()); - PendingIntent pi = PendingIntent.getActivity(this, 1, new Intent(this, OwnCloudMainScreen.class), 0); - n.setLatestEventInfo(this, "A", "B", pi); - nm.notify(1, n); - - HttpResponse response = null; - try { - response = client.execute(host, query, httpContext); - } catch (ClientProtocolException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - File sdCard = Environment.getExternalStorageDirectory(); - File dir = new File (sdCard.getAbsolutePath() + "/owncloud"); - dir.mkdirs(); - File file = new File(dir, "filename"); - - try { - FileOutputStream f = new FileOutputStream(file); - byte[] b = new byte[(int)response.getEntity().getContentLength()]; - response.getEntity().getContent().read(b); - f.write(b); - } catch (FileNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalStateException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - - return START_NOT_STICKY; - } - -} +package eu.alefzero.owncloud; + +import java.io.File; + +import android.accounts.Account; +import android.accounts.AccountManager; +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.app.Service; +import android.content.ContentValues; +import android.content.Intent; +import android.net.Uri; +import android.os.Environment; +import android.os.Handler; +import android.os.HandlerThread; +import android.os.IBinder; +import android.os.Looper; +import android.os.Message; +import android.os.Process; +import android.util.Log; +import eu.alefzero.owncloud.authenticator.AccountAuthenticator; +import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta; +import eu.alefzero.owncloud.ui.activity.FileDisplayActivity; +import eu.alefzero.owncloud.utils.OwnCloudVersion; +import eu.alefzero.webdav.WebdavClient; + +public class FileDownloader extends Service { + public static final String DOWNLOAD_FINISH_MESSAGE = "DOWNLOAD_FINISH"; + public static final String EXTRA_ACCOUNT = "ACCOUNT"; + public static final String EXTRA_FILE_PATH = "FILE_PATH"; + private static final String TAG = "FileDownloader"; + + 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 + public IBinder onBind(Intent arg0) { + return null; + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + if (!intent.hasExtra(EXTRA_ACCOUNT) + && !intent.hasExtra(EXTRA_FILE_PATH)) { + Log.e(TAG, "Not enough information provided in intent"); + return START_STICKY; + } + mAccount = intent.getParcelableExtra(EXTRA_ACCOUNT); + mFilePath = intent.getStringExtra(EXTRA_FILE_PATH); + Message msg = mServiceHandler.obtainMessage(); + msg.arg1 = startId; + mServiceHandler.sendMessage(msg); + + return START_NOT_STICKY; + } + + void downloadFile() { + AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE); + String oc_base_url = am.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL); + OwnCloudVersion ocv = new OwnCloudVersion(am + .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION)); + String webdav_path = AccountUtils.getWebdavPath(ocv); + Uri oc_url = Uri.parse(oc_base_url+webdav_path); + + WebdavClient wdc = new WebdavClient(Uri.parse(oc_base_url + webdav_path)); + + String username = mAccount.name.split("@")[0]; + String password = ""; + try { + password = am.blockingGetAuthToken(mAccount, + AccountAuthenticator.AUTH_TOKEN_TYPE, true); + } catch (Exception e) { + e.printStackTrace(); + return; + } + + wdc.setCredentials(username, password); + wdc.allowUnsignedCertificates(); + + Notification n = new Notification(R.drawable.icon, "Downloading file", + System.currentTimeMillis()); + PendingIntent pi = PendingIntent.getActivity(this, 1, new Intent(this, + FileDisplayActivity.class), 0); + n.setLatestEventInfo(this, "Downloading file", "Downloading file " + + mFilePath, pi); + nm.notify(1, n); + + File sdCard = Environment.getExternalStorageDirectory(); + File dir = new File(sdCard.getAbsolutePath() + "/owncloud"); + dir.mkdirs(); + File file = new File(dir, mFilePath.replace('/', '.')); + + Log.e(TAG, file.getAbsolutePath() + " " + oc_url.toString()); + Log.e(TAG, mFilePath+""); + if (wdc.downloadFile(mFilePath, file)) { + ContentValues cv = new ContentValues(); + cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getAbsolutePath()); + getContentResolver().update( + ProviderTableMeta.CONTENT_URI, + cv, + ProviderTableMeta.FILE_NAME + "=? AND " + + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", + new String[] { + mFilePath.substring(mFilePath.lastIndexOf('/') + 1), + mAccount.name }); + } + nm.cancel(1); + Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE); + sendBroadcast(end); + } + +}