From 153d5ff15929467418e0b1aa8bbb1199f1ba83c3 Mon Sep 17 00:00:00 2001 From: 18401019693 Date: Mon, 3 Apr 2023 15:29:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BF=83=E6=84=BF=E5=8D=95?= =?UTF-8?q?=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 3 + .../java/com/shayu/phonelive/AppContext.java | 3 + .../yunbao/common/activity/AbsActivity.java | 16 +-- .../com/yunbao/common/bean/WishModel.java | 5 +- .../fragment/LiveNewWishListFragment.java | 13 +- .../com/yunbao/common/utils/AppManager.java | 130 ++++++++++++++++++ .../common/views/LiveNewWishListPopup.java | 12 +- common/src/main/res/values-en/strings.xml | 2 + common/src/main/res/values/strings.xml | 2 + main/src/main/AndroidManifest.xml | 4 + .../activity/LanguageSettingActivity.java | 54 ++++++++ .../yunbao/main/activity/SettingActivity.java | 7 + .../com/yunbao/main/utils/LanguageUtil.java | 95 +++++++++++++ .../main/res/drawable/background_language.xml | 9 ++ main/src/main/res/drawable/icon_xuanzhong.png | Bin 0 -> 1499 bytes .../res/layout/activity_language_sett.xml | 107 ++++++++++++++ main/src/main/res/layout/activity_setting.xml | 31 +++++ 17 files changed, 482 insertions(+), 11 deletions(-) create mode 100644 common/src/main/java/com/yunbao/common/utils/AppManager.java create mode 100644 main/src/main/java/com/yunbao/main/activity/LanguageSettingActivity.java create mode 100644 main/src/main/java/com/yunbao/main/utils/LanguageUtil.java create mode 100644 main/src/main/res/drawable/background_language.xml create mode 100644 main/src/main/res/drawable/icon_xuanzhong.png create mode 100644 main/src/main/res/layout/activity_language_sett.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6f4878418..3fedf332c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -6,6 +6,8 @@ + (activity)); CrashSaveBean.getInstance().setActivitySize(activities); + AppManager.getInstance().addActivity(activity); } @Override diff --git a/common/src/main/java/com/yunbao/common/activity/AbsActivity.java b/common/src/main/java/com/yunbao/common/activity/AbsActivity.java index e75bf5ae6..42fa80d32 100644 --- a/common/src/main/java/com/yunbao/common/activity/AbsActivity.java +++ b/common/src/main/java/com/yunbao/common/activity/AbsActivity.java @@ -43,14 +43,14 @@ public abstract class AbsActivity extends AppCompatActivity { protected List mLifeCycleListeners; protected boolean isFullWindow; - @Override - public Resources getResources() { - Resources res = super.getResources(); - Configuration config = new Configuration(); - config.setToDefaults(); - res.updateConfiguration(config, res.getDisplayMetrics()); - return res; - } +// @Override +// public Resources getResources() { +// Resources res = super.getResources(); +// Configuration config = new Configuration(); +// config.setToDefaults(); +// res.updateConfiguration(config, res.getDisplayMetrics()); +// return res; +// } @Override diff --git a/common/src/main/java/com/yunbao/common/bean/WishModel.java b/common/src/main/java/com/yunbao/common/bean/WishModel.java index b1bf7e722..136fbe952 100644 --- a/common/src/main/java/com/yunbao/common/bean/WishModel.java +++ b/common/src/main/java/com/yunbao/common/bean/WishModel.java @@ -1,5 +1,8 @@ package com.yunbao.common.bean; +import androidx.room.Transaction; + +import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; public class WishModel extends BaseModel { @@ -29,7 +32,7 @@ public class WishModel extends BaseModel { @SerializedName("price") private int price; @SerializedName("isShow") - private int isShow = -1; + private transient int isShow = -1; public int getIsShow() { return isShow; diff --git a/common/src/main/java/com/yunbao/common/fragment/LiveNewWishListFragment.java b/common/src/main/java/com/yunbao/common/fragment/LiveNewWishListFragment.java index f7c912e08..897c19eb9 100644 --- a/common/src/main/java/com/yunbao/common/fragment/LiveNewWishListFragment.java +++ b/common/src/main/java/com/yunbao/common/fragment/LiveNewWishListFragment.java @@ -10,6 +10,7 @@ import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.lxj.xpopup.XPopup; import com.yunbao.common.R; import com.yunbao.common.adapter.LiveNewWishAdapter; @@ -23,6 +24,7 @@ import com.yunbao.common.utils.ToastUtil; import com.yunbao.common.views.LiveNewWishGiftPopup; import com.yunbao.common.views.weight.ViewClicksAntiShake; +import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; @@ -100,8 +102,17 @@ public class LiveNewWishListFragment extends BaseFragment { public void saveWish() { List wishList = liveNewWishAdapter.getWishList(); wishList.remove(wishList.size() - 1); + // 1. Gson构造器 + GsonBuilder builder = new GsonBuilder(); + // 2. 排除使用特定修饰符的字段 + builder.excludeFieldsWithModifiers(Modifier.TRANSIENT); + // 3. 格式良好的输出 + builder.setPrettyPrinting(); + // 4. 创建Gson对象 + Gson gson = builder.create(); + LiveNetManager.get(getContext()). - setWishlistV2(type, new Gson().toJson(wishList), new HttpCallback() { + setWishlistV2(type, gson.toJson(wishList), new HttpCallback() { @Override public void onSuccess(String data) { ToastUtil.show(data); diff --git a/common/src/main/java/com/yunbao/common/utils/AppManager.java b/common/src/main/java/com/yunbao/common/utils/AppManager.java new file mode 100644 index 000000000..95f7d5a83 --- /dev/null +++ b/common/src/main/java/com/yunbao/common/utils/AppManager.java @@ -0,0 +1,130 @@ +package com.yunbao.common.utils; + +import android.app.Activity; + +import java.util.Stack; + +public class AppManager { + + private static Stack activityStack; + + public AppManager() { + } + + /** + * 单一实例 + */ + public static AppManager getInstance() { + return SingleApp.INSTANCE; + } + + public static class SingleApp { + public static AppManager INSTANCE = new AppManager(); + } + + /** + * 添加Activity到堆栈 + */ + public void addActivity(Activity activity) { + if (activityStack == null) { + activityStack = new Stack(); + } + activityStack.add(activity); + } + + /** + * 移除Activity + */ + public void removeActivity(Activity activity) { + activityStack.remove(activity); + } + + /** + * 获取指定的Activity + */ + public static Activity getActivity(Class cls) { + if (activityStack != null) + for (Activity activity : activityStack) { + if (activity.getClass().equals(cls)) { + return activity; + } + } + return null; + } + + /** + * 获取当前显示Activity(堆栈中最后一个传入的activity) + */ + public Activity getLastActivity() { + Activity activity = activityStack.lastElement(); + return activity; + } + + /** + * 获取所有Activity + */ + public Stack getAllActivityStacks() { + return activityStack; + } + + /** + * 结束指定的Activity + */ + public void finishActivity(Activity activity) { + if (activity != null) { + if (!activity.isFinishing()) { + activity.finish(); + activityStack.remove(activity); + } + } + } + + /** + * 结束指定类名的Activity + */ + public void finishActivity(Class cls) { + for (Activity activity : activityStack) { + if (activity.getClass().equals(cls)) { + finishActivity(activity); + break; + } + } + } + + /** + * 结束除当前传入以外所有Activity + */ + public void finishOthersActivity(Class cls) { + if (activityStack != null) + for (Activity activity : activityStack) { + if (!activity.getClass().equals(cls)) { + activity.finish(); + } + } + } + + /** + * 结束所有Activity + */ + public void finishAllActivity() { + if (activityStack != null) + for (Activity activity : activityStack) { + activity.finish(); + } + activityStack.clear(); + } + + + /** + * 退出应用程序 + */ + public void AppExit() { + try { + finishAllActivity(); + android.os.Process.killProcess(android.os.Process.myPid());// 杀死该应用进程 + System.exit(0); + } catch (Exception e) { + } + } + +} \ No newline at end of file diff --git a/common/src/main/java/com/yunbao/common/views/LiveNewWishListPopup.java b/common/src/main/java/com/yunbao/common/views/LiveNewWishListPopup.java index b4a636abc..a2b6f830b 100644 --- a/common/src/main/java/com/yunbao/common/views/LiveNewWishListPopup.java +++ b/common/src/main/java/com/yunbao/common/views/LiveNewWishListPopup.java @@ -14,6 +14,7 @@ import androidx.viewpager2.widget.ViewPager2; import com.google.android.material.tabs.TabLayout; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.lxj.xpopup.XPopup; import com.lxj.xpopup.core.BottomPopupView; import com.yunbao.common.R; @@ -33,6 +34,7 @@ import com.yunbao.common.views.weight.ViewClicksAntiShake; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; +import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; @@ -286,8 +288,16 @@ public class LiveNewWishListPopup extends BottomPopupView { break; } List wishList = new ArrayList<>(); + // 1. Gson构造器 + GsonBuilder builder = new GsonBuilder(); + // 2. 排除使用特定修饰符的字段 + builder.excludeFieldsWithModifiers(Modifier.TRANSIENT); + // 3. 格式良好的输出 + builder.setPrettyPrinting(); + // 4. 创建Gson对象 + Gson gson = builder.create(); LiveNetManager.get(getContext()). - setWishlistV2(type, new Gson().toJson(wishList), new HttpCallback() { + setWishlistV2(type, gson.toJson(wishList), new HttpCallback() { @Override public void onSuccess(String data) { ToastUtil.show(data); diff --git a/common/src/main/res/values-en/strings.xml b/common/src/main/res/values-en/strings.xml index 81ad887c4..6a63d995c 100644 --- a/common/src/main/res/values-en/strings.xml +++ b/common/src/main/res/values-en/strings.xml @@ -1116,4 +1116,6 @@ Limited ride And limited avatar frame Use wish Play setting + language setting + Traditional Chinese diff --git a/common/src/main/res/values/strings.xml b/common/src/main/res/values/strings.xml index 3939bc3ce..d7ef14ef8 100644 --- a/common/src/main/res/values/strings.xml +++ b/common/src/main/res/values/strings.xml @@ -1114,5 +1114,7 @@ 使用 心願 播放設置 + 語言設定 + 繁體中文 diff --git a/main/src/main/AndroidManifest.xml b/main/src/main/AndroidManifest.xml index e9afd0342..950e8e921 100644 --- a/main/src/main/AndroidManifest.xml +++ b/main/src/main/AndroidManifest.xml @@ -60,6 +60,10 @@ + onBackPressed()); + ViewClicksAntiShake.clicksAntiShake(traditionalChinese, new ViewClicksAntiShake.ViewClicksCallBack() { + @Override + public void onViewClicks() { + changeAllActivitysLanguage(Locale.SIMPLIFIED_CHINESE); + } + }); + ViewClicksAntiShake.clicksAntiShake(english, new ViewClicksAntiShake.ViewClicksCallBack() { + @Override + public void onViewClicks() { + changeAllActivitysLanguage(Locale.US); + } + }); + } + + private void changeAllActivitysLanguage(Locale locale) { + Stack allActivityStacks = AppManager.getInstance().getAllActivityStacks(); + for (int i = 0; i < allActivityStacks.size(); i++) { + LanguageUtil.shiftLanguage(locale, allActivityStacks.get(i),mContext); + } + + } +} diff --git a/main/src/main/java/com/yunbao/main/activity/SettingActivity.java b/main/src/main/java/com/yunbao/main/activity/SettingActivity.java index 1da67ee61..2acb7ded4 100644 --- a/main/src/main/java/com/yunbao/main/activity/SettingActivity.java +++ b/main/src/main/java/com/yunbao/main/activity/SettingActivity.java @@ -144,6 +144,13 @@ public class SettingActivity extends AbsActivity implements OnItemClickListener< startActivity(new Intent(SettingActivity.this, MsgSettActivity.class)); } }); + //语言设置 + ViewClicksAntiShake.clicksAntiShake(findViewById(R.id.language_setting), new ViewClicksAntiShake.ViewClicksCallBack() { + @Override + public void onViewClicks() { + startActivity(new Intent(SettingActivity.this, LanguageSettingActivity.class)); + } + }); //屏蔽礼物特效 ViewClicksAntiShake.clicksAntiShake(studioGiftEffects, new ViewClicksAntiShake.ViewClicksCallBack() { @Override diff --git a/main/src/main/java/com/yunbao/main/utils/LanguageUtil.java b/main/src/main/java/com/yunbao/main/utils/LanguageUtil.java new file mode 100644 index 000000000..c18836014 --- /dev/null +++ b/main/src/main/java/com/yunbao/main/utils/LanguageUtil.java @@ -0,0 +1,95 @@ +package com.yunbao.main.utils; + +import android.app.Activity; +import android.app.backup.BackupManager; +import android.content.Context; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.util.DisplayMetrics; + +import com.yunbao.main.activity.MainActivity; + +import java.lang.reflect.Method; +import java.util.Locale; + +public class LanguageUtil { + + /** + * 这个方法虽然更新了资源但是只能以后的界面生效,之前没有finish的页面还是保留原来的语言 + * + * @param locale + * @param context + */ + public static void shiftLanguage(Locale locale, Context context) { + Resources resources = context.getResources(); + Configuration config = resources.getConfiguration(); + DisplayMetrics dm = resources.getDisplayMetrics(); + config.locale = locale; + resources.updateConfiguration(config, dm); + + } + + /** + * 这个方法是为了让全部的activity都修改语言 + * + * @param locale + * @param activity + * @param context + */ + public static void shiftLanguage(Locale locale, Activity activity, Context context) { + Resources resources = context.getResources(); + Configuration config = resources.getConfiguration(); + DisplayMetrics dm = resources.getDisplayMetrics(); + config.locale = locale; + resources.updateConfiguration(config, dm); + if (!(activity instanceof MainActivity)) { + activity.recreate(); + } + + } + + public static void updateLanguage(Locale locale) { + try { + Object objIActMag, objActMagNative; + Class clzIActMag = Class.forName("android.app.IActivityManager"); + + Class clzActMagNative = Class + .forName("android.app.ActivityManagerNative"); + //amn = ActivityManagerNative.getDefault(); + Method mtdActMagNative$getDefault = clzActMagNative + .getDeclaredMethod("getDefault"); + objIActMag = mtdActMagNative$getDefault.invoke(clzActMagNative); + // objIActMag = amn.getConfiguration(); + Method mtdIActMag$getConfiguration = clzIActMag + .getDeclaredMethod("getConfiguration"); + Configuration config = (Configuration) mtdIActMag$getConfiguration.invoke(objIActMag); + // set the locale to the new value + config.locale = locale; + + //持久化 config.userSetLocale = true; + Class clzConfig = Class + .forName("android.content.res.Configuration"); + java.lang.reflect.Field userSetLocale = clzConfig + .getField("userSetLocale"); + userSetLocale.set(config, true); + + //如果有阿拉伯语,必须加上,否则阿拉伯语与其它语言切换时,布局与文字方向不会改变 + Method setLayoutDirection = clzConfig + .getDeclaredMethod("setLayoutDirection", Locale.class); + setLayoutDirection.invoke(config, locale); + + // 此处需要声明权限:android.permission.CHANGE_CONFIGURATION + // 会重新调用 onCreate(); + Class[] clzParams = {Configuration.class}; + // objIActMag.updateConfiguration(config); + Method mtdIActMag$updateConfiguration = clzIActMag + .getDeclaredMethod("updateConfiguration", clzParams); + + mtdIActMag$updateConfiguration.invoke(objIActMag, config); + BackupManager.dataChanged("com.android.providers.settings"); + + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/main/src/main/res/drawable/background_language.xml b/main/src/main/res/drawable/background_language.xml new file mode 100644 index 000000000..7c342c273 --- /dev/null +++ b/main/src/main/res/drawable/background_language.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/main/src/main/res/drawable/icon_xuanzhong.png b/main/src/main/res/drawable/icon_xuanzhong.png new file mode 100644 index 0000000000000000000000000000000000000000..24b810cddcda0ca561800e9d33aea5c102005925 GIT binary patch literal 1499 zcmb8v`#;lr0LSsqcNa4mQ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@V^we)peN5!hf7dA(Vm%OZ>F)4q|VG?A0 zGxMJt>7IxY^FbQs2R~0bB8>H)UK*lG^k{~V_{Bb&lAFz&E%-Q-NY2Mz|J>n_Tav2e zZ-dEAM8)7hz}t(u+hf19LUH|ualie7?4!auTf5$7lPBCAR4{HxLpyE6=xn8UvCT;F zV*Y8_?6I59`s>CVXYXg!lWBgyuUK+sB6e}Xuu>}d8GnCjz9dp%t7kq#qP-|+G&aWf mWF?A&-N66n>^~2|K`@VK&?O3Z;qN9Fv<579PdAxsP~tymN2PxN literal 0 HcmV?d00001 diff --git a/main/src/main/res/layout/activity_language_sett.xml b/main/src/main/res/layout/activity_language_sett.xml new file mode 100644 index 000000000..725f459a7 --- /dev/null +++ b/main/src/main/res/layout/activity_language_sett.xml @@ -0,0 +1,107 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/main/src/main/res/layout/activity_setting.xml b/main/src/main/res/layout/activity_setting.xml index b5a61cd29..4cfbcf7f5 100644 --- a/main/src/main/res/layout/activity_setting.xml +++ b/main/src/main/res/layout/activity_setting.xml @@ -155,6 +155,37 @@ android:src="@mipmap/icon_arrow_right" /> + + + + + + + + + + +