Skip to content

Commit

Permalink
Fix a bug on Harmony3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DawningW committed Sep 1, 2023
1 parent d991457 commit 71482e6
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ android {
}

dependencies {
implementation 'androidx.appcompat:appcompat:1.6.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
Expand Down
Binary file added app/release/app-release.apk
Binary file not shown.
5 changes: 5 additions & 0 deletions app/src/main/java/kongsang/swybot/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
Expand Down Expand Up @@ -81,6 +82,10 @@ private void requestPermissions() {
}

private void startGame() {
// HACK Harmony3.0在解开应用锁后swybot仍为竖屏状态, 导致分辨率获取错误
if (SystemUtil.isHarmonyOS() && SystemUtil.getHarmonyVersion() >= 3) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
startActivity(getPackageManager().getLaunchIntentForPackage(SWY_PACKAGE_NAME));
finish();
}
Expand Down
14 changes: 8 additions & 6 deletions app/src/main/java/kongsang/swybot/SwyBotService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Surface;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityEvent;

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

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

public int getScreenDpi() {
Expand Down Expand Up @@ -232,6 +232,8 @@ public void showMenu(String title, List<String> items, DialogInterface.OnClickLi
.setItems(items.toArray(new String[0]), (dialog, which) -> {
if (which == items.size() - 1) {
disableSelf();
// HACK 禁用无障碍服务后Application未结束导致空指针异常
android.os.Process.killProcess(android.os.Process.myPid());
} else {
listener.onClick(dialog, which);
}
Expand Down
29 changes: 29 additions & 0 deletions app/src/main/java/kongsang/swybot/SystemUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kongsang.swybot;

import android.accessibilityservice.AccessibilityService;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.provider.Settings;
Expand All @@ -13,6 +14,7 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;

public final class SystemUtil {
private static final String TAG = "SystemUtil";
Expand Down Expand Up @@ -66,4 +68,31 @@ public static void saveBitmap(Context context, String name, Bitmap bitmap) {
e.printStackTrace();
}
}

public static boolean isHarmonyOS() {
try {
Class<?> clz = Class.forName("com.huawei.system.BuildEx");
Method method = clz.getMethod("getOsBrand");
String ret = (String) method.invoke(clz);
return "harmony".equals(ret);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

@SuppressLint("PrivateApi")
public static int getHarmonyVersion() {
try {
Class<?> clz = Class.forName("android.os.SystemProperties");
Method method = clz.getDeclaredMethod("get", String.class);
String ret = (String) method.invoke(clz, "hw_sc.build.platform.version");
if (!TextUtils.isEmpty(ret)) {
return Integer.parseInt(ret.split("\\.")[0]);
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.4.0' apply false
id 'com.android.library' version '7.4.0' apply false
id 'com.android.application' version '7.4.2' apply false
id 'com.android.library' version '7.4.2' apply false
id 'com.chaquo.python' version '14.0.2' apply false
}

0 comments on commit 71482e6

Please sign in to comment.