Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit 14da0a8

Browse files
committed
v1.1.0
1 parent 88f64bc commit 14da0a8

14 files changed

Lines changed: 151 additions & 39 deletions

File tree

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ android {
77
applicationId "com.RichardLuo.notificationpush"
88
minSdkVersion 22
99
targetSdkVersion 28
10-
versionCode 12
10+
versionCode 13
1111
versionName "1.1.0"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
}

app/release/app-release.apk

3.11 KB
Binary file not shown.

app/release/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":12,"versionName":"1.1.0","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
1+
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":13,"versionName":"1.1.0","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]

app/src/main/AndroidManifest.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@
4343
<action android:name="com.google.firebase.MESSAGING_EVENT" />
4444
</intent-filter>
4545
</service>
46+
<service
47+
android:name=".ForegroundMonitor"
48+
android:label="@string/access_name"
49+
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
50+
<intent-filter>
51+
<action android:name="android.accessibilityservice.AccessibilityService" />
52+
</intent-filter>
53+
<meta-data
54+
android:name="android.accessibilityservice"
55+
android:resource="@xml/accessibility" />
56+
</service>
4657
</application>
4758

4859
</manifest>

app/src/main/java/com/RichardLuo/notificationpush/Application.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void onNothingSelected(AdapterView<?> parent) {
6262

6363
}
6464
};
65-
packageView.add(new info(name, packageManager.getApplicationIcon(applicationInfo), onItemSelectedListener,preferences.getInt(applicationInfo.packageName, 0)));
65+
packageView.add(new info(name, packageManager.getApplicationIcon(applicationInfo), onItemSelectedListener, preferences.getInt(applicationInfo.packageName, 0)));
6666
}
6767

6868
handler.post(new Runnable() {
@@ -132,5 +132,9 @@ class info {
132132
}.start();
133133
}
134134

135-
135+
@Override
136+
public boolean onSupportNavigateUp() {
137+
finish();
138+
return true;
139+
}
136140
}

app/src/main/java/com/RichardLuo/notificationpush/FCMReceiver.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
4646
senderName = data.get("senderName");
4747
String packageName = remoteMessage.getData().get("package");
4848
PendingIntent intent;
49+
if (getSharedPreferences("MainActivity", MODE_PRIVATE).getBoolean("hide", false) && ForegroundMonitor.packageName.equals(packageName))
50+
return;
4951

5052
setChannel(packageName);
5153

@@ -56,6 +58,9 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
5658
case "com.tencent.tim":
5759
case "com.tencent.mobileqq":
5860
case "com.jinhaihan.qqnotfandshare":
61+
String className = ForegroundMonitor.packageName;
62+
if (getSharedPreferences("MainActivity", MODE_PRIVATE).getBoolean("hide", false) && (className.contains("com.tencent.") && (className.contains("qq") || className.contains("tim"))))
63+
return;
5964
String QQpackageName = getSharedPreferences("MainActivity", MODE_PRIVATE).getString("installedQQ", null);
6065
intent = getIntent(QQpackageName);
6166
if (senderName == null)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.RichardLuo.notificationpush;
2+
3+
import android.accessibilityservice.AccessibilityService;
4+
import android.view.accessibility.AccessibilityEvent;
5+
6+
public class ForegroundMonitor extends AccessibilityService {
7+
static String packageName = "";
8+
9+
@Override
10+
public void onAccessibilityEvent(AccessibilityEvent event) {
11+
if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
12+
if (event.getPackageName() == null)
13+
return;
14+
packageName = event.getPackageName().toString();
15+
}
16+
}
17+
18+
@Override
19+
public void onInterrupt() {
20+
21+
}
22+
}

app/src/main/java/com/RichardLuo/notificationpush/MainActivity.java

