AggH5Tools.java
9.0 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
package com.agg.h5game.tools;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class AggH5Tools {
// 获取Manifest.xml 中MetaData属性
public static String getMetaData(Context ctx, String key) {
try {
ApplicationInfo appInfo = ctx.getPackageManager()
.getApplicationInfo(ctx.getPackageName(),
PackageManager.GET_META_DATA);
if (appInfo != null && appInfo.metaData != null
&& appInfo.metaData.containsKey(key)) {
return "" + appInfo.metaData.get(key);
} else {
AggH5Log.e("TFSDK", "The meta-data key is not exists." + key);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/***
* 获取assets目录下文件内容
*
* @param context
* @param assetsFile
* @return
*/
public static String getAssetConfigs(Context context, String assetsFile) {
InputStreamReader reader = null;
BufferedReader br = null;
try {
reader = new InputStreamReader(context.getAssets().open(assetsFile));
br = new BufferedReader(reader);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if (reader != null) {
try {
reader.close();
reader = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
public static String getAssetsPropertiesContext(Context context, String fileName, String valueName) {
String value = "";
Properties properties = new Properties();
try {
properties.load(context.getAssets()
.open(fileName));
value = properties.getProperty(valueName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return value;
}
/**
* 获取assets目录下面指定的.properties文件内容
*
* @param context
* @param assetsPropertiesFile
* @return
*/
public static Map<String, String> getAssetPropConfig(Context context,
String assetsPropertiesFile) {
try {
Properties pro = new Properties();
pro.load(context.getAssets().open(assetsPropertiesFile));
Map<String, String> result = new HashMap<String, String>();
for (Map.Entry<Object, Object> entry : pro.entrySet()) {
String keyStr = entry.getKey().toString().trim();
String keyVal = entry.getValue().toString().trim();
if (!result.containsKey(keyStr)) {
result.put(keyStr, keyVal);
}
}
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String getLogicChannel(Context context, String prefix) {
ApplicationInfo appInfo = context.getApplicationInfo();
String sourceDir = appInfo.sourceDir;
String targetEntryName = findTargetEntryName(sourceDir, prefix);
if (!TextUtils.isEmpty(targetEntryName)) {
// 去掉 prefix_
String replaceStr = prefix + "_";
return targetEntryName.replace(replaceStr, "");
}
return "1";
}
private static String findTargetEntryName(String sourceDir, String prefix) {
String key = "META-INF/" + prefix;
try (ZipFile zip = new ZipFile(sourceDir)) {
Enumeration<? extends ZipEntry> entries = zip.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
String entryName = entry.getName();
if (entryName.startsWith(key)) {
return entryName;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
* 是否第一次进入APP
*
* @param context
* @return
*/
public static boolean firstIn(Context context) {
SharedPreferences shared = context.getSharedPreferences("info", Context.MODE_PRIVATE);
boolean isfer = shared.getBoolean("isfer", true);
SharedPreferences.Editor editor = shared.edit();
if (isfer) {
//第一次进入
editor.putBoolean("isfer", false);
editor.commit();
return true;
} else {
//非第一次进入
return false;
}
}
/**
* 获取时间
*
* @return 返回时间,如20170829
*/
public static String getDay() {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());// 设置日期格式
Date now = new Date();
String day = df.format(now);// 获取当前的日期
return day;
}
/**
* 获取手机型号
*
* @return 手机型号
*/
public static String getSystemModel() {
return android.os.Build.MODEL;
}
public static Map<String, String> common2map(boolean is2h5) {
Map<String, String> datas = new HashMap<String, String>();
/*datas.put("game_id", ConfigInfor.gameId);
datas.put("client_id", ConfigInfor.clientId);
datas.put("dyClientId", ConfigInfor.dyClientId);
datas.put("imei", "0000-000000-0000000000000");
datas.put("oaid", ConfigInfor.oaid);
datas.put("device", AndroidTools.getDeviceInfor());
datas.put("os", "1");
datas.put("version", AndroidTools.getAndroidSDKVersion());
datas.put("sdkVersion", QNConstant.versionName);
datas.put("android_id", AndroidTools.getAndroidID(QNSdkManager.getInstance().getsApplication()));
if (switchWithKey("is_permissAuth").equals("Y")) {
//datas.put("imei", AndroidTools.getAndroidID(QNSdkManager.getInstance().getsApplication()));
datas.put("imei", AndroidTools.getImei(QNSdkManager.getInstance().getsApplication()));
} else {
datas.put("imei", AndroidTools.getAndroidID(QNSdkManager.getInstance().getsApplication()));
}
if (!is2h5 && QNSdkManager.getInstance().isLogin()) {
datas.put("uid", ConfigInfor.getInstance().getCurrentUserKey("uid"));
datas.put("username", ConfigInfor.getInstance().getCurrentUserKey("username"));
datas.put("token", ConfigInfor.getInstance().getCurrentUserKey("token"));
}
if (is2h5) {
datas.put("appKey", ConfigInfor.appKey);
datas.put("payJump", ConfigInfor.payJump);
datas.putAll(ConfigInfor.getInstance().swithConfigs);
}*/
return datas;
}
public static String map2string(Map<String, String> jsondatas) {
JSONObject json = new JSONObject();
for (String key : jsondatas.keySet()) {
String value = jsondatas.get(key);
if (value == null) {
continue;
}
if (value.contains("[") && value.contains("]")) {
try {
json.put(key, new JSONArray(value));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (value.contains("{") && value.contains("}")) {
try {
json.put(key, new JSONObject(value));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
try {
json.put(key, jsondatas.get(key).toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return json.toString();
}
}