diff --git a/app/src/main/java/eu/faircode/netguard/AdapterRule.java b/app/src/main/java/eu/faircode/netguard/AdapterRule.java index 5aea50b2..108d1539 100644 --- a/app/src/main/java/eu/faircode/netguard/AdapterRule.java +++ b/app/src/main/java/eu/faircode/netguard/AdapterRule.java @@ -62,11 +62,21 @@ import android.widget.TextView; import com.squareup.picasso.Picasso; +import org.json.JSONArray; +import org.json.JSONObject; + +import java.io.BufferedOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.URL; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; +import javax.net.ssl.HttpsURLConnection; + public class AdapterRule extends RecyclerView.Adapter implements Filterable { private static final String TAG = "NetGuard.Adapter"; @@ -83,6 +93,9 @@ public class AdapterRule extends RecyclerView.Adapter im private List listAll = new ArrayList<>(); private List listFiltered = new ArrayList<>(); + private static final String cUrl = "https://crowd.netguard.me/"; + private static final int cTimeOutMs = 15000; + public static class ViewHolder extends RecyclerView.ViewHolder { public View view; @@ -112,6 +125,7 @@ public class AdapterRule extends RecyclerView.Adapter im public CheckBox cbApply; public Button btnRelated; + public ImageButton ibFetch; public ImageButton ibSettings; public ImageButton ibLaunch; @@ -162,6 +176,7 @@ public class AdapterRule extends RecyclerView.Adapter im cbApply = (CheckBox) itemView.findViewById(R.id.cbApply); btnRelated = (Button) itemView.findViewById(R.id.btnRelated); + ibFetch = (ImageButton) itemView.findViewById(R.id.ibFetch); ibSettings = (ImageButton) itemView.findViewById(R.id.ibSettings); ibLaunch = (ImageButton) itemView.findViewById(R.id.ibLaunch); @@ -429,6 +444,72 @@ public class AdapterRule extends RecyclerView.Adapter im } }); + // Fetch settings + holder.ibFetch.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + new AsyncTask() { + @Override + protected void onPreExecute() { + holder.ibFetch.setEnabled(false); + } + + @Override + protected Object doInBackground(Rule... rules) { + HttpsURLConnection urlConnection = null; + try { + JSONObject json = new JSONObject(); + + json.put("type", "fetch"); + json.put("netguard", Util.getSelfVersionCode(context)); + json.put("fingerprint", Util.getFingerprint(context)); + + JSONArray pkgs = new JSONArray(); + pkgs.put(rules[0].info.packageName); + json.put("package", pkgs); + + urlConnection = (HttpsURLConnection) new URL(cUrl).openConnection(); + urlConnection.setConnectTimeout(cTimeOutMs); + urlConnection.setReadTimeout(cTimeOutMs); + urlConnection.setRequestProperty("Accept", "application/json"); + urlConnection.setRequestProperty("Content-type", "application/json"); + urlConnection.setRequestMethod("POST"); + urlConnection.setDoInput(true); + urlConnection.setDoOutput(true); + + Log.i(TAG, "Request=" + json.toString()); + OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream()); + out.write(json.toString().getBytes()); // UTF-8 + out.flush(); + + int code = urlConnection.getResponseCode(); + if (code != HttpsURLConnection.HTTP_OK) + throw new IOException("HTTP " + code); + + InputStreamReader isr = new InputStreamReader(urlConnection.getInputStream()); + Log.i(TAG, "Response=" + Util.readString(isr).toString()); + + } catch (Throwable ex) { + Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); + return ex; + + } finally { + if (urlConnection != null) + urlConnection.disconnect(); + } + + return null; + } + + @Override + protected void onPostExecute(Object result) { + holder.ibFetch.setEnabled(true); + } + + }.execute(rule); + } + }); + // Launch application settings final Intent settings = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS); settings.setData(Uri.parse("package:" + rule.info.packageName)); diff --git a/app/src/main/java/eu/faircode/netguard/ServiceJob.java b/app/src/main/java/eu/faircode/netguard/ServiceJob.java index 67ab1f47..86f73ebe 100644 --- a/app/src/main/java/eu/faircode/netguard/ServiceJob.java +++ b/app/src/main/java/eu/faircode/netguard/ServiceJob.java @@ -105,8 +105,7 @@ public class ServiceJob extends JobService { throw new IOException("HTTP " + code); InputStreamReader isr = new InputStreamReader(urlConnection.getInputStream()); - String response = read(isr); - Log.i(TAG, "Response=" + response); + Log.i(TAG, "Response=" + Util.readString(isr).toString()); jobFinished(params[0], false); @@ -142,17 +141,6 @@ public class ServiceJob extends JobService { return true; } - private String read(InputStreamReader reader) { - StringBuilder sb = new StringBuilder(2048); - char[] read = new char[128]; - try { - for (int i; (i = reader.read(read)) >= 0; sb.append(read, 0, i)) ; - } catch (Throwable ex) { - Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); - } - return sb.toString(); - } - public static void submit(Rule rule, Context context) { PersistableBundle bundle = new PersistableBundle(); bundle.putString("type", "rule"); diff --git a/app/src/main/java/eu/faircode/netguard/Util.java b/app/src/main/java/eu/faircode/netguard/Util.java index 48465493..0153248c 100644 --- a/app/src/main/java/eu/faircode/netguard/Util.java +++ b/app/src/main/java/eu/faircode/netguard/Util.java @@ -386,7 +386,7 @@ public class Util { return "com.android.vending".equals(context.getPackageManager().getInstallerPackageName(context.getPackageName())); } - public static boolean hasValidFingerprint(Context context) { + public static String getFingerprint(Context context) { try { PackageManager pm = context.getPackageManager(); String pkg = context.getPackageName(); @@ -397,15 +397,19 @@ public class Util { StringBuilder sb = new StringBuilder(); for (byte b : bytes) sb.append(Integer.toString(b & 0xff, 16).toLowerCase()); - String calculated = sb.toString(); - String expected = context.getString(R.string.fingerprint); - return calculated.equals(expected); + return sb.toString(); } catch (Throwable ex) { Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); - return false; + return null; } } + public static boolean hasValidFingerprint(Context context) { + String calculated = getFingerprint(context); + String expected = context.getString(R.string.fingerprint); + return (calculated != null && calculated.equals(expected)); + } + public static void setTheme(Context context) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean dark = prefs.getBoolean("dark_theme", false); @@ -549,6 +553,17 @@ public class Util { } } + public static StringBuilder readString(InputStreamReader reader) { + StringBuilder sb = new StringBuilder(2048); + char[] read = new char[128]; + try { + for (int i; (i = reader.read(read)) >= 0; sb.append(read, 0, i)) ; + } catch (Throwable ex) { + Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); + } + return sb; + } + public static void sendCrashReport(Throwable ex, final Context context) { if (!isPlayStoreInstall(context)) return; diff --git a/app/src/main/res/drawable-hdpi/ic_cloud_download_black_24dp.png b/app/src/main/res/drawable-hdpi/ic_cloud_download_black_24dp.png new file mode 100644 index 00000000..1b590a24 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_cloud_download_black_24dp.png differ diff --git a/app/src/main/res/drawable-hdpi/ic_cloud_download_white_24dp.png b/app/src/main/res/drawable-hdpi/ic_cloud_download_white_24dp.png new file mode 100644 index 00000000..4c5d2d04 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_cloud_download_white_24dp.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_cloud_download_black_24dp.png b/app/src/main/res/drawable-mdpi/ic_cloud_download_black_24dp.png new file mode 100644 index 00000000..47cde709 Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_cloud_download_black_24dp.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_cloud_download_white_24dp.png b/app/src/main/res/drawable-mdpi/ic_cloud_download_white_24dp.png new file mode 100644 index 00000000..0c6978c1 Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_cloud_download_white_24dp.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_cloud_download_black_24dp.png b/app/src/main/res/drawable-xhdpi/ic_cloud_download_black_24dp.png new file mode 100644 index 00000000..c324cd55 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_cloud_download_black_24dp.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_cloud_download_white_24dp.png b/app/src/main/res/drawable-xhdpi/ic_cloud_download_white_24dp.png new file mode 100644 index 00000000..d1a0573a Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_cloud_download_white_24dp.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_cloud_download_black_24dp.png b/app/src/main/res/drawable-xxhdpi/ic_cloud_download_black_24dp.png new file mode 100644 index 00000000..254c20f6 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_cloud_download_black_24dp.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_cloud_download_white_24dp.png b/app/src/main/res/drawable-xxhdpi/ic_cloud_download_white_24dp.png new file mode 100644 index 00000000..3392d972 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_cloud_download_white_24dp.png differ diff --git a/app/src/main/res/drawable-xxxhdpi/ic_cloud_download_black_24dp.png b/app/src/main/res/drawable-xxxhdpi/ic_cloud_download_black_24dp.png new file mode 100644 index 00000000..04ae3760 Binary files /dev/null and b/app/src/main/res/drawable-xxxhdpi/ic_cloud_download_black_24dp.png differ diff --git a/app/src/main/res/drawable-xxxhdpi/ic_cloud_download_white_24dp.png b/app/src/main/res/drawable-xxxhdpi/ic_cloud_download_white_24dp.png new file mode 100644 index 00000000..b24e573e Binary files /dev/null and b/app/src/main/res/drawable-xxxhdpi/ic_cloud_download_white_24dp.png differ diff --git a/app/src/main/res/drawable/fetch_black.xml b/app/src/main/res/drawable/fetch_black.xml new file mode 100644 index 00000000..355ad63c --- /dev/null +++ b/app/src/main/res/drawable/fetch_black.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/fetch_disabled_black.xml b/app/src/main/res/drawable/fetch_disabled_black.xml new file mode 100644 index 00000000..0166eda1 --- /dev/null +++ b/app/src/main/res/drawable/fetch_disabled_black.xml @@ -0,0 +1,4 @@ + + \ No newline at end of file diff --git a/app/src/main/res/drawable/fetch_disabled_white.xml b/app/src/main/res/drawable/fetch_disabled_white.xml new file mode 100644 index 00000000..43c77adc --- /dev/null +++ b/app/src/main/res/drawable/fetch_disabled_white.xml @@ -0,0 +1,4 @@ + + \ No newline at end of file diff --git a/app/src/main/res/drawable/fetch_white.xml b/app/src/main/res/drawable/fetch_white.xml new file mode 100644 index 00000000..44d35acb --- /dev/null +++ b/app/src/main/res/drawable/fetch_white.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/rule.xml b/app/src/main/res/layout/rule.xml index b84a378d..0e571ccc 100644 --- a/app/src/main/res/layout/rule.xml +++ b/app/src/main/res/layout/rule.xml @@ -233,11 +233,20 @@ android:layout_marginRight="8dp" android:text="@string/title_related" /> + + + @@ -17,6 +18,7 @@ @drawable/expander_black @drawable/ic_hourglass_empty_black_24dp @drawable/ic_attach_money_black_24dp + @drawable/fetch_black @drawable/ic_settings_black_24dp @drawable/ic_delete_black_24dp @drawable/ic_launch_black_24dp @@ -27,6 +29,7 @@ @drawable/expander_white @drawable/ic_hourglass_empty_white_24dp @drawable/ic_attach_money_white_24dp + @drawable/fetch_white @drawable/ic_settings_white_24dp @drawable/ic_delete_white_24dp @drawable/ic_launch_white_24dp