From b6d2758875fe83087e7121342f3f82a831ac6e7d Mon Sep 17 00:00:00 2001 From: M M Arif Date: Mon, 13 Apr 2020 02:08:34 +0500 Subject: [PATCH 1/4] Themes for source code in file viewer --- .../gitnex/activities/FileViewActivity.java | 20 ++++++++- .../gitnex/fragments/SettingsFragment.java | 45 +++++++++++++++++++ .../res/layout/layout_settings_fileview.xml | 30 +++++++++++++ app/src/main/res/values/strings.xml | 3 ++ 4 files changed, 97 insertions(+), 1 deletion(-) 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 b57f0e9..549b255 100644 --- a/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java @@ -175,7 +175,25 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie pdfViewFrame.setVisibility(View.GONE); singleCodeContents.setVisibility(View.VISIBLE); - singleCodeContents.setTheme(Theme.GRUVBOX_DARK); + if(tinyDb.getInt("fileviewerSourceCodeThemeId") == 1) { + singleCodeContents.setTheme(Theme.ARDUINO_LIGHT); + } + else if(tinyDb.getInt("fileviewerSourceCodeThemeId") == 2) { + singleCodeContents.setTheme(Theme.GITHUB); + } + else if(tinyDb.getInt("fileviewerSourceCodeThemeId") == 3) { + singleCodeContents.setTheme(Theme.FAR); + } + else if(tinyDb.getInt("fileviewerSourceCodeThemeId") == 4) { + singleCodeContents.setTheme(Theme.IR_BLACK); + } + else if(tinyDb.getInt("fileviewerSourceCodeThemeId") == 5) { + singleCodeContents.setTheme(Theme.ANDROID_STUDIO); + } + else { + singleCodeContents.setTheme(Theme.MONOKAI_SUBLIME); + } + singleCodeContents.setShowLineNumbers(true); singleCodeContents.setSource(appUtil.decodeBase64(response.body().getContent())); diff --git a/app/src/main/java/org/mian/gitnex/fragments/SettingsFragment.java b/app/src/main/java/org/mian/gitnex/fragments/SettingsFragment.java index 4202d91..fa5a3d3 100644 --- a/app/src/main/java/org/mian/gitnex/fragments/SettingsFragment.java +++ b/app/src/main/java/org/mian/gitnex/fragments/SettingsFragment.java @@ -47,6 +47,9 @@ public class SettingsFragment extends Fragment { private static String[] themeList = {"Dark", "Light", "Auto (Day/Night)"}; private static int themeSelectedChoice = 0; + private static String[] fileveiwerSourceCodeThemesList = {"Sublime", "Arduino Light", "Github", "Far ", "Ir Black", "Android Studio"}; + private static int fileveiwerSourceCodeThemesSelectedChoice = 0; + @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -60,6 +63,7 @@ public class SettingsFragment extends Fragment { final TextView homeScreenSelected = v.findViewById(R.id.homeScreenSelected); // setter for home screen final TextView customFontSelected = v.findViewById(R.id.customFontSelected); // setter for custom font final TextView themeSelected = v.findViewById(R.id.themeSelected); // setter for theme + final TextView fileveiwerSourceCodeThemesSelected = v.findViewById(R.id.sourceCodeThemeSelected); // setter for fileviewer theme LinearLayout langFrame = v.findViewById(R.id.langFrame); LinearLayout timeFrame = v.findViewById(R.id.timeFrame); @@ -68,6 +72,7 @@ public class SettingsFragment extends Fragment { LinearLayout customFontFrame = v.findViewById(R.id.customFontFrame); LinearLayout themeFrame = v.findViewById(R.id.themeSelectionFrame); LinearLayout certsFrame = v.findViewById(R.id.certsFrame); + LinearLayout sourceCodeThemeFrame = v.findViewById(R.id.sourceCodeThemeFrame); Switch counterBadgesSwitch = v.findViewById(R.id.switchCounterBadge); Switch pdfModeSwitch = v.findViewById(R.id.switchPdfMode); @@ -107,6 +112,10 @@ public class SettingsFragment extends Fragment { themeSelected.setText(tinyDb.getString("themeStr")); } + if(!tinyDb.getString("fileviewerSourceCodeThemeStr").isEmpty()) { + fileveiwerSourceCodeThemesSelected.setText(tinyDb.getString("fileviewerSourceCodeThemeStr")); + } + if(langSelectedChoice == 0) { langSelectedChoice = tinyDb.getInt("langId"); } @@ -131,6 +140,10 @@ public class SettingsFragment extends Fragment { themeSelectedChoice = tinyDb.getInt("themeId"); } + if(fileveiwerSourceCodeThemesSelectedChoice == 0) { + fileveiwerSourceCodeThemesSelectedChoice = tinyDb.getInt("fileviewerThemeId"); + } + if(tinyDb.getBoolean("enableCounterBadges")) { counterBadgesSwitch.setChecked(true); } @@ -145,6 +158,38 @@ public class SettingsFragment extends Fragment { pdfModeSwitch.setChecked(false); } + // fileviewer srouce code theme selection dialog + sourceCodeThemeFrame.setOnClickListener(view -> { + + AlertDialog.Builder fvtsBuilder = new AlertDialog.Builder(ctx); + + fvtsBuilder.setTitle(R.string.fileviewerSourceCodeThemeSelectorDialogTitle); + if(fileveiwerSourceCodeThemesSelectedChoice != -1) { + fvtsBuilder.setCancelable(true); + } + else { + fvtsBuilder.setCancelable(false); + } + + fvtsBuilder.setSingleChoiceItems(fileveiwerSourceCodeThemesList, fileveiwerSourceCodeThemesSelectedChoice, (dialogInterfaceTheme, i) -> { + + fileveiwerSourceCodeThemesSelectedChoice = i; + fileveiwerSourceCodeThemesSelected.setText(fileveiwerSourceCodeThemesList[i]); + tinyDb.putString("fileviewerSourceCodeThemeStr", fileveiwerSourceCodeThemesList[i]); + tinyDb.putInt("fileviewerSourceCodeThemeId", i); + + Objects.requireNonNull(getActivity()).recreate(); + getActivity().overridePendingTransition(0, 0); + dialogInterfaceTheme.dismiss(); + Toasty.info(getContext(), getResources().getString(R.string.settingsSave)); + + }); + + AlertDialog cfDialog = fvtsBuilder.create(); + cfDialog.show(); + + }); + // certs deletion certsFrame.setOnClickListener(v1 -> { diff --git a/app/src/main/res/layout/layout_settings_fileview.xml b/app/src/main/res/layout/layout_settings_fileview.xml index 54ae9a8..bee3c2c 100644 --- a/app/src/main/res/layout/layout_settings_fileview.xml +++ b/app/src/main/res/layout/layout_settings_fileview.xml @@ -49,4 +49,34 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 282447b..cc5f75f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -279,6 +279,9 @@ PDF Night Mode File Viewer Counter Badges + Source Code Theme + Sublime + Select Source Code Theme No more data available -- 2.40.1 From 4706cb44ec58ef1e7a37e7f54b2b8f8dea5142d2 Mon Sep 17 00:00:00 2001 From: M M Arif Date: Mon, 13 Apr 2020 12:27:36 +0500 Subject: [PATCH 2/4] Make string translatable false --- app/src/main/res/values/strings.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cc5f75f..ec02591 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -280,7 +280,7 @@ File Viewer Counter Badges Source Code Theme - Sublime + Sublime Select Source Code Theme -- 2.40.1 From efe755255d9f84a7ba6a50fd197afdae73ee1af2 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 13 Apr 2020 14:04:55 +0200 Subject: [PATCH 3/4] move from else-if to switch-case --- .../gitnex/activities/FileViewActivity.java | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) 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 549b255..14b139a 100644 --- a/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java @@ -175,23 +175,24 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie pdfViewFrame.setVisibility(View.GONE); singleCodeContents.setVisibility(View.VISIBLE); - if(tinyDb.getInt("fileviewerSourceCodeThemeId") == 1) { - singleCodeContents.setTheme(Theme.ARDUINO_LIGHT); - } - else if(tinyDb.getInt("fileviewerSourceCodeThemeId") == 2) { - singleCodeContents.setTheme(Theme.GITHUB); - } - else if(tinyDb.getInt("fileviewerSourceCodeThemeId") == 3) { - singleCodeContents.setTheme(Theme.FAR); - } - else if(tinyDb.getInt("fileviewerSourceCodeThemeId") == 4) { - singleCodeContents.setTheme(Theme.IR_BLACK); - } - else if(tinyDb.getInt("fileviewerSourceCodeThemeId") == 5) { - singleCodeContents.setTheme(Theme.ANDROID_STUDIO); - } - else { - singleCodeContents.setTheme(Theme.MONOKAI_SUBLIME); + switch(tinyDb.getInt("fileviewerSourceCodeThemeId")) { + case 1: + singleCodeContents.setTheme(Theme.ARDUINO_LIGHT); + break; + case 2: + singleCodeContents.setTheme(Theme.GITHUB); + break; + case 3: + singleCodeContents.setTheme(Theme.FAR); + break; + case 4: + singleCodeContents.setTheme(Theme.IR_BLACK); + break; + case 5: + singleCodeContents.setTheme(Theme.ANDROID_STUDIO); + break; + default: + singleCodeContents.setTheme(Theme.MONOKAI_SUBLIME); } singleCodeContents.setShowLineNumbers(true); -- 2.40.1 From fcf0ce91531d961c31e62cf9638547eafac7de99 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 13 Apr 2020 14:05:20 +0200 Subject: [PATCH 4/4] reformat code --- .../gitnex/activities/FileViewActivity.java | 666 +++++++++--------- 1 file changed, 325 insertions(+), 341 deletions(-) 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 14b139a..3d91456 100644 --- a/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java @@ -54,359 +54,343 @@ import retrofit2.Callback; public class FileViewActivity extends BaseActivity implements BottomSheetFileViewerFragment.BottomSheetListener { - private View.OnClickListener onClickListener; - private TextView singleFileContents; - private LinearLayout singleFileContentsFrame; - private HighlightJsView singleCodeContents; - private PhotoView imageView; - final Context ctx = this; - private ProgressBar mProgressBar; - private byte[] imageData; - private PDFView pdfView; - private LinearLayout pdfViewFrame; - private byte[] decodedPdf; - private Boolean pdfNightMode; - private static final int PERMISSION_REQUEST_CODE = 1; + private View.OnClickListener onClickListener; + private TextView singleFileContents; + private LinearLayout singleFileContentsFrame; + private HighlightJsView singleCodeContents; + private PhotoView imageView; + final Context ctx = this; + private ProgressBar mProgressBar; + private byte[] imageData; + private PDFView pdfView; + private LinearLayout pdfViewFrame; + private byte[] decodedPdf; + private Boolean pdfNightMode; + private static final int PERMISSION_REQUEST_CODE = 1; - @Override - protected int getLayoutResourceId(){ - return R.layout.activity_file_view; - } + @Override + protected int getLayoutResourceId() { - @Override - public void onCreate(Bundle savedInstanceState) { + return R.layout.activity_file_view; + } - super.onCreate(savedInstanceState); - Toolbar toolbar = findViewById(R.id.toolbar); - setSupportActionBar(toolbar); + @Override + public void onCreate(Bundle savedInstanceState) { - 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"); + super.onCreate(savedInstanceState); + Toolbar toolbar = findViewById(R.id.toolbar); + setSupportActionBar(toolbar); - ImageView closeActivity = findViewById(R.id.close); - singleFileContents = findViewById(R.id.singleFileContents); - singleCodeContents = findViewById(R.id.singleCodeContents); - imageView = findViewById(R.id.imageView); - mProgressBar = findViewById(R.id.progress_bar); - pdfView = findViewById(R.id.pdfView); - pdfViewFrame = findViewById(R.id.pdfViewFrame); - singleFileContentsFrame = findViewById(R.id.singleFileContentsFrame); - - String singleFileName = getIntent().getStringExtra("singleFileName"); - - TextView toolbar_title = findViewById(R.id.toolbar_title); - toolbar_title.setMovementMethod(new ScrollingMovementMethod()); - - initCloseListener(); - closeActivity.setOnClickListener(onClickListener); - - tinyDb.putString("downloadFileContents", ""); + 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"); - try { - - singleFileName = URLDecoder.decode(singleFileName, "UTF-8"); - singleFileName = singleFileName.replaceAll("//", "/"); - singleFileName = singleFileName.startsWith("/") ? singleFileName.substring(1) : singleFileName; - - } - catch (UnsupportedEncodingException e) { - - assert singleFileName != null; - Log.i("singleFileName", singleFileName); - - } - - toolbar_title.setText(singleFileName); - - getSingleFileContents(instanceUrl, instanceToken, repoOwner, repoName, singleFileName); - - } - - private void getSingleFileContents(String instanceUrl, String token, final String owner, String repo, final String filename) { - - final TinyDB tinyDb = new TinyDB(getApplicationContext()); - - Call call = RetrofitClient - .getInstance(instanceUrl, getApplicationContext()) - .getApiInterface() - .getSingleFileContents(token, owner, repo, filename); - - call.enqueue(new Callback() { - - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - - if (response.code() == 200) { - - AppUtil appUtil = new AppUtil(); - assert response.body() != null; - - if(!response.body().getContent().equals("")) { - - String fileExtension = FileUtils.getExtension(filename); - mProgressBar.setVisibility(View.GONE); - - // download file meta - tinyDb.putString("downloadFileName", filename); - tinyDb.putString("downloadFileContents", response.body().getContent()); - - if(appUtil.imageExtension(fileExtension)) { // file is image - - singleFileContentsFrame.setVisibility(View.GONE); - singleCodeContents.setVisibility(View.GONE); - pdfViewFrame.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 if (appUtil.sourceCodeExtension(fileExtension)) { // file is sourcecode + ImageView closeActivity = findViewById(R.id.close); + singleFileContents = findViewById(R.id.singleFileContents); + singleCodeContents = findViewById(R.id.singleCodeContents); + imageView = findViewById(R.id.imageView); + mProgressBar = findViewById(R.id.progress_bar); + pdfView = findViewById(R.id.pdfView); + pdfViewFrame = findViewById(R.id.pdfViewFrame); + singleFileContentsFrame = findViewById(R.id.singleFileContentsFrame); - imageView.setVisibility(View.GONE); - singleFileContentsFrame.setVisibility(View.GONE); - pdfViewFrame.setVisibility(View.GONE); - singleCodeContents.setVisibility(View.VISIBLE); + String singleFileName = getIntent().getStringExtra("singleFileName"); - switch(tinyDb.getInt("fileviewerSourceCodeThemeId")) { - case 1: - singleCodeContents.setTheme(Theme.ARDUINO_LIGHT); - break; - case 2: - singleCodeContents.setTheme(Theme.GITHUB); - break; - case 3: - singleCodeContents.setTheme(Theme.FAR); - break; - case 4: - singleCodeContents.setTheme(Theme.IR_BLACK); - break; - case 5: - singleCodeContents.setTheme(Theme.ANDROID_STUDIO); - break; - default: - singleCodeContents.setTheme(Theme.MONOKAI_SUBLIME); - } + TextView toolbar_title = findViewById(R.id.toolbar_title); + toolbar_title.setMovementMethod(new ScrollingMovementMethod()); - singleCodeContents.setShowLineNumbers(true); - singleCodeContents.setSource(appUtil.decodeBase64(response.body().getContent())); + initCloseListener(); + closeActivity.setOnClickListener(onClickListener); - } - else if (appUtil.pdfExtension(fileExtension)) { // file is pdf + tinyDb.putString("downloadFileContents", ""); - imageView.setVisibility(View.GONE); - singleFileContentsFrame.setVisibility(View.GONE); - singleCodeContents.setVisibility(View.GONE); - pdfViewFrame.setVisibility(View.VISIBLE); + try { - pdfNightMode = tinyDb.getBoolean("enablePdfMode"); + singleFileName = URLDecoder.decode(singleFileName, "UTF-8"); + singleFileName = singleFileName.replaceAll("//", "/"); + singleFileName = singleFileName.startsWith("/") ? singleFileName.substring(1) : singleFileName; - 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) - .nightMode(pdfNightMode) - .load(); - - } - else if (appUtil.excludeFilesInFileViewerExtension(fileExtension)) { // files need to be excluded - - imageView.setVisibility(View.GONE); - singleCodeContents.setVisibility(View.GONE); - pdfViewFrame.setVisibility(View.GONE); - singleFileContentsFrame.setVisibility(View.VISIBLE); - - singleFileContents.setText(getResources().getString(R.string.excludeFilesInFileviewer)); - singleFileContents.setGravity(Gravity.CENTER); - singleFileContents.setTypeface(null, Typeface.BOLD); - - } - else { // file type not known - plain text view - - imageView.setVisibility(View.GONE); - singleCodeContents.setVisibility(View.GONE); - pdfViewFrame.setVisibility(View.GONE); - singleFileContentsFrame.setVisibility(View.VISIBLE); - - singleFileContents.setText(appUtil.decodeBase64(response.body().getContent())); - - } - - } - 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 call, @NonNull Throwable t) { - Log.e("onFailure", t.toString()); - } - }); - - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - MenuInflater inflater = getMenuInflater(); - inflater.inflate(R.menu.generic_nav_dotted_menu, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - int id = item.getItemId(); - - switch (id) { - case android.R.id.home: - finish(); - return true; - case R.id.genericMenu: - BottomSheetFileViewerFragment bottomSheet = new BottomSheetFileViewerFragment(); - bottomSheet.show(getSupportFragmentManager(), "fileViewerBottomSheet"); - return true; - default: - return super.onOptionsItemSelected(item); - } - - } - - @Override - public void onButtonClicked(String text) { - - switch (text) { - case "downloadFile": - - if (Build.VERSION.SDK_INT >= 23) - { - if (checkPermission()) - { - requestFileDownload(); - } - else { - requestPermission(); - } - } - else - { - requestFileDownload(); - } - break; - - } - - } - - private void requestFileDownload() { - - final TinyDB tinyDb = new TinyDB(getApplicationContext()); - - if(!tinyDb.getString("downloadFileContents").isEmpty()) { - - 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); - FileOutputStream fileOutputStream = null; - - try { - - fileOutputStream = new FileOutputStream(downloadFilePath, false); - Objects.requireNonNull(fileOutputStream).write(pdfAsBytes); - fileOutputStream.flush(); - fileOutputStream.close(); - Toasty.info(getApplicationContext(), getString(R.string.downloadFileSaved)); - - } - catch (IOException e) { - Log.e("errorFileDownloading", Objects.requireNonNull(e.getMessage())); - } - - } - else { - Toasty.error(getApplicationContext(), getString(R.string.waitLoadingDownloadFile)); - } - - } - - 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 - public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { - 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() { - onClickListener = new View.OnClickListener() { - @Override - public void onClick(View view) { - getIntent().removeExtra("singleFileName"); - finish(); - } - }; - } + } + catch(UnsupportedEncodingException e) { + + assert singleFileName != null; + Log.i("singleFileName", singleFileName); + + } + + toolbar_title.setText(singleFileName); + + getSingleFileContents(instanceUrl, instanceToken, repoOwner, repoName, singleFileName); + + } + + private void getSingleFileContents(String instanceUrl, String token, final String owner, String repo, final String filename) { + + final TinyDB tinyDb = new TinyDB(getApplicationContext()); + + Call call = RetrofitClient.getInstance(instanceUrl, getApplicationContext()).getApiInterface().getSingleFileContents(token, owner, repo, filename); + + call.enqueue(new Callback() { + + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + + if(response.code() == 200) { + + AppUtil appUtil = new AppUtil(); + assert response.body() != null; + + if(!response.body().getContent().equals("")) { + + String fileExtension = FileUtils.getExtension(filename); + mProgressBar.setVisibility(View.GONE); + + // download file meta + tinyDb.putString("downloadFileName", filename); + tinyDb.putString("downloadFileContents", response.body().getContent()); + + if(appUtil.imageExtension(fileExtension)) { // file is image + + singleFileContentsFrame.setVisibility(View.GONE); + singleCodeContents.setVisibility(View.GONE); + pdfViewFrame.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 if(appUtil.sourceCodeExtension(fileExtension)) { // file is sourcecode + + imageView.setVisibility(View.GONE); + singleFileContentsFrame.setVisibility(View.GONE); + pdfViewFrame.setVisibility(View.GONE); + singleCodeContents.setVisibility(View.VISIBLE); + + switch(tinyDb.getInt("fileviewerSourceCodeThemeId")) { + case 1: + singleCodeContents.setTheme(Theme.ARDUINO_LIGHT); + break; + case 2: + singleCodeContents.setTheme(Theme.GITHUB); + break; + case 3: + singleCodeContents.setTheme(Theme.FAR); + break; + case 4: + singleCodeContents.setTheme(Theme.IR_BLACK); + break; + case 5: + singleCodeContents.setTheme(Theme.ANDROID_STUDIO); + break; + default: + singleCodeContents.setTheme(Theme.MONOKAI_SUBLIME); + } + + singleCodeContents.setShowLineNumbers(true); + singleCodeContents.setSource(appUtil.decodeBase64(response.body().getContent())); + + } + else if(appUtil.pdfExtension(fileExtension)) { // file is pdf + + imageView.setVisibility(View.GONE); + singleFileContentsFrame.setVisibility(View.GONE); + singleCodeContents.setVisibility(View.GONE); + pdfViewFrame.setVisibility(View.VISIBLE); + + pdfNightMode = tinyDb.getBoolean("enablePdfMode"); + + 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).nightMode(pdfNightMode).load(); + + } + else if(appUtil.excludeFilesInFileViewerExtension(fileExtension)) { // files need to be excluded + + imageView.setVisibility(View.GONE); + singleCodeContents.setVisibility(View.GONE); + pdfViewFrame.setVisibility(View.GONE); + singleFileContentsFrame.setVisibility(View.VISIBLE); + + singleFileContents.setText(getResources().getString(R.string.excludeFilesInFileviewer)); + singleFileContents.setGravity(Gravity.CENTER); + singleFileContents.setTypeface(null, Typeface.BOLD); + + } + else { // file type not known - plain text view + + imageView.setVisibility(View.GONE); + singleCodeContents.setVisibility(View.GONE); + pdfViewFrame.setVisibility(View.GONE); + singleFileContentsFrame.setVisibility(View.VISIBLE); + + singleFileContents.setText(appUtil.decodeBase64(response.body().getContent())); + + } + + } + 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 call, @NonNull Throwable t) { + + Log.e("onFailure", t.toString()); + } + }); + + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.generic_nav_dotted_menu, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + + int id = item.getItemId(); + + switch(id) { + case android.R.id.home: + finish(); + return true; + case R.id.genericMenu: + BottomSheetFileViewerFragment bottomSheet = new BottomSheetFileViewerFragment(); + bottomSheet.show(getSupportFragmentManager(), "fileViewerBottomSheet"); + return true; + default: + return super.onOptionsItemSelected(item); + } + + } + + @Override + public void onButtonClicked(String text) { + + switch(text) { + case "downloadFile": + + if(Build.VERSION.SDK_INT >= 23) { + if(checkPermission()) { + requestFileDownload(); + } + else { + requestPermission(); + } + } + else { + requestFileDownload(); + } + break; + + } + + } + + private void requestFileDownload() { + + final TinyDB tinyDb = new TinyDB(getApplicationContext()); + + if(!tinyDb.getString("downloadFileContents").isEmpty()) { + + 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); + FileOutputStream fileOutputStream = null; + + try { + + fileOutputStream = new FileOutputStream(downloadFilePath, false); + Objects.requireNonNull(fileOutputStream).write(pdfAsBytes); + fileOutputStream.flush(); + fileOutputStream.close(); + Toasty.info(getApplicationContext(), getString(R.string.downloadFileSaved)); + + } + catch(IOException e) { + Log.e("errorFileDownloading", Objects.requireNonNull(e.getMessage())); + } + + } + else { + Toasty.error(getApplicationContext(), getString(R.string.waitLoadingDownloadFile)); + } + + } + + 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 + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + + 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() { + + onClickListener = new View.OnClickListener() { + + @Override + public void onClick(View view) { + + getIntent().removeExtra("singleFileName"); + finish(); + } + }; + } } \ No newline at end of file -- 2.40.1