From c30e2d6a40a31b3af81b6fbea821314ad6f70e22 Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 26 Jun 2020 19:16:00 +0200 Subject: [PATCH] some changes --- app/build.gradle | 5 +- app/src/main/AndroidManifest.xml | 19 + .../app/fedilab/fedilabtube/FedilabTupe.java | 5 +- .../fedilabtube/client/HttpsConnection.java | 1097 +++++------------ .../fedilabtube/client/PeertubeAPI.java | 2 +- .../fedilab/fedilabtube/helper/Helper.java | 30 +- .../services/PeertubeUploadReceiver.java | 63 + app/src/main/res/layout/activity_main.xml | 2 +- .../main/res/navigation/mobile_navigation.xml | 10 +- 9 files changed, 447 insertions(+), 786 deletions(-) create mode 100644 app/src/main/java/app/fedilab/fedilabtube/services/PeertubeUploadReceiver.java diff --git a/app/build.gradle b/app/build.gradle index f0b7e94..755132b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -15,7 +15,9 @@ android { multiDexEnabled true testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } - + dexOptions { + javaMaxHeapSize "4g" + } buildTypes { release { minifyEnabled false @@ -53,6 +55,7 @@ dependencies { androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' + implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0' implementation 'com.github.GrenderG:Toasty:1.4.2' implementation 'com.google.android.exoplayer:exoplayer:2.10.6' implementation "com.github.mabbas007:TagsEditText:1.0.5" diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 48f9869..4044781 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -28,6 +28,25 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/app/fedilab/fedilabtube/FedilabTupe.java b/app/src/main/java/app/fedilab/fedilabtube/FedilabTupe.java index adf1331..e4a8964 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/FedilabTupe.java +++ b/app/src/main/java/app/fedilab/fedilabtube/FedilabTupe.java @@ -9,6 +9,9 @@ public class FedilabTupe extends MultiDexApplication { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); - MultiDex.install(this); + + MultiDex.install(FedilabTupe.this); } + + } \ No newline at end of file diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/HttpsConnection.java b/app/src/main/java/app/fedilab/fedilabtube/client/HttpsConnection.java index 3714349..2fc14a0 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/HttpsConnection.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/HttpsConnection.java @@ -56,7 +56,6 @@ public class HttpsConnection { private HttpsURLConnection httpsURLConnection; - private HttpURLConnection httpURLConnection; private String since_id, max_id; private Context context; private int CHUNK_SIZE = 4096; @@ -246,96 +245,49 @@ public class HttpsConnection { URL url = new URL(urlConnection); - if (urlConnection.startsWith("https://")) { - if (proxy != null) - httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy); - else - httpsURLConnection = (HttpsURLConnection) url.openConnection(); - httpsURLConnection.setConnectTimeout(30 * 1000); - httpsURLConnection.setRequestProperty("http.keepAlive", "false"); - httpsURLConnection.setRequestProperty("Content-Type", "application/json"); - httpsURLConnection.setRequestProperty("Accept", "application/json"); - httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT); - httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance)); - httpsURLConnection.setRequestMethod("GET"); - httpsURLConnection.setDefaultUseCaches(true); - httpsURLConnection.setUseCaches(true); - String response; - if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) { - getSinceMaxId(); - response = converToString(httpsURLConnection.getInputStream()); - } else { - String error = null; - if (httpsURLConnection.getErrorStream() != null) { - InputStream stream = httpsURLConnection.getErrorStream(); - if (stream == null) { - stream = httpsURLConnection.getInputStream(); - } - try (Scanner scanner = new Scanner(stream)) { - scanner.useDelimiter("\\Z"); - if (scanner.hasNext()) { - error = scanner.next(); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - int responseCode = httpsURLConnection.getResponseCode(); - try { - httpsURLConnection.getInputStream().close(); - } catch (Exception ignored) { - } - throw new HttpsConnectionException(responseCode, error); - } + if (proxy != null) + httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy); + else + httpsURLConnection = (HttpsURLConnection) url.openConnection(); + httpsURLConnection.setConnectTimeout(30 * 1000); + httpsURLConnection.setRequestProperty("http.keepAlive", "false"); + httpsURLConnection.setRequestProperty("Content-Type", "application/json"); + httpsURLConnection.setRequestProperty("Accept", "application/json"); + httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT); + httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance)); + httpsURLConnection.setRequestMethod("GET"); + httpsURLConnection.setDefaultUseCaches(true); + httpsURLConnection.setUseCaches(true); + String response; + if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) { getSinceMaxId(); - httpsURLConnection.getInputStream().close(); - return response; + response = converToString(httpsURLConnection.getInputStream()); } else { - if (proxy != null) - httpURLConnection = (HttpURLConnection) url.openConnection(proxy); - else - httpURLConnection = (HttpURLConnection) url.openConnection(); - httpURLConnection.setConnectTimeout(30 * 1000); - httpURLConnection.setRequestProperty("http.keepAlive", "false"); - httpURLConnection.setRequestProperty("Content-Type", "application/json"); - httpURLConnection.setRequestProperty("Accept", "application/json"); - httpURLConnection.setRequestProperty("User-Agent", USER_AGENT); - httpURLConnection.setRequestMethod("GET"); - httpURLConnection.setDefaultUseCaches(true); - httpURLConnection.setUseCaches(true); - String response; - if (httpURLConnection.getResponseCode() >= 200 && httpURLConnection.getResponseCode() < 400) { - getSinceMaxId(); - response = converToString(httpURLConnection.getInputStream()); - } else { - String error = null; - if (httpURLConnection.getErrorStream() != null) { - InputStream stream = httpURLConnection.getErrorStream(); - if (stream == null) { - stream = httpURLConnection.getInputStream(); - } - try (Scanner scanner = new Scanner(stream)) { - scanner.useDelimiter("\\Z"); - if (scanner.hasNext()) { - error = scanner.next(); - } - } catch (Exception e) { - e.printStackTrace(); - } + String error = null; + if (httpsURLConnection.getErrorStream() != null) { + InputStream stream = httpsURLConnection.getErrorStream(); + if (stream == null) { + stream = httpsURLConnection.getInputStream(); } - int responseCode = httpURLConnection.getResponseCode(); - try { - httpURLConnection.getInputStream().close(); - } catch (Exception ignored) { + try (Scanner scanner = new Scanner(stream)) { + scanner.useDelimiter("\\Z"); + if (scanner.hasNext()) { + error = scanner.next(); + } + } catch (Exception e) { + e.printStackTrace(); } - throw new HttpsConnectionException(responseCode, error); } - getSinceMaxId(); - httpURLConnection.getInputStream().close(); - return response; + int responseCode = httpsURLConnection.getResponseCode(); + try { + httpsURLConnection.getInputStream().close(); + } catch (Exception ignored) { + } + throw new HttpsConnectionException(responseCode, error); } - - + getSinceMaxId(); + httpsURLConnection.getInputStream().close(); + return response; } @@ -358,102 +310,53 @@ public class HttpsConnection { postData.append(param.getValue()); } byte[] postDataBytes = postData.toString().getBytes(StandardCharsets.UTF_8); - if (urlConnection.startsWith("https://")) { - if (proxy != null) - httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy); - else - httpsURLConnection = (HttpsURLConnection) url.openConnection(); - httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT); - httpsURLConnection.setConnectTimeout(timeout * 1000); - httpsURLConnection.setDoOutput(true); - httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance)); - httpsURLConnection.setRequestMethod("POST"); - if (token != null && !token.startsWith("Basic ")) - httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token); - else if (token != null && token.startsWith("Basic ")) - httpsURLConnection.setRequestProperty("Authorization", token); - httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); + if (proxy != null) + httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy); + else + httpsURLConnection = (HttpsURLConnection) url.openConnection(); + httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT); + httpsURLConnection.setConnectTimeout(timeout * 1000); + httpsURLConnection.setDoOutput(true); + httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance)); + httpsURLConnection.setRequestMethod("POST"); + if (token != null && !token.startsWith("Basic ")) + httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token); + else if (token != null && token.startsWith("Basic ")) + httpsURLConnection.setRequestProperty("Authorization", token); + httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); - httpsURLConnection.getOutputStream().write(postDataBytes); - String response; - if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) { - getSinceMaxId(); - response = converToString(httpsURLConnection.getInputStream()); - } else { - String error = null; - if (httpsURLConnection.getErrorStream() != null) { - InputStream stream = httpsURLConnection.getErrorStream(); - if (stream == null) { - stream = httpsURLConnection.getInputStream(); - } - try (Scanner scanner = new Scanner(stream)) { - scanner.useDelimiter("\\Z"); - if (scanner.hasNext()) { - error = scanner.next(); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - int responseCode = httpsURLConnection.getResponseCode(); - try { - httpsURLConnection.getInputStream().close(); - } catch (Exception ignored) { - } - throw new HttpsConnectionException(responseCode, error); - } + httpsURLConnection.getOutputStream().write(postDataBytes); + String response; + if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) { getSinceMaxId(); - httpsURLConnection.getInputStream().close(); - return response; + response = converToString(httpsURLConnection.getInputStream()); } else { - if (proxy != null) - httpURLConnection = (HttpURLConnection) url.openConnection(proxy); - else - httpURLConnection = (HttpURLConnection) url.openConnection(); - httpURLConnection.setRequestProperty("User-Agent", USER_AGENT); - httpURLConnection.setConnectTimeout(timeout * 1000); - httpURLConnection.setDoOutput(true); - httpURLConnection.setRequestMethod("POST"); - if (token != null && !token.startsWith("Basic ")) - httpURLConnection.setRequestProperty("Authorization", "Bearer " + token); - else if (token != null && token.startsWith("Basic ")) - httpURLConnection.setRequestProperty("Authorization", token); - httpURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); - - httpURLConnection.getOutputStream().write(postDataBytes); - String response; - if (httpURLConnection.getResponseCode() >= 200 && httpURLConnection.getResponseCode() < 400) { - getSinceMaxId(); - response = converToString(httpURLConnection.getInputStream()); - } else { - String error = null; - if (httpURLConnection.getErrorStream() != null) { - InputStream stream = httpURLConnection.getErrorStream(); - if (stream == null) { - stream = httpURLConnection.getInputStream(); - } - try (Scanner scanner = new Scanner(stream)) { - scanner.useDelimiter("\\Z"); - if (scanner.hasNext()) { - error = scanner.next(); - } - } catch (Exception e) { - e.printStackTrace(); - } + String error = null; + if (httpsURLConnection.getErrorStream() != null) { + InputStream stream = httpsURLConnection.getErrorStream(); + if (stream == null) { + stream = httpsURLConnection.getInputStream(); } - int responseCode = httpURLConnection.getResponseCode(); - try { - httpURLConnection.getInputStream().close(); - } catch (Exception ignored) { + try (Scanner scanner = new Scanner(stream)) { + scanner.useDelimiter("\\Z"); + if (scanner.hasNext()) { + error = scanner.next(); + } + } catch (Exception e) { + e.printStackTrace(); } - throw new HttpsConnectionException(responseCode, error); } - getSinceMaxId(); - httpURLConnection.getInputStream().close(); - return response; + int responseCode = httpsURLConnection.getResponseCode(); + try { + httpsURLConnection.getInputStream().close(); + } catch (Exception ignored) { + } + throw new HttpsConnectionException(responseCode, error); } - + getSinceMaxId(); + httpsURLConnection.getInputStream().close(); + return response; } @@ -462,105 +365,56 @@ public class HttpsConnection { URL url = new URL(urlConnection); byte[] postDataBytes; postDataBytes = jsonObject.toString().getBytes(StandardCharsets.UTF_8); - if (urlConnection.startsWith("https://")) { - if (proxy != null) - httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy); - else - httpsURLConnection = (HttpsURLConnection) url.openConnection(); - httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT); - httpsURLConnection.setConnectTimeout(timeout * 1000); - httpsURLConnection.setDoOutput(true); - httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance)); - httpsURLConnection.setRequestProperty("Content-Type", "application/json"); - httpsURLConnection.setRequestProperty("Accept", "application/json"); - httpsURLConnection.setRequestMethod("POST"); - if (token != null && !token.startsWith("Basic ")) - httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token); - else if (token != null && token.startsWith("Basic ")) - httpsURLConnection.setRequestProperty("Authorization", token); - httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); + if (proxy != null) + httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy); + else + httpsURLConnection = (HttpsURLConnection) url.openConnection(); + httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT); + httpsURLConnection.setConnectTimeout(timeout * 1000); + httpsURLConnection.setDoOutput(true); + httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance)); + httpsURLConnection.setRequestProperty("Content-Type", "application/json"); + httpsURLConnection.setRequestProperty("Accept", "application/json"); + httpsURLConnection.setRequestMethod("POST"); + if (token != null && !token.startsWith("Basic ")) + httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token); + else if (token != null && token.startsWith("Basic ")) + httpsURLConnection.setRequestProperty("Authorization", token); + httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); - httpsURLConnection.getOutputStream().write(postDataBytes); - String response; - if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) { - getSinceMaxId(); - response = converToString(httpsURLConnection.getInputStream()); - } else { - String error = null; - if (httpsURLConnection.getErrorStream() != null) { - InputStream stream = httpsURLConnection.getErrorStream(); - if (stream == null) { - stream = httpsURLConnection.getInputStream(); - } - try (Scanner scanner = new Scanner(stream)) { - scanner.useDelimiter("\\Z"); - if (scanner.hasNext()) { - error = scanner.next(); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - int responseCode = httpsURLConnection.getResponseCode(); - try { - httpsURLConnection.getInputStream().close(); - } catch (Exception ignored) { - } - throw new HttpsConnectionException(responseCode, error); - } + httpsURLConnection.getOutputStream().write(postDataBytes); + String response; + if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) { getSinceMaxId(); - httpsURLConnection.getInputStream().close(); - return response; + response = converToString(httpsURLConnection.getInputStream()); } else { - if (proxy != null) - httpURLConnection = (HttpURLConnection) url.openConnection(proxy); - else - httpURLConnection = (HttpURLConnection) url.openConnection(); - httpURLConnection.setRequestProperty("User-Agent", USER_AGENT); - httpURLConnection.setConnectTimeout(timeout * 1000); - httpURLConnection.setDoOutput(true); - httpURLConnection.setRequestMethod("POST"); - if (token != null && !token.startsWith("Basic ")) - httpURLConnection.setRequestProperty("Authorization", "Bearer " + token); - else if (token != null && token.startsWith("Basic ")) - httpURLConnection.setRequestProperty("Authorization", token); - httpURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); - - httpURLConnection.getOutputStream().write(postDataBytes); - String response; - if (httpURLConnection.getResponseCode() >= 200 && httpURLConnection.getResponseCode() < 400) { - getSinceMaxId(); - response = converToString(httpURLConnection.getInputStream()); - } else { - String error = null; - if (httpURLConnection.getErrorStream() != null) { - InputStream stream = httpURLConnection.getErrorStream(); - if (stream == null) { - stream = httpURLConnection.getInputStream(); - } - try (Scanner scanner = new Scanner(stream)) { - scanner.useDelimiter("\\Z"); - if (scanner.hasNext()) { - error = scanner.next(); - } - } catch (Exception e) { - e.printStackTrace(); - } + String error = null; + if (httpsURLConnection.getErrorStream() != null) { + InputStream stream = httpsURLConnection.getErrorStream(); + if (stream == null) { + stream = httpsURLConnection.getInputStream(); } - int responseCode = httpURLConnection.getResponseCode(); - try { - httpURLConnection.getInputStream().close(); - } catch (Exception ignored) { + try (Scanner scanner = new Scanner(stream)) { + scanner.useDelimiter("\\Z"); + if (scanner.hasNext()) { + error = scanner.next(); + } + } catch (Exception e) { + e.printStackTrace(); } - throw new HttpsConnectionException(responseCode, error); } - getSinceMaxId(); - httpURLConnection.getInputStream().close(); - return response; + int responseCode = httpsURLConnection.getResponseCode(); + try { + httpsURLConnection.getInputStream().close(); + } catch (Exception ignored) { + } + throw new HttpsConnectionException(responseCode, error); } - + getSinceMaxId(); + httpsURLConnection.getInputStream().close(); + return response; } @SuppressWarnings("SameParameterValue") @@ -627,119 +481,59 @@ public class HttpsConnection { new Thread(() -> { URL url; HttpsURLConnection httpsURLConnection; - HttpURLConnection httpURLConnection; - if (downloadUrl.startsWith("https://")) { - try { - url = new URL(downloadUrl); - if (proxy != null) - httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy); - else - httpsURLConnection = (HttpsURLConnection) url.openConnection(); - httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT); - int responseCode = httpsURLConnection.getResponseCode(); + try { + url = new URL(downloadUrl); + if (proxy != null) + httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy); + else + httpsURLConnection = (HttpsURLConnection) url.openConnection(); + httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT); + int responseCode = httpsURLConnection.getResponseCode(); - // always check HTTP response code first - if (responseCode == HttpURLConnection.HTTP_OK) { - String fileName = ""; - String disposition = httpsURLConnection.getHeaderField("Content-Disposition"); + // always check HTTP response code first + if (responseCode == HttpURLConnection.HTTP_OK) { + String fileName = ""; + String disposition = httpsURLConnection.getHeaderField("Content-Disposition"); - if (disposition != null) { - // extracts file name from header field - int index = disposition.indexOf("filename="); - if (index > 0) { - fileName = disposition.substring(index + 10, - disposition.length() - 1); - } - } else { - // extracts file name from URL - fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/") + 1 - ); + if (disposition != null) { + // extracts file name from header field + int index = disposition.indexOf("filename="); + if (index > 0) { + fileName = disposition.substring(index + 10, + disposition.length() - 1); } - fileName = FileNameCleaner.cleanFileName(fileName); - // opens input stream from the HTTP connection - InputStream inputStream = httpsURLConnection.getInputStream(); - File saveDir = context.getCacheDir(); - final String saveFilePath = saveDir + File.separator + fileName; - - // opens an output stream to save into file - FileOutputStream outputStream = new FileOutputStream(saveFilePath); - - int bytesRead; - byte[] buffer = new byte[CHUNK_SIZE]; - int contentSize = httpsURLConnection.getContentLength(); - int downloadedFileSize = 0; - while ((bytesRead = inputStream.read(buffer)) != -1) { - outputStream.write(buffer, 0, bytesRead); - downloadedFileSize += bytesRead; - } - outputStream.close(); - inputStream.close(); } else { - final Error error = new Error(); - error.setError(String.valueOf(responseCode)); - + // extracts file name from URL + fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/") + 1 + ); } - } catch (IOException e) { - Error error = new Error(); - error.setError(context.getString(R.string.toast_error)); - } + fileName = FileNameCleaner.cleanFileName(fileName); + // opens input stream from the HTTP connection + InputStream inputStream = httpsURLConnection.getInputStream(); + File saveDir = context.getCacheDir(); + final String saveFilePath = saveDir + File.separator + fileName; - } else { - try { - url = new URL(downloadUrl); - if (proxy != null) - httpURLConnection = (HttpURLConnection) url.openConnection(proxy); - else - httpURLConnection = (HttpURLConnection) url.openConnection(); - httpURLConnection.setRequestProperty("User-Agent", USER_AGENT); - int responseCode = httpURLConnection.getResponseCode(); - - // always check HTTP response code first - if (responseCode == HttpURLConnection.HTTP_OK) { - String fileName = ""; - String disposition = httpURLConnection.getHeaderField("Content-Disposition"); - - if (disposition != null) { - // extracts file name from header field - int index = disposition.indexOf("filename="); - if (index > 0) { - fileName = disposition.substring(index + 10, - disposition.length() - 1); - } - } else { - // extracts file name from URL - fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/") + 1 - ); - } - fileName = FileNameCleaner.cleanFileName(fileName); - // opens input stream from the HTTP connection - InputStream inputStream = httpURLConnection.getInputStream(); - File saveDir = context.getCacheDir(); - final String saveFilePath = saveDir + File.separator + fileName; - - // opens an output stream to save into file - FileOutputStream outputStream = new FileOutputStream(saveFilePath); - - int bytesRead; - byte[] buffer = new byte[CHUNK_SIZE]; - int contentSize = httpURLConnection.getContentLength(); - int downloadedFileSize = 0; - while ((bytesRead = inputStream.read(buffer)) != -1) { - outputStream.write(buffer, 0, bytesRead); - downloadedFileSize += bytesRead; - } - outputStream.close(); - inputStream.close(); - } else { - final Error error = new Error(); - error.setError(String.valueOf(responseCode)); + // opens an output stream to save into file + FileOutputStream outputStream = new FileOutputStream(saveFilePath); + int bytesRead; + byte[] buffer = new byte[CHUNK_SIZE]; + int contentSize = httpsURLConnection.getContentLength(); + int downloadedFileSize = 0; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + downloadedFileSize += bytesRead; } - } catch (IOException e) { - Error error = new Error(); - error.setError(context.getString(R.string.toast_error)); - } + outputStream.close(); + inputStream.close(); + } else { + final Error error = new Error(); + error.setError(String.valueOf(responseCode)); + } + } catch (IOException e) { + Error error = new Error(); + error.setError(context.getString(R.string.toast_error)); } }).start(); } @@ -753,49 +547,26 @@ public class HttpsConnection { } catch (MalformedURLException e) { return null; } - if (downloadUrl.startsWith("https://")) { + try { + if (proxy != null) + httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy); + else + httpsURLConnection = (HttpsURLConnection) url.openConnection(); + httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance)); + httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT); + int responseCode = httpsURLConnection.getResponseCode(); + // always check HTTP response code first + if (responseCode == HttpURLConnection.HTTP_OK) { + // opens input stream from the HTTP connection + return httpsURLConnection.getInputStream(); + } + httpsURLConnection.getInputStream().close(); + } catch (IOException | NoSuchAlgorithmException | KeyManagementException ignored) { + } + if (httpsURLConnection != null) try { - if (proxy != null) - httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy); - else - httpsURLConnection = (HttpsURLConnection) url.openConnection(); - httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance)); - httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT); - int responseCode = httpsURLConnection.getResponseCode(); - // always check HTTP response code first - if (responseCode == HttpURLConnection.HTTP_OK) { - // opens input stream from the HTTP connection - return httpsURLConnection.getInputStream(); - } httpsURLConnection.getInputStream().close(); - } catch (IOException | NoSuchAlgorithmException | KeyManagementException ignored) { - } - if (httpsURLConnection != null) - try { - httpsURLConnection.getInputStream().close(); - } catch (Exception ignored) { - } - } else { - try { - if (proxy != null) - httpURLConnection = (HttpURLConnection) url.openConnection(proxy); - else - httpURLConnection = (HttpURLConnection) url.openConnection(); - httpURLConnection.setRequestProperty("User-Agent", USER_AGENT); - int responseCode = httpURLConnection.getResponseCode(); - // always check HTTP response code first - if (responseCode == HttpURLConnection.HTTP_OK) { - // opens input stream from the HTTP connection - return httpURLConnection.getInputStream(); - } - httpURLConnection.getInputStream().close(); - } catch (IOException ignored) { - } - if (httpURLConnection != null) - try { - httpURLConnection.getInputStream().close(); - } catch (Exception ignored) { - } + } catch (Exception ignored) { } return null; } @@ -881,128 +652,67 @@ public class HttpsConnection { } byte[] postDataBytes = (postData.toString()).getBytes(StandardCharsets.UTF_8); - if (urlConnection.startsWith("https://")) { - if (proxy != null) - httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy); - else - httpsURLConnection = (HttpsURLConnection) url.openConnection(); - httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT); - httpsURLConnection.setConnectTimeout(timeout * 1000); - httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance)); - if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) { - httpsURLConnection.setRequestMethod("PATCH"); - } else { - httpsURLConnection.setRequestProperty("X-HTTP-Method-Override", "PATCH"); - httpsURLConnection.setRequestMethod("POST"); - } - if (token != null && !token.startsWith("Basic ")) - httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token); - else if (token != null && token.startsWith("Basic ")) - httpsURLConnection.setRequestProperty("Authorization", token); - httpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); - httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); - httpsURLConnection.setDoOutput(true); - - String response; - OutputStream outputStream = httpsURLConnection.getOutputStream(); - outputStream.write(postDataBytes); - if (avatar != null) { - uploadMedia(urlConnection, avatar, null, avatarName); - } - if (header != null) { - uploadMedia(urlConnection, null, header, headerName); - } - if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) { - response = converToString(httpsURLConnection.getInputStream()); - } else { - String error = null; - if (httpsURLConnection.getErrorStream() != null) { - InputStream stream = httpsURLConnection.getErrorStream(); - if (stream == null) { - stream = httpsURLConnection.getInputStream(); - } - try (Scanner scanner = new Scanner(stream)) { - scanner.useDelimiter("\\Z"); - if (scanner.hasNext()) { - error = scanner.next(); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - int responseCode = httpsURLConnection.getResponseCode(); - try { - httpsURLConnection.getInputStream().close(); - } catch (Exception ignored) { - } - try { - httpsURLConnection.getInputStream().close(); - } catch (Exception ignored) { - } - throw new HttpsConnectionException(responseCode, error); - } - httpsURLConnection.getInputStream().close(); - return response; + if (proxy != null) + httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy); + else + httpsURLConnection = (HttpsURLConnection) url.openConnection(); + httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT); + httpsURLConnection.setConnectTimeout(timeout * 1000); + httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance)); + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) { + httpsURLConnection.setRequestMethod("PATCH"); } else { - if (proxy != null) - httpURLConnection = (HttpsURLConnection) url.openConnection(proxy); - else - httpURLConnection = (HttpsURLConnection) url.openConnection(); - httpURLConnection.setRequestProperty("User-Agent", USER_AGENT); - httpURLConnection.setConnectTimeout(timeout * 1000); - if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) { - httpURLConnection.setRequestMethod("PATCH"); - } else { - httpURLConnection.setRequestProperty("X-HTTP-Method-Override", "PATCH"); - httpURLConnection.setRequestMethod("POST"); - } - if (token != null && !token.startsWith("Basic ")) - httpURLConnection.setRequestProperty("Authorization", "Bearer " + token); - else if (token != null && token.startsWith("Basic ")) - httpURLConnection.setRequestProperty("Authorization", token); - httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); - httpURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); - httpURLConnection.setDoOutput(true); - - - OutputStream outputStream = httpURLConnection.getOutputStream(); - outputStream.write(postDataBytes); - if (avatar != null) { - uploadMedia(urlConnection, avatar, null, avatarName); - } - if (header != null) { - uploadMedia(urlConnection, null, header, headerName); - } - String response; - if (httpURLConnection.getResponseCode() >= 200 && httpURLConnection.getResponseCode() < 400) { - response = converToString(httpsURLConnection.getInputStream()); - } else { - String error = null; - if (httpURLConnection.getErrorStream() != null) { - InputStream stream = httpURLConnection.getErrorStream(); - if (stream == null) { - stream = httpURLConnection.getInputStream(); - } - try (Scanner scanner = new Scanner(stream)) { - scanner.useDelimiter("\\Z"); - if (scanner.hasNext()) { - error = scanner.next(); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - int responseCode = httpURLConnection.getResponseCode(); - try { - httpURLConnection.getInputStream().close(); - } catch (Exception ignored) { - } - - throw new HttpsConnectionException(responseCode, error); - } - httpURLConnection.getInputStream().close(); - return response; + httpsURLConnection.setRequestProperty("X-HTTP-Method-Override", "PATCH"); + httpsURLConnection.setRequestMethod("POST"); } + if (token != null && !token.startsWith("Basic ")) + httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token); + else if (token != null && token.startsWith("Basic ")) + httpsURLConnection.setRequestProperty("Authorization", token); + httpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); + httpsURLConnection.setDoOutput(true); + + String response; + OutputStream outputStream = httpsURLConnection.getOutputStream(); + outputStream.write(postDataBytes); + if (avatar != null) { + uploadMedia(urlConnection, avatar, null, avatarName); + } + if (header != null) { + uploadMedia(urlConnection, null, header, headerName); + } + if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) { + response = converToString(httpsURLConnection.getInputStream()); + } else { + String error = null; + if (httpsURLConnection.getErrorStream() != null) { + InputStream stream = httpsURLConnection.getErrorStream(); + if (stream == null) { + stream = httpsURLConnection.getInputStream(); + } + try (Scanner scanner = new Scanner(stream)) { + scanner.useDelimiter("\\Z"); + if (scanner.hasNext()) { + error = scanner.next(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + int responseCode = httpsURLConnection.getResponseCode(); + try { + httpsURLConnection.getInputStream().close(); + } catch (Exception ignored) { + } + try { + httpsURLConnection.getInputStream().close(); + } catch (Exception ignored) { + } + throw new HttpsConnectionException(responseCode, error); + } + httpsURLConnection.getInputStream().close(); + return response; } @@ -1027,104 +737,54 @@ public class HttpsConnection { } byte[] postDataBytes = postData.toString().getBytes(StandardCharsets.UTF_8); - if (urlConnection.startsWith("https://")) { - if (proxy != null) - httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy); - else - httpsURLConnection = (HttpsURLConnection) url.openConnection(); - httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT); - httpsURLConnection.setConnectTimeout(timeout * 1000); - httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance)); - if (token != null && !token.startsWith("Basic ")) - httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token); - else if (token != null && token.startsWith("Basic ")) - httpsURLConnection.setRequestProperty("Authorization", token); - httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); + if (proxy != null) + httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy); + else + httpsURLConnection = (HttpsURLConnection) url.openConnection(); + httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT); + httpsURLConnection.setConnectTimeout(timeout * 1000); + httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance)); + if (token != null && !token.startsWith("Basic ")) + httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token); + else if (token != null && token.startsWith("Basic ")) + httpsURLConnection.setRequestProperty("Authorization", token); + httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); - httpsURLConnection.setRequestMethod("PUT"); - httpsURLConnection.setDoInput(true); - httpsURLConnection.setDoOutput(true); + httpsURLConnection.setRequestMethod("PUT"); + httpsURLConnection.setDoInput(true); + httpsURLConnection.setDoOutput(true); - httpsURLConnection.getOutputStream().write(postDataBytes); - String response; - if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) { - getSinceMaxId(); - response = converToString(httpsURLConnection.getInputStream()); - } else { - String error = null; - if (httpsURLConnection.getErrorStream() != null) { - InputStream stream = httpsURLConnection.getErrorStream(); - if (stream == null) { - stream = httpsURLConnection.getInputStream(); - } - try (Scanner scanner = new Scanner(stream)) { - scanner.useDelimiter("\\Z"); - if (scanner.hasNext()) { - error = scanner.next(); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - int responseCode = httpsURLConnection.getResponseCode(); - try { - httpsURLConnection.getInputStream().close(); - } catch (Exception ignored) { - } - throw new HttpsConnectionException(responseCode, error); - } + httpsURLConnection.getOutputStream().write(postDataBytes); + String response; + if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) { getSinceMaxId(); - httpsURLConnection.getInputStream().close(); - return response; + response = converToString(httpsURLConnection.getInputStream()); } else { - if (proxy != null) - httpURLConnection = (HttpURLConnection) url.openConnection(proxy); - else - httpURLConnection = (HttpURLConnection) url.openConnection(); - httpURLConnection.setRequestProperty("User-Agent", USER_AGENT); - httpURLConnection.setConnectTimeout(timeout * 1000); - if (token != null && !token.startsWith("Basic ")) - httpURLConnection.setRequestProperty("Authorization", "Bearer " + token); - else if (token != null && token.startsWith("Basic ")) - httpURLConnection.setRequestProperty("Authorization", token); - httpURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); - - httpURLConnection.setRequestMethod("PUT"); - httpURLConnection.setDoInput(true); - httpURLConnection.setDoOutput(true); - - httpURLConnection.getOutputStream().write(postDataBytes); - String response; - if (httpURLConnection.getResponseCode() >= 200 && httpURLConnection.getResponseCode() < 400) { - getSinceMaxId(); - response = converToString(httpURLConnection.getInputStream()); - } else { - String error = null; - if (httpURLConnection.getErrorStream() != null) { - InputStream stream = httpURLConnection.getErrorStream(); - if (stream == null) { - stream = httpURLConnection.getInputStream(); - } - try (Scanner scanner = new Scanner(stream)) { - scanner.useDelimiter("\\Z"); - if (scanner.hasNext()) { - error = scanner.next(); - } - } catch (Exception e) { - e.printStackTrace(); - } + String error = null; + if (httpsURLConnection.getErrorStream() != null) { + InputStream stream = httpsURLConnection.getErrorStream(); + if (stream == null) { + stream = httpsURLConnection.getInputStream(); } - int responseCode = httpURLConnection.getResponseCode(); - try { - httpURLConnection.getInputStream().close(); - } catch (Exception ignored) { + try (Scanner scanner = new Scanner(stream)) { + scanner.useDelimiter("\\Z"); + if (scanner.hasNext()) { + error = scanner.next(); + } + } catch (Exception e) { + e.printStackTrace(); } - throw new HttpsConnectionException(responseCode, error); } - getSinceMaxId(); - httpURLConnection.getInputStream().close(); - return response; + int responseCode = httpsURLConnection.getResponseCode(); + try { + httpsURLConnection.getInputStream().close(); + } catch (Exception ignored) { + } + throw new HttpsConnectionException(responseCode, error); } + getSinceMaxId(); + httpsURLConnection.getInputStream().close(); + return response; } @@ -1149,95 +809,48 @@ public class HttpsConnection { } byte[] postDataBytes = postData.toString().getBytes(StandardCharsets.UTF_8); - if (urlConnection.startsWith("https://")) { - if (proxy != null) - httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy); - else - httpsURLConnection = (HttpsURLConnection) url.openConnection(); - httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT); - httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance)); - if (token != null && !token.startsWith("Basic ")) - httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token); - else if (token != null && token.startsWith("Basic ")) - httpsURLConnection.setRequestProperty("Authorization", token); - httpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); - httpsURLConnection.setRequestMethod("DELETE"); - httpsURLConnection.setConnectTimeout(timeout * 1000); - httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); + if (proxy != null) + httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy); + else + httpsURLConnection = (HttpsURLConnection) url.openConnection(); + httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT); + httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance)); + if (token != null && !token.startsWith("Basic ")) + httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token); + else if (token != null && token.startsWith("Basic ")) + httpsURLConnection.setRequestProperty("Authorization", token); + httpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + httpsURLConnection.setRequestMethod("DELETE"); + httpsURLConnection.setConnectTimeout(timeout * 1000); + httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); - httpsURLConnection.getOutputStream().write(postDataBytes); - if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) { - getSinceMaxId(); - httpsURLConnection.getInputStream().close(); - return httpsURLConnection.getResponseCode(); - } else { - String error = null; - if (httpsURLConnection.getErrorStream() != null) { - InputStream stream = httpsURLConnection.getErrorStream(); - if (stream == null) { - stream = httpsURLConnection.getInputStream(); - } - try (Scanner scanner = new Scanner(stream)) { - scanner.useDelimiter("\\Z"); - if (scanner.hasNext()) { - error = scanner.next(); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - int responseCode = httpsURLConnection.getResponseCode(); - try { - httpsURLConnection.getInputStream().close(); - } catch (Exception ignored) { - } - throw new HttpsConnectionException(responseCode, error); - } + httpsURLConnection.getOutputStream().write(postDataBytes); + if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) { + getSinceMaxId(); + httpsURLConnection.getInputStream().close(); + return httpsURLConnection.getResponseCode(); } else { - if (proxy != null) - httpURLConnection = (HttpURLConnection) url.openConnection(proxy); - else - httpURLConnection = (HttpURLConnection) url.openConnection(); - httpURLConnection.setRequestProperty("User-Agent", USER_AGENT); - if (token != null && !token.startsWith("Basic ")) - httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token); - else if (token != null && token.startsWith("Basic ")) - httpsURLConnection.setRequestProperty("Authorization", token); - httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); - httpURLConnection.setRequestMethod("DELETE"); - httpURLConnection.setConnectTimeout(timeout * 1000); - httpURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); - - httpURLConnection.getOutputStream().write(postDataBytes); - - - if (httpURLConnection.getResponseCode() >= 200 && httpURLConnection.getResponseCode() < 400) { - getSinceMaxId(); - httpURLConnection.getInputStream().close(); - return httpURLConnection.getResponseCode(); - } else { - String error = null; - if (httpURLConnection.getErrorStream() != null) { - InputStream stream = httpURLConnection.getErrorStream(); - if (stream == null) { - stream = httpURLConnection.getInputStream(); - } - try (Scanner scanner = new Scanner(stream)) { - scanner.useDelimiter("\\Z"); - if (scanner.hasNext()) { - error = scanner.next(); - } - } catch (Exception e) { - e.printStackTrace(); - } + String error = null; + if (httpsURLConnection.getErrorStream() != null) { + InputStream stream = httpsURLConnection.getErrorStream(); + if (stream == null) { + stream = httpsURLConnection.getInputStream(); } - int responseCode = httpURLConnection.getResponseCode(); - try { - httpURLConnection.getInputStream().close(); - } catch (Exception ignored) { + try (Scanner scanner = new Scanner(stream)) { + scanner.useDelimiter("\\Z"); + if (scanner.hasNext()) { + error = scanner.next(); + } + } catch (Exception e) { + e.printStackTrace(); } - throw new HttpsConnectionException(responseCode, error); } + int responseCode = httpsURLConnection.getResponseCode(); + try { + httpsURLConnection.getInputStream().close(); + } catch (Exception ignored) { + } + throw new HttpsConnectionException(responseCode, error); } } @@ -1251,55 +864,31 @@ public class HttpsConnection { private void getSinceMaxId() { - if (Helper.getLiveInstanceWithProtocol(context) == null) + + if (httpsURLConnection == null) return; - if (Helper.getLiveInstanceWithProtocol(context).startsWith("https://")) { - if (httpsURLConnection == null) - return; - Map> map = httpsURLConnection.getHeaderFields(); + Map> map = httpsURLConnection.getHeaderFields(); - for (Map.Entry> entry : map.entrySet()) { - if (entry.toString().startsWith("Link") || entry.toString().startsWith("link")) { - Pattern patternMaxId = Pattern.compile("max_id=([0-9a-zA-Z]+).*"); - Matcher matcherMaxId = patternMaxId.matcher(entry.toString()); - if (matcherMaxId.find()) { - max_id = matcherMaxId.group(1); - } - if (entry.toString().startsWith("Link")) { - Pattern patternSinceId = Pattern.compile("since_id=([0-9a-zA-Z]+).*"); - Matcher matcherSinceId = patternSinceId.matcher(entry.toString()); - if (matcherSinceId.find()) { - since_id = matcherSinceId.group(1); - } - - } - } else if (entry.toString().startsWith("Min-Id") || entry.toString().startsWith("min-id")) { - Pattern patternMaxId = Pattern.compile("min-id=\\[([0-9a-zA-Z]+).*]"); - Matcher matcherMaxId = patternMaxId.matcher(entry.toString()); - if (matcherMaxId.find()) { - max_id = matcherMaxId.group(1); - } + for (Map.Entry> entry : map.entrySet()) { + if (entry.toString().startsWith("Link") || entry.toString().startsWith("link")) { + Pattern patternMaxId = Pattern.compile("max_id=([0-9a-zA-Z]+).*"); + Matcher matcherMaxId = patternMaxId.matcher(entry.toString()); + if (matcherMaxId.find()) { + max_id = matcherMaxId.group(1); } - } - } else { - if (httpURLConnection == null) - return; - Map> map = httpURLConnection.getHeaderFields(); - for (Map.Entry> entry : map.entrySet()) { - if (entry.toString().startsWith("Link") || entry.toString().startsWith("link")) { - Pattern patternMaxId = Pattern.compile("max_id=([0-9a-zA-Z]+).*"); - Matcher matcherMaxId = patternMaxId.matcher(entry.toString()); - if (matcherMaxId.find()) { - max_id = matcherMaxId.group(1); + if (entry.toString().startsWith("Link")) { + Pattern patternSinceId = Pattern.compile("since_id=([0-9a-zA-Z]+).*"); + Matcher matcherSinceId = patternSinceId.matcher(entry.toString()); + if (matcherSinceId.find()) { + since_id = matcherSinceId.group(1); } - if (entry.toString().startsWith("Link")) { - Pattern patternSinceId = Pattern.compile("since_id=([0-9a-zA-Z]+).*"); - Matcher matcherSinceId = patternSinceId.matcher(entry.toString()); - if (matcherSinceId.find()) { - since_id = matcherSinceId.group(1); - } - } + } + } else if (entry.toString().startsWith("Min-Id") || entry.toString().startsWith("min-id")) { + Pattern patternMaxId = Pattern.compile("min-id=\\[([0-9a-zA-Z]+).*]"); + Matcher matcherMaxId = patternMaxId.matcher(entry.toString()); + if (matcherMaxId.find()) { + max_id = matcherMaxId.group(1); } } } @@ -1311,18 +900,10 @@ public class HttpsConnection { } int getActionCode() { - if (Helper.getLiveInstanceWithProtocol(context).startsWith("https://")) { - try { - return httpsURLConnection.getResponseCode(); - } catch (IOException e) { - return -1; - } - } else { - try { - return httpURLConnection.getResponseCode(); - } catch (IOException e) { - return -1; - } + try { + return httpsURLConnection.getResponseCode(); + } catch (IOException e) { + return -1; } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java index aad2ac1..8620c7a 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java @@ -1975,7 +1975,7 @@ public class PeertubeAPI { private String getAbsoluteUrl(String action) { - return Helper.instanceWithProtocol(this.instance) + "/api/v1" + action; + return Helper.instanceWithProtocol(context) + "/api/v1" + action; } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java b/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java index 5b20339..1294a3c 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java +++ b/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java @@ -1,5 +1,6 @@ package app.fedilab.fedilabtube.helper; +import android.annotation.SuppressLint; import android.app.Activity; import android.app.DownloadManager; import android.content.Context; @@ -159,14 +160,14 @@ public class Helper { public static final String SET_EMBEDDED_BROWSER = "set_embedded_browser"; public static final String SET_CUSTOM_TABS = "set_custom_tabs"; public static final String SET_DISPLAY_CONFIRM = "set_display_confirm"; - + public static final String INTENT_ADD_UPLOADED_MEDIA = "intent_add_uploaded_media"; /** * Returns the peertube URL depending of the academic domain name * @param acad String academic domain name * @return String the peertube URL */ - public static String getPeertubeUrl(String acad) { - return "https://tube-"+acad.replace("ac\\-|\\.fr","")+".beta.education.fr/"; + private static String getPeertubeUrl(String acad) { + return "tube-"+acad.replaceAll("ac-|\\.fr","")+".beta.education.fr"; } @@ -178,19 +179,13 @@ public class Helper { */ public static String getLiveInstance(Context context) { final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); - return sharedpreferences.getString(Helper.PREF_INSTANCE, null); + String acad = sharedpreferences.getString(Helper.PREF_INSTANCE, "ac-lyon.fr"); + return getPeertubeUrl(acad); + } - - public static String getLiveInstanceWithProtocol(Context context) { - return instanceWithProtocol(getLiveInstance(context)); - } - - - public static String instanceWithProtocol(String instance) { - if (instance == null) - return null; - return "https://" + instance; + public static String instanceWithProtocol(Context context) { + return "https://"+getLiveInstance(context); } /** @@ -349,15 +344,11 @@ public class Helper { public static void loadGiF(final Context context, Account account, final ImageView imageView) { - SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); - String url = account.getAvatar(); if (url != null && url.startsWith("/")) { - url = Helper.getLiveInstanceWithProtocol(context) + url; + url = Helper.getLiveInstance(context) + url; } - - try { Glide.with(imageView.getContext()) .asDrawable() @@ -426,6 +417,7 @@ public class Helper { } + @SuppressLint("SetJavaScriptEnabled") public static CustomWebview initializeWebview(Activity activity, int webviewId, View rootView) { CustomWebview webView; diff --git a/app/src/main/java/app/fedilab/fedilabtube/services/PeertubeUploadReceiver.java b/app/src/main/java/app/fedilab/fedilabtube/services/PeertubeUploadReceiver.java new file mode 100644 index 0000000..717c01f --- /dev/null +++ b/app/src/main/java/app/fedilab/fedilabtube/services/PeertubeUploadReceiver.java @@ -0,0 +1,63 @@ +package app.fedilab.fedilabtube.services; + +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; + + +import androidx.localbroadcastmanager.content.LocalBroadcastManager; + +import net.gotev.uploadservice.ServerResponse; +import net.gotev.uploadservice.UploadInfo; +import net.gotev.uploadservice.UploadServiceBroadcastReceiver; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; + +import app.fedilab.fedilabtube.helper.Helper; + + +public class PeertubeUploadReceiver extends UploadServiceBroadcastReceiver { + + + @Override + public void onProgress(Context context, UploadInfo uploadInfo) { + // your code here + } + + @Override + public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse, + Exception exception) { + // your code here + } + + @Override + public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) { + try { + JSONObject response = new JSONObject(serverResponse.getBodyAsString()); + + if (!response.has("video")) { //IT's not from Peertube + ArrayList file = uploadInfo.getSuccessfullyUploadedFiles(); + Intent addMedia = new Intent(Helper.INTENT_ADD_UPLOADED_MEDIA); + addMedia.putExtra("response", serverResponse.getBodyAsString()); + addMedia.putStringArrayListExtra("uploadInfo", file); + LocalBroadcastManager.getInstance(context).sendBroadcast(addMedia); + } else { + String videoID = response.getJSONObject("video").get("id").toString(); + SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(Helper.VIDEO_ID, videoID); + editor.commit(); + } + } catch (JSONException e) { + e.printStackTrace(); + } + } + + @Override + public void onCancelled(Context context, UploadInfo uploadInfo) { + // your code here + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 80aba36..86c8df6 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -18,7 +18,7 @@ app:layout_constraintRight_toRightOf="parent" app:menu="@menu/bottom_nav_menu" /> - @@ -23,7 +23,7 @@ tools:layout="@layout/fragment_video"> @@ -34,7 +34,7 @@ tools:layout="@layout/fragment_video"> @@ -45,7 +45,7 @@ tools:layout="@layout/fragment_video"> @@ -56,7 +56,7 @@ tools:layout="@layout/fragment_video">