Lines changed: 74 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.app.NotificationChannel;
44
import android.app.NotificationManager;
55
import android.content.ComponentName;
6+
import android.content.Context;
67
import android.content.DialogInterface;
78
import android.content.Intent;
89
import android.content.SharedPreferences;
@@ -19,7 +20,6 @@
1920
import android.widget.Button;
2021
import android.widget.CompoundButton;
2122
import android.widget.EditText;
22-
import android.widget.Spinner;
2323
import android.widget.Switch;
2424
import android.widget.TextView;
2525
import android.widget.Toast;
@@ -31,12 +31,12 @@
3131

3232
import java.util.List;
3333

34-
//TODO 增加应用前台判断
35-
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
34+
public class MainActivity extends AppCompatActivity implements Switch.OnCheckedChangeListener {
3635
static SharedPreferences preferences;
37-
static Intent service;
38-
boolean isEnabled;
36+
boolean isNotiListenerEnabled;
37+
boolean isAccessEnabled;
3938
Switch swh;
39+
Switch hide;
4040
EditText input;
4141
TextView DeviceID;
4242
Button priority;
@@ -53,12 +53,27 @@ protected void onCreate(Bundle savedInstanceState) {
5353
setTheme(preferences.getInt("style", R.style.base_AppTheme_teal));
5454
setContentView(R.layout.activity_main);
5555
swh = findViewById(R.id.switch1);
56+
swh.setOnCheckedChangeListener(this);
5657

57-
priority = findViewById(R.id.priority);
58-
priority.setOnClickListener(new Button.OnClickListener() {
58+
hide = findViewById(R.id.switch2);
59+
hide.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener() {
5960
@Override
60-
public void onClick(View v) {
61-
startActivity(new Intent(getApplicationContext(), Application.class));
61+
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
62+
Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
63+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
64+
if (isChecked) {
65+
if (isAccessEnabled) {
66+
preferences.edit().putBoolean("hide", true).apply();
67+
} else {
68+
startActivity(intent);
69+
preferences.edit().putBoolean("hide", false).apply();
70+
hide.setChecked(false);
71+
}
72+
} else {
73+
if (isAccessEnabled)
74+
startActivity(intent);
75+
preferences.edit().putBoolean("hide", false).apply();
76+
}
6277
}
6378
});
6479

@@ -77,6 +92,14 @@ public void onClick(View v) {
7792
}
7893
});
7994

95+
priority = findViewById(R.id.priority);
96+
priority.setOnClickListener(new Button.OnClickListener() {
97+
@Override
98+
public void onClick(View v) {
99+
startActivity(new Intent(getApplicationContext(), Application.class));
100+
}
101+
});
102+
80103
colors = findViewById(R.id.colors);
81104
colors.setOnClickListener(new View.OnClickListener() {
82105
@Override
@@ -146,7 +169,7 @@ public void onClick(DialogInterface dialog, int which) {
146169
normalDialog.show();
147170
}
148171
});
149-
swh.setOnCheckedChangeListener(this);
172+
150173
FirebaseInstanceId.getInstance().getInstanceId()
151174
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
152175
@Override
@@ -158,7 +181,6 @@ public void onComplete(@NonNull Task<InstanceIdResult> task) {
158181
DeviceID.setText(task.getResult().getToken());
159182
}
160183
});
161-
startService(new Intent(this, FCMReceiver.class));
162184
input.setText(preferences.getString("ID", ""));
163185

