Skip to content

Commit da65f47

Browse files
committed
feat: android select dir support sd dir
1 parent d690eaf commit da65f47

File tree

6 files changed

+83
-59
lines changed

6 files changed

+83
-59
lines changed

project/android/app/src/main/java/com/zeromake/onscripter/Globals.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,24 @@ public class Globals {
77
public static String[] CurrentDirectoryValidPathArray = null;
88
public static final String[] CURRENT_DIRECTORY_PATH_TEMPLATE_ARRAY = new String[]{"${SDCARD}/mine", "${SDCARD}/ons"};
99
public static boolean CurrentGameRunning = false;
10+
11+
public static String[] FallbackDirectoryPathArray = new String[]{
12+
"/mnt/ext_card",
13+
"/mnt/flash",
14+
"/mnt/sdcard",
15+
"/mnt/sdcard/external_sd",
16+
"/mnt/sdcard-ext",
17+
"/mnt/storage/sdcard",
18+
"/mnt/udisk",
19+
"/mnt/usbdisk",
20+
"/sdcard",
21+
"/sdcard/sd",
22+
"/storage/sdcard",
23+
"/storage/sdcard0",
24+
"/storage/sdcard1",
25+
"/storage/emulated/0",
26+
"/storage/emulated/legacy",
27+
"/storage/usb0",
28+
};
29+
public static String[] FallbackDirectoryValidPathArray = null;
1030
}

project/android/app/src/main/java/com/zeromake/onscripter/MainActivity.java

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@
4646
import java.util.stream.Stream;
4747

