Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/src/main/java/com/hyphenate/easeim/DemoHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
import com.hyphenate.easeui.provider.EaseEmojiconInfoProvider;
import com.hyphenate.easeui.provider.EaseSettingsProvider;
import com.hyphenate.easeui.provider.EaseUserProfileProvider;
import com.hyphenate.chat.translator.EMTranslateParams;
import com.hyphenate.chat.translator.EMTranslationManager;
import com.hyphenate.exceptions.HyphenateException;
import com.hyphenate.push.EMPushConfig;
import com.hyphenate.push.EMPushHelper;
Expand Down Expand Up @@ -149,6 +151,9 @@ public void init(Context context) {
//callKit初始化
InitCallKit(context);

//Translation Manager 初始化
initTranslationManager();

//启动获取用户信息线程
fetchUserInfoList = FetchUserInfoList.getInstance();
fetchUserRunnable = new FetchUserRunnable();
Expand Down Expand Up @@ -375,6 +380,12 @@ public EaseUser getUser(String username) {
});
}

private void initTranslationManager() {
EMTranslateParams params = new EMTranslateParams("46c34219512d4f09ae6f8e04c083b7a3", "https://api.cognitive.microsofttranslator.com");

EMTranslationManager.getInstance().init(params);
}

/**
* 统一配置头像
* @return
Expand Down Expand Up @@ -575,6 +586,7 @@ public void logoutSuccess() {
Log.d(TAG, "logout: onSuccess");
setAutoLogin(false);
DemoDbHelper.getInstance(DemoApplication.getInstance()).closeDb();
EMTranslationManager.getInstance().logout();
}

public EaseAvatarOptions getEaseAvatarOptions() {
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/hyphenate/easeim/common/model/DemoModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,20 @@ public void modifyConComeFromStatus() {
preferences.edit().putBoolean("is_conversation_come_from_server", false).apply();
}

/**
* 获取目标翻译语言
*/
public String getTargetLanguage() {
return PreferenceManager.getInstance().getTargetLanguage();
}

/**
* 设置目标翻译语言
*/
public void setTargetLanguage(String languageCode) {
PreferenceManager.getInstance().setTargetLanguage(languageCode);
}

