TangoInitializationHelper.java
Go to the documentation of this file.
00001 /*
00002  * Copyright 2016 Google Inc. All Rights Reserved.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 package eu.intermodalics.nodelet_manager;
00018 
00019 import android.app.Activity;
00020 import android.content.ComponentName;
00021 import android.content.Context;
00022 import android.content.Intent;
00023 import android.content.ServiceConnection;
00024 import android.os.IBinder;
00025 import android.util.Log;
00026 
00027 import java.io.File;
00028 
00033 public class TangoInitializationHelper {
00034     public static final int ARCH_ERROR = -2;
00035     public static final int ARCH_FALLBACK = -1;
00036     public static final int ARCH_DEFAULT = 0;
00037     public static final int ARCH_ARM64 = 1;
00038     public static final int ARCH_ARM32 = 2;
00039     public static final int ARCH_X86_64 = 3;
00040     public static final int ARCH_X86 = 4;
00041 
00042     protected static boolean mIsTangoServiceBound;
00043     protected static boolean mIsTangoVersionOk;
00044 
00051     protected static native boolean setBinderTangoService(IBinder nativeTangoServiceBinder);
00052 
00059     protected static native boolean isTangoVersionOk(Activity callerActivity);
00060 
00061     // Getters for Tango Status variables
00062     public static boolean isTangoServiceBound() {
00063         return mIsTangoServiceBound;
00064     }
00065 
00066     public static boolean isTangoVersionOk() {
00067         return mIsTangoVersionOk;
00068     }
00069 
00077     public static final boolean bindTangoService(final Activity activity,
00078                                                  ServiceConnection connection) {
00079         testTangoVersionOk(activity);
00080 
00081         Intent intent = new Intent();
00082         intent.setClassName("com.google.tango", "com.google.atap.tango.TangoService");
00083 
00084         boolean hasJavaService = (activity.getPackageManager().resolveService(intent, 0) != null);
00085 
00086         // User doesn't have the latest packagename for TangoCore, fallback to the previous name.
00087         if (!hasJavaService) {
00088             intent = new Intent();
00089             intent.setClassName("com.projecttango.tango", "com.google.atap.tango.TangoService");
00090             hasJavaService = (activity.getPackageManager().resolveService(intent, 0) != null);
00091         }
00092 
00093         // User doesn't have a Java-fied TangoCore at all; fallback to the deprecated approach
00094         // of doing nothing and letting the native side auto-init to the system-service version
00095         // of Tango.
00096         if (!hasJavaService) {
00097             return false;
00098         }
00099 
00100         return activity.bindService(intent, connection, Context.BIND_AUTO_CREATE);
00101     }
00102 
00107     public static final void unbindTangoService(final Context context,
00108                                                  ServiceConnection connection) {
00109         context.unbindService(connection);
00110         mIsTangoServiceBound = false;
00111     }
00112 
00113 
00119     public static final int loadTangoSharedLibrary() {
00120         int loadedSoId = ARCH_ERROR;
00121         String basePath = "/data/data/com.google.tango/libfiles/";
00122         if (!(new File(basePath).exists())) {
00123             basePath = "/data/data/com.projecttango.tango/libfiles/";
00124         }
00125         Log.i("TangoInitializationHelp", "basePath: " + basePath);
00126 
00127         try {
00128             System.load(basePath + "arm64-v8a/libtango_client_api.so");
00129             loadedSoId = ARCH_ARM64;
00130             Log.i("TangoInitializationHelp", "Success! Using arm64-v8a/libtango_client_api.");
00131         } catch (UnsatisfiedLinkError e) {
00132             Log.e("TangoInitializationHelp", e.toString());
00133         }
00134         if (loadedSoId < ARCH_DEFAULT) {
00135             try {
00136                 System.load(basePath + "armeabi-v7a/libtango_client_api.so");
00137                 loadedSoId = ARCH_ARM32;
00138                 Log.i("TangoInitializationHelp", "Success! Using armeabi-v7a/libtango_client_api.");
00139             } catch (UnsatisfiedLinkError e) {
00140                 Log.e("TangoInitializationHelp", e.toString());
00141             }
00142         }
00143         if (loadedSoId < ARCH_DEFAULT) {
00144             try {
00145                 System.load(basePath + "x86_64/libtango_client_api.so");
00146                 loadedSoId = ARCH_X86_64;
00147                 Log.i("TangoInitializationHelp", "Success! Using x86_64/libtango_client_api.");
00148             } catch (UnsatisfiedLinkError e) {
00149                 Log.e("TangoInitializationHelp", e.toString());
00150             }
00151         }
00152         if (loadedSoId < ARCH_DEFAULT) {
00153             try {
00154                 System.load(basePath + "x86/libtango_client_api.so");
00155                 loadedSoId = ARCH_X86;
00156                 Log.i("TangoInitializationHelp", "Success! Using x86/libtango_client_api.");
00157             } catch (UnsatisfiedLinkError e) {
00158                 Log.e("TangoInitializationHelp", e.toString());
00159             }
00160         }
00161         if (loadedSoId < ARCH_DEFAULT) {
00162             try {
00163                 System.load(basePath + "default/libtango_client_api.so");
00164                 loadedSoId = ARCH_DEFAULT;
00165                 Log.i("TangoInitializationHelp", "Success! Using default/libtango_client_api.");
00166             } catch (UnsatisfiedLinkError e) {
00167                 Log.e("TangoInitializationHelp", e.toString());
00168             }
00169         }
00170         if (loadedSoId < ARCH_DEFAULT) {
00171             try {
00172                 System.loadLibrary("tango_client_api");
00173                 loadedSoId = ARCH_FALLBACK;
00174                 Log.i("TangoInitializationHelp", "Falling back to libtango_client_api.so symlink.");
00175             } catch (UnsatisfiedLinkError e) {
00176                 Log.e("TangoInitializationHelp", e.toString());
00177             }
00178         }
00179         return loadedSoId;
00180     }
00181 
00187     public static final int loadTangoRosNodeSharedLibrary() {
00188         int loadedSo = ARCH_ERROR;
00189         try {
00190             System.loadLibrary(TangoNodeletManager.DEFAULT_LIB_NAME);
00191             loadedSo = ARCH_DEFAULT;
00192         } catch (Exception e) {
00193             Log.e(TangoInitializationHelper.class.getName(),
00194                     "Error loading library " + TangoNodeletManager.DEFAULT_LIB_NAME, e);
00195         }
00196         return loadedSo;
00197     }
00198 
00203     public static ServiceConnection NewDefaultServiceConnection() {
00204         return new DefaultTangoServiceConnection(null);
00205     }
00206 
00212     public static ServiceConnection NewDefaultServiceConnection(
00213             DefaultTangoServiceConnection.AfterConnectionCallback callback) {
00214         return new DefaultTangoServiceConnection(callback);
00215     }
00216 
00222     private static boolean testTangoVersionOk(Activity activity) {
00223         mIsTangoVersionOk = isTangoVersionOk(activity);
00224         return mIsTangoVersionOk;
00225     }
00226 
00234     public static class DefaultTangoServiceConnection implements ServiceConnection {
00235 
00236         private AfterConnectionCallback connectionErrorCallback;
00237 
00238         public DefaultTangoServiceConnection(AfterConnectionCallback callback) {
00239             connectionErrorCallback = callback;
00240         }
00241 
00242         public static interface AfterConnectionCallback {
00243             public void execute();
00244         }
00245 
00246         public void onServiceConnected(ComponentName name, IBinder service) {
00247             // Synchronization around RunningActivity object is to avoid
00248             // Tango disconnect in the middle of the connecting operation.
00249             mIsTangoServiceBound = setBinderTangoService(service);
00250 
00251             if (connectionErrorCallback != null) {
00252                 connectionErrorCallback.execute();
00253             }
00254         }
00255 
00256         public void onServiceDisconnected(ComponentName name) {
00257             // Handle this if you need to gracefully shutdown/retry
00258             // in the event that Tango itself crashes/gets upgraded while running.
00259         }
00260     }
00261 }


TangoRosStreamer
Author(s):
autogenerated on Thu Jun 6 2019 19:49:58