Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c6931d97 authored by Peter Li's avatar Peter Li Committed by lpeter
Browse files

Create proper ui context if original context is not a ui context

Since Android API 31 there is a new check in StrictMode, this
check ensures that any ui-related operations involving context
object are only performed on the ui context. If the context is
not the ui context, it will fail with error when calling the
ui-related methods.

So we need to create proper ui context if original context is
not a ui context.

Bug: 220698651
Test: atest CtsVoiceInteractionTestCases
Test: manual. Test VoiceInteractionSession UI.
Change-Id: Ibbeae9b135b90078b3b9e36a2d0c5a92f6184b2a
parent 011618a5
Loading
Loading
Loading
Loading
+21 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.service.voice;

import static android.view.Display.DEFAULT_DISPLAY;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

import android.annotation.CallbackExecutor;
@@ -38,6 +39,7 @@ import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.Region;
import android.hardware.display.DisplayManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.CancellationSignal;
@@ -1043,15 +1045,33 @@ public class VoiceInteractionSession implements KeyEvent.Callback, ComponentCall
    }

    public VoiceInteractionSession(Context context, Handler handler) {
        mContext = context;
        mHandlerCaller = new HandlerCaller(context, handler.getLooper(),
                mCallbacks, true);
        mContext = createWindowContextIfNeeded(context);
    }

    public Context getContext() {
        return mContext;
    }

    private Context createWindowContextIfNeeded(Context context) {
        try {
            if (!context.isUiContext()) {
                DisplayManager displayManager = context.getSystemService(DisplayManager.class);
                if (displayManager != null) {
                    return context.createWindowContext(
                            displayManager.getDisplay(DEFAULT_DISPLAY),
                            WindowManager.LayoutParams.TYPE_VOICE_INTERACTION,
                            /* options= */ null);
                }
            }
            return context;
        } catch (RuntimeException e) {
            Log.w(TAG, "Fail to createWindowContext, Exception = " + e);
            return context;
        }
    }

    void addRequest(Request req) {
        synchronized (this) {
            mActiveRequests.put(req.mInterface.asBinder(), req);