1
0
mirror of https://github.com/TwidereProject/Twidere-Android synced 2025-01-30 16:35:00 +01:00

migrating to new api

[skip ci]
This commit is contained in:
Mariotaku Lee 2015-05-07 00:15:38 +08:00
parent 341be6cf4e
commit f6bed5e772
22 changed files with 167 additions and 182 deletions

View File

@ -23,8 +23,8 @@ import com.bluelinelabs.logansquare.LoganSquare;
import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonObject;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.mariotaku.simplerestapi.http.Endpoint;
import org.mariotaku.simplerestapi.http.KeyValuePair;
import org.mariotaku.simplerestapi.http.RestHttpClient;
import org.mariotaku.simplerestapi.http.RestRequest;
import org.mariotaku.simplerestapi.http.RestResponse;
@ -350,7 +350,7 @@ public class MediaPreviewUtils {
if (client != null) {
final RestRequest.Builder builder = new RestRequest.Builder();
builder.method(GET.METHOD);
builder.url(Endpoint.constructUrl(URL_PHOTOZOU_PHOTO_INFO, new KeyValuePair("photo_id", id)));
builder.url(Endpoint.constructUrl(URL_PHOTOZOU_PHOTO_INFO, new ImmutablePair<>("photo_id", id)));
final RestResponse response = client.execute(builder.build());
final PhotoZouPhotoInfo info = LoganSquare.parse(response.getBody().stream(), PhotoZouPhotoInfo.class);
if (info.info != null && info.info.photo != null) {

View File

@ -19,7 +19,8 @@
package org.mariotaku.simplerestapi;
import org.mariotaku.simplerestapi.http.KeyValuePair;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import java.io.Closeable;
import java.io.IOException;
@ -103,7 +104,7 @@ public class Utils {
return buf.toString();
}
public static void parseGetParameters(final String queryString, final List<KeyValuePair> params,
public static void parseGetParameters(final String queryString, final List<Pair<String, String>> params,
final String encoding) {
final String[] queryStrings = split(queryString, "&");
try {
@ -111,9 +112,9 @@ public class Utils {
final String[] split = split(query, "=");
final String key = URLDecoder.decode(split[0], encoding);
if (split.length == 2) {
params.add(new KeyValuePair(key, URLDecoder.decode(split[1], encoding)));
params.add(new ImmutablePair<>(key, URLDecoder.decode(split[1], encoding)));
} else {
params.add(new KeyValuePair(key, ""));
params.add(new ImmutablePair<>(key, ""));
}
}
} catch (final UnsupportedEncodingException e) {

View File

@ -1,6 +1,8 @@
package org.mariotaku.simplerestapi.http;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.mariotaku.simplerestapi.Utils;
import java.nio.charset.Charset;
@ -15,20 +17,20 @@ public final class ContentType {
public static final ContentType OCTET_STREAM = ContentType.parse("application/octet-stream");
private final String contentType;
private final List<KeyValuePair> parameters;
private final List<Pair<String, String>> parameters;
public ContentType(String contentType, Charset charset) {
this(contentType, new ArrayList<KeyValuePair>());
parameters.add(new KeyValuePair("charset", charset.name()));
this(contentType, new ArrayList<Pair<String, String>>());
parameters.add(new ImmutablePair<>("charset", charset.name()));
}
public ContentType(String contentType, List<KeyValuePair> parameters) {
public ContentType(String contentType, List<Pair<String, String>> parameters) {
this.contentType = contentType;
this.parameters = parameters;
}
public static ContentType parse(String string) {
final List<KeyValuePair> parameters = new ArrayList<>();
final List<Pair<String, String>> parameters = new ArrayList<>();
int previousIndex = string.indexOf(';', 0);
String contentType;
if (previousIndex == -1) {
@ -45,7 +47,7 @@ public final class ContentType {
segs = Utils.split(string.substring(previousIndex + 1, idx).trim(), "=");
}
if (segs.length == 2) {
parameters.add(new KeyValuePair(segs[0], segs[1]));
parameters.add(new ImmutablePair<>(segs[0], segs[1]));
}
if (idx < 0) {
break;
@ -65,7 +67,7 @@ public final class ContentType {
public Charset getCharset() {
if (parameters == null) return null;
for (KeyValuePair parameter : parameters) {
for (Pair<String, String> parameter : parameters) {
if ("charset".equals(parameter.getKey())) {
return Charset.forName(parameter.getValue());
}
@ -79,7 +81,7 @@ public final class ContentType {
public String toHeader() {
final StringBuilder sb = new StringBuilder(contentType);
for (KeyValuePair parameter : parameters) {
for (Pair<String, String> parameter : parameters) {
sb.append("; ");
sb.append(parameter.getKey());
sb.append("=");

View File

@ -30,7 +30,8 @@ public class Endpoint {
return constructUrl(url, path, queries);
}
public String construct(String path, Pair<String, String>... queries) {
@SafeVarargs
public final String construct(String path, Pair<String, String>... queries) {
return constructUrl(url, path, Arrays.asList(queries));
}
@ -54,7 +55,7 @@ public class Endpoint {
if (queries == null || queries.isEmpty()) return url;
final StringBuilder urlBuilder = new StringBuilder(url);
for (int i = 0, j = queries.size(); i < j; i++) {
final KeyValuePair item = queries.get(i);
final Pair<String, String> item = queries.get(i);
urlBuilder.append(i != 0 ? '&' : '?');
urlBuilder.append(Utils.encode(item.getKey(), "UTF-8"));
urlBuilder.append('=');
@ -63,7 +64,8 @@ public class Endpoint {
return urlBuilder.toString();
}
public static String constructUrl(String url, KeyValuePair... queries) {
@SafeVarargs
public static String constructUrl(String url, Pair<String, String>... queries) {
return constructUrl(url, Arrays.asList(queries));
}
}

View File

@ -1,41 +0,0 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.simplerestapi.http;
import org.apache.commons.lang3.tuple.MutablePair;
/**
* Created by mariotaku on 15/2/4.
*/
public class KeyValuePair extends MutablePair<String, String> {
public KeyValuePair() {
}
public KeyValuePair(String left, String right) {
super(left, right);
}
@Override
public String toString() {
return "'" + getKey() + "'='" + getValue() + "'";
}
}

View File

@ -3,6 +3,8 @@ package org.mariotaku.simplerestapi.http;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.mariotaku.simplerestapi.RestMethod;
import org.mariotaku.simplerestapi.RestMethodInfo;
import org.mariotaku.simplerestapi.http.mime.TypedData;
@ -17,7 +19,7 @@ public class RestRequest {
private final String method;
private final String url;
private final List<KeyValuePair> headers;
private final List<Pair<String, String>> headers;
private final TypedData body;
private final Object extra;
@ -29,7 +31,7 @@ public class RestRequest {
return url;
}
public List<KeyValuePair> getHeaders() {
public List<Pair<String, String>> getHeaders() {
return headers;
}
@ -51,7 +53,7 @@ public class RestRequest {
'}';
}
public RestRequest(String method, String url, List<KeyValuePair> headers, TypedData body, Object extra) {
public RestRequest(String method, String url, List<Pair<String, String>> headers, TypedData body, Object extra) {
this.method = method;
this.url = url;
this.headers = headers;
@ -62,7 +64,7 @@ public class RestRequest {
public static final class Builder {
private String method;
private String url;
private List<KeyValuePair> headers;
private List<Pair<String, String>> headers;
private TypedData body;
private Object extra;
@ -79,7 +81,7 @@ public class RestRequest {
return this;
}
public Builder headers(List<KeyValuePair> headers) {
public Builder headers(List<Pair<String, String>> headers) {
this.headers = headers;
return this;
}
@ -109,10 +111,10 @@ public class RestRequest {
public RestRequest create(@NonNull Endpoint endpoint, @NonNull RestMethodInfo methodInfo, @Nullable Authorization authorization) {
final RestMethod restMethod = methodInfo.getMethod();
final String url = Endpoint.constructUrl(endpoint.getUrl(), methodInfo);
final ArrayList<KeyValuePair> headers = new ArrayList<>(methodInfo.getHeaders());
final ArrayList<Pair<String, String>> headers = new ArrayList<>(methodInfo.getHeaders());
if (authorization != null && authorization.hasAuthorization()) {
headers.add(new KeyValuePair("Authorization", authorization.getHeader(endpoint, methodInfo)));
headers.add(new ImmutablePair<>("Authorization", authorization.getHeader(endpoint, methodInfo)));
}
return new RestRequest(restMethod.value(), url, headers, methodInfo.getBody(), null);
}

View File

@ -30,8 +30,9 @@ import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.mariotaku.simplerestapi.http.ContentType;
import org.mariotaku.simplerestapi.http.KeyValuePair;
import org.mariotaku.simplerestapi.http.RestHttpClient;
import org.mariotaku.simplerestapi.http.RestRequest;
import org.mariotaku.simplerestapi.http.RestResponse;
@ -61,9 +62,9 @@ public class OkHttpRestClient implements RestHttpClient {
final Request.Builder builder = new Request.Builder();
builder.url(restRequest.getUrl());
builder.method(restRequest.getMethod(), RestToOkBody.wrap(restRequest.getBody()));
final List<KeyValuePair> headers = restRequest.getHeaders();
final List<Pair<String, String>> headers = restRequest.getHeaders();
if (headers != null) {
for (KeyValuePair header : headers) {
for (Pair<String, String> header : headers) {
builder.addHeader(header.getKey(), header.getValue());
}
}
@ -109,11 +110,11 @@ public class OkHttpRestClient implements RestHttpClient {
}
@Override
public List<KeyValuePair> getHeaders() {
public List<Pair<String, String>> getHeaders() {
final Headers headers = response.headers();
final ArrayList<KeyValuePair> headersList = new ArrayList<>();
final ArrayList<Pair<String, String>> headersList = new ArrayList<>();
for (int i = 0, j = headers.size(); i < j; i++) {
headersList.add(new KeyValuePair(headers.name(i), headers.value(i)));
headersList.add(new ImmutablePair<>(headers.name(i), headers.value(i)));
}
return headersList;
}

View File

@ -21,12 +21,13 @@ package org.mariotaku.twidere.api.twitter.auth;
import android.util.Base64;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.mariotaku.simplerestapi.RestMethod;
import org.mariotaku.simplerestapi.RestMethodInfo;
import org.mariotaku.simplerestapi.Utils;
import org.mariotaku.simplerestapi.http.Authorization;
import org.mariotaku.simplerestapi.http.Endpoint;
import org.mariotaku.simplerestapi.http.KeyValuePair;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
@ -67,8 +68,8 @@ public class OAuthAuthorization implements Authorization {
private String generateOAuthSignature(RestMethod method, String url,
String oauthNonce, long timestamp,
String oauthToken, String oauthTokenSecret,
List<KeyValuePair> queries,
List<KeyValuePair> forms) {
List<Pair<String, String>> queries,
List<Pair<String, String>> forms) {
final List<String> encodeParams = new ArrayList<>();
encodeParams.add(encodeParameter("oauth_consumer_key", consumerKey));
encodeParams.add(encodeParameter("oauth_nonce", oauthNonce));
@ -79,12 +80,12 @@ public class OAuthAuthorization implements Authorization {
encodeParams.add(encodeParameter("oauth_token", oauthToken));
}
if (queries != null) {
for (KeyValuePair query : queries) {
for (Pair<String, String> query : queries) {
encodeParams.add(encodeParameter(query.getKey(), query.getValue()));
}
}
if (forms != null) {
for (KeyValuePair form : forms) {
for (Pair<String, String> form : forms) {
encodeParams.add(encodeParameter(form.getKey(), form.getValue()));
}
}
@ -136,15 +137,15 @@ public class OAuthAuthorization implements Authorization {
}
final String oauthSignature = generateOAuthSignature(method, url, oauthNonce, timestamp, oauthToken,
oauthTokenSecret, request.getQueries(), request.getForms());
final List<KeyValuePair> encodeParams = new ArrayList<>();
encodeParams.add(new KeyValuePair("oauth_consumer_key", consumerKey));
encodeParams.add(new KeyValuePair("oauth_nonce", oauthNonce));
encodeParams.add(new KeyValuePair("oauth_signature", encode(oauthSignature)));
encodeParams.add(new KeyValuePair("oauth_signature_method", OAUTH_SIGNATURE_METHOD));
encodeParams.add(new KeyValuePair("oauth_timestamp", String.valueOf(timestamp)));
encodeParams.add(new KeyValuePair("oauth_version", OAUTH_VERSION));
final List<Pair<String, String>> encodeParams = new ArrayList<>();
encodeParams.add(new ImmutablePair<>("oauth_consumer_key", consumerKey));
encodeParams.add(new ImmutablePair<>("oauth_nonce", oauthNonce));
encodeParams.add(new ImmutablePair<>("oauth_signature", encode(oauthSignature)));
encodeParams.add(new ImmutablePair<>("oauth_signature_method", OAUTH_SIGNATURE_METHOD));
encodeParams.add(new ImmutablePair<>("oauth_timestamp", String.valueOf(timestamp)));
encodeParams.add(new ImmutablePair<>("oauth_version", OAUTH_VERSION));
if (oauthToken != null) {
encodeParams.add(new KeyValuePair("oauth_token", oauthToken));
encodeParams.add(new ImmutablePair<>("oauth_token", oauthToken));
}
Collections.sort(encodeParams);
final StringBuilder headerBuilder = new StringBuilder();
@ -153,7 +154,7 @@ public class OAuthAuthorization implements Authorization {
if (i != 0) {
headerBuilder.append(", ");
}
final KeyValuePair keyValuePair = encodeParams.get(i);
final Pair<String, String> keyValuePair = encodeParams.get(i);
headerBuilder.append(keyValuePair.getKey());
headerBuilder.append("=\"");
headerBuilder.append(keyValuePair.getValue());

View File

@ -1,7 +1,7 @@
package org.mariotaku.twidere.api.twitter.auth;
import org.apache.commons.lang3.tuple.Pair;
import org.mariotaku.simplerestapi.Utils;
import org.mariotaku.simplerestapi.http.KeyValuePair;
import org.mariotaku.simplerestapi.http.ValueMap;
import java.nio.charset.Charset;
@ -41,9 +41,9 @@ public class OAuthToken implements ValueMap {
}
public OAuthToken(String body, Charset charset) throws ParseException {
List<KeyValuePair> params = new ArrayList<>();
List<Pair<String, String>> params = new ArrayList<>();
Utils.parseGetParameters(body, params, charset.name());
for (KeyValuePair param : params) {
for (Pair<String, String> param : params) {
switch (param.getKey()) {
case "oauth_token": {
oauthToken = param.getValue();

View File

@ -40,6 +40,7 @@ import android.webkit.WebViewClient;
import android.widget.Toast;
import org.mariotaku.twidere.R;
import org.mariotaku.twidere.api.twitter.auth.OAuthToken;
import org.mariotaku.twidere.app.TwidereApplication;
import org.mariotaku.twidere.provider.TwidereDataStore.Accounts;
import org.mariotaku.twidere.util.AsyncTaskUtils;
@ -57,7 +58,6 @@ import twitter4j.Twitter;
import twitter4j.TwitterConstants;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.RequestToken;
import twitter4j.conf.ConfigurationBuilder;
import static android.text.TextUtils.isEmpty;
@ -75,7 +75,7 @@ public class BrowserSignInActivity extends BaseSupportDialogActivity implements
private WebSettings mWebSettings;
private RequestToken mRequestToken;
private OAuthToken mRequestToken;
private GetRequestTokenTask mTask;
@ -145,7 +145,7 @@ public class BrowserSignInActivity extends BaseSupportDialogActivity implements
mProgressContainer.setVisibility(shown ? View.VISIBLE : View.GONE);
}
private void setRequestToken(final RequestToken token) {
private void setRequestToken(final OAuthToken token) {
mRequestToken = token;
}
@ -196,12 +196,12 @@ public class BrowserSignInActivity extends BaseSupportDialogActivity implements
final Uri uri = Uri.parse(url);
if (url.startsWith(OAUTH_CALLBACK_URL)) {
final String oauth_verifier = uri.getQueryParameter(EXTRA_OAUTH_VERIFIER);
final RequestToken request_token = mActivity.mRequestToken;
if (oauth_verifier != null && request_token != null) {
final OAuthToken requestToken = mActivity.mRequestToken;
if (oauth_verifier != null && requestToken != null) {
final Intent intent = new Intent();
intent.putExtra(EXTRA_OAUTH_VERIFIER, oauth_verifier);
intent.putExtra(EXTRA_REQUEST_TOKEN, request_token.getToken());
intent.putExtra(EXTRA_REQUEST_TOKEN_SECRET, request_token.getTokenSecret());
intent.putExtra(EXTRA_REQUEST_TOKEN, requestToken.getOauthToken());
intent.putExtra(EXTRA_REQUEST_TOKEN_SECRET, requestToken.getOauthTokenSecret());
mActivity.setResult(RESULT_OK, intent);
mActivity.finish();
}
@ -212,7 +212,7 @@ public class BrowserSignInActivity extends BaseSupportDialogActivity implements
}
static class GetRequestTokenTask extends AsyncTask<Object, Object, RequestToken> {
static class GetRequestTokenTask extends AsyncTask<Object, Object, OAuthToken> {
private final String mConsumerKey, mConsumerSecret;
private final TwidereApplication mApplication;
@ -229,7 +229,7 @@ public class BrowserSignInActivity extends BaseSupportDialogActivity implements
}
@Override
protected RequestToken doInBackground(final Object... params) {
protected OAuthToken doInBackground(final Object... params) {
final ConfigurationBuilder cb = new ConfigurationBuilder();
final boolean enable_gzip_compressing = mPreferences.getBoolean(KEY_GZIP_COMPRESSING, false);
final boolean ignore_ssl_error = mPreferences.getBoolean(KEY_IGNORE_SSL_ERROR, false);
@ -271,7 +271,7 @@ public class BrowserSignInActivity extends BaseSupportDialogActivity implements
}
@Override
protected void onPostExecute(final RequestToken data) {
protected void onPostExecute(final OAuthToken data) {
mActivity.setLoadProgressShown(false);
mActivity.setRequestToken(data);
if (data == null) {
@ -302,12 +302,12 @@ public class BrowserSignInActivity extends BaseSupportDialogActivity implements
@JavascriptInterface
public void processHTML(final String html) {
final String oauthVerifier = mActivity.readOAuthPin(html);
final RequestToken requestToken = mActivity.mRequestToken;
final OAuthToken requestToken = mActivity.mRequestToken;
if (oauthVerifier != null && requestToken != null) {
final Intent intent = new Intent();
intent.putExtra(EXTRA_OAUTH_VERIFIER, oauthVerifier);
intent.putExtra(EXTRA_REQUEST_TOKEN, requestToken.getToken());
intent.putExtra(EXTRA_REQUEST_TOKEN_SECRET, requestToken.getTokenSecret());
intent.putExtra(EXTRA_REQUEST_TOKEN, requestToken.getOauthToken());
intent.putExtra(EXTRA_REQUEST_TOKEN_SECRET, requestToken.getOauthTokenSecret());
mActivity.setResult(RESULT_OK, intent);
mActivity.finish();
}

View File

@ -480,7 +480,7 @@ public class SignInActivity extends BaseAppCompatActivity implements TwitterCons
}
case Accounts.AUTH_TYPE_OAUTH:
case Accounts.AUTH_TYPE_XAUTH: {
values = ContentValuesCreator.createAccount(result.conf, result.access_token,
values = ContentValuesCreator.createAccount(result.conf, result.OAuthToken,
result.user, result.auth_type, result.color, result.api_url_format,
result.same_oauth_signing_url, result.no_version_suffix);
break;
@ -640,7 +640,7 @@ public class SignInActivity extends BaseAppCompatActivity implements TwitterCons
protected SignInResponse doInBackground(final Object... params) {
try {
final Twitter twitter = new TwitterFactory(conf).getInstance();
final OAuthToken access_token = twitter.getOAuthAccessToken(new RequestToken(conf, request_token,
final OAuthToken access_token = twitter.getOAuthOAuthToken(new RequestToken(conf, request_token,
request_token_secret), oauth_verifier);
final long userId = access_token.getUserId();
if (userId <= 0) return new SignInResponse(false, false, null);
@ -732,7 +732,7 @@ public class SignInActivity extends BaseAppCompatActivity implements TwitterCons
private SignInResponse authOAuth() throws AuthenticationException, TwitterException {
final Twitter twitter = new TwitterFactory(conf).getInstance();
final OAuthPasswordAuthenticator authenticator = new OAuthPasswordAuthenticator(twitter);
final OAuthToken access_token = authenticator.getOAuthAccessToken(username, password);
final OAuthToken access_token = authenticator.getOAuthOAuthToken(username, password);
final long user_id = access_token.getUserId();
if (user_id <= 0) return new SignInResponse(false, false, null);
final User user = twitter.verifyCredentials();
@ -754,13 +754,13 @@ public class SignInActivity extends BaseAppCompatActivity implements TwitterCons
private SignInResponse authxAuth() throws TwitterException {
final Twitter twitter = new TwitterFactory(conf).getInstance();
final AccessToken accessToken = twitter.getOAuthAccessToken(username, password);
final OAuthToken OAuthToken = twitter.getOAuthOAuthToken(username, password);
final User user = twitter.verifyCredentials();
final long user_id = user.getId();
if (user_id <= 0) return new SignInResponse(false, false, null);
if (isUserLoggedIn(context, user_id)) return new SignInResponse(true, false, null);
final int color = analyseUserProfileColor(user);
return new SignInResponse(conf, accessToken, user, Accounts.AUTH_TYPE_XAUTH, color,
return new SignInResponse(conf, OAuthToken, user, Accounts.AUTH_TYPE_XAUTH, color,
api_url_format, same_oauth_signing_url, no_version_suffix);
}
@ -772,7 +772,7 @@ public class SignInActivity extends BaseAppCompatActivity implements TwitterCons
public final Exception exception;
public final Configuration conf;
public final String basic_username, basic_password;
public final AccessToken access_token;
public final OAuthToken OAuthToken;
public final User user;
public final int auth_type, color;
public final String api_url_format;
@ -784,7 +784,7 @@ public class SignInActivity extends BaseAppCompatActivity implements TwitterCons
public SignInResponse(final boolean already_logged_in, final boolean succeed, final Exception exception,
final Configuration conf, final String basic_username, final String basic_password,
final AccessToken access_token, final User user, final int auth_type, final int color,
final OAuthToken OAuthToken, final User user, final int auth_type, final int color,
final String api_url_format, final boolean same_oauth_signing_url, final boolean no_version_suffix) {
this.already_logged_in = already_logged_in;
this.succeed = succeed;
@ -792,7 +792,7 @@ public class SignInActivity extends BaseAppCompatActivity implements TwitterCons
this.conf = conf;
this.basic_username = basic_username;
this.basic_password = basic_password;
this.access_token = access_token;
this.OAuthToken = OAuthToken;
this.user = user;
this.auth_type = auth_type;
this.color = color;
@ -801,10 +801,10 @@ public class SignInActivity extends BaseAppCompatActivity implements TwitterCons
this.no_version_suffix = no_version_suffix;
}
public SignInResponse(final Configuration conf, final AccessToken access_token, final User user,
public SignInResponse(final Configuration conf, final OAuthToken OAuthToken, final User user,
final int auth_type, final int color, final String api_url_format,
final boolean same_oauth_signing_url, final boolean no_version_suffix) {
this(false, true, null, conf, null, null, access_token, user, auth_type, color, api_url_format,
this(false, true, null, conf, null, null, OAuthToken, user, auth_type, color, api_url_format,
same_oauth_signing_url, no_version_suffix);
}

View File

@ -25,8 +25,8 @@ import org.mariotaku.twidere.model.ParcelableUser;
import java.util.List;
import twitter4j.CursorPaging;
import twitter4j.PageableResponseList;
import twitter4j.Paging;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.User;
@ -38,13 +38,14 @@ public abstract class CursorSupportUsersLoader extends BaseCursorSupportUsersLoa
super(context, accountId, cursor, data, fromUser);
}
protected abstract PageableResponseList<User> getCursoredUsers(Twitter twitter, CursorPaging paging)
protected abstract PageableResponseList<User> getCursoredUsers(Twitter twitter, Paging paging)
throws TwitterException;
@Override
protected final List<User> getUsers(final Twitter twitter) throws TwitterException {
if (twitter == null) return null;
final CursorPaging paging = new CursorPaging(getCount());
final Paging paging = new Paging();
paging.count(getCount());
if (getCursor() > 0) {
paging.setCursor(getCursor());
}

View File

@ -25,8 +25,8 @@ import org.mariotaku.twidere.model.ParcelableUser;
import java.util.List;
import twitter4j.CursorPaging;
import twitter4j.IDs;
import twitter4j.Paging;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.User;
@ -41,7 +41,8 @@ public abstract class IDsUsersLoader extends BaseCursorSupportUsersLoader {
@Override
public List<User> getUsers(final Twitter twitter) throws TwitterException {
if (twitter == null) return null;
final CursorPaging paging = new CursorPaging(getCount());
final Paging paging = new Paging();
paging.count(getCount());
if (getCursor() > 0) {
paging.setCursor(getCursor());
}
@ -51,6 +52,6 @@ public abstract class IDsUsersLoader extends BaseCursorSupportUsersLoader {
return twitter.lookupUsers(ids.getIDs());
}
protected abstract IDs getIDs(Twitter twitter, CursorPaging paging) throws TwitterException;
protected abstract IDs getIDs(Twitter twitter, Paging paging) throws TwitterException;
}

View File

@ -25,22 +25,22 @@ import org.mariotaku.twidere.model.ParcelableUser;
import java.util.List;
import twitter4j.CursorPaging;
import twitter4j.IDs;
import twitter4j.Paging;
import twitter4j.Twitter;
import twitter4j.TwitterException;
public class IncomingFriendshipsLoader extends IDsUsersLoader {
public IncomingFriendshipsLoader(final Context context, final long accountId, final long maxId,
public IncomingFriendshipsLoader(final Context context, final long accountId, final long maxId,
final List<ParcelableUser> data, boolean fromUser) {
super(context, accountId, maxId, data, fromUser);
}
super(context, accountId, maxId, data, fromUser);
}
@Override
protected IDs getIDs(final Twitter twitter, final CursorPaging paging) throws TwitterException {
if (twitter == null) return null;
return twitter.getIncomingFriendships(paging);
}
@Override
protected IDs getIDs(final Twitter twitter, final Paging paging) throws TwitterException {
if (twitter == null) return null;
return twitter.getIncomingFriendships(paging);
}
}

View File

@ -25,8 +25,8 @@ import org.mariotaku.twidere.model.ParcelableUser;
import java.util.List;
import twitter4j.CursorPaging;
import twitter4j.PageableResponseList;
import twitter4j.Paging;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.User;
@ -39,7 +39,7 @@ public class MutesUsersLoader extends CursorSupportUsersLoader {
}
@Override
protected final PageableResponseList<User> getCursoredUsers(final Twitter twitter, final CursorPaging paging)
protected final PageableResponseList<User> getCursoredUsers(final Twitter twitter, final Paging paging)
throws TwitterException {
if (twitter == null) return null;
return twitter.getMutesUsersList(paging);

View File

@ -25,26 +25,26 @@ import org.mariotaku.twidere.model.ParcelableUser;
import java.util.List;
import twitter4j.CursorPaging;
import twitter4j.IDs;
import twitter4j.Paging;
import twitter4j.Twitter;
import twitter4j.TwitterException;
public class StatusFavoritersLoader extends IDsUsersLoader {
private final long mStatusId;
private final long mStatusId;
public StatusFavoritersLoader(final Context context, final long accountId, final long statusId,
public StatusFavoritersLoader(final Context context, final long accountId, final long statusId,
final long cursor, final List<ParcelableUser> data, boolean fromUser) {
super(context, accountId, cursor, data, fromUser);
mStatusId = statusId;
}
super(context, accountId, cursor, data, fromUser);
mStatusId = statusId;
}
@Override
protected IDs getIDs(final Twitter twitter, final CursorPaging paging) throws TwitterException {
if (twitter == null) return null;
if (mStatusId > 0) return twitter.getStatusActivitySummary(mStatusId).getFavoriters();
return null;
}
@Override
protected IDs getIDs(final Twitter twitter, final Paging paging) throws TwitterException {
if (twitter == null) return null;
if (mStatusId > 0) return twitter.getStatusActivitySummary(mStatusId).getFavoriters();
return null;
}
}

View File

@ -25,8 +25,8 @@ import org.mariotaku.twidere.model.ParcelableUser;
import java.util.List;
import twitter4j.CursorPaging;
import twitter4j.IDs;
import twitter4j.Paging;
import twitter4j.Twitter;
import twitter4j.TwitterException;
@ -41,7 +41,7 @@ public class StatusRetweetersLoader extends IDsUsersLoader {
}
@Override
protected IDs getIDs(final Twitter twitter, final CursorPaging paging) throws TwitterException {
protected IDs getIDs(final Twitter twitter, final Paging paging) throws TwitterException {
if (twitter == null) return null;
if (mStatusId > 0) return twitter.getRetweetersIDs(mStatusId, paging);
return null;

View File

@ -27,6 +27,7 @@ import java.util.List;
import twitter4j.CursorPaging;
import twitter4j.PageableResponseList;
import twitter4j.Paging;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.User;
@ -39,7 +40,7 @@ public class UserBlocksLoader extends CursorSupportUsersLoader {
}
@Override
protected final PageableResponseList<User> getCursoredUsers(final Twitter twitter, final CursorPaging paging)
protected final PageableResponseList<User> getCursoredUsers(final Twitter twitter, final Paging paging)
throws TwitterException {
if (twitter == null) return null;
return twitter.getBlocksList(paging);

View File

@ -25,8 +25,8 @@ import org.mariotaku.twidere.model.ParcelableUser;
import java.util.List;
import twitter4j.CursorPaging;
import twitter4j.PageableResponseList;
import twitter4j.Paging;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.User;
@ -45,7 +45,7 @@ public class UserFollowersLoader extends CursorSupportUsersLoader {
}
@Override
protected PageableResponseList<User> getCursoredUsers(final Twitter twitter, final CursorPaging paging)
protected PageableResponseList<User> getCursoredUsers(final Twitter twitter, final Paging paging)
throws TwitterException {
if (twitter == null) return null;
if (mUserId > 0)

View File

@ -25,8 +25,8 @@ import org.mariotaku.twidere.model.ParcelableUser;
import java.util.List;
import twitter4j.CursorPaging;
import twitter4j.PageableResponseList;
import twitter4j.Paging;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.User;
@ -45,7 +45,7 @@ public class UserFriendsLoader extends CursorSupportUsersLoader {
}
@Override
protected PageableResponseList<User> getCursoredUsers(final Twitter twitter, final CursorPaging paging)
protected PageableResponseList<User> getCursoredUsers(final Twitter twitter, final Paging paging)
throws TwitterException {
if (twitter == null) return null;
if (mUserId > 0)

View File

@ -25,8 +25,8 @@ import org.mariotaku.twidere.model.ParcelableUser;
import java.util.List;
import twitter4j.CursorPaging;
import twitter4j.PageableResponseList;
import twitter4j.Paging;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.User;
@ -48,7 +48,7 @@ public class UserListSubscribersLoader extends CursorSupportUsersLoader {
}
@Override
public PageableResponseList<User> getCursoredUsers(final Twitter twitter, final CursorPaging paging)
public PageableResponseList<User> getCursoredUsers(final Twitter twitter, final Paging paging)
throws TwitterException {
if (twitter == null) return null;
if (mListId > 0)

View File

@ -25,9 +25,17 @@ import android.util.Xml;
import com.nostra13.universalimageloader.utils.IoUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.mariotaku.simplerestapi.RestClient;
import org.mariotaku.simplerestapi.http.Endpoint;
import org.mariotaku.simplerestapi.http.RestHttpClient;
import org.mariotaku.simplerestapi.http.RestRequest;
import org.mariotaku.simplerestapi.http.RestResponse;
import org.mariotaku.simplerestapi.http.mime.BaseTypedData;
import org.mariotaku.simplerestapi.http.mime.FormTypedBody;
import org.mariotaku.simplerestapi.method.GET;
import org.mariotaku.simplerestapi.method.POST;
import org.mariotaku.twidere.Constants;
import org.mariotaku.twidere.api.twitter.auth.OAuthToken;
import org.xmlpull.v1.XmlPullParser;
@ -36,18 +44,15 @@ import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.net.HttpCookie;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import twitter4j.TwitterException;
import twitter4j.TwitterOAuth;
import twitter4j.conf.Configuration;
import twitter4j.http.HeaderMap;
import twitter4j.http.HttpClientWrapper;
import twitter4j.http.HttpParameter;
import static android.text.TextUtils.isEmpty;
@ -56,19 +61,22 @@ public class OAuthPasswordAuthenticator implements Constants {
private static final String INPUT_AUTHENTICITY_TOKEN = "authenticity_token";
private static final String INPUT_REDIRECT_AFTER_LOGIN = "redirect_after_login";
private final TwitterOAuth twitter;
private final TwitterOAuth oauth;
private final RestHttpClient client;
private final Endpoint endpoint;
public OAuthPasswordAuthenticator(final TwitterOAuth twitter) {
final Configuration conf = twitter.getConfiguration();
this.twitter = twitter;
client = new HttpClientWrapper(conf);
public OAuthPasswordAuthenticator(final TwitterOAuth oauth) {
final InvocationHandler handler = Proxy.getInvocationHandler(oauth);
if (!(handler instanceof RestClient)) throw new IllegalArgumentException();
this.oauth = oauth;
this.client = ((RestClient) handler).getRestClient();
this.endpoint = ((RestClient) handler).getEndpoint();
}
public OAuthToken getOAuthAccessToken(final String username, final String password) throws AuthenticationException {
final OAuthToken requestToken;
try {
requestToken = twitter.getRequestToken(OAUTH_CALLBACK_OOB);
requestToken = oauth.getRequestToken(OAUTH_CALLBACK_OOB);
} catch (final Exception e) {
// if (e.isCausedByNetworkIssue()) throw new AuthenticationException(e);
throw new AuthenticityTokenException();
@ -76,44 +84,50 @@ public class OAuthPasswordAuthenticator implements Constants {
RestResponse authorizePage = null, authorizeResult = null;
try {
final String oauthToken = requestToken.getOauthToken();
final String authorizationUrl = requestToken.getAuthorizationURL();
final HashMap<String, String> inputMap = new HashMap<>();
authorizePage = client.get(authorizationUrl, authorizationUrl, null, null, null);
final RestRequest.Builder authorizePageBuilder = new RestRequest.Builder();
authorizePageBuilder.method(GET.METHOD);
authorizePageBuilder.url(endpoint.construct("/oauth/authorize", new ImmutablePair<>("oauth_token",
requestToken.getOauthToken())));
final RestRequest authorizePageRequest = authorizePageBuilder.build();
authorizePage = client.execute(authorizePageRequest);
final String[] cookieHeaders = authorizePage.getHeaders("Set-Cookie");
readInputFromHtml(BaseTypedData.reader(authorizePage.getBody()), inputMap,
INPUT_AUTHENTICITY_TOKEN, INPUT_REDIRECT_AFTER_LOGIN);
final Configuration conf = twitter.getConfiguration();
final List<HttpParameter> params = new ArrayList<>();
params.add(new HttpParameter("oauth_token", oauthToken));
params.add(new HttpParameter(INPUT_AUTHENTICITY_TOKEN, inputMap.get(INPUT_AUTHENTICITY_TOKEN)));
final List<Pair<String, String>> params = new ArrayList<>();
params.add(new ImmutablePair<>("oauth_token", oauthToken));
params.add(new ImmutablePair<>(INPUT_AUTHENTICITY_TOKEN, inputMap.get(INPUT_AUTHENTICITY_TOKEN)));
if (inputMap.containsKey(INPUT_REDIRECT_AFTER_LOGIN)) {
params.add(new HttpParameter(INPUT_REDIRECT_AFTER_LOGIN, inputMap.get(INPUT_REDIRECT_AFTER_LOGIN)));
params.add(new ImmutablePair<>(INPUT_REDIRECT_AFTER_LOGIN, inputMap.get(INPUT_REDIRECT_AFTER_LOGIN)));
}
params.add(new HttpParameter("session[username_or_email]", username));
params.add(new HttpParameter("session[password]", password));
final HeaderMap requestHeaders = new HeaderMap();
requestHeaders.addHeader("Origin", "https://twitter.com");
requestHeaders.addHeader("Referer", "https://twitter.com/oauth/authorize?oauth_token=" + requestToken.getToken());
final List<String> modifiedCookieHeaders = new ArrayList<>();
final String oAuthAuthorizationUrl = conf.getOAuthAuthorizationURL();
params.add(new ImmutablePair<>("session[username_or_email]", username));
params.add(new ImmutablePair<>("session[password]", password));
final FormTypedBody authorizationResultBody = new FormTypedBody(params);
final ArrayList<Pair<String, String>> requestHeaders = new ArrayList<>();
requestHeaders.add(new ImmutablePair<>("Origin", "https://twitter.com"));
requestHeaders.add(new ImmutablePair<>("Referer", Endpoint.constructUrl("https://twitter.com/oauth/authorize",
new ImmutablePair<>("oauth_token", requestToken.getOauthToken()))));
final String host = parseUrlHost(oAuthAuthorizationUrl);
final String host = parseUrlHost(endpoint.getUrl());
for (String cookieHeader : cookieHeaders) {
for (HttpCookie cookie : HttpCookie.parse(cookieHeader)) {
if (HttpCookie.domainMatches(cookie.getDomain(), host)) {
cookie.setVersion(1);
cookie.setDomain("twitter.com");
}
modifiedCookieHeaders.add(cookie.toString());
requestHeaders.add(new ImmutablePair<>("Cookie", cookie.toString()));
}
}
requestHeaders.put("Cookie", modifiedCookieHeaders);
authorizeResult = client.post(oAuthAuthorizationUrl, oAuthAuthorizationUrl,
params.toArray(new HttpParameter[params.size()]), requestHeaders);
final String oauthPin = readOAuthPINFromHtml(authorizeResult.asReader());
final RestRequest.Builder authorizeResultBuilder = new RestRequest.Builder();
authorizeResultBuilder.method(POST.METHOD);
authorizeResultBuilder.url(endpoint.construct("/oauth/authorize"));
authorizeResultBuilder.headers(requestHeaders);
authorizeResultBuilder.body(authorizationResultBody);
authorizeResult = client.execute(authorizeResultBuilder.build());
final String oauthPin = readOAuthPINFromHtml(BaseTypedData.reader(authorizeResult.getBody()));
if (isEmpty(oauthPin)) throw new WrongUserPassException();
return twitter.getOAuthAccessToken(requestToken, oauthPin);
} catch (final IOException | TwitterException | NullPointerException | XmlPullParserException e) {
return oauth.getAccessToken(requestToken, oauthPin);
} catch (final IOException | NullPointerException | XmlPullParserException e) {
throw new AuthenticationException(e);
} finally {
if (authorizePage != null) {