zezung27 / api-mymeostore

1 stars 0 forks source link

MainAccessibilityService #2

Closed zezung27 closed 1 year ago

zezung27 commented 1 year ago

package com.matcos.dynamicislandlight.core.service;

import android.accessibilityservice.AccessibilityService; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.media.MediaMetadata; import android.media.session.MediaController; import android.media.session.MediaSessionManager; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Parcelable; import android.util.Log; import android.view.accessibility.AccessibilityEvent;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.airbnb.lottie.L; import com.matcos.dynamicislandlight.core.service.modal.NotificationModal; import com.matcos.dynamicislandlight.utils.Contanst; import com.matcos.dynamicislandlight.utils.Utils;

import java.util.ArrayList;

public class MainAccessibilityService extends AccessibilityService { private WindowPopup window; private Handler mediaHandler = new Handler(); private Runnable mediaUpdateRunnable; int priorityMode = 0; BroadcastReceiver notiReceiver = new BroadcastReceiver(){ public void onReceive(Context context, Intent intent) { if (intent != null) { if(intent.getAction().equals(Contanst.NOTI_INTENT_LOCAL_BROADCAST)) { convertNotiData(intent); } if(intent.getAction().equals(Contanst.MINI_TO_EXPAND_MEDIA_LOCAL_BROADCAST)) { setUpProgressMedia(window.getCurrentMediaNoti()); } if(intent.getAction().equals(Contanst.EXPAND_TO_MINI_MEDIA_LOCAL_BROADCAST)) { mediaHandler.removeCallbacksAndMessages(null); } if(intent.getAction().equals(Contanst.REMOVE_NOTIFICATIONS)) { handleRemoveNotiData(intent); } } } };

private void handleRemoveNotiData(Intent intent) {
    Bundle bundle = intent.getExtras();
    String packageName = bundle.getString("packageName");
    if(priorityMode == 5 || priorityMode == 4) {
        // remove call
        window.removeCallNoti();
    }
}

private void setUpProgressMedia(NotificationModal notificationModal) {
    // handle media update
    mediaUpdateRunnable = new Runnable() {
        @Override
        public void run() {
            notificationModal.setDuration(getMediaDuration());
            notificationModal.setPosition(getMediaPosition());
            if (notificationModal.getDuration() > 0L) {
                int n = (int)(100.0f * ((float)notificationModal.getPosition() / (float)notificationModal.getDuration()));
                notificationModal.setProgressMax(100);
                notificationModal.setProgress(n);
                notificationModal.setProgressIndeterminate(false);
                window.updateMediaProgress(notificationModal);
            }
            mediaHandler.postDelayed(this, 1000);
        }
    };
    mediaHandler.postDelayed(mediaUpdateRunnable, 1000);
}
private void convertNotiData(Intent intent) {
    Bundle bundle = intent.getExtras();
    boolean isGroupConversation = bundle.getBoolean("isGroupConversation");
    boolean isAppGroup = bundle.getBoolean("isAppGroup");
    boolean isGroup = bundle.getBoolean("isGroup");
    boolean isOngoing = bundle.getBoolean("isOngoing");
    String tag = bundle.getString("tag");
    int uId = bundle.getInt("uId");
    String group_key = bundle.getString("group_key");
    String key = bundle.getString("key");
    String id = bundle.getString("id");

    String packageName = bundle.getString("package");
    String ticker = bundle.getString("ticker");
    String appName = bundle.getString("appName");
    CharSequence title = bundle.getCharSequence("title");
    boolean isAdded = bundle.getBoolean("isAdded");
    long postTime = bundle.getLong("postTime");

    CharSequence text = bundle.getCharSequence("text");
    CharSequence bigText = bundle.getCharSequence("bigText");
    boolean isClearable = bundle.getBoolean("isClearable");
    int color = bundle.getInt("color");
    byte[] largeIcon = intent.getExtras().getByteArray("largeIcon");
    CharSequence substName = bundle.getCharSequence("substName");
    CharSequence subText = bundle.getCharSequence("subText");
    CharSequence titleBig = bundle.getCharSequence("titleBig");
    CharSequence summaryText = bundle.getCharSequence("summaryText");
    CharSequence infoText = bundle.getCharSequence("infoText");
    byte[] picture = intent.getExtras().getByteArray("picture");
    byte[] icon = intent.getExtras().getByteArray("icon");
    ArrayList listActions= bundle.getParcelableArrayList("actions");
    PendingIntent pendingIntent = bundle.getParcelable("pendingIntent");
    String template = bundle.getString("template");
    String category = bundle.getString("category");
    boolean showChronometer = bundle.getBoolean("showChronometer");
    int progressMax = bundle.getInt("progressMax");
    int progress = bundle.getInt("progress");
    boolean progressIndeterminate = bundle.getBoolean("progressIndeterminate");
    // create Notification Modal
    NotificationModal notificationModal = new NotificationModal(
            appName,
            isGroupConversation,
            isAppGroup,
            isGroup,
            isOngoing,
            tag,
            uId,
            group_key,
            key,
            id,
            packageName,
            ticker,
            title,
            isAdded,
            postTime,
            text,
            bigText,
            isClearable,
            color,
            substName,
            subText,
            bigText,
            summaryText,
            infoText,
            icon,
            largeIcon,
            picture,
            listActions,
            pendingIntent,
            template,
            progress,
            progressMax,
            progressIndeterminate,
            category,
            showChronometer
    );
    priorityMode = 1;
    // 1 mini, 2 noti has actions, 3 notimusic , 4 call
    if(isOngoing) {
        if(category.equalsIgnoreCase("call") ) {
            priorityMode = 4;
        } else if(showChronometer) { //
            priorityMode = 5;
        }
    }
zezung27 commented 1 year ago

if(listActions != null && listActions.size() > 0 && !template.equals("") && template.equals("MediaStyle")) { priorityMode = 3; } window.setCurrentProrityMode(priorityMode); if(priorityMode == 1 && !window.isExpandMedia() && priorityMode != 6) { // check if isexpandmedia -> not show window.updateList(notificationModal, priorityMode); window.handleIdleToMiniAnim(); } if(priorityMode == 3) { window.setCurrentMediaNoti(notificationModal); if(window.isExpandMedia()) { //update view window.addViewActionsToLayout(window.getLayoutListActionMedia(), notificationModal, true); } else { window.setUpMediaNoti(notificationModal); } } if(priorityMode == 4) { window.handleShowMiniCall(notificationModal); } if(priorityMode == 5) { window.showChronometerCall(notificationModal); } } @Override protected void onServiceConnected() { window = WindowPopup.getInstance(this); window.open(); }

@Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
    Log.d("DlAccessibilityService", "onAccessibilityEvent: " + accessibilityEvent);
}

@Override
public void onInterrupt() {
}

@Override
public void onCreate() {
    super.onCreate();
    LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
    lbm.registerReceiver(notiReceiver, new IntentFilter(Contanst.NOTI_INTENT_LOCAL_BROADCAST));
    lbm.registerReceiver(notiReceiver, new IntentFilter(Contanst.REMOVE_NOTIFICATIONS));
    lbm.registerReceiver(notiReceiver, new IntentFilter(Contanst.MINI_TO_EXPAND_MEDIA_LOCAL_BROADCAST));
    lbm.registerReceiver(notiReceiver, new IntentFilter(Contanst.EXPAND_TO_MINI_MEDIA_LOCAL_BROADCAST));
}

@Override
public void onDestroy() {
    super.onDestroy();
    stopSelf();
    if (this.notiReceiver != null) {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(this.notiReceiver);
    }
}

// media
public long getMediaDuration() {
    try {
        long l = ((MediaController)((MediaSessionManager)this.getSystemService(Context.MEDIA_SESSION_SERVICE)).getActiveSessions(new ComponentName(this.getApplicationContext(), NotificationService.class)).get(0)).getMetadata().getLong(MediaMetadata.METADATA_KEY_DURATION);
        return l;
    }
    catch (Exception exception) {
        return 1L;
    }
}

public long getMediaPosition() {
    try {
        long l = ((MediaController)((MediaSessionManager)this.getSystemService(Context.MEDIA_SESSION_SERVICE)).getActiveSessions(new ComponentName(this.getApplicationContext(), NotificationService.class)).get(0)).getPlaybackState().getPosition();
        return l;
    }
    catch (Exception exception) {
        return 0L;
    }
}

}