mirror of
https://github.com/TwidereProject/Twidere-Android
synced 2025-02-17 04:00:48 +01:00
migrating to new api
[skip ci]
This commit is contained in:
parent
f6bed5e772
commit
1b76a2bf7f
@ -19,11 +19,12 @@
|
||||
|
||||
package org.mariotaku.twidere.util;
|
||||
|
||||
import android.util.Pair;
|
||||
|
||||
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.RestHttpClient;
|
||||
import org.mariotaku.simplerestapi.http.RestRequest;
|
||||
@ -350,7 +351,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 ImmutablePair<>("photo_id", id)));
|
||||
builder.url(Endpoint.constructUrl(URL_PHOTOZOU_PHOTO_INFO, Pair.create("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) {
|
||||
|
@ -1,9 +1,8 @@
|
||||
package org.mariotaku.simplerestapi;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Pair;
|
||||
|
||||
import org.apache.commons.lang3.tuple.ImmutablePair;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.mariotaku.simplerestapi.http.ValueMap;
|
||||
import org.mariotaku.simplerestapi.http.mime.BaseTypedData;
|
||||
import org.mariotaku.simplerestapi.http.mime.FormTypedBody;
|
||||
@ -114,12 +113,12 @@ public final class RestMethodInfo {
|
||||
final ValueMap valueMap = (ValueMap) value;
|
||||
for (String key : form.value()) {
|
||||
if (valueMap.has(key)) {
|
||||
list.add(new ImmutablePair<>(key, String.valueOf(valueMap.get(key))));
|
||||
list.add(Pair.create(key, String.valueOf(valueMap.get(key))));
|
||||
}
|
||||
}
|
||||
} else if (value != null) {
|
||||
for (String key : form.value()) {
|
||||
list.add(new ImmutablePair<>(key, String.valueOf(value)));
|
||||
list.add(Pair.create(key, String.valueOf(value)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -134,9 +133,9 @@ public final class RestMethodInfo {
|
||||
final String[] names = form.value();
|
||||
final Object value = entry.getValue();
|
||||
if (value instanceof TypedData) {
|
||||
list.add(new ImmutablePair<>(names[0], (TypedData) value));
|
||||
list.add(Pair.create(names[0], (TypedData) value));
|
||||
} else if (value != null) {
|
||||
list.add(new ImmutablePair<>(names[0], BaseTypedData.wrap(value)));
|
||||
list.add(Pair.create(names[0], BaseTypedData.wrap(value)));
|
||||
}
|
||||
}
|
||||
return partsCache = list;
|
||||
@ -153,12 +152,12 @@ public final class RestMethodInfo {
|
||||
final ValueMap valueMap = (ValueMap) value;
|
||||
for (String key : form.value()) {
|
||||
if (valueMap.has(key)) {
|
||||
list.add(new ImmutablePair<>(key, String.valueOf(valueMap.get(key))));
|
||||
list.add(Pair.create(key, String.valueOf(valueMap.get(key))));
|
||||
}
|
||||
}
|
||||
} else if (value != null) {
|
||||
for (String key : form.value()) {
|
||||
list.add(new ImmutablePair<>(key, String.valueOf(value)));
|
||||
list.add(Pair.create(key, String.valueOf(value)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -195,12 +194,12 @@ public final class RestMethodInfo {
|
||||
final ValueMap valueMap = (ValueMap) value;
|
||||
for (String key : form.value()) {
|
||||
if (valueMap.has(key)) {
|
||||
list.add(new ImmutablePair<>(key, String.valueOf(valueMap.get(key))));
|
||||
list.add(Pair.create(key, String.valueOf(valueMap.get(key))));
|
||||
}
|
||||
}
|
||||
} else if (value != null) {
|
||||
for (String key : form.value()) {
|
||||
list.add(new ImmutablePair<>(key, String.valueOf(value)));
|
||||
list.add(Pair.create(key, String.valueOf(value)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package org.mariotaku.simplerestapi.http;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import android.util.Pair;
|
||||
|
||||
import org.mariotaku.simplerestapi.RestMethodInfo;
|
||||
import org.mariotaku.simplerestapi.Utils;
|
||||
|
||||
@ -57,9 +58,9 @@ public class Endpoint {
|
||||
for (int i = 0, j = queries.size(); i < j; i++) {
|
||||
final Pair<String, String> item = queries.get(i);
|
||||
urlBuilder.append(i != 0 ? '&' : '?');
|
||||
urlBuilder.append(Utils.encode(item.getKey(), "UTF-8"));
|
||||
urlBuilder.append(Utils.encode(item.first, "UTF-8"));
|
||||
urlBuilder.append('=');
|
||||
urlBuilder.append(Utils.encode(item.getValue(), "UTF-8"));
|
||||
urlBuilder.append(Utils.encode(item.second, "UTF-8"));
|
||||
}
|
||||
return urlBuilder.toString();
|
||||
}
|
||||
|
@ -2,9 +2,8 @@ package org.mariotaku.simplerestapi.http;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Pair;
|
||||
|
||||
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;
|
||||
@ -114,7 +113,7 @@ public class RestRequest {
|
||||
final ArrayList<Pair<String, String>> headers = new ArrayList<>(methodInfo.getHeaders());
|
||||
|
||||
if (authorization != null && authorization.hasAuthorization()) {
|
||||
headers.add(new ImmutablePair<>("Authorization", authorization.getHeader(endpoint, methodInfo)));
|
||||
headers.add(Pair.create("Authorization", authorization.getHeader(endpoint, methodInfo)));
|
||||
}
|
||||
return new RestRequest(restMethod.value(), url, headers, methodInfo.getBody(), null);
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package org.mariotaku.simplerestapi.http;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import android.util.Pair;
|
||||
|
||||
import org.mariotaku.simplerestapi.http.mime.TypedData;
|
||||
|
||||
import java.io.Closeable;
|
||||
@ -22,7 +23,7 @@ public abstract class RestResponse implements Closeable {
|
||||
final List<Pair<String, String>> headers = getHeaders();
|
||||
if (headers == null) return null;
|
||||
for (Pair<String, String> header : headers) {
|
||||
if (header.getKey().equalsIgnoreCase(name)) return header.getValue();
|
||||
if (header.first.equalsIgnoreCase(name)) return header.second;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -33,8 +34,8 @@ public abstract class RestResponse implements Closeable {
|
||||
if (headers == null) return new String[0];
|
||||
final ArrayList<String> result = new ArrayList<>();
|
||||
for (Pair<String, String> header : headers) {
|
||||
if (name.equalsIgnoreCase(header.getKey())) {
|
||||
result.add(header.getValue());
|
||||
if (name.equalsIgnoreCase(header.first)) {
|
||||
result.add(header.second);
|
||||
}
|
||||
}
|
||||
return result.toArray(new String[result.size()]);
|
||||
|
@ -1,5 +1,7 @@
|
||||
package org.mariotaku.simplerestapi.http.mime;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import org.mariotaku.simplerestapi.http.ContentType;
|
||||
import org.mariotaku.simplerestapi.io.StreamingGZIPInputStream;
|
||||
|
||||
@ -57,7 +59,7 @@ public class BaseTypedData implements TypedData {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(OutputStream os) throws IOException {
|
||||
public void writeTo(@NonNull OutputStream os) throws IOException {
|
||||
final byte[] buffer = new byte[8192];
|
||||
for (int len; (len = stream.read(buffer)) != -1; ) {
|
||||
os.write(buffer, 0, len);
|
||||
@ -65,6 +67,7 @@ public class BaseTypedData implements TypedData {
|
||||
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public InputStream stream() {
|
||||
return stream;
|
||||
|
@ -19,6 +19,8 @@
|
||||
|
||||
package org.mariotaku.simplerestapi.http.mime;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import org.mariotaku.simplerestapi.Utils;
|
||||
import org.mariotaku.simplerestapi.http.ContentType;
|
||||
|
||||
@ -52,10 +54,11 @@ public class FileTypedData implements TypedData {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(OutputStream os) throws IOException {
|
||||
public void writeTo(@NonNull OutputStream os) throws IOException {
|
||||
Utils.copyStream(stream(), os);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public InputStream stream() throws IOException {
|
||||
if (stream != null) return stream;
|
||||
|
@ -1,6 +1,8 @@
|
||||
package org.mariotaku.simplerestapi.http.mime;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Pair;
|
||||
|
||||
import org.mariotaku.simplerestapi.Utils;
|
||||
import org.mariotaku.simplerestapi.http.ContentType;
|
||||
|
||||
@ -44,9 +46,9 @@ public class FormTypedBody implements TypedData {
|
||||
sb.append('&');
|
||||
}
|
||||
final Pair<String, String> form = forms.get(i);
|
||||
sb.append(Utils.encode(form.getKey(), charset.name()));
|
||||
sb.append(Utils.encode(form.first, charset.name()));
|
||||
sb.append('=');
|
||||
sb.append(Utils.encode(form.getValue(), charset.name()));
|
||||
sb.append(Utils.encode(form.second, charset.name()));
|
||||
}
|
||||
bytes = sb.toString().getBytes(charset);
|
||||
}
|
||||
@ -58,11 +60,12 @@ public class FormTypedBody implements TypedData {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(OutputStream os) throws IOException {
|
||||
public void writeTo(@NonNull OutputStream os) throws IOException {
|
||||
toRawBytes();
|
||||
os.write(bytes);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public InputStream stream() {
|
||||
toRawBytes();
|
||||
|
@ -20,9 +20,8 @@
|
||||
package org.mariotaku.simplerestapi.http.mime;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Pair;
|
||||
|
||||
import org.apache.commons.lang3.tuple.ImmutablePair;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.mariotaku.simplerestapi.http.ContentType;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -48,7 +47,7 @@ public class MultipartTypedBody implements TypedData {
|
||||
}
|
||||
|
||||
public void add(@NonNull String name, @NonNull TypedData data) {
|
||||
parts.add(new ImmutablePair<>(name, data));
|
||||
parts.add(Pair.create(name, data));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -66,7 +65,7 @@ public class MultipartTypedBody implements TypedData {
|
||||
if (!lengthSet) {
|
||||
length = 0;
|
||||
for (Pair<String, TypedData> part : parts) {
|
||||
length += part.getValue().length();
|
||||
length += part.second.length();
|
||||
}
|
||||
lengthSet = true;
|
||||
}
|
||||
@ -74,12 +73,13 @@ public class MultipartTypedBody implements TypedData {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(OutputStream os) throws IOException {
|
||||
public void writeTo(@NonNull OutputStream os) throws IOException {
|
||||
for (Pair<String, TypedData> part : parts) {
|
||||
part.getValue().writeTo(os);
|
||||
part.second.writeTo(os);
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public InputStream stream() throws IOException {
|
||||
return null;
|
||||
@ -88,7 +88,7 @@ public class MultipartTypedBody implements TypedData {
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
for (Pair<String, TypedData> part : parts) {
|
||||
part.getValue().close();
|
||||
part.second.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,8 @@
|
||||
package org.mariotaku.simplerestapi.http.mime;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import org.mariotaku.simplerestapi.http.ContentType;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -10,14 +13,16 @@ import java.io.OutputStream;
|
||||
* Created by mariotaku on 15/2/6.
|
||||
*/
|
||||
public interface TypedData {
|
||||
@Nullable
|
||||
ContentType contentType();
|
||||
|
||||
String contentEncoding();
|
||||
|
||||
long length() throws IOException;
|
||||
|
||||
void writeTo(OutputStream os) throws IOException;
|
||||
void writeTo(@NonNull OutputStream os) throws IOException;
|
||||
|
||||
@NonNull
|
||||
InputStream stream() throws IOException;
|
||||
|
||||
void close() throws IOException;
|
||||
|
@ -19,7 +19,9 @@
|
||||
|
||||
package org.mariotaku.twidere.api.twitter;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Pair;
|
||||
|
||||
import com.squareup.okhttp.Call;
|
||||
import com.squareup.okhttp.Headers;
|
||||
@ -30,8 +32,6 @@ 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.RestHttpClient;
|
||||
import org.mariotaku.simplerestapi.http.RestRequest;
|
||||
@ -54,7 +54,11 @@ public class OkHttpRestClient implements RestHttpClient {
|
||||
private final OkHttpClient client;
|
||||
|
||||
public OkHttpRestClient() {
|
||||
client = new OkHttpClient();
|
||||
this(new OkHttpClient());
|
||||
}
|
||||
|
||||
public OkHttpRestClient(OkHttpClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -65,7 +69,7 @@ public class OkHttpRestClient implements RestHttpClient {
|
||||
final List<Pair<String, String>> headers = restRequest.getHeaders();
|
||||
if (headers != null) {
|
||||
for (Pair<String, String> header : headers) {
|
||||
builder.addHeader(header.getKey(), header.getValue());
|
||||
builder.addHeader(header.first, header.second);
|
||||
}
|
||||
}
|
||||
final Call call = client.newCall(builder.build());
|
||||
@ -81,7 +85,9 @@ public class OkHttpRestClient implements RestHttpClient {
|
||||
|
||||
@Override
|
||||
public MediaType contentType() {
|
||||
return MediaType.parse(body.contentType().toHeader());
|
||||
final ContentType contentType = body.contentType();
|
||||
if (contentType == null) return null;
|
||||
return MediaType.parse(contentType.toHeader());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -114,7 +120,7 @@ public class OkHttpRestClient implements RestHttpClient {
|
||||
final Headers headers = response.headers();
|
||||
final ArrayList<Pair<String, String>> headersList = new ArrayList<>();
|
||||
for (int i = 0, j = headers.size(); i < j; i++) {
|
||||
headersList.add(new ImmutablePair<>(headers.name(i), headers.value(i)));
|
||||
headersList.add(Pair.create(headers.name(i), headers.value(i)));
|
||||
}
|
||||
return headersList;
|
||||
}
|
||||
@ -155,7 +161,9 @@ public class OkHttpRestClient implements RestHttpClient {
|
||||
|
||||
@Override
|
||||
public ContentType contentType() {
|
||||
return ContentType.parse(body.contentType().toString());
|
||||
final MediaType mediaType = body.contentType();
|
||||
if (mediaType == null) return null;
|
||||
return ContentType.parse(mediaType.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -169,9 +177,10 @@ public class OkHttpRestClient implements RestHttpClient {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(OutputStream os) throws IOException {
|
||||
public void writeTo(@NonNull OutputStream os) throws IOException {
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public InputStream stream() throws IOException {
|
||||
return body.byteStream();
|
||||
|
@ -1,54 +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.twidere.api.twitter;
|
||||
|
||||
import org.mariotaku.simplerestapi.RestAPIFactory;
|
||||
import org.mariotaku.simplerestapi.http.Authorization;
|
||||
import org.mariotaku.twidere.api.twitter.auth.OAuthEndpoint;
|
||||
import org.mariotaku.twidere.api.twitter.model.impl.StatusImpl;
|
||||
import org.mariotaku.twidere.api.twitter.model.impl.TypeConverterMapper;
|
||||
import org.mariotaku.twidere.api.twitter.model.impl.UserImpl;
|
||||
|
||||
import twitter4j.Status;
|
||||
import twitter4j.Twitter;
|
||||
import twitter4j.User;
|
||||
|
||||
/**
|
||||
* Created by mariotaku on 15/5/5.
|
||||
*/
|
||||
public class TwitterAPIFactory {
|
||||
|
||||
static {
|
||||
TypeConverterMapper.register(Status.class, StatusImpl.class);
|
||||
TypeConverterMapper.register(User.class, UserImpl.class);
|
||||
}
|
||||
|
||||
|
||||
public static Twitter getInstance(Authorization authorization) {
|
||||
final OAuthEndpoint endpoint = new OAuthEndpoint("https://api.twitter.com/1.1/");
|
||||
final RestAPIFactory factory = new RestAPIFactory();
|
||||
factory.setClient(new OkHttpRestClient());
|
||||
factory.setConverter(new TwitterConverter());
|
||||
factory.setEndpoint(endpoint);
|
||||
factory.setAuthorization(authorization);
|
||||
return factory.build(Twitter.class);
|
||||
}
|
||||
|
||||
}
|
@ -26,18 +26,37 @@ import org.mariotaku.simplerestapi.http.ContentType;
|
||||
import org.mariotaku.simplerestapi.http.RestResponse;
|
||||
import org.mariotaku.simplerestapi.http.mime.TypedData;
|
||||
import org.mariotaku.twidere.api.twitter.auth.OAuthToken;
|
||||
import org.mariotaku.twidere.api.twitter.model.impl.ResponseListImpl;
|
||||
import org.mariotaku.twidere.api.twitter.model.impl.SavedSearchImpl;
|
||||
import org.mariotaku.twidere.api.twitter.model.impl.StatusImpl;
|
||||
import org.mariotaku.twidere.api.twitter.model.impl.TypeConverterMapper;
|
||||
import org.mariotaku.twidere.api.twitter.model.impl.UserImpl;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.charset.Charset;
|
||||
import java.text.ParseException;
|
||||
|
||||
import twitter4j.ResponseList;
|
||||
import twitter4j.SavedSearch;
|
||||
import twitter4j.Status;
|
||||
import twitter4j.User;
|
||||
|
||||
/**
|
||||
* Created by mariotaku on 15/5/5.
|
||||
*/
|
||||
class TwitterConverter implements Converter {
|
||||
public class TwitterConverter implements Converter {
|
||||
|
||||
static {
|
||||
TypeConverterMapper.register(Status.class, StatusImpl.class);
|
||||
TypeConverterMapper.register(User.class, UserImpl.class);
|
||||
TypeConverterMapper.register(SavedSearch.class, SavedSearchImpl.class);
|
||||
// TypeConverterMapper.register(DirectMessage.class, DirectMessageImpl.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(RestResponse response, Type type) throws IOException {
|
||||
final TypedData body = response.getBody();
|
||||
@ -48,7 +67,10 @@ class TwitterConverter implements Converter {
|
||||
if (OAuthToken.class.isAssignableFrom(cls)) {
|
||||
final ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
body.writeTo(os);
|
||||
Charset charset = contentType.getCharset();
|
||||
Charset charset = contentType != null ? contentType.getCharset() : null;
|
||||
if (charset == null) {
|
||||
charset = Charset.defaultCharset();
|
||||
}
|
||||
try {
|
||||
return new OAuthToken(os.toString(charset.name()), charset);
|
||||
} catch (ParseException e) {
|
||||
@ -56,7 +78,22 @@ class TwitterConverter implements Converter {
|
||||
}
|
||||
}
|
||||
LoganSquare.parse(stream, cls);
|
||||
} else if (type instanceof ParameterizedType) {
|
||||
final Type rawType = ((ParameterizedType) type).getRawType();
|
||||
if (rawType instanceof Class<?>) {
|
||||
final Class<?> rawClass = (Class<?>) rawType;
|
||||
if (ResponseList.class.isAssignableFrom(rawClass)) {
|
||||
final Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0];
|
||||
return new ResponseListImpl<>(LoganSquare.parseList(stream, (Class<?>) elementType));
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new UnsupportedTypeException(type);
|
||||
}
|
||||
|
||||
private class UnsupportedTypeException extends UnsupportedOperationException {
|
||||
public UnsupportedTypeException(Type type) {
|
||||
super("Unsupported type " + type);
|
||||
}
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Twidere - Twitter client for Android
|
||||
* Twidere - Twitter client for Android
|
||||
*
|
||||
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
|
||||
*
|
||||
@ -17,26 +17,26 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.mariotaku.twidere.util.net;
|
||||
package org.mariotaku.twidere.api.twitter;
|
||||
|
||||
import android.content.Context;
|
||||
import com.bluelinelabs.logansquare.typeconverters.DateTypeConverter;
|
||||
|
||||
import twitter4j.http.HttpClient;
|
||||
import twitter4j.http.HttpClientConfiguration;
|
||||
import twitter4j.http.HttpClientFactory;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Created by mariotaku on 15/1/22.
|
||||
* Created by mariotaku on 15/5/7.
|
||||
*/
|
||||
public class OkHttpClientFactory implements HttpClientFactory {
|
||||
private final Context context;
|
||||
public class TwitterDateConverter extends DateTypeConverter {
|
||||
|
||||
public OkHttpClientFactory(Context context) {
|
||||
this.context = context;
|
||||
private final DateFormat mDateFormat;
|
||||
|
||||
public TwitterDateConverter() {
|
||||
mDateFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy", Locale.ENGLISH);
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpClient getInstance(HttpClientConfiguration conf) {
|
||||
return new OkHttpClientImpl(context, conf);
|
||||
public DateFormat getDateFormat() {
|
||||
return mDateFormat;
|
||||
}
|
||||
}
|
@ -20,9 +20,8 @@
|
||||
package org.mariotaku.twidere.api.twitter.auth;
|
||||
|
||||
import android.util.Base64;
|
||||
import android.util.Pair;
|
||||
|
||||
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;
|
||||
@ -35,6 +34,7 @@ import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@ -81,12 +81,12 @@ public class OAuthAuthorization implements Authorization {
|
||||
}
|
||||
if (queries != null) {
|
||||
for (Pair<String, String> query : queries) {
|
||||
encodeParams.add(encodeParameter(query.getKey(), query.getValue()));
|
||||
encodeParams.add(encodeParameter(query.first, query.second));
|
||||
}
|
||||
}
|
||||
if (forms != null) {
|
||||
for (Pair<String, String> form : forms) {
|
||||
encodeParams.add(encodeParameter(form.getKey(), form.getValue()));
|
||||
encodeParams.add(encodeParameter(form.first, form.second));
|
||||
}
|
||||
}
|
||||
Collections.sort(encodeParams);
|
||||
@ -138,16 +138,21 @@ public class OAuthAuthorization implements Authorization {
|
||||
final String oauthSignature = generateOAuthSignature(method, url, oauthNonce, timestamp, oauthToken,
|
||||
oauthTokenSecret, request.getQueries(), request.getForms());
|
||||
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));
|
||||
encodeParams.add(Pair.create("oauth_consumer_key", consumerKey));
|
||||
encodeParams.add(Pair.create("oauth_nonce", oauthNonce));
|
||||
encodeParams.add(Pair.create("oauth_signature", encode(oauthSignature)));
|
||||
encodeParams.add(Pair.create("oauth_signature_method", OAUTH_SIGNATURE_METHOD));
|
||||
encodeParams.add(Pair.create("oauth_timestamp", String.valueOf(timestamp)));
|
||||
encodeParams.add(Pair.create("oauth_version", OAUTH_VERSION));
|
||||
if (oauthToken != null) {
|
||||
encodeParams.add(new ImmutablePair<>("oauth_token", oauthToken));
|
||||
encodeParams.add(Pair.create("oauth_token", oauthToken));
|
||||
}
|
||||
Collections.sort(encodeParams);
|
||||
Collections.sort(encodeParams, new Comparator<Pair<String, String>>() {
|
||||
@Override
|
||||
public int compare(Pair<String, String> lhs, Pair<String, String> rhs) {
|
||||
return lhs.first.compareTo(rhs.first);
|
||||
}
|
||||
});
|
||||
final StringBuilder headerBuilder = new StringBuilder();
|
||||
headerBuilder.append("OAuth ");
|
||||
for (int i = 0, j = encodeParams.size(); i < j; i++) {
|
||||
@ -155,9 +160,9 @@ public class OAuthAuthorization implements Authorization {
|
||||
headerBuilder.append(", ");
|
||||
}
|
||||
final Pair<String, String> keyValuePair = encodeParams.get(i);
|
||||
headerBuilder.append(keyValuePair.getKey());
|
||||
headerBuilder.append(keyValuePair.first);
|
||||
headerBuilder.append("=\"");
|
||||
headerBuilder.append(keyValuePair.getValue());
|
||||
headerBuilder.append(keyValuePair.second);
|
||||
headerBuilder.append('\"');
|
||||
}
|
||||
return headerBuilder.toString();
|
||||
|
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* 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.twidere.api.twitter.model.impl;
|
||||
|
||||
import com.bluelinelabs.logansquare.annotation.JsonObject;
|
||||
|
||||
import twitter4j.DirectMessage;
|
||||
|
||||
/**
|
||||
* Created by mariotaku on 15/5/7.
|
||||
*/
|
||||
@JsonObject
|
||||
public class DirectMessageImpl {
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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.twidere.api.twitter.model.impl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import twitter4j.RateLimitStatus;
|
||||
import twitter4j.ResponseList;
|
||||
|
||||
/**
|
||||
* Created by mariotaku on 15/5/7.
|
||||
*/
|
||||
public class ResponseListImpl<T> extends ArrayList<T> implements ResponseList<T> {
|
||||
|
||||
@Override
|
||||
public int getAccessLevel() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public ResponseListImpl(int capacity) {
|
||||
super(capacity);
|
||||
}
|
||||
|
||||
public ResponseListImpl() {
|
||||
}
|
||||
|
||||
public ResponseListImpl(Collection<? extends T> collection) {
|
||||
super(collection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RateLimitStatus getRateLimitStatus() {
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* 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.twidere.api.twitter.model.impl;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.bluelinelabs.logansquare.annotation.JsonField;
|
||||
import com.bluelinelabs.logansquare.annotation.JsonObject;
|
||||
|
||||
import org.mariotaku.twidere.api.twitter.TwitterDateConverter;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import twitter4j.RateLimitStatus;
|
||||
import twitter4j.SavedSearch;
|
||||
|
||||
/**
|
||||
* Created by mariotaku on 15/5/7.
|
||||
*/
|
||||
@JsonObject
|
||||
public class SavedSearchImpl implements SavedSearch {
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getQuery() {
|
||||
return query;
|
||||
}
|
||||
|
||||
@JsonField(name = "id")
|
||||
int id;
|
||||
|
||||
@JsonField(name = "created_at", typeConverter = TwitterDateConverter.class)
|
||||
Date createdAt;
|
||||
|
||||
@JsonField(name = "name")
|
||||
String name;
|
||||
|
||||
@JsonField(name = "position")
|
||||
int position;
|
||||
|
||||
@JsonField(name = "query")
|
||||
String query;
|
||||
|
||||
@Override
|
||||
public int compareTo(@NonNull SavedSearch another) {
|
||||
return id - another.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAccessLevel() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RateLimitStatus getRateLimitStatus() {
|
||||
return null;
|
||||
}
|
||||
}
|
@ -24,6 +24,8 @@ import android.support.annotation.NonNull;
|
||||
import com.bluelinelabs.logansquare.annotation.JsonField;
|
||||
import com.bluelinelabs.logansquare.annotation.JsonObject;
|
||||
|
||||
import org.mariotaku.twidere.api.twitter.TwitterDateConverter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
|
||||
@ -44,7 +46,7 @@ import twitter4j.UserMentionEntity;
|
||||
@JsonObject
|
||||
public class StatusImpl implements Status {
|
||||
|
||||
@JsonField(name = "created_at")
|
||||
@JsonField(name = "created_at", typeConverter = TwitterDateConverter.class)
|
||||
Date createdAt;
|
||||
|
||||
@JsonField(name = "id")
|
||||
@ -163,7 +165,6 @@ public class StatusImpl implements Status {
|
||||
|
||||
@Override
|
||||
public Date getCreatedAt() {
|
||||
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
|
@ -19,12 +19,15 @@
|
||||
|
||||
package org.mariotaku.twidere.api.twitter.model.impl;
|
||||
|
||||
import com.bluelinelabs.logansquare.JsonMapper;
|
||||
import com.bluelinelabs.logansquare.LoganSquare;
|
||||
import com.bluelinelabs.logansquare.typeconverters.TypeConverter;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by mariotaku on 15/5/5.
|
||||
@ -49,5 +52,22 @@ public class TypeConverterMapper<T> implements TypeConverter<T> {
|
||||
|
||||
public static <T> void register(Class<T> cls, Class<? extends T> impl) {
|
||||
LoganSquare.registerTypeConverter(cls, new TypeConverterMapper<>(impl));
|
||||
try {
|
||||
//noinspection unchecked
|
||||
final Field objectMappersField = LoganSquare.class.getDeclaredField("OBJECT_MAPPERS");
|
||||
objectMappersField.setAccessible(true);
|
||||
final Map<Class, JsonMapper> mappers = (Map<Class, JsonMapper>) objectMappersField.get(null);
|
||||
mappers.put(cls, (JsonMapper) Class.forName(impl.getName() + "$$JsonObjectMapper").newInstance());
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (NoSuchFieldException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InstantiationException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -25,6 +25,8 @@ import android.support.annotation.NonNull;
|
||||
import com.bluelinelabs.logansquare.annotation.JsonField;
|
||||
import com.bluelinelabs.logansquare.annotation.JsonObject;
|
||||
|
||||
import org.mariotaku.twidere.api.twitter.TwitterDateConverter;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import twitter4j.RateLimitStatus;
|
||||
@ -74,7 +76,7 @@ public class UserImpl implements User {
|
||||
@JsonField(name = "listed_count")
|
||||
long listedCount;
|
||||
|
||||
@JsonField(name = "created_at")
|
||||
@JsonField(name = "created_at", typeConverter = TwitterDateConverter.class)
|
||||
Date createdAt;
|
||||
|
||||
@JsonField(name = "favourites_count")
|
||||
|
@ -36,12 +36,12 @@ public interface CardEntity extends Serializable {
|
||||
|
||||
java.util.Map<String, BindingValue> getBindingValues();
|
||||
|
||||
public interface BindingValue extends Serializable {
|
||||
interface BindingValue extends Serializable {
|
||||
|
||||
public static final String TYPE_STRING = "STRING";
|
||||
public static final String TYPE_IMAGE = "IMAGE";
|
||||
public static final String TYPE_USER = "USER";
|
||||
public static final String TYPE_BOOLEAN = "BOOLEAN";
|
||||
String TYPE_STRING = "STRING";
|
||||
String TYPE_IMAGE = "IMAGE";
|
||||
String TYPE_USER = "USER";
|
||||
String TYPE_BOOLEAN = "BOOLEAN";
|
||||
|
||||
String getName();
|
||||
|
||||
@ -49,19 +49,19 @@ public interface CardEntity extends Serializable {
|
||||
}
|
||||
|
||||
|
||||
public interface UserValue extends BindingValue {
|
||||
interface UserValue extends BindingValue {
|
||||
long getUserId();
|
||||
}
|
||||
|
||||
public interface StringValue extends BindingValue {
|
||||
interface StringValue extends BindingValue {
|
||||
String getValue();
|
||||
}
|
||||
|
||||
public interface BooleanValue extends BindingValue {
|
||||
interface BooleanValue extends BindingValue {
|
||||
boolean getValue();
|
||||
}
|
||||
|
||||
public interface ImageValue extends BindingValue {
|
||||
interface ImageValue extends BindingValue {
|
||||
int getWidth();
|
||||
|
||||
int getHeight();
|
||||
|
@ -39,6 +39,26 @@ public class Paging implements ValueMap {
|
||||
this.cursor = cursor;
|
||||
}
|
||||
|
||||
public long getSinceId() {
|
||||
return sinceId;
|
||||
}
|
||||
|
||||
public long getMaxId() {
|
||||
return maxId;
|
||||
}
|
||||
|
||||
public long getCursor() {
|
||||
return cursor;
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public int getPage() {
|
||||
return page;
|
||||
}
|
||||
|
||||
public Paging sinceId(long sinceId) {
|
||||
this.sinceId = sinceId;
|
||||
return this;
|
||||
|
@ -21,7 +21,7 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* List of TwitterResponse.
|
||||
*
|
||||
*
|
||||
* @author Yusuke Yamamoto - yusuke at mac.com
|
||||
*/
|
||||
public interface ResponseList<T> extends TwitterResponse, List<T>, Serializable {
|
||||
|
@ -16,6 +16,8 @@
|
||||
|
||||
package twitter4j;
|
||||
|
||||
import com.bluelinelabs.logansquare.annotation.JsonObject;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
@ -25,7 +27,7 @@ import java.util.Date;
|
||||
* @author Yusuke Yamamoto - yusuke at mac.com
|
||||
* @since Twitter4J 2.0.8
|
||||
*/
|
||||
public interface SavedSearch extends Comparable<SavedSearch>, TwitterResponse, Serializable {
|
||||
public interface SavedSearch extends Comparable<SavedSearch>, TwitterResponse {
|
||||
Date getCreatedAt();
|
||||
|
||||
int getId();
|
||||
|
@ -27,7 +27,7 @@ import twitter4j.conf.Configuration;
|
||||
* @author Yusuke Yamamoto - yusuke at mac.com
|
||||
* @since Twitter4J 2.2.0
|
||||
*/
|
||||
public interface TwitterBase {
|
||||
public interface TwitterBase extends TwitterOAuth{
|
||||
|
||||
/**
|
||||
* Registers a RateLimitStatusListener for account associated rate limits
|
||||
|
@ -19,13 +19,34 @@
|
||||
|
||||
package twitter4j;
|
||||
|
||||
import android.net.SSLCertificateSocketFactory;
|
||||
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import com.squareup.okhttp.internal.Internal;
|
||||
import com.squareup.okhttp.internal.Network;
|
||||
|
||||
import org.mariotaku.simplerestapi.RestAPIFactory;
|
||||
import org.mariotaku.simplerestapi.http.Authorization;
|
||||
import org.mariotaku.twidere.api.twitter.TwitterAPIFactory;
|
||||
import org.mariotaku.twidere.api.twitter.OkHttpRestClient;
|
||||
import org.mariotaku.twidere.api.twitter.TwitterConverter;
|
||||
import org.mariotaku.twidere.api.twitter.auth.OAuthAuthorization;
|
||||
import org.mariotaku.twidere.api.twitter.auth.OAuthEndpoint;
|
||||
import org.mariotaku.twidere.api.twitter.auth.OAuthToken;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Proxy;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.net.SocketFactory;
|
||||
|
||||
import twitter4j.conf.Configuration;
|
||||
import twitter4j.conf.ConfigurationContext;
|
||||
import twitter4j.http.HostAddressResolver;
|
||||
import twitter4j.http.HostAddressResolverFactory;
|
||||
import twitter4j.http.HttpClientConfiguration;
|
||||
|
||||
/**
|
||||
* A factory class for Twitter. <br>
|
||||
@ -58,17 +79,6 @@ public final class TwitterFactory {
|
||||
this.conf = conf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a OAuth Authenticated instance.<br>
|
||||
* consumer key and consumer Secret must be provided by
|
||||
* twitter4j.properties, or system properties.<br>
|
||||
* Unlike {@link twitter4j.Twitter#setOAuthAccessToken(twitter4j.auth.AccessToken)} ,
|
||||
* this factory method potentially returns a cached instance.
|
||||
*
|
||||
* @param accessToken access token
|
||||
* @return an instance
|
||||
* @since Twitter4J 2.1.9
|
||||
*/
|
||||
public Twitter getInstance(final OAuthToken accessToken) {
|
||||
final String consumerKey = conf.getOAuthConsumerKey();
|
||||
final String consumerSecret = conf.getOAuthConsumerSecret();
|
||||
@ -79,7 +89,54 @@ public final class TwitterFactory {
|
||||
}
|
||||
|
||||
public Twitter getInstance(final Authorization auth) {
|
||||
return TwitterAPIFactory.getInstance(auth);
|
||||
final OAuthEndpoint endpoint = new OAuthEndpoint(conf.getRestBaseURL(), conf.getSigningRestBaseURL());
|
||||
final RestAPIFactory factory = new RestAPIFactory();
|
||||
factory.setClient(new OkHttpRestClient(createHttpClient(conf)));
|
||||
factory.setConverter(new TwitterConverter());
|
||||
factory.setEndpoint(endpoint);
|
||||
factory.setAuthorization(auth);
|
||||
return factory.build(Twitter.class);
|
||||
}
|
||||
|
||||
public Twitter getInstance() {
|
||||
return getInstance(new OAuthToken(conf.getOAuthConsumerKey(), conf.getOAuthConsumerSecret()));
|
||||
}
|
||||
|
||||
|
||||
private static OkHttpClient createHttpClient(HttpClientConfiguration conf) {
|
||||
final OkHttpClient client = new OkHttpClient();
|
||||
final boolean ignoreSSLError = conf.isSSLErrorIgnored();
|
||||
final SSLCertificateSocketFactory sslSocketFactory;
|
||||
final HostAddressResolverFactory resolverFactory = conf.getHostAddressResolverFactory();
|
||||
if (ignoreSSLError) {
|
||||
sslSocketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory.getInsecure(0, null);
|
||||
} else {
|
||||
sslSocketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory.getDefault(0, null);
|
||||
}
|
||||
// sslSocketFactory.setTrustManagers(new TrustManager[]{new TwidereTrustManager(context)});
|
||||
// client.setHostnameVerifier(new HostResolvedHostnameVerifier(context, ignoreSSLError));
|
||||
client.setSslSocketFactory(sslSocketFactory);
|
||||
client.setSocketFactory(SocketFactory.getDefault());
|
||||
client.setConnectTimeout(conf.getHttpConnectionTimeout(), TimeUnit.MILLISECONDS);
|
||||
|
||||
if (conf.isProxyConfigured()) {
|
||||
client.setProxy(new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved(conf.getHttpProxyHost(),
|
||||
conf.getHttpProxyPort())));
|
||||
}
|
||||
if (resolverFactory != null) {
|
||||
final HostAddressResolver resolver = resolverFactory.getInstance(conf);
|
||||
Internal.instance.setNetwork(client, new Network() {
|
||||
@Override
|
||||
public InetAddress[] resolveInetAddresses(String host) throws UnknownHostException {
|
||||
try {
|
||||
return resolver.resolve(host);
|
||||
} catch (IOException e) {
|
||||
if (e instanceof UnknownHostException) throw (UnknownHostException) e;
|
||||
throw new UnknownHostException("Unable to resolve address " + e.getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,13 @@
|
||||
|
||||
package twitter4j.api;
|
||||
|
||||
import org.mariotaku.simplerestapi.http.BodyType;
|
||||
import org.mariotaku.simplerestapi.method.GET;
|
||||
import org.mariotaku.simplerestapi.method.POST;
|
||||
import org.mariotaku.simplerestapi.param.Body;
|
||||
import org.mariotaku.simplerestapi.param.Form;
|
||||
import org.mariotaku.simplerestapi.param.Query;
|
||||
|
||||
import twitter4j.DirectMessage;
|
||||
import twitter4j.Paging;
|
||||
import twitter4j.ResponseList;
|
||||
@ -25,127 +32,38 @@ import twitter4j.TwitterException;
|
||||
* @author Joern Huxhorn - jhuxhorn at googlemail.com
|
||||
*/
|
||||
public interface DirectMessagesResources {
|
||||
/**
|
||||
* Destroys the direct message specified in the required ID parameter. The
|
||||
* authenticating user must be the recipient of the specified direct
|
||||
* message. <br>
|
||||
* This method calls http://api.twitter.com/1.1/direct_messages/destroy
|
||||
*
|
||||
* @param id the ID of the direct message to destroy
|
||||
* @return the deleted direct message
|
||||
* @throws TwitterException when Twitter service or network is unavailable
|
||||
* @see <a
|
||||
* href="https://dev.twitter.com/docs/api/1.1/post/direct_messages/destroy/:id">POST
|
||||
* direct_messages/destroy/:id | Twitter Developers</a>
|
||||
* @since Twitter4J 2.0.1
|
||||
*/
|
||||
DirectMessage destroyDirectMessage(long id) throws TwitterException;
|
||||
|
||||
/**
|
||||
* Returns a list of the direct messages sent to the authenticating user. <br>
|
||||
* This method calls http://api.twitter.com/1.1/direct_messages
|
||||
*
|
||||
* @return List
|
||||
* @throws TwitterException when Twitter service or network is unavailable
|
||||
* @see <a
|
||||
* href="https://dev.twitter.com/docs/api/1.1/get/direct_messages">GET
|
||||
* direct_messages | Twitter Developers</a>
|
||||
*/
|
||||
ResponseList<DirectMessage> getDirectMessages() throws TwitterException;
|
||||
@POST("/direct_messages/destroy.json")
|
||||
@Body(BodyType.FORM)
|
||||
DirectMessage destroyDirectMessage(@Form("id") long id) throws TwitterException;
|
||||
|
||||
/**
|
||||
* Returns a list of the direct messages sent to the authenticating user. <br>
|
||||
* This method calls http://api.twitter.com/1.1/direct_messages
|
||||
*
|
||||
* @param paging controls pagination. Supports since_id, max_id, count and
|
||||
* page parameters.
|
||||
* @return List
|
||||
* @throws TwitterException when Twitter service or network is unavailable
|
||||
* @see <a
|
||||
* href="https://dev.twitter.com/docs/api/1.1/get/direct_messages">GET
|
||||
* direct_messages | Twitter Developers</a>
|
||||
*/
|
||||
ResponseList<DirectMessage> getDirectMessages(Paging paging) throws TwitterException;
|
||||
@GET("/direct_messages.json")
|
||||
ResponseList<DirectMessage> getDirectMessages() throws TwitterException;
|
||||
|
||||
/**
|
||||
* Returns a list of the direct messages sent by the authenticating user. <br>
|
||||
* This method calls http://api.twitter.com/1.1/direct_messages/sent
|
||||
*
|
||||
* @return List
|
||||
* @throws TwitterException when Twitter service or network is unavailable
|
||||
* @see <a
|
||||
* href="https://dev.twitter.com/docs/api/1.1/get/direct_messages/sent">GET
|
||||
* direct_messages/sent | Twitter Developers</a>
|
||||
*/
|
||||
ResponseList<DirectMessage> getSentDirectMessages() throws TwitterException;
|
||||
@GET("/direct_messages.json")
|
||||
ResponseList<DirectMessage> getDirectMessages(@Query({"since_id", "max_id", "count"}) Paging paging) throws TwitterException;
|
||||
|
||||
/**
|
||||
* Returns a list of the direct messages sent by the authenticating user. <br>
|
||||
* This method calls http://api.twitter.com/1.1/direct_messages/sent
|
||||
*
|
||||
* @param paging controls pagination. Supports since_id, max_id, count and
|
||||
* page parameters.
|
||||
* @return List
|
||||
* @throws TwitterException when Twitter service or network is unavailable
|
||||
* @see <a
|
||||
* href="https://dev.twitter.com/docs/api/1.1/get/direct_messages/sent">GET
|
||||
* direct_messages/sent | Twitter Developers</a>
|
||||
* @since Twitter4J 2.0.1
|
||||
*/
|
||||
ResponseList<DirectMessage> getSentDirectMessages(Paging paging) throws TwitterException;
|
||||
@GET("/direct_messages/sent.json")
|
||||
ResponseList<DirectMessage> getSentDirectMessages() throws TwitterException;
|
||||
|
||||
DirectMessage sendDirectMessage(long userId, String text) throws TwitterException;
|
||||
@GET("/direct_messages/sent.json")
|
||||
ResponseList<DirectMessage> getSentDirectMessages(@Query({"since_id", "max_id", "count"}) Paging paging) throws TwitterException;
|
||||
|
||||
/**
|
||||
* Sends a new direct message to the specified user from the authenticating
|
||||
* user. Requires both the user and text parameters below. The text will be
|
||||
* trimmed if the length of the text is exceeding 140 characters. <br>
|
||||
* This method calls http://api.twitter.com/1.1/direct_messages/new
|
||||
*
|
||||
* @param userId the screen name of the user to whom send the direct message
|
||||
* @param text The text of your direct message.
|
||||
* @return DirectMessage
|
||||
* @throws TwitterException when Twitter service or network is unavailable
|
||||
* @see <a
|
||||
* href="https://dev.twitter.com/docs/api/1.1/post/direct_messages/new">POST
|
||||
* direct_messages/new | Twitter Developers</a>
|
||||
* @since Twitter4j 2.1.0
|
||||
*/
|
||||
DirectMessage sendDirectMessage(long userId, String text, long mediaId) throws TwitterException;
|
||||
@POST("/direct_messages/new.json")
|
||||
@Body(BodyType.FORM)
|
||||
DirectMessage sendDirectMessage(@Form("user_id") long userId, @Form("text") String text) throws TwitterException;
|
||||
|
||||
DirectMessage sendDirectMessage(String screenName, String text) throws TwitterException;
|
||||
@POST("/direct_messages/new.json")
|
||||
@Body(BodyType.FORM)
|
||||
DirectMessage sendDirectMessage(@Form("user_id") long userId, @Form("text") String text, @Form("media_id") long mediaId) throws TwitterException;
|
||||
|
||||
/**
|
||||
* Sends a new direct message to the specified user from the authenticating
|
||||
* user. Requires both the user and text parameters below. The text will be
|
||||
* trimmed if the length of the text is exceeding 140 characters. <br>
|
||||
* This method calls http://api.twitter.com/1.1/direct_messages/new
|
||||
*
|
||||
* @param screenName the screen name of the user to whom send the direct
|
||||
* message
|
||||
* @param text The text of your direct message.
|
||||
* @return DirectMessage
|
||||
* @throws TwitterException when Twitter service or network is unavailable
|
||||
* @see <a
|
||||
* href="https://dev.twitter.com/docs/api/1.1/post/direct_messages/new">POST
|
||||
* direct_messages/new | Twitter Developers</a>
|
||||
*/
|
||||
DirectMessage sendDirectMessage(String screenName, String text, long mediaId) throws TwitterException;
|
||||
@POST("/direct_messages/new.json")
|
||||
@Body(BodyType.FORM)
|
||||
DirectMessage sendDirectMessage(@Form("screen_name") String screenName, @Form("text") String text) throws TwitterException;
|
||||
|
||||
/**
|
||||
* Returns a single direct message, specified by an id parameter. <br>
|
||||
* This method has not been finalized and the interface is subject to change
|
||||
* in incompatible ways. <br>
|
||||
* This method calls
|
||||
* http://api.twitter.com/1.1/direct_messages/show/:id.json
|
||||
*
|
||||
* @param id message id
|
||||
* @return DirectMessage
|
||||
* @throws TwitterException when Twitter service or network is unavailable
|
||||
* @see <a
|
||||
* href="http://groups.google.com/group/twitter-api-announce/msg/34909da7c399169e">#newtwitter
|
||||
* and the API - Twitter API Announcements | Google Group</a>
|
||||
* @since Twitter4J 2.1.9
|
||||
*/
|
||||
DirectMessage showDirectMessage(long id) throws TwitterException;
|
||||
@POST("/direct_messages/new.json")
|
||||
@Body(BodyType.FORM)
|
||||
DirectMessage sendDirectMessage(@Form("screen_name") String screenName, @Form("text") String text, @Form("media_id") long mediaId) throws TwitterException;
|
||||
|
||||
DirectMessage showDirectMessage(long id) throws TwitterException;
|
||||
}
|
||||
|
@ -1,86 +1,37 @@
|
||||
/*
|
||||
* Copyright 2007 Yusuke Yamamoto
|
||||
* Twidere - Twitter client for Android
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* 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.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* 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 twitter4j.api;
|
||||
|
||||
import org.mariotaku.simplerestapi.method.GET;
|
||||
|
||||
import twitter4j.ResponseList;
|
||||
import twitter4j.SavedSearch;
|
||||
import twitter4j.TwitterException;
|
||||
|
||||
/**
|
||||
* @author Joern Huxhorn - jhuxhorn at googlemail.com
|
||||
*/
|
||||
public interface SavedSearchesResources {
|
||||
/**
|
||||
* Creates a saved search for the authenticated user. <br>
|
||||
* This method calls
|
||||
* http://api.twitter.com/1.1/saved_searches/saved_searches/create.json
|
||||
*
|
||||
* @param query the query string
|
||||
* @return the data for a created saved search
|
||||
* @throws TwitterException when Twitter service or network is unavailable
|
||||
* @see <a
|
||||
* href="https://dev.twitter.com/docs/api/1.1/post/saved_searches/create">POST
|
||||
* saved_searches/create | Twitter Developers</a>
|
||||
* @since Twitter4J 2.0.8
|
||||
*/
|
||||
SavedSearch createSavedSearch(String query) throws TwitterException;
|
||||
SavedSearch createSavedSearch(String query) throws TwitterException;
|
||||
|
||||
/**
|
||||
* Destroys a saved search for the authenticated user. The search specified
|
||||
* by id must be owned by the authenticating user. <br>
|
||||
* This method calls
|
||||
* http://api.twitter.com/1.1/saved_searches/destroy/id.json
|
||||
*
|
||||
* @param id The id of the saved search to be deleted.
|
||||
* @return the data for a destroyed saved search
|
||||
* @throws TwitterException when Twitter service or network is unavailable
|
||||
* @see <a
|
||||
* href="https://dev.twitter.com/docs/api/1.1/post/saved_searches/destroy/:id">POST
|
||||
* saved_searches/destroy/:id | Twitter Developers</a>
|
||||
* @since Twitter4J 2.0.8
|
||||
*/
|
||||
SavedSearch destroySavedSearch(int id) throws TwitterException;
|
||||
SavedSearch destroySavedSearch(int id) throws TwitterException;
|
||||
|
||||
/**
|
||||
* Returns the authenticated user's saved search queries. <br>
|
||||
* This method calls http://api.twitter.com/1.1/saved_searches.json
|
||||
*
|
||||
* @return Returns an array of numeric user ids the authenticating user is
|
||||
* blocking.
|
||||
* @throws TwitterException when Twitter service or network is unavailable
|
||||
* @see <a
|
||||
* href="https://dev.twitter.com/docs/api/1.1/get/saved_searches">GET
|
||||
* saved_searches | Twitter Developers</a>
|
||||
* @since Twitter4J 2.0.8
|
||||
*/
|
||||
ResponseList<SavedSearch> getSavedSearches() throws TwitterException;
|
||||
@GET("/saved_searches/list.json")
|
||||
ResponseList<SavedSearch> getSavedSearches() throws TwitterException;
|
||||
|
||||
/**
|
||||
* Retrieve the data for a saved search owned by the authenticating user
|
||||
* specified by the given id. <br>
|
||||
* This method calls http://api.twitter.com/1.1/saved_searches/show/:id.json
|
||||
*
|
||||
* @param id The id of the saved search to be retrieved.
|
||||
* @return the data for a saved search
|
||||
* @throws TwitterException when Twitter service or network is unavailable
|
||||
* @see <a
|
||||
* href="https://dev.twitter.com/docs/api/1.1/get/saved_searches/show/:id">GET
|
||||
* saved_searches/show/:id | Twitter Developers</a>
|
||||
* @since Twitter4J 2.0.8
|
||||
*/
|
||||
SavedSearch showSavedSearch(int id) throws TwitterException;
|
||||
SavedSearch showSavedSearch(int id) throws TwitterException;
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import org.mariotaku.simplerestapi.http.RestRequest;
|
||||
import org.mariotaku.simplerestapi.http.mime.FileTypedData;
|
||||
import org.mariotaku.simplerestapi.http.mime.MultipartTypedBody;
|
||||
import org.mariotaku.simplerestapi.method.POST;
|
||||
import org.mariotaku.twidere.util.TwitterAPIUtils;
|
||||
import org.mariotaku.twidere.util.Utils;
|
||||
|
||||
import java.io.File;
|
||||
@ -39,7 +40,7 @@ public class SpiceAsyUploadTask extends AsyncTask<Object, Object, Object> {
|
||||
|
||||
public SpiceAsyUploadTask(final Context context) {
|
||||
this.context = context;
|
||||
this.client = Utils.getDefaultHttpClient(context);
|
||||
this.client = TwitterAPIUtils.getDefaultHttpClient(context);
|
||||
}
|
||||
|
||||
|
||||
|
@ -11,7 +11,7 @@ import org.mariotaku.simplerestapi.http.RestResponse;
|
||||
import org.mariotaku.simplerestapi.http.mime.FileTypedData;
|
||||
import org.mariotaku.simplerestapi.http.mime.MultipartTypedBody;
|
||||
import org.mariotaku.simplerestapi.method.POST;
|
||||
import org.mariotaku.twidere.util.Utils;
|
||||
import org.mariotaku.twidere.util.TwitterAPIUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
@ -37,7 +37,7 @@ public class UploadTask extends AsyncTask<Object, Object, Object> {
|
||||
|
||||
public UploadTask(final Context context) {
|
||||
this.context = context;
|
||||
this.client = Utils.getDefaultHttpClient(context);
|
||||
this.client = TwitterAPIUtils.getDefaultHttpClient(context);
|
||||
device_id = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
|
||||
}
|
||||
|
||||
|
@ -47,7 +47,6 @@ import org.mariotaku.twidere.util.AsyncTaskUtils;
|
||||
import org.mariotaku.twidere.util.OAuthPasswordAuthenticator;
|
||||
import org.mariotaku.twidere.util.ParseUtils;
|
||||
import org.mariotaku.twidere.util.Utils;
|
||||
import org.mariotaku.twidere.util.net.OkHttpClientFactory;
|
||||
import org.mariotaku.twidere.util.net.TwidereHostResolverFactory;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
@ -56,7 +55,6 @@ import java.io.StringReader;
|
||||
|
||||
import twitter4j.Twitter;
|
||||
import twitter4j.TwitterConstants;
|
||||
import twitter4j.TwitterException;
|
||||
import twitter4j.TwitterFactory;
|
||||
import twitter4j.conf.ConfigurationBuilder;
|
||||
|
||||
@ -238,7 +236,6 @@ public class BrowserSignInActivity extends BaseSupportDialogActivity implements
|
||||
final String consumerSecret = getNonEmptyString(mPreferences, KEY_CONSUMER_SECRET,
|
||||
TWITTER_CONSUMER_SECRET_3);
|
||||
cb.setHostAddressResolverFactory(new TwidereHostResolverFactory(mApplication));
|
||||
cb.setHttpClientFactory(new OkHttpClientFactory(mApplication));
|
||||
Utils.setClientUserAgent(mActivity, consumerKey, consumerSecret, cb);
|
||||
cb.setRestBaseURL(DEFAULT_REST_BASE_URL);
|
||||
cb.setOAuthBaseURL(DEFAULT_OAUTH_BASE_URL);
|
||||
@ -263,8 +260,8 @@ public class BrowserSignInActivity extends BaseSupportDialogActivity implements
|
||||
}
|
||||
try {
|
||||
final Twitter twitter = new TwitterFactory(cb.build()).getInstance();
|
||||
return twitter.getOAuthRequestToken(OAUTH_CALLBACK_OOB);
|
||||
} catch (final TwitterException e) {
|
||||
return twitter.getRequestToken(OAUTH_CALLBACK_OOB);
|
||||
} catch (final Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
@ -281,7 +278,8 @@ public class BrowserSignInActivity extends BaseSupportDialogActivity implements
|
||||
}
|
||||
return;
|
||||
}
|
||||
mActivity.loadUrl(data.getAuthorizationURL());
|
||||
//TODO
|
||||
// mActivity.loadUrl(data.getAuthorizationURL());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -61,6 +61,7 @@ import com.meizu.flyme.reflect.StatusBarProxy;
|
||||
|
||||
import org.mariotaku.twidere.R;
|
||||
import org.mariotaku.twidere.activity.SettingsActivity;
|
||||
import org.mariotaku.twidere.api.twitter.auth.BasicAuthorization;
|
||||
import org.mariotaku.twidere.api.twitter.auth.EmptyAuthorization;
|
||||
import org.mariotaku.twidere.api.twitter.auth.OAuthToken;
|
||||
import org.mariotaku.twidere.app.TwidereApplication;
|
||||
@ -79,7 +80,6 @@ import org.mariotaku.twidere.util.ThemeUtils;
|
||||
import org.mariotaku.twidere.util.TwidereActionModeForChildListener;
|
||||
import org.mariotaku.twidere.util.TwidereColorUtils;
|
||||
import org.mariotaku.twidere.util.Utils;
|
||||
import org.mariotaku.twidere.util.net.OkHttpClientFactory;
|
||||
import org.mariotaku.twidere.util.net.TwidereHostResolverFactory;
|
||||
import org.mariotaku.twidere.util.support.ViewSupport;
|
||||
import org.mariotaku.twidere.util.support.view.ViewOutlineProviderCompat;
|
||||
@ -89,6 +89,7 @@ import twitter4j.Twitter;
|
||||
import twitter4j.TwitterConstants;
|
||||
import twitter4j.TwitterException;
|
||||
import twitter4j.TwitterFactory;
|
||||
import twitter4j.TwitterOAuth;
|
||||
import twitter4j.User;
|
||||
import twitter4j.conf.Configuration;
|
||||
import twitter4j.conf.ConfigurationBuilder;
|
||||
@ -382,7 +383,6 @@ public class SignInActivity extends BaseAppCompatActivity implements TwitterCons
|
||||
final boolean ignore_ssl_error = mPreferences.getBoolean(KEY_IGNORE_SSL_ERROR, false);
|
||||
final boolean enable_proxy = mPreferences.getBoolean(KEY_ENABLE_PROXY, false);
|
||||
cb.setHostAddressResolverFactory(new TwidereHostResolverFactory(mApplication));
|
||||
cb.setHttpClientFactory(new OkHttpClientFactory(mApplication));
|
||||
Utils.setClientUserAgent(this, mConsumerKey, mConsumerSecret, cb);
|
||||
final String apiUrlFormat = TextUtils.isEmpty(mAPIUrlFormat) ? DEFAULT_TWITTER_API_URL_FORMAT : mAPIUrlFormat;
|
||||
final String versionSuffix = mNoVersionSuffix ? null : "/1.1/";
|
||||
@ -615,40 +615,40 @@ public class SignInActivity extends BaseAppCompatActivity implements TwitterCons
|
||||
public static class BrowserSignInTask extends AbstractSignInTask {
|
||||
|
||||
private final Configuration conf;
|
||||
private final String request_token, request_token_secret, oauth_verifier;
|
||||
private final String requestToken, requestTokenSecret, oauthVerifier;
|
||||
|
||||
private final Context context;
|
||||
private final String api_url_format;
|
||||
private final boolean same_oauth_signing_url, no_version_suffix;
|
||||
private final String apiUrlFormat;
|
||||
private final boolean sameOauthSigningUrl, noVersionSuffix;
|
||||
|
||||
public BrowserSignInTask(final SignInActivity context, final Configuration conf,
|
||||
final String request_token, final String request_token_secret,
|
||||
final String oauth_verifier, final String api_url_format,
|
||||
final boolean same_oauth_signing_url, final boolean no_version_suffix) {
|
||||
final String requestToken, final String requestTokenSecret,
|
||||
final String oauthVerifier, final String apiUrlFormat,
|
||||
final boolean sameOauthSigningUrl, final boolean noVersionSuffix) {
|
||||
super(context, conf);
|
||||
this.context = context;
|
||||
this.conf = conf;
|
||||
this.request_token = request_token;
|
||||
this.request_token_secret = request_token_secret;
|
||||
this.oauth_verifier = oauth_verifier;
|
||||
this.api_url_format = api_url_format;
|
||||
this.same_oauth_signing_url = same_oauth_signing_url;
|
||||
this.no_version_suffix = no_version_suffix;
|
||||
this.requestToken = requestToken;
|
||||
this.requestTokenSecret = requestTokenSecret;
|
||||
this.oauthVerifier = oauthVerifier;
|
||||
this.apiUrlFormat = apiUrlFormat;
|
||||
this.sameOauthSigningUrl = sameOauthSigningUrl;
|
||||
this.noVersionSuffix = noVersionSuffix;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SignInResponse doInBackground(final Object... params) {
|
||||
try {
|
||||
final Twitter twitter = new TwitterFactory(conf).getInstance();
|
||||
final OAuthToken access_token = twitter.getOAuthOAuthToken(new RequestToken(conf, request_token,
|
||||
request_token_secret), oauth_verifier);
|
||||
final long userId = access_token.getUserId();
|
||||
final OAuthToken accessToken = twitter.getAccessToken(new OAuthToken(requestToken,
|
||||
requestTokenSecret), oauthVerifier);
|
||||
final long userId = accessToken.getUserId();
|
||||
if (userId <= 0) return new SignInResponse(false, false, null);
|
||||
final User user = twitter.verifyCredentials();
|
||||
if (isUserLoggedIn(context, userId)) return new SignInResponse(true, false, null);
|
||||
final int color = analyseUserProfileColor(user);
|
||||
return new SignInResponse(conf, access_token, user, Accounts.AUTH_TYPE_OAUTH, color,
|
||||
api_url_format, same_oauth_signing_url, no_version_suffix);
|
||||
return new SignInResponse(conf, accessToken, user, Accounts.AUTH_TYPE_OAUTH, color,
|
||||
apiUrlFormat, sameOauthSigningUrl, noVersionSuffix);
|
||||
} catch (final TwitterException e) {
|
||||
return new SignInResponse(false, false, e);
|
||||
}
|
||||
@ -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.getOAuthOAuthToken(username, password);
|
||||
final OAuthToken access_token = authenticator.getOAuthAccessToken(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,7 +754,7 @@ public class SignInActivity extends BaseAppCompatActivity implements TwitterCons
|
||||
|
||||
private SignInResponse authxAuth() throws TwitterException {
|
||||
final Twitter twitter = new TwitterFactory(conf).getInstance();
|
||||
final OAuthToken OAuthToken = twitter.getOAuthOAuthToken(username, password);
|
||||
final OAuthToken OAuthToken = twitter.getAccessToken(username, password, TwitterOAuth.XAuthMode.CLIENT);
|
||||
final User user = twitter.verifyCredentials();
|
||||
final long user_id = user.getId();
|
||||
if (user_id <= 0) return new SignInResponse(false, false, null);
|
||||
|
@ -45,6 +45,7 @@ import org.mariotaku.twidere.model.ParcelableUser;
|
||||
import org.mariotaku.twidere.model.ParcelableUserList;
|
||||
import org.mariotaku.twidere.model.SingleResponse;
|
||||
import org.mariotaku.twidere.util.AsyncTaskUtils;
|
||||
import org.mariotaku.twidere.util.TwitterAPIUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -59,7 +60,7 @@ import twitter4j.http.HttpResponseCode;
|
||||
import static android.text.TextUtils.isEmpty;
|
||||
import static org.mariotaku.twidere.util.ParseUtils.parseString;
|
||||
import static org.mariotaku.twidere.util.Utils.getAccountScreenName;
|
||||
import static org.mariotaku.twidere.util.Utils.getTwitterInstance;
|
||||
import static org.mariotaku.twidere.util.TwitterAPIUtils.getTwitterInstance;
|
||||
|
||||
public class UserListSelectorActivity extends BaseSupportDialogActivity implements OnClickListener, OnItemClickListener {
|
||||
|
||||
@ -248,7 +249,7 @@ public class UserListSelectorActivity extends BaseSupportDialogActivity implemen
|
||||
|
||||
@Override
|
||||
protected SingleResponse<List<ParcelableUserList>> doInBackground(final Object... params) {
|
||||
final Twitter twitter = getTwitterInstance(mActivity, mAccountId, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mActivity, mAccountId, false);
|
||||
if (twitter == null) return SingleResponse.getInstance();
|
||||
try {
|
||||
final ResponseList<UserList> lists = twitter.getUserLists(mScreenName, true);
|
||||
@ -319,7 +320,7 @@ public class UserListSelectorActivity extends BaseSupportDialogActivity implemen
|
||||
|
||||
@Override
|
||||
protected SingleResponse<List<ParcelableUser>> doInBackground(final Object... params) {
|
||||
final Twitter twitter = getTwitterInstance(mActivity, mAccountId, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mActivity, mAccountId, false);
|
||||
if (twitter == null) return SingleResponse.getInstance();
|
||||
try {
|
||||
final ResponseList<User> lists = twitter.searchUsers(mName, 1);
|
||||
|
@ -108,6 +108,7 @@ import org.mariotaku.twidere.util.StatusAdapterLinkClickHandler;
|
||||
import org.mariotaku.twidere.util.StatusLinkClickHandler;
|
||||
import org.mariotaku.twidere.util.ThemeUtils;
|
||||
import org.mariotaku.twidere.util.TwidereLinkify;
|
||||
import org.mariotaku.twidere.util.TwitterAPIUtils;
|
||||
import org.mariotaku.twidere.util.TwitterCardUtils;
|
||||
import org.mariotaku.twidere.util.UserColorNameManager;
|
||||
import org.mariotaku.twidere.util.Utils;
|
||||
@ -1011,7 +1012,7 @@ public class StatusFragment extends BaseSupportFragment implements LoaderCallbac
|
||||
ParcelableStatus status = params[0];
|
||||
final long accountId = status.account_id;
|
||||
if (Utils.isOfficialKeyAccount(context, accountId)) {
|
||||
final Twitter twitter = Utils.getTwitterInstance(context, accountId, true);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(context, accountId, true);
|
||||
while (status.in_reply_to_status_id > 0 && !isCancelled()) {
|
||||
final ParcelableStatus cached = Utils.findStatusInDatabases(context, accountId, status.in_reply_to_status_id);
|
||||
if (cached == null) break;
|
||||
|
@ -38,6 +38,7 @@ import android.widget.TextView;
|
||||
import org.mariotaku.twidere.R;
|
||||
import org.mariotaku.twidere.model.ParcelableStatus;
|
||||
import org.mariotaku.twidere.model.SingleResponse;
|
||||
import org.mariotaku.twidere.util.TwitterAPIUtils;
|
||||
import org.mariotaku.twidere.util.Utils;
|
||||
import org.mariotaku.twidere.view.holder.StatusViewHolder;
|
||||
import org.mariotaku.twidere.view.holder.StatusViewHolder.DummyStatusHolderAdapter;
|
||||
@ -159,7 +160,7 @@ public class StatusTranslateDialogFragment extends BaseSupportDialogFragment imp
|
||||
@Override
|
||||
public SingleResponse<TranslationResult> loadInBackground() {
|
||||
final Context context = getContext();
|
||||
final Twitter twitter = Utils.getTwitterInstance(context, mAccountId, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(context, mAccountId, false);
|
||||
final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
|
||||
if (twitter == null) return SingleResponse.getInstance();
|
||||
try {
|
||||
|
@ -125,6 +125,7 @@ import org.mariotaku.twidere.util.ThemeUtils;
|
||||
import org.mariotaku.twidere.util.TwidereColorUtils;
|
||||
import org.mariotaku.twidere.util.TwidereLinkify;
|
||||
import org.mariotaku.twidere.util.TwidereLinkify.OnLinkClickListener;
|
||||
import org.mariotaku.twidere.util.TwitterAPIUtils;
|
||||
import org.mariotaku.twidere.util.UserColorNameManager;
|
||||
import org.mariotaku.twidere.util.Utils;
|
||||
import org.mariotaku.twidere.util.menu.TwidereMenuInfo;
|
||||
@ -436,15 +437,18 @@ public class UserFragment extends BaseSupportFragment implements OnClickListener
|
||||
final SupportTabSpec spec = mPagerAdapter.getTab(mViewPager.getCurrentItem());
|
||||
switch (spec.type) {
|
||||
case TAB_TYPE_STATUSES: {
|
||||
actionBar.setSubtitle(getResources().getQuantityString(R.plurals.N_statuses, user.statuses_count, user.statuses_count));
|
||||
actionBar.setSubtitle(getResources().getQuantityString(R.plurals.N_statuses,
|
||||
(int) user.statuses_count, user.statuses_count));
|
||||
break;
|
||||
}
|
||||
case TAB_TYPE_MEDIA: {
|
||||
actionBar.setSubtitle(getResources().getQuantityString(R.plurals.N_media, user.media_count, user.media_count));
|
||||
actionBar.setSubtitle(getResources().getQuantityString(R.plurals.N_media,
|
||||
(int) user.media_count, user.media_count));
|
||||
break;
|
||||
}
|
||||
case TAB_TYPE_FAVORITES: {
|
||||
actionBar.setSubtitle(getResources().getQuantityString(R.plurals.N_favorites, user.favorites_count, user.favorites_count));
|
||||
actionBar.setSubtitle(getResources().getQuantityString(R.plurals.N_favorites,
|
||||
(int) user.favorites_count, user.favorites_count));
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
@ -1633,7 +1637,7 @@ public class UserFragment extends BaseSupportFragment implements OnClickListener
|
||||
@Override
|
||||
public SingleResponse<Relationship> loadInBackground() {
|
||||
if (account_id == user_id) return SingleResponse.getInstance();
|
||||
final Twitter twitter = Utils.getTwitterInstance(context, account_id, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(context, account_id, false);
|
||||
if (twitter == null) return SingleResponse.getInstance();
|
||||
try {
|
||||
final Relationship relationship = twitter.showFriendship(account_id, user_id);
|
||||
|
@ -75,6 +75,7 @@ import org.mariotaku.twidere.util.ParseUtils;
|
||||
import org.mariotaku.twidere.util.SharedPreferencesWrapper;
|
||||
import org.mariotaku.twidere.util.ThemeUtils;
|
||||
import org.mariotaku.twidere.util.TwidereLinkify;
|
||||
import org.mariotaku.twidere.util.TwitterAPIUtils;
|
||||
import org.mariotaku.twidere.util.UserColorNameManager;
|
||||
import org.mariotaku.twidere.util.Utils;
|
||||
import org.mariotaku.twidere.view.TabPagerIndicator;
|
||||
@ -85,7 +86,7 @@ import twitter4j.UserList;
|
||||
|
||||
import static org.mariotaku.twidere.util.MenuUtils.setMenuItemAvailability;
|
||||
import static org.mariotaku.twidere.util.Utils.addIntentToMenu;
|
||||
import static org.mariotaku.twidere.util.Utils.getTwitterInstance;
|
||||
import static org.mariotaku.twidere.util.TwitterAPIUtils.getTwitterInstance;
|
||||
import static org.mariotaku.twidere.util.Utils.openUserListDetails;
|
||||
import static org.mariotaku.twidere.util.Utils.openUserProfile;
|
||||
|
||||
@ -549,7 +550,7 @@ public class UserListFragment extends BaseSupportFragment implements OnClickList
|
||||
final ParcelableUserList cache = mExtras.getParcelable(EXTRA_USER_LIST);
|
||||
if (cache != null) return SingleResponse.getInstance(cache);
|
||||
}
|
||||
final Twitter twitter = getTwitterInstance(getContext(), mAccountId, true);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(getContext(), mAccountId, true);
|
||||
if (twitter == null) return SingleResponse.getInstance();
|
||||
try {
|
||||
final UserList list;
|
||||
|
@ -30,12 +30,13 @@ import org.mariotaku.twidere.loader.support.CursorSupportUsersLoader;
|
||||
import org.mariotaku.twidere.loader.support.UserListMembersLoader;
|
||||
import org.mariotaku.twidere.model.ParcelableUserList;
|
||||
import org.mariotaku.twidere.util.AsyncTaskUtils;
|
||||
import org.mariotaku.twidere.util.TwitterAPIUtils;
|
||||
|
||||
import twitter4j.Twitter;
|
||||
import twitter4j.TwitterException;
|
||||
import twitter4j.UserList;
|
||||
|
||||
import static org.mariotaku.twidere.util.Utils.getTwitterInstance;
|
||||
import static org.mariotaku.twidere.util.TwitterAPIUtils.getTwitterInstance;
|
||||
|
||||
public class UserListMembersFragment extends CursorSupportUsersListFragment {
|
||||
|
||||
@ -123,7 +124,7 @@ public class UserListMembersFragment extends CursorSupportUsersListFragment {
|
||||
|
||||
@Override
|
||||
protected ParcelableUserList doInBackground(final Object... params) {
|
||||
final Twitter twitter = getTwitterInstance(getActivity(), accountId, true);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(getActivity(), accountId, true);
|
||||
if (twitter == null) return null;
|
||||
try {
|
||||
final UserList list;
|
||||
|
@ -62,6 +62,7 @@ import org.mariotaku.twidere.util.AsyncTwitterWrapper.UpdateProfileImageTask;
|
||||
import org.mariotaku.twidere.util.KeyboardShortcutsHandler;
|
||||
import org.mariotaku.twidere.util.MediaLoaderWrapper;
|
||||
import org.mariotaku.twidere.util.ParseUtils;
|
||||
import org.mariotaku.twidere.util.TwitterAPIUtils;
|
||||
import org.mariotaku.twidere.util.TwitterValidatorMETLengthChecker;
|
||||
import org.mariotaku.twidere.util.TwitterWrapper;
|
||||
import org.mariotaku.twidere.util.Utils;
|
||||
@ -73,7 +74,7 @@ import twitter4j.TwitterException;
|
||||
import twitter4j.User;
|
||||
|
||||
import static android.text.TextUtils.isEmpty;
|
||||
import static org.mariotaku.twidere.util.Utils.getTwitterInstance;
|
||||
import static org.mariotaku.twidere.util.TwitterAPIUtils.getTwitterInstance;
|
||||
|
||||
public class UserProfileEditorFragment extends BaseSupportFragment implements OnSizeChangedListener, TextWatcher,
|
||||
OnClickListener, LoaderCallbacks<SingleResponse<ParcelableUser>>,
|
||||
@ -440,7 +441,7 @@ public class UserProfileEditorFragment extends BaseSupportFragment implements On
|
||||
|
||||
@Override
|
||||
protected SingleResponse<ParcelableUser> doInBackground(final Object... params) {
|
||||
final Twitter twitter = getTwitterInstance(mActivity, mAccountId, true);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mActivity, mAccountId, true);
|
||||
try {
|
||||
User user = null;
|
||||
if (isColorChanged()) {
|
||||
|
@ -24,6 +24,7 @@ import android.support.v4.content.AsyncTaskLoader;
|
||||
|
||||
import org.mariotaku.twidere.loader.support.iface.ICursorSupportLoader;
|
||||
import org.mariotaku.twidere.model.ParcelableUserList;
|
||||
import org.mariotaku.twidere.util.TwitterAPIUtils;
|
||||
import org.mariotaku.twidere.util.collection.NoDuplicatesArrayList;
|
||||
|
||||
import java.util.Collections;
|
||||
@ -35,7 +36,7 @@ import twitter4j.Twitter;
|
||||
import twitter4j.TwitterException;
|
||||
import twitter4j.UserList;
|
||||
|
||||
import static org.mariotaku.twidere.util.Utils.getTwitterInstance;
|
||||
import static org.mariotaku.twidere.util.TwitterAPIUtils.getTwitterInstance;
|
||||
|
||||
public abstract class BaseUserListsLoader extends AsyncTaskLoader<List<ParcelableUserList>>
|
||||
implements ICursorSupportLoader {
|
||||
@ -75,7 +76,7 @@ public abstract class BaseUserListsLoader extends AsyncTaskLoader<List<Parcelabl
|
||||
|
||||
@Override
|
||||
public List<ParcelableUserList> loadInBackground() {
|
||||
final Twitter twitter = getTwitterInstance(getContext(), mAccountId, true);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(getContext(), mAccountId, true);
|
||||
List<UserList> listLoaded = null;
|
||||
try {
|
||||
listLoaded = getUserLists(twitter);
|
||||
|
@ -34,6 +34,7 @@ import org.mariotaku.twidere.model.ParcelableUser.CachedIndices;
|
||||
import org.mariotaku.twidere.model.SingleResponse;
|
||||
import org.mariotaku.twidere.provider.TwidereDataStore.Accounts;
|
||||
import org.mariotaku.twidere.provider.TwidereDataStore.CachedUsers;
|
||||
import org.mariotaku.twidere.util.TwitterAPIUtils;
|
||||
import org.mariotaku.twidere.util.TwitterWrapper;
|
||||
|
||||
import twitter4j.Twitter;
|
||||
@ -41,7 +42,7 @@ import twitter4j.TwitterException;
|
||||
import twitter4j.User;
|
||||
|
||||
import static org.mariotaku.twidere.util.ContentValuesCreator.createCachedUser;
|
||||
import static org.mariotaku.twidere.util.Utils.getTwitterInstance;
|
||||
import static org.mariotaku.twidere.util.TwitterAPIUtils.getTwitterInstance;
|
||||
import static org.mariotaku.twidere.util.Utils.isMyAccount;
|
||||
|
||||
public final class ParcelableUserLoader extends AsyncTaskLoader<SingleResponse<ParcelableUser>> implements Constants {
|
||||
@ -75,7 +76,7 @@ public final class ParcelableUserLoader extends AsyncTaskLoader<SingleResponse<P
|
||||
return SingleResponse.getInstance(user);
|
||||
}
|
||||
}
|
||||
final Twitter twitter = getTwitterInstance(context, mAccountId, true);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(context, mAccountId, true);
|
||||
if (twitter == null) return SingleResponse.getInstance();
|
||||
if (mLoadFromCache) {
|
||||
final Expression where;
|
||||
|
@ -38,7 +38,7 @@ import static org.mariotaku.twidere.util.Utils.isFiltered;
|
||||
|
||||
public class RetweetsOfMeLoader extends TwitterAPIStatusesLoader {
|
||||
|
||||
private int mTotalItemsCount;
|
||||
private long mTotalItemsCount;
|
||||
|
||||
public RetweetsOfMeLoader(final Context context, final long accountId, final long sinceId, final long maxId,
|
||||
final List<ParcelableStatus> data, final String[] savedStatusesArgs,
|
||||
@ -46,7 +46,7 @@ public class RetweetsOfMeLoader extends TwitterAPIStatusesLoader {
|
||||
super(context, accountId, sinceId, maxId, data, savedStatusesArgs, tabPosition, fromUser);
|
||||
}
|
||||
|
||||
public int getTotalItemsCount() {
|
||||
public long getTotalItemsCount() {
|
||||
return mTotalItemsCount;
|
||||
}
|
||||
|
||||
|
@ -22,12 +22,14 @@ package org.mariotaku.twidere.loader.support;
|
||||
import android.content.Context;
|
||||
import android.support.v4.content.AsyncTaskLoader;
|
||||
|
||||
import org.mariotaku.twidere.util.TwitterAPIUtils;
|
||||
|
||||
import twitter4j.ResponseList;
|
||||
import twitter4j.SavedSearch;
|
||||
import twitter4j.Twitter;
|
||||
import twitter4j.TwitterException;
|
||||
|
||||
import static org.mariotaku.twidere.util.Utils.getTwitterInstance;
|
||||
import static org.mariotaku.twidere.util.TwitterAPIUtils.getTwitterInstance;
|
||||
|
||||
public class SavedSearchesLoader extends AsyncTaskLoader<ResponseList<SavedSearch>> {
|
||||
|
||||
@ -40,7 +42,7 @@ public class SavedSearchesLoader extends AsyncTaskLoader<ResponseList<SavedSearc
|
||||
|
||||
@Override
|
||||
public ResponseList<SavedSearch> loadInBackground() {
|
||||
final Twitter twitter = getTwitterInstance(getContext(), mAccountId, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(getContext(), mAccountId, false);
|
||||
if (twitter == null) return null;
|
||||
try {
|
||||
return twitter.getSavedSearches();
|
||||
|
@ -29,6 +29,7 @@ import com.bluelinelabs.logansquare.LoganSquare;
|
||||
|
||||
import org.mariotaku.jsonserializer.JSONFileIO;
|
||||
import org.mariotaku.twidere.model.ParcelableActivity;
|
||||
import org.mariotaku.twidere.util.TwitterAPIUtils;
|
||||
import org.mariotaku.twidere.util.Utils;
|
||||
|
||||
import java.io.File;
|
||||
@ -46,7 +47,7 @@ import twitter4j.Paging;
|
||||
import twitter4j.Twitter;
|
||||
import twitter4j.TwitterException;
|
||||
|
||||
import static org.mariotaku.twidere.util.Utils.getTwitterInstance;
|
||||
import static org.mariotaku.twidere.util.TwitterAPIUtils.getTwitterInstance;
|
||||
import static org.mariotaku.twidere.util.Utils.truncateActivities;
|
||||
|
||||
public abstract class Twitter4JActivitiesLoader extends ParcelableActivitiesLoader {
|
||||
@ -145,7 +146,7 @@ public abstract class Twitter4JActivitiesLoader extends ParcelableActivitiesLoad
|
||||
protected abstract List<Activity> getActivities(Twitter twitter, Paging paging) throws TwitterException;
|
||||
|
||||
protected final Twitter getTwitter() {
|
||||
return getTwitterInstance(mContext, mAccountIds, true, true);
|
||||
return TwitterAPIUtils.getTwitterInstance(mContext, mAccountIds, true, true);
|
||||
}
|
||||
|
||||
protected abstract boolean shouldFilterActivity(final SQLiteDatabase database, final ParcelableActivity activity);
|
||||
|
@ -22,6 +22,7 @@ package org.mariotaku.twidere.loader.support;
|
||||
import android.content.Context;
|
||||
|
||||
import org.mariotaku.twidere.model.ParcelableUser;
|
||||
import org.mariotaku.twidere.util.TwitterAPIUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@ -30,7 +31,7 @@ import twitter4j.Twitter;
|
||||
import twitter4j.TwitterException;
|
||||
import twitter4j.User;
|
||||
|
||||
import static org.mariotaku.twidere.util.Utils.getTwitterInstance;
|
||||
import static org.mariotaku.twidere.util.TwitterAPIUtils.getTwitterInstance;
|
||||
|
||||
public abstract class Twitter4JUsersLoader extends ParcelableUsersLoader {
|
||||
|
||||
@ -49,7 +50,7 @@ public abstract class Twitter4JUsersLoader extends ParcelableUsersLoader {
|
||||
final List<ParcelableUser> data = getData();
|
||||
final List<User> users;
|
||||
try {
|
||||
users = getUsers(getTwitterInstance(mContext, mAccountId, true));
|
||||
users = getUsers(TwitterAPIUtils.getTwitterInstance(mContext, mAccountId, true));
|
||||
if (users == null) return data;
|
||||
} catch (final TwitterException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -32,6 +32,7 @@ import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.mariotaku.jsonserializer.JSONFileIO;
|
||||
import org.mariotaku.twidere.app.TwidereApplication;
|
||||
import org.mariotaku.twidere.model.ParcelableStatus;
|
||||
import org.mariotaku.twidere.util.TwitterAPIUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
@ -48,7 +49,7 @@ import twitter4j.Status;
|
||||
import twitter4j.Twitter;
|
||||
import twitter4j.TwitterException;
|
||||
|
||||
import static org.mariotaku.twidere.util.Utils.getTwitterInstance;
|
||||
import static org.mariotaku.twidere.util.TwitterAPIUtils.getTwitterInstance;
|
||||
import static org.mariotaku.twidere.util.Utils.truncateStatuses;
|
||||
|
||||
public abstract class TwitterAPIStatusesLoader extends ParcelableStatusesLoader {
|
||||
@ -168,7 +169,7 @@ public abstract class TwitterAPIStatusesLoader extends ParcelableStatusesLoader
|
||||
|
||||
@Nullable
|
||||
protected final Twitter getTwitter() {
|
||||
return getTwitterInstance(mContext, mAccountId, true, true);
|
||||
return TwitterAPIUtils.getTwitterInstance(mContext, mAccountId, true, true);
|
||||
}
|
||||
|
||||
protected abstract boolean shouldFilterStatus(final SQLiteDatabase database, final ParcelableStatus status);
|
||||
|
@ -25,7 +25,6 @@ import org.mariotaku.twidere.model.ParcelableUser;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import twitter4j.CursorPaging;
|
||||
import twitter4j.PageableResponseList;
|
||||
import twitter4j.Paging;
|
||||
import twitter4j.Twitter;
|
||||
|
@ -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 UserListMembersLoader 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)
|
||||
|
@ -39,6 +39,7 @@ import android.widget.TextView;
|
||||
|
||||
import org.mariotaku.twidere.Constants;
|
||||
import org.mariotaku.twidere.R;
|
||||
import org.mariotaku.twidere.util.TwitterAPIUtils;
|
||||
|
||||
import java.text.Collator;
|
||||
import java.util.Comparator;
|
||||
@ -49,7 +50,7 @@ import twitter4j.Twitter;
|
||||
import twitter4j.TwitterException;
|
||||
import twitter4j.api.HelpResources.Language;
|
||||
|
||||
import static org.mariotaku.twidere.util.Utils.getDefaultTwitterInstance;
|
||||
import static org.mariotaku.twidere.util.TwitterAPIUtils.getDefaultTwitterInstance;
|
||||
|
||||
public class TranslationDestinationPreference extends Preference implements Constants, OnClickListener {
|
||||
|
||||
@ -171,7 +172,7 @@ public class TranslationDestinationPreference extends Preference implements Cons
|
||||
|
||||
@Override
|
||||
protected ResponseList<Language> doInBackground(final Object... args) {
|
||||
final Twitter twitter = getDefaultTwitterInstance(getContext(), false);
|
||||
final Twitter twitter = TwitterAPIUtils.getDefaultTwitterInstance(getContext(), false);
|
||||
final String pref = mPreferences.getString(KEY_TRANSLATION_DESTINATION, null);
|
||||
if (twitter == null) return null;
|
||||
try {
|
||||
|
@ -37,6 +37,7 @@ import android.widget.TextView;
|
||||
|
||||
import org.mariotaku.twidere.Constants;
|
||||
import org.mariotaku.twidere.R;
|
||||
import org.mariotaku.twidere.util.TwitterAPIUtils;
|
||||
|
||||
import java.text.Collator;
|
||||
import java.util.Comparator;
|
||||
@ -47,7 +48,7 @@ import twitter4j.ResponseList;
|
||||
import twitter4j.Twitter;
|
||||
import twitter4j.TwitterException;
|
||||
|
||||
import static org.mariotaku.twidere.util.Utils.getDefaultTwitterInstance;
|
||||
import static org.mariotaku.twidere.util.TwitterAPIUtils.getDefaultTwitterInstance;
|
||||
|
||||
public class TrendsLocationPreference extends Preference implements Constants, OnClickListener {
|
||||
|
||||
@ -171,7 +172,7 @@ public class TrendsLocationPreference extends Preference implements Constants, O
|
||||
|
||||
@Override
|
||||
protected ResponseList<Location> doInBackground(final Object... args) {
|
||||
final Twitter twitter = getDefaultTwitterInstance(getContext(), false);
|
||||
final Twitter twitter = TwitterAPIUtils.getDefaultTwitterInstance(getContext(), false);
|
||||
if (twitter == null) return null;
|
||||
try {
|
||||
return twitter.getAvailableTrends();
|
||||
|
@ -73,6 +73,7 @@ import org.mariotaku.twidere.util.ParseUtils;
|
||||
import org.mariotaku.twidere.util.StatusCodeMessageUtils;
|
||||
import org.mariotaku.twidere.util.StatusShortenerInterface;
|
||||
import org.mariotaku.twidere.util.TwidereValidator;
|
||||
import org.mariotaku.twidere.util.TwitterAPIUtils;
|
||||
import org.mariotaku.twidere.util.Utils;
|
||||
import org.mariotaku.twidere.util.io.ContentLengthInputStream;
|
||||
import org.mariotaku.twidere.util.io.ContentLengthInputStream.ReadListener;
|
||||
@ -98,7 +99,7 @@ import static android.text.TextUtils.isEmpty;
|
||||
import static org.mariotaku.twidere.util.ContentValuesCreator.createMessageDraft;
|
||||
import static org.mariotaku.twidere.util.Utils.getImagePathFromUri;
|
||||
import static org.mariotaku.twidere.util.Utils.getImageUploadStatus;
|
||||
import static org.mariotaku.twidere.util.Utils.getTwitterInstance;
|
||||
import static org.mariotaku.twidere.util.TwitterAPIUtils.getTwitterInstance;
|
||||
|
||||
public class BackgroundOperationService extends IntentService implements Constants {
|
||||
|
||||
@ -396,7 +397,7 @@ public class BackgroundOperationService extends IntentService implements Constan
|
||||
private SingleResponse<ParcelableDirectMessage> sendDirectMessage(final NotificationCompat.Builder builder,
|
||||
final long accountId, final long recipientId,
|
||||
final String text, final String imageUri) {
|
||||
final Twitter twitter = getTwitterInstance(this, accountId, true, true);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(this, accountId, true, true);
|
||||
if (twitter == null) return SingleResponse.getInstance();
|
||||
try {
|
||||
final ParcelableDirectMessage directMessage;
|
||||
@ -516,7 +517,7 @@ public class BackgroundOperationService extends IntentService implements Constan
|
||||
}
|
||||
}
|
||||
for (final ParcelableAccount account : statusUpdate.accounts) {
|
||||
final Twitter twitter = getTwitterInstance(this, account.account_id, true, true);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(this, account.account_id, true, true);
|
||||
final StatusUpdate status = new StatusUpdate(shortenedText);
|
||||
status.setInReplyToStatusId(statusUpdate.in_reply_to_status_id);
|
||||
if (statusUpdate.location != null) {
|
||||
|
@ -107,7 +107,7 @@ import static org.mariotaku.twidere.util.Utils.getDefaultAccountId;
|
||||
import static org.mariotaku.twidere.util.Utils.getNewestMessageIdsFromDatabase;
|
||||
import static org.mariotaku.twidere.util.Utils.getNewestStatusIdsFromDatabase;
|
||||
import static org.mariotaku.twidere.util.Utils.getStatusCountInDatabase;
|
||||
import static org.mariotaku.twidere.util.Utils.getTwitterInstance;
|
||||
import static org.mariotaku.twidere.util.TwitterAPIUtils.getTwitterInstance;
|
||||
import static org.mariotaku.twidere.util.Utils.showErrorMessage;
|
||||
import static org.mariotaku.twidere.util.Utils.showInfoMessage;
|
||||
import static org.mariotaku.twidere.util.Utils.showOkMessage;
|
||||
@ -527,7 +527,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
protected SingleResponse<Object> doInBackground(Long... params) {
|
||||
final ContentResolver cr = mContext.getContentResolver();
|
||||
for (long accountId : params) {
|
||||
final Twitter twitter = Utils.getTwitterInstance(mContext, accountId, true);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, accountId, true);
|
||||
if (twitter == null) continue;
|
||||
try {
|
||||
final ResponseList<SavedSearch> searches = twitter.getSavedSearches();
|
||||
@ -575,7 +575,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
@Override
|
||||
protected SingleResponse<ParcelableUser> doInBackground(final Object... params) {
|
||||
try {
|
||||
final Twitter twitter = getTwitterInstance(mContext, mAccountId, true);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, mAccountId, true);
|
||||
TwitterWrapper.updateProfileBannerImage(mContext, twitter, mImageUri, mDeleteImage);
|
||||
// Wait for 5 seconds, see
|
||||
// https://dev.twitter.com/docs/api/1.1/post/account/update_profile_image
|
||||
@ -613,7 +613,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
@Override
|
||||
protected SingleResponse<ParcelableUser> doInBackground(final Object... params) {
|
||||
try {
|
||||
final Twitter twitter = getTwitterInstance(mContext, mAccountId, true);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, mAccountId, true);
|
||||
TwitterWrapper.updateProfileImage(mContext, twitter, mImageUri, mDeleteImage);
|
||||
// Wait for 5 seconds, see
|
||||
// https://dev.twitter.com/docs/api/1.1/post/account/update_profile_image
|
||||
@ -702,7 +702,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
@Override
|
||||
protected SingleResponse<User> doInBackground(final Object... params) {
|
||||
|
||||
final Twitter twitter = getTwitterInstance(mContext, mAccountId, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, mAccountId, false);
|
||||
if (twitter == null) return SingleResponse.getInstance();
|
||||
try {
|
||||
final User user = twitter.acceptFriendship(mUserId);
|
||||
@ -749,7 +749,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
|
||||
@Override
|
||||
protected SingleResponse<ParcelableUserList> doInBackground(final Object... params) {
|
||||
final Twitter twitter = getTwitterInstance(mContext, accountId, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, accountId, false);
|
||||
if (twitter == null || users == null) return SingleResponse.getInstance();
|
||||
try {
|
||||
final long[] userIds = new long[users.length];
|
||||
@ -836,7 +836,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
|
||||
@Override
|
||||
protected SingleResponse<ParcelableUser> doInBackground(final Object... params) {
|
||||
final Twitter twitter = getTwitterInstance(mContext, account_id, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, account_id, false);
|
||||
if (twitter == null) return SingleResponse.getInstance();
|
||||
try {
|
||||
final User user = twitter.createBlock(user_id);
|
||||
@ -889,7 +889,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
@Override
|
||||
protected SingleResponse<ParcelableStatus> doInBackground(final Object... params) {
|
||||
if (account_id < 0) return SingleResponse.getInstance();
|
||||
final Twitter twitter = getTwitterInstance(mContext, account_id, true);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, account_id, true);
|
||||
if (twitter == null) return SingleResponse.getInstance();
|
||||
try {
|
||||
final twitter4j.Status status = twitter.createFavorite(status_id);
|
||||
@ -973,7 +973,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
@Override
|
||||
protected SingleResponse<ParcelableUser> doInBackground(final Object... params) {
|
||||
|
||||
final Twitter twitter = getTwitterInstance(mContext, mAccountId, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, mAccountId, false);
|
||||
if (twitter == null) return SingleResponse.getInstance();
|
||||
try {
|
||||
final User user = twitter.createFriendship(user_id);
|
||||
@ -1033,7 +1033,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
@Override
|
||||
protected ListResponse<Long> doInBackground(final Object... params) {
|
||||
final List<Long> blocked_users = new ArrayList<>();
|
||||
final Twitter twitter = getTwitterInstance(mContext, account_id, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, account_id, false);
|
||||
if (twitter != null) {
|
||||
for (final long user_id : user_ids) {
|
||||
try {
|
||||
@ -1080,7 +1080,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
|
||||
@Override
|
||||
protected SingleResponse<ParcelableUser> doInBackground(final Object... params) {
|
||||
final Twitter twitter = getTwitterInstance(mContext, mAccountId, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, mAccountId, false);
|
||||
if (twitter == null) return SingleResponse.getInstance();
|
||||
try {
|
||||
final User user = twitter.createMute(mUserId);
|
||||
@ -1127,7 +1127,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
|
||||
@Override
|
||||
protected SingleResponse<SavedSearch> doInBackground(final Object... params) {
|
||||
final Twitter twitter = getTwitterInstance(mContext, mAccountId, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, mAccountId, false);
|
||||
if (twitter == null) return null;
|
||||
try {
|
||||
return SingleResponse.getInstance(twitter.createSavedSearch(mQuery));
|
||||
@ -1162,7 +1162,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
|
||||
@Override
|
||||
protected SingleResponse<ParcelableUserList> doInBackground(final Object... params) {
|
||||
final Twitter twitter = getTwitterInstance(mContext, accountId, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, accountId, false);
|
||||
if (twitter == null) return SingleResponse.getInstance();
|
||||
|
||||
try {
|
||||
@ -1208,7 +1208,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
|
||||
@Override
|
||||
protected SingleResponse<UserList> doInBackground(final Object... params) {
|
||||
final Twitter twitter = getTwitterInstance(mContext, account_id, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, account_id, false);
|
||||
if (twitter == null || list_name == null) return SingleResponse.getInstance();
|
||||
try {
|
||||
final UserList list = twitter.createUserList(list_name, is_public, description);
|
||||
@ -1250,7 +1250,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
|
||||
@Override
|
||||
protected SingleResponse<ParcelableUserList> doInBackground(final Object... params) {
|
||||
final Twitter twitter = getTwitterInstance(mContext, mAccountId, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, mAccountId, false);
|
||||
if (twitter == null) return SingleResponse.getInstance();
|
||||
try {
|
||||
final long[] userIds = new long[users.length];
|
||||
@ -1317,7 +1317,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
@Override
|
||||
protected SingleResponse<User> doInBackground(final Object... params) {
|
||||
|
||||
final Twitter twitter = getTwitterInstance(mContext, mAccountId, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, mAccountId, false);
|
||||
if (twitter == null) return SingleResponse.getInstance();
|
||||
try {
|
||||
final User user = twitter.denyFriendship(mUserId);
|
||||
@ -1361,7 +1361,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
|
||||
@Override
|
||||
protected SingleResponse<ParcelableUser> doInBackground(final Object... params) {
|
||||
final Twitter twitter = getTwitterInstance(mContext, mAccountId, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, mAccountId, false);
|
||||
if (twitter == null) return SingleResponse.getInstance();
|
||||
try {
|
||||
final User user = twitter.destroyBlock(mUserId);
|
||||
@ -1419,7 +1419,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
|
||||
@Override
|
||||
protected SingleResponse<DirectMessage> doInBackground(final Object... args) {
|
||||
final Twitter twitter = getTwitterInstance(mContext, account_id, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, account_id, false);
|
||||
if (twitter == null) return SingleResponse.getInstance();
|
||||
try {
|
||||
final DirectMessage message = twitter.destroyDirectMessage(message_id);
|
||||
@ -1463,7 +1463,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
@Override
|
||||
protected SingleResponse<ParcelableStatus> doInBackground(final Object... params) {
|
||||
if (account_id < 0) return SingleResponse.getInstance();
|
||||
final Twitter twitter = getTwitterInstance(mContext, account_id, true);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, account_id, true);
|
||||
if (twitter != null) {
|
||||
try {
|
||||
final twitter4j.Status status = twitter.destroyFavorite(status_id);
|
||||
@ -1546,7 +1546,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
@Override
|
||||
protected SingleResponse<ParcelableUser> doInBackground(final Object... params) {
|
||||
|
||||
final Twitter twitter = getTwitterInstance(mContext, mAccountId, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, mAccountId, false);
|
||||
if (twitter != null) {
|
||||
try {
|
||||
final User user = twitter.destroyFriendship(user_id);
|
||||
@ -1596,7 +1596,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
|
||||
@Override
|
||||
protected SingleResponse<ParcelableUser> doInBackground(final Object... params) {
|
||||
final Twitter twitter = getTwitterInstance(mContext, mAccountId, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, mAccountId, false);
|
||||
if (twitter == null) return SingleResponse.getInstance();
|
||||
try {
|
||||
final User user = twitter.destroyMute(mUserId);
|
||||
@ -1640,7 +1640,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
|
||||
@Override
|
||||
protected SingleResponse<SavedSearch> doInBackground(final Object... params) {
|
||||
final Twitter twitter = getTwitterInstance(mContext, mAccountId, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, mAccountId, false);
|
||||
if (twitter == null) return SingleResponse.getInstance();
|
||||
try {
|
||||
return SingleResponse.getInstance(twitter.destroySavedSearch(mSearchId));
|
||||
@ -1676,7 +1676,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
|
||||
@Override
|
||||
protected SingleResponse<ParcelableStatus> doInBackground(final Object... params) {
|
||||
final Twitter twitter = getTwitterInstance(mContext, account_id, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, account_id, false);
|
||||
if (twitter == null) return SingleResponse.getInstance();
|
||||
ParcelableStatus status = null;
|
||||
TwitterException exception = null;
|
||||
@ -1738,7 +1738,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
@Override
|
||||
protected SingleResponse<ParcelableUserList> doInBackground(final Object... params) {
|
||||
|
||||
final Twitter twitter = getTwitterInstance(mContext, mAccountId, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, mAccountId, false);
|
||||
if (twitter != null) {
|
||||
try {
|
||||
final ParcelableUserList list = new ParcelableUserList(
|
||||
@ -1782,7 +1782,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
@Override
|
||||
protected SingleResponse<ParcelableUserList> doInBackground(final Object... params) {
|
||||
|
||||
final Twitter twitter = getTwitterInstance(mContext, mAccountId, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, mAccountId, false);
|
||||
if (twitter != null) {
|
||||
try {
|
||||
if (mListId > 0) {
|
||||
@ -1851,7 +1851,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
int idx = 0;
|
||||
final int load_item_limit = mPreferences.getInt(KEY_LOAD_ITEM_LIMIT, DEFAULT_LOAD_ITEM_LIMIT);
|
||||
for (final long accountId : account_ids) {
|
||||
final Twitter twitter = getTwitterInstance(mContext, accountId, true);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, accountId, true);
|
||||
if (twitter == null) continue;
|
||||
try {
|
||||
final Paging paging = new Paging();
|
||||
@ -2196,7 +2196,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
int idx = 0;
|
||||
final int loadItemLimit = mPreferences.getInt(KEY_LOAD_ITEM_LIMIT, DEFAULT_LOAD_ITEM_LIMIT);
|
||||
for (final long accountId : mAccountIds) {
|
||||
final Twitter twitter = getTwitterInstance(mContext, accountId, true);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, accountId, true);
|
||||
if (twitter == null) continue;
|
||||
try {
|
||||
final Paging paging = new Paging();
|
||||
@ -2242,7 +2242,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
|
||||
@Override
|
||||
protected ListResponse<Trends> doInBackground(final Object... params) {
|
||||
final Twitter twitter = getTwitterInstance(mContext, account_id, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, account_id, false);
|
||||
final Bundle extras = new Bundle();
|
||||
extras.putLong(EXTRA_ACCOUNT_ID, account_id);
|
||||
if (twitter != null) {
|
||||
@ -2290,7 +2290,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
final Bundle extras = new Bundle();
|
||||
extras.putLong(EXTRA_ACCOUNT_ID, account_id);
|
||||
final List<Long> reported_users = new ArrayList<>();
|
||||
final Twitter twitter = getTwitterInstance(mContext, account_id, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, account_id, false);
|
||||
if (twitter != null) {
|
||||
for (final long user_id : user_ids) {
|
||||
try {
|
||||
@ -2340,7 +2340,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
|
||||
@Override
|
||||
protected SingleResponse<ParcelableUser> doInBackground(final Object... params) {
|
||||
final Twitter twitter = getTwitterInstance(mContext, mAccountId, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, mAccountId, false);
|
||||
if (twitter != null) {
|
||||
try {
|
||||
final User user = twitter.reportSpam(user_id);
|
||||
@ -2386,7 +2386,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
@Override
|
||||
protected SingleResponse<ParcelableStatus> doInBackground(final Object... params) {
|
||||
if (account_id < 0) return SingleResponse.getInstance();
|
||||
final Twitter twitter = getTwitterInstance(mContext, account_id, true);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, account_id, true);
|
||||
if (twitter == null) {
|
||||
return SingleResponse.getInstance();
|
||||
}
|
||||
@ -2538,7 +2538,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper {
|
||||
@Override
|
||||
protected SingleResponse<ParcelableUserList> doInBackground(final Object... params) {
|
||||
|
||||
final Twitter twitter = getTwitterInstance(mContext, accountId, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(mContext, accountId, false);
|
||||
if (twitter != null) {
|
||||
try {
|
||||
final UserList list = twitter.updateUserList(listId, name, isPublic, description);
|
||||
|
@ -20,13 +20,12 @@
|
||||
package org.mariotaku.twidere.util;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import android.util.Pair;
|
||||
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;
|
||||
@ -87,7 +86,7 @@ public class OAuthPasswordAuthenticator implements Constants {
|
||||
final HashMap<String, String> inputMap = new HashMap<>();
|
||||
final RestRequest.Builder authorizePageBuilder = new RestRequest.Builder();
|
||||
authorizePageBuilder.method(GET.METHOD);
|
||||
authorizePageBuilder.url(endpoint.construct("/oauth/authorize", new ImmutablePair<>("oauth_token",
|
||||
authorizePageBuilder.url(endpoint.construct("/oauth/authorize", Pair.create("oauth_token",
|
||||
requestToken.getOauthToken())));
|
||||
final RestRequest authorizePageRequest = authorizePageBuilder.build();
|
||||
authorizePage = client.execute(authorizePageRequest);
|
||||
@ -95,18 +94,18 @@ public class OAuthPasswordAuthenticator implements Constants {
|
||||
readInputFromHtml(BaseTypedData.reader(authorizePage.getBody()), inputMap,
|
||||
INPUT_AUTHENTICITY_TOKEN, INPUT_REDIRECT_AFTER_LOGIN);
|
||||
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)));
|
||||
params.add(Pair.create("oauth_token", oauthToken));
|
||||
params.add(Pair.create(INPUT_AUTHENTICITY_TOKEN, inputMap.get(INPUT_AUTHENTICITY_TOKEN)));
|
||||
if (inputMap.containsKey(INPUT_REDIRECT_AFTER_LOGIN)) {
|
||||
params.add(new ImmutablePair<>(INPUT_REDIRECT_AFTER_LOGIN, inputMap.get(INPUT_REDIRECT_AFTER_LOGIN)));
|
||||
params.add(Pair.create(INPUT_REDIRECT_AFTER_LOGIN, inputMap.get(INPUT_REDIRECT_AFTER_LOGIN)));
|
||||
}
|
||||
params.add(new ImmutablePair<>("session[username_or_email]", username));
|
||||
params.add(new ImmutablePair<>("session[password]", password));
|
||||
params.add(Pair.create("session[username_or_email]", username));
|
||||
params.add(Pair.create("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()))));
|
||||
requestHeaders.add(Pair.create("Origin", "https://twitter.com"));
|
||||
requestHeaders.add(Pair.create("Referer", Endpoint.constructUrl("https://twitter.com/oauth/authorize",
|
||||
Pair.create("oauth_token", requestToken.getOauthToken()))));
|
||||
|
||||
final String host = parseUrlHost(endpoint.getUrl());
|
||||
for (String cookieHeader : cookieHeaders) {
|
||||
@ -115,7 +114,7 @@ public class OAuthPasswordAuthenticator implements Constants {
|
||||
cookie.setVersion(1);
|
||||
cookie.setDomain("twitter.com");
|
||||
}
|
||||
requestHeaders.add(new ImmutablePair<>("Cookie", cookie.toString()));
|
||||
requestHeaders.add(Pair.create("Cookie", cookie.toString()));
|
||||
}
|
||||
}
|
||||
final RestRequest.Builder authorizeResultBuilder = new RestRequest.Builder();
|
||||
|
@ -0,0 +1,212 @@
|
||||
package org.mariotaku.twidere.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Pair;
|
||||
|
||||
import org.mariotaku.simplerestapi.http.Authorization;
|
||||
import org.mariotaku.simplerestapi.http.RestHttpClient;
|
||||
import org.mariotaku.simplerestapi.http.RestRequest;
|
||||
import org.mariotaku.simplerestapi.http.RestResponse;
|
||||
import org.mariotaku.simplerestapi.method.GET;
|
||||
import org.mariotaku.twidere.TwidereConstants;
|
||||
import org.mariotaku.twidere.api.twitter.OkHttpRestClient;
|
||||
import org.mariotaku.twidere.api.twitter.auth.BasicAuthorization;
|
||||
import org.mariotaku.twidere.api.twitter.auth.EmptyAuthorization;
|
||||
import org.mariotaku.twidere.api.twitter.auth.OAuthToken;
|
||||
import org.mariotaku.twidere.app.TwidereApplication;
|
||||
import org.mariotaku.twidere.constant.SharedPreferenceConstants;
|
||||
import org.mariotaku.twidere.model.ParcelableAccount;
|
||||
import org.mariotaku.twidere.provider.TwidereDataStore;
|
||||
import org.mariotaku.twidere.util.net.TwidereHostResolverFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Proxy;
|
||||
import java.net.SocketAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import twitter4j.Twitter;
|
||||
import twitter4j.TwitterConstants;
|
||||
import twitter4j.TwitterFactory;
|
||||
import twitter4j.conf.ConfigurationBuilder;
|
||||
import twitter4j.http.HostAddressResolverFactory;
|
||||
|
||||
import static android.text.TextUtils.isEmpty;
|
||||
|
||||
/**
|
||||
* Created by mariotaku on 15/5/7.
|
||||
*/
|
||||
public class TwitterAPIUtils {
|
||||
|
||||
public static RestResponse getRedirectedHttpResponse(@NonNull final RestHttpClient client, @NonNull final String url,
|
||||
final String signUrl, final Authorization auth,
|
||||
final List<Pair<String, String>> additionalHeaders)
|
||||
throws IOException {
|
||||
final ArrayList<String> urls = new ArrayList<>();
|
||||
urls.add(url);
|
||||
RestResponse resp;
|
||||
RestRequest req = new RestRequest.Builder().method(GET.METHOD).url(url).headers(additionalHeaders).build();
|
||||
resp = client.execute(req);
|
||||
while (resp != null && Utils.isRedirected(resp.getStatus())) {
|
||||
final String requestUrl = resp.getHeader("Location");
|
||||
if (requestUrl == null) return null;
|
||||
if (urls.contains(requestUrl)) throw new IOException("Too many redirects");
|
||||
urls.add(requestUrl);
|
||||
req = new RestRequest.Builder().method(GET.METHOD).url(requestUrl).headers(additionalHeaders).build();
|
||||
resp = client.execute(req);
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
|
||||
public static Twitter getDefaultTwitterInstance(final Context context, final boolean includeEntities) {
|
||||
if (context == null) return null;
|
||||
return getDefaultTwitterInstance(context, includeEntities, true);
|
||||
}
|
||||
|
||||
public static Twitter getDefaultTwitterInstance(final Context context, final boolean includeEntities,
|
||||
final boolean includeRetweets) {
|
||||
if (context == null) return null;
|
||||
return getTwitterInstance(context, Utils.getDefaultAccountId(context), includeEntities, includeRetweets);
|
||||
}
|
||||
|
||||
public static Twitter getTwitterInstance(final Context context, final long accountId,
|
||||
final boolean includeEntities) {
|
||||
return getTwitterInstance(context, accountId, includeEntities, true);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Twitter getTwitterInstance(final Context context, final long accountId,
|
||||
final boolean includeEntities,
|
||||
final boolean includeRetweets) {
|
||||
if (context == null) return null;
|
||||
final TwidereApplication app = TwidereApplication.getInstance(context);
|
||||
final SharedPreferences prefs = context.getSharedPreferences(TwidereConstants.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
|
||||
final int connection_timeout = prefs.getInt(SharedPreferenceConstants.KEY_CONNECTION_TIMEOUT, 10) * 1000;
|
||||
final boolean enableGzip = prefs.getBoolean(SharedPreferenceConstants.KEY_GZIP_COMPRESSING, true);
|
||||
final boolean ignoreSslError = prefs.getBoolean(SharedPreferenceConstants.KEY_IGNORE_SSL_ERROR, false);
|
||||
final boolean enableProxy = prefs.getBoolean(SharedPreferenceConstants.KEY_ENABLE_PROXY, false);
|
||||
// Here I use old consumer key/secret because it's default key for older
|
||||
// versions
|
||||
final ParcelableAccount.ParcelableCredentials credentials = ParcelableAccount.ParcelableCredentials.getCredentials(context, accountId);
|
||||
if (credentials == null) return null;
|
||||
final ConfigurationBuilder cb = new ConfigurationBuilder();
|
||||
cb.setHostAddressResolverFactory(new TwidereHostResolverFactory(app));
|
||||
cb.setHttpConnectionTimeout(connection_timeout);
|
||||
cb.setGZIPEnabled(enableGzip);
|
||||
cb.setIgnoreSSLError(ignoreSslError);
|
||||
cb.setIncludeCards(true);
|
||||
cb.setCardsPlatform("Android-12");
|
||||
// cb.setModelVersion(7);
|
||||
if (enableProxy) {
|
||||
final String proxy_host = prefs.getString(SharedPreferenceConstants.KEY_PROXY_HOST, null);
|
||||
final int proxy_port = ParseUtils.parseInt(prefs.getString(SharedPreferenceConstants.KEY_PROXY_PORT, "-1"));
|
||||
if (!isEmpty(proxy_host) && proxy_port > 0) {
|
||||
cb.setHttpProxyHost(proxy_host);
|
||||
cb.setHttpProxyPort(proxy_port);
|
||||
}
|
||||
}
|
||||
final String prefConsumerKey = prefs.getString(SharedPreferenceConstants.KEY_CONSUMER_KEY, TwidereConstants.TWITTER_CONSUMER_KEY);
|
||||
final String prefConsumerSecret = prefs.getString(SharedPreferenceConstants.KEY_CONSUMER_SECRET, TwidereConstants.TWITTER_CONSUMER_SECRET);
|
||||
final String apiUrlFormat = credentials.api_url_format;
|
||||
final String consumerKey = Utils.trim(credentials.consumer_key);
|
||||
final String consumerSecret = Utils.trim(credentials.consumer_secret);
|
||||
final boolean sameOAuthSigningUrl = credentials.same_oauth_signing_url;
|
||||
final boolean noVersionSuffix = credentials.no_version_suffix;
|
||||
if (!isEmpty(apiUrlFormat)) {
|
||||
final String versionSuffix = noVersionSuffix ? null : "/1.1/";
|
||||
cb.setRestBaseURL(Utils.getApiUrl(apiUrlFormat, "api", versionSuffix));
|
||||
cb.setOAuthBaseURL(Utils.getApiUrl(apiUrlFormat, "api", "/oauth/"));
|
||||
cb.setUploadBaseURL(Utils.getApiUrl(apiUrlFormat, "upload", versionSuffix));
|
||||
cb.setOAuthAuthorizationURL(Utils.getApiUrl(apiUrlFormat, null, null));
|
||||
if (!sameOAuthSigningUrl) {
|
||||
cb.setSigningRestBaseURL(TwitterConstants.DEFAULT_SIGNING_REST_BASE_URL);
|
||||
cb.setSigningOAuthBaseURL(TwitterConstants.DEFAULT_SIGNING_OAUTH_BASE_URL);
|
||||
cb.setSigningUploadBaseURL(TwitterConstants.DEFAULT_SIGNING_UPLOAD_BASE_URL);
|
||||
}
|
||||
}
|
||||
Utils.setClientUserAgent(context, consumerKey, consumerSecret, cb);
|
||||
|
||||
cb.setIncludeEntitiesEnabled(includeEntities);
|
||||
cb.setIncludeRTsEnabled(includeRetweets);
|
||||
cb.setIncludeReplyCountEnabled(true);
|
||||
cb.setIncludeDescendentReplyCountEnabled(true);
|
||||
switch (credentials.auth_type) {
|
||||
case TwidereDataStore.Accounts.AUTH_TYPE_OAUTH:
|
||||
case TwidereDataStore.Accounts.AUTH_TYPE_XAUTH: {
|
||||
if (!isEmpty(consumerKey) && !isEmpty(consumerSecret)) {
|
||||
cb.setOAuthConsumerKey(consumerKey);
|
||||
cb.setOAuthConsumerSecret(consumerSecret);
|
||||
} else if (!isEmpty(prefConsumerKey) && !isEmpty(prefConsumerSecret)) {
|
||||
cb.setOAuthConsumerKey(prefConsumerKey);
|
||||
cb.setOAuthConsumerSecret(prefConsumerSecret);
|
||||
} else {
|
||||
cb.setOAuthConsumerKey(TwidereConstants.TWITTER_CONSUMER_KEY);
|
||||
cb.setOAuthConsumerSecret(TwidereConstants.TWITTER_CONSUMER_SECRET);
|
||||
}
|
||||
final String token = credentials.oauth_token;
|
||||
final String tokenSecret = credentials.oauth_token_secret;
|
||||
if (isEmpty(token) || isEmpty(tokenSecret)) return null;
|
||||
return new TwitterFactory(cb.build()).getInstance(new OAuthToken(token, tokenSecret));
|
||||
}
|
||||
case TwidereDataStore.Accounts.AUTH_TYPE_BASIC: {
|
||||
final String screenName = credentials.screen_name;
|
||||
final String username = credentials.basic_auth_username;
|
||||
final String loginName = username != null ? username : screenName;
|
||||
final String password = credentials.basic_auth_password;
|
||||
if (isEmpty(loginName) || isEmpty(password)) return null;
|
||||
return new TwitterFactory(cb.build()).getInstance(new BasicAuthorization(loginName, password));
|
||||
}
|
||||
case TwidereDataStore.Accounts.AUTH_TYPE_TWIP_O_MODE: {
|
||||
return new TwitterFactory(cb.build()).getInstance(new EmptyAuthorization());
|
||||
}
|
||||
default: {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static RestHttpClient getHttpClient(final Context context, final int timeoutMillis,
|
||||
final boolean ignoreSslError, final Proxy proxy,
|
||||
final HostAddressResolverFactory resolverFactory,
|
||||
final String userAgent, final boolean twitterClientHeader) {
|
||||
final ConfigurationBuilder cb = new ConfigurationBuilder();
|
||||
cb.setHttpConnectionTimeout(timeoutMillis);
|
||||
cb.setIgnoreSSLError(ignoreSslError);
|
||||
cb.setIncludeTwitterClientHeader(twitterClientHeader);
|
||||
if (proxy != null && !Proxy.NO_PROXY.equals(proxy)) {
|
||||
final SocketAddress address = proxy.address();
|
||||
if (address instanceof InetSocketAddress) {
|
||||
cb.setHttpProxyHost(((InetSocketAddress) address).getHostName());
|
||||
cb.setHttpProxyPort(((InetSocketAddress) address).getPort());
|
||||
}
|
||||
}
|
||||
cb.setHostAddressResolverFactory(resolverFactory);
|
||||
return new OkHttpRestClient();
|
||||
}
|
||||
|
||||
public static RestHttpClient getDefaultHttpClient(final Context context) {
|
||||
if (context == null) return null;
|
||||
final SharedPreferences prefs = context.getSharedPreferences(TwidereConstants.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
|
||||
final int timeoutMillis = prefs.getInt(SharedPreferenceConstants.KEY_CONNECTION_TIMEOUT, 10000) * 1000;
|
||||
final Proxy proxy = Utils.getProxy(context);
|
||||
final String userAgent = TwidereApplication.getInstance(context).getDefaultUserAgent();
|
||||
final HostAddressResolverFactory resolverFactory = new TwidereHostResolverFactory(
|
||||
TwidereApplication.getInstance(context));
|
||||
return getHttpClient(context, timeoutMillis, true, proxy, resolverFactory, userAgent, false);
|
||||
}
|
||||
|
||||
public static RestHttpClient getImageLoaderHttpClient(final Context context) {
|
||||
if (context == null) return null;
|
||||
final SharedPreferences prefs = context.getSharedPreferences(TwidereConstants.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
|
||||
final int timeoutMillis = prefs.getInt(SharedPreferenceConstants.KEY_CONNECTION_TIMEOUT, 10000) * 1000;
|
||||
final Proxy proxy = Utils.getProxy(context);
|
||||
final String userAgent = TwidereApplication.getInstance(context).getDefaultUserAgent();
|
||||
final HostAddressResolverFactory resolverFactory = new TwidereHostResolverFactory(
|
||||
TwidereApplication.getInstance(context));
|
||||
return getHttpClient(context, timeoutMillis, true, proxy, resolverFactory, userAgent, false);
|
||||
}
|
||||
}
|
@ -47,7 +47,7 @@ import twitter4j.Twitter;
|
||||
import twitter4j.TwitterException;
|
||||
import twitter4j.User;
|
||||
|
||||
import static org.mariotaku.twidere.util.Utils.getTwitterInstance;
|
||||
import static org.mariotaku.twidere.util.TwitterAPIUtils.getTwitterInstance;
|
||||
|
||||
public class TwitterWrapper implements Constants {
|
||||
|
||||
@ -67,7 +67,7 @@ public class TwitterWrapper implements Constants {
|
||||
}
|
||||
|
||||
public static SingleResponse<Boolean> deleteProfileBannerImage(final Context context, final long account_id) {
|
||||
final Twitter twitter = getTwitterInstance(context, account_id, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(context, account_id, false);
|
||||
if (twitter == null) return new SingleResponse<>(false, null);
|
||||
try {
|
||||
twitter.removeProfileBannerImage();
|
||||
@ -164,7 +164,7 @@ public class TwitterWrapper implements Constants {
|
||||
|
||||
public static SingleResponse<ParcelableUser> updateProfile(final Context context, final long account_id,
|
||||
final String name, final String url, final String location, final String description) {
|
||||
final Twitter twitter = getTwitterInstance(context, account_id, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(context, account_id, false);
|
||||
if (twitter != null) {
|
||||
try {
|
||||
final User user = twitter.updateProfile(name, url, location, description);
|
||||
@ -179,7 +179,7 @@ public class TwitterWrapper implements Constants {
|
||||
public static void updateProfileBannerImage(final Context context, final long accountId,
|
||||
final Uri imageUri, final boolean deleteImage)
|
||||
throws FileNotFoundException, TwitterException {
|
||||
final Twitter twitter = getTwitterInstance(context, accountId, false);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(context, accountId, false);
|
||||
updateProfileBannerImage(context, twitter, imageUri, deleteImage);
|
||||
}
|
||||
|
||||
@ -220,7 +220,7 @@ public class TwitterWrapper implements Constants {
|
||||
public static User updateProfileImage(final Context context, final long accountId,
|
||||
final Uri imageUri, final boolean deleteImage)
|
||||
throws FileNotFoundException, TwitterException {
|
||||
final Twitter twitter = getTwitterInstance(context, accountId, true);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(context, accountId, true);
|
||||
return updateProfileImage(context, twitter, imageUri, deleteImage);
|
||||
}
|
||||
|
||||
|
@ -131,7 +131,6 @@ import org.mariotaku.querybuilder.Tables;
|
||||
import org.mariotaku.querybuilder.query.SQLSelectQuery;
|
||||
import org.mariotaku.simplerestapi.http.Authorization;
|
||||
import org.mariotaku.simplerestapi.http.RestHttpClient;
|
||||
import org.mariotaku.simplerestapi.http.RestResponse;
|
||||
import org.mariotaku.twidere.BuildConfig;
|
||||
import org.mariotaku.twidere.Constants;
|
||||
import org.mariotaku.twidere.R;
|
||||
@ -141,7 +140,6 @@ import org.mariotaku.twidere.activity.support.MediaViewerActivity;
|
||||
import org.mariotaku.twidere.adapter.iface.IBaseAdapter;
|
||||
import org.mariotaku.twidere.adapter.iface.IBaseCardAdapter;
|
||||
import org.mariotaku.twidere.api.twitter.auth.BasicAuthorization;
|
||||
import org.mariotaku.twidere.api.twitter.auth.EmptyAuthorization;
|
||||
import org.mariotaku.twidere.api.twitter.auth.OAuthAuthorization;
|
||||
import org.mariotaku.twidere.api.twitter.auth.OAuthToken;
|
||||
import org.mariotaku.twidere.api.twitter.auth.XAuthAuthorization;
|
||||
@ -222,7 +220,6 @@ import org.mariotaku.twidere.service.RefreshService;
|
||||
import org.mariotaku.twidere.util.TwidereLinkify.HighlightStyle;
|
||||
import org.mariotaku.twidere.util.content.ContentResolverUtils;
|
||||
import org.mariotaku.twidere.util.menu.TwidereMenuInfo;
|
||||
import org.mariotaku.twidere.util.net.OkHttpClientFactory;
|
||||
import org.mariotaku.twidere.util.net.TwidereHostResolverFactory;
|
||||
import org.mariotaku.twidere.view.CardMediaContainer.OnMediaClickListener;
|
||||
import org.mariotaku.twidere.view.CardMediaContainer.PreviewStyle;
|
||||
@ -265,7 +262,6 @@ import twitter4j.Status;
|
||||
import twitter4j.Twitter;
|
||||
import twitter4j.TwitterConstants;
|
||||
import twitter4j.TwitterException;
|
||||
import twitter4j.TwitterFactory;
|
||||
import twitter4j.UserMentionEntity;
|
||||
import twitter4j.conf.Configuration;
|
||||
import twitter4j.conf.ConfigurationBuilder;
|
||||
@ -1172,7 +1168,7 @@ public final class Utils implements Constants, TwitterConstants {
|
||||
if (context == null) throw new NullPointerException();
|
||||
final ParcelableStatus cached = findStatusInDatabases(context, accountId, statusId);
|
||||
if (cached != null) return cached;
|
||||
final Twitter twitter = getTwitterInstance(context, accountId, true);
|
||||
final Twitter twitter = TwitterAPIUtils.getTwitterInstance(context, accountId, true);
|
||||
if (twitter == null) throw new TwitterException("Account does not exist");
|
||||
final Status status = twitter.showStatus(statusId);
|
||||
final String where = Expression.and(Expression.equals(Statuses.ACCOUNT_ID, accountId),
|
||||
@ -1764,17 +1760,6 @@ public final class Utils implements Constants, TwitterConstants {
|
||||
return context.getResources().getInteger(R.integer.default_text_size);
|
||||
}
|
||||
|
||||
public static Twitter getDefaultTwitterInstance(final Context context, final boolean includeEntities) {
|
||||
if (context == null) return null;
|
||||
return getDefaultTwitterInstance(context, includeEntities, true);
|
||||
}
|
||||
|
||||
public static Twitter getDefaultTwitterInstance(final Context context, final boolean includeEntities,
|
||||
final boolean includeRetweets) {
|
||||
if (context == null) return null;
|
||||
return getTwitterInstance(context, getDefaultAccountId(context), includeEntities, includeRetweets);
|
||||
}
|
||||
|
||||
public static String getErrorMessage(final Context context, final CharSequence message) {
|
||||
if (context == null) return ParseUtils.parseString(message);
|
||||
if (isEmpty(message)) return context.getString(R.string.error_unknown_error);
|
||||
@ -1812,51 +1797,6 @@ public final class Utils implements Constants, TwitterConstants {
|
||||
}
|
||||
|
||||
|
||||
public static RestHttpClient getHttpClient(final Context context, final int timeoutMillis,
|
||||
final boolean ignoreSslError, final Proxy proxy,
|
||||
final HostAddressResolverFactory resolverFactory,
|
||||
final String userAgent, final boolean twitterClientHeader) {
|
||||
final ConfigurationBuilder cb = new ConfigurationBuilder();
|
||||
cb.setHttpConnectionTimeout(timeoutMillis);
|
||||
cb.setIgnoreSSLError(ignoreSslError);
|
||||
cb.setIncludeTwitterClientHeader(twitterClientHeader);
|
||||
if (proxy != null && !Proxy.NO_PROXY.equals(proxy)) {
|
||||
final SocketAddress address = proxy.address();
|
||||
if (address instanceof InetSocketAddress) {
|
||||
cb.setHttpProxyHost(((InetSocketAddress) address).getHostName());
|
||||
cb.setHttpProxyPort(((InetSocketAddress) address).getPort());
|
||||
}
|
||||
}
|
||||
cb.setHostAddressResolverFactory(resolverFactory);
|
||||
if (userAgent != null) {
|
||||
cb.setHttpUserAgent(userAgent);
|
||||
}
|
||||
cb.setHttpClientFactory(new OkHttpClientFactory(context));
|
||||
return new HttpClientWrapper(cb.build());
|
||||
}
|
||||
|
||||
public static RestHttpClient getDefaultHttpClient(final Context context) {
|
||||
if (context == null) return null;
|
||||
final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
|
||||
final int timeoutMillis = prefs.getInt(KEY_CONNECTION_TIMEOUT, 10000) * 1000;
|
||||
final Proxy proxy = getProxy(context);
|
||||
final String userAgent = TwidereApplication.getInstance(context).getDefaultUserAgent();
|
||||
final HostAddressResolverFactory resolverFactory = new TwidereHostResolverFactory(
|
||||
TwidereApplication.getInstance(context));
|
||||
return getHttpClient(context, timeoutMillis, true, proxy, resolverFactory, userAgent, false);
|
||||
}
|
||||
|
||||
public static RestHttpClient getImageLoaderHttpClient(final Context context) {
|
||||
if (context == null) return null;
|
||||
final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
|
||||
final int timeoutMillis = prefs.getInt(KEY_CONNECTION_TIMEOUT, 10000) * 1000;
|
||||
final Proxy proxy = getProxy(context);
|
||||
final String userAgent = TwidereApplication.getInstance(context).getDefaultUserAgent();
|
||||
final HostAddressResolverFactory resolverFactory = new TwidereHostResolverFactory(
|
||||
TwidereApplication.getInstance(context));
|
||||
return getHttpClient(context, timeoutMillis, true, proxy, resolverFactory, userAgent, false);
|
||||
}
|
||||
|
||||
public static String getImageMimeType(final File image) {
|
||||
if (image == null) return null;
|
||||
final BitmapFactory.Options o = new BitmapFactory.Options();
|
||||
@ -2186,38 +2126,6 @@ public final class Utils implements Constants, TwitterConstants {
|
||||
return getTwitterProfileImageOfSize(url, "reasonably_small");
|
||||
}
|
||||
|
||||
public static RestResponse getRedirectedHttpResponse(@NonNull final RestHttpClient client, @NonNull final String url,
|
||||
final String signUrl, final Authorization auth,
|
||||
final HashMap<String, List<String>> additionalHeaders)
|
||||
throws TwitterException {
|
||||
final ArrayList<String> urls = new ArrayList<>();
|
||||
urls.add(url);
|
||||
RestResponse resp;
|
||||
try {
|
||||
resp = client.get(url, signUrl, auth, additionalHeaders);
|
||||
} catch (final TwitterException te) {
|
||||
if (isRedirected(te.getStatusCode())) {
|
||||
resp = te.getHttpResponse();
|
||||
} else
|
||||
throw te;
|
||||
}
|
||||
while (resp != null && isRedirected(resp.getStatus())) {
|
||||
final String request_url = resp.getHeader("Location");
|
||||
if (request_url == null) return null;
|
||||
if (urls.contains(request_url)) throw new TwitterException("Too many redirects");
|
||||
urls.add(request_url);
|
||||
try {
|
||||
resp = client.get(request_url, request_url, additionalHeaders);
|
||||
} catch (final TwitterException te) {
|
||||
if (isRedirected(te.getStatusCode())) {
|
||||
resp = te.getHttpResponse();
|
||||
} else
|
||||
throw te;
|
||||
}
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
|
||||
public static int getResId(final Context context, final String string) {
|
||||
if (context == null || string == null) return 0;
|
||||
Matcher m = PATTERN_RESOURCE_IDENTIFIER.matcher(string);
|
||||
@ -2524,103 +2432,6 @@ public final class Utils implements Constants, TwitterConstants {
|
||||
return te.getMessage();
|
||||
}
|
||||
|
||||
public static Twitter getTwitterInstance(final Context context, final long accountId,
|
||||
final boolean includeEntities) {
|
||||
return getTwitterInstance(context, accountId, includeEntities, true);
|
||||
}
|
||||
|
||||
|
||||
@Nullable
|
||||
public static Twitter getTwitterInstance(final Context context, final long accountId,
|
||||
final boolean includeEntities,
|
||||
final boolean includeRetweets) {
|
||||
if (context == null) return null;
|
||||
final TwidereApplication app = TwidereApplication.getInstance(context);
|
||||
final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
|
||||
final int connection_timeout = prefs.getInt(KEY_CONNECTION_TIMEOUT, 10) * 1000;
|
||||
final boolean enableGzip = prefs.getBoolean(KEY_GZIP_COMPRESSING, true);
|
||||
final boolean ignoreSslError = prefs.getBoolean(KEY_IGNORE_SSL_ERROR, false);
|
||||
final boolean enableProxy = prefs.getBoolean(KEY_ENABLE_PROXY, false);
|
||||
// Here I use old consumer key/secret because it's default key for older
|
||||
// versions
|
||||
final ParcelableCredentials credentials = ParcelableCredentials.getCredentials(context, accountId);
|
||||
if (credentials == null) return null;
|
||||
final ConfigurationBuilder cb = new ConfigurationBuilder();
|
||||
cb.setHostAddressResolverFactory(new TwidereHostResolverFactory(app));
|
||||
cb.setHttpClientFactory(new OkHttpClientFactory(context));
|
||||
cb.setHttpConnectionTimeout(connection_timeout);
|
||||
cb.setGZIPEnabled(enableGzip);
|
||||
cb.setIgnoreSSLError(ignoreSslError);
|
||||
cb.setIncludeCards(true);
|
||||
cb.setCardsPlatform("Android-12");
|
||||
// cb.setModelVersion(7);
|
||||
if (enableProxy) {
|
||||
final String proxy_host = prefs.getString(KEY_PROXY_HOST, null);
|
||||
final int proxy_port = ParseUtils.parseInt(prefs.getString(KEY_PROXY_PORT, "-1"));
|
||||
if (!isEmpty(proxy_host) && proxy_port > 0) {
|
||||
cb.setHttpProxyHost(proxy_host);
|
||||
cb.setHttpProxyPort(proxy_port);
|
||||
}
|
||||
}
|
||||
final String prefConsumerKey = prefs.getString(KEY_CONSUMER_KEY, TWITTER_CONSUMER_KEY);
|
||||
final String prefConsumerSecret = prefs.getString(KEY_CONSUMER_SECRET, TWITTER_CONSUMER_SECRET);
|
||||
final String apiUrlFormat = credentials.api_url_format;
|
||||
final String consumerKey = trim(credentials.consumer_key);
|
||||
final String consumerSecret = trim(credentials.consumer_secret);
|
||||
final boolean sameOAuthSigningUrl = credentials.same_oauth_signing_url;
|
||||
final boolean noVersionSuffix = credentials.no_version_suffix;
|
||||
if (!isEmpty(apiUrlFormat)) {
|
||||
final String versionSuffix = noVersionSuffix ? null : "/1.1/";
|
||||
cb.setRestBaseURL(getApiUrl(apiUrlFormat, "api", versionSuffix));
|
||||
cb.setOAuthBaseURL(getApiUrl(apiUrlFormat, "api", "/oauth/"));
|
||||
cb.setUploadBaseURL(getApiUrl(apiUrlFormat, "upload", versionSuffix));
|
||||
cb.setOAuthAuthorizationURL(getApiUrl(apiUrlFormat, null, null));
|
||||
if (!sameOAuthSigningUrl) {
|
||||
cb.setSigningRestBaseURL(DEFAULT_SIGNING_REST_BASE_URL);
|
||||
cb.setSigningOAuthBaseURL(DEFAULT_SIGNING_OAUTH_BASE_URL);
|
||||
cb.setSigningUploadBaseURL(DEFAULT_SIGNING_UPLOAD_BASE_URL);
|
||||
}
|
||||
}
|
||||
setClientUserAgent(context, consumerKey, consumerSecret, cb);
|
||||
|
||||
cb.setIncludeEntitiesEnabled(includeEntities);
|
||||
cb.setIncludeRTsEnabled(includeRetweets);
|
||||
cb.setIncludeReplyCountEnabled(true);
|
||||
cb.setIncludeDescendentReplyCountEnabled(true);
|
||||
switch (credentials.auth_type) {
|
||||
case Accounts.AUTH_TYPE_OAUTH:
|
||||
case Accounts.AUTH_TYPE_XAUTH: {
|
||||
if (!isEmpty(consumerKey) && !isEmpty(consumerSecret)) {
|
||||
cb.setOAuthConsumerKey(consumerKey);
|
||||
cb.setOAuthConsumerSecret(consumerSecret);
|
||||
} else if (!isEmpty(prefConsumerKey) && !isEmpty(prefConsumerSecret)) {
|
||||
cb.setOAuthConsumerKey(prefConsumerKey);
|
||||
cb.setOAuthConsumerSecret(prefConsumerSecret);
|
||||
} else {
|
||||
cb.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
|
||||
cb.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
|
||||
}
|
||||
final String token = credentials.oauth_token;
|
||||
final String tokenSecret = credentials.oauth_token_secret;
|
||||
if (isEmpty(token) || isEmpty(tokenSecret)) return null;
|
||||
return new TwitterFactory(cb.build()).getInstance(new OAuthToken(token, tokenSecret));
|
||||
}
|
||||
case Accounts.AUTH_TYPE_BASIC: {
|
||||
final String screenName = credentials.screen_name;
|
||||
final String username = credentials.basic_auth_username;
|
||||
final String loginName = username != null ? username : screenName;
|
||||
final String password = credentials.basic_auth_password;
|
||||
if (isEmpty(loginName) || isEmpty(password)) return null;
|
||||
return new TwitterFactory(cb.build()).getInstance(new BasicAuthorization(loginName, password));
|
||||
}
|
||||
case Accounts.AUTH_TYPE_TWIP_O_MODE: {
|
||||
return new TwitterFactory(cb.build()).getInstance(new EmptyAuthorization());
|
||||
}
|
||||
default: {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String getTwitterProfileImageOfSize(final String url, final String size) {
|
||||
if (url == null) return null;
|
||||
|
@ -24,6 +24,7 @@ import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Pair;
|
||||
import android.webkit.URLUtil;
|
||||
|
||||
import com.nostra13.universalimageloader.core.assist.ContentLengthInputStream;
|
||||
@ -47,14 +48,16 @@ import org.mariotaku.twidere.util.Utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import twitter4j.TwitterException;
|
||||
|
||||
import static org.mariotaku.twidere.util.TwidereLinkify.PATTERN_TWITTER_PROFILE_IMAGES;
|
||||
import static org.mariotaku.twidere.util.Utils.getImageLoaderHttpClient;
|
||||
import static org.mariotaku.twidere.util.TwitterAPIUtils.getImageLoaderHttpClient;
|
||||
import static org.mariotaku.twidere.util.TwitterAPIUtils.getRedirectedHttpResponse;
|
||||
import static org.mariotaku.twidere.util.Utils.getNormalTwitterProfileImage;
|
||||
import static org.mariotaku.twidere.util.Utils.getRedirectedHttpResponse;
|
||||
import static org.mariotaku.twidere.util.Utils.getTwitterAuthorization;
|
||||
import static org.mariotaku.twidere.util.Utils.getTwitterProfileImageOfSize;
|
||||
|
||||
@ -165,9 +168,9 @@ public class TwidereImageDownloader extends BaseImageDownloader implements Const
|
||||
if (mThumbor != null) {
|
||||
modifiedUri = mThumbor.buildImage(modifiedUri).filter(ThumborUrlBuilder.quality(85)).toUrl();
|
||||
}
|
||||
final HeaderMap additionalHeaders = new HeaderMap();
|
||||
final List<Pair<String, String>> additionalHeaders = new ArrayList<>();
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
||||
additionalHeaders.addHeader("Accept", "image/webp, */*");
|
||||
additionalHeaders.add(Pair.create("Accept", "image/webp, */*"));
|
||||
}
|
||||
final RestResponse resp = getRedirectedHttpResponse(mClient, modifiedUri, uriString, auth, additionalHeaders);
|
||||
final TypedData body = resp.getBody();
|
||||
|
@ -1,292 +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.twidere.util.net;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.SSLCertificateSocketFactory;
|
||||
import android.net.Uri;
|
||||
|
||||
import com.nostra13.universalimageloader.utils.IoUtils;
|
||||
import com.squareup.okhttp.Headers;
|
||||
import com.squareup.okhttp.MediaType;
|
||||
import com.squareup.okhttp.MultipartBuilder;
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import com.squareup.okhttp.Request.Builder;
|
||||
import com.squareup.okhttp.RequestBody;
|
||||
import com.squareup.okhttp.Response;
|
||||
import com.squareup.okhttp.internal.Internal;
|
||||
import com.squareup.okhttp.internal.Network;
|
||||
|
||||
import org.mariotaku.twidere.TwidereConstants;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Proxy;
|
||||
import java.net.Proxy.Type;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
import javax.net.SocketFactory;
|
||||
|
||||
import okio.BufferedSink;
|
||||
import twitter4j.TwitterException;
|
||||
import twitter4j.auth.Authorization;
|
||||
import twitter4j.http.HostAddressResolver;
|
||||
import twitter4j.http.HttpClient;
|
||||
import twitter4j.http.HttpClientConfiguration;
|
||||
import twitter4j.http.HttpParameter;
|
||||
import twitter4j.http.HttpRequest;
|
||||
import twitter4j.http.HttpResponse;
|
||||
import twitter4j.http.RequestMethod;
|
||||
|
||||
/**
|
||||
* Created by mariotaku on 15/1/22.
|
||||
*/
|
||||
public class OkHttpClientImpl implements HttpClient, TwidereConstants {
|
||||
|
||||
public static final MediaType APPLICATION_FORM_URLENCODED = MediaType.parse("application/x-www-form-urlencoded; charset=UTF-8");
|
||||
private final Context context;
|
||||
private final HttpClientConfiguration conf;
|
||||
private final OkHttpClient client;
|
||||
private final HostAddressResolver resolver;
|
||||
|
||||
public OkHttpClientImpl(Context context, HttpClientConfiguration conf) {
|
||||
this.context = context;
|
||||
this.conf = conf;
|
||||
this.resolver = conf.getHostAddressResolverFactory().getInstance(conf);
|
||||
this.client = createHttpClient(conf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpResponse request(HttpRequest req) throws TwitterException {
|
||||
final Builder builder = new Builder();
|
||||
for (Entry<String, List<String>> headerEntry : req.getRequestHeaders().entrySet()) {
|
||||
final String name = headerEntry.getKey();
|
||||
for (String value : headerEntry.getValue()) {
|
||||
builder.addHeader(name, value);
|
||||
}
|
||||
}
|
||||
final Authorization authorization = req.getAuthorization();
|
||||
if (authorization != null) {
|
||||
final String authHeader = authorization.getAuthorizationHeader(req);
|
||||
if (authHeader != null) {
|
||||
builder.header("Authorization", authHeader);
|
||||
}
|
||||
}
|
||||
Response response = null;
|
||||
try {
|
||||
setupRequestBuilder(builder, req);
|
||||
response = client.newCall(builder.build()).execute();
|
||||
return new OkHttpResponse(conf, null, response);
|
||||
} catch (IOException e) {
|
||||
throw new TwitterException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
|
||||
}
|
||||
|
||||
private OkHttpClient createHttpClient(HttpClientConfiguration conf) {
|
||||
final OkHttpClient client = new OkHttpClient();
|
||||
final boolean ignoreSSLError = conf.isSSLErrorIgnored();
|
||||
final SSLCertificateSocketFactory sslSocketFactory;
|
||||
if (ignoreSSLError) {
|
||||
sslSocketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory.getInsecure(0, null);
|
||||
} else {
|
||||
sslSocketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory.getDefault(0, null);
|
||||
}
|
||||
// sslSocketFactory.setTrustManagers(new TrustManager[]{new TwidereTrustManager(context)});
|
||||
// client.setHostnameVerifier(new HostResolvedHostnameVerifier(context, ignoreSSLError));
|
||||
client.setSslSocketFactory(sslSocketFactory);
|
||||
client.setSocketFactory(SocketFactory.getDefault());
|
||||
client.setConnectTimeout(conf.getHttpConnectionTimeout(), TimeUnit.MILLISECONDS);
|
||||
|
||||
if (conf.isProxyConfigured()) {
|
||||
client.setProxy(new Proxy(Type.HTTP, InetSocketAddress.createUnresolved(conf.getHttpProxyHost(),
|
||||
conf.getHttpProxyPort())));
|
||||
}
|
||||
Internal.instance.setNetwork(client, new Network() {
|
||||
@Override
|
||||
public InetAddress[] resolveInetAddresses(String host) throws UnknownHostException {
|
||||
try {
|
||||
return resolver.resolve(host);
|
||||
} catch (IOException e) {
|
||||
if (e instanceof UnknownHostException) throw (UnknownHostException) e;
|
||||
throw new UnknownHostException("Unable to resolve address " + e.getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
return client;
|
||||
}
|
||||
|
||||
private RequestBody getRequestBody(HttpParameter[] params) throws IOException {
|
||||
if (params == null) return null;
|
||||
if (!HttpParameter.containsFile(params)) {
|
||||
return RequestBody.create(APPLICATION_FORM_URLENCODED, HttpParameter.encodeParameters(params));
|
||||
}
|
||||
final MultipartBuilder builder = new MultipartBuilder();
|
||||
builder.type(MultipartBuilder.FORM);
|
||||
for (final HttpParameter param : params) {
|
||||
if (param.isFile()) {
|
||||
RequestBody requestBody;
|
||||
if (param.hasFileBody()) {
|
||||
requestBody = new StreamRequestBody(MediaType.parse(param.getContentType()), param.getFileBody(), true);
|
||||
} else {
|
||||
requestBody = RequestBody.create(MediaType.parse(param.getContentType()), param.getFile());
|
||||
}
|
||||
builder.addFormDataPart(param.getName(), param.getFileName(), requestBody);
|
||||
} else {
|
||||
builder.addFormDataPart(param.getName(), param.getValue());
|
||||
}
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
static class StreamRequestBody extends RequestBody {
|
||||
|
||||
private final MediaType contentType;
|
||||
private final InputStream stream;
|
||||
private final boolean closeAfterWrite;
|
||||
|
||||
StreamRequestBody(MediaType contentType, InputStream stream, boolean closeAfterWrite) {
|
||||
this.contentType = contentType;
|
||||
this.stream = stream;
|
||||
this.closeAfterWrite = closeAfterWrite;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MediaType contentType() {
|
||||
return contentType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(BufferedSink sink) throws IOException {
|
||||
int len;
|
||||
byte[] buf = new byte[8192];
|
||||
while ((len = stream.read(buf)) != -1) {
|
||||
sink.write(buf, 0, len);
|
||||
}
|
||||
if (closeAfterWrite) {
|
||||
IoUtils.closeSilently(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setupRequestBuilder(Builder builder, HttpRequest req) throws IOException {
|
||||
final Uri.Builder uriBuilder = Uri.parse(req.getURL()).buildUpon();
|
||||
final RequestMethod method = req.getMethod();
|
||||
if (method != RequestMethod.POST && method != RequestMethod.PUT) {
|
||||
final HttpParameter[] parameters = req.getParameters();
|
||||
if (parameters != null) {
|
||||
for (HttpParameter param : parameters) {
|
||||
uriBuilder.appendQueryParameter(param.getName(), param.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
final Uri uri = uriBuilder.build();
|
||||
switch (req.getMethod()) {
|
||||
case GET: {
|
||||
builder.get();
|
||||
break;
|
||||
}
|
||||
case POST: {
|
||||
builder.post(getRequestBody(req.getParameters()));
|
||||
break;
|
||||
}
|
||||
case DELETE: {
|
||||
builder.delete();
|
||||
break;
|
||||
}
|
||||
case HEAD: {
|
||||
builder.head();
|
||||
break;
|
||||
}
|
||||
case PUT: {
|
||||
builder.put(getRequestBody(req.getParameters()));
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
builder.url(uri.toString());
|
||||
}
|
||||
|
||||
private static class OkHttpResponse extends HttpResponse {
|
||||
|
||||
private final Response response;
|
||||
|
||||
public OkHttpResponse(HttpClientConfiguration conf, HttpRequest request, Response response)
|
||||
throws TwitterException, IOException {
|
||||
super(conf);
|
||||
this.response = response;
|
||||
statusCode = response.code();
|
||||
if ("gzip".equals(response.header("Content-Encoding"))) {
|
||||
is = new GZIPInputStream(response.body().byteStream());
|
||||
} else {
|
||||
is = response.body().byteStream();
|
||||
}
|
||||
if (!response.isSuccessful()) {
|
||||
throw new TwitterException(response.message(), request, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect() throws IOException {
|
||||
if (is != null) {
|
||||
is.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getResponseHeader(String name) {
|
||||
return response.header(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, List<String>> getResponseHeaderFields() {
|
||||
final Headers headers = response.headers();
|
||||
final Map<String, List<String>> maps = new HashMap<>();
|
||||
for (final String name : headers.names()) {
|
||||
final List<String> values = new ArrayList<>(1);
|
||||
for (final String value : headers.values(name)) {
|
||||
values.add(value);
|
||||
}
|
||||
maps.put(name, values);
|
||||
}
|
||||
return maps;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getResponseHeaders(String name) {
|
||||
return response.headers(name);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
<?xml version="1.0" encoding="utf-8"?><!--
|
||||
~ Twidere - Twitter client for Android
|
||||
~
|
||||
~ Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com>
|
||||
@ -18,8 +17,7 @@
|
||||
~ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
-->
|
||||
|
||||
<ScrollView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
@ -43,7 +41,7 @@
|
||||
android:layout_weight="1"
|
||||
android:singleLine="true"
|
||||
android:text="@string/api_url_format"
|
||||
android:textAppearance="?android:attr/textAppearanceSmall"/>
|
||||
android:textAppearance="?android:attr/textAppearanceSmall" />
|
||||
|
||||
<org.mariotaku.twidere.view.ActionIconView
|
||||
android:id="@+id/api_url_format_help"
|
||||
@ -51,7 +49,7 @@
|
||||
android:layout_height="@dimen/element_size_small"
|
||||
android:layout_weight="0"
|
||||
android:background="?selectableItemBackground"
|
||||
android:src="@drawable/ic_action_info"/>
|
||||
android:src="@drawable/ic_action_info" />
|
||||
</LinearLayout>
|
||||
|
||||
<EditText
|
||||
@ -60,14 +58,14 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:ems="10"
|
||||
android:inputType="textUri"
|
||||
android:singleLine="true"/>
|
||||
android:singleLine="true" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/label_auth_type"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/auth_type"
|
||||
android:textAppearance="?android:attr/textAppearanceSmall"/>
|
||||
android:textAppearance="?android:attr/textAppearanceSmall" />
|
||||
|
||||
<HorizontalScrollView
|
||||
android:id="@+id/auth_type_scroll"
|
||||
@ -85,31 +83,31 @@
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="@string/oauth"/>
|
||||
android:text="@string/oauth" />
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/basic"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="@string/basic"/>
|
||||
android:text="@string/basic" />
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/twip_o"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="@string/twip_o"/>
|
||||
android:text="@string/twip_o" />
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/xauth"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="@string/xauth"/>
|
||||
android:text="@string/xauth" />
|
||||
</RadioGroup>
|
||||
</HorizontalScrollView>
|
||||
|
||||
<include layout="@layout/layout_api_editor_advanced_fields"/>
|
||||
<include layout="@layout/layout_api_editor_advanced_fields" />
|
||||
</LinearLayout>
|
||||
</ScrollView>
|
@ -17,8 +17,7 @@
|
||||
~ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
-->
|
||||
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:id="@+id/advanced_api_config"
|
||||
android:layout_width="match_parent"
|
||||
|
Loading…
x
Reference in New Issue
Block a user