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

Commit 3ef658ee authored by Kweku Adams's avatar Kweku Adams Committed by Android (Google) Code Review
Browse files

Merge "Add initial Scribe code."

parents 1db048d2 d149ab8b
Loading
Loading
Loading
Loading
+10 −8
Original line number Original line Diff line number Diff line
@@ -83,6 +83,8 @@ public class InternalResourceService extends SystemService {
    static final long UNUSED_RECLAMATION_PERIOD_MS = 24 * HOUR_IN_MILLIS;
    static final long UNUSED_RECLAMATION_PERIOD_MS = 24 * HOUR_IN_MILLIS;
    /** How much of an app's unused wealth should be reclaimed periodically. */
    /** How much of an app's unused wealth should be reclaimed periodically. */
    private static final float DEFAULT_UNUSED_RECLAMATION_PERCENTAGE = .1f;
    private static final float DEFAULT_UNUSED_RECLAMATION_PERCENTAGE = .1f;
    /** The amount of time to delay reclamation by after boot. */
    private static final long RECLAMATION_STARTUP_DELAY_MS = 30_000L;
    private static final int PACKAGE_QUERY_FLAGS =
    private static final int PACKAGE_QUERY_FLAGS =
            PackageManager.MATCH_DIRECT_BOOT_AWARE | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
            PackageManager.MATCH_DIRECT_BOOT_AWARE | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
                    | PackageManager.MATCH_APEX;
                    | PackageManager.MATCH_APEX;
@@ -99,6 +101,7 @@ public class InternalResourceService extends SystemService {
    private final CompleteEconomicPolicy mCompleteEconomicPolicy;
    private final CompleteEconomicPolicy mCompleteEconomicPolicy;
    private final ConfigObserver mConfigObserver;
    private final ConfigObserver mConfigObserver;
    private final EconomyManagerStub mEconomyManagerStub;
    private final EconomyManagerStub mEconomyManagerStub;
    private final Scribe mScribe;


    @NonNull
    @NonNull
    @GuardedBy("mLock")
    @GuardedBy("mLock")
@@ -117,9 +120,6 @@ public class InternalResourceService extends SystemService {
    // In the range [0,100] to represent 0% to 100% battery.
    // In the range [0,100] to represent 0% to 100% battery.
    @GuardedBy("mLock")
    @GuardedBy("mLock")
    private int mCurrentBatteryLevel;
    private int mCurrentBatteryLevel;
    // TODO: load from disk
    @GuardedBy("mLock")
    private long mLastUnusedReclamationTime;


    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Nullable
        @Nullable
@@ -187,7 +187,7 @@ public class InternalResourceService extends SystemService {
                public void onAlarm() {
                public void onAlarm() {
                    synchronized (mLock) {
                    synchronized (mLock) {
                        mAgent.reclaimUnusedAssetsLocked(DEFAULT_UNUSED_RECLAMATION_PERCENTAGE);
                        mAgent.reclaimUnusedAssetsLocked(DEFAULT_UNUSED_RECLAMATION_PERCENTAGE);
                        mLastUnusedReclamationTime = getCurrentTimeMillis();
                        mScribe.setLastReclamationTimeLocked(getCurrentTimeMillis());
                        scheduleUnusedWealthReclamationLocked();
                        scheduleUnusedWealthReclamationLocked();
                    }
                    }
                }
                }
@@ -216,6 +216,7 @@ public class InternalResourceService extends SystemService {
        mPackageManager = context.getPackageManager();
        mPackageManager = context.getPackageManager();
        mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class);
        mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class);
        mEconomyManagerStub = new EconomyManagerStub();
        mEconomyManagerStub = new EconomyManagerStub();
        mScribe = new Scribe(this);
        mCompleteEconomicPolicy = new CompleteEconomicPolicy(this);
        mCompleteEconomicPolicy = new CompleteEconomicPolicy(this);
        mAgent = new Agent(this, mCompleteEconomicPolicy);
        mAgent = new Agent(this, mCompleteEconomicPolicy);


@@ -451,8 +452,8 @@ public class InternalResourceService extends SystemService {
    @GuardedBy("mLock")
    @GuardedBy("mLock")
    private void scheduleUnusedWealthReclamationLocked() {
    private void scheduleUnusedWealthReclamationLocked() {
        final long now = getCurrentTimeMillis();
        final long now = getCurrentTimeMillis();
        final long nextReclamationTime =
        final long nextReclamationTime = Math.max(now + RECLAMATION_STARTUP_DELAY_MS,
                Math.max(mLastUnusedReclamationTime + UNUSED_RECLAMATION_PERIOD_MS, now + 30_000);
                mScribe.getLastReclamationTimeLocked() + UNUSED_RECLAMATION_PERIOD_MS);
        mHandler.post(() -> {
        mHandler.post(() -> {
            // Never call out to AlarmManager with the lock held. This sits below AM.
            // Never call out to AlarmManager with the lock held. This sits below AM.
            AlarmManager alarmManager = getContext().getSystemService(AlarmManager.class);
            AlarmManager alarmManager = getContext().getSystemService(AlarmManager.class);
@@ -463,7 +464,7 @@ public class InternalResourceService extends SystemService {
                        ALARM_TAG_WEALTH_RECLAMATION, mUnusedWealthReclamationListener, mHandler);
                        ALARM_TAG_WEALTH_RECLAMATION, mUnusedWealthReclamationListener, mHandler);
            } else {
            } else {
                mHandler.sendEmptyMessageDelayed(
                mHandler.sendEmptyMessageDelayed(
                        MSG_SCHEDULE_UNUSED_WEALTH_RECLAMATION_EVENT, 30_000);
                        MSG_SCHEDULE_UNUSED_WEALTH_RECLAMATION_EVENT, RECLAMATION_STARTUP_DELAY_MS);
            }
            }
        });
        });
    }
    }
@@ -531,6 +532,7 @@ public class InternalResourceService extends SystemService {
            if (isFirstSetup) {
            if (isFirstSetup) {
                mAgent.grantBirthrightsLocked();
                mAgent.grantBirthrightsLocked();
            }
            }
            scheduleUnusedWealthReclamationLocked();
        }
        }
    }
    }


