/
NetworkUtil.java
142 lines (128 loc) · 5.28 KB
/
NetworkUtil.java
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
/*
* Yalp Store
* Copyright (C) 2018 Sergey Yeriomin <yeriomin@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package com.github.yeriomin.yalpstore;
import android.annotation.TargetApi;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.TrafficStats;
import android.os.Build;
import android.util.Log;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.util.Collections;
import info.guardianproject.netcipher.NetCipher;
import static android.content.Context.CONNECTIVITY_SERVICE;
import static android.net.ConnectivityManager.TYPE_MOBILE;
import static android.net.ConnectivityManager.TYPE_MOBILE_DUN;
import static android.net.ConnectivityManager.TYPE_MOBILE_HIPRI;
import static android.net.ConnectivityManager.TYPE_MOBILE_MMS;
import static android.net.ConnectivityManager.TYPE_MOBILE_SUPL;
import static android.net.ConnectivityManager.TYPE_WIFI;
import static android.net.ConnectivityManager.TYPE_WIMAX;
public class NetworkUtil {
static public HttpURLConnection getHttpURLConnection(String url) throws IOException {
return getHttpURLConnection(new URL(url));
}
static public HttpURLConnection getHttpURLConnection(URL url) throws IOException {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
TrafficStats.setThreadStatsTag(Thread.currentThread().hashCode());
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
return (HttpURLConnection) url.openConnection();
}
return NetCipher.getHttpURLConnection(url, true);
}
static public boolean isNetworkAvailable(Context context) {
NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
static public boolean isVpn(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return isVpnLollipop(context);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
return isVpnHoneycomb();
} else {
return false;
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
static private boolean isVpnLollipop(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) {
return false;
}
for (NetworkInfo networkInfo: cm.getAllNetworkInfo()) {
if (networkInfo.isConnectedOrConnecting() && networkInfo.getType() == ConnectivityManager.TYPE_VPN) {
return true;
}
}
return false;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
static private boolean isVpnHoneycomb() {
try {
for (NetworkInterface ni: Collections.list(NetworkInterface.getNetworkInterfaces())) {
if (ni.isUp() && (ni.getName().startsWith("tun") || ni.getName().startsWith("ppp"))) {
Log.i(NetworkUtil.class.getSimpleName(), "VPN seems to be on: " + ni.getName());
return true;
}
}
} catch (SocketException e) {
// Could not get network interfaces
}
return false;
}
static public boolean isMetered(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
if (null == connectivityManager) {
return true;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
return isActiveNetworkMetered(connectivityManager);
} else {
return connectivityManager.isActiveNetworkMetered();
}
}
static private boolean isActiveNetworkMetered(ConnectivityManager connectivityManager) {
final NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info == null) {
// err on side of caution
return true;
}
final int type = info.getType();
switch (type) {
case TYPE_MOBILE:
case TYPE_MOBILE_DUN:
case TYPE_MOBILE_HIPRI:
case TYPE_MOBILE_MMS:
case TYPE_MOBILE_SUPL:
case TYPE_WIMAX:
return true;
case TYPE_WIFI:
return false;
default:
// err on side of caution
return true;
}
}
}