3 Star 3 Fork 0

Archermind-TI / Voice Overlay

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
MainAbilitySlice.java 4.06 KB
一键复制 编辑 原始数据 按行查看 历史
cdyp 提交于 2021-09-27 21:51 . docs:更新版权标注
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.algolia.instantsearch.voice.demo.slice;
import com.algolia.instantsearch.voice.VoiceSpeechRecognizer;
import com.algolia.instantsearch.voice.demo.MainAbility;
import com.algolia.instantsearch.voice.demo.ResourceTable;
import com.algolia.instantsearch.voice.ui.Voice;
import com.algolia.instantsearch.voice.ui.VoiceInputDialogFraction;
import com.algolia.instantsearch.voice.ui.VoicePermissionDialogFraction;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import java.lang.ref.SoftReference;
import java.util.List;
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener, VoiceSpeechRecognizer.ResultsListener {
private static final HiLogLabel LOG_LABEL = new HiLogLabel(HiLog.LOG_APP,0,"MainAbilitySlice");
private Text resultsText;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
initView();
MainAbility.setCurrentSlice(new SoftReference<>(this));
}
private void initView() {
Button voiceButton = (Button) findComponentById(ResourceTable.Id_buttonVoice);
voiceButton.setClickedListener(this);
Button permissionButton = (Button) findComponentById(ResourceTable.Id_buttonPermission);
permissionButton.setClickedListener(this);
resultsText = (Text) findComponentById(ResourceTable.Id_results);
}
@Override
public void onClick(Component component) {
switch (component.getId()) {
case ResourceTable.Id_buttonVoice:
if (!Voice.isRecordAudioPermissionGranted(getApplicationContext())) {
((FractionAbility)getAbility()).getFractionManager()
.startFractionScheduler()
.replace(ResourceTable.Id_main_fraction, new VoicePermissionDialogFraction(this))
.submit();
} else {
showVoiceDialog();
}
break;
case ResourceTable.Id_buttonPermission:
((FractionAbility)getAbility()).getFractionManager()
.startFractionScheduler()
.replace(ResourceTable.Id_main_fraction, new VoicePermissionDialogFraction(this))
.submit();
break;
default:
}
}
@Override
public void onResults(List<String> possibleTexts) {
HiLog.debug(LOG_LABEL, "onResults() possibleTexts:" + possibleTexts.get(0));
StringBuilder sb = new StringBuilder();
for (String text : possibleTexts) {
sb.append(text);
sb.append('\n');
}
resultsText.setText(sb.toString());
}
public void showVoiceDialog() {
HiLog.debug(LOG_LABEL, "showVoiceDialog()");
VoiceInputDialogFraction voiceInput = new VoiceInputDialogFraction(this);
String[] suggestions = {"Phone case", "Running shoes"};
voiceInput.setSuggestions(suggestions);
voiceInput.setAutoStart(true);
((FractionAbility)getAbility()).getFractionManager()
.startFractionScheduler()
.replace(ResourceTable.Id_main_fraction, voiceInput)
.submit();
}
}
1
https://gitee.com/archermind-ti/voice-overlay-ohos.git
git@gitee.com:archermind-ti/voice-overlay-ohos.git
archermind-ti
voice-overlay-ohos
Voice Overlay
master

搜索帮助