Skip to content

Commit

Permalink
add storage and database controller
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamarine5 committed Apr 12, 2022
1 parent 69536b3 commit e62e9c8
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 31 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "com.aquaapps.readtorecite"
minSdkVersion 21
targetSdkVersion 31
versionCode 5
versionName "v1.0-beta-1"
versionCode 6
versionName "v1.0-beta-2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aquaapps.readtorecite">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public class CameraController {
public CameraController(AppCompatActivity activity) {
this.activity = activity;
try {

PermissionController.requestPermissions(activity, new String[]{
Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
});

cameraProvider = ProcessCameraProvider.getInstance(activity).get();
Preview preview = new Preview.Builder().build();
previewView = activity.findViewById(R.id.previewView);
Expand Down Expand Up @@ -85,12 +92,6 @@ public void startRecord(Consumer<VideoRecordEvent> videoRecordListener) {
.setContentValues(values)
.build();

PermissionController.requestPermissions(activity, new String[]{
Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
});

recording = videoCapture.getOutput()
.prepareRecording(activity, options)
.withAudioEnabled()
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/aquaapps/readtorecite/DataLabels.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.aquaapps.readtorecite;

public final class DataLabels {
public static final String PROPERTIES = "properties.json";
}
38 changes: 38 additions & 0 deletions app/src/main/java/com/aquaapps/readtorecite/DataStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.aquaapps.readtorecite;

import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class DataStorage {
public static String readFile(File file)
throws IOException {
FileInputStream fis = new FileInputStream(file.getCanonicalFile());
StringBuilder stringBuilder = new StringBuilder();
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis, StandardCharsets.UTF_8))) {
String line = bufferedReader.readLine();
while (line != null) {
stringBuilder.append(line).append('\n');
line = bufferedReader.readLine();
}
}
return stringBuilder.toString();
}

public static void writeFile(File file, String contents)
throws IOException {
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(contents.getBytes(StandardCharsets.UTF_8));
}
}

public static File getFile(AppCompatActivity activity, String dataLabels) {
return new File(activity.getExternalFilesDir(null), dataLabels);
}
}
19 changes: 19 additions & 0 deletions app/src/main/java/com/aquaapps/readtorecite/ExceptionCatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.aquaapps.readtorecite;

import android.app.AlertDialog;

import androidx.appcompat.app.AppCompatActivity;

