导航菜单

页面标题

页面副标题

Perplexity v2.48.1 - LifecycleService.java 源代码

正在查看: Perplexity v2.48.1 应用的 LifecycleService.java JAVA 源代码文件

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


package com.mapbox.common;

import android.app.Activity;
import android.app.Application;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import com.mapbox.common.LifecycleService;
import java.lang.ref.WeakReference;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import kotlin.Metadata;
import kotlin.NoWhenBranchMatchedException;
import kotlin.jvm.internal.Intrinsics;

@Metadata(d1 = {"\u0000S\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0002\b\u000e*\u0001\u0006\u0018\u0000 (2\u00020\u0001:\u0004%&'(B\u0005¢\u0006\u0002\u0010\u0002J\b\u0010\u0015\u001a\u00020\tH\u0002J\u0012\u0010\u0016\u001a\u00020\u00172\b\u0010\u0018\u001a\u0004\u0018\u00010\u0019H\u0016J\b\u0010\u001a\u001a\u00020\u001bH\u0016J\b\u0010\u001c\u001a\u00020\u001bH\u0016J\u0018\u0010\u001d\u001a\u00020\u001b2\u0006\u0010\u001e\u001a\u00020\u00122\u0006\u0010\u001f\u001a\u00020\u0013H\u0002J\u0010\u0010 \u001a\u00020\u001b2\b\u0010!\u001a\u0004\u0018\u00010\u000bJ\u0010\u0010\"\u001a\u00020\u001b2\u0006\u0010\u001e\u001a\u00020\u0012H\u0002J\u0010\u0010#\u001a\u00020\u001b2\u0006\u0010\u001f\u001a\u00020\tH\u0002J\f\u0010$\u001a\u00020\t*\u00020\u0013H\u0002R\u0012\u0010\u0003\u001a\u00060\u0004R\u00020\u0000X\u0082\u0004¢\u0006\u0002\n\u0000R\u0010\u0010\u0005\u001a\u00020\u0006X\u0082\u0004¢\u0006\u0004\n\u0002\u0010\u0007R\u000e\u0010\b\u001a\u00020\tX\u0082\u000e¢\u0006\u0002\n\u0000R\u0010\u0010\n\u001a\u0004\u0018\u00010\u000bX\u0082\u000e¢\u0006\u0002\n\u0000R\u0011\u0010\f\u001a\u00020\t8F¢\u0006\u0006\u001a\u0004\b\r\u0010\u000eR6\u0010\u000f\u001a*\u0012\n\u0012\b\u0012\u0004\u0012\u00020\u00120\u0011\u0012\u0004\u0012\u00020\u00130\u0010j\u0014\u0012\n\u0012\b\u0012\u0004\u0012\u00020\u00120\u0011\u0012\u0004\u0012\u00020\u0013`\u0014X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006)"}, d2 = {"Lcom/mapbox/common/LifecycleService;", "Landroid/app/Service;", "()V", "binder", "Lcom/mapbox/common/LifecycleService$Binder;", "callback", "com/mapbox/common/LifecycleService$callback$1", "Lcom/mapbox/common/LifecycleService$callback$1;", "currentLifecycleState", "Lcom/mapbox/common/LifecycleState;", "lifecycleCallback", "Lcom/mapbox/common/LifecycleService$Callback;", "lifecycleState", "getLifecycleState", "()Lcom/mapbox/common/LifecycleState;", "registeredActivities", "Ljava/util/HashMap;", "Ljava/lang/ref/WeakReference;", "Landroid/app/Activity;", "Lcom/mapbox/common/LifecycleService$ActivityState;", "Lkotlin/collections/HashMap;", "calculateLifecycleState", "onBind", "Landroid/os/IBinder;", "intent", "Landroid/content/Intent;", "onCreate", "", "onDestroy", "registerActivityWithState", "activity", "state", "setCallback", "observer", "unregisterActivity", "updateLifecycleState", "toLifecycleState", "ActivityState", "Binder", "Callback", "Companion", "common_release"}, k = 1, mv = {1, 7, 1}, xi = 48)
public final class LifecycleService extends Service {
    public static final String TAG = "LifecycleService";
    private Callback lifecycleCallback;
    private final HashMap<WeakReference<Activity>, ActivityState> registeredActivities = new HashMap<>();
    private final Binder binder = new Binder();
    private LifecycleState currentLifecycleState = LifecycleState.UNKNOWN;
    private final LifecycleService$callback$1 callback = new Application.ActivityLifecycleCallbacks() {
        @Override
        public synchronized void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            Intrinsics.h(activity, "activity");
            LifecycleService.this.registerActivityWithState(activity, LifecycleService.ActivityState.Created);
        }

        @Override
        public synchronized void onActivityDestroyed(Activity activity) {
            Intrinsics.h(activity, "activity");
            LifecycleService.this.unregisterActivity(activity);
        }

