导航菜单

页面标题

页面副标题

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

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

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


package org.thoughtcrime.securesms.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import org.thoughtcrime.securesms.ApplicationContext;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.connect.ForegroundDetector;
import org.thoughtcrime.securesms.notifications.NotificationCenter;
import org.thoughtcrime.securesms.util.Util;

public final class FetchForegroundService extends Service {
    private static Intent service;
    private static final String TAG = "FcmReceiveService";
    private static final Object SERVICE_LOCK = new Object();
    private static final Object STOP_NOTIFIER = new Object();
    private static volatile boolean fetchingSynchronously = false;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public static void start(Context context) {
        ForegroundDetector foregroundDetector = ForegroundDetector.getInstance();
        if (foregroundDetector == null || !foregroundDetector.isForeground()) {
            GenericForegroundService.createFgNotificationChannel(context);
            try {
                synchronized (SERVICE_LOCK) {
                    if (service == null) {
                        Intent intent = new Intent(context, (Class<?>) FetchForegroundService.class);
                        service = intent;
                        ContextCompat.startForegroundService(context, intent);
                    }
                }
            } catch (Exception e) {
                Log.w(TAG, "Failed to start foreground service: " + e + ", fetching in background.");
                fetchingSynchronously = true;
                if (ApplicationContext.dcAccounts.backgroundFetch(10)) {
                    synchronized (STOP_NOTIFIER) {
                        while (fetchingSynchronously) {
                            try {
                                STOP_NOTIFIER.wait();
                            } catch (InterruptedException unused) {
                            }
                        }
                    }
                }
            }
        }
    }

    public static void stop(Context context) {
        if (fetchingSynchronously) {
            fetchingSynchronously = false;
            Object obj = STOP_NOTIFIER;
            synchronized (obj) {
                obj.notifyAll();
            }
        }
        synchronized (SERVICE_LOCK) {
            Intent intent = service;
            if (intent != null) {
                context.stopService(intent);
                service = null;
            }
        }
    }

    @Override
    public void onCreate() {
        Log.i(TAG, "Creating fetch service");
        super.onCreate();
        startForeground(4, new NotificationCompat.Builder(this, NotificationCenter.CH_GENERIC).setContentTitle(getString(R.string.connectivity_updating)).setSmallIcon(R.drawable.notification_permanent).build());
        Util.runOnAnyBackgroundThread(new Runnable() {
            @Override
            public final void run() {
                FetchForegroundService.this.m1633x72f6afc6();
            }
        });
    }

    void m1633x72f6afc6() {
        Log.i(TAG, "Starting fetch");
        if (ApplicationContext.dcAccounts.backgroundFetch(300)) {
            return;
        }
        stop(this);
    }

    @Override
    public void onDestroy() {
        stopForeground(true);
    }
}