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

Commit

Permalink
android 12 动态配色支持
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardLuo0 committed May 7, 2022
1 parent 510e0e5 commit 972d27a
Show file tree
Hide file tree
Showing 22 changed files with 426 additions and 310 deletions.
6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "com.RichardLuo.notificationpush"
minSdkVersion 21
targetSdkVersion 31
versionCode 29
versionName "1.1.5"
versionCode 30
versionName "1.1.6"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildConfigField "String", "FCM_AUTHORIZATION", localProperties['FCM_AUTHORIZATION']
buildConfigField "String", "FCM_SENDER", localProperties['FCM_SENDER']
Expand Down
Binary file modified app/release/app-release.apk
Binary file not shown.
20 changes: 20 additions & 0 deletions app/release/output-metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": 3,
"artifactType": {
"type": "APK",
"kind": "Directory"
},
"applicationId": "com.RichardLuo.notificationpush",
"variantName": "release",
"elements": [
{
"type": "SINGLE",
"filters": [],
"attributes": [],
"versionCode": 30,
"versionName": "1.1.6",
"outputFile": "app-release.apk"
}
],
"elementType": "File"
}
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/base.DayNight.AppTheme_teal">

Expand Down Expand Up @@ -85,7 +85,6 @@
android:name="android.accessibilityservice"
android:resource="@xml/accessibility" />
</service>

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Info {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(getSharedPreferences("MainActivity", MODE_PRIVATE).getInt("style", R.style.base_DayNight_AppTheme_teal));
setTheme(ThemeProvider.getCurrentStyle(this));
setContentView(R.layout.activity_application);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
listView = findViewById(R.id.listview);
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/RichardLuo/notificationpush/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
public class Const {

static final String TAG = "NotificationPush";

static final String[] QQ_NAMES = new String[]{"com.tencent.mobileqq", "com.tencent.tim", "com.tencent.mobileqqi", "com.tencent.qqlite", "com.tencent.minihd.qq"};
}
210 changes: 104 additions & 106 deletions app/src/main/java/com/RichardLuo/notificationpush/FCMReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
Expand All @@ -19,14 +21,14 @@
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.os.Build;
import android.os.PatternMatcher;
import android.service.notification.StatusBarNotification;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.app.Person;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.IconCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
Expand All @@ -38,24 +40,39 @@
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class FCMReceiver extends FirebaseMessagingService {
static Map<String, PendingIntent> Package_Intent = new HashMap<>();
static Map<String, PendingIntent> package_Intent = new HashMap<>();

int color = 0;
Boolean ringForEach;
NotificationManagerCompat notificationManagerCompat;

private final BroadcastReceiver br = new QQInstallReceiver();

@Override
public void onCreate() {
notificationManagerCompat = NotificationManagerCompat.from(this);
super.onCreate();

QQInstallReceiver.findQQPackage(this);
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addDataScheme("package");
filter.addDataSchemeSpecificPart("com.tencent", PatternMatcher.PATTERN_LITERAL);
registerReceiver(br, filter);
}

@Override
public void onDestroy() {
super.onDestroy();

unregisterReceiver(br);
}

@Override
Expand All @@ -79,7 +96,7 @@ public void onNewToken(@NonNull String s) {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
ringForEach = getDefaultSharedPreferences(this).getBoolean("ringForEach", false);
color = ContextCompat.getColor(this, getSharedPreferences("MainActivity", MODE_PRIVATE).getInt("color", R.color.teal));
color = ThemeProvider.getCurrentColor(this);
Map<String, String> data = remoteMessage.getData();
String title = data.get("title");
String body = data.get("body");
Expand All @@ -96,88 +113,91 @@ public void onMessageReceived(RemoteMessage remoteMessage) {

setChannel(AppName);

switch (Objects.requireNonNull(packageName)) {
case "com.tencent.minihd.qq":
case "com.tencent.mobileqqi":
case "com.tencent.qqlite":
case "com.tencent.tim":
case "com.tencent.mobileqq":
case "com.jinhaihan.qqnotfandshare":
String className = ForegroundMonitor.packageName;
if (hide && (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)
break;
if (senderName.equals(""))
senderName = " ";
IconCompat icon = null;
Bitmap largeIcon = null;
if (getDefaultSharedPreferences(this).getBoolean("sendQQ", false) && this.getDatabasePath("friends.db").exists()) {
boolean isfriend = senderName.equals(title);
String encodeSendername = md5(senderName);
File file = new File(this.getCacheDir().getPath() + "/" + encodeSendername);
out:
try {
if (!file.exists()) {
SQLiteDatabase db;
Cursor cursor;
if (isfriend) {
db = SQLiteDatabase.openOrCreateDatabase(this.getDatabasePath("friends.db"), null);
if (getSharedPreferences("groups", MODE_PRIVATE).contains("sync_friends"))
cursor = db.query("friends", new String[]{"uin"}, "name ='" + senderName + "'", null, null, null, null);
else
break out;
} else if (getSharedPreferences("groups", MODE_PRIVATE).contains(title)) {
db = SQLiteDatabase.openOrCreateDatabase(this.getDatabasePath("friends.db"), null);
Cursor cursorTemp = db.query("'" + title + "'", new String[]{"uin"}, "name ='" + senderName + "'", null, null, null, null);
if (cursorTemp.getCount() == 0) {
cursorTemp.close();
cursor = db.query("friends", new String[]{"uin"}, "name ='" + senderName + "'", null, null, null, null);
} else cursor = cursorTemp;
} else
boolean isQQ = false;
for (String qqName : Const.QQ_NAMES) {
if (qqName.equals(packageName)) {
isQQ = true;
break;
}
}

if (isQQ && senderName != null) {
String className = ForegroundMonitor.packageName;
String qqPackageName = getSharedPreferences("MainActivity", MODE_PRIVATE).getString("installedQQ", null);
intent = getIntent(qqPackageName);
if (hide && qqPackageName != null && className.contains(qqPackageName))
return;
if (senderName.equals(""))
senderName = " ";
Bitmap icon = null;
Bitmap largeIcon = null;
if (getDefaultSharedPreferences(this).getBoolean("sendQQ", false) && this.getDatabasePath("friends.db").exists()) {
boolean isfriend = senderName.equals(title);
String encodeSendername = "member_" + Utils.md5(senderName);
File file = new File(this.getCacheDir().getPath() + "/" + encodeSendername);
out:
try {
if (!file.exists()) {
SQLiteDatabase db;
Cursor cursor;
if (isfriend) {
db = SQLiteDatabase.openOrCreateDatabase(this.getDatabasePath("friends.db"), null);
if (getSharedPreferences("groups", MODE_PRIVATE).contains("sync_friends"))
cursor = db.query("friends", new String[]{"uin"}, "name ='" + senderName + "'", null, null, null, null);
else
break out;
if (cursor.getCount() != 0) {
if (cursor.moveToFirst()) {
String QQnumber = cursor.getString(0);
cursor.close();
db.close();
downloadIcon("https://q4.qlogo.cn/g?b=qq&s=140&nk=" + QQnumber, encodeSendername);
}
} else {
} else if (getSharedPreferences("groups", MODE_PRIVATE).contains(title)) {
db = SQLiteDatabase.openOrCreateDatabase(this.getDatabasePath("friends.db"), null);
Cursor cursorTemp = db.query("'" + title + "'", new String[]{"uin"}, "name ='" + senderName + "'", null, null, null, null);
if (cursorTemp.getCount() == 0) {
cursorTemp.close();
cursor = db.query("friends", new String[]{"uin"}, "name ='" + senderName + "'", null, null, null, null);
} else cursor = cursorTemp;
} else
break out;
if (cursor.getCount() != 0) {
if (cursor.moveToFirst()) {
String QQnumber = cursor.getString(0);
cursor.close();
db.close();
break out;
downloadIcon("https://q4.qlogo.cn/g?b=qq&s=140&nk=" + QQnumber, encodeSendername);
}
} else {
cursor.close();
db.close();
break out;
}
icon = IconCompat.createWithBitmap(BitmapFactory.decodeFile(this.getCacheDir().getPath() + "/" + encodeSendername));
} catch (IOException e) {
Log.e("", e.getMessage(), e);
}
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.P)
if (isfriend) {
if (icon != null)
largeIcon = BitmapFactory.decodeFile(this.getCacheDir().getPath() + "/" + encodeSendername);
} else if (new File(this.getCacheDir().getPath() + "/" + title).exists())
largeIcon = BitmapFactory.decodeFile(this.getCacheDir().getPath() + "/" + title);
else {
String groupNumber = getSharedPreferences("groupsNumber", MODE_PRIVATE).getString(title, null);
if (groupNumber != null) {
try {
largeIcon = downloadIcon("https://p.qlogo.cn/gh/" + groupNumber + "/" + groupNumber + "/100", title);
} catch (IOException e) {
Log.e(Const.TAG, e.getMessage(), e);
}
icon = BitmapFactory.decodeFile(this.getCacheDir().getPath() + "/" + encodeSendername);
} catch (IOException e) {
Log.e("", e.getMessage(), e);
}
// Large icon
if (isfriend) {
if (icon != null)
largeIcon = icon;
} else if (title != null) {
String encodeTitle = "group_" + Utils.md5(title);
if (new File(this.getCacheDir().getPath() + "/" + encodeTitle).exists())
largeIcon = BitmapFactory.decodeFile(this.getCacheDir().getPath() + "/" + encodeTitle);
else {
String groupNumber = getSharedPreferences("groupsNumber", MODE_PRIVATE).getString(title, null);
if (groupNumber != null) {
try {
largeIcon = downloadIcon("https://p.qlogo.cn/gh/" + groupNumber + "/" + groupNumber + "/100", encodeTitle);
} catch (IOException e) {
Log.e(Const.TAG, e.getMessage(), e);
}
}
}
}
setSummary(packageName, AppName, intent);
MessagingStyle(packageName, AppName, title, senderName, body, intent, id, icon, largeIcon);
return;
default:
intent = getIntent(packageName);
setSummary(packageName, AppName, intent);
}
setSummary(packageName, AppName, intent);
MessagingStyle(packageName, AppName, title, senderName, body, intent, id, icon == null ? null : IconCompat.createWithBitmap(icon), largeIcon);
return;
} else {
intent = getIntent(packageName);
setSummary(packageName, AppName, intent);
}

Notification notification = new NotificationCompat.Builder(this, AppName == null ? "" : AppName)
Expand All @@ -195,28 +215,6 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
notificationManagerCompat.notify(packageName, id, notification);
}

public static String md5(final String s) {
final String MD5 = "MD5";
try {
MessageDigest digest = java.security.MessageDigest
.getInstance(MD5);
digest.update(s.getBytes());
byte[] messageDigest = digest.digest();

StringBuilder hexString = new StringBuilder();
for (byte aMessageDigest : messageDigest) {
StringBuilder h = new StringBuilder(Integer.toHexString(0xFF & aMessageDigest));
while (h.length() < 2)
h.insert(0, "0");
hexString.append(h);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
Log.e(Const.TAG, e.getMessage(), e);
}
return "unknown";
}

@SuppressWarnings("SuspiciousNameCombination")
private Bitmap downloadIcon(String url, String name) throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
Expand Down Expand Up @@ -258,11 +256,11 @@ public void setChannel(String AppName) {

private PendingIntent getIntent(String packageName) {
PendingIntent intent = null;
if (Package_Intent.containsKey(packageName)) {
intent = Package_Intent.get(packageName);
if (package_Intent.containsKey(packageName)) {
intent = package_Intent.get(packageName);
return intent;
}
if (packageName != null && !packageName.contains("android"))
if (packageName != null)
try {
Intent launchIntent = getPackageManager().getLaunchIntentForPackage(packageName);
if (launchIntent == null)
Expand All @@ -274,10 +272,10 @@ private PendingIntent getIntent(String packageName) {
flags = FLAG_UPDATE_CURRENT;
intent = PendingIntent.getActivity(this, 200, launchIntent, flags);
} catch (Exception e) {
Package_Intent.put(packageName, null);
package_Intent.put(packageName, null);
return null;
}
Package_Intent.put(packageName, intent);
package_Intent.put(packageName, intent);
return intent;
}

Expand Down Expand Up @@ -337,7 +335,7 @@ private void MessagingStyle(String packageName, String AppName, String title, St
.setColor(color)
.setContentTitle(packageName)
.setStyle(style);
if (largeIcon != null && android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.P)
if (largeIcon != null)
notification.setLargeIcon(largeIcon);
notification.setGroup(packageName)
.setContentIntent(intent)
Expand Down
Loading

0 comments on commit 972d27a

Please sign in to comment.