1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.shape_detection;
6 
7 import android.content.Context;
8 import android.content.pm.PackageInfo;
9 import android.content.pm.PackageManager.NameNotFoundException;
10 
11 import com.google.android.gms.common.GoogleApiAvailability;
12 
13 import org.chromium.base.ContextUtils;
14 import org.chromium.base.Log;
15 import org.chromium.gms.ChromiumPlayServicesAvailability;
16 import org.chromium.mojo.bindings.InterfaceRequest;
17 import org.chromium.mojo.system.MojoException;
18 import org.chromium.shape_detection.mojom.BarcodeDetection;
19 import org.chromium.shape_detection.mojom.BarcodeDetectionProvider;
20 import org.chromium.shape_detection.mojom.BarcodeDetectorOptions;
21 import org.chromium.shape_detection.mojom.BarcodeFormat;
22 
23 /**
24  * Service provider to create BarcodeDetection services
25  */
26 public class BarcodeDetectionProviderImpl implements BarcodeDetectionProvider {
27     private static final String TAG = "BarcodeProviderImpl";
28 
BarcodeDetectionProviderImpl()29     public BarcodeDetectionProviderImpl() {}
30 
31     @Override
createBarcodeDetection( InterfaceRequest<BarcodeDetection> request, BarcodeDetectorOptions options)32     public void createBarcodeDetection(
33             InterfaceRequest<BarcodeDetection> request, BarcodeDetectorOptions options) {
34         BarcodeDetection.MANAGER.bind(new BarcodeDetectionImpl(options), request);
35     }
36 
37     @Override
enumerateSupportedFormats(EnumerateSupportedFormatsResponse callback)38     public void enumerateSupportedFormats(EnumerateSupportedFormatsResponse callback) {
39         // Keep this list in sync with the constants defined in
40         // com.google.android.gms.vision.barcode.Barcode and the format hints
41         // supported by BarcodeDetectionImpl.
42         int[] supportedFormats = {BarcodeFormat.AZTEC, BarcodeFormat.CODE_128,
43                 BarcodeFormat.CODE_39, BarcodeFormat.CODE_93, BarcodeFormat.CODABAR,
44                 BarcodeFormat.DATA_MATRIX, BarcodeFormat.EAN_13, BarcodeFormat.EAN_8,
45                 BarcodeFormat.ITF, BarcodeFormat.PDF417, BarcodeFormat.QR_CODE, BarcodeFormat.UPC_A,
46                 BarcodeFormat.UPC_E};
47         callback.call(supportedFormats);
48     }
49 
50     @Override
close()51     public void close() {}
52 
53     @Override
onConnectionError(MojoException e)54     public void onConnectionError(MojoException e) {}
55 
create()56     public static BarcodeDetectionProvider create() {
57         Context ctx = ContextUtils.getApplicationContext();
58         if (!ChromiumPlayServicesAvailability.isGooglePlayServicesAvailable(ctx)) {
59             Log.w(TAG, "Google Play Services not available");
60             return null;
61         }
62         try {
63             PackageInfo playServicesPackage = ctx.getPackageManager().getPackageInfo(
64                     GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0);
65             if (playServicesPackage.versionCode < 19742000) {
66                 // https://crbug.com/1020746
67                 Log.w(TAG, "Detection disabled (%s < 19.7.42)", playServicesPackage.versionName);
68                 return null;
69             }
70         } catch (NameNotFoundException e) {
71             Log.w(TAG, "Google Play Services not available");
72             return null;
73         }
74         return new BarcodeDetectionProviderImpl();
75     }
76 }
77