Commit f8b33e24 authored by Gayane Petrosyan's avatar Gayane Petrosyan Committed by Commit Bot

BarcodeDetector should be created on a background thread.

BarcodeDetector creation takes long time and triggers strict mode
violation warning therefore should be created on a worker thread.

Also some cleanup - new tab now can be created from worker thread as it
is launching an intent instead of going through tab manager.

Bug: 993920
Change-Id: Ic974ebe2cae41c7a82fca87976656335799f5ba6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2081615
Commit-Queue: Gayane Petrosyan <gayane@chromium.org>
Reviewed-by: default avatarTanya Gupta <tgupta@chromium.org>
Reviewed-by: default avatarTravis Skare <skare@chromium.org>
Cr-Commit-Position: refs/heads/master@{#750641}
parent 6bba8a56
...@@ -12,8 +12,6 @@ import android.content.pm.PackageManager; ...@@ -12,8 +12,6 @@ import android.content.pm.PackageManager;
import android.graphics.ImageFormat; import android.graphics.ImageFormat;
import android.hardware.Camera; import android.hardware.Camera;
import android.net.Uri; import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Process; import android.os.Process;
import android.provider.Browser; import android.provider.Browser;
import android.util.SparseArray; import android.util.SparseArray;
...@@ -23,6 +21,7 @@ import com.google.android.gms.vision.Frame; ...@@ -23,6 +21,7 @@ import com.google.android.gms.vision.Frame;
import com.google.android.gms.vision.barcode.Barcode; import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector; import com.google.android.gms.vision.barcode.BarcodeDetector;
import org.chromium.base.task.AsyncTask;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import org.chromium.chrome.browser.IntentHandler; import org.chromium.chrome.browser.IntentHandler;
import org.chromium.chrome.browser.ShortcutHelper; import org.chromium.chrome.browser.ShortcutHelper;
...@@ -43,16 +42,13 @@ public class QrCodeScanMediator implements Camera.PreviewCallback { ...@@ -43,16 +42,13 @@ public class QrCodeScanMediator implements Camera.PreviewCallback {
/** Interface used for notifying in the event of navigation to a URL. */ /** Interface used for notifying in the event of navigation to a URL. */
public interface NavigationObserver { void onNavigation(); } public interface NavigationObserver { void onNavigation(); }
/** Interface for creating and navigation to a new tab for a given URL. */
public interface TabCreator { void createNewTab(String url); }
private final Context mContext; private final Context mContext;
private final PropertyModel mPropertyModel; private final PropertyModel mPropertyModel;
private final BarcodeDetector mDetector;
private final NavigationObserver mNavigationObserver; private final NavigationObserver mNavigationObserver;
private final Handler mMainThreadHandler;
private final AndroidPermissionDelegate mPermissionDelegate; private final AndroidPermissionDelegate mPermissionDelegate;
private BarcodeDetector mDetector;
/** /**
* The QrCodeScanMediator constructor. * The QrCodeScanMediator constructor.
* *
...@@ -66,9 +62,11 @@ public class QrCodeScanMediator implements Camera.PreviewCallback { ...@@ -66,9 +62,11 @@ public class QrCodeScanMediator implements Camera.PreviewCallback {
mPermissionDelegate = new ActivityAndroidPermissionDelegate( mPermissionDelegate = new ActivityAndroidPermissionDelegate(
new WeakReference<Activity>((Activity) mContext)); new WeakReference<Activity>((Activity) mContext));
updatePermissionSettings(); updatePermissionSettings();
mDetector = new BarcodeDetector.Builder(context).build();
mNavigationObserver = observer; mNavigationObserver = observer;
mMainThreadHandler = new Handler(Looper.getMainLooper());
// Set detector to null until it gets initialized asynchronously.
mDetector = null;
initBarcodeDetectorAsync();
} }
/** Returns whether the user has granted camera permissions. */ /** Returns whether the user has granted camera permissions. */
...@@ -142,6 +140,10 @@ public class QrCodeScanMediator implements Camera.PreviewCallback { ...@@ -142,6 +140,10 @@ public class QrCodeScanMediator implements Camera.PreviewCallback {
*/ */
@Override @Override
public void onPreviewFrame(byte[] data, Camera camera) { public void onPreviewFrame(byte[] data, Camera camera) {
if (mDetector == null) {
return;
}
ByteBuffer buffer = ByteBuffer.allocate(data.length); ByteBuffer buffer = ByteBuffer.allocate(data.length);
buffer.put(data); buffer.put(data);
Frame frame = Frame frame =
...@@ -164,11 +166,8 @@ public class QrCodeScanMediator implements Camera.PreviewCallback { ...@@ -164,11 +166,8 @@ public class QrCodeScanMediator implements Camera.PreviewCallback {
return; return;
} }
/** Tab creation should happen on the main thread. */ openUrl(firstCode.rawValue);
mMainThreadHandler.post(() -> { mNavigationObserver.onNavigation();
openUrl(firstCode.rawValue);
mNavigationObserver.onNavigation();
});
} }
private void openUrl(String url) { private void openUrl(String url) {
...@@ -183,4 +182,18 @@ public class QrCodeScanMediator implements Camera.PreviewCallback { ...@@ -183,4 +182,18 @@ public class QrCodeScanMediator implements Camera.PreviewCallback {
IntentHandler.addTrustedIntentExtras(intent); IntentHandler.addTrustedIntentExtras(intent);
mContext.startActivity(intent); mContext.startActivity(intent);
} }
private void initBarcodeDetectorAsync() {
new AsyncTask<BarcodeDetector>() {
@Override
protected BarcodeDetector doInBackground() {
return new BarcodeDetector.Builder(mContext).build();
}
@Override
protected void onPostExecute(BarcodeDetector detector) {
mDetector = detector;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment