Themes for source code in file viewer (#403)
reformat code move from else-if to switch-case Merge branch 'master' into 396-themes-file-veiwer Merge branch 'master' into 396-themes-file-veiwer Make string translatable false Themes for source code in file viewer Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: 6543 <6543@noreply.gitea.io> Reviewed-on: https://gitea.com/gitnex/GitNex/pulls/403 Reviewed-by: 6543 <6543@noreply.gitea.io>
This commit is contained in:
		@@ -54,340 +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);
 | 
			
		||||
		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");
 | 
			
		||||
 | 
			
		||||
        String singleFileName = getIntent().getStringExtra("singleFileName");
 | 
			
		||||
		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);
 | 
			
		||||
 | 
			
		||||
        TextView toolbar_title = findViewById(R.id.toolbar_title);
 | 
			
		||||
        toolbar_title.setMovementMethod(new ScrollingMovementMethod());
 | 
			
		||||
		String singleFileName = getIntent().getStringExtra("singleFileName");
 | 
			
		||||
 | 
			
		||||
        initCloseListener();
 | 
			
		||||
        closeActivity.setOnClickListener(onClickListener);
 | 
			
		||||
 | 
			
		||||
        tinyDb.putString("downloadFileContents", "");
 | 
			
		||||
		TextView toolbar_title = findViewById(R.id.toolbar_title);
 | 
			
		||||
		toolbar_title.setMovementMethod(new ScrollingMovementMethod());
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
 | 
			
		||||
            singleFileName = URLDecoder.decode(singleFileName, "UTF-8");
 | 
			
		||||
            singleFileName = singleFileName.replaceAll("//", "/");
 | 
			
		||||
            singleFileName = singleFileName.startsWith("/") ? singleFileName.substring(1) : singleFileName;
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
        catch (UnsupportedEncodingException e) {
 | 
			
		||||
		initCloseListener();
 | 
			
		||||
		closeActivity.setOnClickListener(onClickListener);
 | 
			
		||||
 | 
			
		||||
            assert singleFileName != null;
 | 
			
		||||
            Log.i("singleFileName", singleFileName);
 | 
			
		||||
		tinyDb.putString("downloadFileContents", "");
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        toolbar_title.setText(singleFileName);
 | 
			
		||||
		try {
 | 
			
		||||
 | 
			
		||||
        getSingleFileContents(instanceUrl, instanceToken, repoOwner, repoName, singleFileName);
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
			singleFileName = URLDecoder.decode(singleFileName, "UTF-8");
 | 
			
		||||
			singleFileName = singleFileName.replaceAll("//", "/");
 | 
			
		||||
			singleFileName = singleFileName.startsWith("/") ? singleFileName.substring(1) : singleFileName;
 | 
			
		||||
 | 
			
		||||
    private void getSingleFileContents(String instanceUrl, String token, final String owner, String repo, final String filename) {
 | 
			
		||||
		}
 | 
			
		||||
		catch(UnsupportedEncodingException e) {
 | 
			
		||||
 | 
			
		||||
        final TinyDB tinyDb = new TinyDB(getApplicationContext());
 | 
			
		||||
			assert singleFileName != null;
 | 
			
		||||
			Log.i("singleFileName", singleFileName);
 | 
			
		||||
 | 
			
		||||
        Call<Files> call = RetrofitClient
 | 
			
		||||
                .getInstance(instanceUrl, getApplicationContext())
 | 
			
		||||
                .getApiInterface()
 | 
			
		||||
                .getSingleFileContents(token, owner, repo, filename);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
        call.enqueue(new Callback<Files>() {
 | 
			
		||||
		toolbar_title.setText(singleFileName);
 | 
			
		||||
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onResponse(@NonNull Call<Files> call, @NonNull retrofit2.Response<Files> response) {
 | 
			
		||||
		getSingleFileContents(instanceUrl, instanceToken, repoOwner, repoName, singleFileName);
 | 
			
		||||
 | 
			
		||||
                if (response.code() == 200) {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
                    AppUtil appUtil = new AppUtil();
 | 
			
		||||
                    assert response.body() != null;
 | 
			
		||||
	private void getSingleFileContents(String instanceUrl, String token, final String owner, String repo, final String filename) {
 | 
			
		||||
 | 
			
		||||
                    if(!response.body().getContent().equals("")) {
 | 
			
		||||
		final TinyDB tinyDb = new TinyDB(getApplicationContext());
 | 
			
		||||
 | 
			
		||||
                        String fileExtension = FileUtils.getExtension(filename);
 | 
			
		||||
                        mProgressBar.setVisibility(View.GONE);
 | 
			
		||||
		Call<Files> call = RetrofitClient.getInstance(instanceUrl, getApplicationContext()).getApiInterface().getSingleFileContents(token, owner, repo, filename);
 | 
			
		||||
 | 
			
		||||
                        // download file meta
 | 
			
		||||
                        tinyDb.putString("downloadFileName", filename);
 | 
			
		||||
                        tinyDb.putString("downloadFileContents", response.body().getContent());
 | 
			
		||||
		call.enqueue(new Callback<Files>() {
 | 
			
		||||
 | 
			
		||||
                        if(appUtil.imageExtension(fileExtension)) { // file is image
 | 
			
		||||
			@Override
 | 
			
		||||
			public void onResponse(@NonNull Call<Files> call, @NonNull retrofit2.Response<Files> response) {
 | 
			
		||||
 | 
			
		||||
                            singleFileContentsFrame.setVisibility(View.GONE);
 | 
			
		||||
                            singleCodeContents.setVisibility(View.GONE);
 | 
			
		||||
                            pdfViewFrame.setVisibility(View.GONE);
 | 
			
		||||
                            imageView.setVisibility(View.VISIBLE);
 | 
			
		||||
				if(response.code() == 200) {
 | 
			
		||||
 | 
			
		||||
                            imageData = Base64.decode(response.body().getContent(), Base64.DEFAULT);
 | 
			
		||||
                            Drawable imageDrawable = new BitmapDrawable(getResources(), BitmapFactory.decodeByteArray(imageData, 0, imageData.length));
 | 
			
		||||
                            imageView.setImageDrawable(imageDrawable);
 | 
			
		||||
					AppUtil appUtil = new AppUtil();
 | 
			
		||||
					assert response.body() != null;
 | 
			
		||||
 | 
			
		||||
                        }
 | 
			
		||||
                        else if (appUtil.sourceCodeExtension(fileExtension)) { // file is sourcecode
 | 
			
		||||
					if(!response.body().getContent().equals("")) {
 | 
			
		||||
 | 
			
		||||
                            imageView.setVisibility(View.GONE);
 | 
			
		||||
                            singleFileContentsFrame.setVisibility(View.GONE);
 | 
			
		||||
                            pdfViewFrame.setVisibility(View.GONE);
 | 
			
		||||
                            singleCodeContents.setVisibility(View.VISIBLE);
 | 
			
		||||
						String fileExtension = FileUtils.getExtension(filename);
 | 
			
		||||
						mProgressBar.setVisibility(View.GONE);
 | 
			
		||||
 | 
			
		||||
                            singleCodeContents.setTheme(Theme.GRUVBOX_DARK);
 | 
			
		||||
                            singleCodeContents.setShowLineNumbers(true);
 | 
			
		||||
                            singleCodeContents.setSource(appUtil.decodeBase64(response.body().getContent()));
 | 
			
		||||
						// download file meta
 | 
			
		||||
						tinyDb.putString("downloadFileName", filename);
 | 
			
		||||
						tinyDb.putString("downloadFileContents", response.body().getContent());
 | 
			
		||||
 | 
			
		||||
                        }
 | 
			
		||||
                        else if (appUtil.pdfExtension(fileExtension)) { // file is pdf
 | 
			
		||||
						if(appUtil.imageExtension(fileExtension)) { // file is image
 | 
			
		||||
 | 
			
		||||
                            imageView.setVisibility(View.GONE);
 | 
			
		||||
                            singleFileContentsFrame.setVisibility(View.GONE);
 | 
			
		||||
                            singleCodeContents.setVisibility(View.GONE);
 | 
			
		||||
                            pdfViewFrame.setVisibility(View.VISIBLE);
 | 
			
		||||
							singleFileContentsFrame.setVisibility(View.GONE);
 | 
			
		||||
							singleCodeContents.setVisibility(View.GONE);
 | 
			
		||||
							pdfViewFrame.setVisibility(View.GONE);
 | 
			
		||||
							imageView.setVisibility(View.VISIBLE);
 | 
			
		||||
 | 
			
		||||
                            pdfNightMode = tinyDb.getBoolean("enablePdfMode");
 | 
			
		||||
							imageData = Base64.decode(response.body().getContent(), Base64.DEFAULT);
 | 
			
		||||
							Drawable imageDrawable = new BitmapDrawable(getResources(), BitmapFactory.decodeByteArray(imageData, 0, imageData.length));
 | 
			
		||||
							imageView.setImageDrawable(imageDrawable);
 | 
			
		||||
 | 
			
		||||
                            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.sourceCodeExtension(fileExtension)) { // file is sourcecode
 | 
			
		||||
 | 
			
		||||
                        }
 | 
			
		||||
                        else if (appUtil.excludeFilesInFileViewerExtension(fileExtension)) { // files need to be excluded
 | 
			
		||||
							imageView.setVisibility(View.GONE);
 | 
			
		||||
							singleFileContentsFrame.setVisibility(View.GONE);
 | 
			
		||||
							pdfViewFrame.setVisibility(View.GONE);
 | 
			
		||||
							singleCodeContents.setVisibility(View.VISIBLE);
 | 
			
		||||
 | 
			
		||||
                            imageView.setVisibility(View.GONE);
 | 
			
		||||
                            singleCodeContents.setVisibility(View.GONE);
 | 
			
		||||
                            pdfViewFrame.setVisibility(View.GONE);
 | 
			
		||||
                            singleFileContentsFrame.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);
 | 
			
		||||
							}
 | 
			
		||||
 | 
			
		||||
                            singleFileContents.setText(getResources().getString(R.string.excludeFilesInFileviewer));
 | 
			
		||||
                            singleFileContents.setGravity(Gravity.CENTER);
 | 
			
		||||
                            singleFileContents.setTypeface(null, Typeface.BOLD);
 | 
			
		||||
							singleCodeContents.setShowLineNumbers(true);
 | 
			
		||||
							singleCodeContents.setSource(appUtil.decodeBase64(response.body().getContent()));
 | 
			
		||||
 | 
			
		||||
                        }
 | 
			
		||||
                        else { // file type not known - plain text view
 | 
			
		||||
						}
 | 
			
		||||
						else if(appUtil.pdfExtension(fileExtension)) { // file is pdf
 | 
			
		||||
 | 
			
		||||
                            imageView.setVisibility(View.GONE);
 | 
			
		||||
                            singleCodeContents.setVisibility(View.GONE);
 | 
			
		||||
                            pdfViewFrame.setVisibility(View.GONE);
 | 
			
		||||
                            singleFileContentsFrame.setVisibility(View.VISIBLE);
 | 
			
		||||
							imageView.setVisibility(View.GONE);
 | 
			
		||||
							singleFileContentsFrame.setVisibility(View.GONE);
 | 
			
		||||
							singleCodeContents.setVisibility(View.GONE);
 | 
			
		||||
							pdfViewFrame.setVisibility(View.VISIBLE);
 | 
			
		||||
 | 
			
		||||
                            singleFileContents.setText(appUtil.decodeBase64(response.body().getContent()));
 | 
			
		||||
							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 {
 | 
			
		||||
                        singleFileContents.setText("");
 | 
			
		||||
                        mProgressBar.setVisibility(View.GONE);
 | 
			
		||||
                    }
 | 
			
		||||
						}
 | 
			
		||||
						else if(appUtil.excludeFilesInFileViewerExtension(fileExtension)) { // files need to be excluded
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
                else if(response.code() == 401) {
 | 
			
		||||
							imageView.setVisibility(View.GONE);
 | 
			
		||||
							singleCodeContents.setVisibility(View.GONE);
 | 
			
		||||
							pdfViewFrame.setVisibility(View.GONE);
 | 
			
		||||
							singleFileContentsFrame.setVisibility(View.VISIBLE);
 | 
			
		||||
 | 
			
		||||
                    AlertDialogs.authorizationTokenRevokedDialog(ctx, getResources().getString(R.string.alertDialogTokenRevokedTitle),
 | 
			
		||||
                            getResources().getString(R.string.alertDialogTokenRevokedMessage),
 | 
			
		||||
                            getResources().getString(R.string.alertDialogTokenRevokedCopyNegativeButton),
 | 
			
		||||
                            getResources().getString(R.string.alertDialogTokenRevokedCopyPositiveButton));
 | 
			
		||||
							singleFileContents.setText(getResources().getString(R.string.excludeFilesInFileviewer));
 | 
			
		||||
							singleFileContents.setGravity(Gravity.CENTER);
 | 
			
		||||
							singleFileContents.setTypeface(null, Typeface.BOLD);
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
                else if(response.code() == 403) {
 | 
			
		||||
						}
 | 
			
		||||
						else { // file type not known - plain text view
 | 
			
		||||
 | 
			
		||||
                    Toasty.info(ctx, ctx.getString(R.string.authorizeError));
 | 
			
		||||
							imageView.setVisibility(View.GONE);
 | 
			
		||||
							singleCodeContents.setVisibility(View.GONE);
 | 
			
		||||
							pdfViewFrame.setVisibility(View.GONE);
 | 
			
		||||
							singleFileContentsFrame.setVisibility(View.VISIBLE);
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
                else if(response.code() == 404) {
 | 
			
		||||
 | 
			
		||||
                    Toasty.info(ctx, ctx.getString(R.string.apiNotFound));
 | 
			
		||||
							singleFileContents.setText(appUtil.decodeBase64(response.body().getContent()));
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
                else {
 | 
			
		||||
 | 
			
		||||
                    Toasty.info(getApplicationContext(), getString(R.string.labelGeneralError));
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
						}
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onFailure(@NonNull Call<Files> 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();
 | 
			
		||||
            }
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
					}
 | 
			
		||||
					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());
 | 
			
		||||
			}
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@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();
 | 
			
		||||
			}
 | 
			
		||||
		};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user