feat(service): 添加内容捕获与语音交互服务

This commit is contained in:
2026-07-27 08:51:26 +08:00
parent 6e37800148
commit 8783f95307
7 changed files with 193 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
package com.ttstd.dialer.service;
import android.os.Build;
import android.service.contentcapture.ContentCaptureService;
import android.view.contentcapture.ContentCaptureEvent;
import android.view.contentcapture.ContentCaptureSessionId;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import com.ttstd.dialer.utils.Logger;
@RequiresApi(api = Build.VERSION_CODES.Q)
public class MyContentCaptureService extends ContentCaptureService {
private static final String TAG = "MyContentCaptureService";
@Override
public void onConnected() {
super.onConnected();
Logger.d(TAG, "onConnected");
}
@Override
public void onDisconnected() {
super.onDisconnected();
Logger.d(TAG, "onDisconnected");
}
@Override
public void onContentCaptureEvent(@NonNull ContentCaptureSessionId sessionId, @NonNull ContentCaptureEvent event) {
// 这是核心方法,所有的界面变化都会在这里回调
Logger.v(TAG, "onContentCaptureEvent: type=" + event.getType());
if (event.getType() == ContentCaptureEvent.TYPE_VIEW_APPEARED) {
if (event.getViewNode() != null) {
CharSequence text = event.getViewNode().getText();
String id = event.getViewNode().getIdEntry();
Logger.d(TAG, "View Appeared: text=" + text + ", id=" + id);
}
}
}
}

View File

@@ -0,0 +1,15 @@
package com.ttstd.dialer.service;
import android.service.voice.VoiceInteractionService;
import com.ttstd.dialer.utils.Logger;
public class MyVoiceInteractionService extends VoiceInteractionService {
private static final String TAG = "MyVoiceInteractionService";
@Override
public void onCreate() {
super.onCreate();
Logger.d(TAG, "onCreate");
}
}

View File

@@ -0,0 +1,47 @@
package com.ttstd.dialer.service;
import android.app.assist.AssistContent;
import android.app.assist.AssistStructure;
import android.content.Context;
import android.os.Bundle;
import android.service.voice.VoiceInteractionSession;
import android.view.View;
import com.ttstd.dialer.utils.Logger;
public class MyVoiceInteractionSession extends VoiceInteractionSession {
private static final String TAG = "MyVoiceInteractionSession";
public MyVoiceInteractionSession(Context context) {
super(context);
}
@Override
public void onHandleAssist(Bundle data, AssistStructure structure, AssistContent content) {
super.onHandleAssist(data, structure, content);
// 这是核心方法structure 包含了整个屏幕的 View 树
Logger.d(TAG, "onHandleAssist");
if (structure != null) {
AssistStructure.WindowNode windowNode = structure.getWindowNodeAt(0);
if (windowNode != null) {
traverseNode(windowNode.getRootViewNode(), "");
}
}
}
private void traverseNode(AssistStructure.ViewNode node, String prefix) {
if (node == null) return;
Logger.d(TAG, prefix + "View: text=" + node.getText() + ", id=" + node.getIdEntry());
for (int i = 0; i < node.getChildCount(); i++) {
traverseNode(node.getChildAt(i), prefix + " ");
}
}
@Override
public View onCreateContentView() {
// 可以返回一个自定义布局,用于在屏幕上显示助手界面
return super.onCreateContentView();
}
}

View File

@@ -0,0 +1,17 @@
package com.ttstd.dialer.service;
import android.os.Bundle;
import android.service.voice.VoiceInteractionSession;
import android.service.voice.VoiceInteractionSessionService;
import com.ttstd.dialer.utils.Logger;
public class MyVoiceInteractionSessionService extends VoiceInteractionSessionService {
private static final String TAG = "MyVoiceInteractionSessionService";
@Override
public VoiceInteractionSession onNewSession(Bundle args) {
Logger.d(TAG, "onNewSession");
return new MyVoiceInteractionSession(this);
}
}