diff --git a/core/api/current.txt b/core/api/current.txt index ff72863786b24..b89c3f7ffd956 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -5278,6 +5278,13 @@ package android.app { field @NonNull public static final android.os.Parcelable.Creator CREATOR; } + public final class ForegroundServiceTypeNotAllowedException extends android.app.ServiceStartNotAllowedException implements android.os.Parcelable { + ctor public ForegroundServiceTypeNotAllowedException(@NonNull String); + method public int describeContents(); + method public void writeToParcel(@NonNull android.os.Parcel, int); + field @NonNull public static final android.os.Parcelable.Creator CREATOR; + } + @Deprecated public class Fragment implements android.content.ComponentCallbacks2 android.view.View.OnCreateContextMenuListener { ctor @Deprecated public Fragment(); method @Deprecated public void dump(String, java.io.FileDescriptor, java.io.PrintWriter, String[]); diff --git a/core/java/android/app/ForegroundServiceTypeNotAllowedException.java b/core/java/android/app/ForegroundServiceTypeNotAllowedException.java new file mode 100644 index 0000000000000..c258242627c9a --- /dev/null +++ b/core/java/android/app/ForegroundServiceTypeNotAllowedException.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2022 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.app; + +import android.annotation.NonNull; +import android.os.Parcel; +import android.os.Parcelable; + +/** + * Exception thrown when an app tries to start a foreground {@link Service} without a valid type. + */ +public final class ForegroundServiceTypeNotAllowedException + extends ServiceStartNotAllowedException implements Parcelable { + /** + * Constructor. + */ + public ForegroundServiceTypeNotAllowedException(@NonNull String message) { + super(message); + } + + ForegroundServiceTypeNotAllowedException(@NonNull Parcel source) { + super(source.readString()); + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(@NonNull Parcel dest, int flags) { + dest.writeString(getMessage()); + } + + public static final @NonNull Creator + CREATOR = new Creator() { + @NonNull + public android.app.ForegroundServiceTypeNotAllowedException createFromParcel( + Parcel source) { + return new android.app.ForegroundServiceTypeNotAllowedException(source); + } + + @NonNull + public android.app.ForegroundServiceTypeNotAllowedException[] newArray(int size) { + return new android.app.ForegroundServiceTypeNotAllowedException[size]; + } + }; +} diff --git a/core/java/android/hardware/usb/IUsbManager.aidl b/core/java/android/hardware/usb/IUsbManager.aidl index 5a442445d8327..51236fe3b2c08 100644 --- a/core/java/android/hardware/usb/IUsbManager.aidl +++ b/core/java/android/hardware/usb/IUsbManager.aidl @@ -79,9 +79,20 @@ interface IUsbManager /* Returns true if the caller has permission to access the device. */ boolean hasDevicePermission(in UsbDevice device, String packageName); + /* Returns true if the given package/pid/uid has permission to access the device. */ + @JavaPassthrough(annotation= + "@android.annotation.RequiresPermission(android.Manifest.permission.MANAGE_USB)") + boolean hasDevicePermissionWithIdentity(in UsbDevice device, String packageName, + int pid, int uid); + /* Returns true if the caller has permission to access the accessory. */ boolean hasAccessoryPermission(in UsbAccessory accessory); + /* Returns true if the given pid/uid has permission to access the accessory. */ + @JavaPassthrough(annotation= + "@android.annotation.RequiresPermission(android.Manifest.permission.MANAGE_USB)") + boolean hasAccessoryPermissionWithIdentity(in UsbAccessory accessory, int pid, int uid); + /* Requests permission for the given package to access the device. * Will display a system dialog to query the user if permission * had not already been given. diff --git a/core/java/android/hardware/usb/UsbManager.java b/core/java/android/hardware/usb/UsbManager.java index 2c38f7031eff3..50dd0064a5cbe 100644 --- a/core/java/android/hardware/usb/UsbManager.java +++ b/core/java/android/hardware/usb/UsbManager.java @@ -837,6 +837,28 @@ public class UsbManager { } } + /** + * Returns true if the caller has permission to access the device. It's similar to the + * {@link #hasPermission(UsbDevice)} but allows to specify a different package/uid/pid. + * + *

Not for third-party apps.

+ * + * @hide + */ + @RequiresPermission(Manifest.permission.MANAGE_USB) + @RequiresFeature(PackageManager.FEATURE_USB_HOST) + public boolean hasPermission(@NonNull UsbDevice device, @NonNull String packageName, + int pid, int uid) { + if (mService == null) { + return false; + } + try { + return mService.hasDevicePermissionWithIdentity(device, packageName, pid, uid); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + /** * Returns true if the caller has permission to access the accessory. * Permission might have been granted temporarily via @@ -859,6 +881,27 @@ public class UsbManager { } /** + * Returns true if the caller has permission to access the accessory. It's similar to the + * {@link #hasPermission(UsbAccessory)} but allows to specify a different uid/pid. + * + *

Not for third-party apps.

+ * + * @hide + */ + @RequiresPermission(Manifest.permission.MANAGE_USB) + @RequiresFeature(PackageManager.FEATURE_USB_ACCESSORY) + public boolean hasPermission(@NonNull UsbAccessory accessory, int pid, int uid) { + if (mService == null) { + return false; + } + try { + return mService.hasAccessoryPermissionWithIdentity(accessory, pid, uid); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + + /** * Requests temporary permission for the given package to access the device. * This may result in a system dialog being displayed to the user * if permission had not already been granted. diff --git a/services/usb/java/com/android/server/usb/UsbService.java b/services/usb/java/com/android/server/usb/UsbService.java index 86f877fcd5312..72f6cc3649a72 100644 --- a/services/usb/java/com/android/server/usb/UsbService.java +++ b/services/usb/java/com/android/server/usb/UsbService.java @@ -503,6 +503,15 @@ public class UsbService extends IUsbManager.Stub { } } + @Override + public boolean hasDevicePermissionWithIdentity(UsbDevice device, String packageName, + int pid, int uid) { + mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); + + final int userId = UserHandle.getUserId(uid); + return getPermissionsForUser(userId).hasPermission(device, packageName, pid, uid); + } + @Override public boolean hasAccessoryPermission(UsbAccessory accessory) { final int uid = Binder.getCallingUid(); @@ -517,6 +526,14 @@ public class UsbService extends IUsbManager.Stub { } } + @Override + public boolean hasAccessoryPermissionWithIdentity(UsbAccessory accessory, int pid, int uid) { + mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); + + final int userId = UserHandle.getUserId(uid); + return getPermissionsForUser(userId).hasPermission(accessory, pid, uid); + } + @Override public void requestDevicePermission(UsbDevice device, String packageName, PendingIntent pi) { final int uid = Binder.getCallingUid();