1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.provider; 18 19 import android.annotation.NonNull; 20 import android.os.Binder; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 24 import java.io.IOException; 25 26 /** 27 * Wrapper class that offers to transport typical {@link Throwable} across a 28 * {@link Binder} call. This class is typically used to transport exceptions 29 * that cannot be modified to add {@link Parcelable} behavior, such as 30 * {@link IOException}. 31 * <ul> 32 * <li>The wrapped throwable must be defined as system class (that is, it must 33 * be in the same {@link ClassLoader} as {@link Parcelable}). 34 * <li>The wrapped throwable must support the 35 * {@link Throwable#Throwable(String)} constructor. 36 * <li>The receiver side must catch any thrown {@link ParcelableException} and 37 * call {@link #maybeRethrow(Class)} for all expected exception types. 38 * </ul> 39 * 40 * Similar to android.os.ParcelableException which is hidden and cannot be used by MediaProvider 41 * 42 * @hide 43 */ 44 public final class ParcelableException extends RuntimeException implements Parcelable { ParcelableException(Throwable t)45 public ParcelableException(Throwable t) { 46 super(t); 47 } 48 49 /** 50 * Rethrow the {@link ParcelableException} as the passed Exception class if the cause of the 51 * {@link ParcelableException} has the same class passed. 52 */ 53 @SuppressWarnings("unchecked") maybeRethrow(Class<T> clazz)54 public <T extends Throwable> void maybeRethrow(Class<T> clazz) throws T { 55 if (clazz.isAssignableFrom(getCause().getClass())) { 56 throw (T) getCause(); 57 } 58 } 59 readFromParcel(Parcel in)60 private static Throwable readFromParcel(Parcel in) { 61 final String name = in.readString(); 62 final String msg = in.readString(); 63 try { 64 final Class<?> clazz = Class.forName(name, true, Parcelable.class.getClassLoader()); 65 if (Throwable.class.isAssignableFrom(clazz)) { 66 return (Throwable) clazz.getConstructor(String.class).newInstance(msg); 67 } 68 } catch (ReflectiveOperationException e) { 69 // ignore as we will throw generic RuntimeException below 70 } 71 return new RuntimeException(name + ": " + msg); 72 } 73 74 @Override describeContents()75 public int describeContents() { 76 return 0; 77 } 78 79 @Override writeToParcel(Parcel dest, int flags)80 public void writeToParcel(Parcel dest, int flags) { 81 Throwable throwable = getCause(); 82 dest.writeString(throwable.getClass().getName()); 83 dest.writeString(throwable.getMessage()); 84 } 85 86 @NonNull 87 public static final Creator<ParcelableException> CREATOR = new Creator<ParcelableException>() { 88 @Override 89 public ParcelableException createFromParcel(Parcel source) { 90 return new ParcelableException(readFromParcel(source)); 91 } 92 93 @Override 94 public ParcelableException[] newArray(int size) { 95 return new ParcelableException[size]; 96 } 97 }; 98 } 99