package com.mm.android.deviceaddmodule.mobilecommon.AppConsume; import android.app.Application; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.telephony.TelephonyManager; import android.text.TextUtils; import com.mm.android.deviceaddmodule.mobilecommon.utils.LogUtil; import com.mm.android.deviceaddmodule.mobilecommon.utils.PreferencesHelper; import java.io.File; /** * 业务必è¦å±žæ€§ * * Appé…置文件必须è¦é…置的 推é€ID * {@code meta_data4project} Appé…置文件必须è¦é…置的 项目åç§° {@code meta_data4Type} * Appé…置文件必须è¦é…置的 åè®®å‘èµ·æ–¹ */ public final class EnvironmentConfig { public static final String meta_data4Type = "meta-data4type"; public static final String meta_data4AppId = "meta-data4appid"; public static final String meta_isHttps = "meta-ishttps"; /** * 1.clientType:客户端类型(必填),"phone"-手机,"web"-æµè§ˆå™¨ï¼Œ"box"-ç›’å。 * {@link } 环境é…置属性。 */ private String clientType; /** * 2.clientMac:客户端MAC地å€ï¼ˆå¿…å¡«ï¼‰ï¼Œç”¨äºŽå”¯ä¸€æ ‡è¯†è¿™ä¸ªå®¢æˆ·ç«¯ã€‚ */ private String clientMac; /** * æœåŠ¡ç«¯åœ°å€ */ private String host; /** * æ˜¯å¦æ˜¯èµ°httpsåè®® */ private boolean isHttps; /** * ç¼“å˜æ–‡ä»¶å */ private String userAgent = "volley/0"; /** * 缓å˜è·¯å¾„ */ private File cacheDir; /** * 应用ID */ private String appId; /** * Application */ private Context application; /** * 版本å */ private String versionName; private String language = null; public static Context mContext;// applicationContext public EnvironmentConfig(Builder b) { this.clientMac = b.clientMac; this.clientType = b.clientType; userAgent = b.userAgent; cacheDir = b.cacheDir; appId = b.appId; host = b.host; this.isHttps = b.isHttps; application = b.applicationContext; versionName = b.versionName; language = b.language; } public String getHost() { return host; } public String getClientType() { return clientType; } public void setHost(String host) { this.host = host; } public String getClientMacAddress() { return clientMac; } public Context getContext() { return application; } public String getVersionName() { return versionName; } public String getLanguage() { return language; } public static class Builder { private String clientType; private String clientMac; private String project; private String userAgent; private File cacheDir; private String appId; private String host; private boolean isHttps; private Context applicationContext; private String versionName; private String language = null; public Builder setContext(Context context) throws Exception { if (!(context instanceof Application)) { throw new Exception("context must instance application"); } applicationContext = context; mContext = applicationContext.getApplicationContext(); return this; } public Builder setClientType(String clientType) { this.clientType = clientType; return this; } private void setVersionName(Context context) { PackageManager pm = context.getPackageManager(); PackageInfo pi = null; try { pi = pm.getPackageInfo(context.getPackageName(), 0); versionName = pi.versionName; } catch (NameNotFoundException e) { e.printStackTrace(); } } private void setClientMac(Context context) { try { // FIXME:Mac地å€å¯èƒ½ä¸ºç©º IMEI 未识别信æ¯ï¼Œå¦‚果未å‘çŽ°ï¼Œåˆ™åŠŸèƒ½ä¸æ£å¸¸ä½¿ç”¨ TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); String imei = telephonyManager.getDeviceId(); LogUtil.debugLog("lechange", "imei : " + imei); if (imei == null) { WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo()); String mac = info != null ? info.getMacAddress() : ""; LogUtil.debugLog("lechange", "mac : " + mac); if (mac != null) { mac = mac.replace(":", ""); } else { mac = ""; } clientMac = mac; } else { clientMac = imei; } LogUtil.debugLog("lechange", "clientMac : " + clientMac); } catch (Exception e) { clientMac = ""; } } public Builder setProject(String project) { this.project = project; return this; } private void setCacheFile(Context context) { cacheDir = context.getCacheDir(); userAgent = "volley/0"; try { String packageName = context.getPackageName(); PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); userAgent = packageName + "/" + info.versionCode; } catch (NameNotFoundException e) { } } public Builder setHost(String host) { this.host = host; String historyHost = PreferencesHelper.getInstance(applicationContext).getString(PreferencesConfig.HOST_HELP); if (!TextUtils.isEmpty(historyHost)) {// 如果历å²è®°å½•Host 䏿˜¯nil // 则设置历å²Host,用于测试æœåС噍 this.host = historyHost; } return this; } private void setDefaultHost(String host) { this.host = host; String historyHost = PreferencesHelper.getInstance(applicationContext).getString(PreferencesConfig.HOST_HELP); if (!TextUtils.isEmpty(historyHost)) {// 如果历å²è®°å½•Host 䏿˜¯nil // 则设置历å²Host,用于测试æœåС噍 this.host = historyHost; } } public Builder setLanguage(String language) { this.language = language; return this; } private void setLanguage(Context context) { this.language = context.getResources().getConfiguration().locale.getLanguage(); } public EnvironmentConfig build() { if (versionName == null || versionName.equals("")) { setVersionName(applicationContext); } if (cacheDir == null) { setCacheFile(applicationContext); } if (clientMac == null || clientMac.equals("")) { setClientMac(applicationContext); } if (language == null || language.equals("")) { setLanguage(applicationContext); } try { ApplicationInfo appInfo = applicationContext.getPackageManager().getApplicationInfo(applicationContext.getPackageName(), PackageManager.GET_META_DATA); if (clientType == null || clientType.equals("")) { clientType = appInfo.metaData.getString(meta_data4Type); } isHttps = appInfo.metaData.getBoolean(meta_isHttps); } catch (NameNotFoundException e) { e.printStackTrace(); } return new EnvironmentConfig(this); } } }