Deepl integration + set API KEY

This commit is contained in:
stom79 2018-08-14 16:20:53 +02:00
parent 406b9ad5d7
commit 2bd65baab6
7 changed files with 140 additions and 36 deletions

View File

@ -65,7 +65,7 @@ dependencies {
implementation "com.gongwen:swipeback:$swipebackLibraryVersion"
implementation 'com.github.stom79:country-picker-android:1.2.0'
implementation 'com.github.stom79:mytransl:1.4'
implementation 'com.github.stom79:mytransl:1.5'
playstoreImplementation "io.github.kobakei:ratethisapp:$ratethisappLibraryVersion"

View File

@ -571,7 +571,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
holder.yandex_translate.setVisibility(View.VISIBLE);
break;
default:
holder.yandex_translate.setVisibility(View.VISIBLE);
holder.yandex_translate.setVisibility(View.GONE);
}
//Manages theme for icon colors
@ -1988,9 +1988,29 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
private void translateToot(Status status){
//Manages translations
final MyTransL myTransL = MyTransL.getInstance(MyTransL.translatorEngine.YANDEX);
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
int trans = sharedpreferences.getInt(Helper.SET_TRANSLATOR, Helper.TRANS_YANDEX);
MyTransL.translatorEngine et = MyTransL.translatorEngine.YANDEX;
String api_key = null;
if( trans == Helper.TRANS_YANDEX) {
et = MyTransL.translatorEngine.YANDEX;
}else if( trans == Helper.TRANS_DEEPL) {
et = MyTransL.translatorEngine.DEEPL;
}
final MyTransL myTransL = MyTransL.getInstance(et);
myTransL.setObfuscation(true);
myTransL.setYandexAPIKey(Helper.YANDEX_KEY);
if( trans == Helper.TRANS_YANDEX) {
api_key = sharedpreferences.getString(Helper.SET_YANDEX_API_KEY, Helper.YANDEX_KEY);
myTransL.setYandexAPIKey(api_key);
}else if( trans == Helper.TRANS_DEEPL) {
api_key = sharedpreferences.getString(Helper.SET_DEEPL_API_KEY, "");
myTransL.setDeeplAPIKey(api_key);
}
if( !status.isTranslated() ){
String statusToTranslate;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
@ -2015,6 +2035,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
}
@Override
public void onFail(HttpsConnectionException e) {
e.printStackTrace();
Toast.makeText(context, R.string.toast_error_translate, Toast.LENGTH_LONG).show();
}
});

View File

