TangoInitializationHelper.java
Go to the documentation of this file.
1 /*
2  * Copyright 2016 Google Inc. All Rights Reserved.
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 eu.intermodalics.nodelet_manager;
18 
19 import android.app.Activity;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.ServiceConnection;
24 import android.os.IBinder;
25 import android.util.Log;
26 
27 import java.io.File;
28 
34  public static final int ARCH_ERROR = -2;
35  public static final int ARCH_FALLBACK = -1;
36  public static final int ARCH_DEFAULT = 0;
37  public static final int ARCH_ARM64 = 1;
38  public static final int ARCH_ARM32 = 2;
39  public static final int ARCH_X86_64 = 3;
40  public static final int ARCH_X86 = 4;
41 
42  protected static boolean mIsTangoServiceBound;
43  protected static boolean mIsTangoVersionOk;
44 
51  protected static native boolean setBinderTangoService(IBinder nativeTangoServiceBinder);
52 
59  protected static native boolean isTangoVersionOk(Activity callerActivity);
60 
61  // Getters for Tango Status variables
62  public static boolean isTangoServiceBound() {
63  return mIsTangoServiceBound;
64  }
65 
66  public static boolean isTangoVersionOk() {
67  return mIsTangoVersionOk;
68  }
69 
77  public static final boolean bindTangoService(final Activity activity,
78  ServiceConnection connection) {
79  testTangoVersionOk(activity);
80 
81  Intent intent = new Intent();
82  intent.setClassName("com.google.tango", "com.google.atap.tango.TangoService");
83 
84  boolean hasJavaService = (activity.getPackageManager().resolveService(intent, 0) != null);
85 
86  // User doesn't have the latest packagename for TangoCore, fallback to the previous name.
87  if (!hasJavaService) {
88  intent = new Intent();
89  intent.setClassName("com.projecttango.tango", "com.google.atap.tango.TangoService");
90  hasJavaService = (activity.getPackageManager().resolveService(intent, 0) != null);
91  }
92 
93  // User doesn't have a Java-fied TangoCore at all; fallback to the deprecated approach
94  // of doing nothing and letting the native side auto-init to the system-service version
95  // of Tango.
96  if (!hasJavaService) {
97  return false;
98  }
99 
100  return activity.bindService(intent, connection, Context.BIND_AUTO_CREATE);
101  }
102 
107  public static final void unbindTangoService(final Context context,
108  ServiceConnection connection) {
109  context.unbindService(connection);
110  mIsTangoServiceBound = false;
111  }
112 
113 
119  public static final int loadTangoSharedLibrary() {
120  int loadedSoId = ARCH_ERROR;
121  String basePath = "/data/data/com.google.tango/libfiles/";
122  if (!(new File(basePath).exists())) {
123  basePath = "/data/data/com.projecttango.tango/libfiles/";
124  }
125  Log.i("TangoInitializationHelp", "basePath: " + basePath);
126 
127  try {
128  System.load(basePath + "arm64-v8a/libtango_client_api.so");
129  loadedSoId = ARCH_ARM64;
130  Log.i("TangoInitializationHelp", "Success! Using arm64-v8a/libtango_client_api.");
131  } catch (UnsatisfiedLinkError e) {
132  Log.e("TangoInitializationHelp", e.toString());
133  }
134  if (loadedSoId < ARCH_DEFAULT) {
135  try {
136  System.load(basePath + "armeabi-v7a/libtango_client_api.so");
137  loadedSoId = ARCH_ARM32;
138  Log.i("TangoInitializationHelp", "Success! Using armeabi-v7a/libtango_client_api.");
139  } catch (UnsatisfiedLinkError e) {
140  Log.e("TangoInitializationHelp", e.toString());
141  }
142  }
143  if (loadedSoId < ARCH_DEFAULT) {
144  try {
145  System.load(basePath + "x86_64/libtango_client_api.so");
146  loadedSoId = ARCH_X86_64;
147  Log.i("TangoInitializationHelp", "Success! Using x86_64/libtango_client_api.");
148  } catch (UnsatisfiedLinkError e) {
149  Log.e("TangoInitializationHelp", e.toString());
150  }
151  }
152  if (loadedSoId < ARCH_DEFAULT) {
153  try {
154  System.load(basePath + "x86/libtango_client_api.so");
155  loadedSoId = ARCH_X86;
156  Log.i("TangoInitializationHelp", "Success! Using x86/libtango_client_api.");
157  } catch (UnsatisfiedLinkError e) {
158  Log.e("TangoInitializationHelp", e.toString());
159  }
160  }
161  if (loadedSoId < ARCH_DEFAULT) {
162  try {
163  System.load(basePath + "default/libtango_client_api.so");
164  loadedSoId = ARCH_DEFAULT;
165  Log.i("TangoInitializationHelp", "Success! Using default/libtango_client_api.");
166  } catch (UnsatisfiedLinkError e) {
167  Log.e("TangoInitializationHelp", e.toString());
168  }
169  }
170  if (loadedSoId < ARCH_DEFAULT) {
171  try {
172  System.loadLibrary("tango_client_api");
173  loadedSoId = ARCH_FALLBACK;
174  Log.i("TangoInitializationHelp", "Falling back to libtango_client_api.so symlink.");
175  } catch (UnsatisfiedLinkError e) {
176  Log.e("TangoInitializationHelp", e.toString());
177  }
178  }
179  return loadedSoId;
180  }
181 
187  public static final int loadTangoRosNodeSharedLibrary() {
188  int loadedSo = ARCH_ERROR;
189  try {
190  System.loadLibrary(TangoNodeletManager.DEFAULT_LIB_NAME);
191  loadedSo = ARCH_DEFAULT;
192  } catch (Exception e) {
193  Log.e(TangoInitializationHelper.class.getName(),
194  "Error loading library " + TangoNodeletManager.DEFAULT_LIB_NAME, e);
195  }
196  return loadedSo;
197  }
198 
203  public static ServiceConnection NewDefaultServiceConnection() {
204  return new DefaultTangoServiceConnection(null);
205  }
206 
212  public static ServiceConnection NewDefaultServiceConnection(
214  return new DefaultTangoServiceConnection(callback);
215  }
216 
222  private static boolean testTangoVersionOk(Activity activity) {
223  mIsTangoVersionOk = isTangoVersionOk(activity);
224  return mIsTangoVersionOk;
225  }
226 
234  public static class DefaultTangoServiceConnection implements ServiceConnection {
235 
237 
239  connectionErrorCallback = callback;
240  }
241 
242  public static interface AfterConnectionCallback {
243  public void execute();
244  }
245 
246  public void onServiceConnected(ComponentName name, IBinder service) {
247  // Synchronization around RunningActivity object is to avoid
248  // Tango disconnect in the middle of the connecting operation.
249  mIsTangoServiceBound = setBinderTangoService(service);
250 
251  if (connectionErrorCallback != null) {
252  connectionErrorCallback.execute();
253  }
254  }
255 
256  public void onServiceDisconnected(ComponentName name) {
257  // Handle this if you need to gracefully shutdown/retry
258  // in the event that Tango itself crashes/gets upgraded while running.
259  }
260  }
261 }
static final boolean bindTangoService(final Activity activity, ServiceConnection connection)
static ServiceConnection NewDefaultServiceConnection(DefaultTangoServiceConnection.AfterConnectionCallback callback)
static final void unbindTangoService(final Context context, ServiceConnection connection)
static native boolean setBinderTangoService(IBinder nativeTangoServiceBinder)
ROSCPP_DECL bool exists(const std::string &service_name, bool print_failure_reason)


TangoRosStreamer
Author(s):
autogenerated on Mon Jun 10 2019 15:37:54