mirror of
https://github.com/sismics/docs.git
synced 2024-11-22 22:07:56 +01:00
Android: File(s) download
This commit is contained in:
parent
92b2219bed
commit
bc719b3165
@ -21,7 +21,6 @@ public class MainApplication extends Application {
|
|||||||
ApplicationContext.getInstance().setUserInfo(getApplicationContext(), json);
|
ApplicationContext.getInstance().setUserInfo(getApplicationContext(), json);
|
||||||
|
|
||||||
// TODO Fullscreen preview
|
// TODO Fullscreen preview
|
||||||
// TODO Downloading
|
|
||||||
// TODO Sharing
|
// TODO Sharing
|
||||||
// TODO Error feedback
|
// TODO Error feedback
|
||||||
// TODO Infinite scrolling on documents
|
// TODO Infinite scrolling on documents
|
||||||
|
@ -1,10 +1,16 @@
|
|||||||
package com.sismics.docs.activity;
|
package com.sismics.docs.activity;
|
||||||
|
|
||||||
|
import android.app.DownloadManager;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Environment;
|
||||||
import android.support.v4.view.ViewPager;
|
import android.support.v4.view.ViewPager;
|
||||||
import android.support.v7.app.ActionBarActivity;
|
import android.support.v7.app.ActionBarActivity;
|
||||||
import android.text.format.DateFormat;
|
import android.text.format.DateFormat;
|
||||||
|
import android.view.Menu;
|
||||||
|
import android.view.MenuInflater;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
@ -15,6 +21,7 @@ import com.sismics.docs.R;
|
|||||||
import com.sismics.docs.adapter.FilePagerAdapter;
|
import com.sismics.docs.adapter.FilePagerAdapter;
|
||||||
import com.sismics.docs.model.application.ApplicationContext;
|
import com.sismics.docs.model.application.ApplicationContext;
|
||||||
import com.sismics.docs.resource.FileResource;
|
import com.sismics.docs.resource.FileResource;
|
||||||
|
import com.sismics.docs.util.PreferenceUtil;
|
||||||
import com.sismics.docs.util.TagUtil;
|
import com.sismics.docs.util.TagUtil;
|
||||||
|
|
||||||
import org.apache.http.Header;
|
import org.apache.http.Header;
|
||||||
@ -30,6 +37,20 @@ import java.util.Date;
|
|||||||
* @author bgamard
|
* @author bgamard
|
||||||
*/
|
*/
|
||||||
public class DocumentActivity extends ActionBarActivity {
|
public class DocumentActivity extends ActionBarActivity {
|
||||||
|
/**
|
||||||
|
* File view pager.
|
||||||
|
*/
|
||||||
|
ViewPager fileViewPager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* File pager adapter.
|
||||||
|
*/
|
||||||
|
FilePagerAdapter filePagerAdapter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Document displayed.
|
||||||
|
*/
|
||||||
|
JSONObject document;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(final Bundle args) {
|
protected void onCreate(final Bundle args) {
|
||||||
@ -55,7 +76,6 @@ public class DocumentActivity extends ActionBarActivity {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
JSONObject document;
|
|
||||||
try {
|
try {
|
||||||
document = new JSONObject(documentJson);
|
document = new JSONObject(documentJson);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
@ -117,17 +137,33 @@ public class DocumentActivity extends ActionBarActivity {
|
|||||||
FileResource.list(this, id, new JsonHttpResponseHandler() {
|
FileResource.list(this, id, new JsonHttpResponseHandler() {
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
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.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);
|
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
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
switch (item.getItemId()) {
|
switch (item.getItemId()) {
|
||||||
|
case R.id.download_file:
|
||||||
|
downloadCurrentFile();
|
||||||
|
return true;
|
||||||
|
|
||||||
|
case R.id.download_document:
|
||||||
|
downloadZip();
|
||||||
|
return true;
|
||||||
|
|
||||||
case android.R.id.home:
|
case android.R.id.home:
|
||||||
finish();
|
finish();
|
||||||
return true;
|
return true;
|
||||||
@ -136,4 +172,55 @@ public class DocumentActivity extends ActionBarActivity {
|
|||||||
return super.onOptionsItemSelected(item);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
@ -89,4 +89,18 @@ public class FilePagerAdapter extends PagerAdapter {
|
|||||||
public boolean isViewFromObject(View view, Object object) {
|
public boolean isViewFromObject(View view, Object object) {
|
||||||
return view == 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,8 @@
|
|||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/createdDateLabel"
|
android:id="@+id/createdDateLabel"
|
||||||
android:layout_width="100dp"
|
android:layout_width="100dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="24dp"
|
||||||
|
android:gravity="center_vertical"
|
||||||
android:layout_alignParentTop="true"
|
android:layout_alignParentTop="true"
|
||||||
android:layout_marginRight="8dp"
|
android:layout_marginRight="8dp"
|
||||||
android:layout_marginEnd="8dp"
|
android:layout_marginEnd="8dp"
|
||||||
@ -27,7 +28,8 @@
|
|||||||
android:layout_toRightOf="@id/createdDateLabel"
|
android:layout_toRightOf="@id/createdDateLabel"
|
||||||
android:layout_toEndOf="@id/createdDateLabel"
|
android:layout_toEndOf="@id/createdDateLabel"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="24dp"
|
||||||
|
android:gravity="center_vertical"
|
||||||
android:layout_alignParentTop="true"
|
android:layout_alignParentTop="true"
|
||||||
android:fontFamily="sans-serif-light"
|
android:fontFamily="sans-serif-light"
|
||||||
android:text="01/12/2014"/>
|
android:text="01/12/2014"/>
|
||||||
|
17
docs-android/app/src/main/res/menu/document_activity.xml
Normal file
17
docs-android/app/src/main/res/menu/document_activity.xml
Normal 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>
|
@ -26,5 +26,9 @@
|
|||||||
<string name="validate_error_length_max">Too long (max. %d)</string>
|
<string name="validate_error_length_max">Too long (max. %d)</string>
|
||||||
<string name="validate_error_required">Required</string>
|
<string name="validate_error_required">Required</string>
|
||||||
<string name="validate_error_alphanumeric">Only letters and numbers</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>
|
</resources>
|
||||||
|
Loading…
Reference in New Issue
Block a user