Skip to content

Commit 2d9bfd6

Browse files
committed
Merge branch 'master' of github.com:Blankj/AndroidUtilCode
2 parents c228f53 + f6ceb0f commit 2d9bfd6

File tree

6 files changed

+100
-3
lines changed

6 files changed

+100
-3
lines changed

lib/utilcode/src/main/java/com/blankj/utilcode/util/AppUtils.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,38 @@ public static int getAppIconId(final String packageName) {
385385
}
386386
}
387387

388+
389+
/**
390+
* Return true if this is the first ever time that the application is installed on the device.
391+
*
392+
* @return true if this is the first ever time that the application is installed on the device.
393+
*/
394+
public static boolean isFirstTimeInstall(){
395+
try {
396+
Long firstInstallTime = Utils.getApp().getPackageManager().getPackageInfo(this.getAppPackageName(), 0).firstInstallTime;
397+
Long lastUpdateTime = Utils.getApp().getPackageManager().getPackageInfo(this.getAppPackageName(), 0).lastUpdateTime;
398+
return firstInstallTime == lastUpdateTime;
399+
} catch (Exception e) {
400+
return false;
401+
}
402+
}
403+
404+
/**
405+
* Return true if app was previously installed and this one is an update/upgrade to that one, returns false if this is a fresh installation and not an update/upgrade.
406+
*
407+
* @return true if app was previously installed and this one is an update/upgrade to that one, returns false if this is a fresh installation and not an update/upgrade.
408+
*/
409+
public static boolean isAppUpgraded(){
410+
try {
411+
Long firstInstallTime = Utils.getApp().getPackageManager().getPackageInfo(this.getAppPackageName(), 0).firstInstallTime;
412+
Long lastUpdateTime = Utils.getApp().getPackageManager().getPackageInfo(this.getAppPackageName(), 0).lastUpdateTime;
413+
return firstInstallTime != lastUpdateTime;
414+
} catch (Exception e) {
415+
return false;
416+
}
417+
}
418+
419+
388420
/**
389421
* Return the application's package name.
390422
*

lib/utilcode/src/main/java/com/blankj/utilcode/util/JsonUtils.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@ private JsonUtils() {
2626
throw new UnsupportedOperationException("u can't instantiate me...");
2727
}
2828

29+
30+
/**
31+
* Checks if a given input is a JSONObject.
32+
*
33+
* @param input Anything.
34+
* @return true if it is a JSONObject.
35+
*/
36+
public static <T> boolean isJSONObject(final T input) {
37+
return input instanceof JSONObject;
38+
}
39+
40+
/**
41+
* Checks if a given input is a JSONArray
42+
*
43+
* @param input Anything.
44+
* @return true if it is a JSONArray.
45+
*/
46+
public static <T> boolean isJSONArray(final T input) {
47+
return input instanceof JSONArray;
48+
}
49+
2950
public static boolean getBoolean(final JSONObject jsonObject,
3051
final String key) {
3152
return getBoolean(jsonObject, key, false);

lib/utilcode/src/main/java/com/blankj/utilcode/util/KeyboardUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,9 @@ public static void unregisterSoftInputChangedListener(@NonNull final Window wind
260260
Object tag = contentView.getTag(TAG_ON_GLOBAL_LAYOUT_LISTENER);
261261
if (tag instanceof OnGlobalLayoutListener) {
262262
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
263-
contentView.getViewTreeObserver()
264-
.removeOnGlobalLayoutListener((OnGlobalLayoutListener) tag);
263+
contentView.getViewTreeObserver().removeOnGlobalLayoutListener((OnGlobalLayoutListener) tag);
264+
//这里会发生内存泄漏 如果不设置为null
265+
contentView.setTag(TAG_ON_GLOBAL_LAYOUT_LISTENER, null);
265266
}
266267
}
267268
}

lib/utilcode/src/main/java/com/blankj/utilcode/util/MessengerUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,15 +322,17 @@ public int onStartCommand(Intent intent, int flags, int startId) {
322322
}
323323

324324
private void sendMsg2Client(final Message msg) {
325+
final Message obtain = Message.obtain(msg); //Copy the original
325326
for (Messenger client : mClientMap.values()) {
326327
try {
327328
if (client != null) {
328-
client.send(msg);
329+
client.send(Message.obtain(obtain));
329330
}
330331
} catch (RemoteException e) {
331332
e.printStackTrace();
332333
}
333334
}
335+
obtain.recycle(); //Recycled copy
334336
}
335337

336338
private void consumeServerProcessCallback(final Message msg) {

lib/utilcode/src/main/java/com/blankj/utilcode/util/NetworkUtils.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,34 @@ public static boolean getMobileDataEnabled() {
262262
}
263263
return false;
264264
}
265+
266+
/**
267+
* Returns true if device is connecting to the internet via a proxy, works for both Wi-Fi and Mobile Data.
268+
*
269+
* @return true if using proxy to connect to the internet.
270+
*/
271+
public static boolean isBehindProxy(){
272+
return !(System.getProperty("http.proxyHost") == null || System.getProperty("http.proxyPort") == null);
273+
}
274+
275+
/**
276+
* Returns true if device is connecting to the internet via a VPN.
277+
*
278+
* @return true if using VPN to conncet to the internet.
279+
*/
280+
public static boolean isUsingVPN(){
281+
ConnectivityManager cm = (ConnectivityManager) com.blankj.utilcode.util.Utils.getApp().getSystemService(Context.CONNECTIVITY_SERVICE);
282+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
283+
return cm.getNetworkInfo(ConnectivityManager.TYPE_VPN).isConnectedOrConnecting()
284+
} else {
285+
return cm.getNetworkInfo(NetworkCapabilities.TRANSPORT_VPN).isConnectedOrConnecting()
286+
}
287+
}
288+
289+
290+
291+
292+
265293

266294
/**
267295
* Return whether using mobile data.

lib/utilcode/src/main/java/com/blankj/utilcode/util/TimeUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ protected Map<String, SimpleDateFormat> initialValue() {
3737
private static SimpleDateFormat getDefaultFormat() {
3838
return getSafeDateFormat("yyyy-MM-dd HH:mm:ss");
3939
}
40+
/**
41+
* Checks whether the device is using Network Provided Time or not.
42+
* Useful in situations where you want to verify that the device has a correct time set, to avoid fraud, or if you want to prevent the user from messing with the time and abusing your "one-time" and "expiring" features.
43+
* @return {@code true}: yes<br>{@code false}: no
44+
*/
45+
public static boolean isUsingNetworkProvidedTime() {
46+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
47+
return Settings.Global.getInt(Utils.getApp().getContentResolver(), Settings.Global.AUTO_TIME, 0) == 1;
48+
} else {
49+
return android.provider.Settings.System.getInt(Utils.getApp().getContentResolver(), android.provider.Settings.System.AUTO_TIME, 0) == 1;
50+
}
51+
}
52+
4053

4154
@SuppressLint("SimpleDateFormat")
4255
public static SimpleDateFormat getSafeDateFormat(String pattern) {

0 commit comments

Comments
 (0)