1 /* 2 * Copyright (C) 2023 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.devicelock; 18 19 import android.os.Parcel; 20 import android.os.Parcelable; 21 22 /** 23 * Wrapper class for transporting exceptions, similar to android.os.ParcelableException that is 24 * hidden and cannot be used by device lock. 25 * 26 * @hide 27 */ 28 public final class ParcelableException extends Exception implements Parcelable { ParcelableException(Exception t)29 public ParcelableException(Exception t) { 30 super(t); 31 } 32 ParcelableException(String message)33 public ParcelableException(String message) { 34 super(message); 35 } 36 readFromParcel(Parcel in)37 private static Exception readFromParcel(Parcel in) { 38 final String name = in.readString(); 39 final String msg = in.readString(); 40 try { 41 final Class<?> clazz = Class.forName(name, true, Parcelable.class.getClassLoader()); 42 if (Exception.class.isAssignableFrom(clazz)) { 43 return (Exception) clazz.getConstructor(String.class).newInstance(msg); 44 } 45 } catch (ReflectiveOperationException e) { 46 // return the below exception in this case. 47 } 48 return new Exception(name + ": " + msg); 49 } 50 writeToParcel(Parcel out, Throwable t)51 private static void writeToParcel(Parcel out, Throwable t) { 52 out.writeString(t.getClass().getName()); 53 out.writeString(t.getMessage()); 54 } 55 56 @Override describeContents()57 public int describeContents() { 58 return 0; 59 } 60 61 @Override writeToParcel(Parcel dest, int flags)62 public void writeToParcel(Parcel dest, int flags) { 63 writeToParcel(dest, getCause()); 64 } 65 66 /** 67 * Required per Parcelable documentation. 68 */ 69 public static final Creator<ParcelableException> CREATOR = new Creator<>() { 70 @Override 71 public ParcelableException createFromParcel(Parcel source) { 72 return new ParcelableException(readFromParcel(source)); 73 } 74 75 @Override 76 public ParcelableException[] newArray(int size) { 77 return new ParcelableException[size]; 78 } 79 }; 80 81 /** 82 * Get the encapsulated exception. 83 */ getException()84 public Exception getException() { 85 return (Exception) getCause(); 86 } 87 } 88