GrpcBinderConnection.java
Go to the documentation of this file.
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package io.grpc.binder.cpp;
16 
17 import android.content.ComponentName;
18 import android.content.Context;
19 import android.content.Intent;
20 import android.content.ServiceConnection;
21 import android.os.IBinder;
22 import android.util.Log;
23 
24 /* Handles the binder connection state with OnDeviceServer server */
25 public class GrpcBinderConnection implements ServiceConnection {
26  private static final String logTag = "GrpcBinderConnection";
27 
28  private Context mContext;
29  private IBinder mService;
30 
31  // A string that identifies this service connection
32  private final String mConnId;
33 
34  public GrpcBinderConnection(Context context, String connId) {
35  mContext = context;
36  mConnId = connId;
37  }
38 
39  @Override
40  public void onNullBinding(ComponentName className) {
41  // TODO(mingcl): Notify C++ that the connection is never going to happen
42  Log.e(logTag, "Service returned null IBinder. mConnId = " + mConnId);
43  }
44 
45  @Override
46  public void onServiceConnected(ComponentName className, IBinder service) {
47  Log.e(logTag, "Service has connected. mConnId = " + mConnId);
48  if (service == null) {
49  // This should not happen since onNullBinding should be invoked instead
50  throw new IllegalArgumentException("service was null");
51  }
52  synchronized (this) {
53  mService = service;
54  }
56  }
57 
58  @Override
59  public void onServiceDisconnected(ComponentName className) {
60  Log.e(logTag, "Service has disconnected. mConnId = " + mConnId);
61  }
62 
63  public void tryConnect(String pkg, String cls, String action_name) {
64  synchronized (this) {
65  Intent intent = new Intent(action_name);
66  ComponentName compName = new ComponentName(pkg, cls);
67  intent.setComponent(compName);
68  // Will return true if the system is in the process of bringing up a service that your client
69  // has permission to bind to; false if the system couldn't find the service or if your client
70  // doesn't have permission to bind to it
71  boolean result = mContext.bindService(intent, this, Context.BIND_AUTO_CREATE);
72  if (result) {
73  Log.e(logTag, "bindService returns ok");
74  } else {
75  Log.e(
76  logTag,
77  "bindService failed. Maybe the system couldn't find the service or the"
78  + " client doesn't have permission to bind to it.");
79  }
80  }
81  }
82 
83  // Calls a function defined in endpoint_binder_pool.cc
84  private static native void notifyConnected(String connId, IBinder service);
85 }
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
io.grpc.binder.cpp.GrpcBinderConnection.notifyConnected
static native void notifyConnected(String connId, IBinder service)
io.grpc.binder.cpp.GrpcBinderConnection.onNullBinding
void onNullBinding(ComponentName className)
Definition: GrpcBinderConnection.java:40
testing::internal::Log
GTEST_API_ void Log(LogSeverity severity, const std::string &message, int stack_frames_to_skip)
Definition: bloaty/third_party/googletest/googlemock/src/gmock-internal-utils.cc:149
io.grpc.binder.cpp.GrpcBinderConnection.mConnId
final String mConnId
Definition: GrpcBinderConnection.java:32
io.grpc.binder.cpp.GrpcBinderConnection.mService
IBinder mService
Definition: GrpcBinderConnection.java:29
io.grpc.binder.cpp.GrpcBinderConnection.logTag
static final String logTag
Definition: GrpcBinderConnection.java:26
io.grpc.binder.cpp.GrpcBinderConnection.GrpcBinderConnection
GrpcBinderConnection(Context context, String connId)
Definition: GrpcBinderConnection.java:34
io.grpc.binder.cpp.GrpcBinderConnection.onServiceConnected
void onServiceConnected(ComponentName className, IBinder service)
Definition: GrpcBinderConnection.java:46
io.grpc.binder.cpp.GrpcBinderConnection.onServiceDisconnected
void onServiceDisconnected(ComponentName className)
Definition: GrpcBinderConnection.java:59
io.grpc.binder.cpp.GrpcBinderConnection.mContext
Context mContext
Definition: GrpcBinderConnection.java:28
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
io.grpc.binder.cpp.GrpcBinderConnection.tryConnect
void tryConnect(String pkg, String cls, String action_name)
Definition: GrpcBinderConnection.java:63
service
__attribute__((deprecated("Please use GRPCProtoMethod."))) @interface ProtoMethod NSString * service
Definition: ProtoMethod.h:25
io.grpc.binder.cpp.GrpcBinderConnection
Definition: GrpcBinderConnection.java:25


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:48