Skip to content

Commit

Permalink
feat: android select dir support sd dir
Browse files Browse the repository at this point in the history
  • Loading branch information
zeromake committed May 23, 2024
1 parent d690eaf commit da65f47
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,24 @@ public class Globals {
public static String[] CurrentDirectoryValidPathArray = null;
public static final String[] CURRENT_DIRECTORY_PATH_TEMPLATE_ARRAY = new String[]{"${SDCARD}/mine", "${SDCARD}/ons"};
public static boolean CurrentGameRunning = false;

public static String[] FallbackDirectoryPathArray = new String[]{
"/mnt/ext_card",
"/mnt/flash",
"/mnt/sdcard",
"/mnt/sdcard/external_sd",
"/mnt/sdcard-ext",
"/mnt/storage/sdcard",
"/mnt/udisk",
"/mnt/usbdisk",
"/sdcard",
"/sdcard/sd",
"/storage/sdcard",
"/storage/sdcard0",
"/storage/sdcard1",
"/storage/emulated/0",
"/storage/emulated/legacy",
"/storage/usb0",
};
public static String[] FallbackDirectoryValidPathArray = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
import java.util.stream.Stream;

public class MainActivity extends Activity implements
OnPermissionCallback,
DialogInterface.OnClickListener,
AdapterView.OnItemClickListener,
View.OnClickListener{
OnPermissionCallback,
DialogInterface.OnClickListener,
AdapterView.OnItemClickListener,
View.OnClickListener {
private ImageView background;
private ImageView btnSettings;
private ImageView cover;
Expand Down Expand Up @@ -77,12 +77,12 @@ protected void onCreate(Bundle savedInstanceState) {
ONSVariable.dh = displayMetrics.heightPixels;
int phoneType = ((TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE)).getPhoneType();
double doubleValue = new BigDecimal(
ONSVariable.dw / ONSVariable.dh
ONSVariable.dw / ONSVariable.dh
).setScale(
2,
RoundingMode.HALF_UP
2,
RoundingMode.HALF_UP
).doubleValue();
if ((float)ONSVariable.dw / (float)ONSVariable.dh == 1.5d || doubleValue == 1.33d) {
if ((float) ONSVariable.dw / (float) ONSVariable.dh == 1.5d || doubleValue == 1.33d) {
setContentView(R.layout.activity_ft);
} else if (phoneType == 0) {
setContentView(R.layout.activity_pad);
Expand All @@ -102,7 +102,7 @@ protected void onCreate(Bundle savedInstanceState) {
this.items.setOnPlayClickListener(this);
}

private<T extends View> T bindView(int i) {
private <T extends View> T bindView(int i) {
return findViewById(i);
}

Expand All @@ -118,18 +118,19 @@ private void externalStoragePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (!Environment.isExternalStorageManager()) {
XXPermissions.with(this).permission(
Permission.MANAGE_EXTERNAL_STORAGE
Permission.MANAGE_EXTERNAL_STORAGE
).request(this);
}
} else {
XXPermissions.with(this).permission(
Permission.READ_MEDIA_IMAGES,
Permission.READ_MEDIA_VIDEO,
Permission.READ_MEDIA_AUDIO
Permission.READ_MEDIA_IMAGES,
Permission.READ_MEDIA_VIDEO,
Permission.READ_MEDIA_AUDIO
// Permission.WRITE_EXTERNAL_STORAGE
).request(this);
}
}

@Override
public void onGranted(List<String> permissions, boolean all) {
// Toast.makeText(MainActivity.this, "权限申请成功", Toast.LENGTH_SHORT).show();
Expand All @@ -152,16 +153,16 @@ public void setting() {
int width = size.x;
int height = size.y;
settingPopupWindow = new PopupWindow(
linearLayout,
width/4,
height/3,
true
linearLayout,
width / 4,
height / 3,
true
);
settingPopupWindow.setAnimationStyle(R.style.Animation_ConfigPanelAnimation);
settingPopupWindow.setTouchable(true);
settingPopupWindow.setOutsideTouchable(true);
settingPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.popup));
settingPopupWindow.showAtLocation(this.background, Gravity.END|Gravity.BOTTOM, width / 14, height / 24);
settingPopupWindow.showAtLocation(this.background, Gravity.END | Gravity.BOTTOM, width / 14, height / 24);
View popupButton = linearLayout.findViewById(R.id.popup_path);
popupButton.setOnClickListener(view -> this.chooseDir());
}
Expand All @@ -173,7 +174,7 @@ private void chooseDir() {
strArr[strArr.length - 1] = getString(R.string.Other) + "...";
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setInverseBackgroundForced(true);
builder.setTitle(getString(R.string.Launch_ChooseDirectory));
builder.setTitle(getString(R.string.Launch_AutoChooseDirectory));
builder.setItems(strArr, this);
builder.setNegativeButton(getString(R.string.Cancel), null);
builder.setCancelable(true);
Expand Down Expand Up @@ -218,6 +219,20 @@ public void run() {
}
}

private void onOther() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.Launch_OtherChooseDirectory));
this.mDirBrowserDirFileArray = new File[Globals.FallbackDirectoryValidPathArray.length];
for (int index = 0; index < Globals.FallbackDirectoryValidPathArray.length; index++) {
this.mDirBrowserDirFileArray[index] = new File(Globals.FallbackDirectoryValidPathArray[index]);
}
builder.setItems(Globals.FallbackDirectoryValidPathArray, this);
builder.setNegativeButton(getString(R.string.Cancel), null);
builder.setCancelable(true);
this.mDirBrowserDialog = builder.create();
this.mDirBrowserDialog.show();
}

@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (dialogInterface == this.mDirBrowserDialog) {
Expand All @@ -229,15 +244,9 @@ public void onClick(DialogInterface dialogInterface, int i) {
Settings.SaveGlobals(this);
loadCurrentDirectory();
return;
} else if (Globals.CurrentDirectoryPathForLauncher == null || Globals.CurrentDirectoryPathForLauncher.equals("")) {
File f = new File("/storage");
if (f.exists() && f.isDirectory() && f.canRead()) {
this.mDirBrowserCurDirPath = "/storage";
} else {
this.mDirBrowserCurDirPath = "/mnt";
}
} else {
this.mDirBrowserCurDirPath = Globals.CurrentDirectoryPathForLauncher;
this.onOther();
return;
}
File file = new File(this.mDirBrowserCurDirPath);
File parentFile = file.getParentFile();
Expand All @@ -249,7 +258,7 @@ public void onClick(DialogInterface dialogInterface, int i) {
try (Stream<Path> stream = Files.list(Paths.get(this.mDirBrowserCurDirPath))) {
stream.forEach(path -> {
if (Files.isDirectory(path)
&& Files.isReadable(path)) {
&& Files.isReadable(path)) {
dirs.add(new File(path.toAbsolutePath().toString()));
}
});
Expand Down Expand Up @@ -316,11 +325,11 @@ private void loadGameItem(Game game) {
}

private void updateCover(String cover, boolean b) {
ImageLoader.getInstance().displayImage("file:/"+cover, this.cover, Settings.getDisplayImageOptions());
ImageLoader.getInstance().displayImage("file:/" + cover, this.cover, Settings.getDisplayImageOptions());
}

private void updateBackground(String background) {
ImageLoader.getInstance().displayImage("file:/"+background, this.background, Settings.getDisplayImageOptions());
ImageLoader.getInstance().displayImage("file:/" + background, this.background, Settings.getDisplayImageOptions());
}

@Override
Expand All @@ -332,14 +341,14 @@ public void onClick(View view) {
// types(0:全局文字,1:普通文字, 2:精灵文字, 3: 菜单文字, 4: 弹窗文字, 5: ruby 文字),字体大小,字体大小倍率,字体颜色,是否描边,描边宽度,描边颜色,文字换行距离边距,文字换列距离边距
// 大部分游戏只有 1, 2 生效,后面的文字都是用精灵文字去做的,还有不少弹框是用图片做的。
extras.putStringArray(ONScripter.ARGS_KEY, new String[]{
"-r",
selectedItem.path,
"--scale-window",
"--fontcache",
"-r",
selectedItem.path,
"--scale-window",
"--fontcache",
// "--font-config",
// "1:,1.2",
// "--sharpness",
// "1.0",
// "--sharpness",
// "1.0",
});
intent.putExtras(extras);
startActivity(intent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,20 @@ private static void setupCurrentDirectory() {
Globals.CurrentDirectoryPath = null;
ArrayList<String> arrayList = new ArrayList<>();
TreeSet<String> treeSet = new TreeSet<>();
TreeSet<String> validDirectory = new TreeSet<>();
for (String str : Globals.CURRENT_DIRECTORY_PATH_TEMPLATE_ARRAY) {
if (!str.contains("${SDCARD}")) {
treeSet.add(str);
} else {
treeSet.add(str.replace("${SDCARD}", Environment.getExternalStorageDirectory().getAbsolutePath()));
treeSet.add(str.replace("${SDCARD}", "/mnt/ext_card"));
treeSet.add(str.replace("${SDCARD}", "/mnt/flash"));
treeSet.add(str.replace("${SDCARD}", "/mnt/sdcard"));
treeSet.add(str.replace("${SDCARD}", "/mnt/sdcard/external_sd"));
treeSet.add(str.replace("${SDCARD}", "/mnt/sdcard-ext"));
treeSet.add(str.replace("${SDCARD}", "/mnt/storage/sdcard"));
treeSet.add(str.replace("${SDCARD}", "/mnt/udisk"));
treeSet.add(str.replace("${SDCARD}", "/mnt/usbdisk"));
treeSet.add(str.replace("${SDCARD}", "/sdcard"));
treeSet.add(str.replace("${SDCARD}", "/sdcard/sd"));
treeSet.add(str.replace("${SDCARD}", "/storage/sdcard"));
treeSet.add(str.replace("${SDCARD}", "/storage/sdcard0"));
treeSet.add(str.replace("${SDCARD}", "/storage/sdcard1"));
treeSet.add(str.replace("${SDCARD}", "/storage/emulated/0"));
treeSet.add(str.replace("${SDCARD}", "/storage/emulated/legacy"));
treeSet.add(str.replace("${SDCARD}", "/storage/usb0"));
validDirectory.add(Environment.getExternalStorageDirectory().getAbsolutePath());
for (String inlineSd: Globals.FallbackDirectoryPathArray) {
File inlineSdF = new File(inlineSd);
if (inlineSdF.exists() && inlineSdF.isDirectory() && inlineSdF.canRead()) {
treeSet.add(str.replace("${SDCARD}", inlineSd));
validDirectory.add(inlineSd);
}
}
String str2 = System.getenv("EXTERNAL_STORAGE");
if (str2 != null) {
treeSet.add(str.replace("${SDCARD}", str2));
Expand Down Expand Up @@ -116,8 +109,9 @@ private static void setupCurrentDirectory() {
}
}
}
Globals.CurrentDirectoryPathArray = (String[]) treeSet.toArray(new String[0]);
Globals.CurrentDirectoryValidPathArray = (String[]) arrayList.toArray(new String[0]);
Globals.CurrentDirectoryPathArray = treeSet.toArray(new String[0]);
Globals.CurrentDirectoryValidPathArray = arrayList.toArray(new String[0]);
Globals.FallbackDirectoryValidPathArray = validDirectory.toArray(new String[0]);
}

public static DisplayImageOptions getDisplayImageOptions() {
Expand Down
3 changes: 2 additions & 1 deletion project/android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
<string name="alt_icon">图标</string>
<string name="alt_play">运行</string>
<string name="alt_settings">设置</string>
<string name="Launch_ChooseDirectory">选择目录</string>
<string name="Launch_OtherChooseDirectory">选择其它目录</string>
<string name="Launch_AutoChooseDirectory">自动扫描目录</string>
<string name="Launch_SetDirectory">设置目录</string>
<string name="Launch_CouldNotOpenDirectory">无法打开目录</string>
<string name="Other">其它</string>
Expand Down
6 changes: 3 additions & 3 deletions project/android/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id("com.android.application") version "8.1.3" apply false
id("com.android.library") version "8.1.3" apply false
id("com.android.application") version "8.3.0" apply false
id("com.android.library") version "8.3.0" apply false
// id("org.jetbrains.kotlin.android") version "1.7.22" apply false
}
}
2 changes: 1 addition & 1 deletion project/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Dec 06 13:44:19 CST 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

0 comments on commit da65f47

Please sign in to comment.