aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/ml')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java9
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java3
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java11
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java17
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java16
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java108
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java13
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java12
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java267
9 files changed, 389 insertions, 67 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java
index baa2a396..333f36bc 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java
@@ -40,6 +40,15 @@ class AppModule {
.build();
}
+ @Provides @Named("upload_media")
+ @Singleton
+ Retrofit provideUploadMediaRetrofit() {
+ return new Retrofit.Builder()
+ .baseUrl(RedditUtils.API_UPLOAD_MEDIA_URI)
+ .addConverterFactory(ScalarsConverterFactory.create())
+ .build();
+ }
+
@Provides
@Singleton
OkHttpClient provideOkHttpClient(@Named("no_oauth") Retrofit retrofit, @Named("auth_info") SharedPreferences sharedPreferences) {
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java
index 433f22d0..9f2c131f 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java
@@ -71,4 +71,7 @@ public class JSONUtils {
static final String THINGS_KEY = "things";
static final String PARENT_ID_KEY = "parent_id";
static final String ERRORS_KEY = "errors";
+ static final String ARGS_KEY = "args";
+ static final String FIELDS_KEY = "fields";
+ static final String VALUE_KEY = "value";
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java
index 20f615fe..2efa6718 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java
@@ -197,10 +197,10 @@ public class MainActivity extends AppCompatActivity {
loadUserData();
- mName = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.USER_KEY, "");
- mProfileImageUrl = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.PROFILE_IMAGE_URL_KEY, "");
- mBannerImageUrl = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.BANNER_IMAGE_URL_KEY, "");
- mKarma = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.KARMA_KEY, "");
+ mName = mUserInfoSharedPreferences.getString(SharedPreferencesUtils.USER_KEY, "");
+ mProfileImageUrl = mUserInfoSharedPreferences.getString(SharedPreferencesUtils.PROFILE_IMAGE_URL_KEY, "");
+ mBannerImageUrl = mUserInfoSharedPreferences.getString(SharedPreferencesUtils.BANNER_IMAGE_URL_KEY, "");
+ mKarma = mUserInfoSharedPreferences.getString(SharedPreferencesUtils.KARMA_KEY, "");
mNameTextView.setText(mName);
mKarmaTextView.setText(mKarma);
@@ -242,16 +242,19 @@ public class MainActivity extends AppCompatActivity {
startActivity(intent);
dialog.dismiss();
});
+
linkTypeLinearLayout.setOnClickListener(view -> {
Intent intent = new Intent(MainActivity.this, PostLinkActivity.class);
startActivity(intent);
dialog.dismiss();
});
+
imageTypeLinearLayout.setOnClickListener(view -> {
Intent intent = new Intent(MainActivity.this, PostImageActivity.class);
startActivity(intent);
dialog.dismiss();
});
+
videoTypeLinearLayout.setOnClickListener(view -> {
Toast.makeText(this, "Not implemented yet", Toast.LENGTH_SHORT).show();
dialog.dismiss();
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java
index 8cd998b2..ddfce2f5 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java
@@ -27,8 +27,8 @@ class ParsePost {
void onParsePostFail();
}
- static void parsePosts(String response, Locale locale, ParsePostsListingListener parsePostsListingListener) {
- new ParsePostDataAsyncTask(response, locale, parsePostsListingListener).execute();
+ static void parsePosts(String response, Locale locale, int nPosts, ParsePostsListingListener parsePostsListingListener) {
+ new ParsePostDataAsyncTask(response, locale, nPosts, parsePostsListingListener).execute();
}
static void parsePost(String response, Locale locale, ParsePostListener parsePostListener) {
@@ -38,6 +38,7 @@ class ParsePost {
private static class ParsePostDataAsyncTask extends AsyncTask<Void, Void, Void> {
private JSONArray allData;
private Locale locale;
+ private int nPosts;
private ParsePostsListingListener parsePostsListingListener;
private ParsePostListener parsePostListener;
private ArrayList<Post> newPosts;
@@ -45,7 +46,7 @@ class ParsePost {
private String lastItem;
private boolean parseFailed;
- ParsePostDataAsyncTask(String response, Locale locale,
+ ParsePostDataAsyncTask(String response, Locale locale, int nPosts,
ParsePostsListingListener parsePostsListingListener) {
this.parsePostsListingListener = parsePostsListingListener;
try {
@@ -53,6 +54,7 @@ class ParsePost {
allData = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
this.locale = locale;
+ this.nPosts = nPosts;
newPosts = new ArrayList<>();
parseFailed = false;
} catch (JSONException e) {
@@ -99,7 +101,14 @@ class ParsePost {
}
} else {
//Posts listing
- for(int i = 0; i < allData.length(); i++) {
+ int size;
+ if(nPosts < 0 || nPosts > allData.length()) {
+ size = allData.length();
+ } else {
+ size = nPosts;
+ }
+
+ for(int i = 0; i < size; i++) {
String kind = allData.getJSONObject(i).getString(JSONUtils.KIND_KEY);
if(kind.equals("t3")) {
//It's a post
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java
index 88101048..c70f6e5b 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java
@@ -131,7 +131,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) {
- ParsePost.parsePosts(response.body(), locale,
+ ParsePost.parsePosts(response.body(), locale, -1,
new ParsePost.ParsePostsListingListener() {
@Override
public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
@@ -173,7 +173,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if(response.isSuccessful()) {
- ParsePost.parsePosts(response.body(), locale, new ParsePost.ParsePostsListingListener() {
+ ParsePost.parsePosts(response.body(), locale, -1, new ParsePost.ParsePostsListingListener() {
@Override
public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
callback.onResult(newPosts, lastItem);
@@ -207,7 +207,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if(response.isSuccessful()) {
- ParsePost.parsePosts(response.body(), locale,
+ ParsePost.parsePosts(response.body(), locale, -1,
new ParsePost.ParsePostsListingListener() {
@Override
public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
@@ -248,7 +248,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if(response.isSuccessful()) {
- ParsePost.parsePosts(response.body(), locale, new ParsePost.ParsePostsListingListener() {
+ ParsePost.parsePosts(response.body(), locale, -1, new ParsePost.ParsePostsListingListener() {
@Override
public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
callback.onResult(newPosts, lastItem);
@@ -282,7 +282,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if(response.isSuccessful()) {
- ParsePost.parsePosts(response.body(), locale,
+ ParsePost.parsePosts(response.body(), locale, -1,
new ParsePost.ParsePostsListingListener() {
@Override
public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
@@ -328,7 +328,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if(response.isSuccessful()) {
- ParsePost.parsePosts(response.body(), locale, new ParsePost.ParsePostsListingListener() {
+ ParsePost.parsePosts(response.body(), locale, -1, new ParsePost.ParsePostsListingListener() {
@Override
public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
if(newPosts.size() == 0 && !lastItem.equals("null")) {
@@ -366,7 +366,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if(response.isSuccessful()) {
- ParsePost.parsePosts(response.body(), locale,
+ ParsePost.parsePosts(response.body(), locale, -1,
new ParsePost.ParsePostsListingListener() {
@Override
public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
@@ -407,7 +407,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if(response.isSuccessful()) {
- ParsePost.parsePosts(response.body(), locale, new ParsePost.ParsePostsListingListener() {
+ ParsePost.parsePosts(response.body(), locale, -1, new ParsePost.ParsePostsListingListener() {
@Override
public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
callback.onResult(newPosts, lastItem);
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java
index a42724b7..5a7a3a9f 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java
@@ -2,6 +2,7 @@ package ml.docilealligator.infinityforreddit;
import android.content.Intent;
import android.content.SharedPreferences;
+import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
@@ -23,9 +24,12 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.RequestOptions;
+import com.bumptech.glide.request.target.CustomTarget;
+import com.bumptech.glide.request.transition.Transition;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
+import java.util.ArrayList;
import java.util.Locale;
import javax.inject.Inject;
@@ -35,6 +39,8 @@ import butterknife.BindView;
import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import pl.droidsonroids.gif.GifImageView;
+import retrofit2.Call;
+import retrofit2.Callback;
import retrofit2.Retrofit;
public class PostImageActivity extends AppCompatActivity {
@@ -76,6 +82,14 @@ public class PostImageActivity extends AppCompatActivity {
Retrofit mOauthRetrofit;
@Inject
+ @Named("upload_media")
+ Retrofit mUploadMediaRetrofit;
+
+ @Inject
+ @Named("user_info")
+ SharedPreferences mUserInfoSharedPreferences;
+
+ @Inject
@Named("auth_info")
SharedPreferences sharedPreferences;
@@ -149,11 +163,12 @@ public class PostImageActivity extends AppCompatActivity {
selectFromLibraryFab.setOnClickListener(view -> {
Intent intent = new Intent();
intent.setType("image/*");
- intent.setAction(Intent.ACTION_GET_CONTENT);
+ intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
startActivityForResult(Intent.createChooser(intent,getResources().getString(R.string.select_from_gallery)), PICK_IMAGE_REQUEST_CODE);
});
selectAgainTextView.setOnClickListener(view -> {
+ imageUri = null;
selectAgainTextView.setVisibility(View.GONE);
mGlide.clear(imageView);
constraintLayout.setVisibility(View.VISIBLE);
@@ -185,6 +200,11 @@ public class PostImageActivity extends AppCompatActivity {
return true;
}
+ if(imageUri == null) {
+ Snackbar.make(coordinatorLayout, R.string.select_an_image, Snackbar.LENGTH_SHORT).show();
+ return true;
+ }
+
item.setEnabled(false);
item.getIcon().setAlpha(130);
Snackbar postingSnackbar = Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_INDEFINITE);
@@ -197,35 +217,85 @@ public class PostImageActivity extends AppCompatActivity {
subredditName = subreditNameTextView.getText().toString();
}
- /*SubmitPost.submitTextOrLinkPost(mOauthRetrofit, sharedPreferences, mLocale, subredditName,
- titleEditText.getText().toString(), contentEditText.getText().toString(),
- false, RedditUtils.KIND_LINK, new SubmitPost.SubmitPostListener() {
+ Glide.with(this)
+ .asBitmap()
+ .load(imageUri)
+ .into(new CustomTarget<Bitmap>() {
+
@Override
- public void submitSuccessful(Post post) {
- Intent intent = new Intent(PostLinkActivity.this, ViewPostDetailActivity.class);
- intent.putExtra(ViewPostDetailActivity.EXTRA_POST_DATA, post);
- startActivity(intent);
- finish();
+ public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
+ SubmitPost.submitImagePost(mOauthRetrofit, mUploadMediaRetrofit, sharedPreferences,
+ mLocale, subredditName, titleEditText.getText().toString(), resource,
+ false, new SubmitPost.SubmitPostListener() {
+ @Override
+ public void submitSuccessful(Post post) {
+ RedditAPI api = mOauthRetrofit.create(RedditAPI.class);
+ Call<String> getPost = api.getUserBestPosts(mUserInfoSharedPreferences.getString(SharedPreferencesUtils.USER_KEY, ""), null,
+ RedditUtils.getOAuthHeader(sharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "")));
+ getPost.enqueue(new Callback<String>() {
+ @Override
+ public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
+ if(response.isSuccessful()) {
+ ParsePost.parsePosts(response.body(), mLocale, 1,
+ new ParsePost.ParsePostsListingListener() {
+ @Override
+ public void onParsePostsListingSuccess(ArrayList<Post> newPostData, String lastItem) {
+ Intent intent = new Intent(PostImageActivity.this, ViewPostDetailActivity.class);
+ intent.putExtra(ViewPostDetailActivity.EXTRA_POST_DATA, newPostData.get(0));
+ startActivity(intent);
+ finish();
+ }
+
+ @Override
+ public void onParsePostsListingFail() {
+ startViewUserDetailActivity();
+ }
+ });
+ } else {
+ startViewUserDetailActivity();
+ }
+ }
+
+ @Override
+ public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
+ startViewUserDetailActivity();
+ }
+ });
+ }
+
+ @Override
+ public void submitFailed(@Nullable String errorMessage) {
+ postingSnackbar.dismiss();
+ item.setEnabled(true);
+ item.getIcon().setAlpha(255);
+ if(errorMessage == null) {
+ Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show();
+ } else {
+ Snackbar.make(coordinatorLayout, errorMessage, Snackbar.LENGTH_SHORT).show();
+ }
+ }
+ });
}
@Override
- public void submitFailed(@Nullable String errorMessage) {
- postingSnackbar.dismiss();
- item.setEnabled(true);
- item.getIcon().setAlpha(255);
- if(errorMessage == null) {
- Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show();
- } else {
- Snackbar.make(coordinatorLayout, errorMessage, Snackbar.LENGTH_SHORT).show();
- }
+ public void onLoadCleared(@Nullable Drawable placeholder) {
+
}
- });*/
+ });
return true;
}
return false;
}
+ private void startViewUserDetailActivity() {
+ Intent intent = new Intent(PostImageActivity.this, ViewUserDetailActivity.class);
+ intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY,
+ mUserInfoSharedPreferences.getString(SharedPreferencesUtils.USER_KEY, ""));
+ startActivity(intent);
+ finish();
+ }
+
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java
index e1f044c4..4421b595 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java
@@ -2,12 +2,17 @@ package ml.docilealligator.infinityforreddit;
import java.util.Map;
+import okhttp3.MultipartBody;
+import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.HeaderMap;
+import retrofit2.http.Multipart;
import retrofit2.http.POST;
+import retrofit2.http.Part;
+import retrofit2.http.PartMap;
import retrofit2.http.Path;
import retrofit2.http.Query;
@@ -82,4 +87,12 @@ public interface RedditAPI {
@FormUrlEncoded
@POST("/api/submit")
Call<String> submit(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params);
+
+ @FormUrlEncoded
+ @POST("/api/media/asset.json?raw_json=1&gilding_detail=1")
+ Call<String> uploadImage(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params);
+
+ @Multipart
+ @POST(".")
+ Call<String> uploadMediaToAWS(@PartMap()Map<String, RequestBody> params, @Part() MultipartBody.Part file);
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java
index b3b989f0..71e7629b 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java
@@ -5,6 +5,9 @@ import android.util.Base64;
import java.util.HashMap;
import java.util.Map;
+import okhttp3.MediaType;
+import okhttp3.RequestBody;
+
/**
* Created by alex on 2/23/18.
*/
@@ -13,6 +16,7 @@ public class RedditUtils {
static final String OAUTH_URL ="https://www.reddit.com/api/v1/authorize.compact";
static final String OAUTH_API_BASE_URI = "https://oauth.reddit.com";
static final String API_BASE_URI = "https://www.reddit.com";
+ static final String API_UPLOAD_MEDIA_URI = "https://reddit-uploaded-media.s3-accelerate.amazonaws.com";
static final String CLIENT_ID_KEY = "client_id";
static final String CLIENT_ID = "";
@@ -62,6 +66,10 @@ public class RedditUtils {
static final String KIND_TEXT = "text";
static final String KIND_SELF = "self";
static final String KIND_LINK = "link";
+ static final String KIND_IMAGE = "image";
+
+ static final String FILEPATH_KEY = "filepath";
+ static final String MIMETYPE_KEY = "mimetype";
static Map<String, String> getHttpBasicAuthHeader() {
Map<String, String> params = new HashMap<>();
@@ -77,4 +85,8 @@ public class RedditUtils {
params.put(RedditUtils.USER_AGENT_KEY, RedditUtils.USER_AGENT);
return params;
}
+
+ static RequestBody getRequestBody(String s) {
+ return RequestBody.create(MediaType.parse("text/plain"), s);
+ }
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java
index 4a03398d..ae99804f 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java
@@ -1,6 +1,8 @@
package ml.docilealligator.infinityforreddit;
import android.content.SharedPreferences;
+import android.graphics.Bitmap;
+import android.os.AsyncTask;
import android.util.Log;
import androidx.annotation.NonNull;
@@ -9,13 +11,23 @@ import androidx.annotation.Nullable;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlPullParserException;
+import org.xmlpull.v1.XmlPullParserFactory;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.StringReader;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
+import okhttp3.MediaType;
+import okhttp3.MultipartBody;
+import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
+import retrofit2.Response;
import retrofit2.Retrofit;
class SubmitPost {
@@ -37,7 +49,7 @@ class SubmitPost {
params.put(RedditUtils.KIND_KEY, kind);
if(kind.equals(RedditUtils.KIND_SELF)) {
params.put(RedditUtils.TEXT_KEY, content);
- } else if(kind.equals(RedditUtils.KIND_LINK)) {
+ } else if(kind.equals(RedditUtils.KIND_LINK) || kind.equals(RedditUtils.KIND_IMAGE)) {
params.put(RedditUtils.URL_KEY, content);
}
params.put(RedditUtils.NSFW_KEY, Boolean.toString(isNSFW));
@@ -49,8 +61,8 @@ class SubmitPost {
Log.i("code", "asfd" + response.body());
if(response.isSuccessful()) {
try {
- getSubmittedPost(response.body(), oauthRetrofit, authInfoSharedPreferences, locale,
- submitPostListener);
+ getSubmittedPost(response.body(), kind, oauthRetrofit, authInfoSharedPreferences,
+ locale, submitPostListener);
} catch (JSONException e) {
e.printStackTrace();
submitPostListener.submitFailed(null);
@@ -69,7 +81,194 @@ class SubmitPost {
});
}
- private static void getSubmittedPost(String response, Retrofit oauthRetrofit,
+ static void submitImagePost(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
+ SharedPreferences authInfoSharedPreferences, Locale locale,
+ String subredditName, String title, Bitmap image, boolean isNSFW,
+ SubmitPostListener submitPostListener) {
+ RedditAPI api = oauthRetrofit.create(RedditAPI.class);
+ String accessToken = authInfoSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
+
+ Map<String, String> uploadImageParams = new HashMap<>();
+ uploadImageParams.put(RedditUtils.FILEPATH_KEY, "tetestst.jpg");
+ uploadImageParams.put(RedditUtils.MIMETYPE_KEY, "image/jpeg");
+
+ Log.i("map", RedditUtils.getOAuthHeader(accessToken).toString());
+ Call<String> uploadImageCall = api.uploadImage(RedditUtils.getOAuthHeader(accessToken), uploadImageParams);
+ uploadImageCall.enqueue(new Callback<String>() {
+ @Override
+ public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
+ if(response.isSuccessful()) {
+ new ParseJSONResponseFromAWSAsyncTask(response.body(), new ParseJSONResponseFromAWSAsyncTask.ParseJSONResponseFromAWSListener() {
+ @Override
+ public void parseSuccessful(Map<String, RequestBody> nameValuePairsMap) {
+
+ ByteArrayOutputStream stream = new ByteArrayOutputStream();
+ image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
+ byte[] byteArray = stream.toByteArray();
+
+ RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), byteArray);
+ MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", "testing.jpg", fileBody);
+
+ RedditAPI uploadMediaToAWSApi = uploadMediaRetrofit.create(RedditAPI.class);
+ Call<String> uploadMediaToAWS = uploadMediaToAWSApi.uploadMediaToAWS(nameValuePairsMap, fileToUpload);
+
+ uploadMediaToAWS.enqueue(new Callback<String>() {
+ @Override
+ public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
+ Log.i("responsesese", "aws" + response.body());
+ if(response.isSuccessful()) {
+ new ParseXMLReponseFromAWSAsyncTask(response.body(), new ParseXMLReponseFromAWSAsyncTask.ParseXMLResponseFromAWSListener() {
+ @Override
+ public void parseSuccessful(String imageUrl) {
+ submitTextOrLinkPost(oauthRetrofit, authInfoSharedPreferences, locale,
+ subredditName, title, imageUrl, isNSFW, RedditUtils.KIND_IMAGE,
+ submitPostListener);
+ }
+
+ @Override
+ public void parseFailed() {
+ submitPostListener.submitFailed(null);
+ }
+ }).execute();
+ } else {
+ Log.i("asfasdfsd", "failedddddddddd" + response.code());
+ submitPostListener.submitFailed("Error: " + response.code());
+ }
+ }
+
+ @Override
+ public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
+ Log.i("asfasdfsd", "failedddddddddd" + t.getMessage());
+ submitPostListener.submitFailed(t.getMessage());
+ }
+ });
+ }
+
+ @Override
+ public void parseFailed() {
+ submitPostListener.submitFailed("Parse from aws failed");
+ }
+ }).execute();
+ } else {
+ submitPostListener.submitFailed(response.message());
+ }
+ Log.i("image", "dddd" + response.body());
+ }
+
+ @Override
+ public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
+ submitPostListener.submitFailed(t.getMessage());
+ }
+ });
+ }
+
+ private static class ParseJSONResponseFromAWSAsyncTask extends AsyncTask<Void, Void, Void> {
+ interface ParseJSONResponseFromAWSListener {
+ void parseSuccessful(Map<String, RequestBody> nameValuePairsMap);
+ void parseFailed();
+ }
+
+ private String response;
+ private ParseJSONResponseFromAWSListener parseJSONResponseFromAWSListener;
+ private Map<String, RequestBody> nameValuePairsMap;
+ private boolean successful;
+
+ ParseJSONResponseFromAWSAsyncTask(String response, ParseJSONResponseFromAWSListener parseJSONResponseFromAWSListener) {
+ this.response = response;
+ this.parseJSONResponseFromAWSListener = parseJSONResponseFromAWSListener;
+ nameValuePairsMap = new HashMap<>();
+ successful = false;
+ }
+
+ @Override
+ protected Void doInBackground(Void... voids) {
+ try {
+ JSONObject responseObject = new JSONObject(response);
+ JSONArray nameValuePairs = responseObject.getJSONObject(JSONUtils.ARGS_KEY).getJSONArray(JSONUtils.FIELDS_KEY);
+
+ nameValuePairsMap = new HashMap<>();
+ for(int i = 0; i < nameValuePairs.length(); i++) {
+ nameValuePairsMap.put(nameValuePairs.getJSONObject(i).getString(JSONUtils.NAME_KEY),
+ RedditUtils.getRequestBody(nameValuePairs.getJSONObject(i).getString(JSONUtils.VALUE_KEY)));
+ }
+
+ successful = true;
+ } catch (JSONException e) {
+ e.printStackTrace();
+ successful = false;
+ }
+
+ return null;
+ }
+
+ @Override
+ protected void onPostExecute(Void aVoid) {
+ if(successful) {
+ parseJSONResponseFromAWSListener.parseSuccessful(nameValuePairsMap);
+ } else {
+ parseJSONResponseFromAWSListener.parseFailed();
+ }
+ }
+ }
+
+ private static class ParseXMLReponseFromAWSAsyncTask extends AsyncTask<Void, Void, Void> {
+ interface ParseXMLResponseFromAWSListener {
+ void parseSuccessful(String imageUrl);
+ void parseFailed();
+ }
+
+ private String response;
+ private ParseXMLResponseFromAWSListener parseXMLResponseFromAWSListener;
+ private String imageUrl;
+ private boolean successful;
+
+ ParseXMLReponseFromAWSAsyncTask(String response, ParseXMLResponseFromAWSListener parseXMLResponseFromAWSListener) {
+ this.response = response;
+ this.parseXMLResponseFromAWSListener = parseXMLResponseFromAWSListener;
+ successful = false;
+ }
+
+ @Override
+ protected Void doInBackground(Void... voids) {
+ try {
+ XmlPullParser xmlPullParser = XmlPullParserFactory.newInstance().newPullParser();
+ xmlPullParser.setInput(new StringReader(response));
+
+ boolean isLocationTag = false;
+ int eventType = xmlPullParser.getEventType();
+ while(eventType != XmlPullParser.END_DOCUMENT) {
+ if(eventType == XmlPullParser.START_TAG) {
+ if(xmlPullParser.getName().equals("Location")) {
+ isLocationTag = true;
+ }
+ } else if(eventType == XmlPullParser.TEXT) {
+ if(isLocationTag) {
+ imageUrl = xmlPullParser.getText();
+ successful = true;
+ return null;
+ }
+ }
+ eventType = xmlPullParser.next();
+ }
+ } catch (XmlPullParserException | IOException e) {
+ e.printStackTrace();
+ successful = false;
+ }
+
+ return null;
+ }
+
+ @Override
+ protected void onPostExecute(Void aVoid) {
+ if(successful) {
+ parseXMLResponseFromAWSListener.parseSuccessful(imageUrl);
+ } else {
+ parseXMLResponseFromAWSListener.parseFailed();
+ }
+ }
+ }
+
+ private static void getSubmittedPost(String response, String kind, Retrofit oauthRetrofit,
SharedPreferences authInfoSharedPreferences, Locale locale,
SubmitPostListener submitPostListener) throws JSONException {
JSONObject responseObject = new JSONObject(response).getJSONObject(JSONUtils.JSON_KEY);
@@ -94,38 +293,42 @@ class SubmitPost {
return;
}
- String postId = responseObject.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.ID_KEY);
+ if(!kind.equals(RedditUtils.KIND_IMAGE)) {
+ String postId = responseObject.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.ID_KEY);
- RedditAPI api = oauthRetrofit.create(RedditAPI.class);
- String accessToken = authInfoSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
+ RedditAPI api = oauthRetrofit.create(RedditAPI.class);
+ String accessToken = authInfoSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
- Call<String> getPostCall = api.getPost(postId, RedditUtils.getOAuthHeader(accessToken));
- getPostCall.enqueue(new Callback<String>() {
- @Override
- public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
- if(response.isSuccessful()) {
- ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() {
- @Override
- public void onParsePostSuccess(Post post) {
- submitPostListener.submitSuccessful(post);
- }
+ Call<String> getPostCall = api.getPost(postId, RedditUtils.getOAuthHeader(accessToken));
+ getPostCall.enqueue(new Callback<String>() {
+ @Override
+ public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
+ if(response.isSuccessful()) {
+ ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() {
+ @Override
+ public void onParsePostSuccess(Post post) {
+ submitPostListener.submitSuccessful(post);
+ }
- @Override
- public void onParsePostFail() {
- submitPostListener.submitFailed(null);
- }
- });
- } else {
- Log.i("call_failed", response.message());
- submitPostListener.submitFailed(response.message());
+ @Override
+ public void onParsePostFail() {
+ submitPostListener.submitFailed(null);
+ }
+ });
+ } else {
+ Log.i("call_failed", response.message());
+ submitPostListener.submitFailed(response.message());
+ }
}
- }
- @Override
- public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
- Log.i("call_failed", call.request().url().toString());
- submitPostListener.submitFailed(t.getMessage());
- }
- });
+ @Override
+ public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
+ Log.i("call_failed", call.request().url().toString());
+ submitPostListener.submitFailed(t.getMessage());
+ }
+ });
+ } else {
+ submitPostListener.submitSuccessful(null);
+ }
}
}