        @Override
        public synchronized void onActivityPaused(Activity activity) {
            Intrinsics.h(activity, "activity");
            LifecycleService.this.registerActivityWithState(activity, LifecycleService.ActivityState.Paused);
        }

        @Override
        public synchronized void onActivityResumed(Activity activity) {
            Intrinsics.h(activity, "activity");
            LifecycleService.this.registerActivityWithState(activity, LifecycleService.ActivityState.Resumed);
        }

        @Override
        public synchronized void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            Intrinsics.h(activity, "activity");
            Intrinsics.h(outState, "outState");
            LifecycleService.this.registerActivityWithState(activity, LifecycleService.ActivityState.SaveInstanceState);
        }

        @Override
        public synchronized void onActivityStarted(Activity activity) {
            Intrinsics.h(activity, "activity");
            LifecycleService.this.registerActivityWithState(activity, LifecycleService.ActivityState.Started);
        }

        @Override
        public synchronized void onActivityStopped(Activity activity) {
            Intrinsics.h(activity, "activity");
            LifecycleService.this.registerActivityWithState(activity, LifecycleService.ActivityState.Stopped);
        }
    };

    @Metadata(d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0010\u0010\n\u0002\b\n\b\u0082\u0001\u0018\u00002\b\u0012\u0004\u0012\u00020\u00000\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002j\u0002\b\u0003j\u0002\b\u0004j\u0002\b\u0005j\u0002\b\u0006j\u0002\b\u0007j\u0002\b\bj\u0002\b\tj\u0002\b\n¨\u0006\u000b"}, d2 = {"Lcom/mapbox/common/LifecycleService$ActivityState;", "", "(Ljava/lang/String;I)V", "Unknown", "Created", "Started", "Resumed", "Paused", "Stopped", "SaveInstanceState", "Destroyed", "common_release"}, k = 1, mv = {1, 7, 1}, xi = 48)
    public enum ActivityState {
        Unknown,
        Created,
        Started,
        Resumed,
        Paused,
        Stopped,
        SaveInstanceState,
        Destroyed
    }

    @Metadata(d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\b\u0086\u0004\u0018\u00002\u00020\u0001B\u0005¢\u0006\u0002\u0010\u0002J\u0006\u0010\u0003\u001a\u00020\u0004¨\u0006\u0005"}, d2 = {"Lcom/mapbox/common/LifecycleService$Binder;", "Landroid/os/Binder;", "(Lcom/mapbox/common/LifecycleService;)V", "getService", "Lcom/mapbox/common/LifecycleService;", "common_release"}, k = 1, mv = {1, 7, 1}, xi = 48)
    public final class Binder extends android.os.Binder {
        public Binder() {
        }

        public final LifecycleService getThis$0() {
            return LifecycleService.this;
        }
    }

    @Metadata(d1 = {"\u0000\u0016\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\bf\u0018\u00002\u00020\u0001J\u0010\u0010\u0002\u001a\u00020\u00032\u0006\u0010\u0004\u001a\u00020\u0005H&¨\u0006\u0006"}, d2 = {"Lcom/mapbox/common/LifecycleService$Callback;", "", "onLifecycleStateChanged", "", "state", "Lcom/mapbox/common/LifecycleState;", "common_release"}, k = 1, mv = {1, 7, 1}, xi = 48)
    public interface Callback {
        void onLifecycleStateChanged(LifecycleState state);
    }

    @Metadata(k = 3, mv = {1, 7, 1}, xi = 48)
    public class WhenMappings {
        public static final int[] $EnumSwitchMapping$0;

        static {
            int[] iArr = new int[ActivityState.values().length];
            try {
                iArr[ActivityState.Unknown.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                iArr[ActivityState.Created.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                iArr[ActivityState.Started.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                iArr[ActivityState.Resumed.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                iArr[ActivityState.Paused.ordinal()] = 5;
            } catch (NoSuchFieldError unused5) {
            }
            try {
                iArr[ActivityState.Stopped.ordinal()] = 6;
            } catch (NoSuchFieldError unused6) {
            }
            try {
                iArr[ActivityState.SaveInstanceState.ordinal()] = 7;
            } catch (NoSuchFieldError unused7) {
            }
            try {
                iArr[ActivityState.Destroyed.ordinal()] = 8;
            } catch (NoSuchFieldError unused8) {
            }
            $EnumSwitchMapping$0 = iArr;
        }
    }

    private final synchronized LifecycleState calculateLifecycleState() {
        Object obj;
        LifecycleUtils lifecycleUtils = LifecycleUtils.INSTANCE;
        if (lifecycleUtils.hasServiceRunningInForeground(this)) {
            return LifecycleState.FOREGROUND;
        }
        if (this.registeredActivities.isEmpty()) {
            return lifecycleUtils.getAppLifecycleStateFromActivityManager(this);
        }
        LifecycleState[] lifecycleStateArr = {LifecycleState.MOVING_FOREGROUND, LifecycleState.FOREGROUND, LifecycleState.INACTIVE, LifecycleState.MOVING_BACKGROUND};
        for (int i = 0; i < 4; i++) {
            LifecycleState lifecycleState = lifecycleStateArr[i];
            Collection<ActivityState> values = this.registeredActivities.values();
            Intrinsics.g(values, "registeredActivities.values");
            Iterator<T> it = values.iterator();
            while (true) {
                if (!it.hasNext()) {
                    obj = null;
                    break;
                }
                obj = it.next();
                ActivityState activityState = (ActivityState) obj;
                Intrinsics.g(activityState, "it");
                if (toLifecycleState(activityState) == lifecycleState) {
                    break;
                }
            }
            if (((ActivityState) obj) != null) {
                return lifecycleState;
            }
        }
        return LifecycleState.BACKGROUND;
    }

    public final synchronized void registerActivityWithState(Activity activity, ActivityState state) {
        try {
            HashMap<WeakReference<Activity>, ActivityState> hashMap = this.registeredActivities;
            LinkedHashMap linkedHashMap = new LinkedHashMap();
            for (Map.Entry<WeakReference<Activity>, ActivityState> entry : hashMap.entrySet()) {
                if (Intrinsics.c(entry.getKey().get(), activity)) {
                    linkedHashMap.put(entry.getKey(), entry.getValue());
                }
            }
            if (linkedHashMap.isEmpty()) {
                this.registeredActivities.put(new WeakReference<>(activity), state);
            } else {
                Iterator it = linkedHashMap.entrySet().iterator();
                while (it.hasNext()) {
                    this.registeredActivities.put(((Map.Entry) it.next()).getKey(), state);
                }
            }
            updateLifecycleState(calculateLifecycleState());
        } catch (Throwable th) {
            throw th;
        }
    }

    private final LifecycleState toLifecycleState(ActivityState activityState) {
        switch (WhenMappings.$EnumSwitchMapping$0[activityState.ordinal()]) {
            case 1:
                return LifecycleState.UNKNOWN;
            case 2:
                return LifecycleState.MOVING_FOREGROUND;
            case 3:
                return LifecycleState.MOVING_FOREGROUND;
            case 4:
                return LifecycleState.FOREGROUND;
            case 5:
                return LifecycleState.INACTIVE;
            case 6:
                return LifecycleState.MOVING_BACKGROUND;
            case 7:
                return LifecycleState.BACKGROUND;
            case 8:
                return LifecycleState.BACKGROUND;
            default:
                throw new NoWhenBranchMatchedException();
        }
    }

    public final synchronized void unregisterActivity(Activity activity) {
        try {
            HashMap<WeakReference<Activity>, ActivityState> hashMap = this.registeredActivities;
            LinkedHashMap linkedHashMap = new LinkedHashMap();
            for (Map.Entry<WeakReference<Activity>, ActivityState> entry : hashMap.entrySet()) {
                WeakReference<Activity> key = entry.getKey();
                if (key.get() != null && !Intrinsics.c(key.get(), activity)) {
                }
                linkedHashMap.put(entry.getKey(), entry.getValue());
            }
            Iterator it = linkedHashMap.entrySet().iterator();
            while (it.hasNext()) {
                this.registeredActivities.remove(((Map.Entry) it.next()).getKey());
            }
            updateLifecycleState(calculateLifecycleState());
        } catch (Throwable th) {
            throw th;
        }
    }

    private final synchronized void updateLifecycleState(LifecycleState state) {
        if (this.currentLifecycleState == state) {
            return;
        }
        this.currentLifecycleState = state;
        Callback callback = this.lifecycleCallback;
        if (callback != null) {
            callback.onLifecycleStateChanged(state);
        }
    }

    public final LifecycleState getLifecycleState() {
        updateLifecycleState(calculateLifecycleState());
        return this.currentLifecycleState;
    }

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

    @Override
    public void onCreate() {
        this.currentLifecycleState = LifecycleUtils.INSTANCE.getLifecycleState(this);
        Application application = getApplication();
        if (application != null) {
            application.registerActivityLifecycleCallbacks(this.callback);
        }
    }

    @Override
    public void onDestroy() {
        this.currentLifecycleState = LifecycleState.UNKNOWN;
        this.registeredActivities.clear();
        Application application = getApplication();
        if (application != null) {
            application.unregisterActivityLifecycleCallbacks(this.callback);
        }
    }

    public final synchronized void setCallback(Callback observer) {
        this.lifecycleCallback = observer;
        if (observer != null) {
            observer.onLifecycleStateChanged(calculateLifecycleState());
        }
    }
}