From 77d83654aa8063b0c4d9f95ba91bd3d77be84ad9 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 6 Aug 2019 21:00:16 +0800 Subject: Bugs fixed related to submitting link and text posts. --- app/src/main/AndroidManifest.xml | 2 +- .../infinityforreddit/AppComponent.java | 2 +- .../infinityforreddit/PostImageActivity.java | 14 +- .../infinityforreddit/PostLinkActivity.java | 27 ++- .../infinityforreddit/PostMediaService.java | 234 --------------------- .../infinityforreddit/PostTextActivity.java | 25 ++- .../infinityforreddit/PostVideoActivity.java | 14 +- .../infinityforreddit/SubmitPostService.java | 234 +++++++++++++++++++++ app/src/main/res/layout/activity_post_link.xml | 2 +- 9 files changed, 281 insertions(+), 273 deletions(-) delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/PostMediaService.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPostService.java (limited to 'app/src/main') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bebae40b..67fd13cc 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -22,7 +22,7 @@ android:theme="@style/AppTheme" android:usesCleartextTraffic="true"> diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 6dca6581..3f93717a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -25,5 +25,5 @@ interface AppComponent { void inject(FlairBottomSheetFragment flairBottomSheetFragment); void inject(RulesActivity rulesActivity); void inject(CommentsListingFragment commentsListingFragment); - void inject(PostMediaService postMediaService); + void inject(SubmitPostService submitPostService); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java index 5dd8934f..74e19f3a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java @@ -349,14 +349,14 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS subredditName = subreditNameTextView.getText().toString(); } - Intent intent = new Intent(this, PostMediaService.class); + Intent intent = new Intent(this, SubmitPostService.class); intent.setData(imageUri); - intent.putExtra(PostMediaService.EXTRA_SUBREDDIT_NAME, subredditName); - intent.putExtra(PostMediaService.EXTRA_TITLE, titleEditText.getText().toString()); - intent.putExtra(PostMediaService.EXTRA_FLAIR, flair); - intent.putExtra(PostMediaService.EXTRA_IS_SPOILER, isSpoiler); - intent.putExtra(PostMediaService.EXTRA_IS_NSFW, isNSFW); - intent.putExtra(PostMediaService.EXTRA_POST_TYPE, PostMediaService.EXTRA_POST_TYPE_IMAGE); + intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); + intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString()); + intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair); + intent.putExtra(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler); + intent.putExtra(SubmitPostService.EXTRA_IS_NSFW, isNSFW); + intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TYPE_IMAGE); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(intent); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java index bbbd5540..ed537d78 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java @@ -97,6 +97,8 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh ButterKnife.bind(this); + EventBus.getDefault().register(this); + ((Infinity) getApplication()).getmAppComponent().inject(this); setSupportActionBar(toolbar); @@ -241,8 +243,8 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh getMenuInflater().inflate(R.menu.post_link_activity, menu); mMemu = menu; if(isPosting) { - mMemu.findItem(R.id.action_send_post_image_activity).setEnabled(false); - mMemu.findItem(R.id.action_send_post_image_activity).getIcon().setAlpha(130); + mMemu.findItem(R.id.action_send_post_link_activity).setEnabled(false); + mMemu.findItem(R.id.action_send_post_link_activity).getIcon().setAlpha(130); } return true; } @@ -259,6 +261,8 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh return true; } + isPosting = true; + item.setEnabled(false); item.getIcon().setAlpha(130); Snackbar postingSnackbar = Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_INDEFINITE); @@ -271,15 +275,16 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh subredditName = subredditNameTextView.getText().toString(); } - Intent intent = new Intent(this, PostMediaService.class); - intent.putExtra(PostMediaService.EXTRA_SUBREDDIT_NAME, subredditName); - intent.putExtra(PostMediaService.EXTRA_TITLE, titleEditText.getText().toString()); - intent.putExtra(PostMediaService.EXTRA_CONTENT, contentEditText.getText().toString()); - intent.putExtra(PostMediaService.EXTRA_KIND, RedditUtils.KIND_LINK); - intent.putExtra(PostMediaService.EXTRA_FLAIR, flair); - intent.putExtra(PostMediaService.EXTRA_IS_SPOILER, isSpoiler); - intent.putExtra(PostMediaService.EXTRA_IS_NSFW, isNSFW); - intent.putExtra(PostMediaService.EXTRA_POST_TYPE, PostMediaService.EXTRA_POST_TYPE_IMAGE); + Intent intent = new Intent(this, SubmitPostService.class); + intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); + intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString()); + intent.putExtra(SubmitPostService.EXTRA_CONTENT, contentEditText.getText().toString()); + intent.putExtra(SubmitPostService.EXTRA_KIND, RedditUtils.KIND_LINK); + intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair); + intent.putExtra(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler); + intent.putExtra(SubmitPostService.EXTRA_IS_NSFW, isNSFW); + intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TEXT_OR_LINK); + startService(intent); return true; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostMediaService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostMediaService.java deleted file mode 100644 index 21f12cd4..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostMediaService.java +++ /dev/null @@ -1,234 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.app.Notification; -import android.app.NotificationChannel; -import android.app.NotificationManager; -import android.app.Service; -import android.content.Intent; -import android.content.SharedPreferences; -import android.graphics.Bitmap; -import android.graphics.drawable.Drawable; -import android.net.Uri; -import android.os.Build; -import android.os.IBinder; -import android.os.ParcelFileDescriptor; -import android.widget.Toast; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.core.app.NotificationCompat; - -import com.bumptech.glide.Glide; -import com.bumptech.glide.request.target.CustomTarget; -import com.bumptech.glide.request.transition.Transition; - -import org.greenrobot.eventbus.EventBus; - -import java.io.FileInputStream; -import java.io.IOException; - -import javax.inject.Inject; -import javax.inject.Named; - -import retrofit2.Retrofit; - -public class PostMediaService extends Service { - static final String EXTRA_SUBREDDIT_NAME = "ESN"; - static final String EXTRA_TITLE = "ET"; - static final String EXTRA_CONTENT = "EC"; - static final String EXTRA_KIND = "EK"; - static final String EXTRA_FLAIR = "EF"; - static final String EXTRA_IS_SPOILER = "EIS"; - static final String EXTRA_IS_NSFW = "EIN"; - static final String EXTRA_POST_TYPE = "EPT"; - static final int EXTRA_POST_TEXT_OR_LINK = 0; - static final int EXTRA_POST_TYPE_IMAGE = 1; - static final int EXTRA_POST_TYPE_VIDEO = 2; - - @Inject - @Named("oauth") - Retrofit mOauthRetrofit; - - @Inject - @Named("upload_media") - Retrofit mUploadMediaRetrofit; - - @Inject - @Named("upload_video") - Retrofit mUploadVideoRetrofit; - - @Inject - @Named("user_info") - SharedPreferences mUserInfoSharedPreferences; - - @Inject - @Named("auth_info") - SharedPreferences sharedPreferences; - - public PostMediaService() { - } - - @Override - public IBinder onBind(Intent intent) { - return null; - } - - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - ((Infinity) getApplication()).getmAppComponent().inject(this); - - String subredditName = intent.getExtras().getString(EXTRA_SUBREDDIT_NAME); - String title = intent.getExtras().getString(EXTRA_TITLE); - String flair = intent.getExtras().getString(EXTRA_FLAIR); - boolean isSpoiler = intent.getExtras().getBoolean(EXTRA_IS_SPOILER); - boolean isNSFW = intent.getExtras().getBoolean(EXTRA_IS_NSFW); - int postType = intent.getExtras().getInt(EXTRA_POST_TYPE); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel serviceChannel = new NotificationChannel( - NotificationUtils.CHANNEL_POST_MEDIA, - NotificationUtils.CHANNEL_POST_MEDIA, - NotificationManager.IMPORTANCE_LOW - ); - - NotificationManager manager = getSystemService(NotificationManager.class); - manager.createNotificationChannel(serviceChannel); - } - - if(postType == EXTRA_POST_TEXT_OR_LINK) { - String content = intent.getExtras().getString(EXTRA_CONTENT); - String kind = intent.getExtras().getString(EXTRA_KIND); - startForeground(1, createNotification(R.string.posting)); - submitTextOrLinkPost(subredditName, title, content, flair, isSpoiler, isNSFW, kind); - } else if(postType == EXTRA_POST_TYPE_IMAGE) { - Uri imageUri = intent.getData(); - startForeground(1, createNotification(R.string.posting_image)); - submitImagePost(imageUri, subredditName, title, flair, isSpoiler, isNSFW); - } else { - Uri videoUri = intent.getData(); - startForeground(1, createNotification(R.string.posting_video)); - submitVideoPost(videoUri, subredditName, title, flair, isSpoiler, isNSFW); - } - - return START_NOT_STICKY; - } - - private Notification createNotification(int stringResId) { - return new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_POST_MEDIA) - .setContentTitle(getString(stringResId)) - .setContentText(getString(R.string.please_wait)) - .setSmallIcon(R.mipmap.ic_launcher_round) - .build(); - } - - private void submitTextOrLinkPost(String subredditName, String title, String content, String flair, - boolean isSpoiler, boolean isNSFW, String kind) { - SubmitPost.submitTextOrLinkPost(mOauthRetrofit, sharedPreferences, getResources().getConfiguration().locale, - subredditName, title, content, flair, isSpoiler, isNSFW, kind, new SubmitPost.SubmitPostListener() { - @Override - public void submitSuccessful(Post post) { - EventBus.getDefault().post(new SubmitTextOrLinkPostEvent(true, post, null)); - - stopForeground(true); - stopSelf(); - } - - @Override - public void submitFailed(@Nullable String errorMessage) { - EventBus.getDefault().post(new SubmitTextOrLinkPostEvent(false, null, errorMessage)); - - stopForeground(true); - stopSelf(); - } - }); - } - - private void submitImagePost(Uri imageUri, String subredditName, String title, String flair, - boolean isSpoiler, boolean isNSFW) { - Glide.with(this) - .asBitmap() - .load(imageUri) - .into(new CustomTarget() { - - @Override - public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { - SubmitPost.submitImagePost(mOauthRetrofit, mUploadMediaRetrofit, sharedPreferences, - getResources().getConfiguration().locale, subredditName, title, resource, - flair, isSpoiler, isNSFW, new SubmitPost.SubmitPostListener() { - @Override - public void submitSuccessful(Post post) { - EventBus.getDefault().post(new SubmitImagePostEvent(true, null)); - Toast.makeText(PostMediaService.this, R.string.image_is_processing, Toast.LENGTH_SHORT).show(); - - stopForeground(true); - stopSelf(); - } - - @Override - public void submitFailed(@Nullable String errorMessage) { - EventBus.getDefault().post(new SubmitImagePostEvent(false, errorMessage)); - - stopForeground(true); - stopSelf(); - } - }); - } - - @Override - public void onLoadCleared(@Nullable Drawable placeholder) { - - } - }); - } - - private void submitVideoPost(Uri videoUri, String subredditName, String title, String flair, - boolean isSpoiler, boolean isNSFW) { - try (ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(videoUri, "r")) { - FileInputStream in = new FileInputStream(pfd.getFileDescriptor()); - byte[] buffer; - buffer = new byte[in.available()]; - while (in.read(buffer) != -1); - - Glide.with(this) - .asBitmap() - .load(videoUri) - .into(new CustomTarget() { - @Override - public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { - SubmitPost.submitVideoPost(mOauthRetrofit, mUploadMediaRetrofit, mUploadVideoRetrofit, - sharedPreferences, getResources().getConfiguration().locale, subredditName, title, - buffer, getContentResolver().getType(videoUri), resource, flair, isSpoiler, isNSFW, - new SubmitPost.SubmitPostListener() { - @Override - public void submitSuccessful(Post post) { - EventBus.getDefault().post(new SubmitVideoPostEvent(true, false, null)); - Toast.makeText(PostMediaService.this, R.string.video_is_processing, Toast.LENGTH_SHORT).show(); - - stopForeground(true); - stopSelf(); - } - - @Override - public void submitFailed(@Nullable String errorMessage) { - EventBus.getDefault().post(new SubmitVideoPostEvent(false, false, errorMessage)); - - stopForeground(true); - stopSelf(); - } - }); - } - - @Override - public void onLoadCleared(@Nullable Drawable placeholder) { - - } - }); - } catch (IOException e) { - e.printStackTrace(); - EventBus.getDefault().post(new SubmitVideoPostEvent(false, true, null)); - - stopForeground(true); - stopSelf(); - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java index fd485e4b..d96dddc6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java @@ -248,8 +248,8 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh getMenuInflater().inflate(R.menu.post_text_activity, menu); mMemu = menu; if(isPosting) { - mMemu.findItem(R.id.action_send_post_image_activity).setEnabled(false); - mMemu.findItem(R.id.action_send_post_image_activity).getIcon().setAlpha(130); + mMemu.findItem(R.id.action_send_post_text_activity).setEnabled(false); + mMemu.findItem(R.id.action_send_post_text_activity).getIcon().setAlpha(130); } return true; } @@ -266,6 +266,8 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh return true; } + isPosting = true; + item.setEnabled(false); item.getIcon().setAlpha(130); mPostingSnackbar = Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_INDEFINITE); @@ -278,15 +280,16 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh subredditName = subreditNameTextView.getText().toString(); } - Intent intent = new Intent(this, PostMediaService.class); - intent.putExtra(PostMediaService.EXTRA_SUBREDDIT_NAME, subredditName); - intent.putExtra(PostMediaService.EXTRA_TITLE, titleEditText.getText().toString()); - intent.putExtra(PostMediaService.EXTRA_CONTENT, contentEditText.getText().toString()); - intent.putExtra(PostMediaService.EXTRA_KIND, RedditUtils.KIND_SELF); - intent.putExtra(PostMediaService.EXTRA_FLAIR, flair); - intent.putExtra(PostMediaService.EXTRA_IS_SPOILER, isSpoiler); - intent.putExtra(PostMediaService.EXTRA_IS_NSFW, isNSFW); - intent.putExtra(PostMediaService.EXTRA_POST_TYPE, PostMediaService.EXTRA_POST_TYPE_IMAGE); + Intent intent = new Intent(this, SubmitPostService.class); + intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); + intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString()); + intent.putExtra(SubmitPostService.EXTRA_CONTENT, contentEditText.getText().toString()); + intent.putExtra(SubmitPostService.EXTRA_KIND, RedditUtils.KIND_SELF); + intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair); + intent.putExtra(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler); + intent.putExtra(SubmitPostService.EXTRA_IS_NSFW, isNSFW); + intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TEXT_OR_LINK); + startService(intent); return true; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java index 9f3f14fb..6df6134a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java @@ -345,14 +345,14 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS subredditName = subreditNameTextView.getText().toString(); } - Intent intent = new Intent(this, PostMediaService.class); + Intent intent = new Intent(this, SubmitPostService.class); intent.setData(videoUri); - intent.putExtra(PostMediaService.EXTRA_SUBREDDIT_NAME, subredditName); - intent.putExtra(PostMediaService.EXTRA_TITLE, titleEditText.getText().toString()); - intent.putExtra(PostMediaService.EXTRA_FLAIR, flair); - intent.putExtra(PostMediaService.EXTRA_IS_SPOILER, isSpoiler); - intent.putExtra(PostMediaService.EXTRA_IS_NSFW, isNSFW); - intent.putExtra(PostMediaService.EXTRA_POST_TYPE, PostMediaService.EXTRA_POST_TYPE_VIDEO); + intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); + intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString()); + intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair); + intent.putExtra(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler); + intent.putExtra(SubmitPostService.EXTRA_IS_NSFW, isNSFW); + intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TYPE_VIDEO); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(intent); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPostService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPostService.java new file mode 100644 index 00000000..b78f4ab2 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPostService.java @@ -0,0 +1,234 @@ +package ml.docilealligator.infinityforreddit; + +import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.app.Service; +import android.content.Intent; +import android.content.SharedPreferences; +import android.graphics.Bitmap; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.os.Build; +import android.os.IBinder; +import android.os.ParcelFileDescriptor; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.app.NotificationCompat; + +import com.bumptech.glide.Glide; +import com.bumptech.glide.request.target.CustomTarget; +import com.bumptech.glide.request.transition.Transition; + +import org.greenrobot.eventbus.EventBus; + +import java.io.FileInputStream; +import java.io.IOException; + +import javax.inject.Inject; +import javax.inject.Named; + +import retrofit2.Retrofit; + +public class SubmitPostService extends Service { + static final String EXTRA_SUBREDDIT_NAME = "ESN"; + static final String EXTRA_TITLE = "ET"; + static final String EXTRA_CONTENT = "EC"; + static final String EXTRA_KIND = "EK"; + static final String EXTRA_FLAIR = "EF"; + static final String EXTRA_IS_SPOILER = "EIS"; + static final String EXTRA_IS_NSFW = "EIN"; + static final String EXTRA_POST_TYPE = "EPT"; + static final int EXTRA_POST_TEXT_OR_LINK = 0; + static final int EXTRA_POST_TYPE_IMAGE = 1; + static final int EXTRA_POST_TYPE_VIDEO = 2; + + @Inject + @Named("oauth") + Retrofit mOauthRetrofit; + + @Inject + @Named("upload_media") + Retrofit mUploadMediaRetrofit; + + @Inject + @Named("upload_video") + Retrofit mUploadVideoRetrofit; + + @Inject + @Named("user_info") + SharedPreferences mUserInfoSharedPreferences; + + @Inject + @Named("auth_info") + SharedPreferences sharedPreferences; + + public SubmitPostService() { + } + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + ((Infinity) getApplication()).getmAppComponent().inject(this); + + String subredditName = intent.getExtras().getString(EXTRA_SUBREDDIT_NAME); + String title = intent.getExtras().getString(EXTRA_TITLE); + String flair = intent.getExtras().getString(EXTRA_FLAIR); + boolean isSpoiler = intent.getExtras().getBoolean(EXTRA_IS_SPOILER); + boolean isNSFW = intent.getExtras().getBoolean(EXTRA_IS_NSFW); + int postType = intent.getExtras().getInt(EXTRA_POST_TYPE); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel serviceChannel = new NotificationChannel( + NotificationUtils.CHANNEL_POST_MEDIA, + NotificationUtils.CHANNEL_POST_MEDIA, + NotificationManager.IMPORTANCE_LOW + ); + + NotificationManager manager = getSystemService(NotificationManager.class); + manager.createNotificationChannel(serviceChannel); + } + + if(postType == EXTRA_POST_TEXT_OR_LINK) { + String content = intent.getExtras().getString(EXTRA_CONTENT); + String kind = intent.getExtras().getString(EXTRA_KIND); + startForeground(1, createNotification(R.string.posting)); + submitTextOrLinkPost(subredditName, title, content, flair, isSpoiler, isNSFW, kind); + } else if(postType == EXTRA_POST_TYPE_IMAGE) { + Uri imageUri = intent.getData(); + startForeground(1, createNotification(R.string.posting_image)); + submitImagePost(imageUri, subredditName, title, flair, isSpoiler, isNSFW); + } else { + Uri videoUri = intent.getData(); + startForeground(1, createNotification(R.string.posting_video)); + submitVideoPost(videoUri, subredditName, title, flair, isSpoiler, isNSFW); + } + + return START_NOT_STICKY; + } + + private Notification createNotification(int stringResId) { + return new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_POST_MEDIA) + .setContentTitle(getString(stringResId)) + .setContentText(getString(R.string.please_wait)) + .setSmallIcon(R.mipmap.ic_launcher_round) + .build(); + } + + private void submitTextOrLinkPost(String subredditName, String title, String content, String flair, + boolean isSpoiler, boolean isNSFW, String kind) { + SubmitPost.submitTextOrLinkPost(mOauthRetrofit, sharedPreferences, getResources().getConfiguration().locale, + subredditName, title, content, flair, isSpoiler, isNSFW, kind, new SubmitPost.SubmitPostListener() { + @Override + public void submitSuccessful(Post post) { + EventBus.getDefault().post(new SubmitTextOrLinkPostEvent(true, post, null)); + + stopForeground(true); + stopSelf(); + } + + @Override + public void submitFailed(@Nullable String errorMessage) { + EventBus.getDefault().post(new SubmitTextOrLinkPostEvent(false, null, errorMessage)); + + stopForeground(true); + stopSelf(); + } + }); + } + + private void submitImagePost(Uri imageUri, String subredditName, String title, String flair, + boolean isSpoiler, boolean isNSFW) { + Glide.with(this) + .asBitmap() + .load(imageUri) + .into(new CustomTarget() { + + @Override + public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { + SubmitPost.submitImagePost(mOauthRetrofit, mUploadMediaRetrofit, sharedPreferences, + getResources().getConfiguration().locale, subredditName, title, resource, + flair, isSpoiler, isNSFW, new SubmitPost.SubmitPostListener() { + @Override + public void submitSuccessful(Post post) { + EventBus.getDefault().post(new SubmitImagePostEvent(true, null)); + Toast.makeText(SubmitPostService.this, R.string.image_is_processing, Toast.LENGTH_SHORT).show(); + + stopForeground(true); + stopSelf(); + } + + @Override + public void submitFailed(@Nullable String errorMessage) { + EventBus.getDefault().post(new SubmitImagePostEvent(false, errorMessage)); + + stopForeground(true); + stopSelf(); + } + }); + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + + } + }); + } + + private void submitVideoPost(Uri videoUri, String subredditName, String title, String flair, + boolean isSpoiler, boolean isNSFW) { + try (ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(videoUri, "r")) { + FileInputStream in = new FileInputStream(pfd.getFileDescriptor()); + byte[] buffer; + buffer = new byte[in.available()]; + while (in.read(buffer) != -1); + + Glide.with(this) + .asBitmap() + .load(videoUri) + .into(new CustomTarget() { + @Override + public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { + SubmitPost.submitVideoPost(mOauthRetrofit, mUploadMediaRetrofit, mUploadVideoRetrofit, + sharedPreferences, getResources().getConfiguration().locale, subredditName, title, + buffer, getContentResolver().getType(videoUri), resource, flair, isSpoiler, isNSFW, + new SubmitPost.SubmitPostListener() { + @Override + public void submitSuccessful(Post post) { + EventBus.getDefault().post(new SubmitVideoPostEvent(true, false, null)); + Toast.makeText(SubmitPostService.this, R.string.video_is_processing, Toast.LENGTH_SHORT).show(); + + stopForeground(true); + stopSelf(); + } + + @Override + public void submitFailed(@Nullable String errorMessage) { + EventBus.getDefault().post(new SubmitVideoPostEvent(false, false, errorMessage)); + + stopForeground(true); + stopSelf(); + } + }); + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + + } + }); + } catch (IOException e) { + e.printStackTrace(); + EventBus.getDefault().post(new SubmitVideoPostEvent(false, true, null)); + + stopForeground(true); + stopSelf(); + } + } +} diff --git a/app/src/main/res/layout/activity_post_link.xml b/app/src/main/res/layout/activity_post_link.xml index 52d1215e..664cccd2 100644 --- a/app/src/main/res/layout/activity_post_link.xml +++ b/app/src/main/res/layout/activity_post_link.xml @@ -148,7 +148,7 @@ android:gravity="top" android:padding="16dp" android:hint="@string/post_link_hint" - android:inputType="textCapSentences|textMultiLine" + android:inputType="textMultiLine" android:textSize="18sp" android:background="#00000000" android:textColor="@color/primaryTextColor" /> -- cgit v1.2.3