diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fd45b12 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +*.iml +.gradle +/local.properties +/.idea/caches/build_file_checksums.ser +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +.DS_Store +/build +/captures +.externalNativeBuild diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..30aa626 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..7ac24c7 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..e0d5b93 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..f43b415 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,28 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 28 + defaultConfig { + applicationId "cn.com.codeteenager.accessibilityapp" + minSdkVersion 19 + targetSdkVersion 28 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation 'com.android.support:appcompat-v7:28.0.0-alpha1' + implementation 'com.android.support.constraint:constraint-layout:1.1.3' + 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' +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/app/src/androidTest/java/cn/com/codeteenager/accessibilityapp/ExampleInstrumentedTest.java b/app/src/androidTest/java/cn/com/codeteenager/accessibilityapp/ExampleInstrumentedTest.java new file mode 100644 index 0000000..d969090 --- /dev/null +++ b/app/src/androidTest/java/cn/com/codeteenager/accessibilityapp/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package cn.com.codeteenager.accessibilityapp; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("cn.com.codeteenager.accessibilityapp", appContext.getPackageName()); + } +} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..3304875 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/assets/Wandoujia.apk b/app/src/main/assets/Wandoujia.apk new file mode 100644 index 0000000..5efb0af Binary files /dev/null and b/app/src/main/assets/Wandoujia.apk differ diff --git a/app/src/main/java/cn/com/codeteenager/accessibilityapp/MainActivity.java b/app/src/main/java/cn/com/codeteenager/accessibilityapp/MainActivity.java new file mode 100644 index 0000000..b3d95ca --- /dev/null +++ b/app/src/main/java/cn/com/codeteenager/accessibilityapp/MainActivity.java @@ -0,0 +1,189 @@ +package cn.com.codeteenager.accessibilityapp; + +import android.Manifest; +import android.content.ActivityNotFoundException; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.media.MediaScannerConnection; +import android.net.Uri; +import android.os.Build; +import android.provider.Settings; +import android.support.annotation.NonNull; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.ContextCompat; +import android.support.v4.content.FileProvider; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.text.TextUtils; +import android.view.View; +import android.widget.Toast; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; + +public class MainActivity extends AppCompatActivity { + private Context mContext; + private final static int REQUEST_CODE = 100; + private Uri apkUri; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + mContext = this; + //将apk拷贝到sd卡上 + apkUri = copyAssetsFile(mContext, "Wandoujia.apk", getExternalFilesDir(null).getAbsolutePath()); + + findViewById(R.id.btn_uninstall).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE); + intent.setData(Uri.parse("package:" + "com.wandoujia.phoenix2")); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startActivity(intent); + } + }); + + findViewById(R.id.btn_check_permission).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + + if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED + || ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { + ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, + Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE); + } else { + if (!checkPermission(mContext)) { + requestPermission(mContext); + } + } + } + }); + + findViewById(R.id.btn_install).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent intent = new Intent(); + intent.setAction(Intent.ACTION_VIEW); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + if (Build.VERSION.SDK_INT >= 24) { + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + } else { + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + } + intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); + startActivity(intent); + } + }); + } + + /** + * 检查无障碍服务是否开启 + */ + public boolean checkPermission(Context context) { + int accessibilityEnabled = 0; + final String service = context.getPackageName() + "/" + MyAccessibilityService.class.getCanonicalName(); + try { + accessibilityEnabled = Settings.Secure.getInt( + context.getApplicationContext().getContentResolver(), + android.provider.Settings.Secure.ACCESSIBILITY_ENABLED); + } catch (Settings.SettingNotFoundException e) { + e.printStackTrace(); + } + TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':'); + + if (accessibilityEnabled == 1) { + String settingValue = Settings.Secure.getString( + context.getApplicationContext().getContentResolver(), + Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES); + if (settingValue != null) { + mStringColonSplitter.setString(settingValue); + while (mStringColonSplitter.hasNext()) { + String accessibilityService = mStringColonSplitter.next(); + if (accessibilityService.equalsIgnoreCase(service)) { + return true; + } + } + } + } else { + return false; + } + + return false; + } + + /** + * 开启无障碍服务 + * + * @param context + */ + public void requestPermission(Context context) { + Intent accessibleIntent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS); + context.startActivity(accessibleIntent); + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + if (requestCode == REQUEST_CODE) { + if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) { + if (!checkPermission(mContext)) { + requestPermission(mContext); + } + } else { + Toast.makeText(mContext, "权限授予失败", Toast.LENGTH_SHORT).show(); + } + return; + } + } + + /** + * 复制文件到SD卡 + * + * @param context + * @param fileName 复制的文件名 + * @param path 保存的目录路径 + * @return + */ + public Uri copyAssetsFile(Context context, String fileName, String path) { + try { + InputStream mInputStream = context.getAssets().open(fileName); + File file = new File(path); + if (!file.exists()) { + file.mkdir(); + } + File mFile = new File(path + File.separator + "test.apk"); + if (!mFile.exists()) + mFile.createNewFile(); + FileOutputStream mFileOutputStream = new FileOutputStream(mFile); + byte[] mbyte = new byte[1024]; + int i = 0; + while ((i = mInputStream.read(mbyte)) > 0) { + mFileOutputStream.write(mbyte, 0, i); + } + mInputStream.close(); + mFileOutputStream.close(); + Uri uri = null; + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + //包名.fileprovider + uri = FileProvider.getUriForFile(context, "cn.com.codeteenager.accessibilityapp.fileprovider", mFile); + } else { + uri = Uri.fromFile(mFile); + } + } catch (ActivityNotFoundException anfe) { + + } + MediaScannerConnection.scanFile(context, new String[]{mFile.getAbsolutePath()}, null, null); + return uri; + } catch (IOException e) { + e.printStackTrace(); + return null; + } catch (Exception e) { + return null; + } + } + +} diff --git a/app/src/main/java/cn/com/codeteenager/accessibilityapp/MyAccessibilityService.java b/app/src/main/java/cn/com/codeteenager/accessibilityapp/MyAccessibilityService.java new file mode 100755 index 0000000..7296ba4 --- /dev/null +++ b/app/src/main/java/cn/com/codeteenager/accessibilityapp/MyAccessibilityService.java @@ -0,0 +1,106 @@ +package cn.com.codeteenager.accessibilityapp; + +import android.accessibilityservice.AccessibilityService; +import android.view.accessibility.AccessibilityEvent; +import android.view.accessibility.AccessibilityNodeInfo; +import android.widget.Toast; + +import java.util.List; + + +public class MyAccessibilityService extends AccessibilityService { + + private final String INSTALL_AND_UNINSTALL = "com.android.packageinstaller"; + private String[] key = new String[]{"卸载", "下一步", "安装", "继续", "完成"}; + + /** + * 当服务连接成功时 + */ + @Override + protected void onServiceConnected() { + super.onServiceConnected(); + Toast.makeText(MyAccessibilityService.this, "服务连接成功", Toast.LENGTH_SHORT).show(); + } + + /** + * 当指定事件发出服务时 + * + * @param event + */ + @Override + public void onAccessibilityEvent(AccessibilityEvent event) { + System.out.println("窗口事件的包名" + event.getPackageName().toString()); + + if (event == null || !event.getPackageName().toString() + .contains("packageinstaller"))//不写完整包名,是因为某些手机(如小米)安装器包名是自定义的 + return; + + if (event.getSource() != null) { + + findAndPerformActions(event, "安装"); + findAndPerformActions(event, "下一步"); + findAndPerformActions(event, "卸载"); + findAndPerformActions(event, "完成"); +// switch (event.getPackageName().toString()) { +// case INSTALL_AND_UNINSTALL: +// for (int i = 0; i < key.length; i++) { +// AccessibilityNodeInfo nodeInfo = event.getSource(); +// if (nodeInfo != null) { +// List nodes = nodeInfo.findAccessibilityNodeInfosByText(key[i]); +// System.out.println("节点的个数" + nodes.size()); +// if (nodes != null && !nodes.isEmpty()) { +// AccessibilityNodeInfo node; +// for (int j = 0; j < nodes.size(); j++) { +// node = nodes.get(j); +// System.out.println("节点的类名" + node.getClassName().toString()); +// if ((node.getClassName().equals("android.widget.Button") || node.getClassName().equals("android.widget.TextView")) && node.isEnabled()) { +// node.performAction(AccessibilityNodeInfo.ACTION_CLICK); +// break; +// } +// } +// } +// } +// } +// break; +// default: +// break; +// } + } + } + + /** + * 功能:模拟用户点击操作 + * + * @param text + */ + private void findAndPerformActions(AccessibilityEvent event, String text) { + if (event.getSource() != null) { + // 判断当前界面为安装界面 + boolean isInstallPage = event.getPackageName().equals( + INSTALL_AND_UNINSTALL); + if (isInstallPage) { + List action_nodes = event.getSource() + .findAccessibilityNodeInfosByText(text); + if (action_nodes != null && !action_nodes.isEmpty()) { + AccessibilityNodeInfo node = null; + for (int i = 0; i < action_nodes.size(); i++) { + node = action_nodes.get(i); + // 执行按钮点击行为 + if ((node.getClassName().equals("android.widget.Button") || node.getClassName().equals("android.widget.TextView")) + && node.isEnabled()) { + node.performAction(AccessibilityNodeInfo.ACTION_CLICK); + } + } + } + } + } + } + + /** + * 终止服务时调用 + */ + @Override + public void onInterrupt() { + + } +} diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..1f6bb29 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..0d025f9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..f176b55 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,31 @@ + + + +