feat:完成大陆隔离需求(请求信息添加:运营商、本机语言)
This commit is contained in:
@@ -16,6 +16,7 @@ import android.util.Log;
|
||||
import androidx.multidex.MultiDex;
|
||||
|
||||
import com.bumptech.glide.request.target.ViewTarget;
|
||||
import com.chuhai.utils.LanguageUtils;
|
||||
import com.coorchice.library.utils.LogUtils;
|
||||
import com.facebook.stetho.Stetho;
|
||||
import com.hjq.toast.ToastUtils;
|
||||
@@ -400,6 +401,7 @@ public class XChatApplication extends BaseApp {
|
||||
httpParams.put("deviceId", DeviceUuidFactory.getDeviceId(context));
|
||||
httpParams.put("androidId", MD5Utils.getMD5String(Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID)));
|
||||
httpParams.put("channel", AppMetaDataUtil.getChannelID());
|
||||
httpParams.put("lang", LanguageUtils.INSTANCE.getSystemLanguage().toLanguageTag());
|
||||
RxNet.init(context)
|
||||
.debug(BuildConfig.DEBUG)
|
||||
.setBaseUrl(url)
|
||||
|
@@ -5,6 +5,7 @@ import android.text.TextUtils;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.chuhai.utils.TelephonyUtils;
|
||||
import com.yizhuan.xchat_android_core.auth.AuthModel;
|
||||
import com.yizhuan.xchat_android_core.utils.APIEncryptUtil;
|
||||
import com.yizhuan.xchat_android_core.utils.OaidUtil;
|
||||
@@ -34,6 +35,12 @@ public class ParamsInterceptor implements Interceptor {
|
||||
|
||||
private Map<String, String> mHttpParams;
|
||||
|
||||
// 运营商码
|
||||
private String operatorCode;
|
||||
|
||||
// 运营商码的获取时间
|
||||
private long operatorCodeTime;
|
||||
|
||||
public ParamsInterceptor(Map<String, String> params) {
|
||||
this.mHttpParams = params;
|
||||
}
|
||||
@@ -124,7 +131,6 @@ public class ParamsInterceptor implements Interceptor {
|
||||
// Log.e("ParamsInterceptor", " url: " + oldRequest.url()+ " final params Map : " + paramsMap.toString());
|
||||
// Log.e("ParamsInterceptor", "timestamp:"+timestamp + " url: " + oldRequest.url()+ " sign : " + signStr);
|
||||
|
||||
|
||||
Headers headers = oldRequest.headers().newBuilder()
|
||||
.add("pub_ticket", ticket)
|
||||
.add("pub_uid", uid == 0 ? "" : String.valueOf(uid))
|
||||
@@ -137,6 +143,7 @@ public class ParamsInterceptor implements Interceptor {
|
||||
}
|
||||
builder.addQueryParameter("pub_timestamp", timestamp);
|
||||
builder.addQueryParameter("pub_sign", signStr);
|
||||
addHeaderWithOperator(builder);
|
||||
Request newRequest = oldRequest.newBuilder()
|
||||
.method(oldRequest.method(), oldRequest.body())
|
||||
.headers(headers)
|
||||
@@ -146,5 +153,19 @@ public class ParamsInterceptor implements Interceptor {
|
||||
|
||||
}
|
||||
|
||||
private void addHeaderWithOperator(HttpUrl.Builder builder) {
|
||||
if ((System.currentTimeMillis() - operatorCodeTime) > (1000 * 60 * 5)) {
|
||||
loadOperatorCode();
|
||||
}
|
||||
builder.addQueryParameter("mcc", operatorCode);
|
||||
}
|
||||
|
||||
private void loadOperatorCode() {
|
||||
if (TelephonyUtils.INSTANCE.isChinaOperator()) {
|
||||
operatorCode = "460";
|
||||
} else {
|
||||
operatorCode = TelephonyUtils.INSTANCE.getOperatorFirstSim();
|
||||
}
|
||||
operatorCodeTime = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,19 @@
|
||||
package com.chuhai.utils
|
||||
|
||||
import android.os.Build
|
||||
import android.os.LocaleList
|
||||
import java.util.Locale
|
||||
|
||||
/**
|
||||
* Created by Max on 2023/11/17 16:12
|
||||
* Desc:
|
||||
**/
|
||||
object LanguageUtils {
|
||||
fun getSystemLanguage(): Locale {
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
LocaleList.getDefault()[0]
|
||||
} else {
|
||||
Locale.getDefault()
|
||||
}
|
||||
}
|
||||
}
|
134
library/src/module_utils/java/com/chuhai/utils/TelephonyUtils.kt
Normal file
134
library/src/module_utils/java/com/chuhai/utils/TelephonyUtils.kt
Normal file
@@ -0,0 +1,134 @@
|
||||
package com.chuhai.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.telephony.TelephonyManager
|
||||
import com.chuhai.utils.log.ILog
|
||||
|
||||
/**
|
||||
* Created by Max on 2023/11/14 10:17
|
||||
* Desc:TelephonyManager 相关工具
|
||||
**/
|
||||
object TelephonyUtils : ILog {
|
||||
|
||||
/**
|
||||
* 是否为中国运营商(任意卡属于中国就为true)
|
||||
*/
|
||||
fun isChinaOperator(): Boolean {
|
||||
val tm =
|
||||
AppUtils.getApp().getSystemService(Context.TELEPHONY_SERVICE) as? TelephonyManager
|
||||
?: return false
|
||||
if (tm.simState == TelephonyManager.SIM_STATE_READY) {
|
||||
if (!tm.simOperator.isNullOrEmpty() && tm.simOperator.startsWith("460")) {
|
||||
return true
|
||||
}
|
||||
if (isChainOperator(tm.simOperatorName)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
logD("getNetWorkOperatorTest: 在CDMA网络上,不可靠,所以不用这种方法")
|
||||
if (!tm.networkOperator.isNullOrEmpty() && tm.networkOperator.startsWith("460")) {
|
||||
return true
|
||||
}
|
||||
if (isChainOperator(tm.networkOperatorName)) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取运营商(优先SIM)
|
||||
*/
|
||||
fun getOperatorFirstSim(): String? {
|
||||
val operator = getSimOperator()
|
||||
return if (operator.isNullOrEmpty()) {
|
||||
getNetWorkOperator()
|
||||
} else {
|
||||
operator
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取SIM运营商名称
|
||||
*/
|
||||
fun getSimOperator(): String? {
|
||||
val tm =
|
||||
AppUtils.getApp().getSystemService(Context.TELEPHONY_SERVICE) as? TelephonyManager
|
||||
?: return null
|
||||
if (tm.simState != TelephonyManager.SIM_STATE_READY) {
|
||||
logD("SIM状态不对:${tm.simState}")
|
||||
return null
|
||||
}
|
||||
val simOperator = tm.simOperator
|
||||
logD("getSimOperator()获取的MCC+MNC为:$simOperator")
|
||||
logD("getOperatorName()方法获取的运营商名称为:${tm.simOperatorName} ")
|
||||
logD("通过getSimOperator()人为判断的运营商名称是: ${getOperatorName(simOperator)}")
|
||||
return simOperator
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取网络运营商
|
||||
*/
|
||||
fun getNetWorkOperator(): String? {
|
||||
val tm = AppUtils.getApp().getSystemService(Context.TELEPHONY_SERVICE) as? TelephonyManager
|
||||
?: return null
|
||||
//用于判断拨号那张卡的运营商
|
||||
val networkOperator = tm.networkOperator
|
||||
logD("getNetWorkOperator() 获取的MCC+MNC为:$networkOperator")
|
||||
logD("getNetWorkOperator() phoneType:${tm.phoneType}")
|
||||
logD("getNetworkOperatorName()方法获取的网络类型名称是: ${tm.networkOperatorName}")
|
||||
logD("通过getNetWorkOperator()人为判断的运营商名称是: ${getOperatorName(networkOperator)}")
|
||||
return tm.networkOperator
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否中国运营商
|
||||
*/
|
||||
private fun isChainOperator(operatorName: String?): Boolean {
|
||||
if (operatorName == null) return false
|
||||
if (operatorName == "CUCC"
|
||||
|| operatorName == "CMCC"
|
||||
|| operatorName == "CTCC"
|
||||
|| operatorName == "CTT"
|
||||
|| operatorName.contains("中国")
|
||||
|| operatorName.contains("中國")
|
||||
) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* 运营商类型
|
||||
*/
|
||||
private fun getOperatorName(simOperator: String?): String? {
|
||||
if (simOperator == null) {
|
||||
return null
|
||||
}
|
||||
return when (simOperator) {
|
||||
"46001", "46006", "46009" -> {
|
||||
// 联通
|
||||
"CUCC"
|
||||
}
|
||||
|
||||
"46000", "46002", "46004", "46007" -> {
|
||||
// 移动
|
||||
"CMCC"
|
||||
}
|
||||
|
||||
"46003", "46005", "46011" -> {
|
||||
// 电信
|
||||
"CTCC"
|
||||
}
|
||||
|
||||
"46020" -> {
|
||||
// 铁通
|
||||
"CTT"
|
||||
}
|
||||
|
||||
else -> {
|
||||
"OHTER"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user