2019-10-02 18:32:59 +02:00
|
|
|
package org.mian.gitnex.activities;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2019-10-30 15:04:49 +01:00
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
|
|
|
import android.graphics.drawable.Drawable;
|
2019-10-02 18:32:59 +02:00
|
|
|
import android.os.Bundle;
|
2019-10-17 19:47:27 +02:00
|
|
|
import android.text.method.ScrollingMovementMethod;
|
2019-10-30 15:04:49 +01:00
|
|
|
import android.util.Base64;
|
2019-10-02 18:32:59 +02:00
|
|
|
import android.util.Log;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.ImageView;
|
2020-03-04 17:15:57 +01:00
|
|
|
import android.widget.LinearLayout;
|
2019-10-02 18:32:59 +02:00
|
|
|
import android.widget.ProgressBar;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.appcompat.widget.Toolbar;
|
2020-01-30 19:00:31 +01:00
|
|
|
import com.github.barteksc.pdfviewer.PDFView;
|
|
|
|
import com.github.barteksc.pdfviewer.util.FitPolicy;
|
2019-10-30 15:04:49 +01:00
|
|
|
import com.github.chrisbanes.photoview.PhotoView;
|
2019-11-05 09:34:22 +01:00
|
|
|
import com.pddstudio.highlightjs.HighlightJsView;
|
|
|
|
import com.pddstudio.highlightjs.models.Theme;
|
2019-10-30 15:04:49 +01:00
|
|
|
import org.apache.commons.io.FilenameUtils;
|
2019-10-02 18:32:59 +02:00
|
|
|
import org.mian.gitnex.R;
|
|
|
|
import org.mian.gitnex.clients.RetrofitClient;
|
|
|
|
import org.mian.gitnex.helpers.AlertDialogs;
|
|
|
|
import org.mian.gitnex.helpers.Toasty;
|
|
|
|
import org.mian.gitnex.models.Files;
|
|
|
|
import org.mian.gitnex.util.AppUtil;
|
|
|
|
import org.mian.gitnex.util.TinyDB;
|
2019-12-13 09:55:58 +01:00
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.URLDecoder;
|
2019-10-02 18:32:59 +02:00
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Author M M Arif
|
|
|
|
*/
|
|
|
|
|
2020-03-04 17:15:57 +01:00
|
|
|
public class FileViewActivity extends BaseActivity {
|
2019-10-02 18:32:59 +02:00
|
|
|
|
|
|
|
private View.OnClickListener onClickListener;
|
|
|
|
private TextView singleFileContents;
|
2020-03-04 17:15:57 +01:00
|
|
|
private LinearLayout singleFileContentsFrame;
|
2019-11-05 09:34:22 +01:00
|
|
|
private HighlightJsView singleCodeContents;
|
2019-10-30 15:04:49 +01:00
|
|
|
private PhotoView imageView;
|
2019-10-02 18:32:59 +02:00
|
|
|
final Context ctx = this;
|
|
|
|
private ProgressBar mProgressBar;
|
2019-10-30 15:04:49 +01:00
|
|
|
private byte[] imageData;
|
2020-01-30 19:00:31 +01:00
|
|
|
private PDFView pdfView;
|
2020-03-04 17:15:57 +01:00
|
|
|
private LinearLayout pdfViewFrame;
|
2020-01-30 19:00:31 +01:00
|
|
|
private byte[] decodedPdf;
|
2020-03-06 14:46:22 +01:00
|
|
|
private Boolean $nightMode;
|
2019-10-02 18:32:59 +02:00
|
|
|
|
|
|
|
@Override
|
2020-03-04 17:15:57 +01:00
|
|
|
protected int getLayoutResourceId(){
|
|
|
|
return R.layout.activity_file_view;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
2019-10-02 18:32:59 +02:00
|
|
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
|
|
|
setSupportActionBar(toolbar);
|
|
|
|
|
|
|
|
final TinyDB tinyDb = new TinyDB(getApplicationContext());
|
|
|
|
String repoFullName = tinyDb.getString("repoFullName");
|
|
|
|
String[] parts = repoFullName.split("/");
|
|
|
|
final String repoOwner = parts[0];
|
|
|
|
final String repoName = parts[1];
|
|
|
|
final String instanceUrl = tinyDb.getString("instanceUrl");
|
|
|
|
final String loginUid = tinyDb.getString("loginUid");
|
|
|
|
final String instanceToken = "token " + tinyDb.getString(loginUid + "-token");
|
|
|
|
|
|
|
|
ImageView closeActivity = findViewById(R.id.close);
|
|
|
|
singleFileContents = findViewById(R.id.singleFileContents);
|
2019-11-05 09:34:22 +01:00
|
|
|
singleCodeContents = findViewById(R.id.singleCodeContents);
|
2019-10-30 15:04:49 +01:00
|
|
|
imageView = findViewById(R.id.imageView);
|
2019-10-02 18:32:59 +02:00
|
|
|
mProgressBar = findViewById(R.id.progress_bar);
|
2020-01-30 19:00:31 +01:00
|
|
|
pdfView = findViewById(R.id.pdfView);
|
2020-03-04 17:15:57 +01:00
|
|
|
pdfViewFrame = findViewById(R.id.pdfViewFrame);
|
|
|
|
singleFileContentsFrame = findViewById(R.id.singleFileContentsFrame);
|
2019-10-02 18:32:59 +02:00
|
|
|
|
|
|
|
String singleFileName = getIntent().getStringExtra("singleFileName");
|
|
|
|
|
|
|
|
TextView toolbar_title = findViewById(R.id.toolbar_title);
|
2019-10-17 19:47:27 +02:00
|
|
|
toolbar_title.setMovementMethod(new ScrollingMovementMethod());
|
2019-10-02 18:32:59 +02:00
|
|
|
|
|
|
|
initCloseListener();
|
|
|
|
closeActivity.setOnClickListener(onClickListener);
|
|
|
|
|
2019-12-13 09:55:58 +01:00
|
|
|
try {
|
|
|
|
|
|
|
|
singleFileName = URLDecoder.decode(singleFileName, "UTF-8");
|
|
|
|
singleFileName = singleFileName.replaceAll("//", "/");
|
2019-12-25 20:14:33 +01:00
|
|
|
singleFileName = singleFileName.startsWith("/") ? singleFileName.substring(1) : singleFileName;
|
2019-12-13 09:55:58 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
catch (UnsupportedEncodingException e) {
|
|
|
|
|
|
|
|
assert singleFileName != null;
|
|
|
|
Log.i("singleFileName", singleFileName);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-25 20:14:33 +01:00
|
|
|
toolbar_title.setText(singleFileName);
|
|
|
|
|
2019-11-13 16:52:49 +01:00
|
|
|
getSingleFileContents(instanceUrl, instanceToken, repoOwner, repoName, singleFileName);
|
2019-10-02 18:32:59 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void getSingleFileContents(String instanceUrl, String token, final String owner, String repo, final String filename) {
|
|
|
|
|
2020-03-06 14:46:22 +01:00
|
|
|
final TinyDB tinyDb = new TinyDB(getApplicationContext());
|
|
|
|
|
2019-10-02 18:32:59 +02:00
|
|
|
Call<Files> call = RetrofitClient
|
2019-11-13 16:52:49 +01:00
|
|
|
.getInstance(instanceUrl, getApplicationContext())
|
2019-10-02 18:32:59 +02:00
|
|
|
.getApiInterface()
|
|
|
|
.getSingleFileContents(token, owner, repo, filename);
|
|
|
|
|
|
|
|
call.enqueue(new Callback<Files>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<Files> call, @NonNull retrofit2.Response<Files> response) {
|
|
|
|
|
|
|
|
if (response.code() == 200) {
|
|
|
|
|
|
|
|
AppUtil appUtil = new AppUtil();
|
|
|
|
assert response.body() != null;
|
|
|
|
|
|
|
|
if(!response.body().getContent().equals("")) {
|
2019-10-30 15:04:49 +01:00
|
|
|
|
|
|
|
String fileExtension = FilenameUtils.getExtension(filename);
|
2019-10-02 18:32:59 +02:00
|
|
|
mProgressBar.setVisibility(View.GONE);
|
2019-10-30 15:04:49 +01:00
|
|
|
|
2019-11-05 09:34:22 +01:00
|
|
|
if(appUtil.imageExtension(fileExtension)) { // file is image
|
2019-10-30 15:04:49 +01:00
|
|
|
|
2020-03-04 17:15:57 +01:00
|
|
|
singleFileContentsFrame.setVisibility(View.GONE);
|
2019-11-05 09:34:22 +01:00
|
|
|
singleCodeContents.setVisibility(View.GONE);
|
2020-03-04 17:15:57 +01:00
|
|
|
pdfViewFrame.setVisibility(View.GONE);
|
2019-10-30 15:04:49 +01:00
|
|
|
imageView.setVisibility(View.VISIBLE);
|
2019-11-05 09:34:22 +01:00
|
|
|
|
2019-10-30 15:04:49 +01:00
|
|
|
imageData = Base64.decode(response.body().getContent(), Base64.DEFAULT);
|
|
|
|
Drawable imageDrawable = new BitmapDrawable(getResources(), BitmapFactory.decodeByteArray(imageData, 0, imageData.length));
|
|
|
|
imageView.setImageDrawable(imageDrawable);
|
|
|
|
|
|
|
|
}
|
2019-11-05 09:34:22 +01:00
|
|
|
else if (appUtil.sourceCodeExtension(fileExtension)) { // file is sourcecode
|
|
|
|
|
|
|
|
imageView.setVisibility(View.GONE);
|
2020-03-04 17:15:57 +01:00
|
|
|
singleFileContentsFrame.setVisibility(View.GONE);
|
|
|
|
pdfViewFrame.setVisibility(View.GONE);
|
2019-11-05 09:34:22 +01:00
|
|
|
singleCodeContents.setVisibility(View.VISIBLE);
|
|
|
|
|
|
|
|
singleCodeContents.setTheme(Theme.GRUVBOX_DARK);
|
|
|
|
singleCodeContents.setShowLineNumbers(true);
|
|
|
|
singleCodeContents.setSource(appUtil.decodeBase64(response.body().getContent()));
|
|
|
|
|
2020-01-30 19:00:31 +01:00
|
|
|
}
|
|
|
|
else if (appUtil.pdfExtension(fileExtension)) { // file is pdf
|
|
|
|
|
|
|
|
imageView.setVisibility(View.GONE);
|
2020-03-04 17:15:57 +01:00
|
|
|
singleFileContentsFrame.setVisibility(View.GONE);
|
2020-01-30 19:00:31 +01:00
|
|
|
singleCodeContents.setVisibility(View.GONE);
|
2020-03-04 17:15:57 +01:00
|
|
|
pdfViewFrame.setVisibility(View.VISIBLE);
|
2020-01-30 19:00:31 +01:00
|
|
|
|
2020-03-06 14:46:22 +01:00
|
|
|
$nightMode = tinyDb.getBoolean("enablePdfMode");
|
|
|
|
|
2020-01-30 19:00:31 +01:00
|
|
|
decodedPdf = Base64.decode(response.body().getContent(), Base64.DEFAULT);
|
|
|
|
pdfView.fromBytes(decodedPdf)
|
|
|
|
.enableSwipe(true)
|
|
|
|
.swipeHorizontal(false)
|
|
|
|
.enableDoubletap(true)
|
|
|
|
.defaultPage(0)
|
|
|
|
.enableAnnotationRendering(false)
|
|
|
|
.password(null)
|
|
|
|
.scrollHandle(null)
|
|
|
|
.enableAntialiasing(true)
|
|
|
|
.spacing(0)
|
|
|
|
.autoSpacing(true)
|
|
|
|
.pageFitPolicy(FitPolicy.WIDTH)
|
|
|
|
.fitEachPage(true)
|
|
|
|
.pageSnap(false)
|
|
|
|
.pageFling(true)
|
2020-03-06 14:46:22 +01:00
|
|
|
.nightMode($nightMode)
|
2020-01-30 19:00:31 +01:00
|
|
|
.load();
|
|
|
|
|
2019-11-05 09:34:22 +01:00
|
|
|
}
|
|
|
|
else { // file type not known - plain text view
|
2019-10-30 15:04:49 +01:00
|
|
|
|
|
|
|
imageView.setVisibility(View.GONE);
|
2019-11-05 09:34:22 +01:00
|
|
|
singleCodeContents.setVisibility(View.GONE);
|
2020-03-04 17:15:57 +01:00
|
|
|
pdfViewFrame.setVisibility(View.GONE);
|
|
|
|
singleFileContentsFrame.setVisibility(View.VISIBLE);
|
2019-11-05 09:34:22 +01:00
|
|
|
|
2019-10-30 15:04:49 +01:00
|
|
|
singleFileContents.setText(appUtil.decodeBase64(response.body().getContent()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-02 18:32:59 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
singleFileContents.setText("");
|
|
|
|
mProgressBar.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else if(response.code() == 401) {
|
|
|
|
|
|
|
|
AlertDialogs.authorizationTokenRevokedDialog(ctx, getResources().getString(R.string.alertDialogTokenRevokedTitle),
|
|
|
|
getResources().getString(R.string.alertDialogTokenRevokedMessage),
|
|
|
|
getResources().getString(R.string.alertDialogTokenRevokedCopyNegativeButton),
|
|
|
|
getResources().getString(R.string.alertDialogTokenRevokedCopyPositiveButton));
|
|
|
|
|
|
|
|
}
|
|
|
|
else if(response.code() == 403) {
|
|
|
|
|
|
|
|
Toasty.info(ctx, ctx.getString(R.string.authorizeError));
|
|
|
|
|
|
|
|
}
|
|
|
|
else if(response.code() == 404) {
|
|
|
|
|
|
|
|
Toasty.info(ctx, ctx.getString(R.string.apiNotFound));
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
Toasty.info(getApplicationContext(), getString(R.string.labelGeneralError));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<Files> call, @NonNull Throwable t) {
|
|
|
|
Log.e("onFailure", t.toString());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initCloseListener() {
|
|
|
|
onClickListener = new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
getIntent().removeExtra("singleFileName");
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|