|
| 1 | +package org.opencv.android; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.app.AlertDialog; |
| 5 | +import android.content.Context; |
| 6 | +import android.content.DialogInterface; |
| 7 | +import android.content.DialogInterface.OnClickListener; |
| 8 | +import android.util.Log; |
| 9 | + |
| 10 | +/** |
| 11 | + * Basic implementation of LoaderCallbackInterface. |
| 12 | + */ |
| 13 | +public abstract class BaseLoaderCallback implements LoaderCallbackInterface { |
| 14 | + |
| 15 | + public BaseLoaderCallback(Context AppContext) { |
| 16 | + mAppContext = AppContext; |
| 17 | + } |
| 18 | + |
| 19 | + public void onManagerConnected(int status) |
| 20 | + { |
| 21 | + switch (status) |
| 22 | + { |
| 23 | + /** OpenCV initialization was successful. **/ |
| 24 | + case LoaderCallbackInterface.SUCCESS: |
| 25 | + { |
| 26 | + /** Application must override this method to handle successful library initialization. **/ |
| 27 | + } break; |
| 28 | + /** OpenCV loader can not start Google Play Market. **/ |
| 29 | + case LoaderCallbackInterface.MARKET_ERROR: |
| 30 | + { |
| 31 | + Log.e(TAG, "Package installation failed!"); |
| 32 | + AlertDialog MarketErrorMessage = new AlertDialog.Builder(mAppContext).create(); |
| 33 | + MarketErrorMessage.setTitle("OpenCV Manager"); |
| 34 | + MarketErrorMessage.setMessage("Package installation failed!"); |
| 35 | + MarketErrorMessage.setCancelable(false); // This blocks the 'BACK' button |
| 36 | + MarketErrorMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new OnClickListener() { |
| 37 | + public void onClick(DialogInterface dialog, int which) { |
| 38 | + finish(); |
| 39 | + } |
| 40 | + }); |
| 41 | + MarketErrorMessage.show(); |
| 42 | + } break; |
| 43 | + /** Package installation has been canceled. **/ |
| 44 | + case LoaderCallbackInterface.INSTALL_CANCELED: |
| 45 | + { |
| 46 | + Log.d(TAG, "OpenCV library installation was canceled by user"); |
| 47 | + finish(); |
| 48 | + } break; |
| 49 | + /** Application is incompatible with this version of OpenCV Manager. Possibly, a service update is required. **/ |
| 50 | + case LoaderCallbackInterface.INCOMPATIBLE_MANAGER_VERSION: |
| 51 | + { |
| 52 | + Log.d(TAG, "OpenCV Manager Service is uncompatible with this app!"); |
| 53 | + AlertDialog IncomatibilityMessage = new AlertDialog.Builder(mAppContext).create(); |
| 54 | + IncomatibilityMessage.setTitle("OpenCV Manager"); |
| 55 | + IncomatibilityMessage.setMessage("OpenCV Manager service is incompatible with this app. Try to update it via Google Play."); |
| 56 | + IncomatibilityMessage.setCancelable(false); // This blocks the 'BACK' button |
| 57 | + IncomatibilityMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new OnClickListener() { |
| 58 | + public void onClick(DialogInterface dialog, int which) { |
| 59 | + finish(); |
| 60 | + } |
| 61 | + }); |
| 62 | + IncomatibilityMessage.show(); |
| 63 | + } break; |
| 64 | + /** Other status, i.e. INIT_FAILED. **/ |
| 65 | + default: |
| 66 | + { |
| 67 | + Log.e(TAG, "OpenCV loading failed!"); |
| 68 | + AlertDialog InitFailedDialog = new AlertDialog.Builder(mAppContext).create(); |
| 69 | + InitFailedDialog.setTitle("OpenCV error"); |
| 70 | + InitFailedDialog.setMessage("OpenCV was not initialised correctly. Application will be shut down"); |
| 71 | + InitFailedDialog.setCancelable(false); // This blocks the 'BACK' button |
| 72 | + InitFailedDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new OnClickListener() { |
| 73 | + |
| 74 | + public void onClick(DialogInterface dialog, int which) { |
| 75 | + finish(); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + InitFailedDialog.show(); |
| 80 | + } break; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public void onPackageInstall(final int operation, final InstallCallbackInterface callback) |
| 85 | + { |
| 86 | + switch (operation) |
| 87 | + { |
| 88 | + case InstallCallbackInterface.NEW_INSTALLATION: |
| 89 | + { |
| 90 | + AlertDialog InstallMessage = new AlertDialog.Builder(mAppContext).create(); |
| 91 | + InstallMessage.setTitle("Package not found"); |
| 92 | + InstallMessage.setMessage(callback.getPackageName() + " package was not found! Try to install it?"); |
| 93 | + InstallMessage.setCancelable(false); // This blocks the 'BACK' button |
| 94 | + InstallMessage.setButton(AlertDialog.BUTTON_POSITIVE, "Yes", new OnClickListener() |
| 95 | + { |
| 96 | + public void onClick(DialogInterface dialog, int which) |
| 97 | + { |
| 98 | + callback.install(); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + InstallMessage.setButton(AlertDialog.BUTTON_NEGATIVE, "No", new OnClickListener() { |
| 103 | + |
| 104 | + public void onClick(DialogInterface dialog, int which) |
| 105 | + { |
| 106 | + callback.cancel(); |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + InstallMessage.show(); |
| 111 | + } break; |
| 112 | + case InstallCallbackInterface.INSTALLATION_PROGRESS: |
| 113 | + { |
| 114 | + AlertDialog WaitMessage = new AlertDialog.Builder(mAppContext).create(); |
| 115 | + WaitMessage.setTitle("OpenCV is not ready"); |
| 116 | + WaitMessage.setMessage("Installation is in progress. Wait or exit?"); |
| 117 | + WaitMessage.setCancelable(false); // This blocks the 'BACK' button |
| 118 | + WaitMessage.setButton(AlertDialog.BUTTON_POSITIVE, "Wait", new OnClickListener() { |
| 119 | + public void onClick(DialogInterface dialog, int which) { |
| 120 | + callback.wait_install(); |
| 121 | + } |
| 122 | + }); |
| 123 | + WaitMessage.setButton(AlertDialog.BUTTON_NEGATIVE, "Exit", new OnClickListener() { |
| 124 | + public void onClick(DialogInterface dialog, int which) { |
| 125 | + callback.cancel(); |
| 126 | + } |
| 127 | + }); |
| 128 | + |
| 129 | + WaitMessage.show(); |
| 130 | + } break; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + void finish() |
| 135 | + { |
| 136 | + ((Activity) mAppContext).finish(); |
| 137 | + } |
| 138 | + |
| 139 | + protected Context mAppContext; |
| 140 | + private final static String TAG = "OpenCVLoader/BaseLoaderCallback"; |
| 141 | +} |
0 commit comments