mirror of
https://github.com/sismics/docs.git
synced 2024-12-22 11:23:48 +01:00
Closes #55: Android: PDF download
This commit is contained in:
parent
7f325e3eb5
commit
509ab82745
@ -93,6 +93,7 @@
|
||||
<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/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/res" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/restart-dex" />
|
||||
|
@ -3,7 +3,7 @@ buildscript {
|
||||
jcenter()
|
||||
}
|
||||
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'
|
||||
|
@ -1,16 +1,13 @@
|
||||
package com.sismics.docs.activity;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.DownloadManager;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.ClipData;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.support.v4.app.DialogFragment;
|
||||
import android.support.v4.view.GravityCompat;
|
||||
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.FileResource;
|
||||
import com.sismics.docs.service.FileUploadService;
|
||||
import com.sismics.docs.util.NetworkUtil;
|
||||
import com.sismics.docs.util.PreferenceUtil;
|
||||
import com.sismics.docs.util.TagUtil;
|
||||
|
||||
@ -250,7 +248,8 @@ public class DocumentViewActivity extends AppCompatActivity {
|
||||
button.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
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");
|
||||
}
|
||||
});
|
||||
@ -365,11 +364,11 @@ public class DocumentViewActivity extends AppCompatActivity {
|
||||
int position = fileViewPager.getCurrentItem();
|
||||
if (mimeType == null || !mimeType.contains("/")) return;
|
||||
String ext = mimeType.split("/")[1];
|
||||
String fileName = getTitle() + "-" + position + "." + ext;
|
||||
String fileName = document.optString("title") + "-" + 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));
|
||||
NetworkUtil.downloadFile(this, fileUrl, fileName, document.optString("title"), getString(R.string.download_file_title));
|
||||
}
|
||||
|
||||
private void deleteCurrentFile() {
|
||||
@ -428,28 +427,8 @@ public class DocumentViewActivity extends AppCompatActivity {
|
||||
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);
|
||||
String fileName = document.optString("title") + ".zip";
|
||||
NetworkUtil.downloadFile(this, url, fileName, document.optString("title"), getString(R.string.download_document_title));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -8,8 +8,15 @@ import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.DialogFragment;
|
||||
import android.view.LayoutInflater;
|
||||
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.util.NetworkUtil;
|
||||
import com.sismics.docs.util.PreferenceUtil;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Export PDF dialog fragment.
|
||||
@ -21,11 +28,13 @@ public class DocExportPdfFragment extends DialogFragment {
|
||||
* Export PDF dialog fragment.
|
||||
*
|
||||
* @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();
|
||||
Bundle args = new Bundle();
|
||||
args.putString("id", id);
|
||||
args.putString("title", title);
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
}
|
||||
@ -38,11 +47,41 @@ public class DocExportPdfFragment extends DialogFragment {
|
||||
// Setup the view
|
||||
LayoutInflater inflater = getActivity().getLayoutInflater();
|
||||
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
|
||||
builder.setView(view)
|
||||
.setPositiveButton(R.string.download, new DialogInterface.OnClickListener() {
|
||||
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();
|
||||
}
|
||||
})
|
||||
|
@ -10,7 +10,6 @@ import android.content.pm.PackageManager.NameNotFoundException;
|
||||
* @author bgamard
|
||||
*/
|
||||
public class ApplicationUtil {
|
||||
|
||||
/**
|
||||
* Returns version name.
|
||||
*
|
||||
|
@ -12,7 +12,6 @@ import com.sismics.docs.R;
|
||||
* @author bgamard
|
||||
*/
|
||||
public class DialogUtil {
|
||||
|
||||
/**
|
||||
* Create a dialog with an OK button.
|
||||
*
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -8,23 +8,59 @@
|
||||
<CheckBox
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Export metadata"
|
||||
android:id="@+id/checkBox" />
|
||||
android:layout_margin="6dp"
|
||||
android:text="@string/export_metadata"
|
||||
android:id="@+id/exportMetadataCheckbox" />
|
||||
|
||||
<CheckBox
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Export comments"
|
||||
android:id="@+id/checkBox2" />
|
||||
android:layout_margin="6dp"
|
||||
android:text="@string/export_comments"
|
||||
android:id="@+id/exportCommentsCheckbox" />
|
||||
|
||||
<CheckBox
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Fit image to page"
|
||||
android:id="@+id/checkBox3" />
|
||||
android:layout_margin="6dp"
|
||||
android:checked="true"
|
||||
android:text="@string/fit_image_to_page"
|
||||
android:id="@+id/fitToPageCheckbox" />
|
||||
|
||||
<SeekBar
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
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>
|
@ -28,9 +28,7 @@
|
||||
<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="download_file">Download current file</string>
|
||||
<string name="downloading_file">Downloading file number %1s</string>
|
||||
<string name="download_document">Download</string>
|
||||
<string name="downloading_document">Downloading document</string>
|
||||
<string name="action_search">Search documents</string>
|
||||
<string name="all_documents">All documents</string>
|
||||
<string name="shared_documents">Shared documents</string>
|
||||
@ -119,6 +117,13 @@
|
||||
<string name="error_deleting_comment">Error deleting comment</string>
|
||||
<string name="export_pdf">Export PDF</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>
|
||||
|
Loading…
Reference in New Issue
Block a user