From ec6f0e1baf396f2bc25d4ce50c686702cf83981d Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Wed, 10 Apr 2013 10:43:22 +0400 Subject: [PATCH] Incompatible hardware detetction added to OpenCV Manager(Feature #2941) --- .../org/opencv/engine/BinderConnector.java | 42 ++++++++--- .../org/opencv/engine/HardwareDetector.java | 16 +++-- .../opencv/engine/OpenCVEngineService.java | 47 +++++++++--- .../engine/manager/ManagerActivity.java | 72 ++++++++++++++----- 4 files changed, 135 insertions(+), 42 deletions(-) diff --git a/android/service/engine/src/org/opencv/engine/BinderConnector.java b/android/service/engine/src/org/opencv/engine/BinderConnector.java index fd23fbfe49..bde54d5b96 100644 --- a/android/service/engine/src/org/opencv/engine/BinderConnector.java +++ b/android/service/engine/src/org/opencv/engine/BinderConnector.java @@ -4,23 +4,43 @@ import android.os.IBinder; public class BinderConnector { - public BinderConnector(MarketConnector Market) - { - Init(Market); + public BinderConnector(MarketConnector Market) { + mMarket = Market; + } + + public boolean Init() { + boolean result = false; + if (mIsReady) + result = Init(mMarket); + + return result; } + public native IBinder Connect(); + public boolean Disconnect() { - Final(); - return true; - } + if (mIsReady) + Final(); - static - { - System.loadLibrary("OpenCVEngine"); - System.loadLibrary("OpenCVEngine_jni"); + return mIsReady; } private native boolean Init(MarketConnector Market); - public native void Final(); + private native void Final(); + private static boolean mIsReady = false; + private MarketConnector mMarket; + + static { + try { + System.loadLibrary("OpenCVEngine"); + System.loadLibrary("OpenCVEngine_jni"); + mIsReady = true; + } + catch(UnsatisfiedLinkError e) { + mIsReady = false; + e.printStackTrace(); + } + } + } diff --git a/android/service/engine/src/org/opencv/engine/HardwareDetector.java b/android/service/engine/src/org/opencv/engine/HardwareDetector.java index 67320865af..7fc7e1ae8a 100644 --- a/android/service/engine/src/org/opencv/engine/HardwareDetector.java +++ b/android/service/engine/src/org/opencv/engine/HardwareDetector.java @@ -47,9 +47,17 @@ public class HardwareDetector public static native int DetectKnownPlatforms(); - static - { - System.loadLibrary("OpenCVEngine"); - System.loadLibrary("OpenCVEngine_jni"); + public static boolean mIsReady = false; + + static { + try { + System.loadLibrary("OpenCVEngine"); + System.loadLibrary("OpenCVEngine_jni"); + mIsReady = true; + } + catch(UnsatisfiedLinkError e) { + mIsReady = false; + e.printStackTrace(); + } } } diff --git a/android/service/engine/src/org/opencv/engine/OpenCVEngineService.java b/android/service/engine/src/org/opencv/engine/OpenCVEngineService.java index df31c7fe8e..b3c4ea0575 100644 --- a/android/service/engine/src/org/opencv/engine/OpenCVEngineService.java +++ b/android/service/engine/src/org/opencv/engine/OpenCVEngineService.java @@ -3,31 +3,62 @@ package org.opencv.engine; import android.app.Service; import android.content.Intent; import android.os.IBinder; +import android.os.RemoteException; import android.util.Log; - public class OpenCVEngineService extends Service { private static final String TAG = "OpenCVEngine/Service"; - private IBinder mEngineInterface; + private IBinder mEngineInterface = null; private MarketConnector mMarket; private BinderConnector mNativeBinder; - public void onCreate() - { + + public void onCreate() { Log.i(TAG, "Service starting"); super.onCreate(); Log.i(TAG, "Engine binder component creating"); mMarket = new MarketConnector(getBaseContext()); mNativeBinder = new BinderConnector(mMarket); - mEngineInterface = mNativeBinder.Connect(); - Log.i(TAG, "Service started successfully"); + if (mNativeBinder.Init()) { + mEngineInterface = mNativeBinder.Connect(); + Log.i(TAG, "Service started successfully"); + } else { + Log.e(TAG, "Cannot initialize native part of OpenCV Manager!"); + Log.e(TAG, "Using stub instead"); + + mEngineInterface = new OpenCVEngineInterface.Stub() { + + @Override + public boolean installVersion(String version) throws RemoteException { + // TODO Auto-generated method stub + return false; + } + + @Override + public String getLibraryList(String version) throws RemoteException { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getLibPathByVersion(String version) throws RemoteException { + // TODO Auto-generated method stub + return null; + } + + @Override + public int getEngineVersion() throws RemoteException { + return -1; + } + }; + } } - public IBinder onBind(Intent intent) - { + public IBinder onBind(Intent intent) { Log.i(TAG, "Service onBind called for intent " + intent.toString()); return mEngineInterface; } + public boolean onUnbind(Intent intent) { Log.i(TAG, "Service onUnbind called for intent " + intent.toString()); diff --git a/android/service/engine/src/org/opencv/engine/manager/ManagerActivity.java b/android/service/engine/src/org/opencv/engine/manager/ManagerActivity.java index 5213d91495..3c1aac994a 100644 --- a/android/service/engine/src/org/opencv/engine/manager/ManagerActivity.java +++ b/android/service/engine/src/org/opencv/engine/manager/ManagerActivity.java @@ -42,6 +42,26 @@ public class ManagerActivity extends Activity @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + + if (!HardwareDetector.mIsReady) { + Log.e(TAG, "Cannot initialize native part of OpenCV Manager!"); + + AlertDialog dialog = new AlertDialog.Builder(this).create(); + + dialog.setTitle("OpenCV Manager Error"); + dialog.setMessage("OpenCV Manager is incompatible with this device. Please replace it with an appropriate package."); + dialog.setCancelable(false); + dialog.setButton("OK", new DialogInterface.OnClickListener() { + + public void onClick(DialogInterface dialog, int which) { + finish(); + } + }); + + dialog.show(); + return; + } + setContentView(R.layout.main); TextView OsVersionView = (TextView)findViewById(R.id.OsVersionValue); @@ -186,6 +206,20 @@ public class ManagerActivity extends Activity } }); + mPackageChangeReciever = new BroadcastReceiver() { + + @Override + public void onReceive(Context context, Intent intent) { + Log.d("OpenCVManager/Reciever", "Bradcast message " + intent.getAction() + " reciever"); + Log.d("OpenCVManager/Reciever", "Filling package list on broadcast message"); + if (!bindService(new Intent("org.opencv.engine.BIND"), new OpenCVEngineServiceConnection(), Context.BIND_AUTO_CREATE)) + { + TextView EngineVersionView = (TextView)findViewById(R.id.EngineVersionValue); + EngineVersionView.setText("not avaliable"); + } + } + }; + IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_PACKAGE_ADDED); filter.addAction(Intent.ACTION_PACKAGE_CHANGED); @@ -199,17 +233,23 @@ public class ManagerActivity extends Activity @Override protected void onDestroy() { super.onDestroy(); - unregisterReceiver(mPackageChangeReciever); + if (mPackageChangeReciever != null) + unregisterReceiver(mPackageChangeReciever); } @Override protected void onResume() { super.onResume(); - Log.d(TAG, "Filling package list on resume"); - if (!bindService(new Intent("org.opencv.engine.BIND"), new OpenCVEngineServiceConnection(), Context.BIND_AUTO_CREATE)) - { - TextView EngineVersionView = (TextView)findViewById(R.id.EngineVersionValue); - EngineVersionView.setText("not avaliable"); + if (HardwareDetector.mIsReady) { + Log.d(TAG, "Filling package list on resume"); + OpenCVEngineServiceConnection connection = new OpenCVEngineServiceConnection(); + if (!bindService(new Intent("org.opencv.engine.BIND"), connection, Context.BIND_AUTO_CREATE)) { + Log.e(TAG, "Cannot bind to OpenCV Manager service!"); + TextView EngineVersionView = (TextView)findViewById(R.id.EngineVersionValue); + if (EngineVersionView != null) + EngineVersionView.setText("not avaliable"); + unbindService(connection); + } } } @@ -225,19 +265,7 @@ public class ManagerActivity extends Activity protected int ManagerApiLevel = 0; protected String ManagerVersion; - protected BroadcastReceiver mPackageChangeReciever = new BroadcastReceiver() { - - @Override - public void onReceive(Context context, Intent intent) { - Log.d("OpenCVManager/Reciever", "Bradcast message " + intent.getAction() + " reciever"); - Log.d("OpenCVManager/Reciever", "Filling package list on broadcast message"); - if (!bindService(new Intent("org.opencv.engine.BIND"), new OpenCVEngineServiceConnection(), Context.BIND_AUTO_CREATE)) - { - TextView EngineVersionView = (TextView)findViewById(R.id.EngineVersionValue); - EngineVersionView.setText("not avaliable"); - } - } - }; + protected BroadcastReceiver mPackageChangeReciever = null; protected class OpenCVEngineServiceConnection implements ServiceConnection { @@ -246,6 +274,12 @@ public class ManagerActivity extends Activity public void onServiceConnected(ComponentName name, IBinder service) { OpenCVEngineInterface EngineService = OpenCVEngineInterface.Stub.asInterface(service); + if (EngineService == null) { + Log.e(TAG, "Cannot connect to OpenCV Manager Service!"); + unbindService(this); + return; + } + try { ManagerApiLevel = EngineService.getEngineVersion(); } catch (RemoteException e) {