1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.cts.verifier.sensors.helpers;
17 
18 import android.content.ComponentName;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.ServiceConnection;
22 import android.os.IBinder;
23 import android.util.Log;
24 
25 import org.opencv.android.BaseLoaderCallback;
26 import org.opencv.android.LoaderCallbackInterface;
27 import org.opencv.android.OpenCVLoader;
28 
29 import java.util.concurrent.CountDownLatch;
30 import java.util.concurrent.TimeUnit;
31 
32 /**
33  * OpenCV library loader class
34  */
35 public class OpenCVLibrary {
36 
37     private final static String TAG = "OpenCVLibraryProbe";
38     private final static int ASYNC_LOAD_TIMEOUT_SEC = 30;
39     private static boolean sLoaded = false;
40 
41     /**
42      * Load OpenCV Library in async mode
43      *
44      * @param context Activity context.
45      * @param allowStatic Allow trying load from local package.
46      * @param allowInstall Allow installing package from play store.
47      *
48      * @return if load succeed return true. Return false otherwise.
49      */
load(Context context, boolean allowLocal, boolean allowPackage, boolean allowInstall)50     public static boolean load(Context context,
51             boolean allowLocal, boolean allowPackage, boolean allowInstall) {
52         // only need to load once
53         if (!sLoaded) {
54             // Try static load first
55             if (allowLocal && OpenCVLoader.initDebug()) {
56                 sLoaded = true;
57             } else if (allowPackage) {
58                 if (allowInstall || probePackage(context)) {
59                     final CountDownLatch done = new CountDownLatch(1);
60                     // Load the library through async loader
61                     OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, context,
62                             new BaseLoaderCallback(context) {
63                                 @Override
64                                 public void onManagerConnected(int status) {
65                                     Log.v(TAG, "New Loading status: " + status);
66                                     switch (status) {
67                                         case LoaderCallbackInterface.SUCCESS: {
68                                             sLoaded = true;
69                                         }
70                                         break;
71                                         default: {
72                                             Log.e(TAG, "Connecting OpenCV Manager failed");
73                                         }
74                                         break;
75                                     }
76                                     done.countDown();
77                                 }
78                             });
79                     try {
80                         if (!done.await(ASYNC_LOAD_TIMEOUT_SEC, TimeUnit.SECONDS)) {
81                             Log.e(TAG, "Time out when attempt async load");
82                         }
83                     } catch (InterruptedException e) {
84                         Thread.currentThread().interrupt();
85                     }
86                 }
87             }
88         }
89         return sLoaded;
90     }
91 
92     /**
93      * Test if the library is loaded
94      * @return a boolean indicates whether the OpenCV library is loaded.
95      */
isLoaded()96     public static boolean isLoaded() {
97         return sLoaded;
98     }
99 
100     /**
101      * Probe if the OpenCV Manager package is installed
102      *
103      * @return a boolean indicate wheather OpenCV Manager is installed
104      */
probePackage(Context context)105     private static boolean probePackage(Context context) {
106         Intent intent = new Intent("org.opencv.engine.BIND");
107         intent.setPackage("org.opencv.engine");
108 
109         ServiceConnection conn = new ServiceConnection() {
110             @Override
111             public void onServiceConnected(ComponentName className, IBinder service) {
112                 // Do nothing
113             }
114             @Override
115             public void onServiceDisconnected(ComponentName className) {
116                 // Do nothing
117             }
118         };
119 
120         boolean ret = false;
121         try {
122             if (context.bindService(intent, conn, Context.BIND_AUTO_CREATE)) {
123                 ret = true;
124             }
125         } finally {
126             context.unbindService(conn);
127         }
128 
129         return ret;
130     }
131 }
132