Mholloway has uploaded a new change for review. https://gerrit.wikimedia.org/r/299265
Change subject: Convert remaining HttpURLConnection usages to OkHttp ...................................................................... Convert remaining HttpURLConnection usages to OkHttp Most of our core API calls already use the up-to-date Request/Response API provided by OkHttp. But there were a few remaining calls using the HttpRequest library from earlier versions of the app. This patch brings them in line with the rest of the app. This completely removes the HttpRequest library from the app, so remove incidentals related to it. Also includes some light refactoring (use log.L in favor of Log, remove unused constants). Bug: T134904 Change-Id: I785eeb329718453818d6d224fa5c059ce37cad2e --- M app/proguard-rules.pro D app/src/main/assets/licenses/Http-Request M app/src/main/java/org/wikipedia/OkHttpConnectionFactory.java M app/src/main/java/org/wikipedia/RemoteConfigRefreshTask.java M app/src/main/java/org/wikipedia/WikipediaApp.java M app/src/main/java/org/wikipedia/alphaupdater/AlphaUpdateChecker.java M app/src/main/java/org/wikipedia/analytics/EventLoggingEvent.java M app/src/main/java/org/wikipedia/analytics/Funnel.java M app/src/main/java/org/wikipedia/recurring/RecurringTasksExecutor.java M app/src/main/java/org/wikipedia/savedpages/SavedPageSyncService.java M app/src/main/java/org/wikipedia/util/ThrowableUtil.java M app/src/main/res/values/credits.xml 12 files changed, 61 insertions(+), 102 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia refs/changes/65/299265/1 diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index cf588dc..c414278 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -37,8 +37,6 @@ -keep class uk.co.senab.photoview.** {*;} --keep class com.github.kevinsawicki.http.** {*;} - # --- Retrofit2 --- -dontwarn retrofit2.** -keep class retrofit2.** { *; } diff --git a/app/src/main/assets/licenses/Http-Request b/app/src/main/assets/licenses/Http-Request deleted file mode 100644 index 3407a78..0000000 --- a/app/src/main/assets/licenses/Http-Request +++ /dev/null @@ -1,19 +0,0 @@ -Copyright (c) 2014 Kevin Sawicki <kevinsawi...@gmail.com> - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to -deal in the Software without restriction, including without limitation the -rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -sell copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -IN THE SOFTWARE. diff --git a/app/src/main/java/org/wikipedia/OkHttpConnectionFactory.java b/app/src/main/java/org/wikipedia/OkHttpConnectionFactory.java index 8a18c10..21ad8c6 100644 --- a/app/src/main/java/org/wikipedia/OkHttpConnectionFactory.java +++ b/app/src/main/java/org/wikipedia/OkHttpConnectionFactory.java @@ -3,23 +3,15 @@ import android.content.Context; import android.support.annotation.NonNull; -import com.github.kevinsawicki.http.HttpRequest; - import org.wikipedia.settings.Prefs; - -import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.Proxy; -import java.net.URL; import okhttp3.Cache; import okhttp3.CookieJar; import okhttp3.JavaNetCookieJar; import okhttp3.OkHttpClient; -import okhttp3.OkUrlFactory; import okhttp3.logging.HttpLoggingInterceptor; -public class OkHttpConnectionFactory implements HttpRequest.ConnectionFactory { +public class OkHttpConnectionFactory { private static final long HTTP_CACHE_SIZE = 16 * 1024 * 1024; private static final Cache HTTP_CACHE = new Cache(WikipediaApp.getInstance().getCacheDir(), HTTP_CACHE_SIZE); @@ -27,17 +19,6 @@ public OkHttpConnectionFactory(@NonNull Context context) { client = createClient(context).build(); - } - - @Override - public HttpURLConnection create(URL url) throws IOException { - return new OkUrlFactory(client).open(url); // TODO: update to newer API - } - - @Override - public HttpURLConnection create(URL url, Proxy proxy) throws IOException { - throw new UnsupportedOperationException( - "Per-connection proxy is not supported. Use OkHttpClient's setProxy instead."); } public static OkHttpClient.Builder createClient(@NonNull Context context) { diff --git a/app/src/main/java/org/wikipedia/RemoteConfigRefreshTask.java b/app/src/main/java/org/wikipedia/RemoteConfigRefreshTask.java index 51df1c0..ce4d6d3 100644 --- a/app/src/main/java/org/wikipedia/RemoteConfigRefreshTask.java +++ b/app/src/main/java/org/wikipedia/RemoteConfigRefreshTask.java @@ -1,14 +1,16 @@ package org.wikipedia; -import android.support.annotation.NonNull; -import android.util.Log; -import com.github.kevinsawicki.http.HttpRequest; import org.json.JSONObject; import org.wikipedia.concurrency.SaneAsyncTask; import org.wikipedia.recurring.RecurringTask; import org.wikipedia.settings.RbSwitch; +import org.wikipedia.util.log.L; import java.util.Date; + +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; public class RemoteConfigRefreshTask extends RecurringTask { // Switch over to production when it is available @@ -16,12 +18,6 @@ // The 'l' suffix is needed because stupid Java overflows constants otherwise private static final long RUN_INTERVAL_MILLI = 24L * 60L * 60L * 1000L; // Once a day! - - @NonNull private final WikipediaApp app; - - public RemoteConfigRefreshTask(@NonNull WikipediaApp app) { - this.app = app; - } @Override protected boolean shouldRun(Date lastRun) { @@ -33,17 +29,20 @@ new SaneAsyncTask<Boolean>() { @Override public Boolean performTask() throws Throwable { - JSONObject config = new JSONObject(HttpRequest.get(REMOTE_CONFIG_URL).body()); - app.getRemoteConfig().updateConfig(config); + OkHttpClient client = new OkHttpClient(); + Request request = new Request.Builder().url(REMOTE_CONFIG_URL).build(); + Response response = client.newCall(request).execute(); + JSONObject config = new JSONObject(response.body().string()); + WikipediaApp.getInstance().getRemoteConfig().updateConfig(config); RbSwitch.INSTANCE.update(); - Log.d("Wikipedia", config.toString()); + L.d(config.toString()); return true; } @Override public void onCatch(Throwable caught) { // Don't do anything, but do write out a log statement. We don't particularly care. - Log.d("Wikipedia", "Caught " + caught.toString()); + L.d("Caught " + caught.toString()); } }.execute(); } diff --git a/app/src/main/java/org/wikipedia/WikipediaApp.java b/app/src/main/java/org/wikipedia/WikipediaApp.java index dd71928..f274cd2 100644 --- a/app/src/main/java/org/wikipedia/WikipediaApp.java +++ b/app/src/main/java/org/wikipedia/WikipediaApp.java @@ -182,7 +182,6 @@ enableWebViewDebugging(); OkHttpConnectionFactory okHttpConnectionFactory = new OkHttpConnectionFactory(this); - Api.setConnectionFactory(okHttpConnectionFactory); ImagePipelineConfig config = OkHttpImagePipelineConfigFactory .newBuilder(this, okHttpConnectionFactory.client()) diff --git a/app/src/main/java/org/wikipedia/alphaupdater/AlphaUpdateChecker.java b/app/src/main/java/org/wikipedia/alphaupdater/AlphaUpdateChecker.java index 561281b..4034f4a 100644 --- a/app/src/main/java/org/wikipedia/alphaupdater/AlphaUpdateChecker.java +++ b/app/src/main/java/org/wikipedia/alphaupdater/AlphaUpdateChecker.java @@ -2,7 +2,6 @@ import org.wikipedia.R; import org.wikipedia.recurring.RecurringTask; -import com.github.kevinsawicki.http.HttpRequest; import org.json.JSONException; import org.json.JSONObject; import android.app.Notification; @@ -15,12 +14,20 @@ import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.v4.app.NotificationCompat; + +import java.io.IOException; import java.util.Date; + +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; public class AlphaUpdateChecker extends RecurringTask { private static final long RUN_INTERVAL_MILLI = 24L * 60L * 60L * 1000L; // Once a day! private static final String PREFERENCE_KEY_ALPHA_COMMIT = "alpha_last_checked_commit"; + private static final String ALPHA_BUILD_APK_URL = "https://android-builds.wmflabs.org/runs/latest/wikipedia.apk"; + private static final String ALPHA_BUILD_DATA_URL = "https://android-builds.wmflabs.org/runs/latest/meta.json"; @NonNull private final Context context; public AlphaUpdateChecker(@NonNull Context context) { @@ -34,33 +41,31 @@ @Override protected void run(Date lastRun) { + + // Check for updates! - String responseJSON; + JSONObject config; try { - HttpRequest request = HttpRequest.get("https://android-builds.wmflabs.org/runs/latest/meta.json"); - responseJSON = request.body(); - } catch (HttpRequest.HttpRequestException | SecurityException e) { + OkHttpClient client = new OkHttpClient(); + Request request = new Request.Builder().url(ALPHA_BUILD_DATA_URL).build(); + Response response = client.newCall(request).execute(); + config = new JSONObject(response.body().string()); + } catch (IOException | JSONException e) { // It's ok, we can do nothing. return; } - JSONObject meta; - try { - meta = new JSONObject(responseJSON); - } catch (JSONException e) { - throw new RuntimeException(e); - } SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); if (prefs.contains(PREFERENCE_KEY_ALPHA_COMMIT)) { - if (!prefs.getString(PREFERENCE_KEY_ALPHA_COMMIT, "").equals(meta.optString("commit_hash", ""))) { + if (!prefs.getString(PREFERENCE_KEY_ALPHA_COMMIT, "").equals(config.optString("commit_hash", ""))) { showNotification(); } } - prefs.edit().putString(PREFERENCE_KEY_ALPHA_COMMIT, meta.optString("commit_hash")).apply(); + prefs.edit().putString(PREFERENCE_KEY_ALPHA_COMMIT, config.optString("commit_hash")).apply(); } private void showNotification() { - Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://android-builds.wmflabs.org/runs/latest/wikipedia.apk")); + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(ALPHA_BUILD_APK_URL)); PendingIntent pintent = PendingIntent.getActivity(context, 0, intent, 0); Notification notification = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_notification) diff --git a/app/src/main/java/org/wikipedia/analytics/EventLoggingEvent.java b/app/src/main/java/org/wikipedia/analytics/EventLoggingEvent.java index dfd4437..7b7ec19 100644 --- a/app/src/main/java/org/wikipedia/analytics/EventLoggingEvent.java +++ b/app/src/main/java/org/wikipedia/analytics/EventLoggingEvent.java @@ -1,12 +1,15 @@ package org.wikipedia.analytics; import android.net.Uri; -import android.util.Log; -import com.github.kevinsawicki.http.HttpRequest; import org.json.JSONException; import org.json.JSONObject; import org.wikipedia.WikipediaApp; import org.wikipedia.concurrency.SaneAsyncTask; +import org.wikipedia.util.log.L; + +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; /** * Base class for all various types of events that are logged to EventLogging. @@ -59,23 +62,26 @@ private class LogEventTask extends SaneAsyncTask<Integer> { private final JSONObject data; + LogEventTask(JSONObject data) { this.data = data; } @Override public Integer performTask() throws Throwable { - String elUrl = EVENTLOG_URL; - String dataURL = Uri.parse(elUrl) + String dataURL = Uri.parse(EVENTLOG_URL) .buildUpon().query(data.toString()) .build().toString(); - return HttpRequest.get(dataURL).header("User-Agent", userAgent).code(); + OkHttpClient client = new OkHttpClient(); + Request request = new Request.Builder().url(dataURL).header("User-Agent", userAgent).build(); + Response response = client.newCall(request).execute(); + return response.code(); } @Override public void onCatch(Throwable caught) { // Do nothing bad. EL data is ok to lose. - Log.d(Funnel.ANALYTICS_TAG, "Lost EL data: " + data.toString()); + L.d("Lost EL data: " + data.toString()); } } } diff --git a/app/src/main/java/org/wikipedia/analytics/Funnel.java b/app/src/main/java/org/wikipedia/analytics/Funnel.java index f656408..f8dd5de 100644 --- a/app/src/main/java/org/wikipedia/analytics/Funnel.java +++ b/app/src/main/java/org/wikipedia/analytics/Funnel.java @@ -2,11 +2,11 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import android.util.Log; import org.json.JSONException; import org.json.JSONObject; import org.wikipedia.Site; import org.wikipedia.WikipediaApp; +import org.wikipedia.util.log.L; import java.util.UUID; @@ -30,11 +30,6 @@ @Nullable private final Site site; private final String sessionToken = UUID.randomUUID().toString(); - - /** - * The tag used for any analytics-related events sent to the Log. - */ - public static final String ANALYTICS_TAG = "Analytics"; /*package*/ Funnel(WikipediaApp app, String schemaName, int revision) { this(app, schemaName, revision, SAMPLE_LOG_ALL); @@ -142,7 +137,7 @@ preprocessData(eventData, params[i].toString(), params[i + 1]); logString += ", event_" + params[i] + " = " + params[i + 1]; } - Log.d(ANALYTICS_TAG, logString); + L.d(logString); new EventLoggingEvent( schemaName, diff --git a/app/src/main/java/org/wikipedia/recurring/RecurringTasksExecutor.java b/app/src/main/java/org/wikipedia/recurring/RecurringTasksExecutor.java index 62a11e2..163a52f 100644 --- a/app/src/main/java/org/wikipedia/recurring/RecurringTasksExecutor.java +++ b/app/src/main/java/org/wikipedia/recurring/RecurringTasksExecutor.java @@ -19,7 +19,7 @@ public Void performTask() throws Throwable { RecurringTask[] allTasks = new RecurringTask[] { // Has list of all rotating tasks that need to be run - new RemoteConfigRefreshTask(app), + new RemoteConfigRefreshTask(), new SharedImageCleanupTask(app), new DailyEventTask(app) }; diff --git a/app/src/main/java/org/wikipedia/savedpages/SavedPageSyncService.java b/app/src/main/java/org/wikipedia/savedpages/SavedPageSyncService.java index 4ff0b96..a2a7220 100644 --- a/app/src/main/java/org/wikipedia/savedpages/SavedPageSyncService.java +++ b/app/src/main/java/org/wikipedia/savedpages/SavedPageSyncService.java @@ -6,8 +6,6 @@ import android.support.annotation.Nullable; import android.support.annotation.VisibleForTesting; -import com.github.kevinsawicki.http.HttpRequest; - import org.wikipedia.WikipediaApp; import org.wikipedia.page.Page; import org.wikipedia.page.PageTitle; @@ -28,11 +26,10 @@ import java.util.List; import java.util.Map; -import static org.wikipedia.readinglist.page.database.disk.DiskStatus.DELETED; -import static org.wikipedia.readinglist.page.database.disk.DiskStatus.ONLINE; -import static org.wikipedia.readinglist.page.database.disk.DiskStatus.OUTDATED; -import static org.wikipedia.readinglist.page.database.disk.DiskStatus.SAVED; -import static org.wikipedia.readinglist.page.database.disk.DiskStatus.UNSAVED; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + import static org.wikipedia.util.FileUtil.writeFile; public class SavedPageSyncService extends IntentService { @@ -153,12 +150,16 @@ return true; } - HttpRequest request = HttpRequest.get(url).userAgent(WikipediaApp.getInstance() - .getUserAgent()); + OkHttpClient client = new OkHttpClient(); + Request request = new Request.Builder().url(url) + .header("User-Agent", WikipediaApp.getInstance().getUserAgent()) + .build(); + Response response = client.newCall(request).execute(); + try { - if (request.ok()) { - InputStream response = request.stream(); - writeFile(response, file); + if (response.isSuccessful()) { + InputStream stream = response.body().byteStream(); + writeFile(stream, file); response.close(); L.v("downloaded image " + url + " to " + file.getAbsolutePath()); return true; diff --git a/app/src/main/java/org/wikipedia/util/ThrowableUtil.java b/app/src/main/java/org/wikipedia/util/ThrowableUtil.java index d3a410e..16d24e2 100644 --- a/app/src/main/java/org/wikipedia/util/ThrowableUtil.java +++ b/app/src/main/java/org/wikipedia/util/ThrowableUtil.java @@ -2,7 +2,6 @@ import org.wikipedia.R; import org.mediawiki.api.json.ApiException; -import com.github.kevinsawicki.http.HttpRequest; import org.json.JSONException; import android.content.Context; @@ -73,8 +72,7 @@ } private static boolean isNetworkError(@NonNull Throwable e) { - return ThrowableUtil.throwableContainsException(e, HttpRequest.HttpRequestException.class) - || ThrowableUtil.throwableContainsException(e, UnknownHostException.class) + return ThrowableUtil.throwableContainsException(e, UnknownHostException.class) || ThrowableUtil.throwableContainsException(e, TimeoutException.class) || ThrowableUtil.throwableContainsException(e, SSLException.class); } diff --git a/app/src/main/res/values/credits.xml b/app/src/main/res/values/credits.xml index 7bb2410..5bb59a5 100644 --- a/app/src/main/res/values/credits.xml +++ b/app/src/main/res/values/credits.xml @@ -21,10 +21,6 @@ <!-- https://raw.githubusercontent.com/bitstadium/HockeySDK-Android/develop/LICENSE --> (<a href="file:///android_asset/licenses/HockeySDK-Android">license</a>), - <a href="https://github.com/kevinsawicki/http-request">Http-Request</a> - <!-- https://raw.githubusercontent.com/kevinsawicki/http-request/master/LICENSE.md --> - (<a href="file:///android_asset/licenses/Http-Request">license</a>), - <a href="https://github.com/mapbox/mapbox-android-sdk">Mapbox Android SDK</a> <!-- end of https://github.com/mapbox/mapbox-android-sdk/blob/mb-pages/LICENSE.md --> (<a href="file:///android_asset/licenses/Mapbox">license</a>), -- To view, visit https://gerrit.wikimedia.org/r/299265 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I785eeb329718453818d6d224fa5c059ce37cad2e Gerrit-PatchSet: 1 Gerrit-Project: apps/android/wikipedia Gerrit-Branch: master Gerrit-Owner: Mholloway <mhollo...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits