summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--api/current.txt1
-rw-r--r--api/system-current.txt1
-rw-r--r--core/java/android/content/ContentResolver.java14
-rw-r--r--core/java/android/hardware/camera2/CameraCharacteristics.java33
-rw-r--r--core/java/android/hardware/camera2/CameraManager.java18
-rw-r--r--core/java/android/hardware/camera2/CaptureResult.java33
-rw-r--r--core/java/android/hardware/camera2/impl/CameraDeviceImpl.java7
-rw-r--r--core/java/android/os/DropBoxManager.java3
-rw-r--r--core/java/android/os/ParcelFileDescriptor.java10
-rw-r--r--core/java/android/security/IKeystoreService.aidl3
-rw-r--r--core/java/android/util/DisplayMetrics.java8
-rw-r--r--core/java/android/util/StateSet.java7
-rw-r--r--core/java/android/view/WindowInsets.java2
-rw-r--r--keystore/java/android/security/KeyStore.java9
-rw-r--r--keystore/java/android/security/keystore/AndroidKeyStoreBCWorkaroundProvider.java106
-rw-r--r--keystore/java/android/security/keystore/AndroidKeyStoreECDSASignatureSpi.java123
-rw-r--r--keystore/java/android/security/keystore/AndroidKeyStoreECPublicKey.java57
-rw-r--r--keystore/java/android/security/keystore/AndroidKeyStoreKeyGeneratorSpi.java38
-rw-r--r--keystore/java/android/security/keystore/AndroidKeyStoreKeyPairGeneratorSpi.java92
-rw-r--r--keystore/java/android/security/keystore/AndroidKeyStoreProvider.java8
-rw-r--r--keystore/java/android/security/keystore/AndroidKeyStoreRSAPublicKey.java2
-rw-r--r--keystore/java/android/security/keystore/AndroidKeyStoreRSASignatureSpi.java164
-rw-r--r--keystore/java/android/security/keystore/AndroidKeyStoreSignatureSpiBase.java409
-rw-r--r--keystore/java/android/security/keystore/AndroidKeyStoreSpi.java9
-rw-r--r--media/java/android/media/Ringtone.java40
-rwxr-xr-xpackages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java6
-rw-r--r--packages/SystemUI/res/layout/recents_task_view_header.xml1
-rw-r--r--packages/SystemUI/src/com/android/systemui/qs/tiles/FlashlightTile.java6
-rw-r--r--packages/SystemUI/src/com/android/systemui/recents/Recents.java142
-rw-r--r--packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java4
-rw-r--r--packages/SystemUI/src/com/android/systemui/recents/views/RecentsView.java7
-rw-r--r--packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java22
-rw-r--r--packages/SystemUI/src/com/android/systemui/recents/views/TaskView.java27
-rw-r--r--packages/SystemUI/src/com/android/systemui/recents/views/TaskViewHeader.java29
-rw-r--r--packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java4
-rw-r--r--rs/java/android/renderscript/Allocation.java626
-rw-r--r--services/core/java/com/android/server/ConnectivityService.java2
-rw-r--r--services/core/java/com/android/server/InputMethodManagerService.java41
-rw-r--r--services/core/java/com/android/server/MountService.java43
-rw-r--r--services/core/java/com/android/server/pm/PackageManagerService.java125
-rw-r--r--telephony/java/android/telephony/CarrierConfigManager.java8
-rw-r--r--telephony/java/android/telephony/TelephonyManager.java3
-rw-r--r--tools/aapt2/Android.mk3
-rw-r--r--tools/aapt2/BinaryXmlPullParser.cpp259
-rw-r--r--tools/aapt2/BinaryXmlPullParser.h76
-rw-r--r--tools/aapt2/Main.cpp128
-rw-r--r--tools/aapt2/SdkConstants.cpp50
-rw-r--r--tools/aapt2/SdkConstants.h5
-rw-r--r--tools/aapt2/XmlDom.cpp431
-rw-r--r--tools/aapt2/XmlDom.h154
-rw-r--r--tools/aapt2/XmlDom_test.cpp49
-rw-r--r--tools/aapt2/XmlFlattener.cpp818
-rw-r--r--tools/aapt2/XmlFlattener.h69
-rw-r--r--tools/aapt2/XmlFlattener_test.cpp60
54 files changed, 3023 insertions, 1372 deletions
diff --git a/api/current.txt b/api/current.txt
index c9a8efb..0467b69 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -34002,6 +34002,7 @@ package android.util {
method public void setTo(android.util.DisplayMetrics);
method public void setToDefaults();
field public static final int DENSITY_280 = 280; // 0x118
+ field public static final int DENSITY_360 = 360; // 0x168
field public static final int DENSITY_400 = 400; // 0x190
field public static final int DENSITY_560 = 560; // 0x230
field public static final int DENSITY_DEFAULT = 160; // 0xa0
diff --git a/api/system-current.txt b/api/system-current.txt
index 2f26891..d11fe85 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -36267,6 +36267,7 @@ package android.util {
method public void setTo(android.util.DisplayMetrics);
method public void setToDefaults();
field public static final int DENSITY_280 = 280; // 0x118
+ field public static final int DENSITY_360 = 360; // 0x168
field public static final int DENSITY_400 = 400; // 0x190
field public static final int DENSITY_560 = 560; // 0x230
field public static final int DENSITY_DEFAULT = 160; // 0xa0
diff --git a/core/java/android/content/ContentResolver.java b/core/java/android/content/ContentResolver.java
index 17a8eb7..96a80e7 100644
--- a/core/java/android/content/ContentResolver.java
+++ b/core/java/android/content/ContentResolver.java
@@ -1552,23 +1552,21 @@ public abstract class ContentResolver {
*
* @param uri The URI to watch for changes. This can be a specific row URI, or a base URI
* for a whole class of content.
- * @param notifyForDescendents If <code>true</code> changes to URIs beginning with <code>uri</code>
- * will also cause notifications to be sent. If <code>false</code> only changes to the exact URI
- * specified by <em>uri</em> will cause notifications to be sent. If <code>true</code>, any URI values
- * at or below the specified URI will also trigger a match.
+ * @param notifyForDescendents When false, the observer will be notified whenever a
+ * change occurs to the exact URI specified by <code>uri</code> or to one of the
+ * URI's ancestors in the path hierarchy. When true, the observer will also be notified
+ * whenever a change occurs to the URI's descendants in the path hierarchy.
* @param observer The object that receives callbacks when changes occur.
* @see #unregisterContentObserver
*/
public final void registerContentObserver(Uri uri, boolean notifyForDescendents,
- ContentObserver observer)
- {
+ ContentObserver observer) {
registerContentObserver(uri, notifyForDescendents, observer, UserHandle.myUserId());
}
/** @hide - designated user version */
public final void registerContentObserver(Uri uri, boolean notifyForDescendents,
- ContentObserver observer, int userHandle)
- {
+ ContentObserver observer, int userHandle) {
try {
getContentService().registerContentObserver(uri, notifyForDescendents,
observer.getContentObserver(), userHandle);
diff --git a/core/java/android/hardware/camera2/CameraCharacteristics.java b/core/java/android/hardware/camera2/CameraCharacteristics.java
index 85e8827..b69ca88 100644
--- a/core/java/android/hardware/camera2/CameraCharacteristics.java
+++ b/core/java/android/hardware/camera2/CameraCharacteristics.java
@@ -1022,12 +1022,33 @@ public final class CameraCharacteristics extends CameraMetadata<CameraCharacteri
/**
* <p>Position of the camera optical center.</p>
- * <p>As measured in the device sensor coordinate system, the
- * position of the camera device's optical center, as a
- * three-dimensional vector <code>(x,y,z)</code>.</p>
- * <p>To transform a world position to a camera-device centered
- * coordinate system, the position must be translated by this
- * vector and then rotated by {@link CameraCharacteristics#LENS_POSE_ROTATION android.lens.poseRotation}.</p>
+ * <p>The position of the camera device's lens optical center,
+ * as a three-dimensional vector <code>(x,y,z)</code>, relative to the
+ * optical center of the largest camera device facing in the
+ * same direction as this camera, in the {@link android.hardware.SensorEvent Android sensor coordinate
+ * axes}. Note that only the axis definitions are shared with
+ * the sensor coordinate system, but not the origin.</p>
+ * <p>If this device is the largest or only camera device with a
+ * given facing, then this position will be <code>(0, 0, 0)</code>; a
+ * camera device with a lens optical center located 3 cm from
+ * the main sensor along the +X axis (to the right from the
+ * user's perspective) will report <code>(0.03, 0, 0)</code>.</p>
+ * <p>To transform a pixel coordinates between two cameras
+ * facing the same direction, first the source camera
+ * android.lens.radialDistortion must be corrected for. Then
+ * the source camera android.lens.intrinsicCalibration needs
+ * to be applied, followed by the {@link CameraCharacteristics#LENS_POSE_ROTATION android.lens.poseRotation}
+ * of the source camera, the translation of the source camera
+ * relative to the destination camera, the
+ * {@link CameraCharacteristics#LENS_POSE_ROTATION android.lens.poseRotation} of the destination camera, and
+ * finally the inverse of android.lens.intrinsicCalibration
+ * of the destination camera. This obtains a
+ * radial-distortion-free coordinate in the destination
+ * camera pixel coordinates.</p>
+ * <p>To compare this against a real image from the destination
+ * camera, the destination camera image then needs to be
+ * corrected for radial distortion before comparison or
+ * sampling.</p>
* <p><b>Units</b>: Meters</p>
* <p><b>Optional</b> - This value may be {@code null} on some devices.</p>
*
diff --git a/core/java/android/hardware/camera2/CameraManager.java b/core/java/android/hardware/camera2/CameraManager.java
index 0f002a9..0fb6889 100644
--- a/core/java/android/hardware/camera2/CameraManager.java
+++ b/core/java/android/hardware/camera2/CameraManager.java
@@ -802,12 +802,9 @@ public final class CameraManager {
*/
public ICameraService getCameraService() {
synchronized(mLock) {
+ connectCameraServiceLocked();
if (mCameraService == null) {
- Log.i(TAG, "getCameraService: Reconnecting to camera service");
- connectCameraServiceLocked();
- if (mCameraService == null) {
- Log.e(TAG, "Camera service is unavailable");
- }
+ Log.e(TAG, "Camera service is unavailable");
}
return mCameraService;
}
@@ -815,11 +812,16 @@ public final class CameraManager {
/**
* Connect to the camera service if it's available, and set up listeners.
+ * If the service is already connected, do nothing.
*
* <p>Sets mCameraService to a valid pointer or null if the connection does not succeed.</p>
*/
private void connectCameraServiceLocked() {
- mCameraService = null;
+ // Only reconnect if necessary
+ if (mCameraService != null) return;
+
+ Log.i(TAG, "Connecting to camera service");
+
IBinder cameraServiceBinder = ServiceManager.getService(CAMERA_SERVICE_BINDER_NAME);
if (cameraServiceBinder == null) {
// Camera service is now down, leave mCameraService as null
@@ -1098,6 +1100,8 @@ public final class CameraManager {
*/
public void registerAvailabilityCallback(AvailabilityCallback callback, Handler handler) {
synchronized (mLock) {
+ connectCameraServiceLocked();
+
Handler oldHandler = mCallbackMap.put(callback, handler);
// For new callbacks, provide initial availability information
if (oldHandler == null) {
@@ -1120,6 +1124,8 @@ public final class CameraManager {
public void registerTorchCallback(TorchCallback callback, Handler handler) {
synchronized(mLock) {
+ connectCameraServiceLocked();
+
Handler oldHandler = mTorchCallbackMap.put(callback, handler);
// For new callbacks, provide initial torch information
if (oldHandler == null) {
diff --git a/core/java/android/hardware/camera2/CaptureResult.java b/core/java/android/hardware/camera2/CaptureResult.java
index df6c986..3bb2fdb 100644
--- a/core/java/android/hardware/camera2/CaptureResult.java
+++ b/core/java/android/hardware/camera2/CaptureResult.java
@@ -2584,12 +2584,33 @@ public class CaptureResult extends CameraMetadata<CaptureResult.Key<?>> {
/**
* <p>Position of the camera optical center.</p>
- * <p>As measured in the device sensor coordinate system, the
- * position of the camera device's optical center, as a
- * three-dimensional vector <code>(x,y,z)</code>.</p>
- * <p>To transform a world position to a camera-device centered
- * coordinate system, the position must be translated by this
- * vector and then rotated by {@link CameraCharacteristics#LENS_POSE_ROTATION android.lens.poseRotation}.</p>
+ * <p>The position of the camera device's lens optical center,
+ * as a three-dimensional vector <code>(x,y,z)</code>, relative to the
+ * optical center of the largest camera device facing in the
+ * same direction as this camera, in the {@link android.hardware.SensorEvent Android sensor coordinate
+ * axes}. Note that only the axis definitions are shared with
+ * the sensor coordinate system, but not the origin.</p>
+ * <p>If this device is the largest or only camera device with a
+ * given facing, then this position will be <code>(0, 0, 0)</code>; a
+ * camera device with a lens optical center located 3 cm from
+ * the main sensor along the +X axis (to the right from the
+ * user's perspective) will report <code>(0.03, 0, 0)</code>.</p>
+ * <p>To transform a pixel coordinates between two cameras
+ * facing the same direction, first the source camera
+ * android.lens.radialDistortion must be corrected for. Then
+ * the source camera android.lens.intrinsicCalibration needs
+ * to be applied, followed by the {@link CameraCharacteristics#LENS_POSE_ROTATION android.lens.poseRotation}
+ * of the source camera, the translation of the source camera
+ * relative to the destination camera, the
+ * {@link CameraCharacteristics#LENS_POSE_ROTATION android.lens.poseRotation} of the destination camera, and
+ * finally the inverse of android.lens.intrinsicCalibration
+ * of the destination camera. This obtains a
+ * radial-distortion-free coordinate in the destination
+ * camera pixel coordinates.</p>
+ * <p>To compare this against a real image from the destination
+ * camera, the destination camera image then needs to be
+ * corrected for radial distortion before comparison or
+ * sampling.</p>
* <p><b>Units</b>: Meters</p>
* <p><b>Optional</b> - This value may be {@code null} on some devices.</p>
*
diff --git a/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java b/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java
index 16701e5..ad0cd0f 100644
--- a/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java
+++ b/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java
@@ -906,7 +906,6 @@ public class CameraDeviceImpl extends CameraDevice {
}
mRemoteDevice = null;
- mInError = false;
}
}
@@ -1889,13 +1888,13 @@ public class CameraDeviceImpl extends CameraDevice {
}
private void checkIfCameraClosedOrInError() throws CameraAccessException {
+ if (mRemoteDevice == null) {
+ throw new IllegalStateException("CameraDevice was already closed");
+ }
if (mInError) {
throw new CameraAccessException(CameraAccessException.CAMERA_ERROR,
"The camera device has encountered a serious error");
}
- if (mRemoteDevice == null) {
- throw new IllegalStateException("CameraDevice was already closed");
- }
}
/** Whether the camera device has started to close (may not yet have finished) */
diff --git a/core/java/android/os/DropBoxManager.java b/core/java/android/os/DropBoxManager.java
index 27001dc..2b14468 100644
--- a/core/java/android/os/DropBoxManager.java
+++ b/core/java/android/os/DropBoxManager.java
@@ -225,7 +225,8 @@ public class DropBoxManager {
if ((flags & HAS_BYTE_ARRAY) != 0) {
return new Entry(tag, millis, in.createByteArray(), flags & ~HAS_BYTE_ARRAY);
} else {
- return new Entry(tag, millis, in.readFileDescriptor(), flags);
+ ParcelFileDescriptor pfd = ParcelFileDescriptor.CREATOR.createFromParcel(in);
+ return new Entry(tag, millis, pfd, flags);
}
}
};
diff --git a/core/java/android/os/ParcelFileDescriptor.java b/core/java/android/os/ParcelFileDescriptor.java
index c24f3fe..1c9c713 100644
--- a/core/java/android/os/ParcelFileDescriptor.java
+++ b/core/java/android/os/ParcelFileDescriptor.java
@@ -915,8 +915,6 @@ public class ParcelFileDescriptor implements Parcelable, Closeable {
*/
@Override
public void writeToParcel(Parcel out, int flags) {
- // WARNING: This must stay in sync with Parcel::readParcelFileDescriptor()
- // in frameworks/native/libs/binder/Parcel.cpp
if (mWrapped != null) {
try {
mWrapped.writeToParcel(out, flags);
@@ -924,12 +922,13 @@ public class ParcelFileDescriptor implements Parcelable, Closeable {
releaseResources();
}
} else {
- out.writeFileDescriptor(mFd);
if (mCommFd != null) {
out.writeInt(1);
+ out.writeFileDescriptor(mFd);
out.writeFileDescriptor(mCommFd);
} else {
out.writeInt(0);
+ out.writeFileDescriptor(mFd);
}
if ((flags & PARCELABLE_WRITE_RETURN_VALUE) != 0 && !mClosed) {
// Not a real close, so emit no status
@@ -942,11 +941,10 @@ public class ParcelFileDescriptor implements Parcelable, Closeable {
= new Parcelable.Creator<ParcelFileDescriptor>() {
@Override
public ParcelFileDescriptor createFromParcel(Parcel in) {
- // WARNING: This must stay in sync with Parcel::writeParcelFileDescriptor()
- // in frameworks/native/libs/binder/Parcel.cpp
+ int hasCommChannel = in.readInt();
final FileDescriptor fd = in.readRawFileDescriptor();
FileDescriptor commChannel = null;
- if (in.readInt() != 0) {
+ if (hasCommChannel != 0) {
commChannel = in.readRawFileDescriptor();
}
return new ParcelFileDescriptor(fd, commChannel);
diff --git a/core/java/android/security/IKeystoreService.aidl b/core/java/android/security/IKeystoreService.aidl
index 2097d5a..409542d 100644
--- a/core/java/android/security/IKeystoreService.aidl
+++ b/core/java/android/security/IKeystoreService.aidl
@@ -67,7 +67,8 @@ interface IKeystoreService {
OperationResult begin(IBinder appToken, String alias, int purpose, boolean pruneable,
in KeymasterArguments params, in byte[] entropy);
OperationResult update(IBinder token, in KeymasterArguments params, in byte[] input);
- OperationResult finish(IBinder token, in KeymasterArguments params, in byte[] signature);
+ OperationResult finish(IBinder token, in KeymasterArguments params, in byte[] signature,
+ in byte[] entropy);
int abort(IBinder handle);
boolean isOperationAuthorized(IBinder token);
int addAuthToken(in byte[] authToken);
diff --git a/core/java/android/util/DisplayMetrics.java b/core/java/android/util/DisplayMetrics.java
index d0e5b9e..a36e66c 100644
--- a/core/java/android/util/DisplayMetrics.java
+++ b/core/java/android/util/DisplayMetrics.java
@@ -79,6 +79,14 @@ public class DisplayMetrics {
* This is not a density that applications should target, instead relying
* on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
*/
+ public static final int DENSITY_360 = 360;
+
+ /**
+ * Intermediate density for screens that sit somewhere between
+ * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
+ * This is not a density that applications should target, instead relying
+ * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
+ */
public static final int DENSITY_400 = 400;
/**
diff --git a/core/java/android/util/StateSet.java b/core/java/android/util/StateSet.java
index 83dfc47..c2a6a7a 100644
--- a/core/java/android/util/StateSet.java
+++ b/core/java/android/util/StateSet.java
@@ -119,7 +119,14 @@ public class StateSet {
/** @hide */
public StateSet() {}
+ /**
+ * A state specification that will be matched by all StateSets.
+ */
public static final int[] WILD_CARD = new int[0];
+
+ /**
+ * A state set that does not contain any valid states.
+ */
public static final int[] NOTHING = new int[] { 0 };
/**
diff --git a/core/java/android/view/WindowInsets.java b/core/java/android/view/WindowInsets.java
index 9e0719d..997e7e8 100644
--- a/core/java/android/view/WindowInsets.java
+++ b/core/java/android/view/WindowInsets.java
@@ -251,7 +251,7 @@ public final class WindowInsets {
* @return true if any inset values are nonzero
*/
public boolean hasInsets() {
- return hasSystemWindowInsets() || hasWindowDecorInsets();
+ return hasSystemWindowInsets() || hasWindowDecorInsets() || hasStableInsets();
}
/**
diff --git a/keystore/java/android/security/KeyStore.java b/keystore/java/android/security/KeyStore.java
index 37ed723..1a05104 100644
--- a/keystore/java/android/security/KeyStore.java
+++ b/keystore/java/android/security/KeyStore.java
@@ -514,15 +514,20 @@ public class KeyStore {
}
}
- public OperationResult finish(IBinder token, KeymasterArguments arguments, byte[] signature) {
+ public OperationResult finish(IBinder token, KeymasterArguments arguments, byte[] signature,
+ byte[] entropy) {
try {
- return mBinder.finish(token, arguments, signature);
+ return mBinder.finish(token, arguments, signature, entropy);
} catch (RemoteException e) {
Log.w(TAG, "Cannot connect to keystore", e);
return null;
}
}
+ public OperationResult finish(IBinder token, KeymasterArguments arguments, byte[] signature) {
+ return finish(token, arguments, signature, null);
+ }
+
public int abort(IBinder token) {
try {
return mBinder.abort(token);
diff --git a/keystore/java/android/security/keystore/AndroidKeyStoreBCWorkaroundProvider.java b/keystore/java/android/security/keystore/AndroidKeyStoreBCWorkaroundProvider.java
index aa2b946..e555cc0 100644
--- a/keystore/java/android/security/keystore/AndroidKeyStoreBCWorkaroundProvider.java
+++ b/keystore/java/android/security/keystore/AndroidKeyStoreBCWorkaroundProvider.java
@@ -122,6 +122,106 @@ class AndroidKeyStoreBCWorkaroundProvider extends Provider {
PACKAGE_NAME + ".AndroidKeyStoreRSACipherSpi$OAEPWithSHA512AndMGF1Padding");
put("Alg.Alias.Cipher.RSA/None/OAEPWithSHA-512AndMGF1Padding",
"RSA/ECB/OAEPWithSHA-512AndMGF1Padding");
+
+ // --------------------- java.security.Signature
+ putSignatureImpl("NONEwithRSA",
+ PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$NONEWithPKCS1Padding");
+
+ putSignatureImpl("MD5withRSA",
+ PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$MD5WithPKCS1Padding");
+ put("Alg.Alias.Signature.MD5WithRSAEncryption", "MD5WithRSA");
+ put("Alg.Alias.Signature.MD5/RSA", "MD5WithRSA");
+ put("Alg.Alias.Signature.1.2.840.113549.1.1.4", "MD5WithRSA");
+ put("Alg.Alias.Signature.1.2.840.113549.2.5with1.2.840.113549.1.1.1", "MD5WithRSA");
+
+ putSignatureImpl("SHA1withRSA",
+ PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$SHA1WithPKCS1Padding");
+ put("Alg.Alias.Signature.SHA1WithRSAEncryption", "SHA1WithRSA");
+ put("Alg.Alias.Signature.SHA1/RSA", "SHA1WithRSA");
+ put("Alg.Alias.Signature.SHA-1/RSA", "SHA1WithRSA");
+ put("Alg.Alias.Signature.1.2.840.113549.1.1.5", "SHA1WithRSA");
+ put("Alg.Alias.Signature.1.3.14.3.2.26with1.2.840.113549.1.1.1", "SHA1WithRSA");
+ put("Alg.Alias.Signature.1.3.14.3.2.26with1.2.840.113549.1.1.5", "SHA1WithRSA");
+ put("Alg.Alias.Signature.1.3.14.3.2.29", "SHA1WithRSA");
+
+ putSignatureImpl("SHA224withRSA",
+ PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$SHA224WithPKCS1Padding");
+ put("Alg.Alias.Signature.SHA224WithRSAEncryption", "SHA224WithRSA");
+ put("Alg.Alias.Signature.1.2.840.113549.1.1.11", "SHA224WithRSA");
+ put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.4with1.2.840.113549.1.1.1",
+ "SHA224WithRSA");
+ put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.4with1.2.840.113549.1.1.11",
+ "SHA224WithRSA");
+
+ putSignatureImpl("SHA256withRSA",
+ PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$SHA256WithPKCS1Padding");
+ put("Alg.Alias.Signature.SHA256WithRSAEncryption", "SHA256WithRSA");
+ put("Alg.Alias.Signature.1.2.840.113549.1.1.11", "SHA256WithRSA");
+ put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.1with1.2.840.113549.1.1.1",
+ "SHA256WithRSA");
+ put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.1with1.2.840.113549.1.1.11",
+ "SHA256WithRSA");
+
+ putSignatureImpl("SHA384withRSA",
+ PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$SHA384WithPKCS1Padding");
+ put("Alg.Alias.Signature.SHA384WithRSAEncryption", "SHA384WithRSA");
+ put("Alg.Alias.Signature.1.2.840.113549.1.1.12", "SHA384WithRSA");
+ put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.2with1.2.840.113549.1.1.1",
+ "SHA384WithRSA");
+
+ putSignatureImpl("SHA512withRSA",
+ PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$SHA512WithPKCS1Padding");
+ put("Alg.Alias.Signature.SHA512WithRSAEncryption", "SHA512WithRSA");
+ put("Alg.Alias.Signature.1.2.840.113549.1.1.13", "SHA512WithRSA");
+ put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.3with1.2.840.113549.1.1.1",
+ "SHA512WithRSA");
+
+ putSignatureImpl("SHA1withRSA/PSS",
+ PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$SHA1WithPSSPadding");
+ putSignatureImpl("SHA224withRSA/PSS",
+ PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$SHA224WithPSSPadding");
+ putSignatureImpl("SHA256withRSA/PSS",
+ PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$SHA256WithPSSPadding");
+ putSignatureImpl("SHA384withRSA/PSS",
+ PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$SHA384WithPSSPadding");
+ putSignatureImpl("SHA512withRSA/PSS",
+ PACKAGE_NAME + ".AndroidKeyStoreRSASignatureSpi$SHA512WithPSSPadding");
+
+ putSignatureImpl("NONEwithECDSA",
+ PACKAGE_NAME + ".AndroidKeyStoreECDSASignatureSpi$NONE");
+
+ putSignatureImpl("ECDSA", PACKAGE_NAME + ".AndroidKeyStoreECDSASignatureSpi$SHA1");
+ put("Alg.Alias.Signature.SHA1withECDSA", "ECDSA");
+ put("Alg.Alias.Signature.ECDSAwithSHA1", "ECDSA");
+ // iso(1) member-body(2) us(840) ansi-x962(10045) signatures(4) ecdsa-with-SHA1(1)
+ put("Alg.Alias.Signature.1.2.840.10045.4.1", "ECDSA");
+ put("Alg.Alias.Signature.1.3.14.3.2.26with1.2.840.10045.2.1", "ECDSA");
+
+ // iso(1) member-body(2) us(840) ansi-x962(10045) signatures(4) ecdsa-with-SHA2(3)
+ putSignatureImpl("SHA224withECDSA",
+ PACKAGE_NAME + ".AndroidKeyStoreECDSASignatureSpi$SHA224");
+ // ecdsa-with-SHA224(1)
+ put("Alg.Alias.Signature.1.2.840.10045.4.3.1", "SHA224withECDSA");
+ put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.4with1.2.840.10045.2.1", "SHA224withECDSA");
+
+ // iso(1) member-body(2) us(840) ansi-x962(10045) signatures(4) ecdsa-with-SHA2(3)
+ putSignatureImpl("SHA256withECDSA",
+ PACKAGE_NAME + ".AndroidKeyStoreECDSASignatureSpi$SHA256");
+ // ecdsa-with-SHA256(2)
+ put("Alg.Alias.Signature.1.2.840.10045.4.3.2", "SHA256withECDSA");
+ put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.1with1.2.840.10045.2.1", "SHA256withECDSA");
+
+ putSignatureImpl("SHA384withECDSA",
+ PACKAGE_NAME + ".AndroidKeyStoreECDSASignatureSpi$SHA384");
+ // ecdsa-with-SHA384(3)
+ put("Alg.Alias.Signature.1.2.840.10045.4.3.3", "SHA384withECDSA");
+ put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.2with1.2.840.10045.2.1", "SHA384withECDSA");
+
+ putSignatureImpl("SHA512withECDSA",
+ PACKAGE_NAME + ".AndroidKeyStoreECDSASignatureSpi$SHA512");
+ // ecdsa-with-SHA512(4)
+ put("Alg.Alias.Signature.1.2.840.10045.4.3.4", "SHA512withECDSA");
+ put("Alg.Alias.Signature.2.16.840.1.101.3.4.2.3with1.2.840.10045.2.1", "SHA512withECDSA");
}
private void putMacImpl(String algorithm, String implClass) {
@@ -139,4 +239,10 @@ class AndroidKeyStoreBCWorkaroundProvider extends Provider {
put("Cipher." + transformation + " SupportedKeyClasses",
KEYSTORE_PRIVATE_KEY_CLASS_NAME + "|" + KEYSTORE_PUBLIC_KEY_CLASS_NAME);
}
+
+ private void putSignatureImpl(String algorithm, String implClass) {
+ put("Signature." + algorithm, implClass);
+ put("Signature." + algorithm + " SupportedKeyClasses",
+ KEYSTORE_PRIVATE_KEY_CLASS_NAME + "|" + KEYSTORE_PUBLIC_KEY_CLASS_NAME);
+ }
}
diff --git a/keystore/java/android/security/keystore/AndroidKeyStoreECDSASignatureSpi.java b/keystore/java/android/security/keystore/AndroidKeyStoreECDSASignatureSpi.java
new file mode 100644
index 0000000..335da07
--- /dev/null
+++ b/keystore/java/android/security/keystore/AndroidKeyStoreECDSASignatureSpi.java
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2015 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 android.security.keystore;
+
+import android.annotation.NonNull;
+import android.security.KeyStore;
+import android.security.keymaster.KeyCharacteristics;
+import android.security.keymaster.KeymasterArguments;
+import android.security.keymaster.KeymasterDefs;
+
+import java.security.InvalidKeyException;
+import java.security.SignatureSpi;
+
+/**
+ * Base class for {@link SignatureSpi} providing Android KeyStore backed ECDSA signatures.
+ *
+ * @hide
+ */
+abstract class AndroidKeyStoreECDSASignatureSpi extends AndroidKeyStoreSignatureSpiBase {
+
+ public final static class NONE extends AndroidKeyStoreECDSASignatureSpi {
+ public NONE() {
+ super(KeymasterDefs.KM_DIGEST_NONE);
+ }
+ }
+
+ public final static class SHA1 extends AndroidKeyStoreECDSASignatureSpi {
+ public SHA1() {
+ super(KeymasterDefs.KM_DIGEST_SHA1);
+ }
+ }
+
+ public final static class SHA224 extends AndroidKeyStoreECDSASignatureSpi {
+ public SHA224() {
+ super(KeymasterDefs.KM_DIGEST_SHA_2_224);
+ }
+ }
+
+ public final static class SHA256 extends AndroidKeyStoreECDSASignatureSpi {
+ public SHA256() {
+ super(KeymasterDefs.KM_DIGEST_SHA_2_256);
+ }
+ }
+
+ public final static class SHA384 extends AndroidKeyStoreECDSASignatureSpi {
+ public SHA384() {
+ super(KeymasterDefs.KM_DIGEST_SHA_2_384);
+ }
+ }
+
+ public final static class SHA512 extends AndroidKeyStoreECDSASignatureSpi {
+ public SHA512() {
+ super(KeymasterDefs.KM_DIGEST_SHA_2_512);
+ }
+ }
+
+ private final int mKeymasterDigest;
+
+ private int mGroupSizeBytes = -1;
+
+ AndroidKeyStoreECDSASignatureSpi(int keymasterDigest) {
+ mKeymasterDigest = keymasterDigest;
+ }
+
+ @Override
+ protected final void initKey(AndroidKeyStoreKey key) throws InvalidKeyException {
+ if (!KeyProperties.KEY_ALGORITHM_EC.equalsIgnoreCase(key.getAlgorithm())) {
+ throw new InvalidKeyException("Unsupported key algorithm: " + key.getAlgorithm()
+ + ". Only" + KeyProperties.KEY_ALGORITHM_EC + " supported");
+ }
+
+ KeyCharacteristics keyCharacteristics = new KeyCharacteristics();
+ int errorCode = getKeyStore().getKeyCharacteristics(
+ key.getAlias(), null, null, keyCharacteristics);
+ if (errorCode != KeyStore.NO_ERROR) {
+ throw getKeyStore().getInvalidKeyException(key.getAlias(), errorCode);
+ }
+ int keySizeBits = keyCharacteristics.getInt(KeymasterDefs.KM_TAG_KEY_SIZE, -1);
+ if (keySizeBits == -1) {
+ throw new InvalidKeyException("Size of key not known");
+ }
+ mGroupSizeBytes = (keySizeBits + 7) / 8;
+
+ super.initKey(key);
+ }
+
+ @Override
+ protected final void resetAll() {
+ mGroupSizeBytes = -1;
+ super.resetAll();
+ }
+
+ @Override
+ protected final void resetWhilePreservingInitState() {
+ super.resetWhilePreservingInitState();
+ }
+
+ @Override
+ protected void addAlgorithmSpecificParametersToBegin(
+ @NonNull KeymasterArguments keymasterArgs) {
+ keymasterArgs.addInt(KeymasterDefs.KM_TAG_ALGORITHM, KeymasterDefs.KM_ALGORITHM_EC);
+ keymasterArgs.addInt(KeymasterDefs.KM_TAG_DIGEST, mKeymasterDigest);
+ }
+
+ @Override
+ protected int getAdditionalEntropyAmountForBegin() {
+ return (isSigning()) ? mGroupSizeBytes : 0;
+ }
+}
diff --git a/keystore/java/android/security/keystore/AndroidKeyStoreECPublicKey.java b/keystore/java/android/security/keystore/AndroidKeyStoreECPublicKey.java
new file mode 100644
index 0000000..3ed396d
--- /dev/null
+++ b/keystore/java/android/security/keystore/AndroidKeyStoreECPublicKey.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2015 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 android.security.keystore;
+
+import java.security.interfaces.ECPublicKey;
+import java.security.spec.ECParameterSpec;
+import java.security.spec.ECPoint;
+
+/**
+ * {@link ECPublicKey} backed by keystore.
+ *
+ * @hide
+ */
+public class AndroidKeyStoreECPublicKey extends AndroidKeyStorePublicKey implements ECPublicKey {
+
+ private final ECParameterSpec mParams;
+ private final ECPoint mW;
+
+ public AndroidKeyStoreECPublicKey(String alias, byte[] x509EncodedForm, ECParameterSpec params,
+ ECPoint w) {
+ super(alias, KeyProperties.KEY_ALGORITHM_EC, x509EncodedForm);
+ mParams = params;
+ mW = w;
+ }
+
+ public AndroidKeyStoreECPublicKey(String alias, ECPublicKey info) {
+ this(alias, info.getEncoded(), info.getParams(), info.getW());
+ if (!"X.509".equalsIgnoreCase(info.getFormat())) {
+ throw new IllegalArgumentException(
+ "Unsupported key export format: " + info.getFormat());
+ }
+ }
+
+ @Override
+ public ECParameterSpec getParams() {
+ return mParams;
+ }
+
+ @Override
+ public ECPoint getW() {
+ return mW;
+ }
+} \ No newline at end of file
diff --git a/keystore/java/android/security/keystore/AndroidKeyStoreKeyGeneratorSpi.java b/keystore/java/android/security/keystore/AndroidKeyStoreKeyGeneratorSpi.java
index dc4c8a3..4d6178f 100644
--- a/keystore/java/android/security/keystore/AndroidKeyStoreKeyGeneratorSpi.java
+++ b/keystore/java/android/security/keystore/AndroidKeyStoreKeyGeneratorSpi.java
@@ -296,19 +296,33 @@ public abstract class AndroidKeyStoreKeyGeneratorSpi extends KeyGeneratorSpi {
int flags = 0;
String keyAliasInKeystore = Credentials.USER_SECRET_KEY + spec.getKeystoreAlias();
KeyCharacteristics resultingKeyCharacteristics = new KeyCharacteristics();
- int errorCode = mKeyStore.generateKey(
- keyAliasInKeystore, args, additionalEntropy, flags, resultingKeyCharacteristics);
- if (errorCode != KeyStore.NO_ERROR) {
- throw new ProviderException(
- "Keystore operation failed", KeyStore.getKeyStoreException(errorCode));
- }
- @KeyProperties.KeyAlgorithmEnum String keyAlgorithmJCA;
+ boolean success = false;
try {
- keyAlgorithmJCA = KeyProperties.KeyAlgorithm.fromKeymasterSecretKeyAlgorithm(
- mKeymasterAlgorithm, mKeymasterDigest);
- } catch (IllegalArgumentException e) {
- throw new ProviderException("Failed to obtain JCA secret key algorithm name", e);
+ Credentials.deleteAllTypesForAlias(mKeyStore, spec.getKeystoreAlias());
+ int errorCode = mKeyStore.generateKey(
+ keyAliasInKeystore,
+ args,
+ additionalEntropy,
+ flags,
+ resultingKeyCharacteristics);
+ if (errorCode != KeyStore.NO_ERROR) {
+ throw new ProviderException(
+ "Keystore operation failed", KeyStore.getKeyStoreException(errorCode));
+ }
+ @KeyProperties.KeyAlgorithmEnum String keyAlgorithmJCA;
+ try {
+ keyAlgorithmJCA = KeyProperties.KeyAlgorithm.fromKeymasterSecretKeyAlgorithm(
+ mKeymasterAlgorithm, mKeymasterDigest);
+ } catch (IllegalArgumentException e) {
+ throw new ProviderException("Failed to obtain JCA secret key algorithm name", e);
+ }
+ SecretKey result = new AndroidKeyStoreSecretKey(keyAliasInKeystore, keyAlgorithmJCA);
+ success = true;
+ return result;
+ } finally {
+ if (!success) {
+ Credentials.deleteAllTypesForAlias(mKeyStore, spec.getKeystoreAlias());
+ }
}
- return new AndroidKeyStoreSecretKey(keyAliasInKeystore, keyAlgorithmJCA);
}
}
diff --git a/keystore/java/android/security/keystore/AndroidKeyStoreKeyPairGeneratorSpi.java b/keystore/java/android/security/keystore/AndroidKeyStoreKeyPairGeneratorSpi.java
index 4b45fd7..7b5ca3a 100644
--- a/keystore/java/android/security/keystore/AndroidKeyStoreKeyPairGeneratorSpi.java
+++ b/keystore/java/android/security/keystore/AndroidKeyStoreKeyPairGeneratorSpi.java
@@ -121,7 +121,6 @@ public abstract class AndroidKeyStoreKeyPairGeneratorSpi extends KeyPairGenerato
public KeyPair generateKeyPair() {
if (mKeyStore == null || mSpec == null) {
throw new IllegalStateException("Not initialized");
-
}
final int flags = (mEncryptionAtRestRequired) ? KeyStore.FLAG_ENCRYPTED : 0;
@@ -134,62 +133,65 @@ public abstract class AndroidKeyStoreKeyPairGeneratorSpi extends KeyPairGenerato
final String alias = mSpec.getKeystoreAlias();
- Credentials.deleteAllTypesForAlias(mKeyStore, alias);
-
byte[][] args = getArgsForKeyType(mKeyType, mSpec.getAlgorithmParameterSpec());
final String privateKeyAlias = Credentials.USER_PRIVATE_KEY + alias;
- if (!mKeyStore.generate(privateKeyAlias, KeyStore.UID_SELF, mKeyType, mKeySize,
- flags, args)) {
- throw new IllegalStateException("could not generate key in keystore");
- }
+ boolean success = false;
+ try {
+ Credentials.deleteAllTypesForAlias(mKeyStore, alias);
+ if (!mKeyStore.generate(privateKeyAlias, KeyStore.UID_SELF, mKeyType, mKeySize,
+ flags, args)) {
+ throw new IllegalStateException("could not generate key in keystore");
+ }
- Credentials.deleteSecretKeyTypeForAlias(mKeyStore, alias);
+ final PrivateKey privKey;
+ final OpenSSLEngine engine = OpenSSLEngine.getInstance("keystore");
+ try {
+ privKey = engine.getPrivateKeyById(privateKeyAlias);
+ } catch (InvalidKeyException e) {
+ throw new RuntimeException("Can't get key", e);
+ }
- final PrivateKey privKey;
- final OpenSSLEngine engine = OpenSSLEngine.getInstance("keystore");
- try {
- privKey = engine.getPrivateKeyById(privateKeyAlias);
- } catch (InvalidKeyException e) {
- throw new RuntimeException("Can't get key", e);
- }
+ final byte[] pubKeyBytes = mKeyStore.getPubkey(privateKeyAlias);
- final byte[] pubKeyBytes = mKeyStore.getPubkey(privateKeyAlias);
+ final PublicKey pubKey;
+ try {
+ final KeyFactory keyFact = KeyFactory.getInstance(mKeyAlgorithm);
+ pubKey = keyFact.generatePublic(new X509EncodedKeySpec(pubKeyBytes));
+ } catch (NoSuchAlgorithmException e) {
+ throw new IllegalStateException("Can't instantiate key generator", e);
+ } catch (InvalidKeySpecException e) {
+ throw new IllegalStateException("keystore returned invalid key encoding", e);
+ }
- final PublicKey pubKey;
- try {
- final KeyFactory keyFact = KeyFactory.getInstance(mKeyAlgorithm);
- pubKey = keyFact.generatePublic(new X509EncodedKeySpec(pubKeyBytes));
- } catch (NoSuchAlgorithmException e) {
- throw new IllegalStateException("Can't instantiate key generator", e);
- } catch (InvalidKeySpecException e) {
- throw new IllegalStateException("keystore returned invalid key encoding", e);
- }
+ final X509Certificate cert;
+ try {
+ cert = generateCertificate(privKey, pubKey);
+ } catch (Exception e) {
+ throw new IllegalStateException("Can't generate certificate", e);
+ }
- final X509Certificate cert;
- try {
- cert = generateCertificate(privKey, pubKey);
- } catch (Exception e) {
- Credentials.deleteAllTypesForAlias(mKeyStore, alias);
- throw new IllegalStateException("Can't generate certificate", e);
- }
+ byte[] certBytes;
+ try {
+ certBytes = cert.getEncoded();
+ } catch (CertificateEncodingException e) {
+ throw new IllegalStateException("Can't get encoding of certificate", e);
+ }
- byte[] certBytes;
- try {
- certBytes = cert.getEncoded();
- } catch (CertificateEncodingException e) {
- Credentials.deleteAllTypesForAlias(mKeyStore, alias);
- throw new IllegalStateException("Can't get encoding of certificate", e);
- }
+ if (!mKeyStore.put(Credentials.USER_CERTIFICATE + alias, certBytes, KeyStore.UID_SELF,
+ flags)) {
+ throw new IllegalStateException("Can't store certificate in AndroidKeyStore");
+ }
- if (!mKeyStore.put(Credentials.USER_CERTIFICATE + alias, certBytes, KeyStore.UID_SELF,
- flags)) {
- Credentials.deleteAllTypesForAlias(mKeyStore, alias);
- throw new IllegalStateException("Can't store certificate in AndroidKeyStore");
+ KeyPair result = new KeyPair(pubKey, privKey);
+ success = true;
+ return result;
+ } finally {
+ if (!success) {
+ Credentials.deleteAllTypesForAlias(mKeyStore, alias);
+ }
}
-
- return new KeyPair(pubKey, privKey);
}
@SuppressWarnings("deprecation")
diff --git a/keystore/java/android/security/keystore/AndroidKeyStoreProvider.java b/keystore/java/android/security/keystore/AndroidKeyStoreProvider.java
index 649a515..de4213e 100644
--- a/keystore/java/android/security/keystore/AndroidKeyStoreProvider.java
+++ b/keystore/java/android/security/keystore/AndroidKeyStoreProvider.java
@@ -20,6 +20,7 @@ import android.security.KeyStore;
import java.security.Provider;
import java.security.Security;
+import java.security.Signature;
import javax.crypto.Cipher;
import javax.crypto.Mac;
@@ -118,12 +119,15 @@ public class AndroidKeyStoreProvider extends Provider {
throw new NullPointerException();
}
Object spi;
- if (cryptoPrimitive instanceof Mac) {
+ if (cryptoPrimitive instanceof Signature) {
+ spi = ((Signature) cryptoPrimitive).getCurrentSpi();
+ } else if (cryptoPrimitive instanceof Mac) {
spi = ((Mac) cryptoPrimitive).getCurrentSpi();
} else if (cryptoPrimitive instanceof Cipher) {
spi = ((Cipher) cryptoPrimitive).getCurrentSpi();
} else {
- throw new IllegalArgumentException("Unsupported crypto primitive: " + cryptoPrimitive);
+ throw new IllegalArgumentException("Unsupported crypto primitive: " + cryptoPrimitive
+ + ". Supported: Signature, Mac, Cipher");
}
if (spi == null) {
throw new IllegalStateException("Crypto primitive not initialized");
diff --git a/keystore/java/android/security/keystore/AndroidKeyStoreRSAPublicKey.java b/keystore/java/android/security/keystore/AndroidKeyStoreRSAPublicKey.java
index 36bc997..08a173e 100644
--- a/keystore/java/android/security/keystore/AndroidKeyStoreRSAPublicKey.java
+++ b/keystore/java/android/security/keystore/AndroidKeyStoreRSAPublicKey.java
@@ -30,7 +30,7 @@ public class AndroidKeyStoreRSAPublicKey extends AndroidKeyStorePublicKey implem
public AndroidKeyStoreRSAPublicKey(String alias, byte[] x509EncodedForm, BigInteger modulus,
BigInteger publicExponent) {
- super(alias, "RSA", x509EncodedForm);
+ super(alias, KeyProperties.KEY_ALGORITHM_RSA, x509EncodedForm);
mModulus = modulus;
mPublicExponent = publicExponent;
}
diff --git a/keystore/java/android/security/keystore/AndroidKeyStoreRSASignatureSpi.java b/keystore/java/android/security/keystore/AndroidKeyStoreRSASignatureSpi.java
new file mode 100644
index 0000000..898336d
--- /dev/null
+++ b/keystore/java/android/security/keystore/AndroidKeyStoreRSASignatureSpi.java
@@ -0,0 +1,164 @@
+/*
+ * Copyright (C) 2015 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 android.security.keystore;
+
+import android.annotation.NonNull;
+import android.security.keymaster.KeymasterArguments;
+import android.security.keymaster.KeymasterDefs;
+
+import java.security.InvalidKeyException;
+import java.security.SignatureSpi;
+
+/**
+ * Base class for {@link SignatureSpi} providing Android KeyStore backed RSA signatures.
+ *
+ * @hide
+ */
+abstract class AndroidKeyStoreRSASignatureSpi extends AndroidKeyStoreSignatureSpiBase {
+
+ abstract static class PKCS1Padding extends AndroidKeyStoreRSASignatureSpi {
+ PKCS1Padding(int keymasterDigest) {
+ super(keymasterDigest, KeymasterDefs.KM_PAD_RSA_PKCS1_1_5_SIGN);
+ }
+
+ @Override
+ protected final int getAdditionalEntropyAmountForBegin() {
+ // No entropy required for this deterministic signature scheme.
+ return 0;
+ }
+ }
+
+ public static final class NONEWithPKCS1Padding extends PKCS1Padding {
+ public NONEWithPKCS1Padding() {
+ super(KeymasterDefs.KM_DIGEST_NONE);
+ }
+ }
+
+ public static final class MD5WithPKCS1Padding extends PKCS1Padding {
+ public MD5WithPKCS1Padding() {
+ super(KeymasterDefs.KM_DIGEST_MD5);
+ }
+ }
+
+ public static final class SHA1WithPKCS1Padding extends PKCS1Padding {
+ public SHA1WithPKCS1Padding() {
+ super(KeymasterDefs.KM_DIGEST_SHA1);
+ }
+ }
+
+ public static final class SHA224WithPKCS1Padding extends PKCS1Padding {
+ public SHA224WithPKCS1Padding() {
+ super(KeymasterDefs.KM_DIGEST_SHA_2_224);
+ }
+ }
+
+ public static final class SHA256WithPKCS1Padding extends PKCS1Padding {
+ public SHA256WithPKCS1Padding() {
+ super(KeymasterDefs.KM_DIGEST_SHA_2_256);
+ }
+ }
+
+ public static final class SHA384WithPKCS1Padding extends PKCS1Padding {
+ public SHA384WithPKCS1Padding() {
+ super(KeymasterDefs.KM_DIGEST_SHA_2_384);
+ }
+ }
+
+ public static final class SHA512WithPKCS1Padding extends PKCS1Padding {
+ public SHA512WithPKCS1Padding() {
+ super(KeymasterDefs.KM_DIGEST_SHA_2_512);
+ }
+ }
+
+ abstract static class PSSPadding extends AndroidKeyStoreRSASignatureSpi {
+ private static final int SALT_LENGTH_BYTES = 20;
+
+ PSSPadding(int keymasterDigest) {
+ super(keymasterDigest, KeymasterDefs.KM_PAD_RSA_PSS);
+ }
+
+ @Override
+ protected final int getAdditionalEntropyAmountForBegin() {
+ return (isSigning()) ? SALT_LENGTH_BYTES : 0;
+ }
+ }
+
+ public static final class SHA1WithPSSPadding extends PSSPadding {
+ public SHA1WithPSSPadding() {
+ super(KeymasterDefs.KM_DIGEST_SHA1);
+ }
+ }
+
+ public static final class SHA224WithPSSPadding extends PSSPadding {
+ public SHA224WithPSSPadding() {
+ super(KeymasterDefs.KM_DIGEST_SHA_2_224);
+ }
+ }
+
+ public static final class SHA256WithPSSPadding extends PSSPadding {
+ public SHA256WithPSSPadding() {
+ super(KeymasterDefs.KM_DIGEST_SHA_2_256);
+ }
+ }
+
+ public static final class SHA384WithPSSPadding extends PSSPadding {
+ public SHA384WithPSSPadding() {
+ super(KeymasterDefs.KM_DIGEST_SHA_2_384);
+ }
+ }
+
+ public static final class SHA512WithPSSPadding extends PSSPadding {
+ public SHA512WithPSSPadding() {
+ super(KeymasterDefs.KM_DIGEST_SHA_2_512);
+ }
+ }
+
+ private final int mKeymasterDigest;
+ private final int mKeymasterPadding;
+
+ AndroidKeyStoreRSASignatureSpi(int keymasterDigest, int keymasterPadding) {
+ mKeymasterDigest = keymasterDigest;
+ mKeymasterPadding = keymasterPadding;
+ }
+
+ @Override
+ protected final void initKey(AndroidKeyStoreKey key) throws InvalidKeyException {
+ if (!KeyProperties.KEY_ALGORITHM_RSA.equalsIgnoreCase(key.getAlgorithm())) {
+ throw new InvalidKeyException("Unsupported key algorithm: " + key.getAlgorithm()
+ + ". Only" + KeyProperties.KEY_ALGORITHM_RSA + " supported");
+ }
+ super.initKey(key);
+ }
+
+ @Override
+ protected final void resetAll() {
+ super.resetAll();
+ }
+
+ @Override
+ protected final void resetWhilePreservingInitState() {
+ super.resetWhilePreservingInitState();
+ }
+
+ @Override
+ protected final void addAlgorithmSpecificParametersToBegin(
+ @NonNull KeymasterArguments keymasterArgs) {
+ keymasterArgs.addInt(KeymasterDefs.KM_TAG_ALGORITHM, KeymasterDefs.KM_ALGORITHM_RSA);
+ keymasterArgs.addInt(KeymasterDefs.KM_TAG_DIGEST, mKeymasterDigest);
+ keymasterArgs.addInt(KeymasterDefs.KM_TAG_PADDING, mKeymasterPadding);
+ }
+}
diff --git a/keystore/java/android/security/keystore/AndroidKeyStoreSignatureSpiBase.java b/keystore/java/android/security/keystore/AndroidKeyStoreSignatureSpiBase.java
new file mode 100644
index 0000000..4c4062f
--- /dev/null
+++ b/keystore/java/android/security/keystore/AndroidKeyStoreSignatureSpiBase.java
@@ -0,0 +1,409 @@
+/*
+ * Copyright (C) 2015 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 android.security.keystore;
+
+import android.annotation.CallSuper;
+import android.annotation.NonNull;
+import android.os.IBinder;
+import android.security.KeyStore;
+import android.security.KeyStoreException;
+import android.security.keymaster.KeymasterArguments;
+import android.security.keymaster.KeymasterDefs;
+import android.security.keymaster.OperationResult;
+
+import com.android.org.conscrypt.util.EmptyArray;
+
+import java.nio.ByteBuffer;
+import java.security.InvalidKeyException;
+import java.security.InvalidParameterException;
+import java.security.PrivateKey;
+import java.security.ProviderException;
+import java.security.PublicKey;
+import java.security.SecureRandom;
+import java.security.SignatureException;
+import java.security.SignatureSpi;
+
+/**
+ * Base class for {@link SignatureSpi} implementations of Android KeyStore backed ciphers.
+ *
+ * @hide
+ */
+abstract class AndroidKeyStoreSignatureSpiBase extends SignatureSpi
+ implements KeyStoreCryptoOperation {
+ private final KeyStore mKeyStore;
+
+ // Fields below are populated by SignatureSpi.engineInitSign/engineInitVerify and KeyStore.begin
+ // and should be preserved after SignatureSpi.engineSign/engineVerify finishes.
+ private boolean mSigning;
+ private AndroidKeyStoreKey mKey;
+
+ /**
+ * Token referencing this operation inside keystore service. It is initialized by
+ * {@code engineInitSign}/{@code engineInitVerify} and is invalidated when
+ * {@code engineSign}/{@code engineVerify} succeeds and on some error conditions in between.
+ */
+ private IBinder mOperationToken;
+ private long mOperationHandle;
+ private KeyStoreCryptoOperationChunkedStreamer mMessageStreamer;
+
+ /**
+ * Encountered exception which could not be immediately thrown because it was encountered inside
+ * a method that does not throw checked exception. This exception will be thrown from
+ * {@code engineSign} or {@code engineVerify}. Once such an exception is encountered,
+ * {@code engineUpdate} starts ignoring input data.
+ */
+ private Exception mCachedException;
+
+ AndroidKeyStoreSignatureSpiBase() {
+ mKeyStore = KeyStore.getInstance();
+ }
+
+ @Override
+ protected final void engineInitSign(PrivateKey key) throws InvalidKeyException {
+ engineInitSign(key, null);
+ }
+
+ @Override
+ protected final void engineInitSign(PrivateKey privateKey, SecureRandom random)
+ throws InvalidKeyException {
+ resetAll();
+
+ boolean success = false;
+ try {
+ if (privateKey == null) {
+ throw new InvalidKeyException("Unsupported key: null");
+ }
+ AndroidKeyStoreKey keystoreKey;
+ if (privateKey instanceof AndroidKeyStorePrivateKey) {
+ keystoreKey = (AndroidKeyStoreKey) privateKey;
+ } else {
+ throw new InvalidKeyException("Unsupported private key type: " + privateKey);
+ }
+ mSigning = true;
+ initKey(keystoreKey);
+ appRandom = random;
+ ensureKeystoreOperationInitialized();
+ success = true;
+ } finally {
+ if (!success) {
+ resetAll();
+ }
+ }
+ }
+
+ @Override
+ protected final void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
+ resetAll();
+
+ boolean success = false;
+ try {
+ if (publicKey == null) {
+ throw new InvalidKeyException("Unsupported key: null");
+ }
+ AndroidKeyStoreKey keystoreKey;
+ if (publicKey instanceof AndroidKeyStorePublicKey) {
+ keystoreKey = (AndroidKeyStorePublicKey) publicKey;
+ } else {
+ throw new InvalidKeyException("Unsupported public key type: " + publicKey);
+ }
+ mSigning = false;
+ initKey(keystoreKey);
+ appRandom = null;
+ ensureKeystoreOperationInitialized();
+ success = true;
+ } finally {
+ if (!success) {
+ resetAll();
+ }
+ }
+ }
+
+ /**
+ * Configures this signature instance to use the provided key.
+ *
+ * @throws InvalidKeyException if the {@code key} is not suitable.
+ */
+ @CallSuper
+ protected void initKey(AndroidKeyStoreKey key) throws InvalidKeyException {
+ mKey = key;
+ }
+
+ /**
+ * Resets this cipher to its pristine pre-init state. This must be equivalent to obtaining a new
+ * cipher instance.
+ *
+ * <p>Subclasses storing additional state should override this method, reset the additional
+ * state, and then chain to superclass.
+ */
+ @CallSuper
+ protected void resetAll() {
+ IBinder operationToken = mOperationToken;
+ if (operationToken != null) {
+ mOperationToken = null;
+ mKeyStore.abort(operationToken);
+ }
+ mSigning = false;
+ mKey = null;
+ appRandom = null;
+ mOperationToken = null;
+ mOperationHandle = 0;
+ mMessageStreamer = null;
+ mCachedException = null;
+ }
+
+ /**
+ * Resets this cipher while preserving the initialized state. This must be equivalent to
+ * rolling back the cipher's state to just after the most recent {@code engineInit} completed
+ * successfully.
+ *
+ * <p>Subclasses storing additional post-init state should override this method, reset the
+ * additional state, and then chain to superclass.
+ */
+ @CallSuper
+ protected void resetWhilePreservingInitState() {
+ IBinder operationToken = mOperationToken;
+ if (operationToken != null) {
+ mOperationToken = null;
+ mKeyStore.abort(operationToken);
+ }
+ mOperationHandle = 0;
+ mMessageStreamer = null;
+ mCachedException = null;
+ }
+
+ private void ensureKeystoreOperationInitialized() throws InvalidKeyException {
+ if (mMessageStreamer != null) {
+ return;
+ }
+ if (mCachedException != null) {
+ return;
+ }
+ if (mKey == null) {
+ throw new IllegalStateException("Not initialized");
+ }
+
+ KeymasterArguments keymasterInputArgs = new KeymasterArguments();
+ addAlgorithmSpecificParametersToBegin(keymasterInputArgs);
+ byte[] additionalEntropy = KeyStoreCryptoOperationUtils.getRandomBytesToMixIntoKeystoreRng(
+ appRandom, getAdditionalEntropyAmountForBegin());
+
+ OperationResult opResult = mKeyStore.begin(
+ mKey.getAlias(),
+ mSigning ? KeymasterDefs.KM_PURPOSE_SIGN : KeymasterDefs.KM_PURPOSE_VERIFY,
+ true, // permit aborting this operation if keystore runs out of resources
+ keymasterInputArgs,
+ additionalEntropy);
+ if (opResult == null) {
+ throw new KeyStoreConnectException();
+ }
+
+ // Store operation token and handle regardless of the error code returned by KeyStore to
+ // ensure that the operation gets aborted immediately if the code below throws an exception.
+ mOperationToken = opResult.token;
+ mOperationHandle = opResult.operationHandle;
+
+ // If necessary, throw an exception due to KeyStore operation having failed.
+ InvalidKeyException e = KeyStoreCryptoOperationUtils.getInvalidKeyExceptionForInit(
+ mKeyStore, mKey, opResult.resultCode);
+ if (e != null) {
+ throw e;
+ }
+
+ if (mOperationToken == null) {
+ throw new ProviderException("Keystore returned null operation token");
+ }
+ if (mOperationHandle == 0) {
+ throw new ProviderException("Keystore returned invalid operation handle");
+ }
+
+ mMessageStreamer = new KeyStoreCryptoOperationChunkedStreamer(
+ new KeyStoreCryptoOperationChunkedStreamer.MainDataStream(
+ mKeyStore, opResult.token));
+ }
+
+ @Override
+ public final long getOperationHandle() {
+ return mOperationHandle;
+ }
+
+ @Override
+ protected final void engineUpdate(byte[] b, int off, int len) throws SignatureException {
+ if (mCachedException != null) {
+ throw new SignatureException(mCachedException);
+ }
+
+ try {
+ ensureKeystoreOperationInitialized();
+ } catch (InvalidKeyException e) {
+ throw new SignatureException(e);
+ }
+
+ if (len == 0) {
+ return;
+ }
+
+ byte[] output;
+ try {
+ output = mMessageStreamer.update(b, off, len);
+ } catch (KeyStoreException e) {
+ throw new SignatureException(e);
+ }
+
+ if (output.length != 0) {
+ throw new ProviderException(
+ "Update operation unexpectedly produced output: " + output.length + " bytes");
+ }
+ }
+
+ @Override
+ protected final void engineUpdate(byte b) throws SignatureException {
+ engineUpdate(new byte[] {b}, 0, 1);
+ }
+
+ @Override
+ protected final void engineUpdate(ByteBuffer input) {
+ byte[] b;
+ int off;
+ int len = input.remaining();
+ if (input.hasArray()) {
+ b = input.array();
+ off = input.arrayOffset() + input.position();
+ input.position(input.limit());
+ } else {
+ b = new byte[len];
+ off = 0;
+ input.get(b);
+ }
+
+ try {
+ engineUpdate(b, off, len);
+ } catch (SignatureException e) {
+ mCachedException = e;
+ }
+ }
+
+ @Override
+ protected final int engineSign(byte[] out, int outOffset, int outLen)
+ throws SignatureException {
+ return super.engineSign(out, outOffset, outLen);
+ }
+
+ @Override
+ protected final byte[] engineSign() throws SignatureException {
+ if (mCachedException != null) {
+ throw new SignatureException(mCachedException);
+ }
+
+ byte[] signature;
+ try {
+ ensureKeystoreOperationInitialized();
+ signature = mMessageStreamer.doFinal(EmptyArray.BYTE, 0, 0);
+ } catch (InvalidKeyException | KeyStoreException e) {
+ throw new SignatureException(e);
+ }
+
+ resetWhilePreservingInitState();
+ return signature;
+ }
+
+ @Override
+ protected final boolean engineVerify(byte[] signature) throws SignatureException {
+ if (mCachedException != null) {
+ throw new SignatureException(mCachedException);
+ }
+
+ boolean result;
+ try {
+ ensureKeystoreOperationInitialized();
+ mMessageStreamer.flush();
+ OperationResult opResult = mKeyStore.finish(mOperationToken, null, signature);
+ if (opResult == null) {
+ throw new KeyStoreConnectException();
+ }
+ switch (opResult.resultCode) {
+ case KeyStore.NO_ERROR:
+ result = true;
+ break;
+ case KeymasterDefs.KM_ERROR_VERIFICATION_FAILED:
+ result = false;
+ break;
+ default:
+ throw new SignatureException(
+ KeyStore.getKeyStoreException(opResult.resultCode));
+ }
+ } catch (InvalidKeyException | KeyStoreException e) {
+ throw new SignatureException(e);
+ }
+
+ resetWhilePreservingInitState();
+ return result;
+ }
+
+ @Override
+ protected final boolean engineVerify(byte[] sigBytes, int offset, int len)
+ throws SignatureException {
+ return engineVerify(ArrayUtils.subarray(sigBytes, offset, len));
+ }
+
+ @Deprecated
+ @Override
+ protected final Object engineGetParameter(String param) throws InvalidParameterException {
+ throw new InvalidParameterException();
+ }
+
+ @Deprecated
+ @Override
+ protected final void engineSetParameter(String param, Object value)
+ throws InvalidParameterException {
+ throw new InvalidParameterException();
+ }
+
+ protected final KeyStore getKeyStore() {
+ return mKeyStore;
+ }
+
+ /**
+ * Returns {@code true} if this signature is initialized for signing, {@code false} if this
+ * signature is initialized for verification.
+ */
+ protected final boolean isSigning() {
+ return mSigning;
+ }
+
+ // The methods below need to be implemented by subclasses.
+
+ /**
+ * Returns the amount of additional entropy (in bytes) to be provided to the KeyStore's
+ * {@code begin} operation.
+ *
+ * <p>For signature verification, this should be {@code 0} because verification should not be
+ * consuming any entropy. For signature generation, this value should match (or exceed) the
+ * amount of Shannon entropy of the produced signature assuming the key and the message are
+ * known. For example, for ECDSA signature this should be the size of {@code R}, whereas for the
+ * RSA signature with PKCS#1 padding this should be {@code 0}.
+ */
+ protected abstract int getAdditionalEntropyAmountForBegin();
+
+ /**
+ * Invoked to add algorithm-specific parameters for the KeyStore's {@code begin} operation.
+ *
+ * @param keymasterArgs keystore/keymaster arguments to be populated with algorithm-specific
+ * parameters.
+ */
+ protected abstract void addAlgorithmSpecificParametersToBegin(
+ @NonNull KeymasterArguments keymasterArgs);
+}
diff --git a/keystore/java/android/security/keystore/AndroidKeyStoreSpi.java b/keystore/java/android/security/keystore/AndroidKeyStoreSpi.java
index 701bd67..05ddef6 100644
--- a/keystore/java/android/security/keystore/AndroidKeyStoreSpi.java
+++ b/keystore/java/android/security/keystore/AndroidKeyStoreSpi.java
@@ -674,12 +674,13 @@ public class AndroidKeyStoreSpi extends KeyStoreSpi {
@Override
public void engineDeleteEntry(String alias) throws KeyStoreException {
- if (!isKeyEntry(alias) && !isCertificateEntry(alias)) {
+ if (!engineContainsAlias(alias)) {
return;
}
+ // At least one entry corresponding to this alias exists in keystore
if (!Credentials.deleteAllTypesForAlias(mKeyStore, alias)) {
- throw new KeyStoreException("No such entry " + alias);
+ throw new KeyStoreException("Failed to delete entry: " + alias);
}
}
@@ -849,9 +850,7 @@ public class AndroidKeyStoreSpi extends KeyStoreSpi {
throw new KeyStoreException("entry == null");
}
- if (engineContainsAlias(alias)) {
- engineDeleteEntry(alias);
- }
+ Credentials.deleteAllTypesForAlias(mKeyStore, alias);
if (entry instanceof KeyStore.TrustedCertificateEntry) {
KeyStore.TrustedCertificateEntry trE = (KeyStore.TrustedCertificateEntry) entry;
diff --git a/media/java/android/media/Ringtone.java b/media/java/android/media/Ringtone.java
index 8441541..166ff38 100644
--- a/media/java/android/media/Ringtone.java
+++ b/media/java/android/media/Ringtone.java
@@ -21,6 +21,7 @@ import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources.NotFoundException;
import android.database.Cursor;
+import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Binder;
import android.os.RemoteException;
@@ -29,6 +30,7 @@ import android.provider.Settings;
import android.util.Log;
import java.io.IOException;
+import java.util.ArrayList;
/**
* Ringtone provides a quick method for playing a ringtone, notification, or
@@ -49,6 +51,9 @@ public class Ringtone {
MediaStore.Audio.Media.TITLE
};
+ // keep references on active Ringtones until stopped or completion listener called.
+ private static final ArrayList<Ringtone> sActiveRingtones = new ArrayList<Ringtone>();
+
private final Context mContext;
private final AudioManager mAudioManager;
@@ -62,6 +67,7 @@ public class Ringtone {
private final Binder mRemoteToken;
private MediaPlayer mLocalPlayer;
+ private final MyOnCompletionListener mCompletionListener = new MyOnCompletionListener();
private Uri mUri;
private String mTitle;
@@ -247,7 +253,7 @@ public class Ringtone {
// (typically because ringer mode is silent).
if (mAudioManager.getStreamVolume(
AudioAttributes.toLegacyStreamType(mAudioAttributes)) != 0) {
- mLocalPlayer.start();
+ startLocalPlayer();
}
} else if (mAllowRemote && (mRemotePlayer != null)) {
final Uri canonicalUri = mUri.getCanonicalUri();
@@ -285,7 +291,21 @@ public class Ringtone {
mLocalPlayer.reset();
mLocalPlayer.release();
mLocalPlayer = null;
+ synchronized (sActiveRingtones) {
+ sActiveRingtones.remove(this);
+ }
+ }
+ }
+
+ private void startLocalPlayer() {
+ if (mLocalPlayer == null) {
+ return;
}
+ synchronized (sActiveRingtones) {
+ sActiveRingtones.add(this);
+ }
+ mLocalPlayer.setOnCompletionListener(mCompletionListener);
+ mLocalPlayer.start();
}
/**
@@ -330,7 +350,7 @@ public class Ringtone {
}
mLocalPlayer.setAudioAttributes(mAudioAttributes);
mLocalPlayer.prepare();
- mLocalPlayer.start();
+ startLocalPlayer();
afd.close();
return true;
} else {
@@ -352,4 +372,20 @@ public class Ringtone {
void setTitle(String title) {
mTitle = title;
}
+
+ @Override
+ protected void finalize() {
+ if (mLocalPlayer != null) {
+ mLocalPlayer.release();
+ }
+ }
+
+ class MyOnCompletionListener implements MediaPlayer.OnCompletionListener {
+ public void onCompletion(MediaPlayer mp)
+ {
+ synchronized (sActiveRingtones) {
+ sActiveRingtones.remove(Ringtone.this);
+ }
+ }
+ }
}
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java
index d3e7104..9a2f71c 100755
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java
@@ -348,8 +348,12 @@ public final class BluetoothEventManager {
Log.e(TAG, "ACTION_PAIRING_CANCEL with no EXTRA_DEVICE");
return;
}
- int errorMsg = R.string.bluetooth_pairing_error_message;
CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device);
+ if (cachedDevice == null) {
+ Log.e(TAG, "ACTION_PAIRING_CANCEL with no cached device");
+ return;
+ }
+ int errorMsg = R.string.bluetooth_pairing_error_message;
Utils.showError(context, cachedDevice.getName(), errorMsg);
}
}
diff --git a/packages/SystemUI/res/layout/recents_task_view_header.xml b/packages/SystemUI/res/layout/recents_task_view_header.xml
index 53047a3..477d9d7 100644
--- a/packages/SystemUI/res/layout/recents_task_view_header.xml
+++ b/packages/SystemUI/res/layout/recents_task_view_header.xml
@@ -21,6 +21,7 @@
android:layout_gravity="top|center_horizontal">
<com.android.systemui.recents.views.FixedSizeImageView
android:id="@+id/application_icon"
+ android:contentDescription="@string/recents_app_info_button_label"
android:layout_width="@dimen/recents_task_view_application_icon_size"
android:layout_height="@dimen/recents_task_view_application_icon_size"
android:layout_marginStart="8dp"
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/FlashlightTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/FlashlightTile.java
index 0369ab5..5d74604 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/FlashlightTile.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/FlashlightTile.java
@@ -74,7 +74,11 @@ public class FlashlightTile extends QSTile<QSTile.BooleanState> implements
state.visible = mFlashlightController.isAvailable();
state.label = mHost.getContext().getString(R.string.quick_settings_flashlight_label);
if (arg instanceof UserBoolean) {
- state.value = ((UserBoolean) arg).value;
+ boolean value = ((UserBoolean) arg).value;
+ if (value == state.value) {
+ return;
+ }
+ state.value = value;
}
final AnimationIcon icon = state.value ? mEnable : mDisable;
icon.setAllowAnimation(arg instanceof UserBoolean && ((UserBoolean) arg).userInitiated);
diff --git a/packages/SystemUI/src/com/android/systemui/recents/Recents.java b/packages/SystemUI/src/com/android/systemui/recents/Recents.java
index bbd3e60..7d2b5c87 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/Recents.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/Recents.java
@@ -32,6 +32,7 @@ import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
+import android.os.AsyncTask;
import android.os.Handler;
import android.os.SystemClock;
import android.os.UserHandle;
@@ -40,6 +41,7 @@ import android.util.Pair;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
+
import com.android.systemui.R;
import com.android.systemui.RecentsComponent;
import com.android.systemui.SystemUI;
@@ -184,12 +186,16 @@ public class Recents extends SystemUI
// Header (for transition)
TaskViewHeader mHeaderBar;
+ final Object mHeaderBarLock = new Object();
TaskStackView mDummyStackView;
// Variables to keep track of if we need to start recents after binding
boolean mTriggeredFromAltTab;
long mLastToggleTime;
+ Bitmap mThumbnailTransitionBitmapCache;
+ Task mThumbnailTransitionBitmapCacheKey;
+
public Recents() {
}
@@ -360,13 +366,16 @@ public class Recents extends SystemUI
void preloadRecentsInternal() {
// Preload only the raw task list into a new load plan (which will be consumed by the
// RecentsActivity)
+ ActivityManager.RunningTaskInfo topTask = mSystemServicesProxy.getTopMostTask();
+ MutableBoolean topTaskHome = new MutableBoolean(true);
RecentsTaskLoader loader = RecentsTaskLoader.getInstance();
sInstanceLoadPlan = loader.createLoadPlan(mContext);
-
- ActivityManager.RunningTaskInfo topTask = mSystemServicesProxy.getTopMostTask();
- MutableBoolean isTopTaskHome = new MutableBoolean(true);
- if (topTask != null && mSystemServicesProxy.isRecentsTopMost(topTask, isTopTaskHome)) {
- sInstanceLoadPlan.preloadRawTasks(isTopTaskHome.value);
+ if (topTask != null && !mSystemServicesProxy.isRecentsTopMost(topTask, topTaskHome)) {
+ sInstanceLoadPlan.preloadRawTasks(topTaskHome.value);
+ loader.preloadTasks(sInstanceLoadPlan, topTaskHome.value);
+ TaskStack top = sInstanceLoadPlan.getAllTaskStacks().get(0);
+ preCacheThumbnailTransitionBitmapAsync(topTask, top, mDummyStackView,
+ topTaskHome.value);
}
}
@@ -513,12 +522,14 @@ public class Recents extends SystemUI
algo.computeRects(mWindowRect.width(), mWindowRect.height(), taskStackBounds);
Rect taskViewSize = algo.getUntransformedTaskViewSize();
int taskBarHeight = res.getDimensionPixelSize(R.dimen.recents_task_bar_height);
- mHeaderBar = (TaskViewHeader) mInflater.inflate(R.layout.recents_task_view_header, null,
- false);
- mHeaderBar.measure(
- View.MeasureSpec.makeMeasureSpec(taskViewSize.width(), View.MeasureSpec.EXACTLY),
- View.MeasureSpec.makeMeasureSpec(taskBarHeight, View.MeasureSpec.EXACTLY));
- mHeaderBar.layout(0, 0, taskViewSize.width(), taskBarHeight);
+ synchronized (mHeaderBarLock) {
+ mHeaderBar = (TaskViewHeader) mInflater.inflate(R.layout.recents_task_view_header, null,
+ false);
+ mHeaderBar.measure(
+ View.MeasureSpec.makeMeasureSpec(taskViewSize.width(), View.MeasureSpec.EXACTLY),
+ View.MeasureSpec.makeMeasureSpec(taskBarHeight, View.MeasureSpec.EXACTLY));
+ mHeaderBar.layout(0, 0, taskViewSize.width(), taskBarHeight);
+ }
}
/** Prepares the search bar app widget */
@@ -607,30 +618,27 @@ public class Recents extends SystemUI
*/
ActivityOptions getThumbnailTransitionActivityOptions(ActivityManager.RunningTaskInfo topTask,
TaskStack stack, TaskStackView stackView) {
+
// Update the destination rect
Task toTask = new Task();
TaskViewTransform toTransform = getThumbnailTransitionTransform(stack, stackView,
topTask.id, toTask);
- if (toTransform != null && toTask.key != null) {
- Rect toTaskRect = toTransform.rect;
- int toHeaderWidth = (int) (mHeaderBar.getMeasuredWidth() * toTransform.scale);
- int toHeaderHeight = (int) (mHeaderBar.getMeasuredHeight() * toTransform.scale);
- Bitmap thumbnail = Bitmap.createBitmap(toHeaderWidth, toHeaderHeight,
- Bitmap.Config.ARGB_8888);
- if (Constants.DebugFlags.App.EnableTransitionThumbnailDebugMode) {
- thumbnail.eraseColor(0xFFff0000);
- } else {
- Canvas c = new Canvas(thumbnail);
- c.scale(toTransform.scale, toTransform.scale);
- mHeaderBar.rebindToTask(toTask);
- mHeaderBar.draw(c);
- c.setBitmap(null);
- }
- Bitmap thumbnailImmutable = thumbnail.createAshmemBitmap();
-
+ Rect toTaskRect = toTransform.rect;
+ Bitmap thumbnail;
+ if (mThumbnailTransitionBitmapCacheKey != null
+ && mThumbnailTransitionBitmapCacheKey.key != null
+ && mThumbnailTransitionBitmapCacheKey.key.equals(toTask.key)) {
+ thumbnail = mThumbnailTransitionBitmapCache;
+ mThumbnailTransitionBitmapCacheKey = null;
+ mThumbnailTransitionBitmapCache = null;
+ } else {
+ preloadIcon(topTask);
+ thumbnail = drawThumbnailTransitionBitmap(toTask, toTransform);
+ }
+ if (thumbnail != null) {
mStartAnimationTriggered = false;
return ActivityOptions.makeThumbnailAspectScaleDownAnimation(mDummyStackView,
- thumbnailImmutable, toTaskRect.left, toTaskRect.top, toTaskRect.width(),
+ thumbnail, toTaskRect.left, toTaskRect.top, toTaskRect.width(),
toTaskRect.height(), mHandler, this);
}
@@ -638,6 +646,72 @@ public class Recents extends SystemUI
return getUnknownTransitionActivityOptions();
}
+ /**
+ * Preloads the icon of a task.
+ */
+ void preloadIcon(ActivityManager.RunningTaskInfo task) {
+
+ // Ensure that we load the running task's icon
+ RecentsTaskLoadPlan.Options launchOpts = new RecentsTaskLoadPlan.Options();
+ launchOpts.runningTaskId = task.id;
+ launchOpts.loadThumbnails = false;
+ launchOpts.onlyLoadForCache = true;
+ RecentsTaskLoader.getInstance().loadTasks(mContext, sInstanceLoadPlan, launchOpts);
+ }
+
+ /**
+ * Caches the header thumbnail used for a window animation asynchronously into
+ * {@link #mThumbnailTransitionBitmapCache}.
+ */
+ void preCacheThumbnailTransitionBitmapAsync(ActivityManager.RunningTaskInfo topTask,
+ TaskStack stack, TaskStackView stackView, boolean isTopTaskHome) {
+ preloadIcon(topTask);
+
+ // Update the destination rect
+ mDummyStackView.updateMinMaxScrollForStack(stack, mTriggeredFromAltTab, isTopTaskHome);
+ final Task toTask = new Task();
+ final TaskViewTransform toTransform = getThumbnailTransitionTransform(stack, stackView,
+ topTask.id, toTask);
+ new AsyncTask<Void, Void, Bitmap>() {
+ @Override
+ protected Bitmap doInBackground(Void... params) {
+ return drawThumbnailTransitionBitmap(toTask, toTransform);
+ }
+
+ @Override
+ protected void onPostExecute(Bitmap bitmap) {
+ mThumbnailTransitionBitmapCache = bitmap;
+ mThumbnailTransitionBitmapCacheKey = toTask;
+ }
+ }.execute();
+ }
+
+ /**
+ * Draws the header of a task used for the window animation into a bitmap.
+ */
+ Bitmap drawThumbnailTransitionBitmap(Task toTask, TaskViewTransform toTransform) {
+ if (toTransform != null && toTask.key != null) {
+ Bitmap thumbnail;
+ synchronized (mHeaderBarLock) {
+ int toHeaderWidth = (int) (mHeaderBar.getMeasuredWidth() * toTransform.scale);
+ int toHeaderHeight = (int) (mHeaderBar.getMeasuredHeight() * toTransform.scale);
+ thumbnail = Bitmap.createBitmap(toHeaderWidth, toHeaderHeight,
+ Bitmap.Config.ARGB_8888);
+ if (Constants.DebugFlags.App.EnableTransitionThumbnailDebugMode) {
+ thumbnail.eraseColor(0xFFff0000);
+ } else {
+ Canvas c = new Canvas(thumbnail);
+ c.scale(toTransform.scale, toTransform.scale);
+ mHeaderBar.rebindToTask(toTask);
+ mHeaderBar.draw(c);
+ c.setBitmap(null);
+ }
+ }
+ return thumbnail.createAshmemBitmap();
+ }
+ return null;
+ }
+
/** Returns the transition rect for the given task id. */
TaskViewTransform getThumbnailTransitionTransform(TaskStack stack, TaskStackView stackView,
int runningTaskId, Task runningTaskOut) {
@@ -694,7 +768,9 @@ public class Recents extends SystemUI
return;
}
- loader.preloadTasks(sInstanceLoadPlan, isTopTaskHome);
+ if (!sInstanceLoadPlan.hasTasks()) {
+ loader.preloadTasks(sInstanceLoadPlan, isTopTaskHome);
+ }
ArrayList<TaskStack> stacks = sInstanceLoadPlan.getAllTaskStacks();
TaskStack stack = stacks.get(0);
@@ -706,12 +782,6 @@ public class Recents extends SystemUI
boolean useThumbnailTransition = (topTask != null) && !isTopTaskHome && hasRecentTasks;
if (useThumbnailTransition) {
- // Ensure that we load the running task's icon
- RecentsTaskLoadPlan.Options launchOpts = new RecentsTaskLoadPlan.Options();
- launchOpts.runningTaskId = topTask.id;
- launchOpts.loadThumbnails = false;
- launchOpts.onlyLoadForCache = true;
- loader.loadTasks(mContext, sInstanceLoadPlan, launchOpts);
// Try starting with a thumbnail transition
ActivityOptions opts = getThumbnailTransitionActivityOptions(topTask, stack,
diff --git a/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java b/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java
index ad97f91..9404dcb 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java
@@ -441,6 +441,10 @@ public class RecentsActivity extends Activity implements RecentsView.RecentsView
if (mConfig.launchedHasConfigurationChanged) {
onEnterAnimationTriggered();
}
+
+ if (!mConfig.launchedHasConfigurationChanged) {
+ mRecentsView.disableLayersForOneFrame();
+ }
}
@Override
diff --git a/packages/SystemUI/src/com/android/systemui/recents/views/RecentsView.java b/packages/SystemUI/src/com/android/systemui/recents/views/RecentsView.java
index cec613c..1a21d8a 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/views/RecentsView.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/views/RecentsView.java
@@ -431,6 +431,13 @@ public class RecentsView extends FrameLayout implements TaskStackView.TaskStackV
return false;
}
+ public void disableLayersForOneFrame() {
+ List<TaskStackView> stackViews = getTaskStackViews();
+ for (int i = 0; i < stackViews.size(); i++) {
+ stackViews.get(i).disableLayersForOneFrame();
+ }
+ }
+
/**** TaskStackView.TaskStackCallbacks Implementation ****/
@Override
diff --git a/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java b/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java
index 5f151e8..5711cd6 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java
@@ -19,6 +19,7 @@ package com.android.systemui.recents.views;
import android.animation.ValueAnimator;
import android.content.ComponentName;
import android.content.Context;
+import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.view.LayoutInflater;
@@ -63,7 +64,6 @@ public class TaskStackView extends FrameLayout implements TaskStack.TaskStackCal
public void onTaskResize(Task t);
}
-
RecentsConfiguration mConfig;
TaskStack mStack;
@@ -81,7 +81,6 @@ public class TaskStackView extends FrameLayout implements TaskStack.TaskStackCal
boolean mDismissAllButtonAnimating;
int mFocusedTaskIndex = -1;
int mPrevAccessibilityFocusedIndex = -1;
-
// Optimizations
int mStackViewsAnimationDuration;
boolean mStackViewsDirty = true;
@@ -99,6 +98,7 @@ public class TaskStackView extends FrameLayout implements TaskStack.TaskStackCal
ArrayList<TaskView> mTaskViews = new ArrayList<TaskView>();
List<TaskView> mImmutableTaskViews = new ArrayList<TaskView>();
LayoutInflater mInflater;
+ boolean mLayersDisabled;
// A convenience update listener to request updating clipping of tasks
ValueAnimator.AnimatorUpdateListener mRequestUpdateClippingListener =
@@ -375,7 +375,9 @@ public class TaskStackView extends FrameLayout implements TaskStack.TaskStackCal
if (tv == null) {
tv = mViewPool.pickUpViewFromPool(task, task);
-
+ if (mLayersDisabled) {
+ tv.disableLayersForOneFrame();
+ }
if (mStackViewsAnimationDuration > 0) {
// For items in the list, put them in start animating them from the
// approriate ends of the list where they are expected to appear
@@ -1031,6 +1033,20 @@ public class TaskStackView extends FrameLayout implements TaskStack.TaskStackCal
mUIDozeTrigger.poke();
}
+ @Override
+ protected void dispatchDraw(Canvas canvas) {
+ mLayersDisabled = false;
+ super.dispatchDraw(canvas);
+ }
+
+ public void disableLayersForOneFrame() {
+ mLayersDisabled = true;
+ List<TaskView> taskViews = getTaskViews();
+ for (int i = 0; i < taskViews.size(); i++) {
+ taskViews.get(i).disableLayersForOneFrame();
+ }
+ }
+
/**** TaskStackCallbacks Implementation ****/
@Override
diff --git a/packages/SystemUI/src/com/android/systemui/recents/views/TaskView.java b/packages/SystemUI/src/com/android/systemui/recents/views/TaskView.java
index 01ed08a..b0247cb 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/views/TaskView.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/views/TaskView.java
@@ -22,6 +22,7 @@ import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
+import android.view.accessibility.AccessibilityManager;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.view.animation.AccelerateInterpolator;
@@ -650,6 +651,10 @@ public class TaskView extends FrameLayout implements Task.TaskCallbacks,
}
}
+ public void disableLayersForOneFrame() {
+ mHeaderView.disableLayersForOneFrame();
+ }
+
/**** TaskCallbacks Implementation ****/
/** Binds this task view to the task */
@@ -672,7 +677,11 @@ public class TaskView extends FrameLayout implements Task.TaskCallbacks,
mThumbnailView.rebindToTask(mTask);
mHeaderView.rebindToTask(mTask);
// Rebind any listeners
- mHeaderView.mApplicationIcon.setOnClickListener(this);
+ AccessibilityManager am = (AccessibilityManager) getContext().
+ getSystemService(Context.ACCESSIBILITY_SERVICE);
+ if (Constants.DebugFlags.App.EnableTaskFiltering || (am != null && am.isEnabled())) {
+ mHeaderView.mApplicationIcon.setOnClickListener(this);
+ }
mHeaderView.mDismissButton.setOnClickListener(this);
if (mConfig.multiStackEnabled) {
mHeaderView.mMoveTaskButton.setOnClickListener(this);
@@ -718,9 +727,19 @@ public class TaskView extends FrameLayout implements Task.TaskCallbacks,
postDelayed(new Runnable() {
@Override
public void run() {
- if (Constants.DebugFlags.App.EnableTaskFiltering && v == mHeaderView.mApplicationIcon) {
- if (mCb != null) {
- mCb.onTaskViewAppIconClicked(tv);
+ if (v == mHeaderView.mApplicationIcon) {
+ if (Constants.DebugFlags.App.EnableTaskFiltering) {
+ if (mCb != null) {
+ mCb.onTaskViewAppIconClicked(tv);
+ }
+ } else {
+ AccessibilityManager am = (AccessibilityManager) getContext().
+ getSystemService(Context.ACCESSIBILITY_SERVICE);
+ if (am != null && am.isEnabled()) {
+ if (mCb != null) {
+ mCb.onTaskViewAppInfoClicked(tv);
+ }
+ }
}
} else if (v == mHeaderView.mDismissButton) {
dismissTask();
diff --git a/packages/SystemUI/src/com/android/systemui/recents/views/TaskViewHeader.java b/packages/SystemUI/src/com/android/systemui/recents/views/TaskViewHeader.java
index f397bc3..1a22cae 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/views/TaskViewHeader.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/views/TaskViewHeader.java
@@ -80,6 +80,8 @@ public class TaskViewHeader extends FrameLayout {
Paint mDimLayerPaint = new Paint();
PorterDuffColorFilter mDimColorFilter = new PorterDuffColorFilter(0, PorterDuff.Mode.SRC_ATOP);
+ boolean mLayersDisabled;
+
public TaskViewHeader(Context context) {
this(context, null);
}
@@ -172,7 +174,9 @@ public class TaskViewHeader extends FrameLayout {
void setDimAlpha(int alpha) {
mDimColorFilter.setColor(Color.argb(alpha, 0, 0, 0));
mDimLayerPaint.setColorFilter(mDimColorFilter);
- setLayerType(LAYER_TYPE_HARDWARE, mDimLayerPaint);
+ if (!mLayersDisabled) {
+ setLayerType(LAYER_TYPE_HARDWARE, mDimLayerPaint);
+ }
}
/** Returns the secondary color for a primary color. */
@@ -190,7 +194,6 @@ public class TaskViewHeader extends FrameLayout {
} else if (t.applicationIcon != null) {
mApplicationIcon.setImageDrawable(t.applicationIcon);
}
- mApplicationIcon.setContentDescription(t.contentDescription);
if (!mActivityDescription.getText().toString().equals(t.activityLabel)) {
mActivityDescription.setText(t.activityLabel);
}
@@ -305,6 +308,28 @@ public class TaskViewHeader extends FrameLayout {
return new int[] {};
}
+ @Override
+ protected void dispatchDraw(Canvas canvas) {
+ super.dispatchDraw(canvas);
+ if (mLayersDisabled) {
+ mLayersDisabled = false;
+ postOnAnimation(new Runnable() {
+ @Override
+ public void run() {
+ mLayersDisabled = false;
+ setLayerType(LAYER_TYPE_HARDWARE, mDimLayerPaint);
+ }
+ });
+ }
+ }
+
+ public void disableLayersForOneFrame() {
+ mLayersDisabled = true;
+
+ // Disable layer for a frame so we can draw our first frame faster.
+ setLayerType(LAYER_TYPE_NONE, null);
+ }
+
/** Notifies the associated TaskView has been focused. */
void onTaskViewFocusChanged(boolean focused, boolean animateFocusedState) {
// If we are not animating the visible state, just return
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
index fa172a4..f05ac1a 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
@@ -1030,9 +1030,7 @@ public abstract class BaseStatusBar extends SystemUI implements
@Override
public void toggleRecentApps() {
- int msg = MSG_TOGGLE_RECENTS_APPS;
- mHandler.removeMessages(msg);
- mHandler.sendEmptyMessage(msg);
+ toggleRecents();
}
@Override
diff --git a/rs/java/android/renderscript/Allocation.java b/rs/java/android/renderscript/Allocation.java
index bea138e..0a50593 100644
--- a/rs/java/android/renderscript/Allocation.java
+++ b/rs/java/android/renderscript/Allocation.java
@@ -51,6 +51,7 @@ import android.os.Trace;
* <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
* </div>
**/
+
public class Allocation extends BaseObj {
Type mType;
Bitmap mBitmap;
@@ -455,28 +456,31 @@ public class Allocation extends BaseObj {
*
*/
public void syncAll(int srcLocation) {
- Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
- switch (srcLocation) {
- case USAGE_GRAPHICS_TEXTURE:
- case USAGE_SCRIPT:
- if ((mUsage & USAGE_SHARED) != 0) {
- copyFrom(mBitmap);
- }
- break;
- case USAGE_GRAPHICS_CONSTANTS:
- case USAGE_GRAPHICS_VERTEX:
- break;
- case USAGE_SHARED:
- if ((mUsage & USAGE_SHARED) != 0) {
- copyTo(mBitmap);
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
+ switch (srcLocation) {
+ case USAGE_GRAPHICS_TEXTURE:
+ case USAGE_SCRIPT:
+ if ((mUsage & USAGE_SHARED) != 0) {
+ copyFrom(mBitmap);
+ }
+ break;
+ case USAGE_GRAPHICS_CONSTANTS:
+ case USAGE_GRAPHICS_VERTEX:
+ break;
+ case USAGE_SHARED:
+ if ((mUsage & USAGE_SHARED) != 0) {
+ copyTo(mBitmap);
+ }
+ break;
+ default:
+ throw new RSIllegalArgumentException("Source must be exactly one usage type.");
}
- break;
- default:
- throw new RSIllegalArgumentException("Source must be exactly one usage type.");
+ mRS.validate();
+ mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
}
- mRS.validate();
- mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
- Trace.traceEnd(RenderScript.TRACE_TAG);
}
/**
@@ -487,14 +491,17 @@ public class Allocation extends BaseObj {
*
*/
public void ioSend() {
- Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
- if ((mUsage & USAGE_IO_OUTPUT) == 0) {
- throw new RSIllegalArgumentException(
- "Can only send buffer if IO_OUTPUT usage specified.");
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
+ if ((mUsage & USAGE_IO_OUTPUT) == 0) {
+ throw new RSIllegalArgumentException(
+ "Can only send buffer if IO_OUTPUT usage specified.");
+ }
+ mRS.validate();
+ mRS.nAllocationIoSend(getID(mRS));
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
}
- mRS.validate();
- mRS.nAllocationIoSend(getID(mRS));
- Trace.traceEnd(RenderScript.TRACE_TAG);
}
/**
@@ -503,14 +510,17 @@ public class Allocation extends BaseObj {
*
*/
public void ioReceive() {
- Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
- if ((mUsage & USAGE_IO_INPUT) == 0) {
- throw new RSIllegalArgumentException(
- "Can only receive if IO_INPUT usage specified.");
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
+ if ((mUsage & USAGE_IO_INPUT) == 0) {
+ throw new RSIllegalArgumentException(
+ "Can only receive if IO_INPUT usage specified.");
+ }
+ mRS.validate();
+ mRS.nAllocationIoReceive(getID(mRS));
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
}
- mRS.validate();
- mRS.nAllocationIoReceive(getID(mRS));
- Trace.traceEnd(RenderScript.TRACE_TAG);
}
/**
@@ -519,28 +529,31 @@ public class Allocation extends BaseObj {
* @param d Source array.
*/
public void copyFrom(BaseObj[] d) {
- Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
- mRS.validate();
- validateIsObject();
- if (d.length != mCurrentCount) {
- throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
- mCurrentCount + ", array length = " + d.length);
- }
-
- if (RenderScript.sPointerSize == 8) {
- long i[] = new long[d.length * 4];
- for (int ct=0; ct < d.length; ct++) {
- i[ct * 4] = d[ct].getID(mRS);
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
+ mRS.validate();
+ validateIsObject();
+ if (d.length != mCurrentCount) {
+ throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
+ mCurrentCount + ", array length = " + d.length);
}
- copy1DRangeFromUnchecked(0, mCurrentCount, i);
- } else {
- int i[] = new int[d.length];
- for (int ct=0; ct < d.length; ct++) {
- i[ct] = (int)d[ct].getID(mRS);
+
+ if (RenderScript.sPointerSize == 8) {
+ long i[] = new long[d.length * 4];
+ for (int ct=0; ct < d.length; ct++) {
+ i[ct * 4] = d[ct].getID(mRS);
+ }
+ copy1DRangeFromUnchecked(0, mCurrentCount, i);
+ } else {
+ int i[] = new int[d.length];
+ for (int ct=0; ct < d.length; ct++) {
+ i[ct] = (int) d[ct].getID(mRS);
+ }
+ copy1DRangeFromUnchecked(0, mCurrentCount, i);
}
- copy1DRangeFromUnchecked(0, mCurrentCount, i);
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
}
- Trace.traceEnd(RenderScript.TRACE_TAG);
}
private void validateBitmapFormat(Bitmap b) {
@@ -599,16 +612,19 @@ public class Allocation extends BaseObj {
}
private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) {
- Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
- mRS.validate();
- if (mCurrentDimZ > 0) {
- copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen);
- } else if (mCurrentDimY > 0) {
- copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen);
- } else {
- copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen);
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
+ mRS.validate();
+ if (mCurrentDimZ > 0) {
+ copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen);
+ } else if (mCurrentDimY > 0) {
+ copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen);
+ } else {
+ copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen);
+ }
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
}
- Trace.traceEnd(RenderScript.TRACE_TAG);
}
/**
@@ -619,10 +635,13 @@ public class Allocation extends BaseObj {
* @param array The source data array
*/
public void copyFromUnchecked(Object array) {
- Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
- copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false),
- java.lang.reflect.Array.getLength(array));
- Trace.traceEnd(RenderScript.TRACE_TAG);
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
+ copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false),
+ java.lang.reflect.Array.getLength(array));
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
+ }
}
/**
@@ -679,10 +698,13 @@ public class Allocation extends BaseObj {
* @param array The source data array
*/
public void copyFrom(Object array) {
- Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
- copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true),
- java.lang.reflect.Array.getLength(array));
- Trace.traceEnd(RenderScript.TRACE_TAG);
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
+ copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true),
+ java.lang.reflect.Array.getLength(array));
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
+ }
}
/**
@@ -747,19 +769,22 @@ public class Allocation extends BaseObj {
* @param b the source bitmap
*/
public void copyFrom(Bitmap b) {
- Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
- mRS.validate();
- if (b.getConfig() == null) {
- Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
- Canvas c = new Canvas(newBitmap);
- c.drawBitmap(b, 0, 0, null);
- copyFrom(newBitmap);
- return;
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
+ mRS.validate();
+ if (b.getConfig() == null) {
+ Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
+ Canvas c = new Canvas(newBitmap);
+ c.drawBitmap(b, 0, 0, null);
+ copyFrom(newBitmap);
+ return;
+ }
+ validateBitmapSize(b);
+ validateBitmapFormat(b);
+ mRS.nAllocationCopyFromBitmap(getID(mRS), b);
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
}
- validateBitmapSize(b);
- validateBitmapFormat(b);
- mRS.nAllocationCopyFromBitmap(getID(mRS), b);
- Trace.traceEnd(RenderScript.TRACE_TAG);
}
/**
@@ -769,13 +794,16 @@ public class Allocation extends BaseObj {
* @param a the source allocation
*/
public void copyFrom(Allocation a) {
- Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
- mRS.validate();
- if (!mType.equals(a.getType())) {
- throw new RSIllegalArgumentException("Types of allocations must match.");
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
+ mRS.validate();
+ if (!mType.equals(a.getType())) {
+ throw new RSIllegalArgumentException("Types of allocations must match.");
+ }
+ copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
}
- copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
- Trace.traceEnd(RenderScript.TRACE_TAG);
}
/**
@@ -890,17 +918,20 @@ public class Allocation extends BaseObj {
private void copy1DRangeFromUnchecked(int off, int count, Object array,
Element.DataType dt, int arrayLen) {
- Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
- final int dataSize = mType.mElement.getBytesSize() * count;
- // AutoPadding for Vec3 Element
- boolean usePadding = false;
- if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
- usePadding = true;
- }
- data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
- mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
- mType.mElement.mType.mSize, usePadding);
- Trace.traceEnd(RenderScript.TRACE_TAG);
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
+ final int dataSize = mType.mElement.getBytesSize() * count;
+ // AutoPadding for Vec3 Element
+ boolean usePadding = false;
+ if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
+ usePadding = true;
+ }
+ data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
+ mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
+ mType.mElement.mType.mSize, usePadding);
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
+ }
}
/**
@@ -1074,28 +1105,31 @@ public class Allocation extends BaseObj {
void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array,
Element.DataType dt, int arrayLen) {
- Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
- mRS.validate();
- validate2DRange(xoff, yoff, w, h);
- final int dataSize = mType.mElement.getBytesSize() * w * h;
- // AutoPadding for Vec3 Element
- boolean usePadding = false;
- int sizeBytes = arrayLen * dt.mSize;
- if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
- if (dataSize / 4 * 3 > sizeBytes) {
- throw new RSIllegalArgumentException("Array too small for allocation type.");
- }
- usePadding = true;
- sizeBytes = dataSize;
- } else {
- if (dataSize > sizeBytes) {
- throw new RSIllegalArgumentException("Array too small for allocation type.");
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
+ mRS.validate();
+ validate2DRange(xoff, yoff, w, h);
+ final int dataSize = mType.mElement.getBytesSize() * w * h;
+ // AutoPadding for Vec3 Element
+ boolean usePadding = false;
+ int sizeBytes = arrayLen * dt.mSize;
+ if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
+ if (dataSize / 4 * 3 > sizeBytes) {
+ throw new RSIllegalArgumentException("Array too small for allocation type.");
+ }
+ usePadding = true;
+ sizeBytes = dataSize;
+ } else {
+ if (dataSize > sizeBytes) {
+ throw new RSIllegalArgumentException("Array too small for allocation type.");
+ }
}
+ mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
+ array, sizeBytes, dt,
+ mType.mElement.mType.mSize, usePadding);
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
}
- mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
- array, sizeBytes, dt,
- mType.mElement.mType.mSize, usePadding);
- Trace.traceEnd(RenderScript.TRACE_TAG);
}
/**
@@ -1109,11 +1143,14 @@ public class Allocation extends BaseObj {
* @param array Data to be placed into the Allocation
*/
public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) {
- Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
- copy2DRangeFromUnchecked(xoff, yoff, w, h, array,
- validateObjectIsPrimitiveArray(array, true),
- java.lang.reflect.Array.getLength(array));
- Trace.traceEnd(RenderScript.TRACE_TAG);
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
+ copy2DRangeFromUnchecked(xoff, yoff, w, h, array,
+ validateObjectIsPrimitiveArray(array, true),
+ java.lang.reflect.Array.getLength(array));
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
+ }
}
/**
@@ -1194,14 +1231,17 @@ public class Allocation extends BaseObj {
*/
public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
Allocation data, int dataXoff, int dataYoff) {
- Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
- mRS.validate();
- validate2DRange(xoff, yoff, w, h);
- mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
- mSelectedLOD, mSelectedFace.mID,
- w, h, data.getID(mRS), dataXoff, dataYoff,
- data.mSelectedLOD, data.mSelectedFace.mID);
- Trace.traceEnd(RenderScript.TRACE_TAG);
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
+ mRS.validate();
+ validate2DRange(xoff, yoff, w, h);
+ mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
+ mSelectedLOD, mSelectedFace.mID,
+ w, h, data.getID(mRS), dataXoff, dataYoff,
+ data.mSelectedLOD, data.mSelectedFace.mID);
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
+ }
}
/**
@@ -1258,28 +1298,31 @@ public class Allocation extends BaseObj {
*/
private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
Object array, Element.DataType dt, int arrayLen) {
- Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFromUnchecked");
- mRS.validate();
- validate3DRange(xoff, yoff, zoff, w, h, d);
- final int dataSize = mType.mElement.getBytesSize() * w * h * d;
- // AutoPadding for Vec3 Element
- boolean usePadding = false;
- int sizeBytes = arrayLen * dt.mSize;
- if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
- if (dataSize / 4 * 3 > sizeBytes) {
- throw new RSIllegalArgumentException("Array too small for allocation type.");
- }
- usePadding = true;
- sizeBytes = dataSize;
- } else {
- if (dataSize > sizeBytes) {
- throw new RSIllegalArgumentException("Array too small for allocation type.");
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFromUnchecked");
+ mRS.validate();
+ validate3DRange(xoff, yoff, zoff, w, h, d);
+ final int dataSize = mType.mElement.getBytesSize() * w * h * d;
+ // AutoPadding for Vec3 Element
+ boolean usePadding = false;
+ int sizeBytes = arrayLen * dt.mSize;
+ if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
+ if (dataSize / 4 * 3 > sizeBytes) {
+ throw new RSIllegalArgumentException("Array too small for allocation type.");
+ }
+ usePadding = true;
+ sizeBytes = dataSize;
+ } else {
+ if (dataSize > sizeBytes) {
+ throw new RSIllegalArgumentException("Array too small for allocation type.");
+ }
}
+ mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
+ array, sizeBytes, dt,
+ mType.mElement.mType.mSize, usePadding);
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
}
- mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
- array, sizeBytes, dt,
- mType.mElement.mType.mSize, usePadding);
- Trace.traceEnd(RenderScript.TRACE_TAG);
}
/**
@@ -1295,11 +1338,14 @@ public class Allocation extends BaseObj {
* @param array to be placed into the allocation
*/
public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
- Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFrom");
- copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array,
- validateObjectIsPrimitiveArray(array, true),
- java.lang.reflect.Array.getLength(array));
- Trace.traceEnd(RenderScript.TRACE_TAG);
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFrom");
+ copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array,
+ validateObjectIsPrimitiveArray(array, true),
+ java.lang.reflect.Array.getLength(array));
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
+ }
}
/**
@@ -1334,34 +1380,40 @@ public class Allocation extends BaseObj {
* @param b The bitmap to be set from the Allocation.
*/
public void copyTo(Bitmap b) {
- Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
- mRS.validate();
- validateBitmapFormat(b);
- validateBitmapSize(b);
- mRS.nAllocationCopyToBitmap(getID(mRS), b);
- Trace.traceEnd(RenderScript.TRACE_TAG);
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
+ mRS.validate();
+ validateBitmapFormat(b);
+ validateBitmapSize(b);
+ mRS.nAllocationCopyToBitmap(getID(mRS), b);
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
+ }
}
private void copyTo(Object array, Element.DataType dt, int arrayLen) {
- Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
- mRS.validate();
- boolean usePadding = false;
- if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
- usePadding = true;
- }
- if (usePadding) {
- if (dt.mSize * arrayLen < mSize / 4 * 3) {
- throw new RSIllegalArgumentException(
- "Size of output array cannot be smaller than size of allocation.");
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
+ mRS.validate();
+ boolean usePadding = false;
+ if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
+ usePadding = true;
}
- } else {
- if (dt.mSize * arrayLen < mSize) {
- throw new RSIllegalArgumentException(
- "Size of output array cannot be smaller than size of allocation.");
+ if (usePadding) {
+ if (dt.mSize * arrayLen < mSize / 4 * 3) {
+ throw new RSIllegalArgumentException(
+ "Size of output array cannot be smaller than size of allocation.");
+ }
+ } else {
+ if (dt.mSize * arrayLen < mSize) {
+ throw new RSIllegalArgumentException(
+ "Size of output array cannot be smaller than size of allocation.");
+ }
}
+ mRS.nAllocationRead(getID(mRS), array, dt, mType.mElement.mType.mSize, usePadding);
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
}
- mRS.nAllocationRead(getID(mRS), array, dt, mType.mElement.mType.mSize, usePadding);
- Trace.traceEnd(RenderScript.TRACE_TAG);
}
/**
@@ -1499,17 +1551,20 @@ public class Allocation extends BaseObj {
private void copy1DRangeToUnchecked(int off, int count, Object array,
Element.DataType dt, int arrayLen) {
- Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeToUnchecked");
- final int dataSize = mType.mElement.getBytesSize() * count;
- // AutoPadding for Vec3 Element
- boolean usePadding = false;
- if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
- usePadding = true;
- }
- data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
- mRS.nAllocationRead1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
- mType.mElement.mType.mSize, usePadding);
- Trace.traceEnd(RenderScript.TRACE_TAG);
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeToUnchecked");
+ final int dataSize = mType.mElement.getBytesSize() * count;
+ // AutoPadding for Vec3 Element
+ boolean usePadding = false;
+ if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
+ usePadding = true;
+ }
+ data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
+ mRS.nAllocationRead1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
+ mType.mElement.mType.mSize, usePadding);
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
+ }
}
/**
@@ -1649,27 +1704,30 @@ public class Allocation extends BaseObj {
void copy2DRangeToUnchecked(int xoff, int yoff, int w, int h, Object array,
Element.DataType dt, int arrayLen) {
- Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeToUnchecked");
- mRS.validate();
- validate2DRange(xoff, yoff, w, h);
- final int dataSize = mType.mElement.getBytesSize() * w * h;
- // AutoPadding for Vec3 Element
- boolean usePadding = false;
- int sizeBytes = arrayLen * dt.mSize;
- if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
- if (dataSize / 4 * 3 > sizeBytes) {
- throw new RSIllegalArgumentException("Array too small for allocation type.");
- }
- usePadding = true;
- sizeBytes = dataSize;
- } else {
- if (dataSize > sizeBytes) {
- throw new RSIllegalArgumentException("Array too small for allocation type.");
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeToUnchecked");
+ mRS.validate();
+ validate2DRange(xoff, yoff, w, h);
+ final int dataSize = mType.mElement.getBytesSize() * w * h;
+ // AutoPadding for Vec3 Element
+ boolean usePadding = false;
+ int sizeBytes = arrayLen * dt.mSize;
+ if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
+ if (dataSize / 4 * 3 > sizeBytes) {
+ throw new RSIllegalArgumentException("Array too small for allocation type.");
+ }
+ usePadding = true;
+ sizeBytes = dataSize;
+ } else {
+ if (dataSize > sizeBytes) {
+ throw new RSIllegalArgumentException("Array too small for allocation type.");
+ }
}
+ mRS.nAllocationRead2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
+ array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
}
- mRS.nAllocationRead2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
- array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
- Trace.traceEnd(RenderScript.TRACE_TAG);
}
/**
@@ -1757,27 +1815,30 @@ public class Allocation extends BaseObj {
*/
private void copy3DRangeToUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
Object array, Element.DataType dt, int arrayLen) {
- Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeToUnchecked");
- mRS.validate();
- validate3DRange(xoff, yoff, zoff, w, h, d);
- final int dataSize = mType.mElement.getBytesSize() * w * h * d;
- // AutoPadding for Vec3 Element
- boolean usePadding = false;
- int sizeBytes = arrayLen * dt.mSize;
- if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
- if (dataSize / 4 * 3 > sizeBytes) {
- throw new RSIllegalArgumentException("Array too small for allocation type.");
- }
- usePadding = true;
- sizeBytes = dataSize;
- } else {
- if (dataSize > sizeBytes) {
- throw new RSIllegalArgumentException("Array too small for allocation type.");
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeToUnchecked");
+ mRS.validate();
+ validate3DRange(xoff, yoff, zoff, w, h, d);
+ final int dataSize = mType.mElement.getBytesSize() * w * h * d;
+ // AutoPadding for Vec3 Element
+ boolean usePadding = false;
+ int sizeBytes = arrayLen * dt.mSize;
+ if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
+ if (dataSize / 4 * 3 > sizeBytes) {
+ throw new RSIllegalArgumentException("Array too small for allocation type.");
+ }
+ usePadding = true;
+ sizeBytes = dataSize;
+ } else {
+ if (dataSize > sizeBytes) {
+ throw new RSIllegalArgumentException("Array too small for allocation type.");
+ }
}
+ mRS.nAllocationRead3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
+ array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
}
- mRS.nAllocationRead3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
- array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
- Trace.traceEnd(RenderScript.TRACE_TAG);
}
/*
@@ -1815,17 +1876,20 @@ public class Allocation extends BaseObj {
* utilized
*/
static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
- Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
- rs.validate();
- if (type.getID(rs) == 0) {
- throw new RSInvalidStateException("Bad Type");
- }
- long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
- if (id == 0) {
- throw new RSRuntimeException("Allocation creation failed.");
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
+ rs.validate();
+ if (type.getID(rs) == 0) {
+ throw new RSInvalidStateException("Bad Type");
+ }
+ long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
+ if (id == 0) {
+ throw new RSRuntimeException("Allocation creation failed.");
+ }
+ return new Allocation(id, rs, type, usage);
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
}
- Trace.traceEnd(RenderScript.TRACE_TAG);
- return new Allocation(id, rs, type, usage);
}
/**
@@ -1869,18 +1933,21 @@ public class Allocation extends BaseObj {
*/
static public Allocation createSized(RenderScript rs, Element e,
int count, int usage) {
- Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
- rs.validate();
- Type.Builder b = new Type.Builder(rs, e);
- b.setX(count);
- Type t = b.create();
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
+ rs.validate();
+ Type.Builder b = new Type.Builder(rs, e);
+ b.setX(count);
+ Type t = b.create();
- long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
- if (id == 0) {
- throw new RSRuntimeException("Allocation creation failed.");
+ long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
+ if (id == 0) {
+ throw new RSRuntimeException("Allocation creation failed.");
+ }
+ return new Allocation(id, rs, t, usage);
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
}
- Trace.traceEnd(RenderScript.TRACE_TAG);
- return new Allocation(id, rs, t, usage);
}
/**
@@ -1939,44 +2006,47 @@ public class Allocation extends BaseObj {
static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
MipmapControl mips,
int usage) {
- Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
- rs.validate();
+ try {
+ Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
+ rs.validate();
+
+ // WAR undocumented color formats
+ if (b.getConfig() == null) {
+ if ((usage & USAGE_SHARED) != 0) {
+ throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
+ }
+ Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
+ Canvas c = new Canvas(newBitmap);
+ c.drawBitmap(b, 0, 0, null);
+ return createFromBitmap(rs, newBitmap, mips, usage);
+ }
- // WAR undocumented color formats
- if (b.getConfig() == null) {
- if ((usage & USAGE_SHARED) != 0) {
- throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
+ Type t = typeFromBitmap(rs, b, mips);
+
+ // enable optimized bitmap path only with no mipmap and script-only usage
+ if (mips == MipmapControl.MIPMAP_NONE &&
+ t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
+ usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
+ long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
+ if (id == 0) {
+ throw new RSRuntimeException("Load failed.");
+ }
+
+ // keep a reference to the Bitmap around to prevent GC
+ Allocation alloc = new Allocation(id, rs, t, usage);
+ alloc.setBitmap(b);
+ return alloc;
}
- Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
- Canvas c = new Canvas(newBitmap);
- c.drawBitmap(b, 0, 0, null);
- return createFromBitmap(rs, newBitmap, mips, usage);
- }
- Type t = typeFromBitmap(rs, b, mips);
- // enable optimized bitmap path only with no mipmap and script-only usage
- if (mips == MipmapControl.MIPMAP_NONE &&
- t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
- usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
- long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
+ long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
if (id == 0) {
throw new RSRuntimeException("Load failed.");
}
-
- // keep a reference to the Bitmap around to prevent GC
- Allocation alloc = new Allocation(id, rs, t, usage);
- alloc.setBitmap(b);
- return alloc;
- }
-
-
- long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
- if (id == 0) {
- throw new RSRuntimeException("Load failed.");
+ return new Allocation(id, rs, t, usage);
+ } finally {
+ Trace.traceEnd(RenderScript.TRACE_TAG);
}
- Trace.traceEnd(RenderScript.TRACE_TAG);
- return new Allocation(id, rs, t, usage);
}
/**
diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java
index 928d1d6..44d00d7 100644
--- a/services/core/java/com/android/server/ConnectivityService.java
+++ b/services/core/java/com/android/server/ConnectivityService.java
@@ -4509,7 +4509,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
info.setDetailedState(DetailedState.CONNECTED, null, info.getExtraInfo());
sendConnectedBroadcast(info);
} else {
- info.setDetailedState(DetailedState.DISCONNECTED, null, info.getExtraInfo());
+ info.setDetailedState(DetailedState.DISCONNECTED, info.getReason(), info.getExtraInfo());
Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType());
diff --git a/services/core/java/com/android/server/InputMethodManagerService.java b/services/core/java/com/android/server/InputMethodManagerService.java
index 0513d8b..69405cf 100644
--- a/services/core/java/com/android/server/InputMethodManagerService.java
+++ b/services/core/java/com/android/server/InputMethodManagerService.java
@@ -428,19 +428,39 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
private final IPackageManager mIPackageManager;
class SettingsObserver extends ContentObserver {
+ int mUserId;
+ boolean mRegistered = false;
String mLastEnabled = "";
+ /**
+ * <em>This constructor must be called within the lock.</em>
+ */
SettingsObserver(Handler handler) {
super(handler);
+ }
+
+ public void registerContentObserverLocked(int userId) {
+ if (mRegistered && mUserId == userId) {
+ return;
+ }
ContentResolver resolver = mContext.getContentResolver();
+ if (mRegistered) {
+ mContext.getContentResolver().unregisterContentObserver(this);
+ mRegistered = false;
+ }
+ if (mUserId != userId) {
+ mLastEnabled = "";
+ mUserId = userId;
+ }
resolver.registerContentObserver(Settings.Secure.getUriFor(
- Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
+ Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
resolver.registerContentObserver(Settings.Secure.getUriFor(
- Settings.Secure.ENABLED_INPUT_METHODS), false, this);
+ Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
resolver.registerContentObserver(Settings.Secure.getUriFor(
- Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
+ Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
resolver.registerContentObserver(Settings.Secure.getUriFor(
- Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this);
+ Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
+ mRegistered = true;
}
@Override public void onChange(boolean selfChange, Uri uri) {
@@ -460,6 +480,12 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
}
}
}
+
+ @Override
+ public String toString() {
+ return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
+ + " mLastEnabled=" + mLastEnabled + "}";
+ }
}
class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
@@ -756,6 +782,8 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
mContext = context;
mRes = context.getResources();
mHandler = new Handler(this);
+ // Note: SettingsObserver doesn't register observers in its constructor.
+ mSettingsObserver = new SettingsObserver(mHandler);
mIWindowManager = IWindowManager.Stub.asInterface(
ServiceManager.getService(Context.WINDOW_SERVICE));
mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
@@ -860,8 +888,8 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
}
}
- mSettingsObserver = new SettingsObserver(mHandler);
synchronized (mMethodMap) {
+ mSettingsObserver.registerContentObserverLocked(userId);
updateFromSettingsLocked(true);
}
@@ -963,6 +991,8 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
+ " currentUserId=" + mSettings.getCurrentUserId());
+ // ContentObserver should be registered again when the user is changed
+ mSettingsObserver.registerContentObserverLocked(newUserId);
mSettings.setCurrentUserId(newUserId);
updateCurrentProfileIds();
// InputMethodFileManager should be reset when the user is changed
@@ -3713,6 +3743,7 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
p.println(" mCurUserActionNotificationSequenceNumber="
+ mCurUserActionNotificationSequenceNumber);
p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mScreenOn);
+ p.println(" mSettingsObserver=" + mSettingsObserver);
p.println(" mSwitchingController:");
mSwitchingController.dump(p);
}
diff --git a/services/core/java/com/android/server/MountService.java b/services/core/java/com/android/server/MountService.java
index d48953d..8c8be4e 100644
--- a/services/core/java/com/android/server/MountService.java
+++ b/services/core/java/com/android/server/MountService.java
@@ -180,6 +180,7 @@ class MountService extends IMountService.Stub
private static final String TAG_STORAGE_BENCHMARK = "storage_benchmark";
private static final String VOLD_TAG = "VoldConnector";
+ private static final String CRYPTD_TAG = "CryptdConnector";
/** Maximum number of ASEC containers allowed to be mounted. */
private static final int MAX_CONTAINERS = 250;
@@ -367,6 +368,7 @@ class MountService extends IMountService.Stub
private final Context mContext;
private final NativeDaemonConnector mConnector;
+ private final NativeDaemonConnector mCryptConnector;
private volatile boolean mSystemReady = false;
private volatile boolean mDaemonConnected = false;
@@ -375,7 +377,8 @@ class MountService extends IMountService.Stub
private final Callbacks mCallbacks;
- private final CountDownLatch mConnectedSignal = new CountDownLatch(1);
+ // Two connectors - mConnector & mCryptConnector
+ private final CountDownLatch mConnectedSignal = new CountDownLatch(2);
private final CountDownLatch mAsecsScanned = new CountDownLatch(1);
private final Object mUnmountLock = new Object();
@@ -754,6 +757,10 @@ class MountService extends IMountService.Stub
* the hounds!
*/
mConnectedSignal.countDown();
+ if (mConnectedSignal.getCount() != 0) {
+ // More daemons need to connect
+ return;
+ }
// On an encrypted device we can't see system properties yet, so pull
// the system locale out of the mount service.
@@ -1186,6 +1193,7 @@ class MountService extends IMountService.Stub
* amount of containers we'd ever expect to have. This keeps an
* "asec list" from blocking a thread repeatedly.
*/
+
mConnector = new NativeDaemonConnector(this, "vold", MAX_CONTAINERS * 2, VOLD_TAG, 25,
null);
mConnector.setDebug(true);
@@ -1193,6 +1201,14 @@ class MountService extends IMountService.Stub
Thread thread = new Thread(mConnector, VOLD_TAG);
thread.start();
+ // Reuse parameters from first connector since they are tested and safe
+ mCryptConnector = new NativeDaemonConnector(this, "cryptd",
+ MAX_CONTAINERS * 2, CRYPTD_TAG, 25, null);
+ mCryptConnector.setDebug(true);
+
+ Thread crypt_thread = new Thread(mCryptConnector, CRYPTD_TAG);
+ crypt_thread.start();
+
// Add ourself to the Watchdog monitors if enabled.
if (WATCHDOG_ENABLE) {
Watchdog.getInstance().addMonitor(this);
@@ -2049,7 +2065,7 @@ class MountService extends IMountService.Stub
final NativeDaemonEvent event;
try {
- event = mConnector.execute("cryptfs", "cryptocomplete");
+ event = mCryptConnector.execute("cryptfs", "cryptocomplete");
return Integer.parseInt(event.getMessage());
} catch (NumberFormatException e) {
// Bad result - unexpected.
@@ -2096,7 +2112,7 @@ class MountService extends IMountService.Stub
final NativeDaemonEvent event;
try {
- event = mConnector.execute("cryptfs", "checkpw", new SensitiveArg(toHex(password)));
+ event = mCryptConnector.execute("cryptfs", "checkpw", new SensitiveArg(toHex(password)));
final int code = Integer.parseInt(event.getMessage());
if (code == 0) {
@@ -2105,7 +2121,7 @@ class MountService extends IMountService.Stub
mHandler.postDelayed(new Runnable() {
public void run() {
try {
- mConnector.execute("cryptfs", "restart");
+ mCryptConnector.execute("cryptfs", "restart");
} catch (NativeDaemonConnectorException e) {
Slog.e(TAG, "problem executing in background", e);
}
@@ -2135,7 +2151,7 @@ class MountService extends IMountService.Stub
}
try {
- mConnector.execute("cryptfs", "enablecrypto", "inplace", CRYPTO_TYPES[type],
+ mCryptConnector.execute("cryptfs", "enablecrypto", "inplace", CRYPTO_TYPES[type],
new SensitiveArg(toHex(password)));
} catch (NativeDaemonConnectorException e) {
// Encryption failed
@@ -2160,7 +2176,7 @@ class MountService extends IMountService.Stub
}
try {
- NativeDaemonEvent event = mConnector.execute("cryptfs", "changepw", CRYPTO_TYPES[type],
+ NativeDaemonEvent event = mCryptConnector.execute("cryptfs", "changepw", CRYPTO_TYPES[type],
new SensitiveArg(toHex(password)));
return Integer.parseInt(event.getMessage());
} catch (NativeDaemonConnectorException e) {
@@ -2194,7 +2210,7 @@ class MountService extends IMountService.Stub
final NativeDaemonEvent event;
try {
- event = mConnector.execute("cryptfs", "verifypw", new SensitiveArg(toHex(password)));
+ event = mCryptConnector.execute("cryptfs", "verifypw", new SensitiveArg(toHex(password)));
Slog.i(TAG, "cryptfs verifypw => " + event.getMessage());
return Integer.parseInt(event.getMessage());
} catch (NativeDaemonConnectorException e) {
@@ -2214,7 +2230,7 @@ class MountService extends IMountService.Stub
final NativeDaemonEvent event;
try {
- event = mConnector.execute("cryptfs", "getpwtype");
+ event = mCryptConnector.execute("cryptfs", "getpwtype");
for (int i = 0; i < CRYPTO_TYPES.length; ++i) {
if (CRYPTO_TYPES[i].equals(event.getMessage()))
return i;
@@ -2238,7 +2254,7 @@ class MountService extends IMountService.Stub
final NativeDaemonEvent event;
try {
- event = mConnector.execute("cryptfs", "setfield", field, contents);
+ event = mCryptConnector.execute("cryptfs", "setfield", field, contents);
} catch (NativeDaemonConnectorException e) {
throw e.rethrowAsParcelableException();
}
@@ -2257,7 +2273,7 @@ class MountService extends IMountService.Stub
final NativeDaemonEvent event;
try {
final String[] contents = NativeDaemonEvent.filterMessageList(
- mConnector.executeForList("cryptfs", "getfield", field),
+ mCryptConnector.executeForList("cryptfs", "getfield", field),
VoldResponseCode.CryptfsGetfieldResult);
String result = new String();
for (String content : contents) {
@@ -2279,7 +2295,7 @@ class MountService extends IMountService.Stub
final NativeDaemonEvent event;
try {
- event = mConnector.execute("cryptfs", "getpw");
+ event = mCryptConnector.execute("cryptfs", "getpw");
if ("-1".equals(event.getMessage())) {
// -1 equals no password
return null;
@@ -2301,7 +2317,7 @@ class MountService extends IMountService.Stub
final NativeDaemonEvent event;
try {
- event = mConnector.execute("cryptfs", "clearpw");
+ event = mCryptConnector.execute("cryptfs", "clearpw");
} catch (NativeDaemonConnectorException e) {
throw e.rethrowAsParcelableException();
}
@@ -3114,5 +3130,8 @@ class MountService extends IMountService.Stub
if (mConnector != null) {
mConnector.monitor();
}
+ if (mCryptConnector != null) {
+ mCryptConnector.monitor();
+ }
}
}
diff --git a/services/core/java/com/android/server/pm/PackageManagerService.java b/services/core/java/com/android/server/pm/PackageManagerService.java
index 7dfe836..e370afc 100644
--- a/services/core/java/com/android/server/pm/PackageManagerService.java
+++ b/services/core/java/com/android/server/pm/PackageManagerService.java
@@ -1876,7 +1876,7 @@ public class PackageManagerService extends IPackageManager.Stub {
alreadyDexOpted.add(frameworkDir.getPath() + "/core-libart.jar");
/**
- * And there are a number of commands implemented in Java, which
+ * There are a number of commands implemented in Java, which
* we currently need to do the dexopt on so that they can be
* run from a non-root shell.
*/
@@ -6361,63 +6361,39 @@ public class PackageManagerService extends IPackageManager.Stub {
final String path = scanFile.getPath();
final String cpuAbiOverride = deriveAbiOverride(pkg.cpuAbiOverride, pkgSetting);
- if (isSystemApp(pkg) && !pkg.isUpdatedSystemApp()) {
- setBundledAppAbisAndRoots(pkg, pkgSetting);
-
- // If we haven't found any native libraries for the app, check if it has
- // renderscript code. We'll need to force the app to 32 bit if it has
- // renderscript bitcode.
- if (pkg.applicationInfo.primaryCpuAbi == null
- && pkg.applicationInfo.secondaryCpuAbi == null
- && Build.SUPPORTED_64_BIT_ABIS.length > 0) {
- NativeLibraryHelper.Handle handle = null;
- try {
- handle = NativeLibraryHelper.Handle.create(scanFile);
- if (NativeLibraryHelper.hasRenderscriptBitcode(handle)) {
- pkg.applicationInfo.primaryCpuAbi = Build.SUPPORTED_32_BIT_ABIS[0];
- }
- } catch (IOException ioe) {
- Slog.w(TAG, "Error scanning system app : " + ioe);
- } finally {
- IoUtils.closeQuietly(handle);
- }
- }
- setNativeLibraryPaths(pkg);
+ if ((scanFlags & SCAN_NEW_INSTALL) == 0) {
+ derivePackageAbi(pkg, scanFile, cpuAbiOverride, true /* extract libs */);
} else {
- if ((scanFlags & SCAN_NEW_INSTALL) == 0) {
- deriveNonSystemPackageAbi(pkg, scanFile, cpuAbiOverride, true /* extract libs */);
- } else {
- if ((scanFlags & SCAN_MOVE) != 0) {
- // We haven't run dex-opt for this move (since we've moved the compiled output too)
- // but we already have this packages package info in the PackageSetting. We just
- // use that and derive the native library path based on the new codepath.
- pkg.applicationInfo.primaryCpuAbi = pkgSetting.primaryCpuAbiString;
- pkg.applicationInfo.secondaryCpuAbi = pkgSetting.secondaryCpuAbiString;
- }
-
- // Set native library paths again. For moves, the path will be updated based on the
- // ABIs we've determined above. For non-moves, the path will be updated based on the
- // ABIs we determined during compilation, but the path will depend on the final
- // package path (after the rename away from the stage path).
- setNativeLibraryPaths(pkg);
- }
+ if ((scanFlags & SCAN_MOVE) != 0) {
+ // We haven't run dex-opt for this move (since we've moved the compiled output too)
+ // but we already have this packages package info in the PackageSetting. We just
+ // use that and derive the native library path based on the new codepath.
+ pkg.applicationInfo.primaryCpuAbi = pkgSetting.primaryCpuAbiString;
+ pkg.applicationInfo.secondaryCpuAbi = pkgSetting.secondaryCpuAbiString;
+ }
+
+ // Set native library paths again. For moves, the path will be updated based on the
+ // ABIs we've determined above. For non-moves, the path will be updated based on the
+ // ABIs we determined during compilation, but the path will depend on the final
+ // package path (after the rename away from the stage path).
+ setNativeLibraryPaths(pkg);
+ }
- if (DEBUG_INSTALL) Slog.i(TAG, "Linking native library dir for " + path);
- final int[] userIds = sUserManager.getUserIds();
- synchronized (mInstallLock) {
- // Create a native library symlink only if we have native libraries
- // and if the native libraries are 32 bit libraries. We do not provide
- // this symlink for 64 bit libraries.
- if (pkg.applicationInfo.primaryCpuAbi != null &&
- !VMRuntime.is64BitAbi(pkg.applicationInfo.primaryCpuAbi)) {
- final String nativeLibPath = pkg.applicationInfo.nativeLibraryDir;
- for (int userId : userIds) {
- if (mInstaller.linkNativeLibraryDirectory(pkg.volumeUuid, pkg.packageName,
- nativeLibPath, userId) < 0) {
- throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR,
- "Failed linking native library dir (user=" + userId + ")");
- }
+ if (DEBUG_INSTALL) Slog.i(TAG, "Linking native library dir for " + path);
+ final int[] userIds = sUserManager.getUserIds();
+ synchronized (mInstallLock) {
+ // Create a native library symlink only if we have native libraries
+ // and if the native libraries are 32 bit libraries. We do not provide
+ // this symlink for 64 bit libraries.
+ if (pkg.applicationInfo.primaryCpuAbi != null &&
+ !VMRuntime.is64BitAbi(pkg.applicationInfo.primaryCpuAbi)) {
+ final String nativeLibPath = pkg.applicationInfo.nativeLibraryDir;
+ for (int userId : userIds) {
+ if (mInstaller.linkNativeLibraryDirectory(pkg.volumeUuid, pkg.packageName,
+ nativeLibPath, userId) < 0) {
+ throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR,
+ "Failed linking native library dir (user=" + userId + ")");
}
}
}
@@ -6946,8 +6922,8 @@ public class PackageManagerService extends IPackageManager.Stub {
*
* If {@code extractLibs} is true, native libraries are extracted from the app if required.
*/
- public void deriveNonSystemPackageAbi(PackageParser.Package pkg, File scanFile,
- String cpuAbiOverride, boolean extractLibs)
+ public void derivePackageAbi(PackageParser.Package pkg, File scanFile,
+ String cpuAbiOverride, boolean extractLibs)
throws PackageManagerException {
// TODO: We can probably be smarter about this stuff. For installed apps,
// we can calculate this information at install time once and for all. For
@@ -6959,8 +6935,10 @@ public class PackageManagerService extends IPackageManager.Stub {
setNativeLibraryPaths(pkg);
// We would never need to extract libs for forward-locked and external packages,
- // since the container service will do it for us.
- if (pkg.isForwardLocked() || isExternal(pkg)) {
+ // since the container service will do it for us. We shouldn't attempt to
+ // extract libs from system app when it was not updated.
+ if (pkg.isForwardLocked() || isExternal(pkg) ||
+ (isSystemApp(pkg) && !pkg.isUpdatedSystemApp()) ) {
extractLibs = false;
}
@@ -7291,33 +7269,6 @@ public class PackageManagerService extends IPackageManager.Stub {
}
/**
- * Calculate the abis and roots for a bundled app. These can uniquely
- * be determined from the contents of the system partition, i.e whether
- * it contains 64 or 32 bit shared libraries etc. We do not validate any
- * of this information, and instead assume that the system was built
- * sensibly.
- */
- private void setBundledAppAbisAndRoots(PackageParser.Package pkg,
- PackageSetting pkgSetting) {
- final String apkName = deriveCodePathName(pkg.applicationInfo.getCodePath());
-
- // If "/system/lib64/apkname" exists, assume that is the per-package
- // native library directory to use; otherwise use "/system/lib/apkname".
- final String apkRoot = calculateBundledApkRoot(pkg.applicationInfo.sourceDir);
- setBundledAppAbi(pkg, apkRoot, apkName);
- // pkgSetting might be null during rescan following uninstall of updates
- // to a bundled app, so accommodate that possibility. The settings in
- // that case will be established later from the parsed package.
- //
- // If the settings aren't null, sync them up with what we've just derived.
- // note that apkRoot isn't stored in the package settings.
- if (pkgSetting != null) {
- pkgSetting.primaryCpuAbiString = pkg.applicationInfo.primaryCpuAbi;
- pkgSetting.secondaryCpuAbiString = pkg.applicationInfo.secondaryCpuAbi;
- }
- }
-
- /**
* Deduces the ABI of a bundled app and sets the relevant fields on the
* parsed pkg object.
*
@@ -11695,7 +11646,7 @@ public class PackageManagerService extends IPackageManager.Stub {
scanFlags |= SCAN_NO_DEX;
try {
- deriveNonSystemPackageAbi(pkg, new File(pkg.codePath), args.abiOverride,
+ derivePackageAbi(pkg, new File(pkg.codePath), args.abiOverride,
true /* extract libs */);
} catch (PackageManagerException pme) {
Slog.e(TAG, "Error deriving application ABI", pme);
diff --git a/telephony/java/android/telephony/CarrierConfigManager.java b/telephony/java/android/telephony/CarrierConfigManager.java
index dfb02bb..353bb5f 100644
--- a/telephony/java/android/telephony/CarrierConfigManager.java
+++ b/telephony/java/android/telephony/CarrierConfigManager.java
@@ -229,6 +229,14 @@ public class CarrierConfigManager {
*/
public static final String STRING_VVM_TYPE = "string_vvm_type";
+ /**
+ * The package name of the carrier's visual voicemail app to ensure that dialer visual voicemail
+ * and carrier visual voicemail are not active at the same time.
+ *
+ * @hide
+ */
+ public static final String STRING_CARRIER_VVM_PACKAGE_NAME = "string_carrier_vvm_package_name";
+
// These variables are used by the MMS service and exposed through another API, {@link
// SmsManager}. The variable names and string values are copied from there.
public static final String BOOL_MMS_ALIAS_ENABLED = "aliasEnabled";
diff --git a/telephony/java/android/telephony/TelephonyManager.java b/telephony/java/android/telephony/TelephonyManager.java
index f51cb65..2445bf6 100644
--- a/telephony/java/android/telephony/TelephonyManager.java
+++ b/telephony/java/android/telephony/TelephonyManager.java
@@ -2298,7 +2298,8 @@ public class TelephonyManager {
}
/**
- * Returns the voice mail count. Return 0 if unavailable.
+ * Returns the voice mail count. Return 0 if unavailable, -1 if there are unread voice messages
+ * but the count is unknown.
* <p>
* Requires Permission:
* {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
diff --git a/tools/aapt2/Android.mk b/tools/aapt2/Android.mk
index 23d679d..8c128d3 100644
--- a/tools/aapt2/Android.mk
+++ b/tools/aapt2/Android.mk
@@ -27,7 +27,6 @@ main := Main.cpp
sources := \
BigBuffer.cpp \
BinaryResourceParser.cpp \
- BinaryXmlPullParser.cpp \
BindingXmlPullParser.cpp \
ConfigDescription.cpp \
Debug.cpp \
@@ -53,6 +52,7 @@ sources := \
ScopedXmlPullParser.cpp \
SourceXmlPullParser.cpp \
XliffXmlPullParser.cpp \
+ XmlDom.cpp \
XmlFlattener.cpp \
ZipEntry.cpp \
ZipFile.cpp
@@ -76,6 +76,7 @@ testSources := \
StringPool_test.cpp \
Util_test.cpp \
XliffXmlPullParser_test.cpp \
+ XmlDom_test.cpp \
XmlFlattener_test.cpp
cIncludes := \
diff --git a/tools/aapt2/BinaryXmlPullParser.cpp b/tools/aapt2/BinaryXmlPullParser.cpp
deleted file mode 100644
index 476a215..0000000
--- a/tools/aapt2/BinaryXmlPullParser.cpp
+++ /dev/null
@@ -1,259 +0,0 @@
-/*
- * Copyright (C) 2015 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.
- */
-
-#include "BinaryXmlPullParser.h"
-#include "Maybe.h"
-#include "Util.h"
-
-#include <androidfw/ResourceTypes.h>
-#include <memory>
-#include <string>
-#include <vector>
-
-namespace aapt {
-
-static XmlPullParser::Event codeToEvent(android::ResXMLParser::event_code_t code) {
- switch (code) {
- case android::ResXMLParser::START_DOCUMENT:
- return XmlPullParser::Event::kStartDocument;
- case android::ResXMLParser::END_DOCUMENT:
- return XmlPullParser::Event::kEndDocument;
- case android::ResXMLParser::START_NAMESPACE:
- return XmlPullParser::Event::kStartNamespace;
- case android::ResXMLParser::END_NAMESPACE:
- return XmlPullParser::Event::kEndNamespace;
- case android::ResXMLParser::START_TAG:
- return XmlPullParser::Event::kStartElement;
- case android::ResXMLParser::END_TAG:
- return XmlPullParser::Event::kEndElement;
- case android::ResXMLParser::TEXT:
- return XmlPullParser::Event::kText;
- default:
- break;
- }
- return XmlPullParser::Event::kBadDocument;
-}
-
-BinaryXmlPullParser::BinaryXmlPullParser(const std::shared_ptr<android::ResXMLTree>& parser)
- : mParser(parser), mEvent(Event::kStartDocument), mHasComment(false), sEmpty(), sEmpty8(),
- mDepth(0) {
-}
-
-XmlPullParser::Event BinaryXmlPullParser::next() {
- mStr1.clear();
- mStr2.clear();
- mAttributes.clear();
-
- android::ResXMLParser::event_code_t code;
- if (mHasComment) {
- mHasComment = false;
- code = mParser->getEventType();
- } else {
- code = mParser->next();
- if (code != android::ResXMLParser::BAD_DOCUMENT) {
- size_t len;
- const char16_t* comment = mParser->getComment(&len);
- if (comment) {
- mHasComment = true;
- mStr1.assign(comment, len);
- return XmlPullParser::Event::kComment;
- }
- }
- }
-
- size_t len;
- const char16_t* data;
- mEvent = codeToEvent(code);
- switch (mEvent) {
- case Event::kStartNamespace:
- case Event::kEndNamespace: {
- data = mParser->getNamespacePrefix(&len);
- if (data) {
- mStr1.assign(data, len);
- } else {
- mStr1.clear();
- }
- data = mParser->getNamespaceUri(&len);
- if (data) {
- mStr2.assign(data, len);
- } else {
- mStr2.clear();
- }
-
- Maybe<std::u16string> result = util::extractPackageFromNamespace(mStr2);
- if (result) {
- if (mEvent == Event::kStartNamespace) {
- mPackageAliases.emplace_back(mStr1, result.value());
- } else {
- assert(mPackageAliases.back().second == result.value());
- mPackageAliases.pop_back();
- }
- }
- break;
- }
-
- case Event::kStartElement:
- copyAttributes();
- // fallthrough
-
- case Event::kEndElement:
- data = mParser->getElementNamespace(&len);
- if (data) {
- mStr1.assign(data, len);
- } else {
- mStr1.clear();
- }
- data = mParser->getElementName(&len);
- if (data) {
- mStr2.assign(data, len);
- } else {
- mStr2.clear();
- }
- break;
-
- case Event::kText:
- data = mParser->getText(&len);
- if (data) {
- mStr1.assign(data, len);
- } else {
- mStr1.clear();
- }
- break;
-
- default:
- break;
- }
- return mEvent;
-}
-
-XmlPullParser::Event BinaryXmlPullParser::getEvent() const {
- if (mHasComment) {
- return XmlPullParser::Event::kComment;
- }
- return mEvent;
-}
-
-const std::string& BinaryXmlPullParser::getLastError() const {
- return sEmpty8;
-}
-
-const std::u16string& BinaryXmlPullParser::getComment() const {
- if (mHasComment) {
- return mStr1;
- }
- return sEmpty;
-}
-
-size_t BinaryXmlPullParser::getLineNumber() const {
- return mParser->getLineNumber();
-}
-
-size_t BinaryXmlPullParser::getDepth() const {
- return mDepth;
-}
-
-const std::u16string& BinaryXmlPullParser::getText() const {
- if (!mHasComment && mEvent == XmlPullParser::Event::kText) {
- return mStr1;
- }
- return sEmpty;
-}
-
-const std::u16string& BinaryXmlPullParser::getNamespacePrefix() const {
- if (!mHasComment && (mEvent == XmlPullParser::Event::kStartNamespace ||
- mEvent == XmlPullParser::Event::kEndNamespace)) {
- return mStr1;
- }
- return sEmpty;
-}
-
-const std::u16string& BinaryXmlPullParser::getNamespaceUri() const {
- if (!mHasComment && (mEvent == XmlPullParser::Event::kStartNamespace ||
- mEvent == XmlPullParser::Event::kEndNamespace)) {
- return mStr2;
- }
- return sEmpty;
-}
-
-bool BinaryXmlPullParser::applyPackageAlias(std::u16string* package,
- const std::u16string& defaultPackage) const {
- const auto endIter = mPackageAliases.rend();
- for (auto iter = mPackageAliases.rbegin(); iter != endIter; ++iter) {
- if (iter->first == *package) {
- if (iter->second.empty()) {
- *package = defaultPackage;
- } else {
- *package = iter->second;
- }
- return true;
- }
- }
- return false;
-}
-
-const std::u16string& BinaryXmlPullParser::getElementNamespace() const {
- if (!mHasComment && (mEvent == XmlPullParser::Event::kStartElement ||
- mEvent == XmlPullParser::Event::kEndElement)) {
- return mStr1;
- }
- return sEmpty;
-}
-
-const std::u16string& BinaryXmlPullParser::getElementName() const {
- if (!mHasComment && (mEvent == XmlPullParser::Event::kStartElement ||
- mEvent == XmlPullParser::Event::kEndElement)) {
- return mStr2;
- }
- return sEmpty;
-}
-
-size_t BinaryXmlPullParser::getAttributeCount() const {
- return mAttributes.size();
-}
-
-XmlPullParser::const_iterator BinaryXmlPullParser::beginAttributes() const {
- return mAttributes.begin();
-}
-
-XmlPullParser::const_iterator BinaryXmlPullParser::endAttributes() const {
- return mAttributes.end();
-}
-
-void BinaryXmlPullParser::copyAttributes() {
- const size_t attrCount = mParser->getAttributeCount();
- if (attrCount > 0) {
- mAttributes.reserve(attrCount);
- for (size_t i = 0; i < attrCount; i++) {
- XmlPullParser::Attribute attr;
- size_t len;
- const char16_t* str = mParser->getAttributeNamespace(i, &len);
- if (str) {
- attr.namespaceUri.assign(str, len);
- }
- str = mParser->getAttributeName(i, &len);
- if (str) {
- attr.name.assign(str, len);
- }
- str = mParser->getAttributeStringValue(i, &len);
- if (str) {
- attr.value.assign(str, len);
- }
- mAttributes.push_back(std::move(attr));
- }
- }
-}
-
-} // namespace aapt
diff --git a/tools/aapt2/BinaryXmlPullParser.h b/tools/aapt2/BinaryXmlPullParser.h
deleted file mode 100644
index 16fc8b7..0000000
--- a/tools/aapt2/BinaryXmlPullParser.h
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * Copyright (C) 2015 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.
- */
-
-#ifndef AAPT_BINARY_XML_PULL_PARSER_H
-#define AAPT_BINARY_XML_PULL_PARSER_H
-
-#include "XmlPullParser.h"
-
-#include <androidfw/ResourceTypes.h>
-#include <memory>
-#include <string>
-#include <vector>
-
-namespace aapt {
-
-/**
- * Wraps a ResTable into the canonical XmlPullParser interface.
- */
-class BinaryXmlPullParser : public XmlPullParser {
-public:
- BinaryXmlPullParser(const std::shared_ptr<android::ResXMLTree>& parser);
- BinaryXmlPullParser(const BinaryXmlPullParser& rhs) = delete;
-
- Event getEvent() const override;
- const std::string& getLastError() const override;
- Event next() override;
-
- const std::u16string& getComment() const override;
- size_t getLineNumber() const override;
- size_t getDepth() const override;
-
- const std::u16string& getText() const override;
-
- const std::u16string& getNamespacePrefix() const override;
- const std::u16string& getNamespaceUri() const override;
- bool applyPackageAlias(std::u16string* package, const std::u16string& defaultpackage)
- const override;
-
- const std::u16string& getElementNamespace() const override;
- const std::u16string& getElementName() const override;
-
- const_iterator beginAttributes() const override;
- const_iterator endAttributes() const override;
- size_t getAttributeCount() const override;
-
-private:
- void copyAttributes();
-
- std::shared_ptr<android::ResXMLTree> mParser;
- std::u16string mStr1;
- std::u16string mStr2;
- std::vector<Attribute> mAttributes;
- Event mEvent;
- bool mHasComment;
- const std::u16string sEmpty;
- const std::string sEmpty8;
- size_t mDepth;
- std::vector<std::pair<std::u16string, std::u16string>> mPackageAliases;
-};
-
-} // namespace aapt
-
-#endif // AAPT_BINARY_XML_PULL_PARSER_H
diff --git a/tools/aapt2/Main.cpp b/tools/aapt2/Main.cpp
index 91639c5..84957b4 100644
--- a/tools/aapt2/Main.cpp
+++ b/tools/aapt2/Main.cpp
@@ -17,7 +17,6 @@
#include "AppInfo.h"
#include "BigBuffer.h"
#include "BinaryResourceParser.h"
-#include "BinaryXmlPullParser.h"
#include "BindingXmlPullParser.h"
#include "Debug.h"
#include "Files.h"
@@ -128,7 +127,7 @@ void versionStylesForCompat(const std::shared_ptr<ResourceTable>& table) {
auto iter = style.entries.begin();
while (iter != style.entries.end()) {
if (iter->key.name.package == u"android") {
- size_t sdkLevel = findAttributeSdkLevel(iter->key.name.entry);
+ size_t sdkLevel = findAttributeSdkLevel(iter->key.name);
if (sdkLevel > 1 && sdkLevel > configValue.config.sdkVersion) {
// Record that we are about to strip this.
stripped.emplace_back(std::move(*iter));
@@ -300,6 +299,42 @@ struct AaptOptions {
ResourceName dumpStyleTarget;
};
+struct IdCollector : public xml::Visitor {
+ IdCollector(const Source& source, const std::shared_ptr<ResourceTable>& table) :
+ mSource(source), mTable(table) {
+ }
+
+ virtual void visit(xml::Text* node) override {}
+
+ virtual void visit(xml::Namespace* node) override {
+ for (const auto& child : node->children) {
+ child->accept(this);
+ }
+ }
+
+ virtual void visit(xml::Element* node) override {
+ for (const xml::Attribute& attr : node->attributes) {
+ bool create = false;
+ bool priv = false;
+ ResourceNameRef nameRef;
+ if (ResourceParser::tryParseReference(attr.value, &nameRef, &create, &priv)) {
+ if (create) {
+ mTable->addResource(nameRef, {}, mSource.line(node->lineNumber),
+ util::make_unique<Id>());
+ }
+ }
+ }
+
+ for (const auto& child : node->children) {
+ child->accept(this);
+ }
+ }
+
+private:
+ Source mSource;
+ std::shared_ptr<ResourceTable> mTable;
+};
+
bool compileXml(const AaptOptions& options, const std::shared_ptr<ResourceTable>& table,
const CompileItem& item, ZipFile* outApk) {
std::ifstream in(item.source.path, std::ifstream::binary);
@@ -308,20 +343,19 @@ bool compileXml(const AaptOptions& options, const std::shared_ptr<ResourceTable>
return false;
}
- BigBuffer outBuffer(1024);
-
- // No resolver, since we are not compiling attributes here.
- XmlFlattener flattener(table, {});
-
- XmlFlattener::Options xmlOptions;
- xmlOptions.defaultPackage = table->getPackage();
- xmlOptions.keepRawValues = true;
+ SourceLogger logger(item.source);
+ std::unique_ptr<xml::Node> root = xml::inflate(&in, &logger);
+ if (!root) {
+ return false;
+ }
- std::shared_ptr<XmlPullParser> parser = std::make_shared<SourceXmlPullParser>(in);
+ // Collect any resource ID's declared here.
+ IdCollector idCollector(item.source, table);
+ root->accept(&idCollector);
- Maybe<size_t> minStrippedSdk = flattener.flatten(item.source, parser, &outBuffer,
- xmlOptions);
- if (!minStrippedSdk) {
+ BigBuffer outBuffer(1024);
+ if (!xml::flatten(root.get(), options.appInfo.package, &outBuffer)) {
+ logger.error() << "failed to encode XML." << std::endl;
return false;
}
@@ -369,19 +403,13 @@ bool shouldGenerateVersionedResource(const std::shared_ptr<const ResourceTable>&
bool linkXml(const AaptOptions& options, const std::shared_ptr<ResourceTable>& table,
const std::shared_ptr<IResolver>& resolver, const LinkItem& item,
const void* data, size_t dataLen, ZipFile* outApk, std::queue<LinkItem>* outQueue) {
- std::shared_ptr<android::ResXMLTree> tree = std::make_shared<android::ResXMLTree>();
- if (tree->setTo(data, dataLen, false) != android::NO_ERROR) {
+ SourceLogger logger(item.source);
+ std::unique_ptr<xml::Node> root = xml::inflate(data, dataLen, &logger);
+ if (!root) {
return false;
}
- std::shared_ptr<XmlPullParser> parser = std::make_shared<BinaryXmlPullParser>(tree);
-
- BigBuffer outBuffer(1024);
- XmlFlattener flattener({}, resolver);
-
- XmlFlattener::Options xmlOptions;
- xmlOptions.defaultPackage = item.originalPackage;
-
+ xml::FlattenOptions xmlOptions;
if (options.packageType == AaptOptions::PackageType::StaticLibrary) {
xmlOptions.keepRawValues = true;
}
@@ -392,16 +420,12 @@ bool linkXml(const AaptOptions& options, const std::shared_ptr<ResourceTable>& t
xmlOptions.maxSdkAttribute = item.config.sdkVersion ? item.config.sdkVersion : 1;
}
- std::shared_ptr<BindingXmlPullParser> binding;
- if (item.name.type == ResourceType::kLayout) {
- // Layouts may have defined bindings, so we need to make sure they get processed.
- binding = std::make_shared<BindingXmlPullParser>(parser);
- parser = binding;
- }
-
- Maybe<size_t> minStrippedSdk = flattener.flatten(item.source, parser, &outBuffer,
- xmlOptions);
+ BigBuffer outBuffer(1024);
+ Maybe<size_t> minStrippedSdk = xml::flattenAndLink(item.source, root.get(),
+ item.originalPackage, resolver,
+ xmlOptions, &outBuffer);
if (!minStrippedSdk) {
+ logger.error() << "failed to encode XML." << std::endl;
return false;
}
@@ -431,30 +455,6 @@ bool linkXml(const AaptOptions& options, const std::shared_ptr<ResourceTable>& t
<< buildFileReference(item) << "' to apk." << std::endl;
return false;
}
-
- if (binding && !options.bindingOutput.path.empty()) {
- // We generated a binding xml file, write it out.
- Source bindingOutput = options.bindingOutput;
- appendPath(&bindingOutput.path, buildFileReference(item));
-
- if (!mkdirs(bindingOutput.path)) {
- Logger::error(bindingOutput) << strerror(errno) << std::endl;
- return false;
- }
-
- appendPath(&bindingOutput.path, "bind.xml");
-
- std::ofstream bout(bindingOutput.path);
- if (!bout) {
- Logger::error(bindingOutput) << strerror(errno) << std::endl;
- return false;
- }
-
- if (!binding->writeToFile(bout)) {
- Logger::error(bindingOutput) << strerror(errno) << std::endl;
- return false;
- }
- }
return true;
}
@@ -504,13 +504,15 @@ bool compileManifest(const AaptOptions& options, const std::shared_ptr<IResolver
return false;
}
- BigBuffer outBuffer(1024);
- std::shared_ptr<XmlPullParser> xmlParser = std::make_shared<SourceXmlPullParser>(in);
- XmlFlattener flattener({}, resolver);
+ SourceLogger logger(options.manifest);
+ std::unique_ptr<xml::Node> root = xml::inflate(&in, &logger);
+ if (!root) {
+ return false;
+ }
- XmlFlattener::Options xmlOptions;
- xmlOptions.defaultPackage = options.appInfo.package;
- if (!flattener.flatten(options.manifest, xmlParser, &outBuffer, xmlOptions)) {
+ BigBuffer outBuffer(1024);
+ if (!xml::flattenAndLink(options.manifest, root.get(), options.appInfo.package, resolver, {},
+ &outBuffer)) {
return false;
}
diff --git a/tools/aapt2/SdkConstants.cpp b/tools/aapt2/SdkConstants.cpp
index 3f156a6..9bdae49 100644
--- a/tools/aapt2/SdkConstants.cpp
+++ b/tools/aapt2/SdkConstants.cpp
@@ -14,11 +14,51 @@
* limitations under the License.
*/
+#include "SdkConstants.h"
+
+#include <algorithm>
#include <string>
#include <unordered_map>
+#include <vector>
namespace aapt {
+static const std::vector<std::pair<uint16_t, size_t>> sAttrIdMap = {
+ { 0x021c, 1 },
+ { 0x021d, 2 },
+ { 0x0269, SDK_CUPCAKE },
+ { 0x028d, SDK_DONUT },
+ { 0x02ad, SDK_ECLAIR },
+ { 0x02b3, SDK_ECLAIR_0_1 },
+ { 0x02b5, SDK_ECLAIR_MR1 },
+ { 0x02bd, SDK_FROYO },
+ { 0x02cb, SDK_GINGERBREAD },
+ { 0x0361, SDK_HONEYCOMB },
+ { 0x0366, SDK_HONEYCOMB_MR1 },
+ { 0x03a6, SDK_HONEYCOMB_MR2 },
+ { 0x03ae, SDK_JELLY_BEAN },
+ { 0x03cc, SDK_JELLY_BEAN_MR1 },
+ { 0x03da, SDK_JELLY_BEAN_MR2 },
+ { 0x03f1, SDK_KITKAT },
+ { 0x03f6, SDK_KITKAT_WATCH },
+ { 0x04ce, SDK_LOLLIPOP },
+};
+
+static bool lessEntryId(const std::pair<uint16_t, size_t>& p, uint16_t entryId) {
+ return p.first < entryId;
+}
+
+size_t findAttributeSdkLevel(ResourceId id) {
+ if (id.packageId() != 0x01 && id.typeId() != 0x01) {
+ return 0;
+ }
+ auto iter = std::lower_bound(sAttrIdMap.begin(), sAttrIdMap.end(), id.entryId(), lessEntryId);
+ if (iter == sAttrIdMap.end()) {
+ return SDK_LOLLIPOP_MR1;
+ }
+ return iter->second;
+}
+
static const std::unordered_map<std::u16string, size_t> sAttrMap = {
{ u"marqueeRepeatLimit", 2 },
{ u"windowNoDisplay", 3 },
@@ -682,12 +722,16 @@ static const std::unordered_map<std::u16string, size_t> sAttrMap = {
{ u"colorEdgeEffect", 21 }
};
-size_t findAttributeSdkLevel(const std::u16string& name) {
- auto iter = sAttrMap.find(name);
+size_t findAttributeSdkLevel(const ResourceName& name) {
+ if (name.package != u"android" && name.type != ResourceType::kAttr) {
+ return 0;
+ }
+
+ auto iter = sAttrMap.find(name.entry);
if (iter != sAttrMap.end()) {
return iter->second;
}
- return 0;
+ return SDK_LOLLIPOP_MR1;
}
} // namespace aapt
diff --git a/tools/aapt2/SdkConstants.h b/tools/aapt2/SdkConstants.h
index 469c355..803da03 100644
--- a/tools/aapt2/SdkConstants.h
+++ b/tools/aapt2/SdkConstants.h
@@ -19,8 +19,6 @@
#include "Resource.h"
-#include <string>
-
namespace aapt {
enum {
@@ -46,7 +44,8 @@ enum {
SDK_LOLLIPOP_MR1 = 22,
};
-size_t findAttributeSdkLevel(const std::u16string& name);
+size_t findAttributeSdkLevel(ResourceId id);
+size_t findAttributeSdkLevel(const ResourceName& name);
} // namespace aapt
diff --git a/tools/aapt2/XmlDom.cpp b/tools/aapt2/XmlDom.cpp
new file mode 100644
index 0000000..763029f
--- /dev/null
+++ b/tools/aapt2/XmlDom.cpp
@@ -0,0 +1,431 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+#include "Logger.h"
+#include "Util.h"
+#include "XmlDom.h"
+#include "XmlPullParser.h"
+
+#include <cassert>
+#include <memory>
+#include <stack>
+#include <string>
+#include <tuple>
+
+namespace aapt {
+namespace xml {
+
+constexpr char kXmlNamespaceSep = 1;
+
+struct Stack {
+ std::unique_ptr<xml::Node> root;
+ std::stack<xml::Node*> nodeStack;
+ std::u16string pendingComment;
+};
+
+/**
+ * Extracts the namespace and name of an expanded element or attribute name.
+ */
+static void splitName(const char* name, std::u16string* outNs, std::u16string* outName) {
+ const char* p = name;
+ while (*p != 0 && *p != kXmlNamespaceSep) {
+ p++;
+ }
+
+ if (*p == 0) {
+ outNs->clear();
+ *outName = util::utf8ToUtf16(name);
+ } else {
+ *outNs = util::utf8ToUtf16(StringPiece(name, (p - name)));
+ *outName = util::utf8ToUtf16(p + 1);
+ }
+}
+
+static void addToStack(Stack* stack, XML_Parser parser, std::unique_ptr<Node> node) {
+ node->lineNumber = XML_GetCurrentLineNumber(parser);
+ node->columnNumber = XML_GetCurrentColumnNumber(parser);
+
+ Node* thisNode = node.get();
+ if (!stack->nodeStack.empty()) {
+ stack->nodeStack.top()->addChild(std::move(node));
+ } else {
+ stack->root = std::move(node);
+ }
+
+ if (thisNode->type != NodeType::kText) {
+ stack->nodeStack.push(thisNode);
+ }
+}
+
+static void XMLCALL startNamespaceHandler(void* userData, const char* prefix, const char* uri) {
+ XML_Parser parser = reinterpret_cast<XML_Parser>(userData);
+ Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser));
+
+ std::unique_ptr<Namespace> ns = util::make_unique<Namespace>();
+ if (prefix) {
+ ns->namespacePrefix = util::utf8ToUtf16(prefix);
+ }
+
+ if (uri) {
+ ns->namespaceUri = util::utf8ToUtf16(uri);
+ }
+
+ addToStack(stack, parser, std::move(ns));
+}
+
+static void XMLCALL endNamespaceHandler(void* userData, const char* prefix) {
+ XML_Parser parser = reinterpret_cast<XML_Parser>(userData);
+ Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser));
+
+ assert(!stack->nodeStack.empty());
+ stack->nodeStack.pop();
+}
+
+static bool lessAttribute(const Attribute& lhs, const Attribute& rhs) {
+ return std::tie(lhs.namespaceUri, lhs.name, lhs.value) <
+ std::tie(rhs.namespaceUri, rhs.name, rhs.value);
+}
+
+static void XMLCALL startElementHandler(void* userData, const char* name, const char** attrs) {
+ XML_Parser parser = reinterpret_cast<XML_Parser>(userData);
+ Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser));
+
+ std::unique_ptr<Element> el = util::make_unique<Element>();
+ splitName(name, &el->namespaceUri, &el->name);
+
+ while (*attrs) {
+ Attribute attribute;
+ splitName(*attrs++, &attribute.namespaceUri, &attribute.name);
+ attribute.value = util::utf8ToUtf16(*attrs++);
+
+ // Insert in sorted order.
+ auto iter = std::lower_bound(el->attributes.begin(), el->attributes.end(), attribute,
+ lessAttribute);
+ el->attributes.insert(iter, std::move(attribute));
+ }
+
+ el->comment = std::move(stack->pendingComment);
+ addToStack(stack, parser, std::move(el));
+}
+
+static void XMLCALL endElementHandler(void* userData, const char* name) {
+ XML_Parser parser = reinterpret_cast<XML_Parser>(userData);
+ Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser));
+
+ assert(!stack->nodeStack.empty());
+ stack->nodeStack.top()->comment = std::move(stack->pendingComment);
+ stack->nodeStack.pop();
+}
+
+static void XMLCALL characterDataHandler(void* userData, const char* s, int len) {
+ XML_Parser parser = reinterpret_cast<XML_Parser>(userData);
+ Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser));
+
+ if (!s || len <= 0) {
+ return;
+ }
+
+ // See if we can just append the text to a previous text node.
+ if (!stack->nodeStack.empty()) {
+ Node* currentParent = stack->nodeStack.top();
+ if (!currentParent->children.empty()) {
+ Node* lastChild = currentParent->children.back().get();
+ if (lastChild->type == NodeType::kText) {
+ Text* text = static_cast<Text*>(lastChild);
+ text->text += util::utf8ToUtf16(StringPiece(s, len));
+ return;
+ }
+ }
+ }
+
+ std::unique_ptr<Text> text = util::make_unique<Text>();
+ text->text = util::utf8ToUtf16(StringPiece(s, len));
+ addToStack(stack, parser, std::move(text));
+}
+
+static void XMLCALL commentDataHandler(void* userData, const char* comment) {
+ XML_Parser parser = reinterpret_cast<XML_Parser>(userData);
+ Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser));
+
+ if (!stack->pendingComment.empty()) {
+ stack->pendingComment += '\n';
+ }
+ stack->pendingComment += util::utf8ToUtf16(comment);
+}
+
+std::unique_ptr<Node> inflate(std::istream* in, SourceLogger* logger) {
+ Stack stack;
+
+ XML_Parser parser = XML_ParserCreateNS(nullptr, kXmlNamespaceSep);
+ XML_SetUserData(parser, &stack);
+ XML_UseParserAsHandlerArg(parser);
+ XML_SetElementHandler(parser, startElementHandler, endElementHandler);
+ XML_SetNamespaceDeclHandler(parser, startNamespaceHandler, endNamespaceHandler);
+ XML_SetCharacterDataHandler(parser, characterDataHandler);
+ XML_SetCommentHandler(parser, commentDataHandler);
+
+ char buffer[1024];
+ while (!in->eof()) {
+ in->read(buffer, sizeof(buffer) / sizeof(buffer[0]));
+ if (in->bad() && !in->eof()) {
+ stack.root = {};
+ logger->error() << strerror(errno) << std::endl;
+ break;
+ }
+
+ if (XML_Parse(parser, buffer, in->gcount(), in->eof()) == XML_STATUS_ERROR) {
+ stack.root = {};
+ logger->error(XML_GetCurrentLineNumber(parser))
+ << XML_ErrorString(XML_GetErrorCode(parser)) << std::endl;
+ break;
+ }
+ }
+
+ XML_ParserFree(parser);
+ return std::move(stack.root);
+}
+
+static void copyAttributes(Element* el, android::ResXMLParser* parser) {
+ const size_t attrCount = parser->getAttributeCount();
+ if (attrCount > 0) {
+ el->attributes.reserve(attrCount);
+ for (size_t i = 0; i < attrCount; i++) {
+ Attribute attr;
+ size_t len;
+ const char16_t* str16 = parser->getAttributeNamespace(i, &len);
+ if (str16) {
+ attr.namespaceUri.assign(str16, len);
+ }
+
+ str16 = parser->getAttributeName(i, &len);
+ if (str16) {
+ attr.name.assign(str16, len);
+ }
+
+ str16 = parser->getAttributeStringValue(i, &len);
+ if (str16) {
+ attr.value.assign(str16, len);
+ }
+ el->attributes.push_back(std::move(attr));
+ }
+ }
+}
+
+std::unique_ptr<Node> inflate(const void* data, size_t dataLen, SourceLogger* logger) {
+ std::unique_ptr<Node> root;
+ std::stack<Node*> nodeStack;
+
+ android::ResXMLTree tree;
+ if (tree.setTo(data, dataLen) != android::NO_ERROR) {
+ return {};
+ }
+
+ android::ResXMLParser::event_code_t code;
+ while ((code = tree.next()) != android::ResXMLParser::BAD_DOCUMENT &&
+ code != android::ResXMLParser::END_DOCUMENT) {
+ std::unique_ptr<Node> newNode;
+ switch (code) {
+ case android::ResXMLParser::START_NAMESPACE: {
+ std::unique_ptr<Namespace> node = util::make_unique<Namespace>();
+ size_t len;
+ const char16_t* str16 = tree.getNamespacePrefix(&len);
+ if (str16) {
+ node->namespacePrefix.assign(str16, len);
+ }
+
+ str16 = tree.getNamespaceUri(&len);
+ if (str16) {
+ node->namespaceUri.assign(str16, len);
+ }
+ newNode = std::move(node);
+ break;
+ }
+
+ case android::ResXMLParser::START_TAG: {
+ std::unique_ptr<Element> node = util::make_unique<Element>();
+ size_t len;
+ const char16_t* str16 = tree.getElementNamespace(&len);
+ if (str16) {
+ node->namespaceUri.assign(str16, len);
+ }
+
+ str16 = tree.getElementName(&len);
+ if (str16) {
+ node->name.assign(str16, len);
+ }
+
+ copyAttributes(node.get(), &tree);
+
+ newNode = std::move(node);
+ break;
+ }
+
+ case android::ResXMLParser::TEXT: {
+ std::unique_ptr<Text> node = util::make_unique<Text>();
+ size_t len;
+ const char16_t* str16 = tree.getText(&len);
+ if (str16) {
+ node->text.assign(str16, len);
+ }
+ newNode = std::move(node);
+ break;
+ }
+
+ case android::ResXMLParser::END_NAMESPACE:
+ case android::ResXMLParser::END_TAG:
+ assert(!nodeStack.empty());
+ nodeStack.pop();
+ break;
+
+ default:
+ assert(false);
+ break;
+ }
+
+ if (newNode) {
+ newNode->lineNumber = tree.getLineNumber();
+
+ Node* thisNode = newNode.get();
+ if (!root) {
+ assert(nodeStack.empty());
+ root = std::move(newNode);
+ } else {
+ assert(!nodeStack.empty());
+ nodeStack.top()->addChild(std::move(newNode));
+ }
+
+ if (thisNode->type != NodeType::kText) {
+ nodeStack.push(thisNode);
+ }
+ }
+ }
+ return std::move(root);
+}
+
+Node::Node(NodeType type) : type(type), parent(nullptr), lineNumber(0), columnNumber(0) {
+}
+
+void Node::addChild(std::unique_ptr<Node> child) {
+ child->parent = this;
+ children.push_back(std::move(child));
+}
+
+Namespace::Namespace() : BaseNode(NodeType::kNamespace) {
+}
+
+std::unique_ptr<Node> Namespace::clone() const {
+ Namespace* ns = new Namespace();
+ ns->lineNumber = lineNumber;
+ ns->columnNumber = columnNumber;
+ ns->comment = comment;
+ ns->namespacePrefix = namespacePrefix;
+ ns->namespaceUri = namespaceUri;
+ for (auto& child : children) {
+ ns->addChild(child->clone());
+ }
+ return std::unique_ptr<Node>(ns);
+}
+
+Element::Element() : BaseNode(NodeType::kElement) {
+}
+
+std::unique_ptr<Node> Element::clone() const {
+ Element* el = new Element();
+ el->lineNumber = lineNumber;
+ el->columnNumber = columnNumber;
+ el->comment = comment;
+ el->namespaceUri = namespaceUri;
+ el->name = name;
+ el->attributes = attributes;
+ for (auto& child : children) {
+ el->addChild(child->clone());
+ }
+ return std::unique_ptr<Node>(el);
+}
+
+Attribute* Element::findAttribute(const StringPiece16& ns, const StringPiece16& name) {
+ for (auto& attr : attributes) {
+ if (ns == attr.namespaceUri && name == attr.name) {
+ return &attr;
+ }
+ }
+ return nullptr;
+}
+
+Element* Element::findChild(const StringPiece16& ns, const StringPiece16& name) {
+ return findChildWithAttribute(ns, name, nullptr);
+}
+
+Element* Element::findChildWithAttribute(const StringPiece16& ns, const StringPiece16& name,
+ const Attribute* reqAttr) {
+ for (auto& childNode : children) {
+ Node* child = childNode.get();
+ while (child->type == NodeType::kNamespace) {
+ if (child->children.empty()) {
+ break;
+ }
+ child = child->children[0].get();
+ }
+
+ if (child->type == NodeType::kElement) {
+ Element* el = static_cast<Element*>(child);
+ if (ns == el->namespaceUri && name == el->name) {
+ if (!reqAttr) {
+ return el;
+ }
+
+ Attribute* attrName = el->findAttribute(reqAttr->namespaceUri, reqAttr->name);
+ if (attrName && attrName->value == reqAttr->value) {
+ return el;
+ }
+ }
+ }
+ }
+ return nullptr;
+}
+
+std::vector<Element*> Element::getChildElements() {
+ std::vector<Element*> elements;
+ for (auto& childNode : children) {
+ Node* child = childNode.get();
+ while (child->type == NodeType::kNamespace) {
+ if (child->children.empty()) {
+ break;
+ }
+ child = child->children[0].get();
+ }
+
+ if (child->type == NodeType::kElement) {
+ elements.push_back(static_cast<Element*>(child));
+ }
+ }
+ return elements;
+}
+
+Text::Text() : BaseNode(NodeType::kText) {
+}
+
+std::unique_ptr<Node> Text::clone() const {
+ Text* el = new Text();
+ el->lineNumber = lineNumber;
+ el->columnNumber = columnNumber;
+ el->comment = comment;
+ el->text = text;
+ return std::unique_ptr<Node>(el);
+}
+
+} // namespace xml
+} // namespace aapt
diff --git a/tools/aapt2/XmlDom.h b/tools/aapt2/XmlDom.h
new file mode 100644
index 0000000..6931884
--- /dev/null
+++ b/tools/aapt2/XmlDom.h
@@ -0,0 +1,154 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+#ifndef AAPT_XML_DOM_H
+#define AAPT_XML_DOM_H
+
+#include "Logger.h"
+#include "StringPiece.h"
+
+#include <istream>
+#include <libexpat/expat.h>
+#include <memory>
+#include <string>
+#include <vector>
+
+namespace aapt {
+namespace xml {
+
+struct Visitor;
+
+/**
+ * The type of node. Can be used to downcast to the concrete XML node
+ * class.
+ */
+enum class NodeType {
+ kNamespace,
+ kElement,
+ kText,
+};
+
+/**
+ * Base class for all XML nodes.
+ */
+struct Node {
+ NodeType type;
+ Node* parent;
+ size_t lineNumber;
+ size_t columnNumber;
+ std::u16string comment;
+ std::vector<std::unique_ptr<Node>> children;
+
+ Node(NodeType type);
+ void addChild(std::unique_ptr<Node> child);
+ virtual std::unique_ptr<Node> clone() const = 0;
+ virtual void accept(Visitor* visitor) = 0;
+ virtual ~Node() {}
+};
+
+/**
+ * Base class that implements the visitor methods for a
+ * subclass of Node.
+ */
+template <typename Derived>
+struct BaseNode : public Node {
+ BaseNode(NodeType t);
+ virtual void accept(Visitor* visitor) override;
+};
+
+/**
+ * A Namespace XML node. Can only have one child.
+ */
+struct Namespace : public BaseNode<Namespace> {
+ std::u16string namespacePrefix;
+ std::u16string namespaceUri;
+
+ Namespace();
+ virtual std::unique_ptr<Node> clone() const override;
+};
+
+/**
+ * An XML attribute.
+ */
+struct Attribute {
+ std::u16string namespaceUri;
+ std::u16string name;
+ std::u16string value;
+};
+
+/**
+ * An Element XML node.
+ */
+struct Element : public BaseNode<Element> {
+ std::u16string namespaceUri;
+ std::u16string name;
+ std::vector<Attribute> attributes;
+
+ Element();
+ virtual std::unique_ptr<Node> clone() const override;
+ Attribute* findAttribute(const StringPiece16& ns, const StringPiece16& name);
+ xml::Element* findChild(const StringPiece16& ns, const StringPiece16& name);
+ xml::Element* findChildWithAttribute(const StringPiece16& ns, const StringPiece16& name,
+ const xml::Attribute* reqAttr);
+ std::vector<xml::Element*> getChildElements();
+};
+
+/**
+ * A Text (CDATA) XML node. Can not have any children.
+ */
+struct Text : public BaseNode<Text> {
+ std::u16string text;
+
+ Text();
+ virtual std::unique_ptr<Node> clone() const override;
+};
+
+/**
+ * Inflates an XML DOM from a text stream, logging errors to the logger.
+ * Returns the root node on success, or nullptr on failure.
+ */
+std::unique_ptr<Node> inflate(std::istream* in, SourceLogger* logger);
+
+/**
+ * Inflates an XML DOM from a binary ResXMLTree, logging errors to the logger.
+ * Returns the root node on success, or nullptr on failure.
+ */
+std::unique_ptr<Node> inflate(const void* data, size_t dataLen, SourceLogger* logger);
+
+/**
+ * A visitor interface for the different XML Node subtypes.
+ */
+struct Visitor {
+ virtual void visit(Namespace* node) = 0;
+ virtual void visit(Element* node) = 0;
+ virtual void visit(Text* text) = 0;
+};
+
+// Implementations
+
+template <typename Derived>
+BaseNode<Derived>::BaseNode(NodeType type) : Node(type) {
+}
+
+template <typename Derived>
+void BaseNode<Derived>::accept(Visitor* visitor) {
+ visitor->visit(static_cast<Derived*>(this));
+}
+
+} // namespace xml
+} // namespace aapt
+
+#endif // AAPT_XML_DOM_H
diff --git a/tools/aapt2/XmlDom_test.cpp b/tools/aapt2/XmlDom_test.cpp
new file mode 100644
index 0000000..0217144
--- /dev/null
+++ b/tools/aapt2/XmlDom_test.cpp
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+#include "XmlDom.h"
+
+#include <gtest/gtest.h>
+#include <sstream>
+#include <string>
+
+namespace aapt {
+
+constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
+
+TEST(XmlDomTest, Inflate) {
+ std::stringstream in(kXmlPreamble);
+ in << R"EOF(
+ <Layout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content">
+ <TextView android:id="@+id/id"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content" />
+ </Layout>
+ )EOF";
+
+ SourceLogger logger(Source{ "/test/path" });
+ std::unique_ptr<xml::Node> root = xml::inflate(&in, &logger);
+ ASSERT_NE(root, nullptr);
+
+ EXPECT_EQ(root->type, xml::NodeType::kNamespace);
+ xml::Namespace* ns = static_cast<xml::Namespace*>(root.get());
+ EXPECT_EQ(ns->namespaceUri, u"http://schemas.android.com/apk/res/android");
+ EXPECT_EQ(ns->namespacePrefix, u"android");
+}
+
+} // namespace aapt
diff --git a/tools/aapt2/XmlFlattener.cpp b/tools/aapt2/XmlFlattener.cpp
index f78e38d..56b5613 100644
--- a/tools/aapt2/XmlFlattener.cpp
+++ b/tools/aapt2/XmlFlattener.cpp
@@ -34,425 +34,444 @@
#include <vector>
namespace aapt {
+namespace xml {
-constexpr const char16_t* kSchemaAndroid = u"http://schemas.android.com/apk/res/android";
-
-struct AttributeValueFlattener : ValueVisitor {
- AttributeValueFlattener(
- std::shared_ptr<IResolver> resolver, SourceLogger* logger,
- android::Res_value* outValue, std::shared_ptr<XmlPullParser> parser, bool* outError,
- StringPool::Ref rawValue, std::u16string* defaultPackage,
- std::vector<std::pair<StringPool::Ref, android::ResStringPool_ref*>>* outStringRefs) :
- mResolver(resolver), mLogger(logger), mOutValue(outValue), mParser(parser),
- mError(outError), mRawValue(rawValue), mDefaultPackage(defaultPackage),
- mStringRefs(outStringRefs) {
+constexpr uint32_t kLowPriority = 0xffffffffu;
+
+// A vector that maps String refs to their final destination in the out buffer.
+using FlatStringRefList = std::vector<std::pair<StringPool::Ref, android::ResStringPool_ref*>>;
+
+struct XmlFlattener : public Visitor {
+ XmlFlattener(BigBuffer* outBuffer, StringPool* pool, FlatStringRefList* stringRefs,
+ const std::u16string& defaultPackage) :
+ mOut(outBuffer), mPool(pool), mStringRefs(stringRefs),
+ mDefaultPackage(defaultPackage) {
}
- void visit(Reference& reference, ValueVisitorArgs&) override {
- // First see if we can convert the package name from a prefix to a real
- // package name.
- ResourceName aliasedName = reference.name;
+ // No copying.
+ XmlFlattener(const XmlFlattener&) = delete;
+ XmlFlattener& operator=(const XmlFlattener&) = delete;
+
+ void writeNamespace(Namespace* node, uint16_t type) {
+ const size_t startIndex = mOut->size();
+ android::ResXMLTree_node* flatNode = mOut->nextBlock<android::ResXMLTree_node>();
+ android::ResXMLTree_namespaceExt* flatNs =
+ mOut->nextBlock<android::ResXMLTree_namespaceExt>();
+ mOut->align4();
+
+ flatNode->header = { type, sizeof(*flatNode), (uint32_t)(mOut->size() - startIndex) };
+ flatNode->lineNumber = node->lineNumber;
+ flatNode->comment.index = -1;
+ addString(node->namespacePrefix, kLowPriority, &flatNs->prefix);
+ addString(node->namespaceUri, kLowPriority, &flatNs->uri);
+ }
- if (!reference.name.package.empty()) {
- // Only if we specified a package do we look for its alias.
- mParser->applyPackageAlias(&reference.name.package, *mDefaultPackage);
- } else {
- reference.name.package = *mDefaultPackage;
+ virtual void visit(Namespace* node) override {
+ // Extract the package/prefix from this namespace node.
+ Maybe<std::u16string> package = util::extractPackageFromNamespace(node->namespaceUri);
+ if (package) {
+ mPackageAliases.emplace_back(
+ node->namespacePrefix,
+ package.value().empty() ? mDefaultPackage : package.value());
}
- Maybe<ResourceId> result = mResolver->findId(reference.name);
- if (!result || !result.value().isValid()) {
- std::ostream& out = mLogger->error(mParser->getLineNumber())
- << "unresolved reference '"
- << aliasedName
- << "'";
- if (aliasedName != reference.name) {
- out << " (aka '" << reference.name << "')";
- }
- out << "'." << std::endl;
- *mError = true;
- } else {
- reference.id = result.value();
- reference.flatten(*mOutValue);
+ writeNamespace(node, android::RES_XML_START_NAMESPACE_TYPE);
+ for (const auto& child : node->children) {
+ child->accept(this);
}
- }
+ writeNamespace(node, android::RES_XML_END_NAMESPACE_TYPE);
- void visit(String& string, ValueVisitorArgs&) override {
- mOutValue->dataType = android::Res_value::TYPE_STRING;
- mStringRefs->emplace_back(
- mRawValue,
- reinterpret_cast<android::ResStringPool_ref*>(mOutValue->data));
+ if (package) {
+ mPackageAliases.pop_back();
+ }
}
- void visitItem(Item& item, ValueVisitorArgs&) override {
- item.flatten(*mOutValue);
+ virtual void visit(Text* node) override {
+ if (util::trimWhitespace(node->text).empty()) {
+ return;
+ }
+
+ const size_t startIndex = mOut->size();
+ android::ResXMLTree_node* flatNode = mOut->nextBlock<android::ResXMLTree_node>();
+ android::ResXMLTree_cdataExt* flatText = mOut->nextBlock<android::ResXMLTree_cdataExt>();
+ mOut->align4();
+
+ const uint16_t type = android::RES_XML_CDATA_TYPE;
+ flatNode->header = { type, sizeof(*flatNode), (uint32_t)(mOut->size() - startIndex) };
+ flatNode->lineNumber = node->lineNumber;
+ flatNode->comment.index = -1;
+ addString(node->text, kLowPriority, &flatText->data);
}
-private:
- std::shared_ptr<IResolver> mResolver;
- SourceLogger* mLogger;
- android::Res_value* mOutValue;
- std::shared_ptr<XmlPullParser> mParser;
- bool* mError;
- StringPool::Ref mRawValue;
- std::u16string* mDefaultPackage;
- std::vector<std::pair<StringPool::Ref, android::ResStringPool_ref*>>* mStringRefs;
-};
+ virtual void visit(Element* node) override {
+ const size_t startIndex = mOut->size();
+ android::ResXMLTree_node* flatNode = mOut->nextBlock<android::ResXMLTree_node>();
+ android::ResXMLTree_attrExt* flatElem = mOut->nextBlock<android::ResXMLTree_attrExt>();
-struct XmlAttribute {
- uint32_t resourceId;
- const XmlPullParser::Attribute* xmlAttr;
- const Attribute* attr;
- StringPool::Ref nameRef;
-};
+ const uint16_t type = android::RES_XML_START_ELEMENT_TYPE;
+ flatNode->header = { type, sizeof(*flatNode), 0 };
+ flatNode->lineNumber = node->lineNumber;
+ flatNode->comment.index = -1;
-static bool lessAttributeId(const XmlAttribute& a, uint32_t id) {
- return a.resourceId < id;
-}
+ addString(node->namespaceUri, kLowPriority, &flatElem->ns);
+ addString(node->name, kLowPriority, &flatElem->name);
+ flatElem->attributeStart = sizeof(*flatElem);
+ flatElem->attributeSize = sizeof(android::ResXMLTree_attribute);
+ flatElem->attributeCount = node->attributes.size();
-XmlFlattener::XmlFlattener(const std::shared_ptr<ResourceTable>& table,
- const std::shared_ptr<IResolver>& resolver) :
- mTable(table), mResolver(resolver) {
-}
+ if (!writeAttributes(mOut, node, flatElem)) {
+ mError = true;
+ }
-/**
- * Reads events from the parser and writes to a BigBuffer. The binary XML file
- * expects the StringPool to appear first, but we haven't collected the strings yet. We
- * write to a temporary BigBuffer while parsing the input, adding strings we encounter
- * to the StringPool. At the end, we write the StringPool to the given BigBuffer and
- * then move the data from the temporary BigBuffer into the given one. This incurs no
- * copies as the given BigBuffer simply takes ownership of the data.
- */
-Maybe<size_t> XmlFlattener::flatten(const Source& source,
- const std::shared_ptr<XmlPullParser>& parser,
- BigBuffer* outBuffer, Options options) {
- SourceLogger logger(source);
- StringPool pool;
- bool error = false;
+ mOut->align4();
+ flatNode->header.size = (uint32_t)(mOut->size() - startIndex);
- size_t smallestStrippedAttributeSdk = std::numeric_limits<size_t>::max();
+ for (const auto& child : node->children) {
+ child->accept(this);
+ }
- // Attribute names are stored without packages, but we use
- // their StringPool index to lookup their resource IDs.
- // This will cause collisions, so we can't dedupe
- // attribute names from different packages. We use separate
- // pools that we later combine.
- std::map<std::u16string, StringPool> packagePools;
+ const size_t startEndIndex = mOut->size();
+ android::ResXMLTree_node* flatEndNode = mOut->nextBlock<android::ResXMLTree_node>();
+ android::ResXMLTree_endElementExt* flatEndElem =
+ mOut->nextBlock<android::ResXMLTree_endElementExt>();
+ mOut->align4();
- // Attribute resource IDs are stored in the same order
- // as the attribute names appear in the StringPool.
- // Since the StringPool contains more than just attribute
- // names, to maintain a tight packing of resource IDs,
- // we must ensure that attribute names appear first
- // in our StringPool. For this, we assign a low priority
- // (0xffffffff) to non-attribute strings. Attribute
- // names will be stored along with a priority equal
- // to their resource ID so that they are ordered.
- StringPool::Context lowPriority { 0xffffffffu };
+ const uint16_t endType = android::RES_XML_END_ELEMENT_TYPE;
+ flatEndNode->header = { endType, sizeof(*flatEndNode),
+ (uint32_t)(mOut->size() - startEndIndex) };
+ flatEndNode->lineNumber = node->lineNumber;
+ flatEndNode->comment.index = -1;
- // Once we sort the StringPool, we can assign the updated indices
- // to the correct data locations.
- std::vector<std::pair<StringPool::Ref, android::ResStringPool_ref*>> stringRefs;
+ addString(node->namespaceUri, kLowPriority, &flatEndElem->ns);
+ addString(node->name, kLowPriority, &flatEndElem->name);
+ }
- // Since we don't know the size of the final StringPool, we write to this
- // temporary BigBuffer, which we will append to outBuffer later.
- BigBuffer out(1024);
- while (XmlPullParser::isGoodEvent(parser->next())) {
- XmlPullParser::Event event = parser->getEvent();
- switch (event) {
- case XmlPullParser::Event::kStartNamespace:
- case XmlPullParser::Event::kEndNamespace: {
- const size_t startIndex = out.size();
- android::ResXMLTree_node* node = out.nextBlock<android::ResXMLTree_node>();
- if (event == XmlPullParser::Event::kStartNamespace) {
- node->header.type = android::RES_XML_START_NAMESPACE_TYPE;
- } else {
- node->header.type = android::RES_XML_END_NAMESPACE_TYPE;
- }
+ bool success() const {
+ return !mError;
+ }
- node->header.headerSize = sizeof(*node);
- node->lineNumber = parser->getLineNumber();
- node->comment.index = -1;
+protected:
+ void addString(const StringPiece16& str, uint32_t priority, android::ResStringPool_ref* dest) {
+ if (!str.empty()) {
+ mStringRefs->emplace_back(mPool->makeRef(str, StringPool::Context{ priority }), dest);
+ } else {
+ // The device doesn't think a string of size 0 is the same as null.
+ dest->index = -1;
+ }
+ }
- android::ResXMLTree_namespaceExt* ns =
- out.nextBlock<android::ResXMLTree_namespaceExt>();
- stringRefs.emplace_back(
- pool.makeRef(parser->getNamespacePrefix(), lowPriority), &ns->prefix);
- stringRefs.emplace_back(
- pool.makeRef(parser->getNamespaceUri(), lowPriority), &ns->uri);
+ void addString(const StringPool::Ref& ref, android::ResStringPool_ref* dest) {
+ mStringRefs->emplace_back(ref, dest);
+ }
- out.align4();
- node->header.size = out.size() - startIndex;
- break;
+ Maybe<std::u16string> getPackageAlias(const std::u16string& prefix) {
+ const auto endIter = mPackageAliases.rend();
+ for (auto iter = mPackageAliases.rbegin(); iter != endIter; ++iter) {
+ if (iter->first == prefix) {
+ return iter->second;
}
+ }
+ return {};
+ }
- case XmlPullParser::Event::kStartElement: {
- const size_t startIndex = out.size();
- android::ResXMLTree_node* node = out.nextBlock<android::ResXMLTree_node>();
- node->header.type = android::RES_XML_START_ELEMENT_TYPE;
- node->header.headerSize = sizeof(*node);
- node->lineNumber = parser->getLineNumber();
- node->comment.index = -1;
-
- android::ResXMLTree_attrExt* elem = out.nextBlock<android::ResXMLTree_attrExt>();
- if (!parser->getElementNamespace().empty()) {
- stringRefs.emplace_back(
- pool.makeRef(parser->getElementNamespace(), lowPriority), &elem->ns);
- } else {
- // The device doesn't think a string of size 0 is the same as null.
- elem->ns.index = -1;
- }
- stringRefs.emplace_back(
- pool.makeRef(parser->getElementName(), lowPriority), &elem->name);
- elem->attributeStart = sizeof(*elem);
- elem->attributeSize = sizeof(android::ResXMLTree_attribute);
-
- // The resource system expects attributes to be sorted by resource ID.
- std::vector<XmlAttribute> sortedAttributes;
- uint32_t nextAttributeId = 0;
- const auto endAttrIter = parser->endAttributes();
- for (auto attrIter = parser->beginAttributes();
- attrIter != endAttrIter;
- ++attrIter) {
- uint32_t id;
- StringPool::Ref nameRef;
- const Attribute* attr = nullptr;
-
- if (options.maxSdkAttribute && attrIter->namespaceUri == kSchemaAndroid) {
- size_t sdkVersion = findAttributeSdkLevel(attrIter->name);
- if (sdkVersion > options.maxSdkAttribute.value()) {
- // We will silently omit this attribute
- smallestStrippedAttributeSdk =
- std::min(smallestStrippedAttributeSdk, sdkVersion);
- continue;
- }
- }
-
- ResourceNameRef genIdName;
- bool create = false;
- bool privateRef = false;
- if (mTable && ResourceParser::tryParseReference(attrIter->value, &genIdName,
- &create, &privateRef) && create) {
- mTable->addResource(genIdName, {}, source.line(parser->getLineNumber()),
- util::make_unique<Id>());
- }
+ const std::u16string& getDefaultPackage() const {
+ return mDefaultPackage;
+ }
+ /**
+ * Subclasses override this to deal with attributes. Attributes can be flattened as
+ * raw values or as resources.
+ */
+ virtual bool writeAttributes(BigBuffer* out, Element* node,
+ android::ResXMLTree_attrExt* flatElem) = 0;
- Maybe<std::u16string> package = util::extractPackageFromNamespace(
- attrIter->namespaceUri);
- if (!package || !mResolver) {
- // Attributes that have no resource ID (because they don't belong to a
- // package) should appear after those that do have resource IDs. Assign
- // them some integer value that will appear after.
- id = 0x80000000u | nextAttributeId++;
- nameRef = pool.makeRef(attrIter->name, StringPool::Context{ id });
-
- } else {
- // Find the Attribute object via our Resolver.
- ResourceName attrName = {
- package.value(), ResourceType::kAttr, attrIter->name };
-
- if (attrName.package.empty()) {
- attrName.package = options.defaultPackage;
- }
-
- Maybe<IResolver::Entry> result = mResolver->findAttribute(attrName);
- if (!result || !result.value().id.isValid()) {
- logger.error(parser->getLineNumber())
- << "unresolved attribute '"
- << attrName
- << "'."
- << std::endl;
- error = true;
- continue;
- }
-
- if (!result.value().attr) {
- logger.error(parser->getLineNumber())
- << "not a valid attribute '"
- << attrName
- << "'."
- << std::endl;
- error = true;
- continue;
- }
-
- id = result.value().id.id;
- attr = result.value().attr;
-
- // Put the attribute name into a package specific pool, since we don't
- // want to collapse names from different packages.
- nameRef = packagePools[package.value()].makeRef(
- attrIter->name, StringPool::Context{ id });
- }
+private:
+ BigBuffer* mOut;
+ StringPool* mPool;
+ FlatStringRefList* mStringRefs;
+ std::u16string mDefaultPackage;
+ bool mError = false;
+ std::vector<std::pair<std::u16string, std::u16string>> mPackageAliases;
+};
- // Insert the attribute into the sorted vector.
- auto iter = std::lower_bound(sortedAttributes.begin(), sortedAttributes.end(),
- id, lessAttributeId);
- sortedAttributes.insert(iter, XmlAttribute{ id, &*attrIter, attr, nameRef });
- }
+/**
+ * Flattens XML, encoding the attributes as raw strings. This is used in the compile phase.
+ */
+struct CompileXmlFlattener : public XmlFlattener {
+ CompileXmlFlattener(BigBuffer* outBuffer, StringPool* pool, FlatStringRefList* stringRefs,
+ const std::u16string& defaultPackage) :
+ XmlFlattener(outBuffer, pool, stringRefs, defaultPackage) {
+ }
- if (error) {
- break;
- }
+ virtual bool writeAttributes(BigBuffer* out, Element* node,
+ android::ResXMLTree_attrExt* flatElem) override {
+ flatElem->attributeCount = node->attributes.size();
+ if (node->attributes.empty()) {
+ return true;
+ }
- // Now that we have filtered out some attributes, get the final count.
- elem->attributeCount = sortedAttributes.size();
-
- // Flatten the sorted attributes.
- uint16_t attributeIndex = 1;
- for (auto entry : sortedAttributes) {
- android::ResXMLTree_attribute* attr =
- out.nextBlock<android::ResXMLTree_attribute>();
- if (!entry.xmlAttr->namespaceUri.empty()) {
- stringRefs.emplace_back(
- pool.makeRef(entry.xmlAttr->namespaceUri, lowPriority), &attr->ns);
- } else {
- attr->ns.index = -1;
- }
+ android::ResXMLTree_attribute* flatAttrs = out->nextBlock<android::ResXMLTree_attribute>(
+ node->attributes.size());
+ for (const Attribute& attr : node->attributes) {
+ addString(attr.namespaceUri, kLowPriority, &flatAttrs->ns);
+ addString(attr.name, kLowPriority, &flatAttrs->name);
+ addString(attr.value, kLowPriority, &flatAttrs->rawValue);
+ flatAttrs++;
+ }
+ return true;
+ }
+};
- StringPool::Ref rawValueRef = pool.makeRef(entry.xmlAttr->value, lowPriority);
+struct AttributeToFlatten {
+ uint32_t resourceId = 0;
+ const Attribute* xmlAttr = nullptr;
+ const ::aapt::Attribute* resourceAttr = nullptr;
+};
- stringRefs.emplace_back(entry.nameRef, &attr->name);
+static bool lessAttributeId(const AttributeToFlatten& a, uint32_t id) {
+ return a.resourceId < id;
+}
- if (options.keepRawValues) {
- stringRefs.emplace_back(rawValueRef, &attr->rawValue);
- } else {
- attr->rawValue.index = -1;
- }
+/**
+ * Flattens XML, encoding the attributes as resources.
+ */
+struct LinkedXmlFlattener : public XmlFlattener {
+ LinkedXmlFlattener(BigBuffer* outBuffer, StringPool* pool,
+ std::map<std::u16string, StringPool>* packagePools,
+ FlatStringRefList* stringRefs,
+ const std::u16string& defaultPackage,
+ const std::shared_ptr<IResolver>& resolver,
+ SourceLogger* logger,
+ const FlattenOptions& options) :
+ XmlFlattener(outBuffer, pool, stringRefs, defaultPackage), mResolver(resolver),
+ mLogger(logger), mPackagePools(packagePools), mOptions(options) {
+ }
- // Assign the indices for specific attributes.
- if (entry.xmlAttr->namespaceUri == kSchemaAndroid &&
- entry.xmlAttr->name == u"id") {
- elem->idIndex = attributeIndex;
- } else if (entry.xmlAttr->namespaceUri.empty()) {
- if (entry.xmlAttr->name == u"class") {
- elem->classIndex = attributeIndex;
- } else if (entry.xmlAttr->name == u"style") {
- elem->styleIndex = attributeIndex;
- }
- }
- attributeIndex++;
-
- std::unique_ptr<Item> value;
- if (entry.attr) {
- value = ResourceParser::parseItemForAttribute(entry.xmlAttr->value,
- *entry.attr);
- } else {
- bool create = false;
- value = ResourceParser::tryParseReference(entry.xmlAttr->value, &create);
- }
+ virtual bool writeAttributes(BigBuffer* out, Element* node,
+ android::ResXMLTree_attrExt* flatElem) override {
+ bool error = false;
+ std::vector<AttributeToFlatten> sortedAttributes;
+ uint32_t nextAttributeId = 0x80000000u;
+
+ // Sort and filter attributes by their resource ID.
+ for (const Attribute& attr : node->attributes) {
+ AttributeToFlatten attrToFlatten;
+ attrToFlatten.xmlAttr = &attr;
+
+ Maybe<std::u16string> package = util::extractPackageFromNamespace(attr.namespaceUri);
+ if (package) {
+ // Find the Attribute object via our Resolver.
+ ResourceName attrName = { package.value(), ResourceType::kAttr, attr.name };
+ if (attrName.package.empty()) {
+ attrName.package = getDefaultPackage();
+ }
- if (mResolver && value) {
- AttributeValueFlattener flattener(
- mResolver,
- &logger,
- &attr->typedValue,
- parser,
- &error,
- rawValueRef,
- &options.defaultPackage,
- &stringRefs);
- value->accept(flattener, {});
- } else if (!value && entry.attr &&
- !(entry.attr->typeMask & android::ResTable_map::TYPE_STRING)) {
- logger.error(parser->getLineNumber())
- << "'"
- << *rawValueRef
- << "' is not compatible with attribute "
- << *entry.attr
- << "."
- << std::endl;
- error = true;
- } else {
- attr->typedValue.dataType = android::Res_value::TYPE_STRING;
- if (!options.keepRawValues) {
- // Don't set the string twice.
- stringRefs.emplace_back(rawValueRef, &attr->rawValue);
- }
- stringRefs.emplace_back(rawValueRef,
- reinterpret_cast<android::ResStringPool_ref*>(
- &attr->typedValue.data));
+ Maybe<IResolver::Entry> result = mResolver->findAttribute(attrName);
+ if (!result || !result.value().id.isValid() || !result.value().attr) {
+ error = true;
+ mLogger->error(node->lineNumber)
+ << "unresolved attribute '" << attrName << "'."
+ << std::endl;
+ } else {
+ attrToFlatten.resourceId = result.value().id.id;
+ attrToFlatten.resourceAttr = result.value().attr;
+
+ size_t sdk = findAttributeSdkLevel(attrToFlatten.resourceId);
+ if (mOptions.maxSdkAttribute && sdk > mOptions.maxSdkAttribute.value()) {
+ // We need to filter this attribute out.
+ mSmallestFilteredSdk = std::min(mSmallestFilteredSdk, sdk);
+ continue;
}
- attr->typedValue.size = sizeof(attr->typedValue);
}
-
- out.align4();
- node->header.size = out.size() - startIndex;
- break;
}
- case XmlPullParser::Event::kEndElement: {
- const size_t startIndex = out.size();
- android::ResXMLTree_node* node = out.nextBlock<android::ResXMLTree_node>();
- node->header.type = android::RES_XML_END_ELEMENT_TYPE;
- node->header.headerSize = sizeof(*node);
- node->lineNumber = parser->getLineNumber();
- node->comment.index = -1;
-
- android::ResXMLTree_endElementExt* elem =
- out.nextBlock<android::ResXMLTree_endElementExt>();
- stringRefs.emplace_back(
- pool.makeRef(parser->getElementNamespace(), lowPriority), &elem->ns);
- stringRefs.emplace_back(
- pool.makeRef(parser->getElementName(), lowPriority), &elem->name);
-
- out.align4();
- node->header.size = out.size() - startIndex;
- break;
+ if (attrToFlatten.resourceId == 0) {
+ // Attributes that have no resource ID (because they don't belong to a
+ // package) should appear after those that do have resource IDs. Assign
+ // them some integer value that will appear after.
+ attrToFlatten.resourceId = nextAttributeId++;
}
- case XmlPullParser::Event::kText: {
- StringPiece16 text = util::trimWhitespace(parser->getText());
- if (text.empty()) {
- break;
+ // Insert the attribute into the sorted vector.
+ auto iter = std::lower_bound(sortedAttributes.begin(), sortedAttributes.end(),
+ attrToFlatten.resourceId, lessAttributeId);
+ sortedAttributes.insert(iter, std::move(attrToFlatten));
+ }
+
+ flatElem->attributeCount = sortedAttributes.size();
+ if (sortedAttributes.empty()) {
+ return true;
+ }
+
+ android::ResXMLTree_attribute* flatAttr = out->nextBlock<android::ResXMLTree_attribute>(
+ sortedAttributes.size());
+
+ // Now that we have sorted the attributes into their final encoded order, it's time
+ // to actually write them out.
+ uint16_t attributeIndex = 1;
+ for (const AttributeToFlatten& attrToFlatten : sortedAttributes) {
+ Maybe<std::u16string> package = util::extractPackageFromNamespace(
+ attrToFlatten.xmlAttr->namespaceUri);
+
+ // Assign the indices for specific attributes.
+ if (package && package.value() == u"android" && attrToFlatten.xmlAttr->name == u"id") {
+ flatElem->idIndex = attributeIndex;
+ } else if (attrToFlatten.xmlAttr->namespaceUri.empty()) {
+ if (attrToFlatten.xmlAttr->name == u"class") {
+ flatElem->classIndex = attributeIndex;
+ } else if (attrToFlatten.xmlAttr->name == u"style") {
+ flatElem->styleIndex = attributeIndex;
+ }
+ }
+ attributeIndex++;
+
+ // Add the namespaceUri and name to the list of StringRefs to encode.
+ addString(attrToFlatten.xmlAttr->namespaceUri, kLowPriority, &flatAttr->ns);
+ flatAttr->rawValue.index = -1;
+
+ if (!attrToFlatten.resourceAttr) {
+ addString(attrToFlatten.xmlAttr->name, kLowPriority, &flatAttr->name);
+ } else {
+ // We've already extracted the package successfully before.
+ assert(package);
+
+ // Attribute names are stored without packages, but we use
+ // their StringPool index to lookup their resource IDs.
+ // This will cause collisions, so we can't dedupe
+ // attribute names from different packages. We use separate
+ // pools that we later combine.
+ //
+ // Lookup the StringPool for this package and make the reference there.
+ StringPool::Ref nameRef = (*mPackagePools)[package.value()].makeRef(
+ attrToFlatten.xmlAttr->name,
+ StringPool::Context{ attrToFlatten.resourceId });
+
+ // Add it to the list of strings to flatten.
+ addString(nameRef, &flatAttr->name);
+
+ if (mOptions.keepRawValues) {
+ // Keep raw values (this is for static libraries).
+ // TODO(with a smarter inflater for binary XML, we can do without this).
+ addString(attrToFlatten.xmlAttr->value, kLowPriority, &flatAttr->rawValue);
}
+ }
- const size_t startIndex = out.size();
- android::ResXMLTree_node* node = out.nextBlock<android::ResXMLTree_node>();
- node->header.type = android::RES_XML_CDATA_TYPE;
- node->header.headerSize = sizeof(*node);
- node->lineNumber = parser->getLineNumber();
- node->comment.index = -1;
+ error |= !flattenItem(node, attrToFlatten.xmlAttr->value, attrToFlatten.resourceAttr,
+ flatAttr);
+ flatAttr->typedValue.size = sizeof(flatAttr->typedValue);
+ flatAttr++;
+ }
+ return !error;
+ }
- android::ResXMLTree_cdataExt* elem = out.nextBlock<android::ResXMLTree_cdataExt>();
- stringRefs.emplace_back(pool.makeRef(text, lowPriority), &elem->data);
+ Maybe<size_t> getSmallestFilteredSdk() const {
+ if (mSmallestFilteredSdk == std::numeric_limits<size_t>::max()) {
+ return {};
+ }
+ return mSmallestFilteredSdk;
+ }
- out.align4();
- node->header.size = out.size() - startIndex;
- break;
+private:
+ bool flattenItem(const Node* el, const std::u16string& value, const ::aapt::Attribute* attr,
+ android::ResXMLTree_attribute* flatAttr) {
+ std::unique_ptr<Item> item;
+ if (!attr) {
+ bool create = false;
+ item = ResourceParser::tryParseReference(value, &create);
+ if (!item) {
+ flatAttr->typedValue.dataType = android::Res_value::TYPE_STRING;
+ addString(value, kLowPriority, &flatAttr->rawValue);
+ addString(value, kLowPriority, reinterpret_cast<android::ResStringPool_ref*>(
+ &flatAttr->typedValue.data));
+ return true;
}
+ } else {
+ item = ResourceParser::parseItemForAttribute(value, *attr);
+ if (!item) {
+ if (!(attr->typeMask & android::ResTable_map::TYPE_STRING)) {
+ mLogger->error(el->lineNumber)
+ << "'"
+ << value
+ << "' is not compatible with attribute '"
+ << *attr
+ << "'."
+ << std::endl;
+ return false;
+ }
- default:
- break;
+ flatAttr->typedValue.dataType = android::Res_value::TYPE_STRING;
+ addString(value, kLowPriority, &flatAttr->rawValue);
+ addString(value, kLowPriority, reinterpret_cast<android::ResStringPool_ref*>(
+ &flatAttr->typedValue.data));
+ return true;
+ }
}
- }
- out.align4();
+ assert(item);
- if (error) {
- return {};
- }
+ bool error = false;
- if (parser->getEvent() == XmlPullParser::Event::kBadDocument) {
- logger.error(parser->getLineNumber())
- << parser->getLastError()
- << std::endl;
- return {};
- }
+ // If this is a reference, resolve the name into an ID.
+ visitFunc<Reference>(*item, [&](Reference& reference) {
+ // First see if we can convert the package name from a prefix to a real
+ // package name.
+ ResourceName realName = reference.name;
+ if (!realName.package.empty()) {
+ Maybe<std::u16string> package = getPackageAlias(realName.package);
+ if (package) {
+ realName.package = package.value();
+ }
+ } else {
+ realName.package = getDefaultPackage();
+ }
- // Merge the package pools into the main pool.
- for (auto& packagePoolEntry : packagePools) {
- pool.merge(std::move(packagePoolEntry.second));
+ Maybe<ResourceId> result = mResolver->findId(realName);
+ if (!result || !result.value().isValid()) {
+ std::ostream& out = mLogger->error(el->lineNumber)
+ << "unresolved reference '"
+ << reference.name
+ << "'";
+ if (realName != reference.name) {
+ out << " (aka '" << realName << "')";
+ }
+ out << "'." << std::endl;
+ error = true;
+ } else {
+ reference.id = result.value();
+ }
+ });
+
+ if (error) {
+ return false;
+ }
+
+ item->flatten(flatAttr->typedValue);
+ return true;
}
- // Sort so that attribute resource IDs show up first.
- pool.sort([](const StringPool::Entry& a, const StringPool::Entry& b) -> bool {
+ std::shared_ptr<IResolver> mResolver;
+ SourceLogger* mLogger;
+ std::map<std::u16string, StringPool>* mPackagePools;
+ FlattenOptions mOptions;
+ size_t mSmallestFilteredSdk = std::numeric_limits<size_t>::max();
+};
+
+/**
+ * The binary XML file expects the StringPool to appear first, but we haven't collected the
+ * strings yet. We write to a temporary BigBuffer while parsing the input, adding strings
+ * we encounter to the StringPool. At the end, we write the StringPool to the given BigBuffer and
+ * then move the data from the temporary BigBuffer into the given one. This incurs no
+ * copies as the given BigBuffer simply takes ownership of the data.
+ */
+static void flattenXml(StringPool* pool, FlatStringRefList* stringRefs, BigBuffer* outBuffer,
+ BigBuffer&& xmlTreeBuffer) {
+ // Sort the string pool so that attribute resource IDs show up first.
+ pool->sort([](const StringPool::Entry& a, const StringPool::Entry& b) -> bool {
return a.context.priority < b.context.priority;
});
// Now we flatten the string pool references into the correct places.
- for (const auto& refEntry : stringRefs) {
+ for (const auto& refEntry : *stringRefs) {
refEntry.second->index = refEntry.first.getIndex();
}
@@ -463,36 +482,93 @@ Maybe<size_t> XmlFlattener::flatten(const Source& source,
header->header.headerSize = sizeof(*header);
// Flatten the StringPool.
- StringPool::flattenUtf16(outBuffer, pool);
+ StringPool::flattenUtf16(outBuffer, *pool);
// Write the array of resource IDs, indexed by StringPool order.
const size_t beforeResIdMapIndex = outBuffer->size();
android::ResChunk_header* resIdMapChunk = outBuffer->nextBlock<android::ResChunk_header>();
resIdMapChunk->type = android::RES_XML_RESOURCE_MAP_TYPE;
resIdMapChunk->headerSize = sizeof(*resIdMapChunk);
- for (const auto& str : pool) {
+ for (const auto& str : *pool) {
ResourceId id { str->context.priority };
- if (!id.isValid()) {
+ if (id.id == kLowPriority || !id.isValid()) {
// When we see the first non-resource ID,
// we're done.
break;
}
- uint32_t* flatId = outBuffer->nextBlock<uint32_t>();
- *flatId = id.id;
+ *outBuffer->nextBlock<uint32_t>() = id.id;
}
resIdMapChunk->size = outBuffer->size() - beforeResIdMapIndex;
// Move the temporary BigBuffer into outBuffer.
- outBuffer->appendBuffer(std::move(out));
-
+ outBuffer->appendBuffer(std::move(xmlTreeBuffer));
header->header.size = outBuffer->size() - beforeXmlTreeIndex;
+}
+
+bool flatten(Node* root, const std::u16string& defaultPackage, BigBuffer* outBuffer) {
+ StringPool pool;
+
+ // This will hold the StringRefs and the location in which to write the index.
+ // Once we sort the StringPool, we can assign the updated indices
+ // to the correct data locations.
+ FlatStringRefList stringRefs;
+
+ // Since we don't know the size of the final StringPool, we write to this
+ // temporary BigBuffer, which we will append to outBuffer later.
+ BigBuffer out(1024);
+
+ CompileXmlFlattener flattener(&out, &pool, &stringRefs, defaultPackage);
+ root->accept(&flattener);
+
+ if (!flattener.success()) {
+ return false;
+ }
+
+ flattenXml(&pool, &stringRefs, outBuffer, std::move(out));
+ return true;
+};
+
+Maybe<size_t> flattenAndLink(const Source& source, Node* root,
+ const std::u16string& defaultPackage,
+ const std::shared_ptr<IResolver>& resolver,
+ const FlattenOptions& options, BigBuffer* outBuffer) {
+ SourceLogger logger(source);
+ StringPool pool;
+
+ // Attribute names are stored without packages, but we use
+ // their StringPool index to lookup their resource IDs.
+ // This will cause collisions, so we can't dedupe
+ // attribute names from different packages. We use separate
+ // pools that we later combine.
+ std::map<std::u16string, StringPool> packagePools;
+
+ FlatStringRefList stringRefs;
+
+ // Since we don't know the size of the final StringPool, we write to this
+ // temporary BigBuffer, which we will append to outBuffer later.
+ BigBuffer out(1024);
+
+ LinkedXmlFlattener flattener(&out, &pool, &packagePools, &stringRefs, defaultPackage, resolver,
+ &logger, options);
+ root->accept(&flattener);
+
+ if (!flattener.success()) {
+ return {};
+ }
+
+ // Merge the package pools into the main pool.
+ for (auto& packagePoolEntry : packagePools) {
+ pool.merge(std::move(packagePoolEntry.second));
+ }
+
+ flattenXml(&pool, &stringRefs, outBuffer, std::move(out));
- if (smallestStrippedAttributeSdk == std::numeric_limits<size_t>::max()) {
- // Nothing was stripped
- return 0u;
+ if (flattener.getSmallestFilteredSdk()) {
+ return flattener.getSmallestFilteredSdk();
}
- return smallestStrippedAttributeSdk;
+ return 0;
}
+} // namespace xml
} // namespace aapt
diff --git a/tools/aapt2/XmlFlattener.h b/tools/aapt2/XmlFlattener.h
index 2cfcc16..4ece0a3 100644
--- a/tools/aapt2/XmlFlattener.h
+++ b/tools/aapt2/XmlFlattener.h
@@ -21,64 +21,49 @@
#include "Maybe.h"
#include "Resolver.h"
#include "Source.h"
-#include "XmlPullParser.h"
+#include "XmlDom.h"
#include <string>
namespace aapt {
+namespace xml {
/**
* Flattens an XML file into a binary representation parseable by
- * the Android resource system. References to resources are checked
- * and string values are transformed to typed data where possible.
+ * the Android resource system.
*/
-class XmlFlattener {
-public:
- struct Options {
- /**
- * The package to use when a reference has no package specified
- * (or a namespace URI equals "http://schemas.android.com/apk/res-auto").
- */
- std::u16string defaultPackage;
-
- /**
- * If set, tells the XmlFlattener to strip out
- * attributes that have been introduced after
- * max SDK.
- */
- Maybe<size_t> maxSdkAttribute;
-
- /**
- * Setting this to true will keep the raw string value of
- * an attribute's value when it has been resolved.
- */
- bool keepRawValues = false;
- };
+bool flatten(Node* root, const std::u16string& defaultPackage, BigBuffer* outBuffer);
+/**
+ * Options for flattenAndLink.
+ */
+struct FlattenOptions {
/**
- * Creates a flattener with a Resolver to resolve references
- * and attributes.
+ * Keep attribute raw string values along with typed values.
*/
- XmlFlattener(const std::shared_ptr<ResourceTable>& table,
- const std::shared_ptr<IResolver>& resolver);
-
- XmlFlattener(const XmlFlattener&) = delete; // Not copyable.
+ bool keepRawValues = false;
/**
- * Flatten an XML file, reading from the XML parser and writing to the
- * BigBuffer. The source object is mainly for logging errors. If the
- * function succeeds, returns the smallest SDK version of an attribute that
- * was stripped out. If no attributes were stripped out, the return value
- * is 0.
+ * If set, any attribute introduced in a later SDK will not be encoded.
*/
- Maybe<size_t> flatten(const Source& source, const std::shared_ptr<XmlPullParser>& parser,
- BigBuffer* outBuffer, Options options);
-
-private:
- std::shared_ptr<ResourceTable> mTable;
- std::shared_ptr<IResolver> mResolver;
+ Maybe<size_t> maxSdkAttribute;
};
+/**
+ * Like flatten(Node*,BigBuffer*), but references to resources are checked
+ * and string values are transformed to typed data where possible.
+ *
+ * `defaultPackage` is used when a reference has no package or the namespace URI
+ * "http://schemas.android.com/apk/res-auto" is used.
+ *
+ * `resolver` is used to resolve references to resources.
+ */
+Maybe<size_t> flattenAndLink(const Source& source, Node* root,
+ const std::u16string& defaultPackage,
+ const std::shared_ptr<IResolver>& resolver,
+ const FlattenOptions& options, BigBuffer* outBuffer);
+
+} // namespace xml
} // namespace aapt
#endif // AAPT_XML_FLATTENER_H
diff --git a/tools/aapt2/XmlFlattener_test.cpp b/tools/aapt2/XmlFlattener_test.cpp
index b45cd9b..8915d24 100644
--- a/tools/aapt2/XmlFlattener_test.cpp
+++ b/tools/aapt2/XmlFlattener_test.cpp
@@ -17,7 +17,6 @@
#include "MockResolver.h"
#include "ResourceTable.h"
#include "ResourceValues.h"
-#include "SourceXmlPullParser.h"
#include "Util.h"
#include "XmlFlattener.h"
@@ -30,13 +29,14 @@
using namespace android;
namespace aapt {
+namespace xml {
constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
class XmlFlattenerTest : public ::testing::Test {
public:
virtual void SetUp() override {
- std::shared_ptr<IResolver> resolver = std::make_shared<MockResolver>(
+ mResolver = std::make_shared<MockResolver>(
std::make_shared<ResourceTable>(),
std::map<ResourceName, ResourceId>({
{ ResourceName{ u"android", ResourceType::kAttr, u"attr" },
@@ -47,18 +47,21 @@ public:
ResourceId{ 0x01010001u } },
{ ResourceName{ u"com.lib", ResourceType::kId, u"id" },
ResourceId{ 0x01020001u } }}));
-
- mFlattener = std::make_shared<XmlFlattener>(nullptr, resolver);
}
::testing::AssertionResult testFlatten(const std::string& in, ResXMLTree* outTree) {
std::stringstream input(kXmlPreamble);
input << in << std::endl;
- std::shared_ptr<XmlPullParser> xmlParser = std::make_shared<SourceXmlPullParser>(input);
+
+ SourceLogger logger(Source{ "test.xml" });
+ std::unique_ptr<Node> root = inflate(&input, &logger);
+ if (!root) {
+ return ::testing::AssertionFailure();
+ }
+
BigBuffer outBuffer(1024);
- XmlFlattener::Options xmlOptions;
- xmlOptions.defaultPackage = u"android";
- if (!mFlattener->flatten(Source{ "test" }, xmlParser, &outBuffer, xmlOptions)) {
+ if (!flattenAndLink(Source{ "test.xml" }, root.get(), std::u16string(u"android"),
+ mResolver, {}, &outBuffer)) {
return ::testing::AssertionFailure();
}
@@ -69,16 +72,48 @@ public:
return ::testing::AssertionSuccess();
}
- std::shared_ptr<XmlFlattener> mFlattener;
+ std::shared_ptr<IResolver> mResolver;
};
TEST_F(XmlFlattenerTest, ParseSimpleView) {
- std::string input = "<View xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
- " android:attr=\"@id/id\">\n"
- "</View>";
+ std::string input = R"EOF(
+ <View xmlns:android="http://schemas.android.com/apk/res/android"
+ android:attr="@id/id"
+ class="str"
+ style="@id/id">
+ </View>
+ )EOF";
ResXMLTree tree;
ASSERT_TRUE(testFlatten(input, &tree));
+ while (tree.next() != ResXMLTree::START_TAG) {
+ ASSERT_NE(tree.getEventType(), ResXMLTree::END_DOCUMENT);
+ ASSERT_NE(tree.getEventType(), ResXMLTree::BAD_DOCUMENT);
+ }
+
+ const StringPiece16 androidNs = u"http://schemas.android.com/apk/res/android";
+ const StringPiece16 attrName = u"attr";
+ ssize_t idx = tree.indexOfAttribute(androidNs.data(), androidNs.size(), attrName.data(),
+ attrName.size());
+ ASSERT_GE(idx, 0);
+ EXPECT_EQ(tree.getAttributeNameResID(idx), 0x01010000u);
+ EXPECT_EQ(tree.getAttributeDataType(idx), android::Res_value::TYPE_REFERENCE);
+
+ const StringPiece16 class16 = u"class";
+ idx = tree.indexOfAttribute(nullptr, 0, class16.data(), class16.size());
+ ASSERT_GE(idx, 0);
+ EXPECT_EQ(tree.getAttributeNameResID(idx), 0u);
+ EXPECT_EQ(tree.getAttributeDataType(idx), android::Res_value::TYPE_STRING);
+ EXPECT_EQ(tree.getAttributeData(idx), tree.getAttributeValueStringID(idx));
+
+ const StringPiece16 style16 = u"style";
+ idx = tree.indexOfAttribute(nullptr, 0, style16.data(), style16.size());
+ ASSERT_GE(idx, 0);
+ EXPECT_EQ(tree.getAttributeNameResID(idx), 0u);
+ EXPECT_EQ(tree.getAttributeDataType(idx), android::Res_value::TYPE_REFERENCE);
+ EXPECT_EQ((uint32_t) tree.getAttributeData(idx), 0x01020000u);
+ EXPECT_EQ(tree.getAttributeValueStringID(idx), -1);
+
while (tree.next() != ResXMLTree::END_DOCUMENT) {
ASSERT_NE(tree.getEventType(), ResXMLTree::BAD_DOCUMENT);
}
@@ -193,4 +228,5 @@ TEST_F(XmlFlattenerTest, NoNamespaceIsNotTheSameAsEmptyNamespace) {
EXPECT_GE(tree.indexOfAttribute(nullptr, 0, kPackage.data(), kPackage.size()), 0);
}
+} // namespace xml
} // namespace aapt