Skip to content

Commit

Permalink
resources: refactor viewer modules (fixes #2390) (#2394)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Sep 14, 2023
1 parent 4689961 commit e67301c
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 113 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "org.ole.planet.myplanet"
minSdkVersion 21
targetSdkVersion 34
versionCode 1035
versionName "0.10.35"
versionCode 1036
versionName "0.10.36"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
package org.ole.planet.myplanet.ui.viewer;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.example.jean.jcplayer.JcPlayerManagerListener;
import com.example.jean.jcplayer.general.JcStatus;
import com.example.jean.jcplayer.model.JcAudio;
import com.example.jean.jcplayer.view.JcPlayerView;

import org.jetbrains.annotations.NotNull;
import org.ole.planet.myplanet.R;
import org.ole.planet.myplanet.databinding.ActivityAudioPlayerBinding;
import org.ole.planet.myplanet.utilities.Utilities;

import java.io.File;
import java.util.ArrayList;

public class AudioPlayerActivity extends AppCompatActivity implements JcPlayerManagerListener {
JcPlayerView jcplayer;
private ActivityAudioPlayerBinding activityAudioPlayerBinding;
ArrayList<JcAudio> jcAudios;
boolean isFullPath;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_player);
jcplayer = findViewById(R.id.jcplayer);
activityAudioPlayerBinding = ActivityAudioPlayerBinding.inflate(getLayoutInflater());
setContentView(activityAudioPlayerBinding.getRoot());
String filePath = getIntent().getStringExtra("TOUCHED_FILE");
jcAudios = new ArrayList<>();
isFullPath = getIntent().getBooleanExtra("isFullPath", false);
Expand All @@ -41,10 +41,10 @@ protected void onCreate(Bundle savedInstanceState) {
}

jcAudios.add(JcAudio.createFromFilePath(fullPath));
jcplayer.initPlaylist(jcAudios, null);
jcplayer.getRootView().findViewById(R.id.btnNext).setVisibility(View.GONE);
jcplayer.getRootView().findViewById(R.id.btnPrev).setVisibility(View.GONE);
jcplayer.getRootView().findViewById(R.id.btnRepeatOne).setVisibility(View.GONE);
activityAudioPlayerBinding.jcplayer.initPlaylist(jcAudios, null);
activityAudioPlayerBinding.jcplayer.getRootView().findViewById(R.id.btnNext).setVisibility(View.GONE);
activityAudioPlayerBinding.jcplayer.getRootView().findViewById(R.id.btnPrev).setVisibility(View.GONE);
activityAudioPlayerBinding.jcplayer.getRootView().findViewById(R.id.btnRepeatOne).setVisibility(View.GONE);
}

