New file saving (#502)
New file saving Reviewed-on: https://gitea.com/gitnex/GitNex/pulls/502 Reviewed-by: 6543 <6543@noreply.gitea.io>
This commit is contained in:
parent
e83b9eb736
commit
1b213783bc
@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
|
@ -1,14 +1,13 @@
|
|||||||
package org.mian.gitnex.activities;
|
package org.mian.gitnex.activities;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.Intent;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
import android.graphics.Typeface;
|
import android.graphics.Typeface;
|
||||||
import android.graphics.drawable.BitmapDrawable;
|
import android.graphics.drawable.BitmapDrawable;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.os.Build;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Environment;
|
|
||||||
import android.text.method.ScrollingMovementMethod;
|
import android.text.method.ScrollingMovementMethod;
|
||||||
import android.util.Base64;
|
import android.util.Base64;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@ -22,9 +21,8 @@ import android.widget.LinearLayout;
|
|||||||
import android.widget.ProgressBar;
|
import android.widget.ProgressBar;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
import androidx.appcompat.widget.Toolbar;
|
import androidx.appcompat.widget.Toolbar;
|
||||||
import androidx.core.app.ActivityCompat;
|
|
||||||
import androidx.core.content.ContextCompat;
|
|
||||||
import com.github.barteksc.pdfviewer.PDFView;
|
import com.github.barteksc.pdfviewer.PDFView;
|
||||||
import com.github.barteksc.pdfviewer.util.FitPolicy;
|
import com.github.barteksc.pdfviewer.util.FitPolicy;
|
||||||
import com.github.chrisbanes.photoview.PhotoView;
|
import com.github.chrisbanes.photoview.PhotoView;
|
||||||
@ -40,8 +38,8 @@ import org.mian.gitnex.models.Files;
|
|||||||
import org.mian.gitnex.util.AppUtil;
|
import org.mian.gitnex.util.AppUtil;
|
||||||
import org.mian.gitnex.util.TinyDB;
|
import org.mian.gitnex.util.TinyDB;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.URLDecoder;
|
import java.net.URLDecoder;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -67,7 +65,6 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie
|
|||||||
private LinearLayout pdfViewFrame;
|
private LinearLayout pdfViewFrame;
|
||||||
private byte[] decodedPdf;
|
private byte[] decodedPdf;
|
||||||
private Boolean pdfNightMode;
|
private Boolean pdfNightMode;
|
||||||
private static final int PERMISSION_REQUEST_CODE = 1;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected int getLayoutResourceId() {
|
protected int getLayoutResourceId() {
|
||||||
@ -304,21 +301,9 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie
|
|||||||
@Override
|
@Override
|
||||||
public void onButtonClicked(String text) {
|
public void onButtonClicked(String text) {
|
||||||
|
|
||||||
switch(text) {
|
if("downloadFile".equals(text)) {
|
||||||
case "downloadFile":
|
|
||||||
|
|
||||||
if(Build.VERSION.SDK_INT >= 23) {
|
requestFileDownload();
|
||||||
if(checkPermission()) {
|
|
||||||
requestFileDownload();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
requestPermission();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
requestFileDownload();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -330,24 +315,17 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie
|
|||||||
|
|
||||||
if(!tinyDb.getString("downloadFileContents").isEmpty()) {
|
if(!tinyDb.getString("downloadFileContents").isEmpty()) {
|
||||||
|
|
||||||
|
int CREATE_REQUEST_CODE = 40;
|
||||||
|
|
||||||
File outputFileName = new File(tinyDb.getString("downloadFileName"));
|
File outputFileName = new File(tinyDb.getString("downloadFileName"));
|
||||||
final File downloadFilePath = new File(Environment.getExternalStorageDirectory().getPath() + "/Download/" + outputFileName.getName());
|
|
||||||
|
|
||||||
byte[] pdfAsBytes = Base64.decode(tinyDb.getString("downloadFileContents"), 0);
|
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
|
||||||
FileOutputStream fileOutputStream = null;
|
|
||||||
|
|
||||||
try {
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||||
|
intent.setType("*/*");
|
||||||
|
intent.putExtra(Intent.EXTRA_TITLE, outputFileName.getName());
|
||||||
|
|
||||||
fileOutputStream = new FileOutputStream(downloadFilePath, false);
|
startActivityForResult(intent, CREATE_REQUEST_CODE);
|
||||||
Objects.requireNonNull(fileOutputStream).write(pdfAsBytes);
|
|
||||||
fileOutputStream.flush();
|
|
||||||
fileOutputStream.close();
|
|
||||||
Toasty.info(ctx, getString(R.string.downloadFileSaved));
|
|
||||||
|
|
||||||
}
|
|
||||||
catch(IOException e) {
|
|
||||||
Log.e("errorFileDownloading", Objects.requireNonNull(e.getMessage()));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -356,42 +334,47 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean checkPermission() {
|
|
||||||
|
|
||||||
int result = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
|
||||||
return result == PackageManager.PERMISSION_GRANTED;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void requestPermission() {
|
|
||||||
|
|
||||||
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
||||||
|
|
||||||
|
super.onActivityResult(requestCode, resultCode, data);
|
||||||
|
|
||||||
|
final TinyDB tinyDb = new TinyDB(appCtx);
|
||||||
|
|
||||||
|
if (requestCode == 40 && resultCode == RESULT_OK) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
assert data != null;
|
||||||
|
Uri uri = data.getData();
|
||||||
|
|
||||||
|
assert uri != null;
|
||||||
|
OutputStream outputStream = getContentResolver().openOutputStream(uri);
|
||||||
|
|
||||||
|
byte[] dataAsBytes = Base64.decode(tinyDb.getString("downloadFileContents"), 0);
|
||||||
|
|
||||||
|
assert outputStream != null;
|
||||||
|
outputStream.write(dataAsBytes);
|
||||||
|
outputStream.close();
|
||||||
|
|
||||||
|
Toasty.info(ctx, getString(R.string.downloadFileSaved));
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
Log.e("errorFileDownloading", Objects.requireNonNull(e.getMessage()));
|
||||||
|
}
|
||||||
|
|
||||||
switch(requestCode) {
|
|
||||||
case PERMISSION_REQUEST_CODE:
|
|
||||||
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
||||||
Log.i("PermissionsCheck", "Permission Granted");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Log.e("PermissionsCheck", "Permission Denied");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initCloseListener() {
|
private void initCloseListener() {
|
||||||
|
|
||||||
onClickListener = new View.OnClickListener() {
|
onClickListener = view -> {
|
||||||
|
|
||||||
@Override
|
getIntent().removeExtra("singleFileName");
|
||||||
public void onClick(View view) {
|
finish();
|
||||||
|
|
||||||
getIntent().removeExtra("singleFileName");
|
|
||||||
finish();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -605,7 +605,7 @@
|
|||||||
|
|
||||||
<string name="downloadFile">Download This File</string>
|
<string name="downloadFile">Download This File</string>
|
||||||
<string name="waitLoadingDownloadFile">Please wait for the file to load to memory</string>
|
<string name="waitLoadingDownloadFile">Please wait for the file to load to memory</string>
|
||||||
<string name="downloadFileSaved">File is saved to Download directory</string>
|
<string name="downloadFileSaved">File saved successfully</string>
|
||||||
<string name="excludeFilesInFileviewer">This file type is not supported in file viewer. Download it instead from the three dotted menu?</string>
|
<string name="excludeFilesInFileviewer">This file type is not supported in file viewer. Download it instead from the three dotted menu?</string>
|
||||||
|
|
||||||
<string name="sizeCopy">Size</string>
|
<string name="sizeCopy">Size</string>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user