@ -33,6 +33,8 @@ import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v7.widget.SwitchCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -40,6 +42,7 @@ import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
@ -57,6 +60,7 @@ import fr.gouv.etalab.mastodon.R;
import static android.app.Activity.RESULT_OK;
import static fr.gouv.etalab.mastodon.helper.Helper.CHANGE_THEME_INTENT;
import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION;
import static fr.gouv.etalab.mastodon.helper.Helper.SET_YANDEX_API_KEY;
import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor;
@ -71,6 +75,8 @@ public class SettingsFragment extends Fragment {
private static final int ACTIVITY_CHOOSE_FILE = 411;
private TextView set_folder;
int count1, count2, count3 = 0;
private EditText your_api_key;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@ -170,6 +176,36 @@ public class SettingsFragment extends Fragment {
editor.apply();
}
});
your_api_key = rootView.findViewById(R.id.translation_key);
your_api_key.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
SharedPreferences.Editor editor = sharedpreferences.edit();
int translatore = sharedpreferences.getInt(Helper.SET_TRANSLATOR, Helper.TRANS_YANDEX);
String store = null;
if( translatore == Helper.TRANS_YANDEX)
store = Helper.SET_YANDEX_API_KEY;
else if( translatore == Helper.TRANS_DEEPL)
store = Helper.SET_DEEPL_API_KEY;
if( store != null)
if( s != null && s.length() > 0)
editor.putString(store, s.toString().trim());
else
editor.putString(store, null);
editor.apply();
}
});
boolean notif_validation_fav = sharedpreferences.getBoolean(Helper.SET_NOTIF_VALIDATION_FAV, false);
final CheckBox set_share_validation_fav = rootView.findViewById(R.id.set_share_validation_fav);
@ -645,11 +681,20 @@ public class SettingsFragment extends Fragment {
switch (sharedpreferences.getInt(Helper.SET_TRANSLATOR, Helper.TRANS_YANDEX)){
case Helper.TRANS_YANDEX:
positionSpinnerTrans = 0;
your_api_key.setVisibility(View.VISIBLE);
your_api_key.setText(sharedpreferences.getString(Helper.SET_YANDEX_API_KEY, ""));
break;
case Helper.TRANS_DEEPL:
positionSpinnerTrans = 1;
your_api_key.setVisibility(View.VISIBLE);
your_api_key.setText(sharedpreferences.getString(Helper.SET_DEEPL_API_KEY, ""));
break;
case Helper.TRANS_NONE:
positionSpinnerTrans = 1;
positionSpinnerTrans = 2;
your_api_key.setVisibility(View.GONE);
break;
default:
your_api_key.setVisibility(View.VISIBLE);
positionSpinnerTrans = 0;
}
translation_layout_spinner.setSelection(positionSpinnerTrans);
@ -660,10 +705,21 @@ public class SettingsFragment extends Fragment {
SharedPreferences.Editor editor = sharedpreferences.edit();
switch (position){
case 0:
your_api_key.setVisibility(View.VISIBLE);
editor.putInt(Helper.SET_TRANSLATOR, Helper.TRANS_YANDEX);
editor.apply();
if( sharedpreferences.getString(Helper.SET_DEEPL_API_KEY, null) != null)
your_api_key.setText(sharedpreferences.getString(Helper.SET_DEEPL_API_KEY, ""));
break;
case 1:
your_api_key.setVisibility(View.VISIBLE);
editor.putInt(Helper.SET_TRANSLATOR, Helper.TRANS_DEEPL);
editor.apply();
if( sharedpreferences.getString(SET_YANDEX_API_KEY, null) != null)
your_api_key.setText(sharedpreferences.getString(SET_YANDEX_API_KEY, null));
break;
case 2:
your_api_key.setVisibility(View.GONE);
set_trans_forced.isChecked();
editor.putBoolean(Helper.SET_TRANS_FORCED, false);
editor.putInt(Helper.SET_TRANSLATOR, Helper.TRANS_NONE);

View File

@ -262,6 +262,7 @@ public class Helper {
public static final int LED_COLOUR = 0;
public static final int TRANS_YANDEX = 0;
public static final int TRANS_DEEPL = 1;
public static final int TRANS_NONE = 2;
public static final String SET_TRANS_FORCED = "set_trans_forced";
@ -327,6 +328,8 @@ public class Helper {
//User agent
public static final String USER_AGENT = "Mastalab/"+ BuildConfig.VERSION_NAME + " Android/"+ Build.VERSION.RELEASE;
public static final String SET_YANDEX_API_KEY = "set_yandex_api_key";
public static final String SET_DEEPL_API_KEY = "set_deepl_api_key";
private static boolean menuAccountsOpened = false;

View File

@ -197,29 +197,39 @@
android:layout_height="wrap_content" />
</LinearLayout>
<!-- TABS Layout -->
<!-- Translation engine -->
<LinearLayout
android:layout_marginTop="10dp"
android:id="@+id/translation_layout_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/about_yandex"/>
<Spinner
android:id="@+id/translation_layout_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:id="@+id/set_trans_forced"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/about_yandex"/>
<Spinner
android:id="@+id/translation_layout_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:id="@+id/set_trans_forced"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<EditText
android:hint="@string/your_api_key"
android:id="@+id/translation_key"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

View File

@ -202,21 +202,32 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/about_yandex"/>
<Spinner
android:id="@+id/translation_layout_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:id="@+id/set_trans_forced"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/about_yandex"/>
<Spinner
android:id="@+id/translation_layout_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:id="@+id/set_trans_forced"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<EditText
android:hint="@string/your_api_key"
android:id="@+id/translation_key"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

View File

@ -351,9 +351,12 @@
<string-array name="settings_translation">
<item>Yandex</item>
<item>DeepL</item>
<item>No</item>
</string-array>
<string name="your_api_key">Your API key, you can leave blank for Yandex</string>
<string-array name="settings_theme">
<item>Dark</item>
<item>Light</item>