From 463961800fd3ec05aed83f931a660218ea13f1c2 Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply@gitea.io> Date: Tue, 5 Nov 2019 08:34:22 +0000 Subject: [PATCH] [File View] add highlight sourcecode (#187) --- app/build.gradle | 1 + .../gitnex/activities/FileViewActivity.java | 23 +++++++++++-- .../java/org/mian/gitnex/util/AppUtil.java | 32 +++++++++++++++---- .../main/res/layout/activity_file_view.xml | 14 ++++++-- build.gradle | 2 +- 5 files changed, 60 insertions(+), 12 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 3099aa37..bcd7572a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -68,4 +68,5 @@ dependencies { implementation "com.github.HamidrezaAmz:BreadcrumbsView:0.2.9" implementation "commons-io:commons-io:2.6" implementation "com.github.chrisbanes:PhotoView:2.3.0" + implementation "com.pddstudio:highlightjs-android:1.5.0" } diff --git a/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java b/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java index 0db99d01..6d94333d 100644 --- a/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java @@ -16,6 +16,8 @@ import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import com.github.chrisbanes.photoview.PhotoView; +import com.pddstudio.highlightjs.HighlightJsView; +import com.pddstudio.highlightjs.models.Theme; import org.apache.commons.io.FilenameUtils; import org.mian.gitnex.R; import org.mian.gitnex.clients.RetrofitClient; @@ -35,6 +37,7 @@ public class FileViewActivity extends AppCompatActivity { private View.OnClickListener onClickListener; private TextView singleFileContents; + private HighlightJsView singleCodeContents; private PhotoView imageView; final Context ctx = this; private ProgressBar mProgressBar; @@ -61,6 +64,7 @@ public class FileViewActivity extends AppCompatActivity { ImageView closeActivity = findViewById(R.id.close); singleFileContents = findViewById(R.id.singleFileContents); + singleCodeContents = findViewById(R.id.singleCodeContents); imageView = findViewById(R.id.imageView); singleFileContents.setVisibility(View.GONE); mProgressBar = findViewById(R.id.progress_bar); @@ -105,19 +109,34 @@ public class FileViewActivity extends AppCompatActivity { String fileExtension = FilenameUtils.getExtension(filename); mProgressBar.setVisibility(View.GONE); - if(fileExtension.equals("png") || fileExtension.equals("jpg") || fileExtension.equals("jpeg") || fileExtension.equals("gif")) { + if(appUtil.imageExtension(fileExtension)) { // file is image singleFileContents.setVisibility(View.GONE); + singleCodeContents.setVisibility(View.GONE); imageView.setVisibility(View.VISIBLE); + imageData = Base64.decode(response.body().getContent(), Base64.DEFAULT); Drawable imageDrawable = new BitmapDrawable(getResources(), BitmapFactory.decodeByteArray(imageData, 0, imageData.length)); imageView.setImageDrawable(imageDrawable); } - else { + else if (appUtil.sourceCodeExtension(fileExtension)) { // file is sourcecode imageView.setVisibility(View.GONE); + singleFileContents.setVisibility(View.GONE); + singleCodeContents.setVisibility(View.VISIBLE); + + singleCodeContents.setTheme(Theme.GRUVBOX_DARK); + singleCodeContents.setShowLineNumbers(true); + singleCodeContents.setSource(appUtil.decodeBase64(response.body().getContent())); + + } + else { // file type not known - plain text view + + imageView.setVisibility(View.GONE); + singleCodeContents.setVisibility(View.GONE); singleFileContents.setVisibility(View.VISIBLE); + singleFileContents.setText(appUtil.decodeBase64(response.body().getContent())); } diff --git a/app/src/main/java/org/mian/gitnex/util/AppUtil.java b/app/src/main/java/org/mian/gitnex/util/AppUtil.java index e9f86255..66e8116e 100644 --- a/app/src/main/java/org/mian/gitnex/util/AppUtil.java +++ b/app/src/main/java/org/mian/gitnex/util/AppUtil.java @@ -13,6 +13,7 @@ import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import java.text.DecimalFormat; +import java.util.Arrays; import java.util.Calendar; import java.util.Locale; @@ -145,16 +146,16 @@ public class AppUtil { String sMonth; if (Integer.parseInt(month) < 10) { - sMonth = "0"+String.valueOf(month); + sMonth = "0"+ month; } else { - sMonth = String.valueOf(month); + sMonth = month; } String sDay; if (Integer.parseInt(day) < 10) { - sDay = "0"+String.valueOf(day); + sDay = "0"+ day; } else { - sDay = String.valueOf(day); + sDay = day; } return year + "-" + sMonth + "-" + sDay; @@ -170,14 +171,14 @@ public class AppUtil { String sMin; if ((mMinute) < 10) { - sMin = "0"+String.valueOf(mMinute); + sMin = "0"+ mMinute; } else { sMin = String.valueOf(mMinute); } String sSec; if ((mSeconds) < 10) { - sSec = "0"+String.valueOf(mSeconds); + sSec = "0"+ mSeconds; } else { sSec = String.valueOf(mSeconds); } @@ -210,4 +211,23 @@ public class AppUtil { } + public Boolean sourceCodeExtension(String ext) { + + String[] extValues = new String[] {"md", "json", "java", "go", "php", "c", "cc", "cpp", "cxx", "cyc", "m", + "cs", "bash", "sh", "bsh", "cv", "python", "perl", "pm", "rb", "ruby", "javascript", + "coffee", "rc", "rs", "rust", "basic", "clj", "css", "dart", "lisp", "erl", "hs", "lsp", "rkt", + "ss", "llvm", "ll", "lua", "matlab", "pascal", "r", "scala", "sql", "latex", "tex", "vb", "vbs", + "vhd", "tcl", "wiki.meta", "yaml", "yml", "markdown", "xml", "proto", "regex", "py", "pl", "js"}; + + return Arrays.asList(extValues).contains(ext); + + } + + public Boolean imageExtension(String ext) { + + String[] extValues = new String[] {"jpg", "jpeg", "gif", "png", "ico"}; + + return Arrays.asList(extValues).contains(ext); + + } } diff --git a/app/src/main/res/layout/activity_file_view.xml b/app/src/main/res/layout/activity_file_view.xml index d7f03d71..13946674 100644 --- a/app/src/main/res/layout/activity_file_view.xml +++ b/app/src/main/res/layout/activity_file_view.xml @@ -1,10 +1,10 @@ + +