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

Commit df8dc2b4 authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Apply fixes for EfficientCollections.

Drop-in replacements suggested for inefficient collections.  Also
annotate a handful of places where we're unable to update.

Bug: 155703208
Test: none
Exempt-From-Owner-Approval: trivial refactoring
Change-Id: I48b600508df8160ac9b40fea7afca974b2c972f6
parent a49ba3ac
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -7447,6 +7447,7 @@ public class Intent implements Parcelable, Cloneable {

    /** @hide */
    @UnsupportedAppUsage
    @SuppressWarnings("AndroidFrameworkEfficientCollections")
    public static Intent parseCommandArgs(ShellCommand cmd, CommandOptionHandler optionHandler)
            throws URISyntaxException {
        Intent intent = new Intent();
+8 −5
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.compat.annotation.UnsupportedAppUsage;
import android.util.Log;
import android.util.SparseArray;

import java.io.File;
import java.lang.annotation.Retention;
@@ -101,7 +102,9 @@ public abstract class FileObserver {
    private static final String LOG_TAG = "FileObserver";

    private static class ObserverThread extends Thread {
        /** Temporarily retained; appears to be missing UnsupportedAppUsage annotation */
        private HashMap<Integer, WeakReference> m_observers = new HashMap<Integer, WeakReference>();
        private SparseArray<WeakReference> mRealObservers = new SparseArray<>();
        private int m_fd;

        public ObserverThread() {
@@ -127,10 +130,10 @@ public abstract class FileObserver {

            final WeakReference<FileObserver> fileObserverWeakReference =
                    new WeakReference<>(observer);
            synchronized (m_observers) {
            synchronized (mRealObservers) {
                for (int wfd : wfds) {
                    if (wfd >= 0) {
                        m_observers.put(wfd, fileObserverWeakReference);
                        mRealObservers.put(wfd, fileObserverWeakReference);
                    }
                }
            }
@@ -147,12 +150,12 @@ public abstract class FileObserver {
            // look up our observer, fixing up the map if necessary...
            FileObserver observer = null;

            synchronized (m_observers) {
                WeakReference weak = m_observers.get(wfd);
            synchronized (mRealObservers) {
                WeakReference weak = mRealObservers.get(wfd);
                if (weak != null) {  // can happen with lots of events from a dead wfd
                    observer = (FileObserver) weak.get();
                    if (observer == null) {
                        m_observers.remove(wfd);
                        mRealObservers.remove(wfd);
                    }
                }
            }
+7 −3
Original line number Diff line number Diff line
@@ -2010,13 +2010,13 @@ public final class Parcel {
     * A map used by {@link #readSquashed} to cache parcelables. It's a map from
     * an absolute position in a Parcel to the parcelable stored at the position.
     */
    private ArrayMap<Integer, Parcelable> mReadSquashableParcelables;
    private SparseArray<Parcelable> mReadSquashableParcelables;

    private void ensureReadSquashableParcelables() {
        if (mReadSquashableParcelables != null) {
            return;
        }
        mReadSquashableParcelables = new ArrayMap<>();
        mReadSquashableParcelables = new SparseArray<>();
    }

    /**
@@ -2112,9 +2112,13 @@ public final class Parcel {

        final Parcelable p = mReadSquashableParcelables.get(firstAbsolutePos);
        if (p == null) {
            final StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mReadSquashableParcelables.size(); i++) {
                sb.append(mReadSquashableParcelables.keyAt(i)).append(' ');
            }
            Slog.wtfStack(TAG, "Map doesn't contain offset "
                    + firstAbsolutePos
                    + " : contains=" + new ArrayList<>(mReadSquashableParcelables.keySet()));
                    + " : contains=" + sb.toString());
        }
        return (T) p;
    }
+20 −16
Original line number Diff line number Diff line
@@ -60,6 +60,7 @@ import android.util.Log;
import android.util.Printer;
import android.util.Singleton;
import android.util.Slog;
import android.util.SparseLongArray;
import android.view.IWindowManager;

import com.android.internal.annotations.GuardedBy;
@@ -1525,7 +1526,9 @@ public final class StrictMode {
        // Map from violation stacktrace hashcode -> uptimeMillis of
        // last violation.  No locking needed, as this is only
        // accessed by the same thread.
        /** Temporarily retained; appears to be missing UnsupportedAppUsage annotation */
        private ArrayMap<Integer, Long> mLastViolationTime;
        private SparseLongArray mRealLastViolationTime;

        public AndroidBlockGuardPolicy(@ThreadPolicyMask int threadPolicyMask) {
            mThreadPolicyMask = threadPolicyMask;
@@ -1759,17 +1762,17 @@ public final class StrictMode {
            long lastViolationTime = 0;
            long now = SystemClock.uptimeMillis();
            if (sLogger == LOGCAT_LOGGER) { // Don't throttle it if there is a non-default logger
                if (mLastViolationTime != null) {
                    Long vtime = mLastViolationTime.get(crashFingerprint);
                if (mRealLastViolationTime != null) {
                    Long vtime = mRealLastViolationTime.get(crashFingerprint);
                    if (vtime != null) {
                        lastViolationTime = vtime;
                    }
                    clampViolationTimeMap(mLastViolationTime, Math.max(MIN_LOG_INTERVAL_MS,
                    clampViolationTimeMap(mRealLastViolationTime, Math.max(MIN_LOG_INTERVAL_MS,
                                Math.max(MIN_DIALOG_INTERVAL_MS, MIN_DROPBOX_INTERVAL_MS)));
                } else {
                    mLastViolationTime = new ArrayMap<>(1);
                    mRealLastViolationTime = new SparseLongArray(1);
                }
                mLastViolationTime.put(crashFingerprint, now);
                mRealLastViolationTime.put(crashFingerprint, now);
            }
            long timeSinceLastViolationMillis =
                    lastViolationTime == 0 ? Long.MAX_VALUE : (now - lastViolationTime);
@@ -2231,18 +2234,19 @@ public final class StrictMode {
    // Map from VM violation fingerprint to uptime millis.
    @UnsupportedAppUsage
    private static final HashMap<Integer, Long> sLastVmViolationTime = new HashMap<>();
    private static final SparseLongArray sRealLastVmViolationTime = new SparseLongArray();

    /**
     * Clamp the given map by removing elements with timestamp older than the given retainSince.
     */
    private static void clampViolationTimeMap(final @NonNull Map<Integer, Long> violationTime,
    private static void clampViolationTimeMap(final @NonNull SparseLongArray violationTime,
            final long retainSince) {
        final Iterator<Map.Entry<Integer, Long>> iterator = violationTime.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Integer, Long> e = iterator.next();
            if (e.getValue() < retainSince) {
        for (int i = 0; i < violationTime.size(); ) {
            if (violationTime.valueAt(i) < retainSince) {
                // Remove stale entries
                iterator.remove();
                violationTime.removeAt(i);
            } else {
                i++;
            }
        }
        // Ideally we'd cap the total size of the map, though it'll involve quickselect of topK,
@@ -2273,15 +2277,15 @@ public final class StrictMode {
        long lastViolationTime;
        long timeSinceLastViolationMillis = Long.MAX_VALUE;
        if (sLogger == LOGCAT_LOGGER) { // Don't throttle it if there is a non-default logger
            synchronized (sLastVmViolationTime) {
                if (sLastVmViolationTime.containsKey(fingerprint)) {
                    lastViolationTime = sLastVmViolationTime.get(fingerprint);
            synchronized (sRealLastVmViolationTime) {
                if (sRealLastVmViolationTime.indexOfKey(fingerprint) >= 0) {
                    lastViolationTime = sRealLastVmViolationTime.get(fingerprint);
                    timeSinceLastViolationMillis = now - lastViolationTime;
                }
                if (timeSinceLastViolationMillis > MIN_VM_INTERVAL_MS) {
                    sLastVmViolationTime.put(fingerprint, now);
                    sRealLastVmViolationTime.put(fingerprint, now);
                }
                clampViolationTimeMap(sLastVmViolationTime,
                clampViolationTimeMap(sRealLastVmViolationTime,
                        now - Math.max(MIN_VM_INTERVAL_MS, MIN_LOG_INTERVAL_MS));
            }
        }
+4 −3
Original line number Diff line number Diff line
@@ -16,10 +16,11 @@

package android.util.proto;

import android.util.LongArray;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

/**
 * Class to read to a protobuf stream.
@@ -98,7 +99,7 @@ public final class ProtoInputStream extends ProtoStream {
    /**
     * Keeps track of the currently read nested Objects, for end object checking and debug
     */
    private ArrayList<Long> mExpectedObjectTokenStack = null;
    private LongArray mExpectedObjectTokenStack = null;

    /**
     * Current nesting depth of start calls.
@@ -498,7 +499,7 @@ public final class ProtoInputStream extends ProtoStream {
        int messageSize = (int) readVarint();

        if (mExpectedObjectTokenStack == null) {
            mExpectedObjectTokenStack = new ArrayList<>();
            mExpectedObjectTokenStack = new LongArray();
        }
        if (++mDepth == mExpectedObjectTokenStack.size()) {
            // Create a token to keep track of nested Object and extend the object stack
Loading