From 306ed9801a28533bb15ef0aeb63225df43b6c058 Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Wed, 10 Oct 2012 11:21:54 +0400 Subject: [PATCH] Issue #2284 BaseLoaderCallback requires use of Activity fixed. --- android/service/doc/BaseLoaderCallback.rst | 49 +++++++++++++++++++ .../dev_with_OCV_on_Android.rst | 2 + .../src/java/android+BaseLoaderCallback.java | 18 ++++--- .../src/java/android+android+sync.py | 7 --- 4 files changed, 63 insertions(+), 13 deletions(-) delete mode 100644 modules/java/generator/src/java/android+android+sync.py diff --git a/android/service/doc/BaseLoaderCallback.rst b/android/service/doc/BaseLoaderCallback.rst index e30f7c0d1c..f756db46bf 100644 --- a/android/service/doc/BaseLoaderCallback.rst +++ b/android/service/doc/BaseLoaderCallback.rst @@ -9,3 +9,52 @@ Base Loader Callback Interface implementation .. image:: img/AndroidAppUsageModel.png +Using in Java Activity +---------------------- + +There is a very base code snippet implementing the async initialization with BaseLoaderCallback. See the "15-puzzle" OpenCV sample for details. + +.. code-block:: java + :linenos: + + public class MyActivity extends Activity implements HelperCallbackInterface + { + private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) { + @Override + public void onManagerConnected(int status) { + switch (status) { + case LoaderCallbackInterface.SUCCESS: + { + Log.i(TAG, "OpenCV loaded successfully"); + // Create and set View + mView = new puzzle15View(mAppContext); + setContentView(mView); + } break; + default: + { + super.onManagerConnected(status); + } break; + } + } + }; + + /** Call on every application resume **/ + @Override + protected void onResume() + { + Log.i(TAG, "called onResume"); + super.onResume(); + + Log.i(TAG, "Trying to load OpenCV library"); + if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack)) + { + Log.e(TAG, "Cannot connect to OpenCV Manager"); + } + } + +Using in Service +---------------- + +Default BaseLoaderCallback implementation treat application context as Activity and calls Activity.finish() method to exit in case of initialization failure. +To override this behavior you need to override finish() method of BaseLoaderCallback class and implement your own finalization method. + diff --git a/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.rst b/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.rst index 2ea57115ff..160f02591e 100644 --- a/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.rst +++ b/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.rst @@ -112,6 +112,8 @@ There is a very base code snippet implementing the async initialization. It show It this case application works with OpenCV Manager in asynchronous fashion. ``OnManagerConnected`` callback will be called in UI thread, when initialization finishes. Please note, that it is not allowed to use OpenCV calls or load OpenCV-dependent native libs before invoking this callback. Load your own native libraries that depend on OpenCV after the successful OpenCV initialization. +Default BaseLoaderCallback implementation treat application context as Activity and calls Activity.finish() method to exit in case of initialization failure. +To override this behavior you need to override finish() method of BaseLoaderCallback class and implement your own finalization method. Application development with static initialization ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/modules/java/generator/src/java/android+BaseLoaderCallback.java b/modules/java/generator/src/java/android+BaseLoaderCallback.java index bbb1230580..58045e33ee 100644 --- a/modules/java/generator/src/java/android+BaseLoaderCallback.java +++ b/modules/java/generator/src/java/android+BaseLoaderCallback.java @@ -2,6 +2,7 @@ package org.opencv.android; import android.app.Activity; import android.app.AlertDialog; +import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.util.Log; @@ -11,7 +12,7 @@ import android.util.Log; */ public abstract class BaseLoaderCallback implements LoaderCallbackInterface { - public BaseLoaderCallback(Activity AppContext) { + public BaseLoaderCallback(Context AppContext) { mAppContext = AppContext; } @@ -34,7 +35,7 @@ public abstract class BaseLoaderCallback implements LoaderCallbackInterface { MarketErrorMessage.setCancelable(false); // This blocks the 'BACK' button MarketErrorMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { - mAppContext.finish(); + finish(); } }); MarketErrorMessage.show(); @@ -43,7 +44,7 @@ public abstract class BaseLoaderCallback implements LoaderCallbackInterface { case LoaderCallbackInterface.INSTALL_CANCELED: { Log.d(TAG, "OpenCV library instalation was canceled by user"); - mAppContext.finish(); + finish(); } break; /** Application is incompatible with this version of OpenCV Manager. Possibly, a service update is required. **/ case LoaderCallbackInterface.INCOMPATIBLE_MANAGER_VERSION: @@ -55,7 +56,7 @@ public abstract class BaseLoaderCallback implements LoaderCallbackInterface { IncomatibilityMessage.setCancelable(false); // This blocks the 'BACK' button IncomatibilityMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { - mAppContext.finish(); + finish(); } }); IncomatibilityMessage.show(); @@ -71,7 +72,7 @@ public abstract class BaseLoaderCallback implements LoaderCallbackInterface { InitFailedDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { - mAppContext.finish(); + finish(); } }); @@ -130,6 +131,11 @@ public abstract class BaseLoaderCallback implements LoaderCallbackInterface { } } - protected Activity mAppContext; + void finish() + { + ((Activity) mAppContext).finish(); + } + + protected Context mAppContext; private final static String TAG = "OpenCVLoader/BaseLoaderCallback"; } diff --git a/modules/java/generator/src/java/android+android+sync.py b/modules/java/generator/src/java/android+android+sync.py deleted file mode 100644 index baf95cb6e1..0000000000 --- a/modules/java/generator/src/java/android+android+sync.py +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/python - -import os -import shutil - -for f in os.listdir("."): - shutil.copyfile(f, os.path.join("../../../../../../modules/java/generator/src/java/", "android+" + f)); \ No newline at end of file