@@ -542,7 +544,6 @@ public class InternalResourceService extends SystemService {
            registerListeners();
            registerListeners();
            mCurrentBatteryLevel = getCurrentBatteryLevel();
            mCurrentBatteryLevel = getCurrentBatteryLevel();
            mHandler.post(this::setupHeavyWork);
            mHandler.post(this::setupHeavyWork);
            scheduleUnusedWealthReclamationLocked();
            mCompleteEconomicPolicy.setup();
            mCompleteEconomicPolicy.setup();
        }
        }
    }
    }
@@ -562,6 +563,7 @@ public class InternalResourceService extends SystemService {
                }
                }
            });
            });
            mPkgCache.clear();
            mPkgCache.clear();
            mScribe.tearDownLocked();
            mUidToPackageCache.clear();
            mUidToPackageCache.clear();
            getContext().unregisterReceiver(mBroadcastReceiver);
            getContext().unregisterReceiver(mBroadcastReceiver);
            UsageStatsManagerInternal usmi =
            UsageStatsManagerInternal usmi =
+54 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2021 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.server.tare;

import android.util.Log;

import com.android.internal.annotations.GuardedBy;

/**
 * Maintains the current TARE state and handles writing it to disk and reading it back from disk.
 */
public class Scribe {
    private static final String TAG = "TARE-" + Scribe.class.getSimpleName();
    private static final boolean DEBUG = InternalResourceService.DEBUG
            || Log.isLoggable(TAG, Log.DEBUG);

    private final InternalResourceService mIrs;

    @GuardedBy("mIrs.mLock")
    private long mLastReclamationTime;

    Scribe(InternalResourceService irs) {
        mIrs = irs;
    }

    @GuardedBy("mIrs.mLock")
    long getLastReclamationTimeLocked() {
        return mLastReclamationTime;
    }

    @GuardedBy("InternalResourceService.mLock")
    void setLastReclamationTimeLocked(long time) {
        mLastReclamationTime = time;
    }

    @GuardedBy("mIrs.mLock")
    void tearDownLocked() {
        mLastReclamationTime = 0;
    }
}