Tools.java
10.4 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package com.glsx.pushsdk.tools;
import static com.glsx.pushsdk.tools.ISOUtils.TAG;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;
import androidx.core.app.ActivityCompat;
import com.blankj.utilcode.util.LogUtils;
import com.glsx.pushsdk.common.PushCommon;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@SuppressWarnings("deprecation")
public class Tools {
@SuppressLint("HardwareIds")
public static String getImei(Context context) {
if (context == null) {
return "";
}
// Log.d(TAG, "getImeigetImei: 获取Android_ID????");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
return Settings.System.getString(context.getContentResolver(), Settings.System.ANDROID_ID);
} else {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (null == telephonyManager) {
return "";
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
LogUtils.dTag("", " Manifest.permission.READ_PHONE_STATE)= false");
return "";
}
return telephonyManager.getDeviceId();
} else {
return telephonyManager.getDeviceId();
}
}
}
/**
* 通过ManiFest配置的参数获取appkey
*
* @param context
* @return
*/
public static String getAppKey(Context context) {
if (context == null) {
return "";
}
try {
ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(),
PackageManager.GET_META_DATA);
if (null != appInfo.metaData) {
return appInfo.metaData.getString(PushCommon.isRelease_V ? "GLPUSH_APPKEY" : "GLPUSH_APPKEY_TEST");
}
} catch (Exception e) {
// TODO: handle exception
}
return "";
}
/**
* 通过ManiFest配置的参数获取appkey
*
* @param context
* @return
*/
public static String getChannelSecret(Context context) {
if (context == null) {
return "";
}
try {
ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(
context.getPackageName(), PackageManager.GET_META_DATA);
if (null != appInfo.metaData) {
return appInfo.metaData.getString(PushCommon.isRelease_V ? "GLPUSH_CHANNEL_SECRET" : "GLPUSH_CHANNEL_SECRET_TEST");
}
} catch (Exception e) {
// TODO: handle exception
}
return "";
}
/**
* 获取当前版本
*
* @param context
* @return
*/
public static String getVersonCode(Context context) {
if (null != context && getPackageInfo(context) != null) {
return getPackageInfo(context).versionName;
}
return "";
}
/**
* 获取AppName
*
* @param context
* @return
*/
public static String getAppName(Context context) {
if (null != context && getPackageInfo(context) != null) {
PackageManager manager = context.getPackageManager();
return getPackageInfo(context).applicationInfo.loadLabel(manager).toString();
}
return "";
}
/**
* 获取AppIcon
*
* @param context
* @return
*/
public static Drawable getAppIcon(Context context) {
if (null != context && getPackageInfo(context) != null) {
PackageManager manager = context.getPackageManager();
return getPackageInfo(context).applicationInfo.loadIcon(manager);
}
return null;
}
/**
* 获取PackageInfo 详细
*
* @param context
* @return
*/
private static PackageInfo getPackageInfo(Context context) {
if (null != context) {
try {
PackageManager manager = context.getPackageManager();
return manager.getPackageInfo(context.getPackageName(), 0);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* Drawable 转Bitmap
*
* @param drawable
* @return
*/
public static Bitmap Drawable2Bitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
} else if (drawable instanceof NinePatchDrawable) {
Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
} else {
return null;
}
}
/**
* 获取当前时间
*
* @return
*/
public static String getCurrentTime() {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
String strHour = "";
String strMinute = "";
if (hour < 10) {
strHour = "0" + hour;
} else {
strHour = "" + hour;
}
if (minute < 10) {
strMinute = "0" + minute;
} else {
strMinute = "" + minute;
}
return strHour + ":" + strMinute;
}
/**
* 保存用户的userFlag
*
* @param ctx 上下文
* @param userFlag 用户userId
* 回调
*/
public static void saveUserFlag(Context ctx, String userFlag) {
if (ctx == null) {
return;
}
SharedPreferences sharedPreferences = ctx.getSharedPreferences("GL_PUSH_LOGIN", 0);
Editor editor = sharedPreferences.edit();// 获取编辑器
editor.putString("userflag", userFlag);
editor.apply();
}
/**
* 获取用户的userFlag
*
* @param ctx
* @return
*/
public static String getUserFlag(Context ctx) {
SharedPreferences sharedPreferences = ctx.getSharedPreferences("GL_PUSH_LOGIN", 0);
return sharedPreferences.getString("userflag", "");
}
/**
* 保存应用版本号到内存中
*
* @param ctx
* @param version
*/
public static void saveAppVersion(Context ctx, String version) {
if (null != ctx) {
SharedPreferences sharedPreferences = ctx.getSharedPreferences("GL_PUSH_LOGIN", 0);
Editor editor = sharedPreferences.edit();// 获取编辑器
editor.putString("appVersion", version);
editor.apply();
}
}
/**
* 获取用户的userFlag
*
* @param ctx
* @return
*/
public static String getAppVersion(Context ctx) {
SharedPreferences sharedPreferences = ctx.getSharedPreferences("GL_PUSH_LOGIN", 0);
return sharedPreferences.getString("appVersion", "");
}
/**
* 通过反射获取到资源文件的R.id
*
* @param paramContext
* @param paramString1
* @param paramString2
* @return
*/
public static int getId(Context paramContext, String paramString1,
String paramString2) {
try {
Class localClass = Class.forName(paramContext.getPackageName()
+ ".R$" + paramString1);
Field localField = localClass.getField(paramString2);
int i = Integer.parseInt(localField.get(localField.getName())
.toString());
LogUtils.eTag("getIdByReflection error", "通过返回获取到icon的id为:" + i);
return i;
} catch (Exception localException) {
LogUtils.eTag("getIdByReflection error", localException.getMessage());
}
return 0;
}
/**
* 判断程序是否在前台运行
*
* @param context
* @return
*/
public static boolean isTopActivity(Context context) {
String packageName = context.getPackageName();
// LogUtils.iTag("ylg", "应用包名 = " + context.getPackageName());
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
if (tasksInfo.size() > 0) {
// LogUtils.iTag("ylg", "最顶层应用包名 = " + tasksInfo.get(0).topActivity.getPackageName());
//应用程序位于堆栈的顶层
if (packageName.equals(tasksInfo.get(0).topActivity.getPackageName())) {
// LogUtils.iTag("ylg", "前台 = " + packageName);
return true;
}
}
return false;
}
/**
* @param startTime System.currentTimeMillis()
* @param endTime System.currentTimeMillis()
* @return 返回两者时间差值
*/
public static long getCountTime(long startTime, long endTime) {
//开始时间
Date start = new Date(startTime);
//结束时间
Date end = new Date(endTime);
//时间差
long diff = end.getTime() - start.getTime();
return diff;
}
}