From 97bf4e008325911cc7f08627a4f00492bca70657 Mon Sep 17 00:00:00 2001 From: Svet Ganov Date: Thu, 10 Jun 2021 16:46:11 +0000 Subject: [PATCH] Don don't blame if attr ctx created Test: manual bug: 190657833 Change-Id: Id7c65999baa294a53765f5d17f77a7903611e85a --- .../android/speech/RecognitionService.java | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/core/java/android/speech/RecognitionService.java b/core/java/android/speech/RecognitionService.java index b9ff5e7be86a1..055e71f0077b6 100644 --- a/core/java/android/speech/RecognitionService.java +++ b/core/java/android/speech/RecognitionService.java @@ -33,10 +33,13 @@ import android.os.Binder; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; +import android.os.Looper; import android.os.Message; import android.os.RemoteException; import android.util.Log; +import com.android.internal.util.function.pooled.PooledLambda; + import java.lang.ref.WeakReference; import java.util.Objects; @@ -225,6 +228,27 @@ public abstract class RecognitionService extends Service { */ protected abstract void onStopListening(Callback listener); + @Override + public Context createContext(@NonNull ContextParams contextParams) { + if (contextParams.getNextAttributionSource() != null) { + if (mHandler.getLooper().equals(Looper.myLooper())) { + handleAttributionContextCreation(contextParams.getNextAttributionSource()); + } else { + mHandler.sendMessage( + PooledLambda.obtainMessage(this::handleAttributionContextCreation, + contextParams.getNextAttributionSource())); + } + } + return super.createContext(contextParams); + } + + private void handleAttributionContextCreation(@NonNull AttributionSource attributionSource) { + if (mCurrentCallback != null + && mCurrentCallback.mCallingAttributionSource.equals(attributionSource)) { + mCurrentCallback.mAttributionContextCreated = true; + } + } + @Override public final IBinder onBind(final Intent intent) { if (DBG) Log.d(TAG, "onBind, intent=" + intent); @@ -249,6 +273,7 @@ public abstract class RecognitionService extends Service { private final IRecognitionListener mListener; private final @NonNull AttributionSource mCallingAttributionSource; private @Nullable Context mAttributionContext; + private boolean mAttributionContextCreated; private Callback(IRecognitionListener listener, @NonNull AttributionSource attributionSource) { @@ -421,7 +446,7 @@ public abstract class RecognitionService extends Service { } private boolean checkPermissionAndStartDataDelivery() { - if (isPerformingDataDelivery()) { + if (mCurrentCallback.mAttributionContextCreated) { return true; } if (PermissionChecker.checkPermissionAndStartDataDelivery( @@ -441,13 +466,4 @@ public abstract class RecognitionService extends Service { mCurrentCallback.getAttributionContextForCaller().getAttributionSource()); } } - - @SuppressWarnings("ConstantCondition") - private boolean isPerformingDataDelivery() { - final int op = AppOpsManager.permissionToOpCode(Manifest.permission.RECORD_AUDIO); - final AppOpsManager appOpsManager = getSystemService(AppOpsManager.class); - return appOpsManager.isProxying(op, getAttributionTag(), - mCurrentCallback.getCallingAttributionSource().getUid(), - mCurrentCallback.getCallingAttributionSource().getPackageName()); - } }