Skip to content

Commit

Permalink
fix: use single WindowManager instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Xtr126 committed Aug 4, 2024
1 parent 5bfe7c8 commit c7ef61d
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions app/src/main/java/xtr/keymapper/server/RemoteService.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class RemoteService extends IRemoteService.Stub {
private Context context = null;
private int TYPE_SECURE_SYSTEM_OVERLAY;
Handler mHandler = new Handler(Looper.getMainLooper());
private WindowManager windowManager;

public RemoteService() {

Expand All @@ -53,6 +54,7 @@ public RemoteService(Context context) {
}

public RemoteService init(Context context) {
windowManager = context.getSystemService(WindowManager.class);
PackageManager pm = context.getPackageManager();
String packageName = context.getPackageName();
try {
Expand All @@ -62,7 +64,6 @@ public RemoteService init(Context context) {
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(e);
}

this.context = context;
return this;
}
Expand All @@ -74,7 +75,6 @@ public void destroy() {
}

private void addCursorView() {
WindowManager windowManager = context.getSystemService(WindowManager.class);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT,
TYPE_SECURE_SYSTEM_OVERLAY,
Expand All @@ -88,12 +88,18 @@ private void addCursorView() {
// through the cursor
PixelFormat.TRANSLUCENT);

mHandler.post(() -> windowManager.addView(cursorView, params));
mHandler.post(() -> {
if(cursorView != null) windowManager.addView(cursorView, params);
});
}

private void removeCursorView() {
WindowManager windowManager = context.getSystemService(WindowManager.class);
mHandler.post(() -> windowManager.removeView(cursorView));
mHandler.post(() -> {
cursorView.setVisibility(View.GONE);
if (cursorView.isAttachedToWindow()) windowManager.removeView(cursorView);
cursorView.invalidate();
cursorView = null;
});
}

public void prepareCursorOverlayWindow() throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException, InvocationTargetException {
Expand All @@ -102,7 +108,6 @@ public void prepareCursorOverlayWindow() throws NoSuchMethodException, NoSuchFie
cursorView = CursorBinding.inflate(layoutInflater).getRoot();
TYPE_SECURE_SYSTEM_OVERLAY = WindowManager.LayoutParams.class.getField("TYPE_SECURE_SYSTEM_OVERLAY").getInt(null);
Binder sWindowToken = new Binder();
WindowManager windowManager = context.getSystemService(WindowManager.class);
Method setDefaultTokenMethod = windowManager.getClass().getMethod("setDefaultToken", IBinder.class);
setDefaultTokenMethod.invoke(windowManager, sWindowToken);
}
Expand Down Expand Up @@ -173,7 +178,7 @@ public void startServer(KeymapProfile profile, KeymapConfig keymapConfig, IRemot
} catch (Exception e) {
Log.e("overlayWindow", e.getMessage(), e);
}
if (cursorView != null) addCursorView();
addCursorView();
} else {
cursorView = null;
}
Expand Down

0 comments on commit c7ef61d

Please sign in to comment.