Skip to content

Commit

Permalink
Merge pull request #60 from Jinnrry/feature-input
Browse files Browse the repository at this point in the history
新增文本输入功能
  • Loading branch information
Jinnrry authored Sep 1, 2022
2 parents ebd39f5 + 2252f3e commit b66f6c6
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Android/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
/.idea/
build
7 changes: 6 additions & 1 deletion Android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ android {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}
buildToolsVersion = '28.0.3'
buildFeatures {
viewBinding true
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
implementation 'android.arch.navigation:navigation-fragment:1.0.0'
implementation 'android.arch.navigation:navigation-ui:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
Expand Down
18 changes: 16 additions & 2 deletions Android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,22 @@
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>

<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibility_service_config" />
</service> <!-- 输入法service -->
<service
android:name=".Service.FastInputIME"
android:label="RobotInput"
android:permission="android.permission.BIND_INPUT_METHOD">
<intent-filter>
<action android:name="android.view.InputMethod" />
</intent-filter>

<meta-data
android:name="android.view.im"
android:resource="@xml/method" />
</service>
<service
android:name=".Service.Controller"
Expand All @@ -48,9 +61,10 @@
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</activity> <!-- 输入法activity -->


<provider
Expand All @@ -60,7 +74,7 @@
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
android:resource="@xml/provider_paths" />
</provider>
</application>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,8 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

import java.io.IOException;
import java.io.InputStream;

import cn.xjiangwei.RobotHelper.MainApplication;
import cn.xjiangwei.RobotHelper.Tools.Image;
import cn.xjiangwei.RobotHelper.Tools.MLog;
Expand All @@ -37,6 +29,22 @@ public void start() {
//Robot.setExecType(Robot.ExecTypeAccessibillty); //使用安卓无障碍接口执行模拟操作
//Robot.setExecType(Robot.ExecTypeROOT) //使用root权限执行模拟操作(实验阶段,仅在oneplus 7pro测试过,欢迎提bug)

/**************************** 文本输入Demo ******************************/
boolean ret = Robot.input("Hello World!");
if (ret){
MLog.info("文本输入成功!");
}else {
MLog.error("文本输入失败!请检查当前焦点是否在文本框,或RobotHelper输入法是否设置为默认输入法!");
}
sleep(2000);
ret = Robot.input("你好呀!");
if (ret){
MLog.info("文本输入成功!");
}else {
MLog.error("文本输入失败!请检查当前焦点是否在文本框,或RobotHelper输入法是否设置为默认输入法!");
}


/**************************** 模板匹配demo *******************************/
InputStream is = null;
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cn.xjiangwei.RobotHelper.Service;

import android.inputmethodservice.InputMethodService;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;

public class FastInputIME extends InputMethodService {

private static InputConnection ic;

@Override
public void onWindowShown() {
super.onWindowShown();
FastInputIME.ic = getCurrentInputConnection();

}

@Override
public void onWindowHidden() {
super.onWindowHidden();
FastInputIME.ic = null;
}

public static boolean TextInput(String text){
if (ic == null) return false;

ic.commitText(text,1);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,27 @@ private RunTime() {
});
}

//获取一个运行实例
public static RunTime getInstance() {
if (instance == null) {
synchronized (RunTime.class) {
if (instance == null) {
synchronized (RunTime.class) {
if (instance == null) {
// 从来没有运行过
instance = new RunTime();
} else {
// 如果有实例正在运行,返回运行中的实例
if (instance.isRunning()) {
return instance;
} else {
// 实例运行结束了,返回新的实例
instance = new RunTime();
}
}


return instance;

}
return instance;

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public void swipe(float x1, float y1, float x2, float y2, float duration) {
}

@Override
public void input(String str) {

public boolean input(String str) {
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ public interface Input {
/**
* 往输入框输入文字
*
* @param str
*/
void input(String str);
boolean input(String str);


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public void swipe(float x1, float y1, float x2, float y2, float duration) {
}

@Override
public void input(String str) {

public boolean input(String str) {
return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ public void swipe(float x1, float y1, float x2, float y2, float duration) {
}

@Override
public void input(String str) {
public boolean input(String str) {
MLog.error("没有权限执行操作!请检查xposed或者无障碍权限!");
Toast.show("没有权限执行操作!请检查xposed或者无障碍权限!");
return false;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1054,8 +1054,8 @@ public void swipe(float x1, float y1, float x2, float y2, float duration) {
}

@Override
public void input(String str) {

public boolean input(String str) {
return false;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.os.Build;

import cn.xjiangwei.RobotHelper.MainApplication;
import cn.xjiangwei.RobotHelper.Service.FastInputIME;
import cn.xjiangwei.RobotHelper.Tools.InputImp.AccessibilityInput;
import cn.xjiangwei.RobotHelper.Tools.InputImp.Input;
import cn.xjiangwei.RobotHelper.Tools.InputImp.InstrumentationInput;
Expand Down Expand Up @@ -130,15 +131,18 @@ public static void swipe(float x1, float y1, float x2, float y2, float duration)
*
* @param str
*/
public static void input(String str) {
getInput().input(str);
public static boolean input(String str) {
// 统一使用输入法实现,暂时不使用单独实现
// getInput().input(str);

return FastInputIME.TextInput(str);
}


/**
* 放大屏幕(捏开)
*
* @param distance // 缩放距离,0到100
* @param distance // 缩放距离,0到100
*/
public static void pinchOpen(int distance) {
getInput().pinchOpen(distance);
Expand All @@ -148,7 +152,7 @@ public static void pinchOpen(int distance) {
/**
* 缩小屏幕(捏合)
*
* @param distance // 缩放距离,0到100
* @param distance // 缩放距离,0到100
*/
public static void pinchClose(int distance) {
getInput().pinchClose(distance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static Bitmap getScreenCapHorizontal() {
}
} while (image == null);
bitmap = covetBitmap(image);
if (bitmapCacheHorizontal != null && !bitmapCacheVertical.isRecycled()) {
if (bitmapCacheHorizontal != null && !bitmapCacheHorizontal.isRecycled()) {
bitmapCacheHorizontal.recycle();
bitmapCacheHorizontal = null;
}
Expand Down
3 changes: 3 additions & 0 deletions Android/app/src/main/res/xml/method.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<input-method xmlns:android="http://schemas.android.com/apk/res/android"
android:settingsActivity="cn.xjiangwei.RobotHelper.MainActivity"
android:supportsSwitchingToNextInputMethod="true"></input-method>
2 changes: 1 addition & 1 deletion Android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
classpath 'com.android.tools.build:gradle:7.1.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
2 changes: 1 addition & 1 deletion Android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-all.zip
12 changes: 12 additions & 0 deletions UPDATE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
## V2.5版本新功能

1.新增文本输入功能

2。修复脚本无法多次运行的bug

## V2.4.3版本新功能

Bug Fix #55

感谢HuRuWo反馈

## V2.4.2版本新功能

1.修复截图被系统GC后出现的`recycled bitmap` bug
Expand Down

0 comments on commit b66f6c6

Please sign in to comment.