Skip to content

Commit

Permalink
增强稳定性
Browse files Browse the repository at this point in the history
修复部分问题。增强稳定性
  • Loading branch information
LnJan committed Jan 7, 2019
1 parent 46f6ce6 commit c866458
Show file tree
Hide file tree
Showing 207 changed files with 157 additions and 60 deletions.
Binary file modified app/src/main/ic_launcher-web.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
Expand All @@ -16,6 +17,8 @@
import android.view.View;
import android.view.Window;
import android.view.accessibility.AccessibilityManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.Toast;

Expand Down Expand Up @@ -89,6 +92,22 @@ protected void onCreate(Bundle savedInstanceState) {

checkAuthority();

/*mImageView = findViewById(R.id.imv_show_cache);
mImageView.setImageBitmap(BitmapFactory.decodeFile(FeatureDetectionManager.getInstance().getMoneyPicPath()));
Button btnNext = findViewById(R.id.btn_next_pic);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String str = getDirFileNameByIndex(getCacheDir().getAbsolutePath(), mIndex);
if(str==null){
Toast.makeText(MainActivity.this,"没有图片了",Toast.LENGTH_SHORT).show();
}else{
mImageView.setImageBitmap(BitmapFactory.decodeFile(str));
mIndex++;
}
}
});*/

//初始化bugly
Bugly.init(getApplicationContext(), "2212e773ac", true);
}
Expand Down Expand Up @@ -270,7 +289,7 @@ private void openPowerStagey() {

startActivity(intent);

Toast.makeText(this,getString(R.string.background_authority_tips),Toast.LENGTH_LONG).show();
Toast.makeText(this, getString(R.string.background_authority_tips), Toast.LENGTH_LONG).show();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

import org.opencv.core.CvException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -65,13 +68,12 @@ public class HongbaoService extends AccessibilityService {
//private String prevActivityName = CHATTING_LAUNCHER_UI;
private boolean mGlobalMutex = false;
private boolean mPockeyOpenMutex = false;
private boolean isNormalScreen = true;
private SharedPreferences mSharedPreferences;
private HongbaoServiceHandler mHandler = new HongbaoServiceHandler(this);
private boolean isAutoClose = false; //遇到超时页面需要返回时使用此标志位
private PowerUtil mPowerUtil = null;
private LockScreenReceiver mReceiver = null;
private List<String> mSelfOpenList = null;
private int mPackeyTag = 0x00;

@Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
Expand Down Expand Up @@ -110,7 +112,6 @@ public void onDestroy() {
@Override
protected void onServiceConnected() {
super.onServiceConnected();
isNormalScreen = checkScreenSizeIsNormal();
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
this.mPowerUtil = new PowerUtil(this);
this.mReceiver = new LockScreenReceiver(this);
Expand Down Expand Up @@ -152,21 +153,22 @@ private void handleScreenMessage(AccessibilityEvent ev) {
Log.e(TAG, "Recv_ui:AccessibilityNodeInfo null");
}*/
if (mSharedPreferences.getBoolean("pref_watch_chat", false)) {
if (mPackeyTag != 0x01) {
return;
}
mPackeyTag = 0x02;
int delay = mSharedPreferences.getInt(SeekBarPreference.PREFERENCE_TAG, 0);
if (delay == 0x00) {
openPacket();
} else {
mHandler.sendEmptyMessageDelayed(HANDLER_POSTDELAY_OPEN, delay);
}
if (!isAutoClose) {
isAutoClose = true;
mHandler.sendEmptyMessageDelayed(HANDLER_CLOSE_PACKEY, delay + 1000);
}
mHandler.sendEmptyMessageDelayed(HANDLER_CLOSE_PACKEY, delay + 1000);
}
} else if (LUCKY_MONEY_DETAIL_UI.equals(currentActivityName)) { //红包详情页面
if (currentActivityName.equals(currentNodeInfoName)) {
Log.e(TAG, "detail UI");
isAutoClose = false;
mPackeyTag = 0x00;
performGlobalAction(GLOBAL_ACTION_BACK);
}
}
Expand Down Expand Up @@ -255,10 +257,10 @@ private Rect checkNewsImageIsLuckyMoney() {
continue;
}
Bitmap bmSub = Bitmap.createBitmap(bmScreenShot, x, y, width, height);
//SaveBitmapToLocal(bmSub);
SaveBitmapToLocal(bmSub);
boolean b = false;
try {
b = FeatureDetectionManager.getInstance().isPictureMatchLuckyMoney(bmSub, isNormalScreen);
b = FeatureDetectionManager.getInstance().isPictureMatchLuckyMoney(bmSub, ScreenShotter.getInstance().isNormalScreen());
} catch (CvException ex) {
b = false;
}
Expand All @@ -273,7 +275,7 @@ private Rect checkNewsImageIsLuckyMoney() {
return resultRect;
}

/*private void SaveBitmapToLocal(Bitmap bmp) {
private void SaveBitmapToLocal(Bitmap bmp) {
String strSavePath = getCacheDir().getAbsolutePath() + File.separator + java.util.UUID.randomUUID().toString() + ".jpg";
try {
File f = new File(strSavePath);
Expand All @@ -286,20 +288,6 @@ private Rect checkNewsImageIsLuckyMoney() {
} catch (IOException e) {
e.printStackTrace();
}
}*/

/**
* 检查屏幕尺寸是否16:9
* 某些18:9的全面屏手机会出现截屏位置偏移的问题
*
* @return
*/
private boolean checkScreenSizeIsNormal() {
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
return screenHeight < screenWidth * 1.8;
}

private List<Rect> findNewsRectInScreen() {
Expand All @@ -313,9 +301,9 @@ private List<Rect> findNewsRectInScreen() {
if (subChartInfo.getChildCount() > 0) { //表示是未读消息,有可能有红包
Rect outputRect = new Rect();
subChartInfo.getBoundsInScreen(outputRect);
if (!isNormalScreen) {
outputRect.top -= 30;
outputRect.bottom -= 30;
if (!ScreenShotter.getInstance().isNormalScreen()) {
outputRect.top -= 20;
outputRect.bottom -= 20;
}
if (outputRect.height() == 0 || outputRect.width() == 0) {
continue;
Expand Down Expand Up @@ -414,6 +402,12 @@ private void findRedpockeyAndClick(AccessibilityEvent ev) {
if (openPackeyInfo != null) {
AccessibilityNodeInfo parentInfo = openPackeyInfo.getParent();
if (parentInfo != null) {
if (mSharedPreferences.getBoolean("pref_watch_chat", false)) {
if (mPackeyTag != 0x00) {
return;
}
mPackeyTag = 0x01;
}
parentInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
}
Expand Down Expand Up @@ -472,13 +466,15 @@ private void openPacket() {
public void onCompleted(GestureDescription gestureDescription) {
Log.e(TAG, "onCompleted");
mPockeyOpenMutex = false;
mPackeyTag = 0x03;
super.onCompleted(gestureDescription);
}

@Override
public void onCancelled(GestureDescription gestureDescription) {
Log.e(TAG, "onCancelled");
mPockeyOpenMutex = false;
mPackeyTag = 0x03;
super.onCancelled(gestureDescription);
}
}, null);
Expand Down Expand Up @@ -569,8 +565,8 @@ static class HongbaoServiceHandler extends Handler {
public void handleMessage(Message msg) {
switch (msg.what) {
case HANDLER_CLOSE_PACKEY:
if (mRef.get().isAutoClose) {
mRef.get().isAutoClose = false;
if (mRef.get().mPackeyTag == 0x03) {
mRef.get().mPackeyTag = 0x00;
mRef.get().performGlobalAction(GLOBAL_ACTION_BACK);
}
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package com.shareder.ln_jan.wechatluckymoneygetter.utils;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.media.Image;
import android.media.ImageReader;
import android.media.projection.MediaProjection;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.shareder.ln_jan.wechatluckymoneygetter.global.MyApplication;

import java.nio.ByteBuffer;

Expand All @@ -23,10 +31,16 @@ public class ScreenShotter {

private MediaProjection mMediaProjection;
private ImageReader mImageReader;
private boolean mIsNormalScreen = true;
private volatile Point[] mRealSizes = new Point[2];

private static final int PORTRAIT = 0;
private static final int LANDSCAPE = 1;

private ScreenShotter() {
mMediaProjection = null;
mImageReader = null;
mIsNormalScreen = checkScreenSizeIsNormal();
}

public static ScreenShotter getInstance() {
Expand All @@ -36,6 +50,10 @@ public static ScreenShotter getInstance() {
return instance;
}

public boolean isNormalScreen() {
return mIsNormalScreen;
}

public void setMediaProjection(MediaProjection projection) {
mMediaProjection = projection;
}
Expand All @@ -52,15 +70,15 @@ public Bitmap getScreenShotSync() {
if (mImageReader == null) {
mImageReader = ImageReader.newInstance(
getScreenWidth(),
getScreenHeight(),
mIsNormalScreen ? getScreenHeight() : getScreenRealHeight(),
PixelFormat.RGBA_8888,//此处必须和下面 buffer处理一致的格式 ,RGB_565在一些机器上出现兼容问题。
1);
}

VirtualDisplay tmpDisplay = virtualDisplay();
try{
try {
Thread.sleep(50); //需要稍微停一下,否则截图为空
}catch (InterruptedException e){
} catch (InterruptedException e) {
e.printStackTrace();
}
Image img = mImageReader.acquireLatestImage();
Expand Down Expand Up @@ -105,4 +123,39 @@ private int getScreenWidth() {
private int getScreenHeight() {
return Resources.getSystem().getDisplayMetrics().heightPixels;
}

/**
* 某些全面屏手机获取的屏幕宽度不对,需用此方法获取宽度
* @return
*/
public int getScreenRealHeight() {
int orientation = MyApplication.getContext().getResources().getConfiguration().orientation;
orientation = orientation == Configuration.ORIENTATION_PORTRAIT ? PORTRAIT : LANDSCAPE;

if (mRealSizes[orientation] == null) {
WindowManager windowManager = (WindowManager) MyApplication.getContext().getSystemService(Context.WINDOW_SERVICE);
if (windowManager == null) {
return getScreenHeight();
}
Display display = windowManager.getDefaultDisplay();
Point point = new Point();
display.getRealSize(point);
mRealSizes[orientation] = point;
}
return mRealSizes[orientation].y;
}

/**
* 检查屏幕尺寸是否16:9
* 某些18:9的全面屏手机会出现截屏位置偏移的问题
*
* @return
*/
private boolean checkScreenSizeIsNormal() {
DisplayMetrics dm = new DisplayMetrics();
dm = Resources.getSystem().getDisplayMetrics();
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
return screenHeight < screenWidth * 1.8;
}
}
Loading

0 comments on commit c866458

Please sign in to comment.