enum Key{
VibrateAndPlayToneOn,
VibrateOn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public class PreferenceManager {

private static String SHARED_KEY_ENABLE_TOKEN_LOGIN = "enable_token_login";

private static String SHARED_KEY_TARGET_LANGUAGE = "shared_key_target_language";

@SuppressLint("CommitPrefEdits")
private PreferenceManager(Context cxt) {
mSharedPreferences = cxt.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
Expand Down Expand Up @@ -570,4 +572,16 @@ public boolean isEnableTokenLogin() {
return mSharedPreferences.getBoolean(SHARED_KEY_ENABLE_TOKEN_LOGIN, false);
}

/**
* 翻译目标语言
* @param languageCode
*/
public void setTargetLanguage(String languageCode) {
editor.putString(SHARED_KEY_TARGET_LANGUAGE, languageCode);
editor.apply();
}

public String getTargetLanguage() {
return mSharedPreferences.getString(SHARED_KEY_TARGET_LANGUAGE, "en");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.hyphenate.easeim.section.contact.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hyphenate.chat.translator.EMLanguage;

import java.util.List;

import com.hyphenate.easeim.R;

public class LanguageAdapter extends ArrayAdapter<EMLanguage> {
private long mSelectedIndex = 0;

public LanguageAdapter(@NonNull Context context, @NonNull List<EMLanguage> objects) {
super(context, 0, objects);
}

@Override
public View getView(int position, View convertedView, ViewGroup parent) {
if(convertedView == null) {
convertedView = LayoutInflater.from(getContext()).inflate(R.layout.demo_item_language, parent, false);
}

TextView textView = (TextView) convertedView.findViewById(R.id.language_content);
ImageView imageView = (ImageView) convertedView.findViewById(R.id.language_select);

EMLanguage languageItem = getItem(position);
textView.setText(languageItem.LanguageLocalName);
long id = getItemId(position);
if (mSelectedIndex == id) {
imageView.setImageResource(R.drawable.yes);
}else {
imageView.setImageResource(0);
}

return convertedView;
}

public void setSelectedIndex(long index) {
mSelectedIndex = index;
}

public long getSelectedIndex() {
return mSelectedIndex;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class CommonSettingsActivity extends BaseInitActivity implements View.OnC
private SwitchItemView itemAutoDownload;
private SwitchItemView itemAutoAcceptGroup;
private SwitchItemView itemSwitchChatroomDeleteMsg;
private ArrowItemView itemLanguage;

private DemoModel settingsModel;
private EMOptions chatOptions;
Expand Down Expand Up @@ -55,6 +56,7 @@ protected void initView(Bundle savedInstanceState) {
itemAutoDownload = findViewById(R.id.item_switch_auto_download);
itemAutoAcceptGroup = findViewById(R.id.item_switch_auto_accept_group);
itemSwitchChatroomDeleteMsg = findViewById(R.id.item_switch_chatroom_delete_msg);
itemLanguage = findViewById(R.id.item_language);
}

@Override
Expand All @@ -71,6 +73,7 @@ protected void initListener() {
itemAutoDownload.setOnCheckedChangeListener(this);
itemAutoAcceptGroup.setOnCheckedChangeListener(this);
itemSwitchChatroomDeleteMsg.setOnCheckedChangeListener(this);
itemLanguage.setOnClickListener(this);
}

@Override
Expand Down Expand Up @@ -98,6 +101,9 @@ public void onClick(View v) {
case R.id.item_call_option :
CallOptionActivity.actionStart(mContext);
break;
case R.id.item_language:
LanguageActivity.actionStart(mContext);
break;
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.hyphenate.easeim.section.me.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.hyphenate.easeim.DemoHelper;
import com.hyphenate.easeim.R;
import com.hyphenate.easeim.section.base.BaseInitActivity;
import com.hyphenate.easeim.section.contact.adapter.LanguageAdapter;
import com.hyphenate.chat.translator.EMLanguage;
import com.hyphenate.chat.translator.EMTranslationManager;
import com.hyphenate.easeui.widget.EaseTitleBar;

import java.util.List;

public class LanguageActivity extends BaseInitActivity implements EaseTitleBar.OnBackPressListener, EaseTitleBar.OnRightClickListener, AdapterView.OnItemClickListener {
private EaseTitleBar titleBar;
private ListView rvList;
private LanguageAdapter adapter;

private List<EMLanguage> emLanguageList;

public static void actionStart(Context context) {
Intent starter = new Intent(context, LanguageActivity.class);
context.startActivity(starter);
}

@Override
protected int getLayoutId() {
return R.layout.activity_language;
}

@Override
protected void initView(Bundle savedInstanceState) {
super.initView(savedInstanceState);
titleBar = findViewById(R.id.title_bar_language);
rvList = findViewById(R.id.language_list);

emLanguageList = EMTranslationManager.getInstance().getSupportedLanguages();
adapter = new LanguageAdapter(mContext, emLanguageList);
rvList.setAdapter(adapter);
initSelectedLanguage();
rvList.setOnItemClickListener(this);
}

@Override
protected void initListener() {
super.initListener();
titleBar.setOnBackPressListener(this);
titleBar.setOnRightClickListener(this);
}


@Override
public void onBackPress(View view) {
onBackPressed();
}


@Override
public void onRightClick(View view) {
updateLanguage();

onBackPressed();
}

private void initSelectedLanguage() {
String languageCode = DemoHelper.getInstance().getModel().getTargetLanguage();
int selectedIndex = 0;

for(int index = 0 ; index < emLanguageList.size(); index++) {
EMLanguage language = emLanguageList.get(index);
if(language.LanguageCode.equals(languageCode)) {
selectedIndex = index;
break;
}
}

adapter.setSelectedIndex(selectedIndex);
}

private void updateLanguage() {
long selectedIndex = adapter.getSelectedIndex();
String languageCode = emLanguageList.get((int)selectedIndex).LanguageCode;

DemoHelper.getInstance().getModel().setTargetLanguage(languageCode);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(id != adapter.getSelectedIndex()) {
adapter.setSelectedIndex(id);
adapter.notifyDataSetChanged();
}
}
}
Binary file added app/src/main/res/drawable-hdpi/yes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions app/src/main/res/layout/activity_language.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/common_bg">

<com.hyphenate.easeui.widget.EaseTitleBar
android:id="@+id/title_bar_language"
android:layout_width="0dp"
android:layout_height="@dimen/em_common_title_bar_height"
android:background="@color/white"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:titleBarDisplayHomeAsUpEnabled="true"
app:titleBarRightTitle="完成"
app:titleBarRightVisible="true"
app:titleBarTitle="取消"
app:titleBarTitlePosition="left" />

<android.widget.RelativeLayout
android:id="@+id/srl_refresh"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/title_bar_language"
app:layout_constraintBottom_toBottomOf="parent">

<android.widget.ListView
android:id="@+id/language_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@null"
android:minHeight="200dp" />

</android.widget.RelativeLayout>


</androidx.constraintlayout.widget.ConstraintLayout>
15 changes: 14 additions & 1 deletion app/src/main/res/layout/demo_activity_common_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,26 @@
android:layout_height="@dimen/em_common_item_height"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/item_security"
app:layout_constraintTop_toBottomOf="@id/item_language"
app:arrowItemAvatarHeight="@dimen/em_about_me_avatar_size"
app:arrowItemAvatarWidth="@dimen/em_about_me_avatar_size"
app:arrowItemTitle="@string/push_settings"
app:arrowItemTitleSize="@dimen/em_size_normal"
android:foreground="@drawable/demo_ripple_click_gray"
android:background="@color/white"/>
<com.hyphenate.easeim.common.widget.ArrowItemView
android:id="@+id/item_language"
android:layout_width="match_parent"
android:layout_height="@dimen/em_common_item_height"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/item_security"
app:arrowItemAvatarHeight="25dp"
app:arrowItemAvatarWidth="25dp"
app:arrowItemTitle="多语言"
app:arrowItemTitleSize="14sp"
android:foreground="@drawable/demo_ripple_click_gray"
android:background="@color/white"/>

<com.hyphenate.easeim.common.widget.ArrowItemView
android:id="@+id/item_call_option"
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/res/layout/demo_item_language.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/white"
android:gravity="center_vertical"
android:orientation="horizontal">


<TextView
android:id="@+id/language_content"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_weight="0.6"
android:gravity="center_vertical"
android:textColor="@color/black"
tools:text="easemob-demo#chatdemoui" />


<ImageView
android:id="@+id/language_select"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="0dp"
android:layout_weight="0.1"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false" />

</LinearLayout>