add bopomofo

pull/532/head
Bob Yang 2 years ago
parent 82234468e1
commit 80e1fa1ce8
  1. 60
      app/src/leanback/java/com/fongmi/android/tv/ui/activity/SearchActivity.java
  2. 50
      app/src/leanback/java/com/fongmi/android/tv/ui/adapter/KeyboardAdapter.java
  3. 16
      app/src/leanback/java/com/fongmi/android/tv/ui/custom/CustomKeyboard.java

@ -35,6 +35,7 @@ import com.google.common.net.HttpHeaders;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import okhttp3.Call;
@ -51,6 +52,59 @@ public class SearchActivity extends BaseActivity implements WordAdapter.OnClickL
activity.startActivity(new Intent(activity, SearchActivity.class));
}
public static String convertBopomofoToPinyin(String bopomofoString) {
HashMap<String, String> bopomofoToPinyinMap = new HashMap<String, String>();
bopomofoToPinyinMap.put("ㄅ", "b");
bopomofoToPinyinMap.put("ㄆ", "p");
bopomofoToPinyinMap.put("ㄇ", "m");
bopomofoToPinyinMap.put("ㄈ", "f");
bopomofoToPinyinMap.put("ㄉ", "d");
bopomofoToPinyinMap.put("ㄊ", "t");
bopomofoToPinyinMap.put("ㄋ", "n");
bopomofoToPinyinMap.put("ㄌ", "l");
bopomofoToPinyinMap.put("ㄍ", "g");
bopomofoToPinyinMap.put("ㄎ", "k");
bopomofoToPinyinMap.put("ㄏ", "h");
bopomofoToPinyinMap.put("ㄐ", "j");
bopomofoToPinyinMap.put("ㄑ", "q");
bopomofoToPinyinMap.put("ㄒ", "x");
bopomofoToPinyinMap.put("ㄓ", "z");
bopomofoToPinyinMap.put("ㄔ", "c");
bopomofoToPinyinMap.put("ㄕ", "s");
bopomofoToPinyinMap.put("ㄖ", "r");
bopomofoToPinyinMap.put("ㄗ", "z");
bopomofoToPinyinMap.put("ㄘ", "c");
bopomofoToPinyinMap.put("ㄙ", "s");
bopomofoToPinyinMap.put("ㄚ", "a");
bopomofoToPinyinMap.put("ㄛ", "o");
bopomofoToPinyinMap.put("ㄜ", "e");
bopomofoToPinyinMap.put("ㄝ", "e"); // can also be ie
bopomofoToPinyinMap.put("ㄞ", "ai");
bopomofoToPinyinMap.put("ㄟ", "ei");
bopomofoToPinyinMap.put("ㄠ", "ao");
bopomofoToPinyinMap.put("ㄡ", "ou");
bopomofoToPinyinMap.put("ㄢ", "an");
bopomofoToPinyinMap.put("ㄣ", "en");
bopomofoToPinyinMap.put("ㄤ", "ang");
bopomofoToPinyinMap.put("ㄥ", "eng");
bopomofoToPinyinMap.put("ㄦ", "er");
bopomofoToPinyinMap.put("ㄧ", "y");
bopomofoToPinyinMap.put("ㄨ", "w");
bopomofoToPinyinMap.put("ㄩ", "yu");
StringBuilder pinyinStringBuilder = new StringBuilder();
for (char bopomofoChar : bopomofoString.toCharArray()) {
String pinyin = bopomofoToPinyinMap.get(String.valueOf(bopomofoChar));
if (pinyin != null) {
pinyinStringBuilder.append(pinyin);
} else {
// Handle characters not found in the map (e.g., tones)
pinyinStringBuilder.append(bopomofoChar); // Add the character as is
}
}
return pinyinStringBuilder.toString();
}
@Override
protected ViewBinding getBinding() {
return mBinding = ActivitySearchBinding.inflate(getLayoutInflater());
@ -116,7 +170,7 @@ public class SearchActivity extends BaseActivity implements WordAdapter.OnClickL
private void getSuggest(String text) {
mBinding.hint.setText(R.string.search_suggest);
mWordAdapter.clear();
OkHttp.newCall("https://tv.aiseet.atianqi.com/i-tvbin/qtv_video/search/get_search_smart_box?format=json&page_num=0&page_size=10&key=" + URLEncoder.encode(text)).enqueue(new Callback() {
OkHttp.newCall("https://tv.aiseet.atianqi.com/i-tvbin/qtv_video/search/get_search_smart_box?format=json&page_num=0&page_size=10&key=" + URLEncoder.encode(convertBopomofoToPinyin(text))).enqueue(new Callback() {
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
if (mBinding.keyword.getText().toString().trim().isEmpty()) return;
@ -124,7 +178,7 @@ public class SearchActivity extends BaseActivity implements WordAdapter.OnClickL
App.post(() -> mWordAdapter.appendAll(items));
}
});
OkHttp.newCall("https://suggest.video.iqiyi.com/?if=mobile&key=" + URLEncoder.encode(text)).enqueue(new Callback() {
OkHttp.newCall("https://suggest.video.iqiyi.com/?if=mobile&key=" + URLEncoder.encode(convertBopomofoToPinyin(text))).enqueue(new Callback() {
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
if (mBinding.keyword.getText().toString().trim().isEmpty()) return;
@ -151,7 +205,7 @@ public class SearchActivity extends BaseActivity implements WordAdapter.OnClickL
mBinding.keyword.setSelection(mBinding.keyword.length());
Util.hideKeyboard(mBinding.keyword);
if (TextUtils.isEmpty(keyword)) return;
CollectActivity.start(this, keyword);
CollectActivity.start(this, convertBopomofoToPinyin(keyword));
App.post(() -> mRecordAdapter.add(keyword), 250);
}

@ -1,5 +1,7 @@
package com.fongmi.android.tv.ui.adapter;
import static java.util.Locale.*;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -8,22 +10,53 @@ import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.fongmi.android.tv.R;
import com.fongmi.android.tv.Setting;
import com.fongmi.android.tv.databinding.AdapterKeyboardIconBinding;
import com.fongmi.android.tv.databinding.AdapterKeyboardTextBinding;
import java.util.Arrays;
import java.util.List;
public class KeyboardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final OnClickListener mListener;
private final List<Object> mItems;
private List<Object> mItems;
private List<Object> englishKeys =Arrays.asList(
R.drawable.ic_keyboard_remote
, R.drawable.ic_keyboard_left
, R.drawable.ic_keyboard_right
, R.drawable.ic_keyboard_back
, R.drawable.ic_keyboard_search
, R.drawable.ic_setting_home
, R.drawable.ic_action_zhuyin
, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
, "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
private List<Object> zhuYinKeys =Arrays.asList(
R.drawable.ic_keyboard_remote
, R.drawable.ic_keyboard_left
, R.drawable.ic_keyboard_right
, R.drawable.ic_keyboard_back
, R.drawable.ic_keyboard_search
, R.drawable.ic_setting_home
, R.drawable.ic_action_zhuyin
,"ㄅ", "ㄆ", "ㄇ", "ㄈ", "ㄉ", "ㄊ", "ㄋ", "ㄌ", "ㄍ", "ㄎ", "ㄏ", "ㄐ", "ㄑ", "ㄒ",
"ㄓ", "ㄔ", "ㄕ", "ㄖ", "ㄗ", "ㄘ", "ㄙ", "ㄚ", "ㄛ", "ㄜ", "ㄝ", "ㄞ", "ㄟ", "ㄠ", "ㄡ", "ㄢ", "ㄣ",
"ㄤ", "ㄥ", "ㄦ", "ㄧ", "ㄨ", "ㄩ"
, "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
public KeyboardAdapter(OnClickListener listener) {
this.mItems = Arrays.asList(R.drawable.ic_keyboard_remote, R.drawable.ic_keyboard_left, R.drawable.ic_keyboard_right, R.drawable.ic_keyboard_back, R.drawable.ic_keyboard_search, R.drawable.ic_setting_home, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
if(Setting.getLanguage()==2){
this.mItems = zhuYinKeys;
}else{
this.mItems = englishKeys;
}
this.mListener = listener;
}
public interface OnClickListener {
void onTextClick(String text);
@ -33,6 +66,19 @@ public class KeyboardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
boolean onLongClick(int resId);
}
public void updateKeyList(List<Object> newKeys) {
this.mItems = newKeys;
notifyDataSetChanged();
}
public List<Object> getEnglishKeys() {
return englishKeys;
}
public List<Object> getZhuYinKeys() {
return zhuYinKeys;
}
@Override
public int getItemViewType(int position) {
return mItems.get(position) instanceof String ? 0 : 1;

@ -1,15 +1,18 @@
package com.fongmi.android.tv.ui.custom;
import android.annotation.SuppressLint;
import com.fongmi.android.tv.R;
import com.fongmi.android.tv.Setting;
import com.fongmi.android.tv.databinding.ActivitySearchBinding;
import com.fongmi.android.tv.ui.adapter.KeyboardAdapter;
import java.util.List;
public class CustomKeyboard implements KeyboardAdapter.OnClickListener {
private final ActivitySearchBinding binding;
private final Callback callback;
private boolean useZhuYin = false;
private KeyboardAdapter keyboardAdapter;
public static void init(Callback callback, ActivitySearchBinding binding) {
new CustomKeyboard(callback, binding).initView();
@ -18,12 +21,16 @@ public class CustomKeyboard implements KeyboardAdapter.OnClickListener {
public CustomKeyboard(Callback callback, ActivitySearchBinding binding) {
this.callback = callback;
this.binding = binding;
this.keyboardAdapter = new KeyboardAdapter(this);
}
private void initView() {
binding.keyboard.setHasFixedSize(true);
binding.keyboard.addItemDecoration(new SpaceItemDecoration(6, 8));
binding.keyboard.setAdapter(new KeyboardAdapter(this));
if(Setting.getLanguage()==2){
useZhuYin=true;
}
binding.keyboard.setAdapter(keyboardAdapter);
}
@Override
@ -42,6 +49,11 @@ public class CustomKeyboard implements KeyboardAdapter.OnClickListener {
StringBuilder sb = new StringBuilder(binding.keyword.getText().toString());
int cursor = binding.keyword.getSelectionStart();
switch (resId) {
case R.drawable.ic_action_zhuyin:
useZhuYin = !useZhuYin;
List<Object> newKeys = useZhuYin ? keyboardAdapter.getZhuYinKeys() : keyboardAdapter.getEnglishKeys();
keyboardAdapter.updateKeyList(newKeys);
break;
case R.drawable.ic_setting_home:
callback.showDialog();
break;

Loading…
Cancel
Save