From: Bartek Przybylski Date: Sat, 24 Dec 2011 22:59:12 +0000 (+0100) Subject: downloading file, to a fixed name file X-Git-Tag: oc-android-1.4.3~499 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/c2e3ce7c00767b2edb093c6c7df0f70cce279de8 downloading file, to a fixed name file --- diff --git a/src/eu/alefzero/owncloud/FileDownloader.java b/src/eu/alefzero/owncloud/FileDownloader.java index 1631f498..0e8e03b0 100644 --- a/src/eu/alefzero/owncloud/FileDownloader.java +++ b/src/eu/alefzero/owncloud/FileDownloader.java @@ -62,78 +62,32 @@ public class FileDownloader extends Service { 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); + WebdavClient wdc = new WebdavClient(oc_url); 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) { + } catch (Exception e) { // 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) { - + e.printStackTrace(); + return START_NOT_STICKY; } - - 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()); + 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, 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(); - } + wdc.downloadFile(file_path, file); return START_NOT_STICKY; } diff --git a/src/eu/alefzero/owncloud/OwnCloudMainScreen.java b/src/eu/alefzero/owncloud/OwnCloudMainScreen.java index 614058b4..65592b19 100644 --- a/src/eu/alefzero/owncloud/OwnCloudMainScreen.java +++ b/src/eu/alefzero/owncloud/OwnCloudMainScreen.java @@ -290,8 +290,8 @@ public class OwnCloudMainScreen extends ListActivity { fis.read(buffer); Log.d("DUPA", new String(buffer)); - //Intent intent = new Intent(this, FileDownloader.class); - /*intent.putExtra(FileDownloader.EXTRA_FILE_PATH, "/docsy.py"); + Intent intent = new Intent(this, FileDownloader.class); + intent.putExtra(FileDownloader.EXTRA_FILE_PATH, "/"+((TextView)findViewById(R.id.textView1)).getText().toString()); intent.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount); startService(intent); /* diff --git a/src/eu/alefzero/owncloud/WebdavClient.java b/src/eu/alefzero/owncloud/WebdavClient.java index 9ff1ed9c..7ed11cee 100644 --- a/src/eu/alefzero/owncloud/WebdavClient.java +++ b/src/eu/alefzero/owncloud/WebdavClient.java @@ -1,11 +1,17 @@ package eu.alefzero.owncloud; import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; import org.apache.http.HttpHost; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; import org.apache.http.HttpVersion; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPut; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.params.ConnManagerPNames; @@ -62,7 +68,25 @@ public class WebdavClient { mSchemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443)); } - boolean downloadFile(String filepath) { + boolean downloadFile(String filepath, File targetPath) { + HttpGet get = new HttpGet(mUri.toString() + filepath.replace(" ", "%20")); + get.setHeader("Host", mUri.getHost()); + get.setHeader("User-Agent", "Android-ownCloud"); + + try { + HttpResponse response = mHttpClient.execute(mTargetHost, get, mHttpContext); + if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { + return false; + } + InputStreamReader isr = new InputStreamReader(response.getEntity().getContent()); + FileOutputStream fos = new FileOutputStream(targetPath); + int oneByte; + while ((oneByte = isr.read()) != -1) fos.write(oneByte); + + } catch (IOException e) { + e.printStackTrace(); + return false; + } return true; }