Issue #2284 BaseLoaderCallback requires use of Activity fixed.
authorAlexander Smorkalov <alexander.smorkalov@itseez.com>
Wed, 10 Oct 2012 07:21:54 +0000 (11:21 +0400)
committerAlexander Smorkalov <alexander.smorkalov@itseez.com>
Wed, 10 Oct 2012 07:44:01 +0000 (11:44 +0400)
android/service/doc/BaseLoaderCallback.rst
doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.rst
modules/java/generator/src/java/android+BaseLoaderCallback.java
modules/java/generator/src/java/android+android+sync.py [deleted file]

index e30f7c0..f756db4 100644 (file)
@@ -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. 
+
index 2ea5711..160f025 100644 (file)
@@ -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
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
index bbb1230..58045e3 100644 (file)
@@ -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 (file)
index baf95cb..0000000
+++ /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