add: notification listener service

This commit is contained in:
hyb1996
2017-10-31 00:07:45 +08:00
parent bfd6a0c40a
commit cbd2600c33
11 changed files with 255 additions and 60 deletions

View File

@@ -27,11 +27,11 @@ import com.stardust.util.ScreenMetrics;
public class ScreenCapturer {
private static final String LOG_TAG = "ScreenCapturer";
private final Object mCachedImageLock = new Object();
private ImageReader mImageReader;
private MediaProjection mMediaProjection;
private VirtualDisplay mVirtualDisplay;
private volatile Looper mImageAcquireLooper;
private final Object mImageWaitingLock = new Object();
private volatile Image mUnderUsingImage;
private volatile Image mCachedImage;
private final int mScreenWidth;
@@ -87,7 +87,13 @@ public class ScreenCapturer {
@Override
public void onImageAvailable(ImageReader reader) {
if (mCachedImage != null) {
return;
synchronized (mCachedImageLock) {
if (mCachedImage != null) {
mCachedImage.close();
}
mCachedImage = reader.acquireLatestImage();
return;
}
}
mCachedImage = reader.acquireLatestImage();
}
@@ -99,8 +105,10 @@ public class ScreenCapturer {
if (mCachedImage != null) {
if (mUnderUsingImage != null)
mUnderUsingImage.close();
mUnderUsingImage = mCachedImage;
mCachedImage = null;
synchronized (mCachedImageLock) {
mUnderUsingImage = mCachedImage;
mCachedImage = null;
}
}
return mUnderUsingImage;
}

View File

@@ -1,9 +1,9 @@
package com.stardust.autojs.runtime.api;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.Notification;
import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.accessibility.AccessibilityEvent;
@@ -11,6 +11,8 @@ import android.view.accessibility.AccessibilityEvent;
import com.stardust.autojs.R;
import com.stardust.autojs.core.accessibility.AccessibilityBridge;
import com.stardust.autojs.core.eventloop.EventEmitter;
import com.stardust.notification.Notification;
import com.stardust.notification.NotificationListenerService;
import com.stardust.autojs.runtime.ScriptBridges;
import com.stardust.autojs.runtime.exception.ScriptException;
import com.stardust.autojs.core.inputevent.InputEventObserver;
@@ -136,9 +138,14 @@ public class Events extends EventEmitter implements OnKeyListener, TouchObserver
mListeningNotification = true;
ensureHandler();
mLoopers.waitWhenIdle(true);
mAccessibilityBridge.ensureServiceEnabled();
mAccessibilityBridge.getNotificationObserver()
.addListener(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2
&& NotificationListenerService.getInstance() != null) {
NotificationListenerService.getInstance().addListener(this);
} else {
mAccessibilityBridge.ensureServiceEnabled();
mAccessibilityBridge.getNotificationObserver()
.addListener(this);
}
}
public Events onNotification(Object listener) {
@@ -165,6 +172,10 @@ public class Events extends EventEmitter implements OnKeyListener, TouchObserver
}
if (mListeningNotification) {
mAccessibilityBridge.getNotificationObserver().removeListener(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2
&& NotificationListenerService.getInstance() != null) {
NotificationListenerService.getInstance().removeListener(this);
}
}
}
@@ -201,24 +212,11 @@ public class Events extends EventEmitter implements OnKeyListener, TouchObserver
});
}
@Override
public void onNotification(final AccessibilityEvent event, final NotificationInfo notification) {
public void onNotification(final Notification notification) {
mHandler.post(new Runnable() {
@Override
public void run() {
emit("toast", notification);
}
});
}
@Override
public void onNotification(final AccessibilityEvent event, final Notification notification) {
final NotificationInfo info = NotificationInfo.fromEvent(event);
mHandler.post(new Runnable() {
@Override
public void run() {
emit("notification", info, notification);
emit("notification", notification);
}
});

View File

@@ -99,7 +99,6 @@ public class Images {
Image image = mScreenCapturer.capture();
if (image != null) {
saveImage(image, path);
image.close();
return true;
}
return false;