From 4085c0c40c20e13415994e13a30dc3152ef5b33a Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 28 Oct 2019 17:31:47 +0800 Subject: Display a content preview in PostFragment for text posts. --- app/src/main/AndroidManifest.xml | 18 +- .../Activity/PostImageActivity.java | 2 +- .../Activity/PostLinkActivity.java | 2 +- .../Activity/PostTextActivity.java | 2 +- .../Activity/PostVideoActivity.java | 2 +- .../Adapter/PostRecyclerViewAdapter.java | 4 +- .../infinityforreddit/AppComponent.java | 1 + .../infinityforreddit/JSONUtils.java | 1 + .../infinityforreddit/NotificationUtils.java | 6 +- .../infinityforreddit/ParsePost.java | 20 ++ .../ml/docilealligator/infinityforreddit/Post.java | 11 + .../Service/SubmitPostService.java | 261 +++++++++++++++++++++ .../infinityforreddit/SubmitPost.java | 30 +-- .../infinityforreddit/SubmitPostService.java | 256 -------------------- .../docilealligator/infinityforreddit/Utils.java | 18 +- 15 files changed, 343 insertions(+), 291 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Service/SubmitPostService.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPostService.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 907caafe..b166996a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,14 @@ + + + + + + - - - - - - \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostImageActivity.java index a9b138bb..b1ae72f3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostImageActivity.java @@ -51,7 +51,7 @@ import ml.docilealligator.infinityforreddit.Fragment.FlairBottomSheetFragment; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.SubmitPostService; +import ml.docilealligator.infinityforreddit.Service.SubmitPostService; import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostLinkActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostLinkActivity.java index 2c8f33b5..6abce5fb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostLinkActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostLinkActivity.java @@ -41,7 +41,7 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.RedditUtils; -import ml.docilealligator.infinityforreddit.SubmitPostService; +import ml.docilealligator.infinityforreddit.Service.SubmitPostService; import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostTextActivity.java index 7c034945..216d2c3b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostTextActivity.java @@ -41,7 +41,7 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.RedditUtils; -import ml.docilealligator.infinityforreddit.SubmitPostService; +import ml.docilealligator.infinityforreddit.Service.SubmitPostService; import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostVideoActivity.java index 68345423..f0a0aa99 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostVideoActivity.java @@ -53,7 +53,7 @@ import ml.docilealligator.infinityforreddit.Fragment.FlairBottomSheetFragment; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.SubmitPostService; +import ml.docilealligator.infinityforreddit.Service.SubmitPostService; import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java index 62d5277e..f59cfff5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -468,9 +468,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter 150) { + selfTextPlain = selfTextPlain.substring(150); + } + post.setSelfTextPlainTrimmed(selfTextPlain); + } } } else { //No preview link post @@ -202,6 +212,16 @@ public class ParsePost { post.setSelfText(""); } else { post.setSelfText(Utils.addSubredditAndUserLink(data.getString(JSONUtils.SELFTEXT_KEY).trim())); + if (data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) { + post.setSelfTextPlainTrimmed(""); + } else { + String selfTextPlain = Utils.trimTrailingWhitespace( + Html.fromHtml(data.getString(JSONUtils.SELFTEXT_HTML_KEY))).toString(); + if (selfTextPlain.length() > 150) { + selfTextPlain = selfTextPlain.substring(150); + } + post.setSelfTextPlainTrimmed(selfTextPlain); + } } } else { //Link post diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java index 2aee9455..1f2c3681 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java @@ -37,6 +37,7 @@ public class Post implements Parcelable { private String postTime; private String title; private String selfText; + private String selfTextPlainTrimmed; private String previewUrl; private String url; private String videoUrl; @@ -162,6 +163,7 @@ public class Post implements Parcelable { postTime = in.readString(); title = in.readString(); selfText = in.readString(); + selfTextPlainTrimmed = in.readString(); previewUrl = in.readString(); url = in.readString(); videoUrl = in.readString(); @@ -245,6 +247,14 @@ public class Post implements Parcelable { this.selfText = selfText; } + public String getSelfTextPlainTrimmed() { + return selfTextPlainTrimmed; + } + + public void setSelfTextPlainTrimmed(String selfTextPlainTrimmed) { + this.selfTextPlainTrimmed = selfTextPlainTrimmed; + } + public String getPreviewUrl() { return previewUrl; } @@ -395,6 +405,7 @@ public class Post implements Parcelable { parcel.writeString(postTime); parcel.writeString(title); parcel.writeString(selfText); + parcel.writeString(selfTextPlainTrimmed); parcel.writeString(previewUrl); parcel.writeString(url); parcel.writeString(videoUrl); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/SubmitPostService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/SubmitPostService.java new file mode 100644 index 00000000..2b6ad9ce --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/SubmitPostService.java @@ -0,0 +1,261 @@ +package ml.docilealligator.infinityforreddit.Service; + +import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.app.Service; +import android.content.Intent; +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 androidx.core.app.NotificationManagerCompat; + +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 ml.docilealligator.infinityforreddit.Event.SubmitImagePostEvent; +import ml.docilealligator.infinityforreddit.Event.SubmitTextOrLinkPostEvent; +import ml.docilealligator.infinityforreddit.Event.SubmitVideoPostEvent; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.NotificationUtils; +import ml.docilealligator.infinityforreddit.Post; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.SubmitPost; +import retrofit2.Retrofit; + +public class SubmitPostService extends Service { + public static final String EXTRA_ACCESS_TOKEN = "EAT"; + public static final String EXTRA_SUBREDDIT_NAME = "ESN"; + public static final String EXTRA_TITLE = "ET"; + public static final String EXTRA_CONTENT = "EC"; + public static final String EXTRA_KIND = "EK"; + public static final String EXTRA_FLAIR = "EF"; + public static final String EXTRA_IS_SPOILER = "EIS"; + public static final String EXTRA_IS_NSFW = "EIN"; + public static final String EXTRA_POST_TYPE = "EPT"; + public static final int EXTRA_POST_TEXT_OR_LINK = 0; + public static final int EXTRA_POST_TYPE_IMAGE = 1; + public 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; + private String mAccessToken; + private String subredditName; + private String title; + private String flair; + private boolean isSpoiler; + private boolean isNSFW; + private String content; + private String kind; + private Uri mediaUri; + + public SubmitPostService() { + } + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + ((Infinity) getApplication()).getAppComponent().inject(this); + + mAccessToken = intent.getStringExtra(EXTRA_ACCESS_TOKEN); + subredditName = intent.getStringExtra(EXTRA_SUBREDDIT_NAME); + title = intent.getStringExtra(EXTRA_TITLE); + flair = intent.getStringExtra(EXTRA_FLAIR); + isSpoiler = intent.getBooleanExtra(EXTRA_IS_SPOILER, false); + isNSFW = intent.getBooleanExtra(EXTRA_IS_NSFW, false); + int postType = intent.getIntExtra(EXTRA_POST_TYPE, EXTRA_POST_TEXT_OR_LINK); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel serviceChannel = new NotificationChannel( + NotificationUtils.CHANNEL_SUBMIT_POST, + NotificationUtils.CHANNEL_SUBMIT_POST, + NotificationManager.IMPORTANCE_LOW + ); + + NotificationManagerCompat manager = NotificationManagerCompat.from(this); + manager.createNotificationChannel(serviceChannel); + } + + if (postType == EXTRA_POST_TEXT_OR_LINK) { + content = intent.getStringExtra(EXTRA_CONTENT); + kind = intent.getStringExtra(EXTRA_KIND); + startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID, createNotification(R.string.posting)); + submitTextOrLinkPost(); + } else if (postType == EXTRA_POST_TYPE_IMAGE) { + mediaUri = intent.getData(); + startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID, createNotification(R.string.posting_image)); + submitImagePost(); + } else { + mediaUri = intent.getData(); + startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID, createNotification(R.string.posting_video)); + submitVideoPost(); + } + + return START_NOT_STICKY; + } + + private Notification createNotification(int stringResId) { + return new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_SUBMIT_POST) + .setContentTitle(getString(stringResId)) + .setContentText(getString(R.string.please_wait)) + .setSmallIcon(R.drawable.ic_notification) + .setColor(getResources().getColor(R.color.notificationIconColor)) + .build(); + } + + private void submitTextOrLinkPost() { + SubmitPost.submitTextOrLinkPost(mOauthRetrofit, mAccessToken, 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)); + + stopService(); + } + + @Override + public void submitFailed(@Nullable String errorMessage) { + EventBus.getDefault().post(new SubmitTextOrLinkPostEvent(false, null, errorMessage)); + + stopService(); + } + }); + } + + private void submitImagePost() { + Glide.with(this) + .asBitmap() + .load(mediaUri) + .into(new CustomTarget() { + + @Override + public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { + SubmitPost.submitImagePost(mOauthRetrofit, mUploadMediaRetrofit, mAccessToken, + 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(); + + stopService(); + } + + @Override + public void submitFailed(@Nullable String errorMessage) { + EventBus.getDefault().post(new SubmitImagePostEvent(false, errorMessage)); + + stopService(); + } + }); + } + + @Override + public void onLoadFailed(@Nullable Drawable errorDrawable) { + EventBus.getDefault().post(new SubmitImagePostEvent(false, getString(R.string.error_processing_image))); + + stopService(); + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + + } + }); + } + + private void submitVideoPost() { + try (ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(mediaUri, "r")) { + if (pfd != null) { + FileInputStream in = new FileInputStream(pfd.getFileDescriptor()); + byte[] buffer; + buffer = new byte[in.available()]; + while (in.read(buffer) != -1) ; + + Glide.with(this) + .asBitmap() + .load(mediaUri) + .into(new CustomTarget() { + @Override + public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { + String type = getContentResolver().getType(mediaUri); + if (type != null) { + SubmitPost.submitVideoPost(mOauthRetrofit, mUploadMediaRetrofit, mUploadVideoRetrofit, + mAccessToken, getResources().getConfiguration().locale, subredditName, title, + buffer, type, 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(); + + stopService(); + } + + @Override + public void submitFailed(@Nullable String errorMessage) { + EventBus.getDefault().post(new SubmitVideoPostEvent(false, false, errorMessage)); + + stopService(); + } + }); + } else { + EventBus.getDefault().post(new SubmitVideoPostEvent(false, true, null)); + } + } + + @Override + public void onLoadFailed(@Nullable Drawable errorDrawable) { + EventBus.getDefault().post(new SubmitVideoPostEvent(false, true, null)); + + stopService(); + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + + } + }); + } else { + EventBus.getDefault().post(new SubmitVideoPostEvent(false, true, null)); + } + } catch (IOException e) { + e.printStackTrace(); + EventBus.getDefault().post(new SubmitVideoPostEvent(false, true, null)); + + stopService(); + } + } + + private void stopService() { + stopForeground(true); + stopSelf(); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java index d3ce5719..f5192823 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java @@ -28,19 +28,19 @@ import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; -class SubmitPost { - static void submitTextOrLinkPost(Retrofit oauthRetrofit, String accessToken, - Locale locale, String subredditName, String title, String content, - String flair, boolean isSpoiler, boolean isNSFW, String kind, - SubmitPostListener submitPostListener) { +public class SubmitPost { + public static void submitTextOrLinkPost(Retrofit oauthRetrofit, String accessToken, + Locale locale, String subredditName, String title, String content, + String flair, boolean isSpoiler, boolean isNSFW, String kind, + SubmitPostListener submitPostListener) { submitPost(oauthRetrofit, accessToken, locale, subredditName, title, content, flair, isSpoiler, isNSFW, kind, null, submitPostListener); } - static void submitImagePost(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit, - String accessToken, Locale locale, - String subredditName, String title, Bitmap image, String flair, - boolean isSpoiler, boolean isNSFW, SubmitPostListener submitPostListener) { + public static void submitImagePost(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit, + String accessToken, Locale locale, + String subredditName, String title, Bitmap image, String flair, + boolean isSpoiler, boolean isNSFW, SubmitPostListener submitPostListener) { uploadImage(oauthRetrofit, uploadMediaRetrofit, accessToken, image, new UploadImageListener() { @Override @@ -57,11 +57,11 @@ class SubmitPost { }); } - static void submitVideoPost(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit, - Retrofit uploadVideoRetrofit, String accessToken, - Locale locale, String subredditName, String title, byte[] buffer, String mimeType, - Bitmap posterBitmap, String flair, boolean isSpoiler, boolean isNSFW, - SubmitPostListener submitPostListener) { + public static void submitVideoPost(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit, + Retrofit uploadVideoRetrofit, String accessToken, + Locale locale, String subredditName, String title, byte[] buffer, String mimeType, + Bitmap posterBitmap, String flair, boolean isSpoiler, boolean isNSFW, + SubmitPostListener submitPostListener) { RedditAPI api = oauthRetrofit.create(RedditAPI.class); String fileType = mimeType.substring(mimeType.indexOf("/") + 1); @@ -341,7 +341,7 @@ class SubmitPost { } } - interface SubmitPostListener { + public interface SubmitPostListener { void submitSuccessful(Post post); void submitFailed(@Nullable String errorMessage); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPostService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPostService.java deleted file mode 100644 index e065459c..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPostService.java +++ /dev/null @@ -1,256 +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.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 androidx.core.app.NotificationManagerCompat; - -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 ml.docilealligator.infinityforreddit.Event.SubmitImagePostEvent; -import ml.docilealligator.infinityforreddit.Event.SubmitTextOrLinkPostEvent; -import ml.docilealligator.infinityforreddit.Event.SubmitVideoPostEvent; -import retrofit2.Retrofit; - -public class SubmitPostService extends Service { - public static final String EXTRA_ACCESS_TOKEN = "EAT"; - public static final String EXTRA_SUBREDDIT_NAME = "ESN"; - public static final String EXTRA_TITLE = "ET"; - public static final String EXTRA_CONTENT = "EC"; - public static final String EXTRA_KIND = "EK"; - public static final String EXTRA_FLAIR = "EF"; - public static final String EXTRA_IS_SPOILER = "EIS"; - public static final String EXTRA_IS_NSFW = "EIN"; - public static final String EXTRA_POST_TYPE = "EPT"; - public static final int EXTRA_POST_TEXT_OR_LINK = 0; - public static final int EXTRA_POST_TYPE_IMAGE = 1; - public 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; - private String mAccessToken; - private String subredditName; - private String title; - private String flair; - private boolean isSpoiler; - private boolean isNSFW; - private String content; - private String kind; - private Uri mediaUri; - - public SubmitPostService() { - } - - @Override - public IBinder onBind(Intent intent) { - return null; - } - - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - ((Infinity) getApplication()).getAppComponent().inject(this); - - mAccessToken = intent.getStringExtra(EXTRA_ACCESS_TOKEN); - subredditName = intent.getStringExtra(EXTRA_SUBREDDIT_NAME); - title = intent.getStringExtra(EXTRA_TITLE); - flair = intent.getStringExtra(EXTRA_FLAIR); - isSpoiler = intent.getBooleanExtra(EXTRA_IS_SPOILER, false); - isNSFW = intent.getBooleanExtra(EXTRA_IS_NSFW, false); - int postType = intent.getIntExtra(EXTRA_POST_TYPE, EXTRA_POST_TEXT_OR_LINK); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel serviceChannel = new NotificationChannel( - NotificationUtils.CHANNEL_SUBMIT_POST, - NotificationUtils.CHANNEL_SUBMIT_POST, - NotificationManager.IMPORTANCE_LOW - ); - - NotificationManagerCompat manager = NotificationManagerCompat.from(this); - manager.createNotificationChannel(serviceChannel); - } - - if (postType == EXTRA_POST_TEXT_OR_LINK) { - content = intent.getStringExtra(EXTRA_CONTENT); - kind = intent.getStringExtra(EXTRA_KIND); - startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID, createNotification(R.string.posting)); - submitTextOrLinkPost(); - } else if (postType == EXTRA_POST_TYPE_IMAGE) { - mediaUri = intent.getData(); - startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID, createNotification(R.string.posting_image)); - submitImagePost(); - } else { - mediaUri = intent.getData(); - startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID, createNotification(R.string.posting_video)); - submitVideoPost(); - } - - return START_NOT_STICKY; - } - - private Notification createNotification(int stringResId) { - return new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_SUBMIT_POST) - .setContentTitle(getString(stringResId)) - .setContentText(getString(R.string.please_wait)) - .setSmallIcon(R.drawable.ic_notification) - .setColor(getResources().getColor(R.color.notificationIconColor)) - .build(); - } - - private void submitTextOrLinkPost() { - SubmitPost.submitTextOrLinkPost(mOauthRetrofit, mAccessToken, 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)); - - stopService(); - } - - @Override - public void submitFailed(@Nullable String errorMessage) { - EventBus.getDefault().post(new SubmitTextOrLinkPostEvent(false, null, errorMessage)); - - stopService(); - } - }); - } - - private void submitImagePost() { - Glide.with(this) - .asBitmap() - .load(mediaUri) - .into(new CustomTarget() { - - @Override - public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { - SubmitPost.submitImagePost(mOauthRetrofit, mUploadMediaRetrofit, mAccessToken, - 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(); - - stopService(); - } - - @Override - public void submitFailed(@Nullable String errorMessage) { - EventBus.getDefault().post(new SubmitImagePostEvent(false, errorMessage)); - - stopService(); - } - }); - } - - @Override - public void onLoadFailed(@Nullable Drawable errorDrawable) { - EventBus.getDefault().post(new SubmitImagePostEvent(false, getString(R.string.error_processing_image))); - - stopService(); - } - - @Override - public void onLoadCleared(@Nullable Drawable placeholder) { - - } - }); - } - - private void submitVideoPost() { - try (ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(mediaUri, "r")) { - if (pfd != null) { - FileInputStream in = new FileInputStream(pfd.getFileDescriptor()); - byte[] buffer; - buffer = new byte[in.available()]; - while (in.read(buffer) != -1) ; - - Glide.with(this) - .asBitmap() - .load(mediaUri) - .into(new CustomTarget() { - @Override - public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { - String type = getContentResolver().getType(mediaUri); - if (type != null) { - SubmitPost.submitVideoPost(mOauthRetrofit, mUploadMediaRetrofit, mUploadVideoRetrofit, - mAccessToken, getResources().getConfiguration().locale, subredditName, title, - buffer, type, 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(); - - stopService(); - } - - @Override - public void submitFailed(@Nullable String errorMessage) { - EventBus.getDefault().post(new SubmitVideoPostEvent(false, false, errorMessage)); - - stopService(); - } - }); - } else { - EventBus.getDefault().post(new SubmitVideoPostEvent(false, true, null)); - } - } - - @Override - public void onLoadFailed(@Nullable Drawable errorDrawable) { - EventBus.getDefault().post(new SubmitVideoPostEvent(false, true, null)); - - stopService(); - } - - @Override - public void onLoadCleared(@Nullable Drawable placeholder) { - - } - }); - } else { - EventBus.getDefault().post(new SubmitVideoPostEvent(false, true, null)); - } - } catch (IOException e) { - e.printStackTrace(); - EventBus.getDefault().post(new SubmitVideoPostEvent(false, true, null)); - - stopService(); - } - } - - private void stopService() { - stopForeground(true); - stopSelf(); - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils.java index 3e3d4e54..cebff4ea 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils.java @@ -2,7 +2,21 @@ package ml.docilealligator.infinityforreddit; public class Utils { public static String addSubredditAndUserLink(String markdown) { - return markdown.replaceAll("\n", " ") - .replaceAll("((?<=[\\s])|^)/{0,1}[ru]/\\w+/{0,1}", "[$0]($0)"); + return markdown.replaceAll("((?<=[\\s])|^)/{0,1}[rRuU]/\\w+/{0,1}", "[$0]($0)"); + } + + public static CharSequence trimTrailingWhitespace(CharSequence source) { + + if(source == null) + return ""; + + int i = source.length(); + + // loop back to the first non-whitespace character + do { + i--; + } while (i >= 0 && Character.isWhitespace(source.charAt(i))); + + return source.subSequence(0, i+1); } } -- cgit v1.2.3