导航菜单

页面标题

页面副标题

Delta Chat v1.58.3 - GenericForegroundService.java 源代码

正在查看: Delta Chat v1.58.3 应用的 GenericForegroundService.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package org.thoughtcrime.securesms.service;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.thoughtcrime.securesms.DummyActivity;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.notifications.NotificationCenter;
import org.thoughtcrime.securesms.util.IntentUtils;
import org.thoughtcrime.securesms.util.ViewUtil$$ExternalSyntheticApiModelOutline0;

public final class GenericForegroundService extends Service {
    private static final String ACTION_START = "start";
    private static final String ACTION_STOP = "stop";
    private static final String EXTRA_CHANNEL_ID = "extra_channel_id";
    private static final String EXTRA_CONTENT_TEXT = "extra_content_text";
    private static final String EXTRA_ICON_RES = "extra_icon_res";
    private static final String EXTRA_ID = "extra_id";
    private static final String EXTRA_PROGRESS = "extra_progress";
    private static final String EXTRA_PROGRESS_INDETERMINATE = "extra_progress_indeterminate";
    private static final String EXTRA_PROGRESS_MAX = "extra_progress_max";
    private static final String EXTRA_TITLE = "extra_title";
    private static final String TAG = "GenericForegroundService";
    private Entry lastPosted;
    private static final AtomicInteger NEXT_ID = new AtomicInteger();
    private static final AtomicBoolean CHANNEL_CREATED = new AtomicBoolean(false);
    private static int startedCounter = 0;
    private static final Entry DEFAULTS = new Entry("", "", NotificationCenter.CH_GENERIC, R.drawable.icon_notification, -1, 0, 0, false);
    private final IBinder binder = new LocalBinder();
    private final LinkedHashMap<Integer, Entry> allActiveMessages = new LinkedHashMap<>();

    @Override
    public int onStartCommand(Intent intent, int i, int i2) {
        if (intent == null) {
            throw new IllegalStateException("Intent needs to be non-null.");
        }
        synchronized (GenericForegroundService.class) {
            String action = intent.getAction();
            if (ACTION_START.equals(action)) {
                handleStart(intent);
            } else {
                if (!ACTION_STOP.equals(action)) {
                    throw new IllegalStateException(String.format("Action needs to be %s or %s.", ACTION_START, ACTION_STOP));
                }
                handleStop(intent);
            }
            updateNotification();
        }
        return 2;
    }

    private synchronized void updateNotification() {
        Iterator<Entry> it = this.allActiveMessages.values().iterator();
        if (it.hasNext()) {
            postObligatoryForegroundNotification(it.next());
        } else {
            Log.i(TAG, "Last request. Ending foreground service.");
            Entry entry = this.lastPosted;
            if (entry == null) {
                entry = DEFAULTS;
            }
            postObligatoryForegroundNotification(entry);
            stopForeground(true);
            stopSelf();
        }
    }

    private synchronized void handleStart(Intent intent) {
        Entry fromIntent = Entry.fromIntent(intent);
        Log.i(TAG, String.format(Locale.ENGLISH, "handleStart() %s", fromIntent));
        this.allActiveMessages.put(Integer.valueOf(fromIntent.id), fromIntent);
    }

    private synchronized void handleStop(Intent intent) {
        String str = TAG;
        Log.i(str, "handleStop()");
        if (this.allActiveMessages.remove(Integer.valueOf(intent.getIntExtra(EXTRA_ID, -1))) == null) {
            Log.w(str, "Could not find entry to remove");
        }
    }

