Android: File(s) download

This commit is contained in:
jendib 2014-11-22 22:18:21 +01:00
parent 92b2219bed
commit bc719b3165
6 changed files with 130 additions and 7 deletions

View File

@ -21,7 +21,6 @@ public class MainApplication extends Application {
ApplicationContext.getInstance().setUserInfo(getApplicationContext(), json);
// TODO Fullscreen preview
// TODO Downloading
// TODO Sharing
// TODO Error feedback
// TODO Infinite scrolling on documents

View File

@ -1,10 +1,16 @@
package com.sismics.docs.activity;
import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
@ -15,6 +21,7 @@ import com.sismics.docs.R;
import com.sismics.docs.adapter.FilePagerAdapter;
import com.sismics.docs.model.application.ApplicationContext;
import com.sismics.docs.resource.FileResource;
import com.sismics.docs.util.PreferenceUtil;
import com.sismics.docs.util.TagUtil;
import org.apache.http.Header;
@ -30,7 +37,21 @@ import java.util.Date;
* @author bgamard
*/
public class DocumentActivity extends ActionBarActivity {
/**
* File view pager.
*/
ViewPager fileViewPager;
/**
* File pager adapter.
*/
FilePagerAdapter filePagerAdapter;
/**
* Document displayed.
*/
JSONObject document;
@Override
protected void onCreate(final Bundle args) {
super.onCreate(args);
@ -55,7 +76,6 @@ public class DocumentActivity extends ActionBarActivity {
return;
}
JSONObject document;
try {
document = new JSONObject(documentJson);
} catch (JSONException e) {
@ -117,17 +137,33 @@ public class DocumentActivity extends ActionBarActivity {
FileResource.list(this, id, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
ViewPager fileViewPager = (ViewPager) findViewById(R.id.fileViewPager);
fileViewPager = (ViewPager) findViewById(R.id.fileViewPager);
fileViewPager.setOffscreenPageLimit(1);
fileViewPager.setAdapter(new FilePagerAdapter(DocumentActivity.this, response.optJSONArray("files")));
filePagerAdapter = new FilePagerAdapter(DocumentActivity.this, response.optJSONArray("files"));
fileViewPager.setAdapter(filePagerAdapter);
findViewById(R.id.progressBar).setVisibility(View.GONE);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.document_activity, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.download_file:
downloadCurrentFile();
return true;
case R.id.download_document:
downloadZip();
return true;
case android.R.id.home:
finish();
return true;
@ -136,4 +172,55 @@ public class DocumentActivity extends ActionBarActivity {
return super.onOptionsItemSelected(item);
}
}
/**
* Download the current displayed file.
*/
private void downloadCurrentFile() {
if (fileViewPager == null || filePagerAdapter == null) return;
JSONObject file = filePagerAdapter.getObjectAt(fileViewPager.getCurrentItem());
if (file == null) return;
// Build the destination filename
String mimeType = file.optString("mimetype");
int position = fileViewPager.getCurrentItem();
if (mimeType == null || !mimeType.contains("/")) return;
String ext = mimeType.split("/")[1];
String fileName = getTitle() + "-" + position + "." + ext;
// Download the file
String fileUrl = PreferenceUtil.getServerUrl(this) + "/api/file/" + file.optString("id") + "/data";
downloadFile(fileUrl, fileName, getTitle().toString(), getString(R.string.downloading_file, position + 1));
}
/**
* Download the document (all files zipped).
*/
private void downloadZip() {
if (document == null) return;
String url = PreferenceUtil.getServerUrl(this) + "/api/file/zip?id=" + document.optString("id");
String fileName = getTitle() + ".zip";
downloadFile(url, fileName, getTitle().toString(), getString(R.string.downloading_document));
}
/**
* Download a file using Android download manager.
*
* @param url URL to download
* @param fileName Destination file name
* @param title Notification title
* @param description Notification description
*/
private void downloadFile(String url, String fileName, String title, String description) {
String authToken = PreferenceUtil.getAuthToken(this);
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
request.addRequestHeader("Cookie", "auth_token=" + authToken);
request.setTitle(title);
request.setDescription(description);
downloadManager.enqueue(request);
}
}

View File

@ -89,4 +89,18 @@ public class FilePagerAdapter extends PagerAdapter {
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
/**
* Return the object at a given position.
*
* @param position Position
* @return Object
*/
public JSONObject getObjectAt(int position) {
if (files == null) {
return null;
}
return files.optJSONObject(position);
}
}

View File

@ -15,7 +15,8 @@
<TextView
android:id="@+id/createdDateLabel"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_height="24dp"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_marginRight="8dp"
android:layout_marginEnd="8dp"
@ -27,7 +28,8 @@
android:layout_toRightOf="@id/createdDateLabel"
android:layout_toEndOf="@id/createdDateLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="24dp"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:fontFamily="sans-serif-light"
android:text="01/12/2014"/>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/download_file"
app:showAsAction="collapseActionView"
android:title="@string/download_file">
</item>
<item
android:id="@+id/download_document"
app:showAsAction="collapseActionView"
android:title="@string/download_document">
</item>
</menu>

View File

@ -26,5 +26,9 @@
<string name="validate_error_length_max">Too long (max. %d)</string>
<string name="validate_error_required">Required</string>
<string name="validate_error_alphanumeric">Only letters and numbers</string>
<string name="download_file">Download current file</string>
<string name="downloading_file">Downloading file number %1s</string>
<string name="download_document">Download all files</string>
<string name="downloading_document">Downloading document</string>
</resources>