Closes #55: Android: PDF download

This commit is contained in:
jendib 2016-02-11 23:29:52 +01:00
parent 7f325e3eb5
commit 509ab82745
9 changed files with 134 additions and 43 deletions

View File

@ -93,6 +93,7 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/reload-dex" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/reload-dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/restart-dex" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/restart-dex" />

View File

@ -3,7 +3,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-beta2' classpath 'com.android.tools.build:gradle:2.0.0-beta3'
} }
} }
apply plugin: 'com.android.application' apply plugin: 'com.android.application'

View File

@ -1,16 +1,13 @@
package com.sismics.docs.activity; package com.sismics.docs.activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.DownloadManager;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.ClipData; import android.content.ClipData;
import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.support.v4.view.GravityCompat; import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
@ -52,6 +49,7 @@ import com.sismics.docs.resource.CommentResource;
import com.sismics.docs.resource.DocumentResource; import com.sismics.docs.resource.DocumentResource;
import com.sismics.docs.resource.FileResource; import com.sismics.docs.resource.FileResource;
import com.sismics.docs.service.FileUploadService; import com.sismics.docs.service.FileUploadService;
import com.sismics.docs.util.NetworkUtil;
import com.sismics.docs.util.PreferenceUtil; import com.sismics.docs.util.PreferenceUtil;
import com.sismics.docs.util.TagUtil; import com.sismics.docs.util.TagUtil;
@ -250,7 +248,8 @@ public class DocumentViewActivity extends AppCompatActivity {
button.setOnClickListener(new View.OnClickListener() { button.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
DialogFragment dialog = DocExportPdfFragment.newInstance(DocumentViewActivity.this.document.optString("id")); DialogFragment dialog = DocExportPdfFragment.newInstance(
DocumentViewActivity.this.document.optString("id"), DocumentViewActivity.this.document.optString("title"));
dialog.show(getSupportFragmentManager(), "DocExportPdfFragment"); dialog.show(getSupportFragmentManager(), "DocExportPdfFragment");
} }
}); });
@ -365,11 +364,11 @@ public class DocumentViewActivity extends AppCompatActivity {
int position = fileViewPager.getCurrentItem(); int position = fileViewPager.getCurrentItem();
if (mimeType == null || !mimeType.contains("/")) return; if (mimeType == null || !mimeType.contains("/")) return;
String ext = mimeType.split("/")[1]; String ext = mimeType.split("/")[1];
String fileName = getTitle() + "-" + position + "." + ext; String fileName = document.optString("title") + "-" + position + "." + ext;
// Download the file // Download the file
String fileUrl = PreferenceUtil.getServerUrl(this) + "/api/file/" + file.optString("id") + "/data"; String fileUrl = PreferenceUtil.getServerUrl(this) + "/api/file/" + file.optString("id") + "/data";
downloadFile(fileUrl, fileName, getTitle().toString(), getString(R.string.downloading_file, position + 1)); NetworkUtil.downloadFile(this, fileUrl, fileName, document.optString("title"), getString(R.string.download_file_title));
} }
private void deleteCurrentFile() { private void deleteCurrentFile() {
@ -428,28 +427,8 @@ public class DocumentViewActivity extends AppCompatActivity {
private void downloadZip() { private void downloadZip() {
if (document == null) return; if (document == null) return;
String url = PreferenceUtil.getServerUrl(this) + "/api/file/zip?id=" + document.optString("id"); String url = PreferenceUtil.getServerUrl(this) + "/api/file/zip?id=" + document.optString("id");
String fileName = getTitle() + ".zip"; String fileName = document.optString("title") + ".zip";
downloadFile(url, fileName, getTitle().toString(), getString(R.string.downloading_document)); NetworkUtil.downloadFile(this, url, fileName, document.optString("title"), getString(R.string.download_document_title));
}
/**
* 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

@ -8,8 +8,15 @@ import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.TextView;
import com.sismics.docs.R; import com.sismics.docs.R;
import com.sismics.docs.util.NetworkUtil;
import com.sismics.docs.util.PreferenceUtil;
import java.util.Locale;
/** /**
* Export PDF dialog fragment. * Export PDF dialog fragment.
@ -21,11 +28,13 @@ public class DocExportPdfFragment extends DialogFragment {
* Export PDF dialog fragment. * Export PDF dialog fragment.
* *
* @param id Document ID * @param id Document ID
* @param title Document title
*/ */
public static DocExportPdfFragment newInstance(String id) { public static DocExportPdfFragment newInstance(String id, String title) {
DocExportPdfFragment fragment = new DocExportPdfFragment(); DocExportPdfFragment fragment = new DocExportPdfFragment();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putString("id", id); args.putString("id", id);
args.putString("title", title);
fragment.setArguments(args); fragment.setArguments(args);
return fragment; return fragment;
} }
@ -38,11 +47,41 @@ public class DocExportPdfFragment extends DialogFragment {
// Setup the view // Setup the view
LayoutInflater inflater = getActivity().getLayoutInflater(); LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.document_export_pdf_dialog, null); View view = inflater.inflate(R.layout.document_export_pdf_dialog, null);
final SeekBar marginSeekBar = (SeekBar) view.findViewById(R.id.marginSeekBar);
final CheckBox exportMetadataCheckbox = (CheckBox) view.findViewById(R.id.exportMetadataCheckbox);
final CheckBox exportCommentsCheckbox = (CheckBox) view.findViewById(R.id.exportCommentsCheckbox);
final CheckBox fitToPageCheckbox = (CheckBox) view.findViewById(R.id.fitToPageCheckbox);
final TextView marginValueText = (TextView) view.findViewById(R.id.marginValueText);
// Margin label follow seekbar value
marginSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
marginValueText.setText(String.format(Locale.ENGLISH, "%d", progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
// Build the dialog // Build the dialog
builder.setView(view) builder.setView(view)
.setPositiveButton(R.string.download, new DialogInterface.OnClickListener() { .setPositiveButton(R.string.download, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { public void onClick(DialogInterface dialog, int id) {
// Download the PDF
String pdfUrl = PreferenceUtil.getServerUrl(getActivity()) + "/api/document/" + getArguments().getString("id") + "/pdf?" +
"metadata=" + exportMetadataCheckbox.isChecked() + "&comments=" + exportCommentsCheckbox.isChecked() + "&fitimagetopage=" + fitToPageCheckbox.isChecked() +
"&margin=" + marginSeekBar.getProgress();
String title = getArguments().getString("title");
NetworkUtil.downloadFile(getActivity(), pdfUrl, title + ".pdf", title, getString(R.string.download_pdf_title));
getDialog().cancel(); getDialog().cancel();
} }
}) })

View File

@ -10,7 +10,6 @@ import android.content.pm.PackageManager.NameNotFoundException;
* @author bgamard * @author bgamard
*/ */
public class ApplicationUtil { public class ApplicationUtil {
/** /**
* Returns version name. * Returns version name.
* *

View File

@ -12,7 +12,6 @@ import com.sismics.docs.R;
* @author bgamard * @author bgamard
*/ */
public class DialogUtil { public class DialogUtil {
/** /**
* Create a dialog with an OK button. * Create a dialog with an OK button.
* *

View File

@ -0,0 +1,33 @@
package com.sismics.docs.util;
import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.os.Environment;
/**
* Utility class for network actions.
*
* @author bgamard.
*/
public class NetworkUtil {
/**
* 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
*/
public static void downloadFile(Context context, String url, String fileName, String title, String description) {
String authToken = PreferenceUtil.getAuthToken(context);
DownloadManager downloadManager = (DownloadManager) context.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

@ -8,23 +8,59 @@
<CheckBox <CheckBox
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Export metadata" android:layout_margin="6dp"
android:id="@+id/checkBox" /> android:text="@string/export_metadata"
android:id="@+id/exportMetadataCheckbox" />
<CheckBox <CheckBox
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Export comments" android:layout_margin="6dp"
android:id="@+id/checkBox2" /> android:text="@string/export_comments"
android:id="@+id/exportCommentsCheckbox" />
<CheckBox <CheckBox
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Fit image to page" android:layout_margin="6dp"
android:id="@+id/checkBox3" /> android:checked="true"
android:text="@string/fit_image_to_page"
android:id="@+id/fitToPageCheckbox" />
<SeekBar <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/seekBar" /> android:layout_margin="16dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/margin"
android:layout_weight="0"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/marginSeekBar"
android:progress="10"
android:max="50"/>
<TextView
android:id="@+id/marginValueText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="10"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginStart="4dp"
android:layout_weight="0"
android:text="@string/mm"/>
</LinearLayout>
</LinearLayout> </LinearLayout>

View File

@ -28,9 +28,7 @@
<string name="crash_toast_text">A crash occurred, a report has been sent to resolve this problem</string> <string name="crash_toast_text">A crash occurred, a report has been sent to resolve this problem</string>
<string name="created_date">Created date</string> <string name="created_date">Created date</string>
<string name="download_file">Download current file</string> <string name="download_file">Download current file</string>
<string name="downloading_file">Downloading file number %1s</string>
<string name="download_document">Download</string> <string name="download_document">Download</string>
<string name="downloading_document">Downloading document</string>
<string name="action_search">Search documents</string> <string name="action_search">Search documents</string>
<string name="all_documents">All documents</string> <string name="all_documents">All documents</string>
<string name="shared_documents">Shared documents</string> <string name="shared_documents">Shared documents</string>
@ -119,6 +117,13 @@
<string name="error_deleting_comment">Error deleting comment</string> <string name="error_deleting_comment">Error deleting comment</string>
<string name="export_pdf">Export PDF</string> <string name="export_pdf">Export PDF</string>
<string name="download">Download</string> <string name="download">Download</string>
<string name="margin">Margin</string>
<string name="fit_image_to_page">Fit image to page</string>
<string name="export_comments">Export comments</string>
<string name="export_metadata">Export metadata</string>
<string name="mm">mm</string>
<string name="download_file_title">Sismics Docs file export</string>
<string name="download_document_title">Sismics Docs document export</string>
<string name="download_pdf_title">Sismics Docs PDF export</string>
</resources> </resources>