(Context context)
| 388 | } |
| 389 | |
| 390 | static void download(Context context) throws IOException, JSONException { |
| 391 | File file = getFile(context); |
| 392 | |
| 393 | URL url = new URL(LIST); |
| 394 | Log.i("GET " + url); |
| 395 | HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); |
| 396 | connection.setRequestMethod("GET"); |
| 397 | connection.setReadTimeout(FETCH_TIMEOUT); |
| 398 | connection.setConnectTimeout(FETCH_TIMEOUT); |
| 399 | ConnectionHelper.setUserAgent(context, connection); |
| 400 | connection.connect(); |
| 401 | |
| 402 | try { |
| 403 | int status = connection.getResponseCode(); |
| 404 | if (status != HttpsURLConnection.HTTP_OK) |
| 405 | throw new FileNotFoundException("Error " + status + ": " + connection.getResponseMessage()); |
| 406 | |
| 407 | try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { |
| 408 | Helper.copy(connection.getInputStream(), os); |
| 409 | } |
| 410 | |
| 411 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); |
| 412 | prefs.edit().putLong("adguard_last", new Date().getTime()).apply(); |
| 413 | } finally { |
| 414 | connection.disconnect(); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | private static File getFile(Context context) { |
| 419 | return new File(context.getFilesDir(), "adguard.txt"); |
no test coverage detected