zezung27 / api-mymeostore

1 stars 0 forks source link

NotificationService #4

Closed zezung27 closed 1 year ago

zezung27 commented 1 year ago

package com.matcos.dynamicislandlight.core.service;

import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.RemoteInput; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Bundle; import android.os.Parcelable; import android.service.notification.NotificationListenerService; import android.service.notification.StatusBarNotification; import android.text.TextUtils; import android.util.Log;

import androidx.core.content.ContextCompat; import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.matcos.dynamicislandlight.R; import com.matcos.dynamicislandlight.core.service.modal.ActionParsable; import com.matcos.dynamicislandlight.noti.PackageModalPref; import com.matcos.dynamicislandlight.utils.Contanst; import com.matcos.dynamicislandlight.utils.Utils;

import java.util.ArrayList; import java.util.stream.Collectors;

public class NotificationService extends NotificationListenerService { private String TAG = this .getClass().getSimpleName() ; Context context ; @Override public void onCreate () { super.onCreate() ; context = getApplicationContext() ; } private void initNoti(StatusBarNotification sbn) { // NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); // manager.cancel(sbn.getId()); // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // manager.deleteNotificationChannel(String.valueOf(sbn.getId())); // } Intent intent = sendNotification(sbn, true); LocalBroadcastManager.getInstance((Context)this.context).sendBroadcast(intent); } @Override public void onNotificationPosted (StatusBarNotification sbn) { ArrayList mList = Utils.getArrayList(Contanst.NAME_PACKAGE, context) ; if(mList != null && mList.size() > 0) { String namePackage = sbn.getPackageName(); ArrayList filterPackage= (ArrayList) mList.stream().filter(data -> (data.getPackageName().contains(namePackage) && data.isCheck())).collect(Collectors.toList()); if(filterPackage.size() > 0) { initNoti(sbn); } } else { initNoti(sbn); } }

@Override
public void onNotificationRemoved (StatusBarNotification sbn) {
    Log.i( TAG , "********** onNotificationRemoved" );
    Log.i( TAG , "ID :" + sbn.getId() + " \t " + sbn.getNotification(). tickerText + " \t " + sbn.getPackageName()) ;
    Intent intent = new Intent(Contanst.REMOVE_NOTIFICATIONS);
    intent.putExtra("packageName",sbn.getPackageName());
    LocalBroadcastManager.getInstance((Context)this.context).sendBroadcast(intent);
}

public static ArrayList<ActionParsable> getParsableActions(Notification.Action[] arraction) {
    ArrayList arrayList = new ArrayList();
    int n = arraction.length;
    boolean bl = false;
    for (int i = 0; i < n; ++i) {
        Notification.Action action = arraction[i];
        int n2 = Build.VERSION.SDK_INT;
        boolean bl2 = n2 >= 24 ? action.getAllowGeneratedReplies() : action.getExtras().getBoolean("android.support.allowGeneratedReplies");
        boolean bl3 = bl2;
        int n3 = n2 >= 28 ? action.getSemanticAction() : action.getExtras().getInt("android.support.action.semanticAction", 0);
        int n4 = n3;
        int n5 = action.icon;
        CharSequence charSequence = action.title;
        PendingIntent pendingIntent = action.actionIntent;
        Bundle bundle = action.getExtras();
        RemoteInput[] arrremoteInput = action.getRemoteInputs();
        if (Build.VERSION.SDK_INT >= 29) {
            bl = action.isContextual();
            ActionParsable actionParsable = new ActionParsable(charSequence, pendingIntent, bundle, arrremoteInput, bl3, n4, bl, n5);
            arrayList.add((Object)actionParsable);
            continue;
        }
        ActionParsable actionParsable = new ActionParsable(charSequence, pendingIntent, bundle, arrremoteInput, bl3, n4, bl, n5);
        arrayList.add((Object)actionParsable);
    }
    return arrayList;
}

private Intent sendNotification(StatusBarNotification statusBarNotification, boolean bl) {
    Bitmap bitmap5 = null, bitmap3 = null , bitmap = null;
    boolean isGroupConversation;
    String idNoti = String.valueOf((int)statusBarNotification.getId());
    String ticket = statusBarNotification.getNotification().tickerText != null ? statusBarNotification.getNotification().tickerText.toString() : null;
    String appName = "";
    CharSequence title = "";
    CharSequence textNoti = "";
    CharSequence bigTextNoti = "";
    CharSequence subName = "";
    CharSequence subText = "";
    CharSequence summaryText = "";
    CharSequence textBig = "";
    CharSequence infoText = "";
    String template = "";
    int progressMax = 0;
    int progress = 0;
    boolean showChronometer = false;
    String category =  statusBarNotification.getNotification().category != null ? statusBarNotification.getNotification().category : "";
    boolean progressIndeterminate = false;
    try {
        appName = (String)this.getPackageManager().getApplicationLabel(this.getPackageManager().getApplicationInfo(statusBarNotification.getPackageName(), PackageManager.GET_META_DATA));
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

    try {
        Drawable drawable2 = ContextCompat.getDrawable((Context)this.createPackageContext(statusBarNotification.getPackageName(), 0), (int)statusBarNotification.getNotification().icon);
        bitmap5 = Utils.drawableToBmp(null, drawable2, 20);
        bitmap3 = Build.VERSION.SDK_INT >= 23 && statusBarNotification.getNotification().getLargeIcon() != null ? Utils.drawableToBmp(this.context, statusBarNotification.getNotification().getLargeIcon().loadDrawable(this.context), 50) : null;

    } catch (Exception e) {
        e.printStackTrace();
    }

    if (statusBarNotification.getNotification().extras != null) {
        Bundle bundle = statusBarNotification.getNotification().extras;
        if (bundle.containsKey("android.template") && bundle.get("android.template") != null) {
            template = statusBarNotification.getNotification().extras.get("android.template").toString();
            if (!TextUtils.isEmpty((CharSequence)template)) {
                template = template.substring(1 + template.indexOf("$"));
            }
        }
        title = bundle.containsKey("android.title") && bundle.getCharSequence("android.title") != null ? bundle.getCharSequence("android.title", (CharSequence)"") : "";
        if (Build.VERSION.SDK_INT >= 28 && bundle.containsKey("android.isGroupConversation")) {
            isGroupConversation = bundle.getBoolean("android.isGroupConversation", false);
        } else {
            isGroupConversation = false;
        }
zezung27 commented 1 year ago

textNoti = bundle.containsKey("android.text") && bundle.getCharSequence("android.text") != null ? bundle.getCharSequence("android.text", (CharSequence)"") : ""; bigTextNoti = bundle.containsKey("android.bigText") && bundle.getCharSequence("android.bigText") != null ? bundle.getCharSequence("android.bigText", (CharSequence)"") : ""; subName = bundle.containsKey("android.substName") && bundle.getString("android.substName") != null ? bundle.getString("android.substName", "") : ""; subText = bundle.containsKey("android.subText") && bundle.getCharSequence("android.subText") != null ? bundle.getCharSequence("android.subText", (CharSequence)"") : ""; summaryText = bundle.containsKey("android.summaryText") && bundle.getCharSequence("android.summaryText") != null ? bundle.getCharSequence("android.summaryText", (CharSequence)"") : ""; textBig = bundle.containsKey("android.title.big") && bundle.getCharSequence("android.title.big") != null ? bundle.getCharSequence("android.title.big", (CharSequence)"") : ""; infoText = bundle.containsKey("android.infoText") && bundle.getString("android.infoText") != null ? bundle.getString("android.infoText", "") : ""; progressMax = bundle.containsKey("android.progressMax") ? bundle.getInt("android.progressMax", 0) : 0; progress = bundle.containsKey("android.progress") ? bundle.getInt("android.progress", 0) : 0; progressIndeterminate = bundle.containsKey("android.progressIndeterminate") ? bundle.getBoolean("android.progressIndeterminate", false) : false; showChronometer = bundle.containsKey("android.showChronometer") ? bundle.getBoolean("android.showChronometer", false) : false; } else { isGroupConversation = false; title=""; }

    StringBuilder stringBuilder = new StringBuilder();
    Intent intent = new Intent(Contanst.NOTI_INTENT_LOCAL_BROADCAST);
    intent.putExtra("isGroupConversation", isGroupConversation);
    if (Build.VERSION.SDK_INT >= 30) {
        intent.putExtra("isAppGroup", statusBarNotification.isAppGroup());
    }
    if (Build.VERSION.SDK_INT >= 24) {
        intent.putExtra("isGroup", statusBarNotification.isGroup());
    }
    intent.putExtra("isOngoing", statusBarNotification.isOngoing());
    intent.putExtra("tag", statusBarNotification.getTag());
    if (Build.VERSION.SDK_INT >= 29) {
        intent.putExtra("uId", statusBarNotification.getUid());
    }
    intent.putExtra("group_key", statusBarNotification.getGroupKey());
    intent.putExtra("key", statusBarNotification.getKey());
    intent.putExtra("id", idNoti);
    intent.putExtra("package", statusBarNotification.getPackageName());
    intent.putExtra("ticker", ticket);
    intent.putExtra("appName", appName);
    intent.putExtra("title", (CharSequence)title);
    intent.putExtra("isAdded", bl);
    intent.putExtra("postTime", statusBarNotification.getNotification().when);
    intent.putExtra("text", textNoti);
    intent.putExtra("bigText", bigTextNoti);
    intent.putExtra("isClearable", statusBarNotification.isClearable());
    intent.putExtra("color", statusBarNotification.getNotification().color);
    intent.putExtra("largeIcon", Utils.getByteArrayFromBitmap(bitmap3));
    intent.putExtra("substName", subName);
    intent.putExtra("subText", subText);
    intent.putExtra("titleBig", textBig);
    intent.putExtra("summaryText", summaryText);
    intent.putExtra("info_text", infoText);
    intent.putExtra("template", template);
    intent.putExtra("progressMax", progressMax);
    intent.putExtra("progress", progress);
    intent.putExtra("progressIndeterminate", progressIndeterminate);
    intent.putExtra("category", category);
    intent.putExtra("showChronometer", showChronometer);

    if (statusBarNotification.getNotification().extras.get("android.picture") != null && (bitmap = (Bitmap)statusBarNotification.getNotification().extras.getParcelable("android.picture")) != null) {
        intent.putExtra("picture", Utils.getByteArrayFromBitmap2(bitmap));
    }
    if (bitmap5 == null) {
        intent.putExtra("icon", Utils.getByteArrayFromBitmap(Utils.drawableToBmp(null, ContextCompat.getDrawable((Context)this.context, (int) R.drawable.ic_launcher_foreground), 20)));
    } else {
        intent.putExtra("icon", Utils.getByteArrayFromBitmap(bitmap5));
    }

    intent.putExtra("pendingIntent", (Parcelable)statusBarNotification.getNotification().contentIntent);
    if (statusBarNotification.getNotification().actions != null && statusBarNotification.getNotification().actions.length > 0) {
        intent.putExtra("actions", NotificationService.getParsableActions(statusBarNotification.getNotification().actions));
    }

    return intent;
}

}