| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content.pm.dex; |
| 18 |
|
| 19 |
import static android.Manifest.permission.PACKAGE_USAGE_STATS; |
| 20 |
import static android.Manifest.permission.READ_RUNTIME_PROFILES; |
| 21 |
|
| 22 |
import android.annotation.CallbackExecutor; |
| 23 |
import android.annotation.IntDef; |
| 24 |
import android.annotation.NonNull; |
| 25 |
import android.annotation.Nullable; |
| 26 |
import android.annotation.RequiresPermission; |
| 27 |
import android.annotation.SystemApi; |
| 28 |
import android.content.Context; |
| 29 |
import android.os.Environment; |
| 30 |
import android.os.ParcelFileDescriptor; |
| 31 |
import android.os.RemoteException; |
| 32 |
import android.util.Slog; |
| 33 |
|
| 34 |
import java.io.File; |
| 35 |
import java.lang.annotation.Retention; |
| 36 |
import java.lang.annotation.RetentionPolicy; |
| 37 |
import java.util.concurrent.Executor; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
@SystemApi |
| 46 |
public class ArtManager { |
| 47 |
private static final String TAG = "ArtManager"; |
| 48 |
|
| 49 |
|
| 50 |
public static final int SNAPSHOT_FAILED_PACKAGE_NOT_FOUND = 0; |
| 51 |
|
| 52 |
public static final int SNAPSHOT_FAILED_CODE_PATH_NOT_FOUND = 1; |
| 53 |
|
| 54 |
public static final int SNAPSHOT_FAILED_INTERNAL_ERROR = 2; |
| 55 |
|
| 56 |
|
| 57 |
public static final int PROFILE_APPS = 0; |
| 58 |
|
| 59 |
public static final int PROFILE_BOOT_IMAGE = 1; |
| 60 |
|
| 61 |
|
| 62 |
@IntDef(flag = true, prefix = { "PROFILE_" }, value = { |
| 63 |
PROFILE_APPS, |
| 64 |
PROFILE_BOOT_IMAGE, |
| 65 |
}) |
| 66 |
@Retention(RetentionPolicy.SOURCE) |
| 67 |
public @interface ProfileType {} |
| 68 |
|
| 69 |
private final Context mContext; |
| 70 |
private final IArtManager mArtManager; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public ArtManager(@NonNull Context context, @NonNull IArtManager manager) { |
| 76 |
mContext = context; |
| 77 |
mArtManager = manager; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
@RequiresPermission(allOf = { READ_RUNTIME_PROFILES, PACKAGE_USAGE_STATS }) |
| 108 |
public void snapshotRuntimeProfile(@ProfileType int profileType, @Nullable String packageName, |
| 109 |
@Nullable String codePath, @NonNull @CallbackExecutor Executor executor, |
| 110 |
@NonNull SnapshotRuntimeProfileCallback callback) { |
| 111 |
Slog.d(TAG, "Requesting profile snapshot for " + packageName + ":" + codePath); |
| 112 |
|
| 113 |
SnapshotRuntimeProfileCallbackDelegate delegate = |
| 114 |
new SnapshotRuntimeProfileCallbackDelegate(callback, executor); |
| 115 |
try { |
| 116 |
mArtManager.snapshotRuntimeProfile(profileType, packageName, codePath, delegate, |
| 117 |
mContext.getOpPackageName()); |
| 118 |
} catch (RemoteException e) { |
| 119 |
throw e.rethrowAsRuntimeException(); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
@RequiresPermission(allOf = { READ_RUNTIME_PROFILES, PACKAGE_USAGE_STATS }) |
| 132 |
public boolean isRuntimeProfilingEnabled(@ProfileType int profileType) { |
| 133 |
try { |
| 134 |
return mArtManager.isRuntimeProfilingEnabled(profileType, mContext.getOpPackageName()); |
| 135 |
} catch (RemoteException e) { |
| 136 |
throw e.rethrowAsRuntimeException(); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public abstract static class SnapshotRuntimeProfileCallback { |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public abstract void onSuccess(ParcelFileDescriptor profileReadFd); |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public abstract void onError(int errCode); |
| 159 |
} |
| 160 |
|
| 161 |
private static class SnapshotRuntimeProfileCallbackDelegate |
| 162 |
extends android.content.pm.dex.ISnapshotRuntimeProfileCallback.Stub { |
| 163 |
private final ArtManager.SnapshotRuntimeProfileCallback mCallback; |
| 164 |
private final Executor mExecutor; |
| 165 |
|
| 166 |
private SnapshotRuntimeProfileCallbackDelegate( |
| 167 |
ArtManager.SnapshotRuntimeProfileCallback callback, Executor executor) { |
| 168 |
mCallback = callback; |
| 169 |
mExecutor = executor; |
| 170 |
} |
| 171 |
|
| 172 |
@Override |
| 173 |
public void onSuccess(final ParcelFileDescriptor profileReadFd) { |
| 174 |
mExecutor.execute(() -> mCallback.onSuccess(profileReadFd)); |
| 175 |
} |
| 176 |
|
| 177 |
@Override |
| 178 |
public void onError(int errCode) { |
| 179 |
mExecutor.execute(() -> mCallback.onError(errCode)); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public static String getProfileName(String splitName) { |
| 190 |
return splitName == null ? "primary.prof" : splitName + ".split.prof"; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public static String getCurrentProfilePath(String packageName, int userId, String splitName) { |
| 199 |
File profileDir = Environment.getDataProfilesDePackageDirectory(userId, packageName); |
| 200 |
return new File(profileDir, getProfileName(splitName)).getAbsolutePath(); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public static String getReferenceProfilePath(String packageName, int userId, String splitName) { |
| 209 |
File profileDir = Environment.getDataRefProfilesDePackageDirectory(packageName); |
| 210 |
return new File(profileDir, getProfileName(splitName)).getAbsolutePath(); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public static File getProfileSnapshotFileForName(String packageName, String profileName) { |
| 222 |
File profileDir = Environment.getDataRefProfilesDePackageDirectory(packageName); |
| 223 |
return new File(profileDir, profileName + ".snapshot"); |
| 224 |
} |
| 225 |
} |