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
This commit is contained in:
Jeff Sharkey
2020-10-16 20:35:31 -06:00
parent a49ba3ac12
commit df8dc2b4a5
10 changed files with 53 additions and 40 deletions

View File

@@ -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();

View File

@@ -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);
}
}
}

View File

@@ -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;
}

View File

@@ -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));
}
}

View File

@@ -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

View File

@@ -6150,6 +6150,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
* was set.
*/
@NonNull
@SuppressWarnings("AndroidFrameworkEfficientCollections")
public Map<Integer, Integer> getAttributeSourceResourceMap() {
HashMap<Integer, Integer> map = new HashMap<>();
if (!sDebugViewAttributes || mAttributeSourceResId == null) {

View File

@@ -50,6 +50,7 @@ import android.os.Bundle;
import android.os.Parcelable;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.IntArray;
import android.util.Log;
import android.util.Pools;
import android.util.Pools.SynchronizedPool;
@@ -611,7 +612,7 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
private int mNestedScrollAxes;
// Used to manage the list of transient views, added by addTransientView()
private List<Integer> mTransientIndices = null;
private IntArray mTransientIndices = null;
private List<View> mTransientViews = null;
/**
@@ -4853,7 +4854,7 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
}
if (mTransientIndices == null) {
mTransientIndices = new ArrayList<Integer>();
mTransientIndices = new IntArray();
mTransientViews = new ArrayList<View>();
}
final int oldSize = mTransientIndices.size();

View File

@@ -16,25 +16,23 @@
package com.android.internal.os;
import android.app.AppGlobals;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.RemoteException;
import android.os.UserHandle;
import android.util.SparseArray;
import com.android.internal.annotations.VisibleForTesting;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** Maps AppIds to their package names. */
public final class AppIdToPackageMap {
private final Map<Integer, String> mAppIdToPackageMap;
private final SparseArray<String> mAppIdToPackageMap;
@VisibleForTesting
public AppIdToPackageMap(Map<Integer, String> appIdToPackageMap) {
public AppIdToPackageMap(SparseArray<String> appIdToPackageMap) {
mAppIdToPackageMap = appIdToPackageMap;
}
@@ -50,10 +48,10 @@ public final class AppIdToPackageMap {
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
final Map<Integer, String> map = new HashMap<>();
final SparseArray<String> map = new SparseArray<>();
for (PackageInfo pkg : packages) {
final int uid = pkg.applicationInfo.uid;
if (pkg.sharedUserId != null && map.containsKey(uid)) {
if (pkg.sharedUserId != null && map.indexOfKey(uid) >= 0) {
// Use sharedUserId string as package name if there are collisions
map.put(uid, "shared:" + pkg.sharedUserId);
} else {

View File

@@ -26,7 +26,7 @@ import android.os.Looper;
import android.os.UserHandle;
import android.provider.Settings;
import android.service.notification.StatusBarNotification;
import android.util.ArrayMap;
import android.util.SparseArray;
import java.util.Collection;
import java.util.Objects;
@@ -39,7 +39,7 @@ public class NotificationMessagingUtil {
private static final String DEFAULT_SMS_APP_SETTING = Settings.Secure.SMS_DEFAULT_APPLICATION;
private final Context mContext;
private ArrayMap<Integer, String> mDefaultSmsApp = new ArrayMap<>();
private SparseArray<String> mDefaultSmsApp = new SparseArray<>();
public NotificationMessagingUtil(Context context) {
mContext = context;

View File

@@ -497,9 +497,9 @@ public class BinderCallsStatsTest {
bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID);
PrintWriter pw = new PrintWriter(new StringWriter());
bcs.dump(pw, new AppIdToPackageMap(new HashMap<>()), Process.INVALID_UID, true);
bcs.dump(pw, new AppIdToPackageMap(new SparseArray<>()), Process.INVALID_UID, true);
bcs.dump(pw, new AppIdToPackageMap(new HashMap<>()), WORKSOURCE_UID, true);
bcs.dump(pw, new AppIdToPackageMap(new SparseArray<>()), WORKSOURCE_UID, true);
}
@Test