parent
42a8f47991
commit
6f25405ad4
@ -1,117 +0,0 @@ |
||||
/* Copyright 2017 Andrew Dawson |
||||
* |
||||
* This file is part of Tusky. |
||||
* |
||||
* Tusky 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. |
||||
* |
||||
* Tusky 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 Tusky. If not, see |
||||
* <http://www.gnu.org/licenses/>. */
|
||||
|
||||
package com.keylesspalace.tusky; |
||||
|
||||
import com.android.volley.NetworkResponse; |
||||
import com.android.volley.ParseError; |
||||
import com.android.volley.Request; |
||||
import com.android.volley.Response; |
||||
import com.android.volley.toolbox.HttpHeaderParser; |
||||
|
||||
import org.json.JSONException; |
||||
import org.json.JSONObject; |
||||
|
||||
import java.io.ByteArrayOutputStream; |
||||
import java.io.DataOutputStream; |
||||
import java.io.IOException; |
||||
import java.io.UnsupportedEncodingException; |
||||
|
||||
class MultipartRequest extends Request<JSONObject> { |
||||
private static final String CHARSET = "utf-8"; |
||||
private final String boundary = "something-" + System.currentTimeMillis(); |
||||
|
||||
private JSONObject parameters; |
||||
private Response.Listener<JSONObject> listener; |
||||
|
||||
MultipartRequest(int method, String url, JSONObject parameters, |
||||
Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) { |
||||
super(method, url, errorListener); |
||||
this.parameters = parameters; |
||||
this.listener = listener; |
||||
} |
||||
|
||||
@Override |
||||
public String getBodyContentType() { |
||||
return "multipart/form-data;boundary=" + boundary; |
||||
} |
||||
|
||||
@Override |
||||
public byte[] getBody() { |
||||
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); |
||||
DataOutputStream stream = new DataOutputStream(byteStream); |
||||
try { |
||||
// Write the JSON parameters first.
|
||||
if (parameters != null) { |
||||
stream.writeBytes(String.format("--%s\r\n", boundary)); |
||||
stream.writeBytes("Content-Disposition: form-data; name=\"parameters\"\r\n"); |
||||
stream.writeBytes(String.format( |
||||
"Content-Type: application/json; charset=%s\r\n", CHARSET)); |
||||
stream.writeBytes("\r\n"); |
||||
stream.writeBytes(parameters.toString()); |
||||
} |
||||
|
||||
// Write the binary data.
|
||||
DataItem data = getData(); |
||||
if (data != null) { |
||||
stream.writeBytes(String.format("--%s\r\n", boundary)); |
||||
stream.writeBytes(String.format( |
||||
"Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n", |
||||
data.name, data.filename)); |
||||
stream.writeBytes(String.format("Content-Type: %s\r\n", data.mimeType)); |
||||
stream.writeBytes(String.format("Content-Length: %s\r\n", |
||||
String.valueOf(data.content.length))); |
||||
stream.writeBytes("\r\n"); |
||||
stream.write(data.content); |
||||
} |
||||
|
||||
// Close the multipart form data.
|
||||
stream.writeBytes(String.format("--%s--\r\n", boundary)); |
||||
|
||||
return byteStream.toByteArray(); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { |
||||
try { |
||||
String jsonString = new String(response.data, |
||||
HttpHeaderParser.parseCharset(response.headers)); |
||||
return Response.success(new JSONObject(jsonString), |
||||
HttpHeaderParser.parseCacheHeaders(response)); |
||||
} catch (JSONException|UnsupportedEncodingException e) { |
||||
return Response.error(new ParseError(e)); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected void deliverResponse(JSONObject response) { |
||||
listener.onResponse(response); |
||||
} |
||||
|
||||
public DataItem getData() { |
||||
return null; |
||||
} |
||||
|
||||
static class DataItem { |
||||
String name; |
||||
String filename; |
||||
String mimeType; |
||||
byte[] content; |
||||
} |
||||
} |
@ -1,83 +0,0 @@ |
||||
/* Copyright 2017 Andrew Dawson |
||||
* |
||||
* This file is part of Tusky. |
||||
* |
||||
* Tusky 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. |
||||
* |
||||
* Tusky 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 Tusky. If not, see |
||||
* <http://www.gnu.org/licenses/>. */
|
||||
|
||||
package com.keylesspalace.tusky; |
||||
|
||||
import android.content.Context; |
||||
import android.graphics.Bitmap; |
||||
import android.support.v4.util.LruCache; |
||||
|
||||
import com.android.volley.Request; |
||||
import com.android.volley.RequestQueue; |
||||
import com.android.volley.toolbox.ImageLoader; |
||||
import com.android.volley.toolbox.Volley; |
||||
|
||||
import java.lang.ref.WeakReference; |
||||
|
||||
public class VolleySingleton { |
||||
private static VolleySingleton instance; |
||||
private RequestQueue requestQueue; |
||||
private ImageLoader imageLoader; |
||||
/* This is a weak reference to account for the case where it might be held onto beyond the |
||||
* lifetime of the Activity/Fragment/Service, so it can be cleaned up. */ |
||||
private static WeakReference<Context> context; |
||||
|
||||
private VolleySingleton(Context context) { |
||||
VolleySingleton.context = new WeakReference<>(context); |
||||
requestQueue = getRequestQueue(); |
||||
imageLoader = new ImageLoader(requestQueue, |
||||
new ImageLoader.ImageCache() { |
||||
private final LruCache<String, Bitmap> cache = new LruCache<>(20); |
||||
|
||||
@Override |
||||
public Bitmap getBitmap(String url) { |
||||
return cache.get(url); |
||||
} |
||||
|
||||
@Override |
||||
public void putBitmap(String url, Bitmap bitmap) { |
||||
cache.put(url, bitmap); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
public static synchronized VolleySingleton getInstance(Context context) { |
||||
if (instance == null) { |
||||
instance = new VolleySingleton(context); |
||||
} |
||||
return instance; |
||||
} |
||||
|
||||
private RequestQueue getRequestQueue() { |
||||
if (requestQueue == null) { |
||||
/* getApplicationContext() is key, it keeps you from leaking the |
||||
* Activity or BroadcastReceiver if someone passes one in. */ |
||||
requestQueue= Volley.newRequestQueue(context.get().getApplicationContext()); |
||||
} |
||||
return requestQueue; |
||||
} |
||||
|
||||
<T> void addToRequestQueue(Request<T> request) { |
||||
getRequestQueue().add(request); |
||||
} |
||||
|
||||
void cancelAll(String tag) { |
||||
getRequestQueue().cancelAll(tag); |
||||
} |
||||
|
||||
ImageLoader getImageLoader() { |
||||
return imageLoader; |
||||
} |
||||
} |
@ -0,0 +1,23 @@ |
||||
/* Copyright 2017 Andrew Dawson |
||||
* |
||||
* This file is part of Tusky. |
||||
* |
||||
* Tusky 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. |
||||
* |
||||
* Tusky 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 Tusky. If not, see |
||||
* <http://www.gnu.org/licenses/>. */
|
||||
|
||||
package com.keylesspalace.tusky.entity; |
||||
|
||||
import com.google.gson.annotations.SerializedName; |
||||
|
||||
public class AccessToken { |
||||
@SerializedName("access_token") |
||||
public String accessToken; |
||||
} |
@ -0,0 +1,26 @@ |
||||
/* Copyright 2017 Andrew Dawson |
||||
* |
||||
* This file is part of Tusky. |
||||
* |
||||
* Tusky 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. |
||||
* |
||||
* Tusky 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 Tusky. If not, see |
||||
* <http://www.gnu.org/licenses/>. */
|
||||
|
||||
package com.keylesspalace.tusky.entity; |
||||
|
||||
import com.google.gson.annotations.SerializedName; |
||||
|
||||
public class AppCredentials { |
||||
@SerializedName("client_id") |
||||
public String clientId; |
||||
|
||||
@SerializedName("client_secret") |
||||
public String clientSecret; |
||||
} |
Loading…
Reference in new issue