wxr
2022-11-24 2af932533ef851bf983385244e9912976dbd4daa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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);
        }
    }
}