From 11839e2cc94455560781cd2251b116bafb3249ff Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Mon, 8 Oct 2012 12:58:14 +0400 Subject: [PATCH] Wating for OpenCV Manager installation added. --- .../src/java/android+AsyncServiceHelper.java | 134 ++++++++++++++------- .../src/java/android+BaseLoaderCallback.java | 70 ++++++----- .../src/java/android+InstallCallbackInterface.java | 13 ++ .../src/java/android+LoaderCallbackInterface.java | 2 +- .../generator/src/java/android+android+sync.py | 7 ++ 5 files changed, 155 insertions(+), 71 deletions(-) create mode 100644 modules/java/generator/src/java/android+android+sync.py diff --git a/modules/java/generator/src/java/android+AsyncServiceHelper.java b/modules/java/generator/src/java/android+AsyncServiceHelper.java index b5487b9..c612c90 100644 --- a/modules/java/generator/src/java/android+AsyncServiceHelper.java +++ b/modules/java/generator/src/java/android+AsyncServiceHelper.java @@ -33,8 +33,7 @@ class AsyncServiceHelper } } - protected AsyncServiceHelper(String Version, Context AppContext, - LoaderCallbackInterface Callback) + protected AsyncServiceHelper(String Version, Context AppContext, LoaderCallbackInterface Callback) { mOpenCVersion = Version; mUserAppCallback = Callback; @@ -48,62 +47,109 @@ class AsyncServiceHelper protected String mOpenCVersion; protected Context mAppContext; protected int mStatus = LoaderCallbackInterface.SUCCESS; + protected static boolean mServiceInstallationProgress = false; + protected static boolean mLibraryInstallationProgress = false; - private static void InstallService(final Context AppContext, final LoaderCallbackInterface Callback) + protected static boolean InstallServiceQuiet(Context context) { - InstallCallbackInterface InstallQuery = new InstallCallbackInterface() { - private Context mAppContext = AppContext; - private LoaderCallbackInterface mUserAppCallback = Callback; - public String getPackageName() - { - return "OpenCV Manager"; - } - public void install() { - Log.d(TAG, "Trying to install OpenCV Manager via Google Play"); + boolean result = true; + try + { + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(OPEN_CV_SERVICE_URL)); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(intent); + } + catch(Exception e) + { + result = false; + } - boolean result = true; - try + return result; + } + + protected static void InstallService(final Context AppContext, final LoaderCallbackInterface Callback) + { + if (!mServiceInstallationProgress) + { + Log.d(TAG, "Request new service installation"); + InstallCallbackInterface InstallQuery = new InstallCallbackInterface() { + private LoaderCallbackInterface mUserAppCallback = Callback; + public String getPackageName() { - Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(OPEN_CV_SERVICE_URL)); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - mAppContext.startActivity(intent); + return "OpenCV Manager"; } - catch(Exception e) - { - result = false; + public void install() { + Log.d(TAG, "Trying to install OpenCV Manager via Google Play"); + + boolean result = InstallServiceQuiet(AppContext); + if (result) + { + mServiceInstallationProgress = true; + int Status = LoaderCallbackInterface.RESTART_REQUIRED; + Log.d(TAG, "Init finished with status " + Status); + Log.d(TAG, "Calling using callback"); + mUserAppCallback.onManagerConnected(Status); + } + else + { + Log.d(TAG, "OpenCV package was not installed!"); + int Status = LoaderCallbackInterface.MARKET_ERROR; + Log.d(TAG, "Init finished with status " + Status); + Log.d(TAG, "Unbind from service"); + Log.d(TAG, "Calling using callback"); + mUserAppCallback.onManagerConnected(Status); + } } - if (result) + public void cancel() { - int Status = LoaderCallbackInterface.RESTART_REQUIRED; + Log.d(TAG, "OpenCV library installation was canceled"); + int Status = LoaderCallbackInterface.INSTALL_CANCELED; Log.d(TAG, "Init finished with status " + Status); Log.d(TAG, "Calling using callback"); mUserAppCallback.onManagerConnected(Status); } - else + + public void wait_install() + { + Log.e(TAG, "Instalation was not started! Nothing to wait!"); + } + }; + + Callback.onPackageInstall(InstallCallbackInterface.NEW_INSTALLATION, InstallQuery); + } + else + { + Log.d(TAG, "Wating current installation process"); + InstallCallbackInterface WaitQuery = new InstallCallbackInterface() { + private LoaderCallbackInterface mUserAppCallback = Callback; + public String getPackageName() + { + return "OpenCV Manager"; + } + public void install() { - Log.d(TAG, "OpenCV package was not installed!"); - int Status = LoaderCallbackInterface.MARKET_ERROR; + Log.e(TAG, "Nothing to install we just wait current installation"); + } + public void cancel() + { + Log.d(TAG, "Wating for OpenCV canceled by user"); + mServiceInstallationProgress = false; + int Status = LoaderCallbackInterface.INSTALL_CANCELED; Log.d(TAG, "Init finished with status " + Status); - Log.d(TAG, "Unbind from service"); Log.d(TAG, "Calling using callback"); mUserAppCallback.onManagerConnected(Status); } - } - - public void cancel() - { - Log.d(TAG, "OpenCV library installation was canceled"); - int Status = LoaderCallbackInterface.INSTALL_CANCELED; - Log.d(TAG, "Init finished with status " + Status); - Log.d(TAG, "Calling using callback"); - mUserAppCallback.onManagerConnected(Status); - } - }; + public void wait_install() + { + InstallServiceQuiet(AppContext); + } + }; - Callback.onPackageInstall(InstallQuery); + Callback.onPackageInstall(InstallCallbackInterface.INSTALLATION_PROGRESS, WaitQuery); + } } - + /** * URL of OpenCV Manager page on Google Play Market. */ @@ -122,6 +168,7 @@ class AsyncServiceHelper } else { + mServiceInstallationProgress = false; try { if (mEngineService.getEngineVersion() < MINIMUM_ENGINE_VERSION) @@ -177,9 +224,12 @@ class AsyncServiceHelper Log.d(TAG, "Calling using callback"); mUserAppCallback.onManagerConnected(mStatus); } + public void wait_install() { + Log.e(TAG, "Instalation was not started! Nothing to wait!"); + } }; - mUserAppCallback.onPackageInstall(InstallQuery); + mUserAppCallback.onPackageInstall(InstallCallbackInterface.NEW_INSTALLATION, InstallQuery); return; } else @@ -218,13 +268,13 @@ class AsyncServiceHelper } } } - + public void onServiceDisconnected(ComponentName className) { mEngineService = null; } }; - + private boolean loadLibrary(String AbsPath) { boolean result = true; diff --git a/modules/java/generator/src/java/android+BaseLoaderCallback.java b/modules/java/generator/src/java/android+BaseLoaderCallback.java index 4fb5a23..40f1d6c 100644 --- a/modules/java/generator/src/java/android+BaseLoaderCallback.java +++ b/modules/java/generator/src/java/android+BaseLoaderCallback.java @@ -27,18 +27,7 @@ public abstract class BaseLoaderCallback implements LoaderCallbackInterface { /** OpenCV Manager or library package installation is in progress. Restart the application. **/ case LoaderCallbackInterface.RESTART_REQUIRED: { - Log.d(TAG, "OpenCV downloading. App restart is needed!"); - AlertDialog RestartMessage = new AlertDialog.Builder(mAppContext).create(); - RestartMessage.setTitle("App restart is required"); - RestartMessage.setMessage("Application will be closed now. Start it when installation will be finished!"); - RestartMessage.setCancelable(false); // This blocks the 'BACK' button - RestartMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - mAppContext.finish(); - } - }); - - RestartMessage.show(); + Log.d(TAG, "OpenCV downloading. App restart is needed!"); } break; /** OpenCV loader can not start Google Play Market. **/ case LoaderCallbackInterface.MARKET_ERROR: @@ -96,29 +85,54 @@ public abstract class BaseLoaderCallback implements LoaderCallbackInterface { } } - public void onPackageInstall(final InstallCallbackInterface callback) + public void onPackageInstall(final int operation, final InstallCallbackInterface callback) { - AlertDialog InstallMessage = new AlertDialog.Builder(mAppContext).create(); - InstallMessage.setTitle("Package not found"); - InstallMessage.setMessage(callback.getPackageName() + " package was not found! Try to install it?"); - InstallMessage.setCancelable(false); // This blocks the 'BACK' button - InstallMessage.setButton(AlertDialog.BUTTON_POSITIVE, "Yes", new OnClickListener() + switch (operation) { - public void onClick(DialogInterface dialog, int which) + case InstallCallbackInterface.NEW_INSTALLATION: { - callback.install(); - } - }); + AlertDialog InstallMessage = new AlertDialog.Builder(mAppContext).create(); + InstallMessage.setTitle("Package not found"); + InstallMessage.setMessage(callback.getPackageName() + " package was not found! Try to install it?"); + InstallMessage.setCancelable(false); // This blocks the 'BACK' button + InstallMessage.setButton(AlertDialog.BUTTON_POSITIVE, "Yes", new OnClickListener() + { + public void onClick(DialogInterface dialog, int which) + { + callback.install(); + } + }); + + InstallMessage.setButton(AlertDialog.BUTTON_NEGATIVE, "No", new OnClickListener() { - InstallMessage.setButton(AlertDialog.BUTTON_NEGATIVE, "No", new OnClickListener() { + public void onClick(DialogInterface dialog, int which) + { + callback.cancel(); + } + }); - public void onClick(DialogInterface dialog, int which) + InstallMessage.show(); + } break; + case InstallCallbackInterface.INSTALLATION_PROGRESS: { - callback.cancel(); - } - }); + AlertDialog WaitMessage = new AlertDialog.Builder(mAppContext).create(); + WaitMessage.setTitle("OpenCV is not ready"); + WaitMessage.setMessage("Installation is in progeress. Wait or exit?"); + WaitMessage.setCancelable(false); // This blocks the 'BACK' button + WaitMessage.setButton(AlertDialog.BUTTON_POSITIVE, "Wait", new OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + callback.wait_install(); + } + }); + WaitMessage.setButton(AlertDialog.BUTTON_NEGATIVE, "Exit", new OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + callback.cancel(); + } + }); - InstallMessage.show(); + WaitMessage.show(); + } break; + } } protected Activity mAppContext; diff --git a/modules/java/generator/src/java/android+InstallCallbackInterface.java b/modules/java/generator/src/java/android+InstallCallbackInterface.java index 12785d9..f68027a 100644 --- a/modules/java/generator/src/java/android+InstallCallbackInterface.java +++ b/modules/java/generator/src/java/android+InstallCallbackInterface.java @@ -6,6 +6,15 @@ package org.opencv.android; public interface InstallCallbackInterface { /** + * New package installation is required. + */ + static final int NEW_INSTALLATION = 0; + /** + * Current package installation is in progress. + */ + static final int INSTALLATION_PROGRESS = 1; + + /** * Target package name. * @return Return target package name. */ @@ -18,4 +27,8 @@ public interface InstallCallbackInterface * Installation is canceled. */ public void cancel(); + /** + * Wait for package installation. + */ + public void wait_install(); }; diff --git a/modules/java/generator/src/java/android+LoaderCallbackInterface.java b/modules/java/generator/src/java/android+LoaderCallbackInterface.java index 0d932c7..aca80a8 100644 --- a/modules/java/generator/src/java/android+LoaderCallbackInterface.java +++ b/modules/java/generator/src/java/android+LoaderCallbackInterface.java @@ -40,5 +40,5 @@ public interface LoaderCallbackInterface * Callback method, called in case the package installation is needed. * @param callback answer object with approve and cancel methods and the package description. */ - public void onPackageInstall(InstallCallbackInterface callback); + public void onPackageInstall(final int operation, InstallCallbackInterface callback); }; diff --git a/modules/java/generator/src/java/android+android+sync.py b/modules/java/generator/src/java/android+android+sync.py new file mode 100644 index 0000000..baf95cb --- /dev/null +++ b/modules/java/generator/src/java/android+android+sync.py @@ -0,0 +1,7 @@ +#!/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 -- 2.7.4