4848
public class MainActivity extends Activity implements
49-
OnPermissionCallback,
50-
DialogInterface.OnClickListener,
51-
AdapterView.OnItemClickListener,
52-
View.OnClickListener{
49+
OnPermissionCallback,
50+
DialogInterface.OnClickListener,
51+
AdapterView.OnItemClickListener,
52+
View.OnClickListener {
5353
private ImageView background;
5454
private ImageView btnSettings;
5555
private ImageView cover;
@@ -77,12 +77,12 @@ protected void onCreate(Bundle savedInstanceState) {
7777
ONSVariable.dh = displayMetrics.heightPixels;
7878
int phoneType = ((TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE)).getPhoneType();
7979
double doubleValue = new BigDecimal(
80-
ONSVariable.dw / ONSVariable.dh
80+
ONSVariable.dw / ONSVariable.dh
8181
).setScale(
82-
2,
83-
RoundingMode.HALF_UP
82+
2,
83+
RoundingMode.HALF_UP
8484
).doubleValue();
85-
if ((float)ONSVariable.dw / (float)ONSVariable.dh == 1.5d || doubleValue == 1.33d) {
85+
if ((float) ONSVariable.dw / (float) ONSVariable.dh == 1.5d || doubleValue == 1.33d) {
8686
setContentView(R.layout.activity_ft);
8787
} else if (phoneType == 0) {
8888
setContentView(R.layout.activity_pad);
@@ -102,7 +102,7 @@ protected void onCreate(Bundle savedInstanceState) {
102102
this.items.setOnPlayClickListener(this);
103103
}
104104

105-
private<T extends View> T bindView(int i) {
105+
private <T extends View> T bindView(int i) {
106106
return findViewById(i);
107107
}
108108

@@ -118,18 +118,19 @@ private void externalStoragePermission() {
118118
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
119119
if (!Environment.isExternalStorageManager()) {
120120
XXPermissions.with(this).permission(
121-
Permission.MANAGE_EXTERNAL_STORAGE
121+
Permission.MANAGE_EXTERNAL_STORAGE
122122
).request(this);
123123
}
124124
} else {
125125
XXPermissions.with(this).permission(
126-
Permission.READ_MEDIA_IMAGES,
127-
Permission.READ_MEDIA_VIDEO,
128-
Permission.READ_MEDIA_AUDIO
126+
Permission.READ_MEDIA_IMAGES,
127+
Permission.READ_MEDIA_VIDEO,
128+
Permission.READ_MEDIA_AUDIO
129129
// Permission.WRITE_EXTERNAL_STORAGE
130130
).request(this);
131131
}
132132
}
133+
133134
@Override
134135
public void onGranted(List<String> permissions, boolean all) {
135136
// Toast.makeText(MainActivity.this, "权限申请成功", Toast.LENGTH_SHORT).show();
@@ -152,16 +153,16 @@ public void setting() {
152153
int width = size.x;
153154
int height = size.y;
154155
settingPopupWindow = new PopupWindow(
155-
linearLayout,
156-
width/4,
157-
height/3,
158-
true
156+
linearLayout,
157+
width / 4,
158+
height / 3,
159+
true
159160
);
160161
settingPopupWindow.setAnimationStyle(R.style.Animation_ConfigPanelAnimation);
161162
settingPopupWindow.setTouchable(true);
162163
settingPopupWindow.setOutsideTouchable(true);
163164
settingPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.popup));
164-
settingPopupWindow.showAtLocation(this.background, Gravity.END|Gravity.BOTTOM, width / 14, height / 24);
165+
settingPopupWindow.showAtLocation(this.background, Gravity.END | Gravity.BOTTOM, width / 14, height / 24);
165166
View popupButton = linearLayout.findViewById(R.id.popup_path);
166167
popupButton.setOnClickListener(view -> this.chooseDir());
167168
}
@@ -173,7 +174,7 @@ private void chooseDir() {
173174
strArr[strArr.length - 1] = getString(R.string.Other) + "...";
174175
AlertDialog.Builder builder = new AlertDialog.Builder(this);
175176
builder.setInverseBackgroundForced(true);
176-
builder.setTitle(getString(R.string.Launch_ChooseDirectory));
177+
builder.setTitle(getString(R.string.Launch_AutoChooseDirectory));
177178
builder.setItems(strArr, this);
178179
builder.setNegativeButton(getString(R.string.Cancel), null);
179180
builder.setCancelable(true);
@@ -218,6 +219,20 @@ public void run() {
218219
}
219220
}
220221

222+
private void onOther() {
223+
AlertDialog.Builder builder = new AlertDialog.Builder(this);
224+
builder.setTitle(getString(R.string.Launch_OtherChooseDirectory));
225+
this.mDirBrowserDirFileArray = new File[Globals.FallbackDirectoryValidPathArray.length];
226+
for (int index = 0; index < Globals.FallbackDirectoryValidPathArray.length; index++) {
227+
this.mDirBrowserDirFileArray[index] = new File(Globals.FallbackDirectoryValidPathArray[index]);
228+
}
229+
builder.setItems(Globals.FallbackDirectoryValidPathArray, this);
230+
builder.setNegativeButton(getString(R.string.Cancel), null);
231+
builder.setCancelable(true);
232+
this.mDirBrowserDialog = builder.create();
233+
this.mDirBrowserDialog.show();
234+
}
235+
221236
@Override
222237
public void onClick(DialogInterface dialogInterface, int i) {
223238
if (dialogInterface == this.mDirBrowserDialog) {
@@ -229,15 +244,9 @@ public void onClick(DialogInterface dialogInterface, int i) {
229244
Settings.SaveGlobals(this);
230245
loadCurrentDirectory();
231246
return;
232-
} else if (Globals.CurrentDirectoryPathForLauncher == null || Globals.CurrentDirectoryPathForLauncher.equals("")) {
233-
File f = new File("/storage");
234-
if (f.exists() && f.isDirectory() && f.canRead()) {
235-
this.mDirBrowserCurDirPath = "/storage";
236-
} else {
237-
this.mDirBrowserCurDirPath = "/mnt";
238-
}
239247
} else {
240-
this.mDirBrowserCurDirPath = Globals.CurrentDirectoryPathForLauncher;
248+
this.onOther();
249+
return;
241250
}
242251
File file = new File(this.mDirBrowserCurDirPath);
243252
File parentFile = file.getParentFile();
@@ -249,7 +258,7 @@ public void onClick(DialogInterface dialogInterface, int i) {
249258
try (Stream<Path> stream = Files.list(Paths.get(this.mDirBrowserCurDirPath))) {
250259
stream.forEach(path -> {
251260
if (Files.isDirectory(path)
252-
&& Files.isReadable(path)) {
261+
&& Files.isReadable(path)) {
253262
dirs.add(new File(path.toAbsolutePath().toString()));
254263
}
255264
});
@@ -316,11 +325,11 @@ private void loadGameItem(Game game) {
316325
}
317326

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

322331
private void updateBackground(String background) {
323-
ImageLoader.getInstance().displayImage("file:/"+background, this.background, Settings.getDisplayImageOptions());
332+
ImageLoader.getInstance().displayImage("file:/" + background, this.background, Settings.getDisplayImageOptions());
324333
}
325334

326335
@Override
@@ -332,14 +341,14 @@ public void onClick(View view) {
332341
// types(0:全局文字,1:普通文字, 2:精灵文字, 3: 菜单文字, 4: 弹窗文字, 5: ruby 文字),字体大小,字体大小倍率,字体颜色,是否描边,描边宽度,描边颜色,文字换行距离边距,文字换列距离边距
333342
// 大部分游戏只有 1, 2 生效,后面的文字都是用精灵文字去做的,还有不少弹框是用图片做的。
334343
extras.putStringArray(ONScripter.ARGS_KEY, new String[]{
335-
"-r",
336-
selectedItem.path,
337-
"--scale-window",
338-
"--fontcache",
344+
"-r",
345+
selectedItem.path,
346+
"--scale-window",
347+
"--fontcache",
339348
// "--font-config",
340349
// "1:,1.2",
341-
// "--sharpness",
342-
// "1.0",
350+
// "--sharpness",
351+
// "1.0",
343352
});
344353
intent.putExtras(extras);
345354
startActivity(intent);

project/android/app/src/main/java/com/zeromake/onscripter/Settings.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,20 @@ private static void setupCurrentDirectory() {
5050
Globals.CurrentDirectoryPath = null;
5151
ArrayList<String> arrayList = new ArrayList<>();
5252
TreeSet<String> treeSet = new TreeSet<>();
53+
TreeSet<String> validDirectory = new TreeSet<>();
5354
for (String str : Globals.CURRENT_DIRECTORY_PATH_TEMPLATE_ARRAY) {
5455
if (!str.contains("${SDCARD}")) {
5556
treeSet.add(str);
5657
} else {
5758
treeSet.add(str.replace("${SDCARD}", Environment.getExternalStorageDirectory().getAbsolutePath()));
58-
treeSet.add(str.replace("${SDCARD}", "/mnt/ext_card"));
59-
treeSet.add(str.replace("${SDCARD}", "/mnt/flash"));
60-
treeSet.add(str.replace("${SDCARD}", "/mnt/sdcard"));
61-
treeSet.add(str.replace("${SDCARD}", "/mnt/sdcard/external_sd"));
62-
treeSet.add(str.replace("${SDCARD}", "/mnt/sdcard-ext"));
63-
treeSet.add(str.replace("${SDCARD}", "/mnt/storage/sdcard"));
64-
treeSet.add(str.replace("${SDCARD}", "/mnt/udisk"));
65-
treeSet.add(str.replace("${SDCARD}", "/mnt/usbdisk"));
66-
treeSet.add(str.replace("${SDCARD}", "/sdcard"));
67-
treeSet.add(str.replace("${SDCARD}", "/sdcard/sd"));
68-
treeSet.add(str.replace("${SDCARD}", "/storage/sdcard"));
69-
treeSet.add(str.replace("${SDCARD}", "/storage/sdcard0"));
70-
treeSet.add(str.replace("${SDCARD}", "/storage/sdcard1"));
71-
treeSet.add(str.replace("${SDCARD}", "/storage/emulated/0"));
72-
treeSet.add(str.replace("${SDCARD}", "/storage/emulated/legacy"));
73-
treeSet.add(str.replace("${SDCARD}", "/storage/usb0"));
59+
validDirectory.add(Environment.getExternalStorageDirectory().getAbsolutePath());
60+
for (String inlineSd: Globals.FallbackDirectoryPathArray) {
61+
File inlineSdF = new File(inlineSd);
62+
if (inlineSdF.exists() && inlineSdF.isDirectory() && inlineSdF.canRead()) {
63+
treeSet.add(str.replace("${SDCARD}", inlineSd));
64+
validDirectory.add(inlineSd);
65+
}
66+
}
7467
String str2 = System.getenv("EXTERNAL_STORAGE");
7568
if (str2 != null) {
7669
treeSet.add(str.replace("${SDCARD}", str2));
@@ -116,8 +109,9 @@ private static void setupCurrentDirectory() {
116109
}
117110
}
118111
}
119-
Globals.CurrentDirectoryPathArray = (String[]) treeSet.toArray(new String[0]);
120-
Globals.CurrentDirectoryValidPathArray = (String[]) arrayList.toArray(new String[0]);
112+
Globals.CurrentDirectoryPathArray = treeSet.toArray(new String[0]);
113+
Globals.CurrentDirectoryValidPathArray = arrayList.toArray(new String[0]);
114+
Globals.FallbackDirectoryValidPathArray = validDirectory.toArray(new String[0]);
121115
}
122116

123117
public static DisplayImageOptions getDisplayImageOptions() {

project/android/app/src/main/res/values/strings.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
<string name="alt_icon">图标</string>
99
<string name="alt_play">运行</string>
1010
<string name="alt_settings">设置</string>
11-
<string name="Launch_ChooseDirectory">选择目录</string>
11+
<string name="Launch_OtherChooseDirectory">选择其它目录</string>
12+
<string name="Launch_AutoChooseDirectory">自动扫描目录</string>
1213
<string name="Launch_SetDirectory">设置目录</string>
1314
<string name="Launch_CouldNotOpenDirectory">无法打开目录</string>
1415
<string name="Other">其它</string>

project/android/build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
plugins {
2-
id("com.android.application") version "8.1.3" apply false
3-
id("com.android.library") version "8.1.3" apply false
2+
id("com.android.application") version "8.3.0" apply false
3+
id("com.android.library") version "8.3.0" apply false
44
// id("org.jetbrains.kotlin.android") version "1.7.22" apply false
5-
}
5+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Tue Dec 06 13:44:19 CST 2022
22
distributionBase=GRADLE_USER_HOME
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
44
distributionPath=wrapper/dists
55
zipStorePath=wrapper/dists
66
zipStoreBase=GRADLE_USER_HOME

0 commit comments

Comments
 (0)