@Override
Expand All @@ -58,22 +58,22 @@ public boolean onOptionsItemSelected(MenuItem item) {
@Override
protected void onPause() {
super.onPause();
if (jcplayer != null && jcplayer.isPlaying()) {
jcplayer.pause();
if (activityAudioPlayerBinding.jcplayer != null && activityAudioPlayerBinding.jcplayer.isPlaying()) {
activityAudioPlayerBinding.jcplayer.pause();
}
}

@Override
protected void onStop() {
super.onStop();
if (jcplayer != null) jcplayer.kill();
if (activityAudioPlayerBinding.jcplayer != null) activityAudioPlayerBinding.jcplayer.kill();
}

@Override
protected void onResume() {
super.onResume();
if (jcplayer != null && jcAudios.size() > 0) {
jcplayer.playAudio(jcAudios.get(0));
if (activityAudioPlayerBinding.jcplayer != null && jcAudios.size() > 0) {
activityAudioPlayerBinding.jcplayer.playAudio(jcAudios.get(0));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,38 @@
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;

import org.ole.planet.myplanet.R;
import org.ole.planet.myplanet.databinding.ActivityCsvviewerBinding;

import java.io.File;
import java.io.FileReader;
import java.util.Arrays;
import java.util.List;

public class CSVViewerActivity extends AppCompatActivity {

private TextView mCSVNameTitle;
private TextView mCSVContent;
private ActivityCsvviewerBinding activityCsvviewerBinding;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_csvviewer);
declareElements();
activityCsvviewerBinding = ActivityCsvviewerBinding.inflate(getLayoutInflater());
setContentView(activityCsvviewerBinding.getRoot());
renderCSVFile();
}

private void declareElements() {
mCSVNameTitle = (TextView) findViewById(R.id.csvFileName);
mCSVContent = (TextView) findViewById(R.id.csvFileContent);
}

private void renderCSVFile() {
Intent csvFileOpenIntent = getIntent();
String fileName = csvFileOpenIntent.getStringExtra("TOUCHED_FILE");

if (fileName != null && !fileName.isEmpty()) {
mCSVNameTitle.setText(fileName);
mCSVNameTitle.setVisibility(View.VISIBLE);
activityCsvviewerBinding.csvFileName.setText(fileName);
activityCsvviewerBinding.csvFileName.setVisibility(View.VISIBLE);
}


Expand All @@ -65,8 +57,8 @@ private void renderCSVFile() {
List<String[]> allRows = reader.readAll();

for (String[] row : allRows) {
mCSVContent.append(Arrays.toString(row));
mCSVContent.append("\n");
activityCsvviewerBinding.csvFileContent.append(Arrays.toString(row));
activityCsvviewerBinding.csvFileContent.append("\n");
}
} catch (Exception e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,34 @@
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;

import org.ole.planet.myplanet.R;
import org.ole.planet.myplanet.utilities.Utilities;
import org.ole.planet.myplanet.databinding.ActivityImageViewerBinding;

import java.io.File;

public class ImageViewerActivity extends AppCompatActivity {
private TextView mImageFileNameTitle;
private ImageView mImageViewer;
private ActivityImageViewerBinding activityImageViewerBinding;
private boolean isFullPath = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_viewer);
declareElements();
activityImageViewerBinding = ActivityImageViewerBinding.inflate(getLayoutInflater());
setContentView(activityImageViewerBinding.getRoot());
renderImageFile();
}

private void declareElements() {
mImageFileNameTitle = (TextView) findViewById(R.id.imageFileName);
mImageViewer = (ImageView) findViewById(R.id.imageViewer);
}

private void renderImageFile() {
isFullPath = getIntent().getBooleanExtra("isFullPath", false);
Intent imageOpenIntent = getIntent();
String fileName = imageOpenIntent.getStringExtra("TOUCHED_FILE");
if (fileName != null && !fileName.isEmpty()) {
mImageFileNameTitle.setText(fileName);
mImageFileNameTitle.setVisibility(View.VISIBLE);
activityImageViewerBinding.imageFileName.setText(fileName);
activityImageViewerBinding.imageFileName.setVisibility(View.VISIBLE);
}

try {
Expand All @@ -50,7 +41,7 @@ private void renderImageFile() {
File basePath = getExternalFilesDir(null);
imageFile = new File(basePath, "ole/" + fileName);
}
Glide.with(getApplicationContext()).load(imageFile).into(mImageViewer);
Glide.with(getApplicationContext()).load(imageFile).into(activityImageViewerBinding.imageViewer);
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,42 @@
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.ole.planet.myplanet.R;
import org.ole.planet.myplanet.utilities.Utilities;
import org.ole.planet.myplanet.databinding.ActivityMarkdownViewerBinding;

import java.io.File;

import br.tiagohm.markdownview.MarkdownView;

public class MarkdownViewerActivity extends AppCompatActivity {
private TextView mMarkdownNameTitle;
private MarkdownView mMarkdownContent;
private ActivityMarkdownViewerBinding activityMarkdownViewerBinding;
private String fileName;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_markdown_viewer);
declareElements();
activityMarkdownViewerBinding = ActivityMarkdownViewerBinding.inflate(getLayoutInflater());
setContentView(activityMarkdownViewerBinding.getRoot());
renderMarkdownFile();
}

private void declareElements() {
mMarkdownNameTitle = (TextView) findViewById(R.id.markdownFileName);
mMarkdownContent = (MarkdownView) findViewById(R.id.markdown_view);
}

private void renderMarkdownFile() {
Intent markdownOpenIntent = getIntent();
String fileName = markdownOpenIntent.getStringExtra("TOUCHED_FILE");

if (fileName != null && !fileName.isEmpty()) {
mMarkdownNameTitle.setText(fileName);
mMarkdownNameTitle.setVisibility(View.VISIBLE);
activityMarkdownViewerBinding.markdownFileName.setText(fileName);
activityMarkdownViewerBinding.markdownFileName.setVisibility(View.VISIBLE);
}

try {
File basePath = getExternalFilesDir(null);
File markdownFile = new File(basePath, "ole/" + fileName);

if (markdownFile.exists()) {
mMarkdownContent.loadMarkdownFromFile(markdownFile);
activityMarkdownViewerBinding.markdownView.loadMarkdownFromFile(markdownFile);
} else {
Toast.makeText(this, getString(R.string.unable_to_load) + fileName, Toast.LENGTH_LONG).show();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.github.barteksc.pdfviewer.PDFView;
import androidx.appcompat.app.AppCompatActivity;

import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import com.github.barteksc.pdfviewer.listener.OnPageErrorListener;
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;
import com.github.clans.fab.FloatingActionButton;

import org.ole.planet.myplanet.R;
import org.ole.planet.myplanet.databinding.ActivityPdfreaderBinding;
import org.ole.planet.myplanet.datamanager.DatabaseService;
import org.ole.planet.myplanet.model.RealmMyLibrary;
import org.ole.planet.myplanet.service.AudioRecorderService;
Expand All @@ -32,43 +29,35 @@
import io.realm.Realm;

public class PDFReaderActivity extends AppCompatActivity implements OnPageChangeListener, OnLoadCompleteListener, OnPageErrorListener, AudioRecorderService.AudioRecordListener {
private ActivityPdfreaderBinding activityPdfreaderBinding;
private static final String TAG = "PDF Reader Log";
private TextView mPdfFileNameTitle;
private String fileName;
private PDFView pdfView;
private FloatingActionButton fabRecord, fabPlay;
private AudioRecorderService audioRecorderService;
private RealmMyLibrary library;
private Realm mRealm;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdfreader);
activityPdfreaderBinding = ActivityPdfreaderBinding.inflate(getLayoutInflater());
setContentView(activityPdfreaderBinding.getRoot());
audioRecorderService = new AudioRecorderService().setAudioRecordListener(this);
mRealm = new DatabaseService(this).getRealmInstance();
if (getIntent().hasExtra("resourceId")) {
String resourceID = getIntent().getStringExtra("resourceId");
library = mRealm.where(RealmMyLibrary.class).equalTo("id", resourceID).findFirst();
}
declareElements();
renderPdfFile();
}

private void declareElements() {
mPdfFileNameTitle = findViewById(R.id.pdfFileName);
pdfView = findViewById(R.id.pdfView);
fabRecord = findViewById(R.id.fab_record);
fabPlay = findViewById(R.id.fab_play);
fabRecord.setOnClickListener(view -> {
activityPdfreaderBinding.fabRecord.setOnClickListener(view -> {
if (audioRecorderService.isRecording()) {
audioRecorderService.stopRecording();
} else {
audioRecorderService.startRecording();
}
});

fabPlay.setOnClickListener(view -> {
activityPdfreaderBinding.fabPlay.setOnClickListener(view -> {
if (library != null && !TextUtils.isEmpty(library.getTranslationAudioPath())) {
IntentUtils.openAudioFile(this, library.getTranslationAudioPath());
}
Expand All @@ -79,15 +68,15 @@ private void renderPdfFile() {
Intent pdfOpenIntent = getIntent();
fileName = pdfOpenIntent.getStringExtra("TOUCHED_FILE");
if (fileName != null && !fileName.isEmpty()) {
mPdfFileNameTitle.setText(fileName);
mPdfFileNameTitle.setVisibility(View.VISIBLE);
activityPdfreaderBinding.pdfFileName.setText(fileName);
activityPdfreaderBinding.pdfFileName.setVisibility(View.VISIBLE);
}

File file = new File(getExternalFilesDir(null), "ole/" + fileName);
if (file.exists()) {
try {
Utilities.log(file.getAbsolutePath());
pdfView.fromFile(file).defaultPage(0).enableAnnotationRendering(true).onLoad(this).onPageChange(this).scrollHandle(new DefaultScrollHandle(this)).load();
activityPdfreaderBinding.pdfView.fromFile(file).defaultPage(0).enableAnnotationRendering(true).onLoad(this).onPageChange(this).scrollHandle(new DefaultScrollHandle(this)).load();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), getString(R.string.unable_to_load) + fileName, Toast.LENGTH_LONG).show();
Expand All @@ -112,7 +101,7 @@ public void onPageError(int page, Throwable t) {
public void onRecordStarted() {
Utilities.toast(this, getString(R.string.recording_started));
NotificationUtil.create(this, R.drawable.ic_mic, "Recording Audio", getString(R.string.ole_is_recording_audio));
fabRecord.setImageResource(R.drawable.ic_stop);
activityPdfreaderBinding.fabRecord.setImageResource(R.drawable.ic_stop);
}

@Override
Expand All @@ -123,7 +112,7 @@ public void onRecordStopped(String outputFile) {
updateTranslation(outputFile);
AddResourceFragment.showAlert(this, outputFile);
}
fabRecord.setImageResource(R.drawable.ic_mic);
activityPdfreaderBinding.fabRecord.setImageResource(R.drawable.ic_mic);
}

private void updateTranslation(String outputFile) {
Expand All @@ -146,6 +135,6 @@ protected void onDestroy() {
public void onError(String error) {
NotificationUtil.cancellAll(this);
Utilities.toast(this, error);
fabRecord.setImageResource(R.drawable.ic_mic);
activityPdfreaderBinding.fabRecord.setImageResource(R.drawable.ic_mic);
}
}
Loading

0 comments on commit e67301c

Please sign in to comment.