Skip to content

Commit 71482e6

Browse files
committed
Fix a bug on Harmony3.0
1 parent d991457 commit 71482e6

File tree

6 files changed

+45
-9
lines changed

6 files changed

+45
-9
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ android {
4949
}
5050

5151
dependencies {
52-
implementation 'androidx.appcompat:appcompat:1.6.0'
52+
implementation 'androidx.appcompat:appcompat:1.6.1'
5353
testImplementation 'junit:junit:4.13.2'
5454
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
5555
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

app/release/app-release.apk

36 MB
Binary file not shown.

app/src/main/java/kongsang/swybot/MainActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.app.NotificationManager;
55
import android.content.Context;
66
import android.content.Intent;
7+
import android.content.pm.ActivityInfo;
78
import android.content.pm.PackageInfo;
89
import android.content.pm.PackageManager;
910
import android.net.Uri;
@@ -81,6 +82,10 @@ private void requestPermissions() {
8182
}
8283

8384
private void startGame() {
85+
// HACK Harmony3.0在解开应用锁后swybot仍为竖屏状态, 导致分辨率获取错误
86+
if (SystemUtil.isHarmonyOS() && SystemUtil.getHarmonyVersion() >= 3) {
87+
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
88+
}
8489
startActivity(getPackageManager().getLaunchIntentForPackage(SWY_PACKAGE_NAME));
8590
finish();
8691
}

app/src/main/java/kongsang/swybot/SwyBotService.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.os.Handler;
2222
import android.util.DisplayMetrics;
2323
import android.util.Log;
24+
import android.view.Surface;
2425
import android.view.WindowManager;
2526
import android.view.accessibility.AccessibilityEvent;
2627

@@ -86,19 +87,18 @@ public void onDestroy() {
8687
stopTask();
8788
ScriptBridge.setService(null);
8889
floatingButton.hide();
89-
// FIXME 应用强制关闭后无障碍服务无法启动
90-
// android.os.Process.killProcess(android.os.Process.myPid());
9190
}
9291

9392
public int[] getScreenSize() {
9493
// getResources().getDisplayMetrics() 获得的宽高不包括状态栏和导航栏
9594
// TODO 如果以后 Android 多窗口流行则需要改为 WindowManager.getCurrentWindowMetrics()
9695
DisplayMetrics displayMetrics = new DisplayMetrics();
9796
windowManager.getDefaultDisplay().getRealMetrics(displayMetrics);
98-
if (displayMetrics.widthPixels > displayMetrics.heightPixels)
99-
return new int[] { displayMetrics.widthPixels, displayMetrics.heightPixels };
100-
else // TODO Harmony3.0在解开应用锁后swybot仍为竖屏状态, 导致分辨率获取错误
101-
return new int[] { displayMetrics.heightPixels, displayMetrics.widthPixels };
97+
int width = Math.max(displayMetrics.widthPixels, displayMetrics.heightPixels);
98+
int height = Math.min(displayMetrics.widthPixels, displayMetrics.heightPixels);
99+
int rotation = windowManager.getDefaultDisplay().getRotation();
100+
return rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270 ?
101+
new int[] { width, height } : new int[] { height, width };
102102
}
103103

104104
public int getScreenDpi() {
@@ -232,6 +232,8 @@ public void showMenu(String title, List<String> items, DialogInterface.OnClickLi
232232
.setItems(items.toArray(new String[0]), (dialog, which) -> {
233233
if (which == items.size() - 1) {
234234
disableSelf();
235+
// HACK 禁用无障碍服务后Application未结束导致空指针异常
236+
android.os.Process.killProcess(android.os.Process.myPid());
235237
} else {
236238
listener.onClick(dialog, which);
237239
}

app/src/main/java/kongsang/swybot/SystemUtil.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package kongsang.swybot;
22

33
import android.accessibilityservice.AccessibilityService;
4+
import android.annotation.SuppressLint;
45
import android.content.Context;
56
import android.graphics.Bitmap;
67
import android.provider.Settings;
@@ -13,6 +14,7 @@
1314
import java.io.File;
1415
import java.io.FileOutputStream;
1516
import java.io.IOException;
17+
import java.lang.reflect.Method;
1618

1719
public final class SystemUtil {
1820
private static final String TAG = "SystemUtil";
@@ -66,4 +68,31 @@ public static void saveBitmap(Context context, String name, Bitmap bitmap) {
6668
e.printStackTrace();
6769
}
6870
}
71+
72+
public static boolean isHarmonyOS() {
73+
try {
74+
Class<?> clz = Class.forName("com.huawei.system.BuildEx");
75+
Method method = clz.getMethod("getOsBrand");
76+
String ret = (String) method.invoke(clz);
77+
return "harmony".equals(ret);
78+
} catch (Exception e) {
79+
e.printStackTrace();
80+
}
81+
return false;
82+
}
83+
84+
@SuppressLint("PrivateApi")
85+
public static int getHarmonyVersion() {
86+
try {
87+
Class<?> clz = Class.forName("android.os.SystemProperties");
88+
Method method = clz.getDeclaredMethod("get", String.class);
89+
String ret = (String) method.invoke(clz, "hw_sc.build.platform.version");
90+
if (!TextUtils.isEmpty(ret)) {
91+
return Integer.parseInt(ret.split("\\.")[0]);
92+
}
93+
} catch (Exception e) {
94+
e.printStackTrace();
95+
}
96+
return 0;
97+
}
6998
}

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22
plugins {
3-
id 'com.android.application' version '7.4.0' apply false
4-
id 'com.android.library' version '7.4.0' apply false
3+
id 'com.android.application' version '7.4.2' apply false
4+
id 'com.android.library' version '7.4.2' apply false
55
id 'com.chaquo.python' version '14.0.2' apply false
66
}

0 commit comments

Comments
 (0)