|
|
|
@ -73,6 +73,11 @@ import androidx.core.content.ContextCompat;
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
|
|
|
|
|
import com.baidu.speech.EventListener;
|
|
|
|
|
import com.baidu.speech.EventManager;
|
|
|
|
|
import com.baidu.speech.EventManagerFactory;
|
|
|
|
|
import com.baidu.speech.asr.SpeechConstant;
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
|
|
import net.micode.notes.data.Notes;
|
|
|
|
|
import net.micode.notes.data.Notes.TextNote;
|
|
|
|
@ -86,16 +91,21 @@ import net.micode.notes.ui.NoteEditText.OnTextViewChangeListener;
|
|
|
|
|
import net.micode.notes.widget.NoteWidgetProvider_2x;
|
|
|
|
|
import net.micode.notes.widget.NoteWidgetProvider_4x;
|
|
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
NoteSettingChangedListener, OnTextViewChangeListener {
|
|
|
|
|
NoteSettingChangedListener, OnTextViewChangeListener, EventListener {
|
|
|
|
|
private class HeadViewHolder {
|
|
|
|
|
public TextView tvModified;
|
|
|
|
|
|
|
|
|
@ -107,6 +117,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final Map<Integer, Integer> sBgSelectorBtnsMap = new HashMap<Integer, Integer>();
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
sBgSelectorBtnsMap.put(R.id.iv_bg_yellow, ResourceParser.YELLOW);
|
|
|
|
|
sBgSelectorBtnsMap.put(R.id.iv_bg_red, ResourceParser.RED);
|
|
|
|
@ -116,6 +127,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final Map<Integer, Integer> sBgSelectorSelectionMap = new HashMap<Integer, Integer>();
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
sBgSelectorSelectionMap.put(ResourceParser.YELLOW, R.id.iv_bg_yellow_select);
|
|
|
|
|
sBgSelectorSelectionMap.put(ResourceParser.RED, R.id.iv_bg_red_select);
|
|
|
|
@ -125,6 +137,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final Map<Integer, Integer> sFontSizeBtnsMap = new HashMap<Integer, Integer>();
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
sFontSizeBtnsMap.put(R.id.ll_font_large, ResourceParser.TEXT_LARGE);
|
|
|
|
|
sFontSizeBtnsMap.put(R.id.ll_font_small, ResourceParser.TEXT_SMALL);
|
|
|
|
@ -133,6 +146,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final Map<Integer, Integer> sFontSelectorSelectionMap = new HashMap<Integer, Integer>();
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
sFontSelectorSelectionMap.put(ResourceParser.TEXT_LARGE, R.id.iv_large_select);
|
|
|
|
|
sFontSelectorSelectionMap.put(ResourceParser.TEXT_SMALL, R.id.iv_small_select);
|
|
|
|
@ -176,6 +190,11 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
private final int PHOTO_REQUEST = 1;//请求码
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected EditText txtResult;//识别结果
|
|
|
|
|
protected Button startBtn;//开始识别 一直不说话会自动停止,需要再次打开
|
|
|
|
|
|
|
|
|
|
private EventManager asr;//语音识别核心库
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
@ -187,7 +206,33 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
initResources();
|
|
|
|
|
initView();
|
|
|
|
|
txtResult = findViewById(R.id.note_edit_view);
|
|
|
|
|
asr = EventManagerFactory.create(this, "asr");
|
|
|
|
|
//注册自己的输出事件类
|
|
|
|
|
asr.registerListener(this); // EventListener 中 onEvent方法
|
|
|
|
|
startBtn.setOnTouchListener(new View.OnTouchListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onTouch(View v, MotionEvent event) {
|
|
|
|
|
int action = event.getAction();
|
|
|
|
|
switch (action) {
|
|
|
|
|
case MotionEvent.ACTION_DOWN:
|
|
|
|
|
start();
|
|
|
|
|
/*mStartSpeechButton.setBackgroundResource(
|
|
|
|
|
R.drawable.bdspeech_btn_orangelight_pressed);*/
|
|
|
|
|
break;
|
|
|
|
|
case MotionEvent.ACTION_UP:
|
|
|
|
|
stop();
|
|
|
|
|
/*mStartSpeechButton.setBackgroundResource(
|
|
|
|
|
R.drawable.bdspeech_btn_orangelight_normal);*/
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//根据id获取添加图片按钮
|
|
|
|
@ -198,8 +243,8 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
public void onClick(View view) {
|
|
|
|
|
if (ContextCompat.checkSelfPermission(NoteEditActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
//没有授权进行权限申请
|
|
|
|
|
ActivityCompat.requestPermissions(NoteEditActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE}, GET_STORAGE_PERMISSION);}
|
|
|
|
|
else {
|
|
|
|
|
ActivityCompat.requestPermissions(NoteEditActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, GET_STORAGE_PERMISSION);
|
|
|
|
|
} else {
|
|
|
|
|
Log.d(TAG, "onClick: click add image button");
|
|
|
|
|
//ACTION_GET_CONTENT: 允许用户选择特殊种类的数据,并返回(特殊种类的数据:照一张相片或录一段音)
|
|
|
|
|
Intent loadImage = new Intent(Intent.ACTION_GET_CONTENT);
|
|
|
|
@ -374,7 +419,8 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
} else {
|
|
|
|
|
mNoteHeaderHolder.tvAlertDate.setVisibility(View.GONE);
|
|
|
|
|
mNoteHeaderHolder.ivAlertIcon.setVisibility(View.GONE);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//路径字符串格式 转换为 图片image格式
|
|
|
|
@ -491,7 +537,8 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
for (int id : sFontSizeBtnsMap.keySet()) {
|
|
|
|
|
View view = findViewById(id);
|
|
|
|
|
view.setOnClickListener(this);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
|
mFontSizeId = mSharedPrefs.getInt(PREFERENCE_FONT_SIZE, ResourceParser.BG_DEFAULT_FONT_SIZE);
|
|
|
|
|
/**
|
|
|
|
@ -1057,8 +1104,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
return getDataColumn(context, contentUri, null, null);
|
|
|
|
|
}
|
|
|
|
|
// MediaProvider
|
|
|
|
|
else
|
|
|
|
|
if (isMediaDocument(uri)) {
|
|
|
|
|
else if (isMediaDocument(uri)) {
|
|
|
|
|
final String docId = DocumentsContract.getDocumentId(uri);
|
|
|
|
|
final String[] split = docId.split(":");
|
|
|
|
|
final String type = split[0];
|
|
|
|
@ -1121,4 +1167,55 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
public boolean isMediaDocument(Uri uri) {
|
|
|
|
|
return "com.android.providers.media.documents".equals(uri.getAuthority());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void start() {
|
|
|
|
|
Toast.makeText(NoteEditActivity.this, "请开始说话", Toast.LENGTH_SHORT).show();
|
|
|
|
|
Map<String, Object> params = new LinkedHashMap<>();//传递Map<String,Object>的参数,会将Map自动序列化为json
|
|
|
|
|
String event = null;
|
|
|
|
|
event = SpeechConstant.ASR_START;
|
|
|
|
|
params.put(SpeechConstant.ACCEPT_AUDIO_VOLUME, false);//回调当前音量
|
|
|
|
|
String json = null;
|
|
|
|
|
json = new JSONObject(params).toString();//demo用json数据来做数据交换的方式
|
|
|
|
|
asr.send(event, json, null, 0, 0);// 初始化EventManager对象,这个实例只能创建一次,就是我们上方创建的asr,此处开始传入
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void stop() {
|
|
|
|
|
//txtResult.append("停止识别:ASR_STOP");
|
|
|
|
|
asr.send(SpeechConstant.ASR_STOP, null, null, 0, 0);//此处停止
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onDestroy() {
|
|
|
|
|
super.onDestroy();
|
|
|
|
|
asr.send(SpeechConstant.ASR_CANCEL, "{}", null, 0, 0);
|
|
|
|
|
asr.unregisterListener(this);//退出事件管理器
|
|
|
|
|
// 必须与registerListener成对出现,否则可能造成内存泄露
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onEvent(String name, String params, byte[] data, int offset, int length) {
|
|
|
|
|
String resultTxt = null;
|
|
|
|
|
//Log.i(TAG, params);
|
|
|
|
|
if (name.equals(SpeechConstant.CALLBACK_EVENT_ASR_PARTIAL)) {//识别结果参数
|
|
|
|
|
if (params.contains("\"final_result\"")) {//语义结果值
|
|
|
|
|
try {
|
|
|
|
|
JSONObject json = new JSONObject(params);
|
|
|
|
|
String result = json.getString("best_result");//取得key的识别结果
|
|
|
|
|
resultTxt = result;
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (resultTxt != null) {
|
|
|
|
|
resultTxt += "\n";
|
|
|
|
|
txtResult.append(resultTxt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void initView() {
|
|
|
|
|
txtResult = findViewById(R.id.note_edit_view);
|
|
|
|
|
startBtn = findViewById(R.id.listen);
|
|
|
|
|
//stopBtn = findViewById(R.id.stop);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|