Skip to content

Commit

Permalink
Added support for Android 10+ Storage Access Framework.
Browse files Browse the repository at this point in the history
  • Loading branch information
litoj committed Sep 5, 2021
1 parent edf37f7 commit f9eaf4b
Show file tree
Hide file tree
Showing 27 changed files with 442 additions and 187 deletions.
7 changes: 5 additions & 2 deletions Android app/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.schlmgr"
minSdkVersion 19
targetSdkVersion 30
versionCode 50
versionName '1.5.0'
versionCode 52
versionName '1.5.2'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
signingConfig signingConfigs.debug
}
Expand All @@ -18,6 +18,9 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
debuggable false
}
debug {
debuggable true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
Expand Down
Binary file modified Android app/app/libs/SMLib.jar
Binary file not shown.
Binary file modified Android app/app/libs/SMLib.zip
Binary file not shown.
71 changes: 21 additions & 50 deletions Android app/app/src/main/java/com/schlmgr/gui/AndroidIOSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.widget.Toast;

import androidx.core.content.ContextCompat;
import androidx.documentfile.provider.DocumentFile;

import com.google.android.material.snackbar.Snackbar;
import com.schlmgr.BuildConfig;
Expand All @@ -19,14 +20,11 @@
import com.schlmgr.gui.popup.TextPopup;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.charset.StandardCharsets;
import java.io.OutputStream;

import IOSystem.FilePath;
import IOSystem.Formatter;
import IOSystem.SimpleReader;
import IOSystem.SimpleWriter;
Expand Down Expand Up @@ -109,7 +107,7 @@ public static void hideKeyboardFrom(View view) {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

public static String visibleFilePath(String path) {
public static String visibleInternalPath(String path) {
if (!path.contains(defDir)) return path;
return path.substring(defDir.length());
}
Expand All @@ -133,23 +131,15 @@ protected void setDefaults(boolean first) {
Object value;
if ((value = settings.get("version")) == null //old version update compatibility ensure
|| BuildConfig.VERSION_CODE > (Integer) value) {
int lastVersion;
if (value == null) {
settings.put("defaultTestTypePicture", settings.remove("testTypePicture"));
settings.put("flipWord", settings.remove("HIMflip"));
settings.put("flipAllOnClick", settings.remove("HIMflipAll"));
settings.put("parseNames", settings.remove("HIMparse"));
lastVersion = 22;
} else lastVersion = (Integer) value;
if (lastVersion < 30) {
int lastVersion = (Integer) value;
/*0if (lastVersion < 30) {
defaultReacts.put("removeSchNames", moreInfo -> {
for (MainChapter mch : MainChapter.ELEMENTS) mch.removeSetting("schNameCount");
});
}
if (lastVersion < 36) settings.put("doChoosePos", false);
if (lastVersion < 40) settings.put("doShowDesc", false);
if (lastVersion < 40) settings.put("doShowDesc", false);*/
settings.put("version", BuildConfig.VERSION_CODE);
deserialize(setts.getAbsolutePath(), settings, true);
}

HierarchyItemModel.defFlip = (Boolean) settings.get("flipWord");
Expand Down Expand Up @@ -205,7 +195,7 @@ protected void mkDefaultReacts() {
showMsg(msg, msg);
});
defaultReacts.put(SimpleWriter.class + ":success", (o) -> activity.runOnUiThread(() ->
makeText(CONTEXT, visibleFilePath(o[0].toString())
makeText(CONTEXT, visibleInternalPath(o[0].toString())
+ activity.getString(R.string.action_sw_success), Toast.LENGTH_SHORT).show()));
defaultReacts.put(SimpleReader.class + ":success", (o) -> {
int[] i = (int[]) o[0];
Expand Down Expand Up @@ -245,49 +235,30 @@ public static void showMsg(String msg, String fullMsg) {
}

@Override
public String getDefaultObjectsDir() {
return CONTEXT.getExternalFilesDir(null).getAbsolutePath() + "/School objects";
public GeneralPath getDefaultSubjectsDir() {
return new FilePath(new File(CONTEXT.getExternalFilesDir(
null).getAbsolutePath(), "School objects"));
}

@Override
public String mkRealPath(String path) {
return path;
public OutputStream outputInternal(GeneralPath file) throws IOException {
return CONTEXT.openFileOutput(file.getName(), Context.MODE_PRIVATE);
}

@Override
protected void deserialize(String filePath, Object toSave, boolean internal) {
try (ObjectOutputStream oos = new ObjectOutputStream(internal ?
CONTEXT.openFileOutput(filePath.substring(filePath.lastIndexOf('/') + 1),
Context.MODE_PRIVATE) : new java.io.FileOutputStream(filePath))) {
oos.writeObject(toSave);
} catch (IOException ex) {
throw new IllegalArgumentException(ex);
}
public InputStream inputInternal(GeneralPath file) throws IOException {
return CONTEXT.openFileInput(file.getName());
}

@Override
protected Object serialize(String filePath, boolean internal) {
try (ObjectInputStream ois = new ObjectInputStream(internal ?
CONTEXT.openFileInput(filePath.substring(filePath.lastIndexOf('/') + 1))
: new java.io.FileInputStream(filePath))) {
return ois.readObject();
} catch (IOException | ClassNotFoundException ex) {
throw new IllegalArgumentException(ex);
}
public GeneralPath createGeneralPath(Object path) {
return createGeneralPath(path, false);
}

@Override
protected String readStream(InputStream source) throws Exception {
StringBuilder sb = new StringBuilder();
try (InputStreamReader isr = new InputStreamReader(source, StandardCharsets.UTF_8)) {
char[] buffer = new char[1024];
int amount;
while ((amount = isr.read(buffer)) != -1) sb.append(buffer, 0, amount);
}
return sb.toString();
}

public String fileContent(Uri source) throws Exception {
return readStream(activity.getContentResolver().openInputStream(source));
public GeneralPath createGeneralPath(Object path, boolean internal) {
return path instanceof Uri ? new UriPath((Uri) path, internal) : path instanceof DocumentFile ?
new UriPath((DocumentFile) path, internal) : !(path instanceof File) && path.toString().contains(":")
? new UriPath(path.toString(), internal) : super.createGeneralPath(path, internal);
}
}
4 changes: 2 additions & 2 deletions Android app/app/src/main/java/com/schlmgr/gui/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ public static String translate(Class type) {
*
* @author Allan Jiang
*/
public static File getFileFromUri(Uri uri) {
/*public static File getFileFromUri(Uri uri) {
Looper.prepare();
Cursor c = new CursorLoader(activity.getApplicationContext(), uri,
new String[]{Media.DATA}, null, null, null).loadInBackground();
int index = c.getColumnIndexOrThrow(Media.DATA);
c.moveToFirst();
return new File(c.getString(index));
}
}*/
}
27 changes: 14 additions & 13 deletions Android app/app/src/main/java/com/schlmgr/gui/CurrentData.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.schlmgr.gui.list.SearchAdapter;
import com.schlmgr.gui.popup.TextPopup;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
Expand All @@ -19,12 +18,14 @@

import IOSystem.Formatter;
import IOSystem.Formatter.Data;
import IOSystem.Formatter.IOSystem.GeneralPath;
import objects.MainChapter;
import objects.Picture;
import objects.templates.BasicData;
import objects.templates.Container;
import objects.templates.ContainerFile;

import static IOSystem.Formatter.getIOSystem;
import static com.schlmgr.gui.AndroidIOSystem.getFirstCause;
import static com.schlmgr.gui.Controller.activity;

Expand Down Expand Up @@ -174,32 +175,32 @@ public void dismiss(boolean forever) {

public static void createMchs() {
synchronized (toLoad) {
if (Formatter.getPath().listFiles() != null) for (File f : Formatter.getPath().listFiles())
if (Formatter.getSubjectsDir().listFiles() != null) for (GeneralPath f : Formatter.getSubjectsDir().listFiles())
load:{
for (MainChapter mch : MainChapter.ELEMENTS)
if (mch.getDir().equals(f)) break load;
if (new File(f, "setts.dat").exists())
if (f.getChild("setts.dat").exists())
toLoad.add(new MainChapter(new Data(f.getName(), null)));
}
for (File f : ImportedMchs.get())
for (GeneralPath f : ImportedMchs.get())
load:{
for (MainChapter mch : MainChapter.ELEMENTS)
if (mch.getDir().equals(f)) break load;
if (mch.getDir().getOriginalName().equals(f.getOriginalName())) break load;
if (f.exists()) toLoad.add(new MainChapter(new Data(f.getName(), null), f));
}
}
}

public static class ImportedMchs {
static Set<File> importedMchs;
static Set<GeneralPath> importedMchs;

public static void importMch(File mchDir) {
public static void importMch(GeneralPath mchDir) {
checkLoaded();
importedMchs.add(mchDir);
save();
}

public static void removeMch(File mchDir) {
public static void removeMch(GeneralPath mchDir) {
importedMchs.remove(mchDir);
save();
}
Expand All @@ -211,7 +212,7 @@ private static void checkLoaded() {
String imds = (String) Formatter.getSetting("importedMchDirs");
if (imds != null) for (String s : imds.split(";"))
try {
importedMchs.add(new File(s));
importedMchs.add(getIOSystem().createGeneralPath(s));
} catch (Exception e) {
Snackbar.make(Controller.activity.getCurrentFocus(),
Controller.activity.getString(R.string.subject_not_found) + s,
Expand All @@ -225,18 +226,18 @@ private static void save() {
else {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (File f : get()) {
for (GeneralPath f : get()) {
if (first) first = false;
else sb.append(';');
sb.append(f.getPath());
sb.append(f.getOriginalName());
}
Formatter.putSetting("importedMchDirs", sb.toString());
}
}

public static File[] get() {
public static GeneralPath[] get() {
checkLoaded();
return importedMchs.toArray(new File[0]);
return importedMchs.toArray(new GeneralPath[0]);
}
}

Expand Down
Loading

0 comments on commit f9eaf4b

Please sign in to comment.