1 /** 2 * Copyright (C) 2018 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 com.android.car.radio.util; 18 19 import android.os.RemoteException; 20 21 /** 22 * Helper functions to assist remote calls. 23 */ 24 public abstract class Remote { 25 private static final String TAG = "BcRadioApp.Remote"; 26 27 /** 28 * Throwing void function. 29 */ 30 public interface RemoteVoidFunction { 31 /** 32 * The actual throwing function. 33 */ call()34 void call() throws RemoteException; 35 } 36 37 /** 38 * Throwing function that returns some value. 39 * 40 * @param <V> Return type for the function. 41 */ 42 public interface RemoteFunction<V> { 43 /** 44 * The actual throwing function. 45 */ call()46 V call() throws RemoteException; 47 } 48 49 /** 50 * Wraps remote function and rethrows {@link RemoteException}. 51 */ exec(RemoteFunction<V> func)52 public static <V> V exec(RemoteFunction<V> func) { 53 try { 54 return func.call(); 55 } catch (RemoteException e) { 56 throw new RuntimeException("Failed to execute remote call", e); 57 } 58 } 59 60 /** 61 * Wraps remote void function and rethrows {@link RemoteException}. 62 */ exec(RemoteVoidFunction func)63 public static void exec(RemoteVoidFunction func) { 64 try { 65 func.call(); 66 } catch (RemoteException e) { 67 throw new RuntimeException("Failed to execute remote call", e); 68 } 69 } 70 71 /** 72 * Wraps remote function and logs in case of {@link RemoteException}. 73 */ tryExec(RemoteFunction<V> func)74 public static <V> void tryExec(RemoteFunction<V> func) { 75 try { 76 func.call(); 77 } catch (RemoteException e) { 78 Log.e(TAG, "Failed to execute remote call", e); 79 } 80 } 81 82 /** 83 * Wraps remote void function and logs in case of {@link RemoteException}. 84 */ tryExec(RemoteVoidFunction func)85 public static void tryExec(RemoteVoidFunction func) { 86 try { 87 func.call(); 88 } catch (RemoteException e) { 89 Log.e(TAG, "Failed to execute remote call", e); 90 } 91 } 92 } 93