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

Commit d149ab8b authored by Kweku Adams's avatar Kweku Adams
Browse files

Add initial Scribe code.

The Scribe will hold internal state data and handle writing to and
reading the state from disk.

Bug: 158300259
Test: Android builds
Change-Id: I61b0f3b4c9e4b0aa9f3aac7f4b88b15423e689b5
parent da143d4a
Loading
Loading
Loading
Loading
+10 −8
Original line number 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;
    /** How much of an app's unused wealth should be reclaimed periodically. */
    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 =
            PackageManager.MATCH_DIRECT_BOOT_AWARE | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
                    | PackageManager.MATCH_APEX;
@@ -99,6 +101,7 @@ public class InternalResourceService extends SystemService {
    private final CompleteEconomicPolicy mCompleteEconomicPolicy;
    private final ConfigObserver mConfigObserver;
    private final EconomyManagerStub mEconomyManagerStub;
    private final Scribe mScribe;

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

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

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

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