diff --git a/app/src/main/java/com/zfdang/touchhelper/TouchHelperServiceImpl.java b/app/src/main/java/com/zfdang/touchhelper/TouchHelperServiceImpl.java index 4aac27e..babbbc3 100644 --- a/app/src/main/java/com/zfdang/touchhelper/TouchHelperServiceImpl.java +++ b/app/src/main/java/com/zfdang/touchhelper/TouchHelperServiceImpl.java @@ -73,6 +73,7 @@ public class TouchHelperServiceImpl { private Set setPackages, setIMEApps, setHomes, setWhiteList; private Set clickedWidgets; private List keyWordList; + private int skipCounter = 0; private Map mapPackagePositions; private Map> mapPackageWidgets; @@ -158,7 +159,7 @@ public void run() { } } - public void onInterrupt(){ + public void onInterrupt() { stopSkipAdProcess(); } @@ -253,11 +254,11 @@ public void onAccessibilityEvent(AccessibilityEvent event) { // Log.d(TAG, " currentPackageName = " + currentPackageName + " currentActivityName = " + currentActivityName); CharSequence tempPkgName = event.getPackageName(); CharSequence tempClassName = event.getClassName(); - if(tempPkgName == null || tempClassName == null) return; + if (tempPkgName == null || tempClassName == null) return; try { switch (event.getEventType()) { case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED: - if(setIMEApps.contains(tempPkgName)) { + if (setIMEApps.contains(tempPkgName)) { // IME might be temporarily started in the package, skip this event break; } @@ -266,9 +267,9 @@ public void onAccessibilityEvent(AccessibilityEvent event) { final String actName = tempClassName.toString(); boolean isActivity = !actName.startsWith("android.") && !actName.startsWith("androidx."); - if(!currentPackageName.equals(pkgName)) { + if (!currentPackageName.equals(pkgName)) { // new package, is it a activity? - if(isActivity) { + if (isActivity) { // yes, it's an activity // since it's an activity in another package, it must be a new activity, save them currentPackageName = pkgName; @@ -277,7 +278,7 @@ public void onAccessibilityEvent(AccessibilityEvent event) { // stop current skip ad process if it exists stopSkipAdProcess(); - if(setPackages.contains(pkgName)) { + if (setPackages.contains(pkgName)) { // if the package is in our list, start skip ads process // this is the only place to start skip ad process startSkipAdProcess(); @@ -285,9 +286,9 @@ public void onAccessibilityEvent(AccessibilityEvent event) { } } else { // current package, we just save the activity - if(isActivity) { + if (isActivity) { // yes, it's an activity - if(!currentActivityName.equals(actName)) { + if (!currentActivityName.equals(actName)) { // new activity in the package, this means this activity is not the first activity any more, stop skip ad process // update: there are some cases that ad-activity is not the first activity in the package, so don't stop skip ad process // stopSkipAdProcess(); @@ -314,12 +315,12 @@ public void onAccessibilityEvent(AccessibilityEvent event) { @Override public void run() { if (num < PackagePositionClickRetry) { - if(currentActivityName.equals(packagePositionDescription.activityName)) { + if (currentActivityName.equals(packagePositionDescription.activityName)) { // current activity is null, or current activity is the target activity // Log.d(TAG, "Find skip-ad by position, simulate click now! "); click(packagePositionDescription.x, packagePositionDescription.y, 0, 40); } - num ++; + num++; } else { throw new RuntimeException(); } @@ -335,7 +336,7 @@ public void run() { skipad_by_activity_widget = false; setTargetedWidgets = mapPackageWidgets.get(currentPackageName); } - if(setTargetedWidgets != null) { + if (setTargetedWidgets != null) { // Log.d(TAG, "Find skip-ad by widget, simulate click "); // this code could be run multiple times skipAdByTargetedWidget(service.getRootInActiveWindow(), setTargetedWidgets); @@ -348,7 +349,7 @@ public void run() { } break; case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED: - if(!setPackages.contains(tempPkgName)) { + if (!setPackages.contains(tempPkgName)) { break; } @@ -382,7 +383,8 @@ public void onUnbind(Intent intent) { /** * 查找并点击包含keyword控件,目标包括Text和Description - * * */ + * * + */ private void skipAdByKeywords(AccessibilityNodeInfo root) { // Log.d(TAG, "skipAdByKeywords triggered: " + Utilities.describeAccessibilityNode(root)); @@ -402,14 +404,14 @@ private void skipAdByKeywords(AccessibilityNodeInfo root) { CharSequence text = node.getText(); // try to find keyword - for (String keyword: keyWordList) { + for (String keyword : keyWordList) { // text or description contains keyword, but not too long (<= length + 6) - if (text != null && (text.toString().length() <= keyword.length() + 6 ) && text.toString().contains(keyword) && !text.toString().equals(SelfPackageName)) { + if (text != null && (text.toString().length() <= keyword.length() + 6) && text.toString().contains(keyword) && !text.toString().equals(SelfPackageName)) { isFind = true; - } else if (description != null && (description.toString().length() <= keyword.length() + 6) && description.toString().contains(keyword) && !description.toString().equals(SelfPackageName)) { + } else if (description != null && (description.toString().length() <= keyword.length() + 6) && description.toString().contains(keyword) && !description.toString().equals(SelfPackageName)) { isFind = true; } - if(isFind) { + if (isFind) { // if this node matches our target, stop finding more keywords // Log.d(TAG, "identify keyword = " + keyword); break; @@ -420,12 +422,13 @@ private void skipAdByKeywords(AccessibilityNodeInfo root) { if (isFind) { String nodeDesc = Utilities.describeAccessibilityNode(node); // Log.d(TAG, nodeDesc); - if(!clickedWidgets.contains(nodeDesc)){ + if (!clickedWidgets.contains(nodeDesc)) { clickedWidgets.add(nodeDesc); ShowToastInIntentService("正在根据关键字跳过广告..."); boolean clicked = node.performAction(AccessibilityNodeInfo.ACTION_CLICK); // Log.d(TAG, "self clicked = " + clicked); + skipCounter++; if (!clicked) { Rect rect = new Rect(); node.getBoundsInScreen(rect); @@ -488,7 +491,7 @@ private void skipAdByTargetedWidget(AccessibilityNodeInfo root, Set roots = new ArrayList<>(); roots.add(root); ArrayList nodeList = new ArrayList<>(); @@ -556,13 +559,13 @@ private String dumpRootNode(AccessibilityNodeInfo root) { } private void dumpChildNodes(AccessibilityNodeInfo root, List list, StringBuilder dumpString, String indent) { - if(root == null) return; + if (root == null) return; list.add(root); dumpString.append(indent + Utilities.describeAccessibilityNode(root) + "\n"); for (int n = 0; n < root.getChildCount(); n++) { AccessibilityNodeInfo child = root.getChild(n); - dumpChildNodes(child, list, dumpString,indent + " "); + dumpChildNodes(child, list, dumpString, indent + " "); } } @@ -592,7 +595,7 @@ private void startSkipAdProcess() { clickedWidgets.clear(); // cancel all methods N seconds later - if( !futureExpireSkipAdProcess.isCancelled() && !futureExpireSkipAdProcess.isDone()) { + if (!futureExpireSkipAdProcess.isCancelled() && !futureExpireSkipAdProcess.isDone()) { futureExpireSkipAdProcess.cancel(true); } futureExpireSkipAdProcess = executorService.schedule(new Runnable() { @@ -609,7 +612,7 @@ public void run() { private void stopSkipAdProcess() { // Log.d(TAG, "Stop Skip-ad process"); stopSkipAdProcessInner(); - if( !futureExpireSkipAdProcess.isCancelled() && !futureExpireSkipAdProcess.isDone()) { + if (!futureExpireSkipAdProcess.isCancelled() && !futureExpireSkipAdProcess.isDone()) { futureExpireSkipAdProcess.cancel(false); } } @@ -954,7 +957,7 @@ public void onClick(View v) { public void ShowToastInIntentService(final String sText) { final Context myContext = this.service; // show one toast in 5 seconds only - if(mSetting.isSkipAdNotification()) { + if (mSetting.isSkipAdNotification()) { new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { @@ -962,6 +965,10 @@ public void run() { toast.show(); } }); - }; - }; + } + } + + public int getSkipCounter() { + return skipCounter; + } } diff --git a/app/src/main/java/com/zfdang/touchhelper/ui/home/HomeFragment.java b/app/src/main/java/com/zfdang/touchhelper/ui/home/HomeFragment.java index d6e498b..6ef979b 100644 --- a/app/src/main/java/com/zfdang/touchhelper/ui/home/HomeFragment.java +++ b/app/src/main/java/com/zfdang/touchhelper/ui/home/HomeFragment.java @@ -42,10 +42,8 @@ public class HomeFragment extends Fragment { private HomeViewModel homeViewModel; - public View onCreateView(@NonNull LayoutInflater inflater, - ViewGroup container, Bundle savedInstanceState) { - homeViewModel = - ViewModelProviders.of(this).get(HomeViewModel.class); + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class); View root = inflater.inflate(R.layout.fragment_home, container, false); final Drawable drawableYes = ContextCompat.getDrawable(getContext(), R.drawable.ic_right); @@ -56,7 +54,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, homeViewModel.getAccessibilityPermission().observe(getViewLifecycleOwner(), new Observer() { @Override public void onChanged(Boolean aBoolean) { - if(aBoolean) { + if (aBoolean) { imageAccessibilityPermission.setImageDrawable(drawableYes); } else { imageAccessibilityPermission.setImageDrawable(drawableNo); @@ -68,7 +66,7 @@ public void onChanged(Boolean aBoolean) { homeViewModel.getPowerOptimization().observe(getViewLifecycleOwner(), new Observer() { @Override public void onChanged(Boolean aBoolean) { - if(aBoolean) { + if (aBoolean) { imagePowerPermission.setImageDrawable(drawableYes); } else { imagePowerPermission.setImageDrawable(drawableNo); @@ -76,6 +74,13 @@ public void onChanged(Boolean aBoolean) { } }); + final TextView blockCounter = root.findViewById(R.id.block_counter); + homeViewModel.getBlockCounter().observe(getViewLifecycleOwner(), new Observer() { + @Override + public void onChanged(Integer integer) { + blockCounter.setText(integer.toString()); + } + }); // set listener for buttons final ImageButton btAccessibilityPermission = root.findViewById(R.id.button_accessibility_permission); @@ -93,7 +98,7 @@ public void onClick(View v) { @Override public void onClick(View v) { // 打开电池优化的界面,让用户设置 - if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { String packageName = getActivity().getPackageName(); // open battery optimization setting page @@ -118,11 +123,10 @@ public void onResume() { super.onResume(); } - public void checkServiceStatus(){ + public void checkServiceStatus() { // detect the app storage permission - boolean bAppPermission = - ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; + boolean bAppPermission = ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; MutableLiveData liveData = homeViewModel.getAppPermission(); liveData.setValue(bAppPermission); @@ -135,5 +139,10 @@ public void checkServiceStatus(){ boolean hasIgnored = pm.isIgnoringBatteryOptimizations(getContext().getPackageName()); MutableLiveData power = homeViewModel.getPowerOptimization(); power.setValue(hasIgnored); + + if (TouchHelperService.serviceImpl != null) { + MutableLiveData blockCounter = homeViewModel.getBlockCounter(); + blockCounter.setValue(TouchHelperService.serviceImpl.getSkipCounter()); + } } } \ No newline at end of file diff --git a/app/src/main/java/com/zfdang/touchhelper/ui/home/HomeViewModel.java b/app/src/main/java/com/zfdang/touchhelper/ui/home/HomeViewModel.java index 7d8e5f0..053bab7 100644 --- a/app/src/main/java/com/zfdang/touchhelper/ui/home/HomeViewModel.java +++ b/app/src/main/java/com/zfdang/touchhelper/ui/home/HomeViewModel.java @@ -11,12 +11,14 @@ public class HomeViewModel extends ViewModel { private MutableLiveData mAppPermission; private MutableLiveData mAccessibilityPermission; private MutableLiveData mPowerOptimization; + private MutableLiveData mBlockCounter; public HomeViewModel() { mText = new MutableLiveData<>(); mAppPermission = new MutableLiveData<>(); mAccessibilityPermission = new MutableLiveData<>(); mPowerOptimization = new MutableLiveData<>(); + mBlockCounter = new MutableLiveData<>(); } public LiveData getText() { @@ -34,4 +36,8 @@ public MutableLiveData getAccessibilityPermission() { public MutableLiveData getPowerOptimization() { return mPowerOptimization; } + + public MutableLiveData getBlockCounter() { + return mBlockCounter; + } } \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index 08d05f9..13b953e 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -31,6 +31,26 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/text_instructions"> + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3fa0dcc..498975b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -13,6 +13,7 @@ https://touchhelper.zfdang.com/enable 设置 + 已拦截次数 开屏跳过-无障碍服务 \"开屏跳过\"是基于安卓系统的无障碍服务来实现自动跳过广告功能的,为了保证程序能正常工作,打开此服务。\n\n本程序为开源程序,不需要网络权限,不会收集或上传任何个人信息,请放心使用!