1 /* 2 * Copyright 2020 The gRPC Authors 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 io.grpc.binder.internal; 18 19 import android.os.IBinder; 20 import androidx.annotation.AnyThread; 21 import androidx.annotation.MainThread; 22 import io.grpc.Status; 23 24 /** An interface for managing a {@code Binder} connection. */ 25 interface Bindable { 26 27 /** 28 * Callbacks from this class. 29 * 30 * <p>Methods will be called at most once, and always on the application's main thread. 31 */ 32 interface Observer { 33 34 /** We're now bound to the service. Only called once, and only if the binding succeeded. */ 35 @MainThread onBound(IBinder binder)36 void onBound(IBinder binder); 37 38 /** 39 * We've disconnected from (or failed to bind to) the service. This will only be called once, 40 * after which no other calls will be made (but see note on threading above). 41 * 42 * @param reason why the connection failed or couldn't be established in the first place 43 */ 44 @MainThread onUnbound(Status reason)45 void onUnbound(Status reason); 46 } 47 48 /** 49 * Attempt to bind with the remote service. 50 * 51 * <p>Calling this multiple times or after {@link #unbind()} has no effect. 52 */ 53 @AnyThread bind()54 void bind(); 55 56 /** 57 * Unbind from the remote service if connected. 58 * 59 * <p>Observers will be notified with reason code {@code CANCELLED}. 60 * 61 * <p>Subsequent calls to {@link #bind()} (or this method) will have no effect. 62 */ 63 @AnyThread unbind()64 void unbind(); 65 } 66