Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 24423194 authored by Nate Myren's avatar Nate Myren Committed by Android (Google) Code Review
Browse files

Merge "Have LifecycleRegistry throw error if called on non main thread" into rvc-dev

parents 4246e11b ac7948e9
Loading
Loading
Loading
Loading
+13 −8
Original line number Diff line number Diff line
@@ -137,12 +137,12 @@ abstract class SmartUpdateMediatorLiveData<T> : MediatorLiveData<T>(),
    fun observeStale(owner: LifecycleOwner, observer: Observer<in T>) {
        val oldStaleObserver = hasStaleObserver()
        staleObservers.add(owner to observer)
        notifySourcesOnStaleUpdates(oldStaleObserver, true)
        if (owner == ForeverActiveLifecycle) {
            observeForever(observer)
        } else {
            observe(owner, observer)
        }
        updateSourceStaleObservers(oldStaleObserver, true)
    }

    override fun <S : Any?> addSource(source: LiveData<S>, onChanged: Observer<in S>) {
@@ -181,7 +181,7 @@ abstract class SmartUpdateMediatorLiveData<T> : MediatorLiveData<T>(),
    }

    @MainThread
    private fun <S : Any?> updateStaleChildNotify(
    private fun <S : Any?> updateShouldSendStaleUpdates(
        liveData: SmartUpdateMediatorLiveData<S>,
        sendStaleUpdates: Boolean
    ) {
@@ -196,30 +196,35 @@ abstract class SmartUpdateMediatorLiveData<T> : MediatorLiveData<T>(),
    override fun removeObserver(observer: Observer<in T>) {
        val oldStaleObserver = hasStaleObserver()
        staleObservers.removeIf { it.second == observer }
        notifySourcesOnStaleUpdates(oldStaleObserver, hasStaleObserver())
        super.removeObserver(observer)
        updateSourceStaleObservers(oldStaleObserver, hasStaleObserver())
    }

    @MainThread
    override fun removeObservers(owner: LifecycleOwner) {
        val oldStaleObserver = hasStaleObserver()
        staleObservers.removeIf { it.first == owner }
        notifySourcesOnStaleUpdates(oldStaleObserver, hasStaleObserver())
        super.removeObservers(owner)
        updateSourceStaleObservers(oldStaleObserver, hasStaleObserver())
    }

    @MainThread
    private fun notifySourcesOnStaleUpdates(oldHasStale: Boolean, newHasStale: Boolean) {
        if (oldHasStale == newHasStale) {
    override fun observeForever(observer: Observer<in T>) {
        super.observeForever(observer)
    }

    @MainThread
    private fun updateSourceStaleObservers(hadStaleObserver: Boolean, hasStaleObserver: Boolean) {
        if (hadStaleObserver == hasStaleObserver) {
            return
        }
        for (liveData in sources) {
            liveData.updateStaleChildNotify(this, hasStaleObserver())
            liveData.updateShouldSendStaleUpdates(this, hasStaleObserver)
        }

        // if all sources are not stale, and we just requested stale updates, and we are stale,
        // update our value
        if (sources.all { !it.isStale } && newHasStale && isStale) {
        if (sources.all { !it.isStale } && hasStaleObserver && isStale) {
            updateIfActive()
        }
    }
+40 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.permissioncontroller.permission.service

import android.os.Looper
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import java.lang.IllegalStateException

class CheckLifecycleRegistry(provider: LifecycleOwner) : LifecycleRegistry(provider) {

    override fun addObserver(observer: LifecycleObserver) {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            throw IllegalStateException("Lifecycle running on non main thread")
        }
        super.addObserver(observer)
    }

    override fun removeObserver(observer: LifecycleObserver) {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            throw IllegalStateException("Lifecycle running on non main thread")
        }
        super.removeObserver(observer)
    }
}
 No newline at end of file
+36 −31
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package com.android.permissioncontroller.permission.service

import android.content.pm.PackageManager
import android.os.Handler
import android.os.Process
import android.permission.PermissionControllerManager.COUNT_ONLY_WHEN_GRANTED
import android.permission.PermissionControllerManager.COUNT_WHEN_SYSTEM
@@ -34,6 +33,9 @@ import com.android.permissioncontroller.permission.model.livedatatypes.AppPermGr
import com.android.permissioncontroller.permission.model.livedatatypes.AppPermGroupUiInfo.PermGrantState
import com.android.permissioncontroller.permission.model.livedatatypes.LightPackageInfo
import com.android.permissioncontroller.permission.utils.Utils
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.util.function.IntConsumer

/**
@@ -58,7 +60,7 @@ class PermissionControllerServiceModel(private val service: PermissionController
        liveData: LiveData<T>,
        onChangedFun: (t: T?) -> Unit
    ) {
        val main = Handler(service.mainLooper)
        GlobalScope.launch(Main.immediate) {

            if (service.lifecycle.currentState != Lifecycle.State.STARTED) {
                service.setLifecycleToStarted()
@@ -66,7 +68,7 @@ class PermissionControllerServiceModel(private val service: PermissionController

            if (!liveData.hasActiveObservers()) {
                observedLiveDatas.add(liveData)
            main.post { liveData.observe(service, Observer { }) }
                liveData.observe(service, Observer { })
            }

            var updated = false
@@ -85,9 +87,10 @@ class PermissionControllerServiceModel(private val service: PermissionController
            }

            if (liveData is SmartUpdateMediatorLiveData<T>) {
            main.post { liveData.observeStale(service, observer) }
                liveData.observeStale(service, observer)
            } else {
            main.post { liveData.observe(service, observer) }
                liveData.observe(service, observer)
            }
        }
    }

@@ -95,12 +98,14 @@ class PermissionControllerServiceModel(private val service: PermissionController
     * Stop observing all currently observed liveDatas
     */
    fun removeObservers() {
        GlobalScope.launch(Main.immediate) {
            for (liveData in observedLiveDatas) {
                liveData.removeObservers(service)
            }

            observedLiveDatas.clear()
        }
    }

    /**
     * Counts the number of apps that have at least one of a provided list of permissions, subject
+1 −1
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ public class ServiceLifecycleDispatcher {
     * @param provider {@link LifecycleOwner} for a service, usually it is a service itself
     */
    public ServiceLifecycleDispatcher(@NonNull LifecycleOwner provider) {
        mRegistry = new LifecycleRegistry(provider);
        mRegistry = new CheckLifecycleRegistry(provider);
        mHandler = new Handler();
    }