-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69536b3
commit e62e9c8
Showing
10 changed files
with
217 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
app/src/main/java/com/aquaapps/readtorecite/DataStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
app/src/main/java/com/aquaapps/readtorecite/ExceptionCatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
app/src/main/java/com/aquaapps/readtorecite/Properties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/aquaapps/readtorecite/PropertiesKeys.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |