diff --git a/app/build.gradle b/app/build.gradle index 9dcde44..ec40478 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -7,7 +7,7 @@ android { applicationId "com.RichardLuo.notificationpush" minSdkVersion 22 targetSdkVersion 28 - versionCode 12 + versionCode 13 versionName "1.1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } diff --git a/app/release/app-release.apk b/app/release/app-release.apk index 0bc4012..a556125 100644 Binary files a/app/release/app-release.apk and b/app/release/app-release.apk differ diff --git a/app/release/output.json b/app/release/output.json index 62b79fa..21c2ffd 100644 --- a/app/release/output.json +++ b/app/release/output.json @@ -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":{}}] \ No newline at end of file +[{"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":{}}] \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c97299c..8a5157c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -43,6 +43,17 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/RichardLuo/notificationpush/Application.java b/app/src/main/java/com/RichardLuo/notificationpush/Application.java index eb39e81..3451b5e 100644 --- a/app/src/main/java/com/RichardLuo/notificationpush/Application.java +++ b/app/src/main/java/com/RichardLuo/notificationpush/Application.java @@ -62,7 +62,7 @@ public void onNothingSelected(AdapterView parent) { } }; - packageView.add(new info(name, packageManager.getApplicationIcon(applicationInfo), onItemSelectedListener,preferences.getInt(applicationInfo.packageName, 0))); + packageView.add(new info(name, packageManager.getApplicationIcon(applicationInfo), onItemSelectedListener, preferences.getInt(applicationInfo.packageName, 0))); } handler.post(new Runnable() { @@ -132,5 +132,9 @@ class info { }.start(); } - + @Override + public boolean onSupportNavigateUp() { + finish(); + return true; + } } diff --git a/app/src/main/java/com/RichardLuo/notificationpush/FCMReceiver.java b/app/src/main/java/com/RichardLuo/notificationpush/FCMReceiver.java index 4b5497e..cc4ba94 100644 --- a/app/src/main/java/com/RichardLuo/notificationpush/FCMReceiver.java +++ b/app/src/main/java/com/RichardLuo/notificationpush/FCMReceiver.java @@ -46,6 +46,8 @@ public void onMessageReceived(RemoteMessage remoteMessage) { senderName = data.get("senderName"); String packageName = remoteMessage.getData().get("package"); PendingIntent intent; + if (getSharedPreferences("MainActivity", MODE_PRIVATE).getBoolean("hide", false) && ForegroundMonitor.packageName.equals(packageName)) + return; setChannel(packageName); @@ -56,6 +58,9 @@ public void onMessageReceived(RemoteMessage remoteMessage) { case "com.tencent.tim": case "com.tencent.mobileqq": case "com.jinhaihan.qqnotfandshare": + String className = ForegroundMonitor.packageName; + if (getSharedPreferences("MainActivity", MODE_PRIVATE).getBoolean("hide", false) && (className.contains("com.tencent.") && (className.contains("qq") || className.contains("tim")))) + return; String QQpackageName = getSharedPreferences("MainActivity", MODE_PRIVATE).getString("installedQQ", null); intent = getIntent(QQpackageName); if (senderName == null) diff --git a/app/src/main/java/com/RichardLuo/notificationpush/ForegroundMonitor.java b/app/src/main/java/com/RichardLuo/notificationpush/ForegroundMonitor.java new file mode 100644 index 0000000..9a908d0 --- /dev/null +++ b/app/src/main/java/com/RichardLuo/notificationpush/ForegroundMonitor.java @@ -0,0 +1,22 @@ +package com.RichardLuo.notificationpush; + +import android.accessibilityservice.AccessibilityService; +import android.view.accessibility.AccessibilityEvent; + +public class ForegroundMonitor extends AccessibilityService { + static String packageName = ""; + + @Override + public void onAccessibilityEvent(AccessibilityEvent event) { + if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { + if (event.getPackageName() == null) + return; + packageName = event.getPackageName().toString(); + } + } + + @Override + public void onInterrupt() { + + } +} diff --git a/app/src/main/java/com/RichardLuo/notificationpush/MainActivity.java b/app/src/main/java/com/RichardLuo/notificationpush/MainActivity.java index 06bec51..639102c 100644 --- a/app/src/main/java/com/RichardLuo/notificationpush/MainActivity.java +++ b/app/src/main/java/com/RichardLuo/notificationpush/MainActivity.java @@ -3,6 +3,7 @@ import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.ComponentName; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; @@ -19,7 +20,6 @@ import android.widget.Button; import android.widget.CompoundButton; import android.widget.EditText; -import android.widget.Spinner; import android.widget.Switch; import android.widget.TextView; import android.widget.Toast; @@ -31,12 +31,12 @@ import java.util.List; -//TODO 增加应用前台判断 -public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { +public class MainActivity extends AppCompatActivity implements Switch.OnCheckedChangeListener { static SharedPreferences preferences; - static Intent service; - boolean isEnabled; + boolean isNotiListenerEnabled; + boolean isAccessEnabled; Switch swh; + Switch hide; EditText input; TextView DeviceID; Button priority; @@ -53,12 +53,27 @@ protected void onCreate(Bundle savedInstanceState) { setTheme(preferences.getInt("style", R.style.base_AppTheme_teal)); setContentView(R.layout.activity_main); swh = findViewById(R.id.switch1); + swh.setOnCheckedChangeListener(this); - priority = findViewById(R.id.priority); - priority.setOnClickListener(new Button.OnClickListener() { + hide = findViewById(R.id.switch2); + hide.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener() { @Override - public void onClick(View v) { - startActivity(new Intent(getApplicationContext(), Application.class)); + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + if (isChecked) { + if (isAccessEnabled) { + preferences.edit().putBoolean("hide", true).apply(); + } else { + startActivity(intent); + preferences.edit().putBoolean("hide", false).apply(); + hide.setChecked(false); + } + } else { + if (isAccessEnabled) + startActivity(intent); + preferences.edit().putBoolean("hide", false).apply(); + } } }); @@ -77,6 +92,14 @@ public void onClick(View v) { } }); + priority = findViewById(R.id.priority); + priority.setOnClickListener(new Button.OnClickListener() { + @Override + public void onClick(View v) { + startActivity(new Intent(getApplicationContext(), Application.class)); + } + }); + colors = findViewById(R.id.colors); colors.setOnClickListener(new View.OnClickListener() { @Override @@ -146,7 +169,7 @@ public void onClick(DialogInterface dialog, int which) { normalDialog.show(); } }); - swh.setOnCheckedChangeListener(this); + FirebaseInstanceId.getInstance().getInstanceId() .addOnCompleteListener(new OnCompleteListener() { @Override @@ -158,7 +181,6 @@ public void onComplete(@NonNull Task task) { DeviceID.setText(task.getResult().getToken()); } }); - startService(new Intent(this, FCMReceiver.class)); input.setText(preferences.getString("ID", "")); List packageInfo = this.getPackageManager().getInstalledApplications(0); @@ -181,12 +203,19 @@ public void onComplete(@NonNull Task task) { protected void onStart() { super.onStart(); if (isNotificationListenersEnabled()) { - isEnabled = true; + isNotiListenerEnabled = true; swh.setChecked(true); } else { - isEnabled = false; + isNotiListenerEnabled = false; swh.setChecked(false); } + if (isAccessibilitySettingsOn(this)) { + isAccessEnabled = true; + hide.setChecked(true); + } else { + isAccessEnabled = false; + hide.setChecked(false); + } preferences.edit().putString("ID", input.getText().toString()).apply(); } @@ -196,10 +225,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (isChecked) { if (!input.getText().toString().trim().isEmpty()) { - if (isEnabled) { + if (isNotiListenerEnabled) { preferences.edit().putString("ID", input.getText().toString()).apply(); input.setEnabled(false); - startService(service = new Intent(this, GetNotification.class)); } else { startActivity(intent); input.setEnabled(true); @@ -212,10 +240,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { swh.setChecked(false); } } else { - if (isEnabled) + if (isNotiListenerEnabled) startActivity(intent); input.setEnabled(true); - stopService(service); } } @@ -235,4 +262,34 @@ public boolean isNotificationListenersEnabled() { } return false; } + + + private boolean isAccessibilitySettingsOn(Context mContext) { + int accessibilityEnabled; + final String service = getPackageName() + "/" + ForegroundMonitor.class.getCanonicalName(); + try { + accessibilityEnabled = Settings.Secure.getInt( + mContext.getApplicationContext().getContentResolver(), + android.provider.Settings.Secure.ACCESSIBILITY_ENABLED); + } catch (Settings.SettingNotFoundException e) { + return false; + } + TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':'); + + if (accessibilityEnabled == 1) { + String settingValue = Settings.Secure.getString( + mContext.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; + } + } + } + } + return false; + } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 5fbf7ee..583e830 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -24,7 +24,7 @@ android:layout_height="wrap_content" android:paddingTop="10dp" android:paddingBottom="10dp" - android:text="@string/start" + android:text="@string/hide" android:textSize="18sp" tools:checked="true" /> @@ -49,18 +49,18 @@ android:textIsSelectable="true" />