updated stetho

This commit is contained in:
Mariotaku Lee 2016-02-05 17:32:07 +08:00
parent e048f47a53
commit 8b86056492
3 changed files with 48 additions and 34 deletions

View File

@ -44,7 +44,7 @@ public class DebugModeUtils {
private static RefWatcher sRefWatcher; private static RefWatcher sRefWatcher;
public static void initForHttpClient(final OkHttpClient.Builder builder) { public static void initForHttpClient(final OkHttpClient.Builder builder) {
builder.addInterceptor(new StethoInterceptor()); builder.addNetworkInterceptor(new StethoInterceptor());
} }
public static void initForApplication(final Application application) { public static void initForApplication(final Application application) {

View File

@ -66,6 +66,7 @@ import com.rengwuxian.materialedittext.MaterialEditText;
import org.mariotaku.restfu.http.Authorization; import org.mariotaku.restfu.http.Authorization;
import org.mariotaku.restfu.http.Endpoint; import org.mariotaku.restfu.http.Endpoint;
import org.mariotaku.sqliteqb.library.Expression; import org.mariotaku.sqliteqb.library.Expression;
import org.mariotaku.twidere.BuildConfig;
import org.mariotaku.twidere.R; import org.mariotaku.twidere.R;
import org.mariotaku.twidere.activity.SettingsActivity; import org.mariotaku.twidere.activity.SettingsActivity;
import org.mariotaku.twidere.api.twitter.Twitter; import org.mariotaku.twidere.api.twitter.Twitter;
@ -460,6 +461,9 @@ public class SignInActivity extends BaseAppCompatActivity implements OnClickList
startActivity(intent); startActivity(intent);
finish(); finish();
} else { } else {
if (BuildConfig.DEBUG) {
Log.w(LOGTAG, result.exception);
}
if (result.exception instanceof AuthenticityTokenException) { if (result.exception instanceof AuthenticityTokenException) {
Toast.makeText(this, R.string.wrong_api_key, Toast.LENGTH_SHORT).show(); Toast.makeText(this, R.string.wrong_api_key, Toast.LENGTH_SHORT).show();
} else if (result.exception instanceof WrongUserPassException) { } else if (result.exception instanceof WrongUserPassException) {

View File

@ -72,42 +72,15 @@ public class OAuthPasswordAuthenticator implements Constants {
final String userAgent) { final String userAgent) {
final RestClient restClient = RestAPIFactory.getRestClient(oauth); final RestClient restClient = RestAPIFactory.getRestClient(oauth);
this.oauth = oauth; this.oauth = oauth;
this.client = (OkHttpRestClient) restClient.getRestClient();
final OkHttpClient.Builder builder = client.getClient().newBuilder();
builder.cookieJar(new SimpleCookieJar());
builder.addNetworkInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
final Response response = chain.proceed(chain.request());
if (!response.isRedirect()) {
return response;
}
final String location = response.header("Location");
final Response.Builder builder = response.newBuilder();
if (!TextUtils.isEmpty(location) && !endpoint.checkEndpoint(location)) {
final HttpUrl originalLocation = HttpUrl.get(URI.create("https://api.twitter.com/").resolve(location));
final HttpUrl.Builder locationBuilder = HttpUrl.parse(endpoint.getUrl()).newBuilder();
for (String pathSegments : originalLocation.pathSegments()) {
locationBuilder.addPathSegment(pathSegments);
}
for (int i = 0, j = originalLocation.querySize(); i < j; i++) {
final String name = originalLocation.queryParameterName(i);
final String value = originalLocation.queryParameterValue(i);
locationBuilder.addQueryParameter(name, value);
}
final String encodedFragment = originalLocation.encodedFragment();
if (encodedFragment != null) {
locationBuilder.encodedFragment(encodedFragment);
}
final HttpUrl newLocation = locationBuilder.build();
builder.header("Location", newLocation.toString());
}
return builder.build();
}
});
this.endpoint = restClient.getEndpoint(); this.endpoint = restClient.getEndpoint();
this.loginVerificationCallback = loginVerificationCallback; this.loginVerificationCallback = loginVerificationCallback;
this.userAgent = userAgent; this.userAgent = userAgent;
final OkHttpClient oldClient = ((OkHttpRestClient) restClient.getRestClient()).getClient();
final OkHttpClient.Builder builder = oldClient.newBuilder();
builder.cookieJar(new SimpleCookieJar());
builder.addNetworkInterceptor(new EndpointInterceptor(endpoint));
this.client = new OkHttpRestClient(builder.build());
} }
public OAuthToken getOAuthAccessToken(final String username, final String password) throws AuthenticationException { public OAuthToken getOAuthAccessToken(final String username, final String password) throws AuthenticationException {
@ -538,4 +511,41 @@ public class OAuthPasswordAuthenticator implements Constants {
public String oauthPin; public String oauthPin;
} }
private static class EndpointInterceptor implements Interceptor {
private final Endpoint endpoint;
public EndpointInterceptor(Endpoint endpoint) {
this.endpoint = endpoint;
}
@Override
public Response intercept(Chain chain) throws IOException {
final Response response = chain.proceed(chain.request());
if (!response.isRedirect()) {
return response;
}
final String location = response.header("Location");
final Response.Builder builder = response.newBuilder();
if (!TextUtils.isEmpty(location) && !endpoint.checkEndpoint(location)) {
final HttpUrl originalLocation = HttpUrl.get(URI.create("https://api.twitter.com/").resolve(location));
final HttpUrl.Builder locationBuilder = HttpUrl.parse(endpoint.getUrl()).newBuilder();
for (String pathSegments : originalLocation.pathSegments()) {
locationBuilder.addPathSegment(pathSegments);
}
for (int i = 0, j = originalLocation.querySize(); i < j; i++) {
final String name = originalLocation.queryParameterName(i);
final String value = originalLocation.queryParameterValue(i);
locationBuilder.addQueryParameter(name, value);
}
final String encodedFragment = originalLocation.encodedFragment();
if (encodedFragment != null) {
locationBuilder.encodedFragment(encodedFragment);
}
final HttpUrl newLocation = locationBuilder.build();
builder.header("Location", newLocation.toString());
}
return builder.build();
}
}
} }