Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加统计拦截次数 #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public class TouchHelperServiceImpl {
private Set<String> setPackages, setIMEApps, setHomes, setWhiteList;
private Set<String> clickedWidgets;
private List<String> keyWordList;
private int skipCounter = 0;

private Map<String, PackagePositionDescription> mapPackagePositions;
private Map<String, Set<PackageWidgetDescription>> mapPackageWidgets;
Expand Down Expand Up @@ -158,7 +159,7 @@ public void run() {
}
}

public void onInterrupt(){
public void onInterrupt() {
stopSkipAdProcess();
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -277,17 +278,17 @@ 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();
}
}
} 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();
Expand All @@ -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();
}
Expand All @@ -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);
Expand All @@ -348,7 +349,7 @@ public void run() {
}
break;
case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
if(!setPackages.contains(tempPkgName)) {
if (!setPackages.contains(tempPkgName)) {
break;
}

Expand Down Expand Up @@ -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));

Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -488,7 +491,7 @@ private void skipAdByTargetedWidget(AccessibilityNodeInfo root, Set<PackageWidge
if (isFind) {
// Log.d(TAG, "Find skip-ad by Widget " + e.toString());
String nodeDesc = Utilities.describeAccessibilityNode(node);
if(!clickedWidgets.contains(nodeDesc)) {
if (!clickedWidgets.contains(nodeDesc)) {
// add this widget to clicked widget, avoid multiple click on the same widget
clickedWidgets.add(nodeDesc);

Expand Down Expand Up @@ -523,7 +526,7 @@ private void skipAdByTargetedWidget(AccessibilityNodeInfo root, Set<PackageWidge
}


private void showAllChildren(AccessibilityNodeInfo root){
private void showAllChildren(AccessibilityNodeInfo root) {
ArrayList<AccessibilityNodeInfo> roots = new ArrayList<>();
roots.add(root);
ArrayList<AccessibilityNodeInfo> nodeList = new ArrayList<>();
Expand Down Expand Up @@ -556,13 +559,13 @@ private String dumpRootNode(AccessibilityNodeInfo root) {
}

private void dumpChildNodes(AccessibilityNodeInfo root, List<AccessibilityNodeInfo> 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 + " ");
}
}

Expand Down Expand Up @@ -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() {
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -954,14 +957,18 @@ 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() {
Toast toast = Toast.makeText(myContext, sText, Toast.LENGTH_SHORT);
toast.show();
}
});
};
};
}
}

public int getSkipCounter() {
return skipCounter;
}
}
29 changes: 19 additions & 10 deletions app/src/main/java/com/zfdang/touchhelper/ui/home/HomeFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -56,7 +54,7 @@ public View onCreateView(@NonNull LayoutInflater inflater,
homeViewModel.getAccessibilityPermission().observe(getViewLifecycleOwner(), new Observer<Boolean>() {
@Override
public void onChanged(Boolean aBoolean) {
if(aBoolean) {
if (aBoolean) {
imageAccessibilityPermission.setImageDrawable(drawableYes);
} else {
imageAccessibilityPermission.setImageDrawable(drawableNo);
Expand All @@ -68,14 +66,21 @@ public void onChanged(Boolean aBoolean) {
homeViewModel.getPowerOptimization().observe(getViewLifecycleOwner(), new Observer<Boolean>() {
@Override
public void onChanged(Boolean aBoolean) {
if(aBoolean) {
if (aBoolean) {
imagePowerPermission.setImageDrawable(drawableYes);
} else {
imagePowerPermission.setImageDrawable(drawableNo);
}
}
});

final TextView blockCounter = root.findViewById(R.id.block_counter);
homeViewModel.getBlockCounter().observe(getViewLifecycleOwner(), new Observer<Integer>() {
@Override
public void onChanged(Integer integer) {
blockCounter.setText(integer.toString());
}
});

// set listener for buttons
final ImageButton btAccessibilityPermission = root.findViewById(R.id.button_accessibility_permission);
Expand All @@ -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
Expand All @@ -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<Boolean> liveData = homeViewModel.getAppPermission();
liveData.setValue(bAppPermission);

Expand All @@ -135,5 +139,10 @@ public void checkServiceStatus(){
boolean hasIgnored = pm.isIgnoringBatteryOptimizations(getContext().getPackageName());
MutableLiveData<Boolean> power = homeViewModel.getPowerOptimization();
power.setValue(hasIgnored);

if (TouchHelperService.serviceImpl != null) {
MutableLiveData<Integer> blockCounter = homeViewModel.getBlockCounter();
blockCounter.setValue(TouchHelperService.serviceImpl.getSkipCounter());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public class HomeViewModel extends ViewModel {
private MutableLiveData<Boolean> mAppPermission;
private MutableLiveData<Boolean> mAccessibilityPermission;
private MutableLiveData<Boolean> mPowerOptimization;
private MutableLiveData<Integer> mBlockCounter;

public HomeViewModel() {
mText = new MutableLiveData<>();
mAppPermission = new MutableLiveData<>();
mAccessibilityPermission = new MutableLiveData<>();
mPowerOptimization = new MutableLiveData<>();
mBlockCounter = new MutableLiveData<>();
}

public LiveData<String> getText() {
Expand All @@ -34,4 +36,8 @@ public MutableLiveData<Boolean> getAccessibilityPermission() {
public MutableLiveData<Boolean> getPowerOptimization() {
return mPowerOptimization;
}

public MutableLiveData<Integer> getBlockCounter() {
return mBlockCounter;
}
}
20 changes: 20 additions & 0 deletions app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_instructions">

<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/block_counter"
android:textSize="16sp" />

<TextView
android:id="@+id/block_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="0"
android:textSize="40dp" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<string name="text_status_weblink">https://touchhelper.zfdang.com/enable</string>

<string name="button_setting">设置</string>
<string name="block_counter">已拦截次数</string>
<string name="touch_helper_service_label">开屏跳过-无障碍服务</string>
<string name="touch_helper_service_description">\"开屏跳过\"是基于安卓系统的无障碍服务来实现自动跳过广告功能的,为了保证程序能正常工作,打开此服务。\n\n本程序为开源程序,不需要网络权限,不会收集或上传任何个人信息,请放心使用!</string>
<string name="about_app_introduction">
Expand Down