164186
List<ApplicationInfo> packageInfo = this.getPackageManager().getInstalledApplications(0);
@@ -181,12 +203,19 @@ public void onComplete(@NonNull Task<InstanceIdResult> task) {
181203
protected void onStart() {
182204
super.onStart();
183205
if (isNotificationListenersEnabled()) {
184-
isEnabled = true;
206+
isNotiListenerEnabled = true;
185207
swh.setChecked(true);
186208
} else {
187-
isEnabled = false;
209+
isNotiListenerEnabled = false;
188210
swh.setChecked(false);
189211
}
212+
if (isAccessibilitySettingsOn(this)) {
213+
isAccessEnabled = true;
214+
hide.setChecked(true);
215+
} else {
216+
isAccessEnabled = false;
217+
hide.setChecked(false);
218+
}
190219
preferences.edit().putString("ID", input.getText().toString()).apply();
191220
}
192221

@@ -196,10 +225,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
196225
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
197226
if (isChecked) {
198227
if (!input.getText().toString().trim().isEmpty()) {
199-
if (isEnabled) {
228+
if (isNotiListenerEnabled) {
200229
preferences.edit().putString("ID", input.getText().toString()).apply();
201230
input.setEnabled(false);
202-
startService(service = new Intent(this, GetNotification.class));
203231
} else {
204232
startActivity(intent);
205233
input.setEnabled(true);
@@ -212,10 +240,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
212240
swh.setChecked(false);
213241
}
214242
} else {
215-
if (isEnabled)
243+
if (isNotiListenerEnabled)
216244
startActivity(intent);
217245
input.setEnabled(true);
218-
stopService(service);
219246
}
220247
}
221248

@@ -235,4 +262,34 @@ public boolean isNotificationListenersEnabled() {
235262
}
236263
return false;
237264
}
265+
266+
267+
private boolean isAccessibilitySettingsOn(Context mContext) {
268+
int accessibilityEnabled;
269+
final String service = getPackageName() + "/" + ForegroundMonitor.class.getCanonicalName();
270+
try {
271+
accessibilityEnabled = Settings.Secure.getInt(
272+
mContext.getApplicationContext().getContentResolver(),
273+
android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
274+
} catch (Settings.SettingNotFoundException e) {
275+
return false;
276+
}
277+
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
278+
279+
if (accessibilityEnabled == 1) {
280+
String settingValue = Settings.Secure.getString(
281+
mContext.getApplicationContext().getContentResolver(),
282+
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
283+
if (settingValue != null) {
284+
mStringColonSplitter.setString(settingValue);
285+
while (mStringColonSplitter.hasNext()) {
286+
String accessibilityService = mStringColonSplitter.next();
287+
if (accessibilityService.equalsIgnoreCase(service)) {
288+
return true;
289+
}
290+
}
291+
}
292+
}
293+
return false;
294+
}
238295
}

app/src/main/res/layout/activity_main.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
android:layout_height="wrap_content"
2525
android:paddingTop="10dp"
2626
android:paddingBottom="10dp"
27-
android:text="@string/start"
27+
android:text="@string/hide"
2828
android:textSize="18sp"
2929
tools:checked="true" />
3030

@@ -49,18 +49,18 @@
4949
android:textIsSelectable="true" />
5050

5151
<Button
52-
android:id="@+id/priority"
52+
android:id="@+id/clear"
5353
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
5454
android:layout_width="match_parent"
5555
android:layout_height="wrap_content"
56-
android:text="@string/priority" />
56+
android:text="@string/clear" />
5757

5858
<Button
59-
android:id="@+id/clear"
59+
android:id="@+id/priority"
6060
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
6161
android:layout_width="match_parent"
6262
android:layout_height="wrap_content"
63-
android:text="@string/clear" />
63+
android:text="@string/priority" />
6464

6565
<Button
6666
android:id="@+id/colors"

app/src/main/res/layout/app_layout.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@
1111
android:id="@+id/imageView"
1212
android:layout_width="25dp"
1313
android:layout_height="25dp"
14-
tools:srcCompat="@tools:sample/avatars"
1514
android:layout_marginStart="10dp"
16-
android:layout_marginEnd="10dp"/>
15+
android:layout_marginEnd="10dp"
16+
tools:srcCompat="@tools:sample/avatars" />
1717

1818
<TextView
1919
android:id="@+id/appName"
2020
android:layout_width="0dp"
2121
android:layout_height="wrap_content"
2222
android:layout_weight="1"
23+
android:text="@string/priority"
2324
android:textColor="@color/black"
24-
android:textSize="18sp"
25-
android:text="@string/priority"/>
25+
android:textSize="18sp" />
2626

2727
<Spinner
2828
android:id="@+id/spinner"

0 commit comments

Comments
 (0)