    private void postObligatoryForegroundNotification(Entry entry) {
        this.lastPosted = entry;
        startForeground(3, new NotificationCompat.Builder(this, entry.channelId).setSmallIcon(entry.iconRes).setContentTitle(entry.title).setTicker(entry.contentText).setContentText(entry.contentText).setProgress(entry.progressMax, entry.progress, entry.indeterminate).setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, (Class<?>) DummyActivity.class), IntentUtils.FLAG_MUTABLE())).build());
    }

    @Override
    public IBinder onBind(Intent intent) {
        return this.binder;
    }

    public static NotificationController startForegroundTask(Context context, String str) {
        startedCounter++;
        int andIncrement = NEXT_ID.getAndIncrement();
        createFgNotificationChannel(context);
        Intent intent = new Intent(context, (Class<?>) GenericForegroundService.class);
        intent.setAction(ACTION_START);
        intent.putExtra("extra_title", str);
        intent.putExtra(EXTRA_CHANNEL_ID, NotificationCenter.CH_GENERIC);
        intent.putExtra(EXTRA_ICON_RES, R.drawable.notification_permanent);
        intent.putExtra(EXTRA_ID, andIncrement);
        ContextCompat.startForegroundService(context, intent);
        return new NotificationController(context, andIncrement);
    }

    public static void stopForegroundTask(Context context, int i) {
        Intent intent = new Intent(context, (Class<?>) GenericForegroundService.class);
        intent.setAction(ACTION_STOP);
        intent.putExtra(EXTRA_ID, i);
        ContextCompat.startForegroundService(context, intent);
        startedCounter = Math.max(startedCounter - 1, 0);
    }

    public static boolean isForegroundTaskStarted() {
        return startedCounter > 0;
    }

    synchronized void replaceProgress(int i, int i2, int i3, boolean z, String str) {
        Entry entry = this.allActiveMessages.get(Integer.valueOf(i));
        if (entry == null) {
            Log.w(TAG, "Failed to replace notification, it was not found");
            return;
        }
        if (str == null) {
            str = entry.contentText;
        }
        Entry entry2 = new Entry(entry.title, str, entry.channelId, entry.iconRes, entry.id, i2, i3, z);
        if (entry.equals(entry2)) {
            Log.d(TAG, String.format("handleReplace() skip, no change %s", entry2));
            return;
        }
        Log.i(TAG, String.format("handleReplace() %s", entry2));
        this.allActiveMessages.put(Integer.valueOf(entry2.id), entry2);
        updateNotification();
    }

    public static void createFgNotificationChannel(Context context) {
        Object systemService;
        AtomicBoolean atomicBoolean = CHANNEL_CREATED;
        if (atomicBoolean.get() || Build.VERSION.SDK_INT < 26) {
            return;
        }
        atomicBoolean.set(true);
        NotificationChannel m = ViewUtil$$ExternalSyntheticApiModelOutline0.m(NotificationCenter.CH_GENERIC, "Generic Background Service", 1);
        m.setDescription("Ensure app will not be killed while long ongoing background tasks are running.");
        systemService = context.getSystemService((Class<Object>) NotificationManager.class);
        ((NotificationManager) systemService).createNotificationChannel(m);
    }

    private static class Entry {
        final String channelId;
        final String contentText;
        final int iconRes;
        final int id;
        final boolean indeterminate;
        final int progress;
        final int progressMax;
        final String title;

        private Entry(String str, String str2, String str3, int i, int i2, int i3, int i4, boolean z) {
            this.title = str;
            this.contentText = str2;
            this.channelId = str3;
            this.iconRes = i;
            this.id = i2;
            this.progress = i4;
            this.progressMax = i3;
            this.indeterminate = z;
        }

        public static Entry fromIntent(Intent intent) {
            int intExtra = intent.getIntExtra(GenericForegroundService.EXTRA_ID, GenericForegroundService.DEFAULTS.id);
            String stringExtra = intent.getStringExtra("extra_title");
            if (stringExtra == null) {
                stringExtra = GenericForegroundService.DEFAULTS.title;
            }
            String str = stringExtra;
            String stringExtra2 = intent.getStringExtra(GenericForegroundService.EXTRA_CONTENT_TEXT);
            if (stringExtra2 == null) {
                stringExtra2 = GenericForegroundService.DEFAULTS.contentText;
            }
            String str2 = stringExtra2;
            String stringExtra3 = intent.getStringExtra(GenericForegroundService.EXTRA_CHANNEL_ID);
            if (stringExtra3 == null) {
                stringExtra3 = GenericForegroundService.DEFAULTS.channelId;
            }
            return new Entry(str, str2, stringExtra3, intent.getIntExtra(GenericForegroundService.EXTRA_ICON_RES, GenericForegroundService.DEFAULTS.iconRes), intExtra, intent.getIntExtra(GenericForegroundService.EXTRA_PROGRESS_MAX, GenericForegroundService.DEFAULTS.progressMax), intent.getIntExtra(GenericForegroundService.EXTRA_PROGRESS, GenericForegroundService.DEFAULTS.progress), intent.getBooleanExtra(GenericForegroundService.EXTRA_PROGRESS_INDETERMINATE, GenericForegroundService.DEFAULTS.indeterminate));
        }

        public String toString() {
            return String.format(Locale.ENGLISH, "ChannelId: %s  Id: %d Progress: %d/%d %s", this.channelId, Integer.valueOf(this.id), Integer.valueOf(this.progress), Integer.valueOf(this.progressMax), this.indeterminate ? "indeterminate" : "determinate");
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null || getClass() != obj.getClass()) {
                return false;
            }
            Entry entry = (Entry) obj;
            return this.id == entry.id && this.iconRes == entry.iconRes && this.progress == entry.progress && this.progressMax == entry.progressMax && this.indeterminate == entry.indeterminate && this.title.equals(entry.title) && this.contentText.equals(entry.contentText) && this.channelId.equals(entry.channelId);
        }

        public int hashCode() {
            return (((((((((((this.title.hashCode() * 31) + this.channelId.hashCode()) * 31) + this.id) * 31) + this.iconRes) * 31) + this.progress) * 31) + this.progressMax) * 31) + (this.indeterminate ? 1 : 0);
        }
    }

    class LocalBinder extends Binder {
        LocalBinder() {
        }

        GenericForegroundService getService() {
            return GenericForegroundService.this;
        }
    }
}