From 7c3d02c986f1dfe5bedfb7e8f36865cf939a017a Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Tue, 16 Feb 2016 13:18:51 +0800 Subject: [PATCH] changed stream based json deserialization to reader based json deserialization updated library version --- twidere.component.common/build.gradle | 2 +- twidere/build.gradle | 6 +- .../twidere/util/TwidereTypeUtilsTest.java | 24 ++++ .../twitter/util/TwitterConverterFactory.java | 45 +++++-- .../fragment/NetworkDiagnosticsFragment.java | 127 ++++++++++++++---- .../twidere/util/HttpClientFactory.java | 5 +- .../util/OAuthPasswordAuthenticator.java | 2 +- .../twidere/util/TwidereTypeUtils.java | 33 +++++ .../twidere/util/TwitterAPIFactory.java | 8 +- 9 files changed, 195 insertions(+), 57 deletions(-) create mode 100644 twidere/src/androidTest/java/org/mariotaku/twidere/util/TwidereTypeUtilsTest.java rename {twidere.component.common => twidere}/src/main/java/org/mariotaku/twidere/api/twitter/util/TwitterConverterFactory.java (72%) create mode 100644 twidere/src/main/java/org/mariotaku/twidere/util/TwidereTypeUtils.java diff --git a/twidere.component.common/build.gradle b/twidere.component.common/build.gradle index d822b430a..e77e9e0fa 100644 --- a/twidere.component.common/build.gradle +++ b/twidere.component.common/build.gradle @@ -43,7 +43,7 @@ dependencies { compile 'com.android.support:support-v4:23.1.1' compile 'com.bluelinelabs:logansquare:1.3.4' compile 'org.apache.commons:commons-lang3:3.4' - compile 'com.github.mariotaku.RestFu:library:0.9.19' + compile 'com.github.mariotaku.RestFu:library:0.9.20' compile 'com.hannesdorfmann.parcelableplease:annotation:1.0.2' compile 'com.github.mariotaku.ObjectCursor:core:0.9.3' compile fileTree(dir: 'libs', include: ['*.jar']) diff --git a/twidere/build.gradle b/twidere/build.gradle index 56a8ae7ee..bb2ced576 100644 --- a/twidere/build.gradle +++ b/twidere/build.gradle @@ -23,7 +23,7 @@ android { applicationId "org.mariotaku.twidere" minSdkVersion 14 targetSdkVersion 23 - versionCode 144 + versionCode 145 versionName "3.0.5 (snapshot)" multiDexEnabled true @@ -107,8 +107,8 @@ dependencies { compile 'com.soundcloud.android:android-crop:1.0.1@aar' compile 'com.hannesdorfmann.parcelableplease:annotation:1.0.2' compile 'com.github.mariotaku:PickNCrop:0.9.3' - compile 'com.github.mariotaku.RestFu:library:0.9.19' - compile 'com.github.mariotaku.RestFu:okhttp3:0.9.19' + compile 'com.github.mariotaku.RestFu:library:0.9.20' + compile 'com.github.mariotaku.RestFu:okhttp3:0.9.20' compile 'com.squareup.okhttp3:okhttp:3.1.2' compile 'com.lnikkila:extendedtouchview:0.1.0' compile 'com.google.dagger:dagger:2.0.2' diff --git a/twidere/src/androidTest/java/org/mariotaku/twidere/util/TwidereTypeUtilsTest.java b/twidere/src/androidTest/java/org/mariotaku/twidere/util/TwidereTypeUtilsTest.java new file mode 100644 index 000000000..967ffe07e --- /dev/null +++ b/twidere/src/androidTest/java/org/mariotaku/twidere/util/TwidereTypeUtilsTest.java @@ -0,0 +1,24 @@ +package org.mariotaku.twidere.util; + +import org.apache.commons.lang3.reflect.TypeUtils; +import org.junit.Test; +import org.mariotaku.twidere.api.twitter.model.CursorTimestampResponse; +import org.mariotaku.twidere.api.twitter.model.ResponseList; +import org.mariotaku.twidere.api.twitter.model.Status; + +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * Created by mariotaku on 16/2/15. + */ +public class TwidereTypeUtilsTest { + + @Test + public void testGetSimpleName() throws Exception { + assertEquals("CursorTimestampResponse", TwidereTypeUtils.toSimpleName(CursorTimestampResponse.class)); + assertEquals("ResponseList", TwidereTypeUtils.toSimpleName(TypeUtils.parameterize(ResponseList.class, Status.class))); + assertEquals("List>", TwidereTypeUtils.toSimpleName(TypeUtils.parameterize(List.class, TypeUtils.parameterize(List.class, Object.class)))); + } +} \ No newline at end of file diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/util/TwitterConverterFactory.java b/twidere/src/main/java/org/mariotaku/twidere/api/twitter/util/TwitterConverterFactory.java similarity index 72% rename from twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/util/TwitterConverterFactory.java rename to twidere/src/main/java/org/mariotaku/twidere/api/twitter/util/TwitterConverterFactory.java index b3a2c608e..497cf5634 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/util/TwitterConverterFactory.java +++ b/twidere/src/main/java/org/mariotaku/twidere/api/twitter/util/TwitterConverterFactory.java @@ -21,7 +21,10 @@ package org.mariotaku.twidere.api.twitter.util; import android.support.annotation.NonNull; import android.support.v4.util.SimpleArrayMap; +import android.util.Log; +import android.util.TimingLogger; +import com.bluelinelabs.logansquare.JsonMapper; import com.bluelinelabs.logansquare.LoganSquare; import com.bluelinelabs.logansquare.ParameterizedType; import com.bluelinelabs.logansquare.ParameterizedTypeAccessor; @@ -30,14 +33,16 @@ import com.fasterxml.jackson.core.JsonParseException; import org.mariotaku.restfu.RestConverter; import org.mariotaku.restfu.http.HttpResponse; import org.mariotaku.restfu.http.mime.Body; +import org.mariotaku.restfu.http.mime.SimpleBody; +import org.mariotaku.twidere.BuildConfig; import org.mariotaku.twidere.api.twitter.TwitterException; import org.mariotaku.twidere.api.twitter.auth.OAuthToken; import org.mariotaku.twidere.api.twitter.model.ResponseCode; import org.mariotaku.twidere.api.twitter.model.TwitterResponse; -import org.mariotaku.twidere.api.twitter.model.User; +import org.mariotaku.twidere.util.TwidereTypeUtils; import java.io.IOException; -import java.io.InputStream; +import java.io.Reader; import java.lang.reflect.Type; /** @@ -57,7 +62,9 @@ public class TwitterConverterFactory extends RestConverter.SimpleFactory mapper = LoganSquare.mapperFor(TwitterException.class); + final Reader reader = SimpleBody.reader(body); + final TwitterException parse = mapper.parse(LoganSquare.JSON_FACTORY.createParser(reader)); if (parse != null) return parse; return new TwitterException(resp); } catch (JsonParseException e) { @@ -68,11 +75,22 @@ public class TwitterConverterFactory extends RestConverter.SimpleFactory T parseOrThrow(InputStream stream, Type type) + private static T parseOrThrow(Body body, Type type) throws IOException, TwitterException, RestConverter.ConvertException { try { + if (BuildConfig.DEBUG) { + Log.d("TwitterConverter", TwidereTypeUtils.toSimpleName(type) + " <---"); + } final ParameterizedType parameterizedType = ParameterizedTypeAccessor.create(type); - final T parsed = LoganSquare.parse(stream, parameterizedType); + final JsonMapper mapper = LoganSquare.mapperFor(parameterizedType); + if (BuildConfig.DEBUG) { + Log.d("TwitterConverter", TwidereTypeUtils.toSimpleName(type) + " ---> " + TwidereTypeUtils.toSimpleName(mapper.getClass())); + } + final Reader reader = SimpleBody.reader(body); + final T parsed = mapper.parse(LoganSquare.JSON_FACTORY.createParser(reader)); + if (BuildConfig.DEBUG) { + Log.d("TwitterConverter", TwidereTypeUtils.toSimpleName(type) + " Finished"); + } if (parsed == null) { throw new TwitterException("Empty data"); } @@ -82,12 +100,6 @@ public class TwitterConverterFactory extends RestConverter.SimpleFactory forResponse(Type type) { RestConverter converter = sResponseConverters.get(type); @@ -121,13 +133,18 @@ public class TwitterConverterFactory extends RestConverter.SimpleFactory { @@ -108,21 +126,10 @@ public class NetworkDiagnosticsFragment extends BaseFragment { @Override protected Object doInBackground(Object... params) { - publishProgress(new LogText("Build information: ")); - publishProgress(new LogText("version_code: " + BuildConfig.VERSION_CODE), LogText.LINEBREAK); - publishProgress(new LogText("version_name: " + BuildConfig.VERSION_NAME), LogText.LINEBREAK); - publishProgress(new LogText("flavor: " + BuildConfig.FLAVOR), LogText.LINEBREAK); - publishProgress(new LogText("debug: " + BuildConfig.DEBUG), LogText.LINEBREAK); - publishProgress(LogText.LINEBREAK); - publishProgress(new LogText("Basic system information: ")); - publishProgress(new LogText(String.valueOf(mContext.getResources().getConfiguration()))); + publishProgress(new LogText("**** NOTICE ****", LogText.State.WARNING)); publishProgress(LogText.LINEBREAK, LogText.LINEBREAK); - publishProgress(new LogText("Active network info: ")); - publishProgress(new LogText(String.valueOf(mConnectivityManager.getActiveNetworkInfo()))); - publishProgress(LogText.LINEBREAK, LogText.LINEBREAK); - publishProgress(new LogText("**** NOTICE ****")); - publishProgress(LogText.LINEBREAK, LogText.LINEBREAK); - publishProgress(new LogText("Text below may have personal information, BE CAREFUL TO MAKE IT PUBLIC")); + publishProgress(new LogText("Text below may have personal information, BE CAREFUL TO MAKE IT PUBLIC", + LogText.State.WARNING)); publishProgress(LogText.LINEBREAK, LogText.LINEBREAK); DependencyHolder holder = DependencyHolder.get(mContext); final TwidereDns dns = holder.getDns(); @@ -164,12 +171,47 @@ public class NetworkDiagnosticsFragment extends BaseFragment { testDns(dns, host); testNativeLookup(host); } else { - publishProgress(new LogText("API URL format is invalid", LogText.State.BAD)); + publishProgress(new LogText("API URL format is invalid", LogText.State.ERROR)); publishProgress(LogText.LINEBREAK); } publishProgress(LogText.LINEBREAK); + publishProgress(new LogText("Testing Network connectivity")); + publishProgress(LogText.LINEBREAK); + + final String baseUrl = TwitterAPIFactory.getApiBaseUrl(credentials.api_url_format, "api"); + RestHttpClient client = RestAPIFactory.getRestClient(twitter).getRestClient(); + HttpResponse response = null; + try { + publishProgress(new LogText("Connecting to " + baseUrl + "...")); + HttpRequest.Builder builder = new HttpRequest.Builder(); + builder.method(GET.METHOD); + builder.url(baseUrl); + final long start = SystemClock.uptimeMillis(); + response = client.newCall(builder.build()).execute(); + publishProgress(new LogText(String.format(" OK (%d ms)", SystemClock.uptimeMillis() + - start), LogText.State.OK)); + } catch (IOException e) { + publishProgress(new LogText("ERROR: " + e.getMessage(), LogText.State.ERROR)); + } + publishProgress(LogText.LINEBREAK); + try { + if (response != null) { + publishProgress(new LogText("Reading response...")); + final long start = SystemClock.uptimeMillis(); + final CountOutputStream os = new CountOutputStream(); + response.getBody().writeTo(os); + publishProgress(new LogText(String.format(" %d bytes (%d ms)", os.getTotal(), + SystemClock.uptimeMillis() - start), LogText.State.OK)); + } + } catch (IOException e) { + publishProgress(new LogText("ERROR: " + e.getMessage(), LogText.State.ERROR)); + } finally { + Utils.closeSilently(response); + } + publishProgress(LogText.LINEBREAK, LogText.LINEBREAK); + publishProgress(new LogText("Testing API functionality")); publishProgress(LogText.LINEBREAK); testTwitter("verify_credentials", twitter, new TwitterTest() { @@ -202,6 +244,20 @@ public class NetworkDiagnosticsFragment extends BaseFragment { testNativeLookup("twitter.com"); publishProgress(LogText.LINEBREAK, LogText.LINEBREAK); + + publishProgress(new LogText("Build information: ")); + publishProgress(new LogText("version_code: " + BuildConfig.VERSION_CODE), LogText.LINEBREAK); + publishProgress(new LogText("version_name: " + BuildConfig.VERSION_NAME), LogText.LINEBREAK); + publishProgress(new LogText("flavor: " + BuildConfig.FLAVOR), LogText.LINEBREAK); + publishProgress(new LogText("debug: " + BuildConfig.DEBUG), LogText.LINEBREAK); + publishProgress(LogText.LINEBREAK); + publishProgress(new LogText("Basic system information: ")); + publishProgress(new LogText(String.valueOf(mContext.getResources().getConfiguration()))); + publishProgress(LogText.LINEBREAK, LogText.LINEBREAK); + publishProgress(new LogText("Active network info: ")); + publishProgress(new LogText(String.valueOf(mConnectivityManager.getActiveNetworkInfo()))); + publishProgress(LogText.LINEBREAK, LogText.LINEBREAK); + publishProgress(new LogText("Done. You can send this log to me, and I'll contact you to solve related issue.")); return null; } @@ -212,9 +268,9 @@ public class NetworkDiagnosticsFragment extends BaseFragment { final long start = SystemClock.uptimeMillis(); publishProgress(new LogText(String.valueOf(dns.lookupResolver(host)))); publishProgress(new LogText(String.format(" OK (%d ms)", SystemClock.uptimeMillis() - - start), LogText.State.GOOD)); + - start), LogText.State.OK)); } catch (UnknownHostException e) { - publishProgress(new LogText("ERROR: " + e.getMessage(), LogText.State.BAD)); + publishProgress(new LogText("ERROR: " + e.getMessage(), LogText.State.ERROR)); } publishProgress(LogText.LINEBREAK); } @@ -225,9 +281,9 @@ public class NetworkDiagnosticsFragment extends BaseFragment { final long start = SystemClock.uptimeMillis(); publishProgress(new LogText(Arrays.toString(InetAddress.getAllByName(host)))); publishProgress(new LogText(String.format(" OK (%d ms)", SystemClock.uptimeMillis() - - start), LogText.State.GOOD)); + - start), LogText.State.OK)); } catch (UnknownHostException e) { - publishProgress(new LogText("ERROR: " + e.getMessage(), LogText.State.BAD)); + publishProgress(new LogText("ERROR: " + e.getMessage(), LogText.State.ERROR)); } publishProgress(LogText.LINEBREAK); } @@ -238,9 +294,9 @@ public class NetworkDiagnosticsFragment extends BaseFragment { final long start = SystemClock.uptimeMillis(); test.execute(twitter); publishProgress(new LogText(String.format("OK (%d ms)", SystemClock.uptimeMillis() - - start), LogText.State.GOOD)); + - start), LogText.State.OK)); } catch (TwitterException e) { - publishProgress(new LogText("ERROR: " + e.getMessage(), LogText.State.BAD)); + publishProgress(new LogText("ERROR: " + e.getMessage(), LogText.State.ERROR)); } publishProgress(LogText.LINEBREAK); } @@ -311,12 +367,25 @@ public class NetworkDiagnosticsFragment extends BaseFragment { this.message = message; } - @IntDef({State.DEFAULT, State.GOOD, State.BAD}) + @IntDef({State.DEFAULT, State.OK, State.ERROR, State.WARNING}) @interface State { int DEFAULT = 0; - int GOOD = 1; - int BAD = 2; + int OK = 1; + int ERROR = 2; + int WARNING = 3; } } + private static class CountOutputStream extends OutputStream { + private long total; + + public long getTotal() { + return total; + } + + @Override + public void write(int oneByte) throws IOException { + total++; + } + } } diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/HttpClientFactory.java b/twidere/src/main/java/org/mariotaku/twidere/util/HttpClientFactory.java index 7643726b5..d8c8e6920 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/HttpClientFactory.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/HttpClientFactory.java @@ -7,7 +7,7 @@ import android.text.TextUtils; import org.apache.commons.lang3.math.NumberUtils; import org.mariotaku.restfu.http.RestHttpClient; -import org.mariotaku.restfu.okhttp.OkHttpRestClient; +import org.mariotaku.restfu.okhttp3.OkHttpRestClient; import org.mariotaku.twidere.Constants; import org.mariotaku.twidere.util.dagger.DependencyHolder; import org.mariotaku.twidere.util.net.TwidereProxySelector; @@ -101,8 +101,7 @@ public class HttpClientFactory implements Constants { } public static void reloadConnectivitySettings(Context context) { - DependencyHolder holder = DependencyHolder.get(context); - holder.getConnectionPoll().evictAll(); + final DependencyHolder holder = DependencyHolder.get(context); final RestHttpClient client = holder.getRestHttpClient(); if (client instanceof OkHttpRestClient) { final OkHttpClient.Builder builder = new OkHttpClient.Builder(); diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/OAuthPasswordAuthenticator.java b/twidere/src/main/java/org/mariotaku/twidere/util/OAuthPasswordAuthenticator.java index 4ce17e076..ffae5bc1b 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/OAuthPasswordAuthenticator.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/OAuthPasswordAuthenticator.java @@ -40,7 +40,7 @@ import org.mariotaku.restfu.http.MultiValueMap; import org.mariotaku.restfu.http.RestHttpClient; import org.mariotaku.restfu.http.mime.FormBody; import org.mariotaku.restfu.http.mime.SimpleBody; -import org.mariotaku.restfu.okhttp.OkHttpRestClient; +import org.mariotaku.restfu.okhttp3.OkHttpRestClient; import org.mariotaku.twidere.Constants; import org.mariotaku.twidere.api.twitter.TwitterException; import org.mariotaku.twidere.api.twitter.TwitterOAuth; diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/TwidereTypeUtils.java b/twidere/src/main/java/org/mariotaku/twidere/util/TwidereTypeUtils.java new file mode 100644 index 000000000..01f661d99 --- /dev/null +++ b/twidere/src/main/java/org/mariotaku/twidere/util/TwidereTypeUtils.java @@ -0,0 +1,33 @@ +package org.mariotaku.twidere.util; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + +/** + * Created by mariotaku on 16/2/15. + */ +public class TwidereTypeUtils { + + public static String toSimpleName(Type type) { + final StringBuilder sb = new StringBuilder(); + buildSimpleName(type, sb); + return sb.toString(); + } + + private static void buildSimpleName(Type type, StringBuilder sb) { + if (type instanceof Class) { + sb.append(((Class) type).getSimpleName()); + } else if (type instanceof ParameterizedType) { + buildSimpleName(((ParameterizedType) type).getRawType(), sb); + sb.append("<"); + final Type[] args = ((ParameterizedType) type).getActualTypeArguments(); + for (int i = 0; i < args.length; i++) { + if (i != 0) { + sb.append(","); + } + buildSimpleName(args[i], sb); + } + sb.append(">"); + } + } +} diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/TwitterAPIFactory.java b/twidere/src/main/java/org/mariotaku/twidere/util/TwitterAPIFactory.java index 2cf5a6b3f..045482db4 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/TwitterAPIFactory.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/TwitterAPIFactory.java @@ -43,7 +43,6 @@ import org.mariotaku.twidere.model.ConsumerKeyType; import org.mariotaku.twidere.model.ParcelableCredentials; import org.mariotaku.twidere.provider.TwidereDataStore; import org.mariotaku.twidere.util.dagger.DependencyHolder; -import org.mariotaku.twidere.util.net.TwidereDns; import java.io.IOException; import java.util.HashMap; @@ -143,7 +142,7 @@ public class TwitterAPIFactory implements TwidereConstants { factory.setConstantPool(sConstantPoll); factory.setRestConverterFactory(new TwitterConverterFactory()); factory.setHttpRequestFactory(new TwidereHttpRequestFactory(userAgent)); - factory.setExceptionFactory(new TwidereExceptionFactory(holder.getDns())); + factory.setExceptionFactory(new TwidereExceptionFactory()); return factory.build(cls); } @@ -440,10 +439,7 @@ public class TwitterAPIFactory implements TwidereConstants { public static class TwidereExceptionFactory implements ExceptionFactory { - private final TwidereDns dns; - - TwidereExceptionFactory(TwidereDns dns) { - this.dns = dns; + TwidereExceptionFactory() { } @Override