Skip to content

Commit

Permalink
22.02.08
Browse files Browse the repository at this point in the history
v1.0.0.5
cleaned up some codes
  • Loading branch information
Zhong0x29a committed Feb 8, 2022
1 parent aa582fc commit 800fd89
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 95 deletions.
95 changes: 46 additions & 49 deletions app/src/main/java/cc0x29a/specialtranslator/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,17 @@ public class MainActivity extends AppCompatActivity {
RadioButton rbSelectAuto,rbSelectEn,rbSelectCn;

Button btnSelectSouLang,btnSelectDesLang;
Button btnSwapSDLang;

EditText etTextToTrans;
Button btnStartTrans;
TextView tvTextOutput;

// Button btnMenu;
Button btnUseBaidu,btnUseYoudao;

String fromLang,toLang;
String API;
String BaiduAPIToken;

// languages set
HashMap id2lang=new HashMap<Integer,String>();
HashMap<Integer,String> id2lang=new HashMap<>();
HashMap<String,Integer> lang2id=new HashMap<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -57,6 +53,9 @@ protected void onCreate(Bundle savedInstanceState) {
id2lang.put(R.id.mainAct_Select_Auto,"auto");
id2lang.put(R.id.mainAct_Select_English,"en");
id2lang.put(R.id.mainAct_Select_Chinese,"zh");
lang2id.put("auto",R.id.mainAct_Select_Auto);
lang2id.put("en",R.id.mainAct_Select_English);
lang2id.put("zh",R.id.mainAct_Select_Chinese);

svSelectLang=findViewById(R.id.mainAct_SelectLang);
rgLangGroup=findViewById(R.id.mainAct_LangRadioGroup);
Expand All @@ -67,53 +66,46 @@ protected void onCreate(Bundle savedInstanceState) {
// Click to chang language section.
btnSelectSouLang=findViewById(R.id.mainAct_SouLang);
btnSelectDesLang=findViewById(R.id.mainAct_DesLang);
btnSwapSDLang=findViewById(R.id.mainAct_SwpSDLang);

// Inout and output section.
etTextToTrans=findViewById(R.id.mainAct_TextToTrans);
btnStartTrans=findViewById(R.id.mainAct_StartTrans);
tvTextOutput=findViewById(R.id.mainAct_TextOutput_tv);

// TopBar section.
// btnMenu=findViewById(R.id.mainAct_MenuBtn);
btnUseBaidu=findViewById(R.id.mainAct_MenuUseBaiduAPI);
btnUseYoudao=findViewById(R.id.mainAct_MenuUseYoudaoAPI);

// Bind Listener.
// Languages Radio group.
rgLangGroup.setOnCheckedChangeListener((radioGroup, i) -> {
// set the lang.
try {
if (svSelectLang.getTag().toString().equals("SourceLang")) {
fromLang = Objects.requireNonNull(id2lang.get(i)).toString();
fromLang = Objects.requireNonNull(id2lang.get(i));
btnSelectSouLang.setText(fromLang);
} else if (svSelectLang.getTag().toString().equals("DestinyLang")) {
toLang = Objects.requireNonNull(id2lang.get(i)).toString();
toLang = Objects.requireNonNull(id2lang.get(i));
btnSelectDesLang.setText(toLang);
}
}catch (NullPointerException e){
System.err.println(e.getMessage());
}

// hide the scroll view
showOrHideRadioGroupAuto("no");
showOrHideRadioGroupAuto("");
});

// click to select another language.
btnSelectSouLang.setOnClickListener(cliListenerChangeLangBTN);
btnSwapSDLang.setOnClickListener(cliListenerChangeLangBTN);
findViewById(R.id.mainAct_SwpSDLang).setOnClickListener(cliListenerChangeLangBTN);
btnSelectDesLang.setOnClickListener(cliListenerChangeLangBTN);

// open/close menu
findViewById(R.id.mainAct_MenuBtn).setOnClickListener(view -> openOrCloseTopBarMenu());
findViewById(R.id.mainAct_TopBarMenu).setOnClickListener(view -> openOrCloseTopBarMenu());

// select API
btnUseBaidu.setOnClickListener(cliListenerChangeAPI);
btnUseYoudao.setOnClickListener(cliListenerChangeAPI);
findViewById(R.id.mainAct_MenuUseBaiduAPI).setOnClickListener(cliListenerChangeAPI);
findViewById(R.id.mainAct_MenuUseYoudaoAPI).setOnClickListener(cliListenerChangeAPI);

// Start Translation.
btnStartTrans.setOnClickListener(view -> new Thread( () -> {
findViewById(R.id.mainAct_StartTrans).setOnClickListener(view -> new Thread( () -> {
String textToTrans=etTextToTrans.getText().toString();

// if search for a word, look up the dictionary.
Expand All @@ -129,6 +121,12 @@ protected void onCreate(Bundle savedInstanceState) {
showAndHideOptView(false);
// if search sentence
String textOutput;
if ("youdao".equals(API)) {
textOutput = TranslateAPI.YouDaoAPI.translate(fromLang, toLang, textToTrans);
} else {
textOutput = TranslateAPI.BaiduAPI.translateSentence(fromLang, toLang, textToTrans, BaiduAPIToken);
}
/*
switch (API) {
case "baidu":
textOutput = TranslateAPI.BaiduAPI.translateSentence(fromLang, toLang, textToTrans,BaiduAPIToken);
Expand All @@ -139,15 +137,16 @@ protected void onCreate(Bundle savedInstanceState) {
default:
textOutput = TranslateAPI.BaiduAPI.translateSentence(fromLang, toLang, textToTrans, BaiduAPIToken);
}
*/
setTvTextOutputCont(textOutput);
}
}).start());

// save "last status"
SharedPreferences sharedPreferences=getSharedPreferences("info",MODE_PRIVATE);
// get "last status"
// to refresh Baidu api token.
new Thread(()->{
long timeCurr=System.currentTimeMillis();
SharedPreferences sharedPreferences=getSharedPreferences("info",MODE_PRIVATE);
long BDTokenExpDate=Long.parseLong(sharedPreferences.getString("BaiduToken_expDate","0"));
if(timeCurr >= BDTokenExpDate){
String token=TranslateAPI.BaiduAPI.fetchNewToken();
Expand All @@ -158,10 +157,11 @@ protected void onCreate(Bundle savedInstanceState) {
}).start();

// fetch baidu api token from local
BaiduAPIToken=sharedPreferences.getString("BaiduToken","");
BaiduAPIToken=getSharedPreferences("info",MODE_PRIVATE).getString("BaiduToken","");

// set default sourceLang & DestinyLang
new Thread(()-> {
SharedPreferences sharedPreferences=getSharedPreferences("info",MODE_PRIVATE);
fromLang=sharedPreferences.getString("fromLang","en");
toLang=sharedPreferences.getString("toLang","zh");
API=sharedPreferences.getString("API","baidu");
Expand Down Expand Up @@ -222,8 +222,17 @@ protected void onStart() {
};

// Change API
@SuppressLint("NonConstantResourceId")
View.OnClickListener cliListenerChangeAPI = view -> {
switch (view.getId()){
if (view.getId() == R.id.mainAct_MenuUseYoudaoAPI) {
API = "youdao";
getSharedPreferences("info", MODE_PRIVATE).edit().putString("API", "youdao").apply();
} else {
API = "baidu";
getSharedPreferences("info", MODE_PRIVATE).edit().putString("API", "baidu").apply();
}

/* switch (view.getId()){
case R.id.mainAct_MenuUseBaiduAPI:
API="baidu";
getSharedPreferences("info",MODE_PRIVATE).edit().putString("API","baidu").apply();
Expand All @@ -236,39 +245,24 @@ protected void onStart() {
API="baidu";
getSharedPreferences("info",MODE_PRIVATE).edit().putString("API","baidu").apply();
}
*/

openOrCloseTopBarMenu();
};

// Show/hide Checkbox.
//todo: may no use? or simplify?
/**
* use to set the visibility of the radio group.
* @param isDestLang if true, set "auto" uncheckable.
* @param checkedOne set which "were" selected.
*/
public void setRadioGroupChecked(boolean isDestLang,String checkedOne){
rbSelectAuto.setClickable(!isDestLang);
switch (checkedOne){
case "auto":
rbSelectAuto.setChecked(true);
break;
case "en":
rbSelectEn.setChecked(true);
break;
case "zh":
rbSelectCn.setChecked(true);
break;
}
}

// @SuppressLint("NullPointerException")
public void showOrHideRadioGroupAuto(String fromOrTo){
if(svSelectLang.getVisibility()==View.VISIBLE){
svSelectLang.setVisibility(View.GONE);
}else{
if(fromOrTo.equals("from")) {
setRadioGroupChecked(false, fromLang);
rbSelectAuto.setClickable(true);
RadioButton rb = findViewById(lang2id.get(fromLang));
rb.setChecked(true);
}else if(fromOrTo.equals("to")){
setRadioGroupChecked(true,toLang);
rbSelectAuto.setClickable(false);
RadioButton rb = findViewById(lang2id.get(toLang));
rb.setChecked(true);
}
svSelectLang.setVisibility(View.VISIBLE);
}
Expand Down Expand Up @@ -306,11 +300,13 @@ public void showAndHideOptView(boolean whichOne){
}

// Set the Output view new content
@SuppressLint("SetTextI18n")
public void setTvTextOutputCont(String text){
MainActivity.this.runOnUiThread(() -> tvTextOutput.setText(text+"\n\n--by "+API));
}

// display the word lookup details.
@SuppressLint("SetTextI18n")
public void displayWordLookupDetail(JSONObject data){
// init?
try {
Expand Down Expand Up @@ -359,7 +355,8 @@ public void displayWordLookupDetail(JSONObject data){
}

// Display the Everyday Sentence.
public void setTvTextStcEvrDay(String textEng,String textZh){
@SuppressLint("SetTextI18n")
public void setTvTextStcEvrDay(String textEng, String textZh){
MainActivity.this.runOnUiThread(() -> {
TextView tv=findViewById(R.id.mainAct_StcEvrDay);
tv.setText("Everyday sentence: \n\n "+textEng+"\n "+textZh+"\n\n-by iciba");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;

import javax.net.ssl.HttpsURLConnection;

Expand All @@ -20,6 +18,7 @@ public class TranslateAPI {
// 360 's API
public static class _360API{
public static JSONObject lookupDictionary(String fromLang, String toLang, String text){
// https://fanyi.so.com/#
// https://fanyi.so.com/index/search?eng=1&validate=&ignore_trans=0&query=apple
try{
int eng=getEng(fromLang);
Expand Down Expand Up @@ -67,8 +66,6 @@ private static int getEng(String fromLang){

// Baidu 's API
public static class BaiduAPI{
// todo: api token will expire in 30 days!

// final static String APIToken="24.ebbfc44b1c5316fdea6ec0179efe7fb4.2592000.1645786137.282335-25560885";

public static String fetchNewToken(){
Expand Down
26 changes: 12 additions & 14 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@

<!-- Main Content -->
<LinearLayout
android:id="@+id/mainAct_mainContent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
Expand All @@ -77,21 +76,21 @@
android:layout_height="38dp"
android:textSize="20sp"
android:textAlignment="textStart"
android:text="Auto"/>
android:text="@string/auto"/>
<RadioButton
android:id="@+id/mainAct_Select_English"
android:layout_width="match_parent"
android:layout_height="38dp"
android:textSize="20sp"
android:textAlignment="textStart"
android:text="English"/>
android:text="@string/english"/>
<RadioButton
android:id="@+id/mainAct_Select_Chinese"
android:layout_width="match_parent"
android:layout_height="38dp"
android:textSize="20sp"
android:textAlignment="textStart"
android:text="Chinese"/>
android:text="@string/chinese"/>
</RadioGroup>
</ScrollView>

Expand All @@ -107,7 +106,7 @@
android:layout_height="48dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:text="English"
android:text="@string/en"
android:background="@drawable/set_button_selector"
style="?android:attr/buttonBarButtonStyle" />
<Button
Expand All @@ -116,7 +115,7 @@
android:layout_height="44dp"
android:layout_margin="10dp"
android:padding="0dp"
android:text="~"
android:text="@string/exchange_sign"
android:background="@drawable/set_button_selector"
style="?android:attr/buttonBarButtonStyle" />
<Button
Expand All @@ -125,7 +124,7 @@
android:layout_height="48dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:text="Chinese"
android:text="@string/zh"
android:background="@drawable/set_button_selector"
style="?android:attr/buttonBarButtonStyle" />
</LinearLayout>
Expand All @@ -135,7 +134,7 @@
android:id="@+id/mainAct_TextToTrans"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Input Words to translate here. "
android:hint="@string/input_words_to_translate_here"
android:text=""
android:layout_marginHorizontal="28dp"
android:layout_marginTop="18dp"
Expand All @@ -151,7 +150,7 @@
android:layout_marginVertical="14dp"
android:layout_marginHorizontal="42dp"
android:background="@drawable/set_button_selector"
android:text="Translate"/>
android:text="@string/translate"/>

<!-- Translation output -->
<!-- sentence translation output view -->
Expand Down Expand Up @@ -198,7 +197,7 @@
android:paddingVertical="9dp"
android:background="@drawable/main_act_background_evr_day_stc"
android:gravity="bottom"
android:text="Everyday sentence."/>
android:text="@string/everyday_sentence"/>
</LinearLayout>

<!-- top layer -->
Expand All @@ -218,17 +217,16 @@
android:id="@+id/mainAct_MenuUseBaiduAPI"
android:layout_width="177dp"
android:layout_height="58dp"
android:text="Baidu Translate"/>
android:text="@string/baidu_translate"/>
<Button
android:id="@+id/mainAct_MenuUseYoudaoAPI"
android:layout_width="177dp"
android:layout_height="58dp"
android:text="YouDao Translate"/>
android:text="@string/youdao_translate"/>
<Button
android:id="@+id/mainAct_MenuUseXXXAPI"
android:layout_width="177dp"
android:layout_height="58dp"
android:text="..."/>
android:text="@string/more_sign"/>

</LinearLayout>
</RelativeLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/listview_explain_word.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:padding="8dp"
android:layout_margin="8dp">
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
Loading

0 comments on commit 800fd89

Please sign in to comment.