public class ExceptionCatcher {
public static void CatchException(Exception exception, String function, AppCompatActivity activity) {
new AlertDialog.Builder(activity)
.setTitle(function)
.setMessage(exception.getMessage())
.setPositiveButton("确定", (dialogInterface, i) -> {
// TODO: write to log
})
.create()
.show();

}
}
54 changes: 43 additions & 11 deletions app/src/main/java/com/aquaapps/readtorecite/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.camera.video.OutputResults;
Expand All @@ -19,13 +20,11 @@
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.textfield.TextInputEditText;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity {
public CameraController cameraController;
public TextInputEditText textInputEditText;

public boolean isShowingPreview = true;
private boolean isShowingPreview = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -34,7 +33,7 @@ protected void onCreate(Bundle savedInstanceState) {
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
textInputEditText=findViewById(R.id.input_text);
Initialize();
fab.setOnClickListener(view -> {
if (cameraController.isRecording()) {
cameraController.stopRecord();
Expand All @@ -55,10 +54,14 @@ protected void onCreate(Bundle savedInstanceState) {
fab.setImageResource(android.R.drawable.ic_menu_save);
}
});
// Instance Camera
cameraController = new CameraController(this);
}

@Override
public void onStop() {
super.onStop();

Properties.save();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
Expand All @@ -69,7 +72,7 @@ public boolean onCreateOptionsMenu(Menu menu) {

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
PermissionController.onRequestPermissionsResult(requestCode,permissions,grantResults);
PermissionController.onRequestPermissionsResult(requestCode, permissions, grantResults);
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

Expand Down Expand Up @@ -98,18 +101,47 @@ public boolean onOptionsItemSelected(MenuItem item) {

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode){
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
textInputEditText.scrollBy(0,-800);
int y = textInputEditText.getScrollY();
int scroll_y = Properties.readInt(PropertiesKeys.SCROLL_DISTANCE);
if (y < scroll_y) textInputEditText.scrollBy(0, -y);
else textInputEditText.scrollBy(0, -scroll_y);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
textInputEditText.scrollBy(0,800);
textInputEditText.scrollBy(0, Properties.readInt(PropertiesKeys.SCROLL_DISTANCE));
return true;
default:
return super.onKeyDown(keyCode, event);
}
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) {
outState.putCharSequence(PropertiesKeys.LAST_RECITE_CONTENT, textInputEditText.getText());
outState.putInt(PropertiesKeys.SCROLL_POSITION, textInputEditText.getScrollY());

super.onSaveInstanceState(outState, outPersistentState);
}

@Override
public void onRestoreInstanceState(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onRestoreInstanceState(savedInstanceState, persistentState);

if (savedInstanceState != null) {
textInputEditText.setText(savedInstanceState.getCharSequence(PropertiesKeys.LAST_RECITE_CONTENT));
textInputEditText.scrollTo(0, savedInstanceState.getInt(PropertiesKeys.SCROLL_POSITION));
}
}

public void Initialize() {
Properties.initialize(this);
textInputEditText = findViewById(R.id.input_text);
textInputEditText.setText(Properties.readString(PropertiesKeys.LAST_RECITE_CONTENT));
// Instance Camera
cameraController = new CameraController(this);
}

public void shareVideo(Uri videoUri) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@
import androidx.core.content.PermissionChecker;

public class PermissionController {
public static void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
public static void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
// TODO:
}
public static void requestPermissions(AppCompatActivity activity,@NonNull String[] permissions){
for (String permission:
permissions) {
requestPermission(activity,permission);

public static void requestPermissions(AppCompatActivity activity, @NonNull String[] permissions) {
for (String permission :
permissions) {
requestPermission(activity, permission);
}
}
public static void requestPermission(AppCompatActivity activity,String permission){
if(ActivityCompat.checkSelfPermission(activity,permission) == PackageManager.PERMISSION_DENIED &&
ActivityCompat.shouldShowRequestPermissionRationale(activity,permission)){
ActivityCompat.requestPermissions(activity,new String[]{permission},114514);

public static void requestPermission(AppCompatActivity activity, String permission) {
if (ActivityCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_DENIED &&
ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
ActivityCompat.requestPermissions(activity, new String[]{permission}, 114514);
}
}
}
80 changes: 80 additions & 0 deletions app/src/main/java/com/aquaapps/readtorecite/Properties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.aquaapps.readtorecite;

import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;

public class Properties {
private static Properties properties = null;

private JSONObject propertiesContent;

public File propertiesFile;

// TODO: remove static
public static AppCompatActivity activity;


public static void initialize(AppCompatActivity activity) {
properties = new Properties();
properties.activity = activity;
properties.propertiesFile = DataStorage.getFile(activity, DataLabels.PROPERTIES);
try {
if (properties.propertiesFile.exists()) {
properties.propertiesContent = new JSONObject(DataStorage.readFile(properties.propertiesFile));
} else {
properties.propertiesContent = Properties.setup();
}
} catch (IOException | JSONException e) {
ExceptionCatcher.CatchException(e, "Properties.Initialize", activity);
}
}

public static String readString(String propertiesKey) {
try {
return properties.propertiesContent.getString(propertiesKey);
} catch (JSONException e) {
ExceptionCatcher.CatchException(e, "Properties.read", activity);
return null;
}
}

public static int readInt(String propertiesKey) {
try {
return properties.propertiesContent.getInt(propertiesKey);
} catch (JSONException e) {
ExceptionCatcher.CatchException(e, "Properties.read", activity);
return 0;
}
}

public static void write(String propertiesKey, Object values) {
try {
properties.propertiesContent.put(propertiesKey, values);
} catch (JSONException e) {
ExceptionCatcher.CatchException(e, "Properties.write", activity);
}
}

public static JSONObject setup()
throws JSONException, IOException {
JSONObject object = new JSONObject();
object.put(PropertiesKeys.LAST_RECITE_CONTENT, "");

object.put(PropertiesKeys.SCROLL_DISTANCE, 800);
DataStorage.writeFile(properties.propertiesFile, object.toString());
return object;
}

public static void save() {
try {
DataStorage.writeFile(properties.propertiesFile, properties.propertiesContent.toString());
} catch (IOException e) {
ExceptionCatcher.CatchException(e, "Properties.save", activity);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.aquaapps.readtorecite;

public final class PropertiesKeys {
public static final String LAST_RECITE_CONTENT = "LastReciteContent";
public static final String SCROLL_DISTANCE = "ScrollDistance";

public static final String SCROLL_POSITION = "_ScrollPosition";
}

0 comments on commit e62e9c8

Please sign in to comment.