2015-10-25 09:29:49 +00:00
|
|
|
package eu.faircode.netguard;
|
|
|
|
|
2015-11-03 17:57:29 +00:00
|
|
|
/*
|
|
|
|
This file is part of NetGuard.
|
|
|
|
|
|
|
|
NetGuard 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
NetGuard 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 NetGuard. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Copyright 2015 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
|
2015-11-20 09:10:22 +00:00
|
|
|
import android.app.ApplicationErrorReport;
|
2015-10-25 09:29:49 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2015-11-15 15:58:15 +00:00
|
|
|
import android.content.SharedPreferences;
|
2015-11-09 06:01:25 +00:00
|
|
|
import android.content.pm.ApplicationInfo;
|
2015-10-25 09:29:49 +00:00
|
|
|
import android.content.pm.PackageInfo;
|
|
|
|
import android.content.pm.PackageManager;
|
2015-10-25 16:12:25 +00:00
|
|
|
import android.net.ConnectivityManager;
|
2015-11-14 08:50:45 +00:00
|
|
|
import android.net.Network;
|
2015-10-25 16:12:25 +00:00
|
|
|
import android.net.NetworkInfo;
|
2015-10-29 14:01:41 +00:00
|
|
|
import android.net.Uri;
|
2015-11-27 12:36:56 +00:00
|
|
|
import android.net.VpnService;
|
2015-10-29 14:01:41 +00:00
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Build;
|
2015-10-25 09:29:49 +00:00
|
|
|
import android.os.Bundle;
|
2015-10-29 07:48:28 +00:00
|
|
|
import android.os.PowerManager;
|
2015-11-15 15:58:15 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2015-11-01 10:08:33 +00:00
|
|
|
import android.telephony.TelephonyManager;
|
2015-10-25 09:29:49 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2015-10-29 14:01:41 +00:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
2015-11-26 19:34:03 +00:00
|
|
|
import java.io.OutputStream;
|
2015-11-20 09:10:22 +00:00
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.io.StringWriter;
|
2015-10-31 08:11:23 +00:00
|
|
|
import java.security.MessageDigest;
|
2015-11-15 15:58:15 +00:00
|
|
|
import java.util.Map;
|
2015-10-25 09:29:49 +00:00
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
public class Util {
|
2015-11-20 10:34:23 +00:00
|
|
|
private static final String TAG = "NetGuard.Util";
|
|
|
|
|
2015-10-25 09:29:49 +00:00
|
|
|
public static String getSelfVersionName(Context context) {
|
|
|
|
try {
|
|
|
|
PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
|
|
|
|
return pInfo.versionName;
|
|
|
|
} catch (PackageManager.NameNotFoundException ex) {
|
|
|
|
return ex.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-18 07:36:53 +00:00
|
|
|
public static int getSelfVersionCode(Context context) {
|
|
|
|
try {
|
|
|
|
PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
|
|
|
|
return pInfo.versionCode;
|
|
|
|
} catch (PackageManager.NameNotFoundException ex) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-25 20:09:00 +00:00
|
|
|
public static boolean isConnected(Context context) {
|
|
|
|
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
NetworkInfo ni = cm.getActiveNetworkInfo();
|
|
|
|
return (ni != null && ni.isConnected());
|
2015-11-01 10:08:33 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 10:05:10 +00:00
|
|
|
public static boolean isWifiActive(Context context) {
|
2015-10-25 16:12:25 +00:00
|
|
|
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
NetworkInfo ni = cm.getActiveNetworkInfo();
|
2015-11-13 10:05:10 +00:00
|
|
|
return (ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI);
|
2015-10-25 16:12:25 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 07:07:15 +00:00
|
|
|
public static boolean isMeteredNetwork(Context context) {
|
2015-11-04 12:04:27 +00:00
|
|
|
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
2015-11-08 22:23:53 +00:00
|
|
|
return cm.isActiveNetworkMetered();
|
2015-11-04 12:04:27 +00:00
|
|
|
}
|
|
|
|
|
2015-11-25 20:09:00 +00:00
|
|
|
public static boolean isRoaming(Context context) {
|
|
|
|
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
|
|
|
return tm.isNetworkRoaming();
|
|
|
|
}
|
|
|
|
|
2015-10-29 07:48:28 +00:00
|
|
|
public static boolean isInteractive(Context context) {
|
|
|
|
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
|
|
|
return pm.isInteractive();
|
|
|
|
}
|
|
|
|
|
2015-10-30 11:23:05 +00:00
|
|
|
public static boolean isPackageInstalled(String packageName, Context context) {
|
|
|
|
try {
|
2015-11-03 09:42:55 +00:00
|
|
|
context.getPackageManager().getPackageInfo(packageName, 0);
|
|
|
|
return true;
|
2015-10-30 11:23:05 +00:00
|
|
|
} catch (PackageManager.NameNotFoundException ignored) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 06:01:25 +00:00
|
|
|
public static boolean isDebuggable(Context context) {
|
|
|
|
return ((context.getApplicationContext().getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0);
|
|
|
|
}
|
|
|
|
|
2015-11-20 10:34:23 +00:00
|
|
|
public static boolean hasValidFingerprint(Context context) {
|
2015-10-31 08:11:23 +00:00
|
|
|
try {
|
|
|
|
PackageManager pm = context.getPackageManager();
|
|
|
|
String pkg = context.getPackageName();
|
|
|
|
PackageInfo info = pm.getPackageInfo(pkg, PackageManager.GET_SIGNATURES);
|
|
|
|
byte[] cert = info.signatures[0].toByteArray();
|
|
|
|
MessageDigest digest = MessageDigest.getInstance("SHA1");
|
|
|
|
byte[] bytes = digest.digest(cert);
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
for (int i = 0; i < bytes.length; ++i)
|
|
|
|
sb.append(Integer.toString(bytes[i] & 0xff, 16).toLowerCase());
|
|
|
|
String calculated = sb.toString();
|
|
|
|
String expected = context.getString(R.string.fingerprint);
|
|
|
|
return calculated.equals(expected);
|
|
|
|
} catch (Throwable ex) {
|
2015-11-20 10:34:23 +00:00
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
2015-10-31 08:11:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-20 10:34:23 +00:00
|
|
|
public static void logExtras(Intent intent) {
|
2015-11-02 12:44:22 +00:00
|
|
|
if (intent != null)
|
2015-11-20 10:34:23 +00:00
|
|
|
logBundle(intent.getExtras());
|
2015-10-25 09:29:49 +00:00
|
|
|
}
|
|
|
|
|
2015-11-20 10:34:23 +00:00
|
|
|
public static void logBundle(Bundle data) {
|
2015-10-25 09:29:49 +00:00
|
|
|
if (data != null) {
|
|
|
|
Set<String> keys = data.keySet();
|
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
2015-11-08 09:14:56 +00:00
|
|
|
for (String key : keys) {
|
|
|
|
Object value = data.get(key);
|
2015-11-08 18:31:55 +00:00
|
|
|
stringBuilder.append(key)
|
|
|
|
.append("=")
|
|
|
|
.append(value)
|
2015-11-09 13:24:44 +00:00
|
|
|
.append(value == null ? "" : " (" + value.getClass().getSimpleName() + ")")
|
2015-11-08 18:31:55 +00:00
|
|
|
.append("\r\n");
|
2015-11-08 09:14:56 +00:00
|
|
|
}
|
2015-11-20 10:34:23 +00:00
|
|
|
Log.d(TAG, stringBuilder.toString());
|
2015-10-25 09:29:49 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-29 14:01:41 +00:00
|
|
|
|
2015-11-20 10:36:39 +00:00
|
|
|
public static void sendCrashReport(Throwable ex, final Context context) {
|
2015-11-23 09:58:22 +00:00
|
|
|
try {
|
|
|
|
ApplicationErrorReport report = new ApplicationErrorReport();
|
|
|
|
report.packageName = report.processName = context.getPackageName();
|
|
|
|
report.time = System.currentTimeMillis();
|
|
|
|
report.type = ApplicationErrorReport.TYPE_CRASH;
|
|
|
|
report.systemApp = false;
|
|
|
|
|
|
|
|
ApplicationErrorReport.CrashInfo crash = new ApplicationErrorReport.CrashInfo();
|
|
|
|
crash.exceptionClassName = ex.getClass().getSimpleName();
|
|
|
|
crash.exceptionMessage = ex.getMessage();
|
|
|
|
|
|
|
|
StringWriter writer = new StringWriter();
|
|
|
|
PrintWriter printer = new PrintWriter(writer);
|
|
|
|
ex.printStackTrace(printer);
|
|
|
|
|
|
|
|
crash.stackTrace = writer.toString();
|
|
|
|
|
|
|
|
StackTraceElement stack = ex.getStackTrace()[0];
|
|
|
|
crash.throwClassName = stack.getClassName();
|
|
|
|
crash.throwFileName = stack.getFileName();
|
|
|
|
crash.throwLineNumber = stack.getLineNumber();
|
|
|
|
crash.throwMethodName = stack.getMethodName();
|
|
|
|
|
|
|
|
report.crashInfo = crash;
|
|
|
|
|
|
|
|
final Intent bug = new Intent(Intent.ACTION_APP_ERROR);
|
|
|
|
bug.putExtra(Intent.EXTRA_BUG_REPORT, report);
|
|
|
|
bug.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
2015-11-26 19:34:03 +00:00
|
|
|
if (bug.resolveActivity(context.getPackageManager()) != null)
|
2015-11-23 09:58:22 +00:00
|
|
|
context.startActivity(bug);
|
|
|
|
} catch (Throwable exex) {
|
|
|
|
Log.e(TAG, exex.toString() + "\n" + Log.getStackTraceString(exex));
|
|
|
|
}
|
2015-11-20 09:10:22 +00:00
|
|
|
}
|
|
|
|
|
2015-11-26 19:34:03 +00:00
|
|
|
public static void sendLogcat(final Uri uri, final Context context) {
|
2015-10-29 14:01:41 +00:00
|
|
|
AsyncTask task = new AsyncTask<Object, Object, Intent>() {
|
|
|
|
@Override
|
|
|
|
protected Intent doInBackground(Object... objects) {
|
2015-11-26 19:34:03 +00:00
|
|
|
// Get device info
|
2015-10-29 14:01:41 +00:00
|
|
|
StringBuilder sb = new StringBuilder();
|
2015-11-26 19:34:03 +00:00
|
|
|
String version = getSelfVersionName(context);
|
2015-11-27 12:55:09 +00:00
|
|
|
sb.append(String.format("NetGuard: %s/%d\r\n", version, getSelfVersionCode(context)));
|
2015-11-14 08:50:45 +00:00
|
|
|
sb.append(String.format("Android: %s (SDK %d)\r\n", Build.VERSION.RELEASE, Build.VERSION.SDK_INT));
|
|
|
|
sb.append("\r\n");
|
|
|
|
sb.append(String.format("Brand: %s\r\n", Build.BRAND));
|
|
|
|
sb.append(String.format("Manufacturer: %s\r\n", Build.MANUFACTURER));
|
|
|
|
sb.append(String.format("Model: %s\r\n", Build.MODEL));
|
|
|
|
sb.append(String.format("Product: %s\r\n", Build.PRODUCT));
|
|
|
|
sb.append(String.format("Device: %s\r\n", Build.DEVICE));
|
|
|
|
sb.append(String.format("Host: %s\r\n", Build.HOST));
|
|
|
|
sb.append(String.format("Display: %s\r\n", Build.DISPLAY));
|
|
|
|
sb.append(String.format("Id: %s\r\n", Build.ID));
|
2015-11-20 10:34:23 +00:00
|
|
|
sb.append(String.format("Fingerprint: %b\r\n", hasValidFingerprint(context)));
|
2015-11-14 08:50:45 +00:00
|
|
|
sb.append(String.format("VPN dialogs: %b\r\n", isPackageInstalled("com.android.vpndialogs", context)));
|
2015-11-27 12:36:56 +00:00
|
|
|
try {
|
|
|
|
sb.append(String.format("Prepared: %b\r\n", VpnService.prepare(context) == null));
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
sb.append("Prepared: ").append((ex.toString())).append("\r\n").append(Log.getStackTraceString(ex));
|
|
|
|
}
|
2015-11-15 16:13:25 +00:00
|
|
|
sb.append(String.format("Interactive: %b\r\n", isInteractive(context)));
|
|
|
|
sb.append(String.format("WiFi: %b\r\n", isWifiActive(context)));
|
|
|
|
sb.append(String.format("Metered: %b\r\n", isMeteredNetwork(context)));
|
2015-11-14 08:50:45 +00:00
|
|
|
|
2015-11-26 19:34:03 +00:00
|
|
|
// Get connectivity info
|
2015-11-14 08:50:45 +00:00
|
|
|
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
2015-11-14 09:49:15 +00:00
|
|
|
for (Network network : cm.getAllNetworks()) {
|
|
|
|
NetworkInfo ni = cm.getNetworkInfo(network);
|
2015-11-17 20:05:55 +00:00
|
|
|
if (ni != null)
|
|
|
|
sb.append("Network: ")
|
|
|
|
.append(ni.getTypeName())
|
|
|
|
.append("/")
|
|
|
|
.append(ni.getSubtypeName())
|
|
|
|
.append(" ")
|
|
|
|
.append(ni.getDetailedState())
|
|
|
|
.append(ni.isRoaming() ? " R" : "")
|
|
|
|
.append("\r\n");
|
2015-11-14 09:49:15 +00:00
|
|
|
}
|
2015-11-14 08:50:45 +00:00
|
|
|
|
2015-11-26 19:34:03 +00:00
|
|
|
// Get settings
|
2015-11-15 15:58:15 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
Map<String, ?> all = prefs.getAll();
|
|
|
|
for (String key : all.keySet())
|
|
|
|
sb.append("Setting: ").append(key).append('=').append(all.get(key)).append("\r\n");
|
|
|
|
|
2015-11-26 19:34:03 +00:00
|
|
|
// Write logcat
|
|
|
|
OutputStream out = null;
|
2015-10-29 14:01:41 +00:00
|
|
|
try {
|
2015-11-26 19:34:03 +00:00
|
|
|
Log.i(TAG, "Writing logcat URI=" + uri);
|
|
|
|
out = context.getContentResolver().openOutputStream(uri);
|
|
|
|
out.write(getLogcat().toString().getBytes());
|
2015-10-29 14:01:41 +00:00
|
|
|
} catch (Throwable ex) {
|
2015-11-20 10:34:23 +00:00
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
2015-11-26 19:34:03 +00:00
|
|
|
sb.append(ex.toString()).append("\r\n").append(Log.getStackTraceString(ex)).append("\r\n");
|
2015-10-29 14:01:41 +00:00
|
|
|
} finally {
|
2015-11-26 19:34:03 +00:00
|
|
|
if (out != null)
|
2015-10-29 14:01:41 +00:00
|
|
|
try {
|
2015-11-26 19:34:03 +00:00
|
|
|
out.close();
|
2015-10-29 14:01:41 +00:00
|
|
|
} catch (IOException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-26 19:34:03 +00:00
|
|
|
// Finalize message
|
|
|
|
sb.append("\r\n");
|
|
|
|
sb.append("Please describe your problem:\r\n");
|
|
|
|
sb.append("\r\n");
|
2015-10-29 14:01:41 +00:00
|
|
|
|
2015-11-26 19:34:03 +00:00
|
|
|
// Build intent
|
|
|
|
Intent sendEmail = new Intent(Intent.ACTION_SEND);
|
|
|
|
sendEmail.setType("message/rfc822");
|
|
|
|
sendEmail.putExtra(Intent.EXTRA_EMAIL, new String[]{"marcel+netguard@faircode.eu"});
|
|
|
|
sendEmail.putExtra(Intent.EXTRA_SUBJECT, "NetGuard " + version + " logcat");
|
|
|
|
sendEmail.putExtra(Intent.EXTRA_TEXT, sb.toString());
|
|
|
|
sendEmail.putExtra(Intent.EXTRA_STREAM, uri);
|
2015-10-29 14:01:41 +00:00
|
|
|
return sendEmail;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Intent sendEmail) {
|
|
|
|
if (sendEmail != null)
|
|
|
|
try {
|
|
|
|
context.startActivity(sendEmail);
|
|
|
|
} catch (Throwable ex) {
|
2015-11-20 10:34:23 +00:00
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
2015-10-29 14:01:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
task.execute();
|
|
|
|
}
|
|
|
|
|
2015-11-20 10:34:23 +00:00
|
|
|
private static StringBuilder getLogcat() {
|
2015-10-29 14:01:41 +00:00
|
|
|
String pid = Integer.toString(android.os.Process.myPid());
|
|
|
|
StringBuilder builder = new StringBuilder();
|
2015-11-13 11:34:56 +00:00
|
|
|
Process process = null;
|
2015-11-13 11:16:18 +00:00
|
|
|
BufferedReader br = null;
|
2015-10-29 14:01:41 +00:00
|
|
|
try {
|
|
|
|
String[] command = new String[]{"logcat", "-d", "-v", "threadtime"};
|
2015-11-13 11:34:56 +00:00
|
|
|
process = Runtime.getRuntime().exec(command);
|
2015-11-13 11:16:18 +00:00
|
|
|
br = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
2015-10-29 14:01:41 +00:00
|
|
|
String line;
|
2015-11-13 11:16:18 +00:00
|
|
|
while ((line = br.readLine()) != null)
|
2015-11-26 19:34:03 +00:00
|
|
|
if (line.toLowerCase().contains("netguard"))
|
|
|
|
builder.append(line).append("\r\n");
|
2015-10-29 14:01:41 +00:00
|
|
|
} catch (IOException ex) {
|
2015-11-20 10:34:23 +00:00
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
2015-11-13 11:16:18 +00:00
|
|
|
} finally {
|
|
|
|
if (br != null)
|
|
|
|
try {
|
|
|
|
br.close();
|
|
|
|
} catch (IOException ignored) {
|
|
|
|
}
|
2015-11-13 11:34:56 +00:00
|
|
|
if (process != null)
|
|
|
|
process.destroy();
|
2015-10-29 14:01:41 +00:00
|
|
|
}
|
|
|
|
return builder;
|
|
|
|
}
|
2015-10-25 09:29:49 +00:00
|
|
|
}
|