Skip to content

Commit 498b888

Browse files
author
Android Build Coastguard Worker
committed
Snap for 9347470 from 1e9d394 to tm-platform-release
Change-Id: I0d03ca979fdfb5d5afba9f9b365e2c110ba72369
2 parents df70f4f + 1e9d394 commit 498b888

24 files changed

Lines changed: 676 additions & 250 deletions

File tree

core/java/android/hardware/usb/UsbDeviceConnection.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,34 @@ boolean isOpen() {
107107
}
108108
}
109109

110+
/**
111+
* This is meant to be called by UsbRequest's queue() in order to synchronize on
112+
* UsbDeviceConnection's mLock to prevent the connection being closed while queueing.
113+
*/
114+
/* package */ boolean queueRequest(UsbRequest request, ByteBuffer buffer, int length) {
115+
synchronized (mLock) {
116+
if (!isOpen()) {
117+
return false;
118+
}
119+
120+
return request.queueIfConnectionOpen(buffer, length);
121+
}
122+
}
123+
124+
/**
125+
* This is meant to be called by UsbRequest's queue() in order to synchronize on
126+
* UsbDeviceConnection's mLock to prevent the connection being closed while queueing.
127+
*/
128+
/* package */ boolean queueRequest(UsbRequest request, @Nullable ByteBuffer buffer) {
129+
synchronized (mLock) {
130+
if (!isOpen()) {
131+
return false;
132+
}
133+
134+
return request.queueIfConnectionOpen(buffer);
135+
}
136+
}
137+
110138
/**
111139
* Releases all system resources related to the device.
112140
* Once the object is closed it cannot be used again.

core/java/android/hardware/usb/UsbRequest.java

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,13 @@ public boolean initialize(UsbDeviceConnection connection, UsbEndpoint endpoint)
113113
* Releases all resources related to this request.
114114
*/
115115
public void close() {
116-
if (mNativeContext != 0) {
117-
mEndpoint = null;
118-
mConnection = null;
119-
native_close();
120-
mCloseGuard.close();
116+
synchronized (mLock) {
117+
if (mNativeContext != 0) {
118+
mEndpoint = null;
119+
mConnection = null;
120+
native_close();
121+
mCloseGuard.close();
122+
}
121123
}
122124
}
123125

@@ -191,10 +193,32 @@ public void setClientData(Object data) {
191193
*/
192194
@Deprecated
193195
public boolean queue(ByteBuffer buffer, int length) {
196+
UsbDeviceConnection connection = mConnection;
197+
if (connection == null) {
198+
// The expected exception by CTS Verifier - USB Device test
199+
throw new NullPointerException("invalid connection");
200+
}
201+
202+
// Calling into the underlying UsbDeviceConnection to synchronize on its lock, to prevent
203+
// the connection being closed while queueing.
204+
return connection.queueRequest(this, buffer, length);
205+
}
206+
207+
/**
208+
* This is meant to be called from UsbDeviceConnection after synchronizing using the lock over
209+
* there, to prevent the connection being closed while queueing.
210+
*/
211+
/* package */ boolean queueIfConnectionOpen(ByteBuffer buffer, int length) {
212+
UsbDeviceConnection connection = mConnection;
213+
if (connection == null || !connection.isOpen()) {
214+
// The expected exception by CTS Verifier - USB Device test
215+
throw new NullPointerException("invalid connection");
216+
}
217+
194218
boolean out = (mEndpoint.getDirection() == UsbConstants.USB_DIR_OUT);
195219
boolean result;
196220

197-
if (mConnection.getContext().getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.P
221+
if (connection.getContext().getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.P
198222
&& length > MAX_USBFS_BUFFER_SIZE) {
199223
length = MAX_USBFS_BUFFER_SIZE;
200224
}
@@ -243,6 +267,28 @@ public boolean queue(ByteBuffer buffer, int length) {
243267
* @return true if the queueing operation succeeded
244268
*/
245269
public boolean queue(@Nullable ByteBuffer buffer) {
270+
UsbDeviceConnection connection = mConnection;
271+
if (connection == null) {
272+
// The expected exception by CTS Verifier - USB Device test
273+
throw new IllegalStateException("invalid connection");
274+
}
275+
276+
// Calling into the underlying UsbDeviceConnection to synchronize on its lock, to prevent
277+
// the connection being closed while queueing.
278+
return connection.queueRequest(this, buffer);
279+
}
280+
281+
/**
282+
* This is meant to be called from UsbDeviceConnection after synchronizing using the lock over
283+
* there, to prevent the connection being closed while queueing.
284+
*/
285+
/* package */ boolean queueIfConnectionOpen(@Nullable ByteBuffer buffer) {
286+
UsbDeviceConnection connection = mConnection;
287+
if (connection == null || !connection.isOpen()) {
288+
// The expected exception by CTS Verifier - USB Device test
289+
throw new IllegalStateException("invalid connection");
290+
}
291+
246292
// Request need to be initialized
247293
Preconditions.checkState(mNativeContext != 0, "request is not initialized");
248294

@@ -260,7 +306,7 @@ public boolean queue(@Nullable ByteBuffer buffer) {
260306
mIsUsingNewQueue = true;
261307
wasQueued = native_queue(null, 0, 0);
262308
} else {
263-
if (mConnection.getContext().getApplicationInfo().targetSdkVersion
309+
if (connection.getContext().getApplicationInfo().targetSdkVersion
264310
< Build.VERSION_CODES.P) {
265311
// Can only send/receive MAX_USBFS_BUFFER_SIZE bytes at once
266312
Preconditions.checkArgumentInRange(buffer.remaining(), 0, MAX_USBFS_BUFFER_SIZE,
@@ -363,11 +409,12 @@ public boolean queue(@Nullable ByteBuffer buffer) {
363409
* @return true if cancelling succeeded
364410
*/
365411
public boolean cancel() {
366-
if (mConnection == null) {
412+
UsbDeviceConnection connection = mConnection;
413+
if (connection == null) {
367414
return false;
368415
}
369416

370-
return mConnection.cancelRequest(this);
417+
return connection.cancelRequest(this);
371418
}
372419

373420
/**
@@ -382,7 +429,8 @@ public boolean cancel() {
382429
* @return true if cancelling succeeded.
383430
*/
384431
/* package */ boolean cancelIfOpen() {
385-
if (mNativeContext == 0 || (mConnection != null && !mConnection.isOpen())) {
432+
UsbDeviceConnection connection = mConnection;
433+
if (mNativeContext == 0 || (connection != null && !connection.isOpen())) {
386434
Log.w(TAG,
387435
"Detected attempt to cancel a request on a connection which isn't open");
388436
return false;

core/java/android/provider/Settings.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3344,9 +3344,26 @@ public ArrayMap<String, String> getStringsForPrefix(ContentResolver cr, String p
33443344
}
33453345
}
33463346

3347-
// Fetch all flags for the namespace at once for caching purposes
3348-
Bundle b = cp.call(cr.getAttributionSource(),
3349-
mProviderHolder.mUri.getAuthority(), mCallListCommand, null, args);
3347+
Bundle b;
3348+
// b/252663068: if we're in system server and the caller did not call
3349+
// clearCallingIdentity, the read would fail due to mismatched AttributionSources.
3350+
// TODO(b/256013480): remove this bypass after fixing the callers in system server.
3351+
if (namespace.equals(DeviceConfig.NAMESPACE_DEVICE_POLICY_MANAGER)
3352+
&& Settings.isInSystemServer()
3353+
&& Binder.getCallingUid() != Process.myUid()) {
3354+
final long token = Binder.clearCallingIdentity();
3355+
try {
3356+
// Fetch all flags for the namespace at once for caching purposes
3357+
b = cp.call(cr.getAttributionSource(),
3358+
mProviderHolder.mUri.getAuthority(), mCallListCommand, null, args);
3359+
} finally {
3360+
Binder.restoreCallingIdentity(token);
3361+
}
3362+
} else {
3363+
// Fetch all flags for the namespace at once for caching purposes
3364+
b = cp.call(cr.getAttributionSource(),
3365+
mProviderHolder.mUri.getAuthority(), mCallListCommand, null, args);
3366+
}
33503367
if (b == null) {
33513368
// Invalid response, return an empty map
33523369
return keyValues;

core/java/com/android/internal/app/chooser/DisplayResolveInfo.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,14 @@ public boolean start(Activity activity, Bundle options) {
172172

173173
@Override
174174
public boolean startAsCaller(ResolverActivity activity, Bundle options, int userId) {
175-
prepareIntentForCrossProfileLaunch(mResolvedIntent, userId);
175+
TargetInfo.prepareIntentForCrossProfileLaunch(mResolvedIntent, userId);
176176
activity.startActivityAsCaller(mResolvedIntent, options, false, userId);
177177
return true;
178178
}
179179

180180
@Override
181181
public boolean startAsUser(Activity activity, Bundle options, UserHandle user) {
182-
prepareIntentForCrossProfileLaunch(mResolvedIntent, user.getIdentifier());
182+
TargetInfo.prepareIntentForCrossProfileLaunch(mResolvedIntent, user.getIdentifier());
183183
activity.startActivityAsUser(mResolvedIntent, options, user);
184184
return false;
185185
}
@@ -224,13 +224,6 @@ public DisplayResolveInfo[] newArray(int size) {
224224
}
225225
};
226226

227-
private static void prepareIntentForCrossProfileLaunch(Intent intent, int targetUserId) {
228-
final int currentUserId = UserHandle.myUserId();
229-
if (targetUserId != currentUserId) {
230-
intent.fixUris(currentUserId);
231-
}
232-
}
233-
234227
private DisplayResolveInfo(Parcel in) {
235228
mDisplayLabel = in.readCharSequence();
236229
mExtendedInfo = in.readCharSequence();

core/java/com/android/internal/app/chooser/SelectableTargetInfo.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ public boolean startAsCaller(ResolverActivity activity, Bundle options, int user
232232
}
233233
intent.setComponent(mChooserTarget.getComponentName());
234234
intent.putExtras(mChooserTarget.getIntentExtras());
235+
TargetInfo.prepareIntentForCrossProfileLaunch(intent, userId);
235236

236237
// Important: we will ignore the target security checks in ActivityManager
237238
// if and only if the ChooserTarget's target package is the same package

core/java/com/android/internal/app/chooser/TargetInfo.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,15 @@ public interface TargetInfo {
130130
* @return true if this target should be pinned to the front by the request of the user
131131
*/
132132
boolean isPinned();
133+
134+
/**
135+
* Fix the URIs in {@code intent} if cross-profile sharing is required. This should be called
136+
* before launching the intent as another user.
137+
*/
138+
static void prepareIntentForCrossProfileLaunch(Intent intent, int targetUserId) {
139+
final int currentUserId = UserHandle.myUserId();
140+
if (targetUserId != currentUserId) {
141+
intent.fixUris(currentUserId);
142+
}
143+
}
133144
}

0 commit comments

Comments
 (0)