aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2019-08-06 09:35:54 +0000
committerAlex Ning <chineseperson5@gmail.com>2019-08-06 09:35:54 +0000
commitfe898a5c6c6cea96ab29e3677f9c89435f6c1075 (patch)
treeeb88979822d29fb05f8807e49b1a3fc54f5398da /app/src/main/java
parent5bb9cf789143725c7645ec54ed908ea52a255770 (diff)
downloadinfinity-for-reddit-fe898a5c6c6cea96ab29e3677f9c89435f6c1075.tar
infinity-for-reddit-fe898a5c6c6cea96ab29e3677f9c89435f6c1075.tar.gz
infinity-for-reddit-fe898a5c6c6cea96ab29e3677f9c89435f6c1075.tar.bz2
infinity-for-reddit-fe898a5c6c6cea96ab29e3677f9c89435f6c1075.tar.lz
infinity-for-reddit-fe898a5c6c6cea96ab29e3677f9c89435f6c1075.tar.xz
infinity-for-reddit-fe898a5c6c6cea96ab29e3677f9c89435f6c1075.tar.zst
infinity-for-reddit-fe898a5c6c6cea96ab29e3677f9c89435f6c1075.zip
Create PostMediaService to submit video and image posts.
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/CustomView/CustomMarkwonView.java2
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java1
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java4
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/NotificationUtils.java5
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java146
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostMediaService.java203
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java153
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SubmitImagePostEvent.java11
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java1
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SubmitVideoPostEvent.java13
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java23
11 files changed, 396 insertions, 166 deletions
diff --git a/app/src/main/java/CustomView/CustomMarkwonView.java b/app/src/main/java/CustomView/CustomMarkwonView.java
index 05c2e2fa..da6b2f55 100644
--- a/app/src/main/java/CustomView/CustomMarkwonView.java
+++ b/app/src/main/java/CustomView/CustomMarkwonView.java
@@ -41,7 +41,7 @@ public class CustomMarkwonView extends MarkwonView {
builder.setToolbarColor(context.getResources().getColor(R.color.colorPrimary));
CustomTabsIntent customTabsIntent = builder.build();
Uri uri = Uri.parse(link);
- if(uri.getHost() != null && uri.getHost().equals("www.reddit.com")) {
+ if(uri.getHost() != null && uri.getHost().contains("reddit.com")) {
customTabsIntent.intent.setPackage(context.getPackageName());
}
customTabsIntent.launchUrl(context, uri);
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java
index 79aed4ef..6dca6581 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java
@@ -25,4 +25,5 @@ interface AppComponent {
void inject(FlairBottomSheetFragment flairBottomSheetFragment);
void inject(RulesActivity rulesActivity);
void inject(CommentsListingFragment commentsListingFragment);
+ void inject(PostMediaService postMediaService);
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java
index 7b284a79..68199d6d 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java
@@ -309,8 +309,8 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
loadUserData();
return true;
case R.id.action_lazy_mode_main_activity:
- MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_main_activity);
- /*if(isInLazyMode) {
+ /*MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_main_activity);
+ if(isInLazyMode) {
isInLazyMode = false;
((FragmentCommunicator) mFragment).stopLazyMode();
lazyModeItem.setTitle(R.string.action_start_lazy_mode);
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/NotificationUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/NotificationUtils.java
new file mode 100644
index 00000000..978ebe0f
--- /dev/null
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/NotificationUtils.java
@@ -0,0 +1,5 @@
+package ml.docilealligator.infinityforreddit;
+
+class NotificationUtils {
+ static final String CHANNEL_POST_MEDIA = "Post Media";
+}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java
index 212f3fa5..5dd8934f 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java
@@ -2,9 +2,8 @@ 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.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
@@ -15,7 +14,6 @@ import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
-import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -28,15 +26,15 @@ import androidx.core.content.FileProvider;
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 com.libRG.CustomTextView;
+import org.greenrobot.eventbus.EventBus;
+import org.greenrobot.eventbus.Subscribe;
+
import java.io.File;
import java.io.IOException;
-import java.util.Locale;
import javax.inject.Inject;
import javax.inject.Named;
@@ -46,8 +44,6 @@ 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 implements FlairBottomSheetFragment.FlairSelectionCallback {
@@ -60,6 +56,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
private static final String SUBREDDIT_IS_USER_STATE = "SIUS";
private static final String IMAGE_URI_STATE = "IUS";
private static final String LOAD_SUBREDDIT_ICON_STATE = "LSIS";
+ private static final String IS_POSTING_STATE = "IPS";
private static final String FLAIR_STATE = "FS";
private static final String IS_SPOILER_STATE = "ISS";
private static final String IS_NSFW_STATE = "INS";
@@ -88,15 +85,17 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
private boolean subredditSelected = false;
private boolean subredditIsUser;
private boolean loadSubredditIconSuccessful = true;
+ private boolean isPosting;
private Uri imageUri;
private String flair = null;
private boolean isSpoiler = false;
private boolean isNSFW = false;
+ private Menu mMemu;
private RequestManager mGlide;
- private Locale mLocale;
private FlairBottomSheetFragment flairSelectionBottomSheetFragment;
+ private Snackbar mPostingSnackbar;
@Inject
@Named("no_oauth")
@@ -125,13 +124,14 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
ButterKnife.bind(this);
+ EventBus.getDefault().register(this);
+
((Infinity) getApplication()).getmAppComponent().inject(this);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mGlide = Glide.with(this);
- mLocale = getResources().getConfiguration().locale;
if(savedInstanceState != null) {
subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE);
@@ -139,6 +139,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
subredditSelected = savedInstanceState.getBoolean(SUBREDDIT_SELECTED_STATE);
subredditIsUser = savedInstanceState.getBoolean(SUBREDDIT_IS_USER_STATE);
loadSubredditIconSuccessful = savedInstanceState.getBoolean(LOAD_SUBREDDIT_ICON_STATE);
+ isPosting = savedInstanceState.getBoolean(IS_POSTING_STATE);
flair = savedInstanceState.getString(FLAIR_STATE);
isSpoiler = savedInstanceState.getBoolean(IS_SPOILER_STATE);
isNSFW = savedInstanceState.getBoolean(IS_NSFW_STATE);
@@ -157,6 +158,11 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
}
displaySubredditIcon();
+ if(isPosting) {
+ mPostingSnackbar = Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_INDEFINITE);
+ mPostingSnackbar.show();
+ }
+
if(flair != null) {
flairTextView.setText(flair);
flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark));
@@ -168,6 +174,8 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
nsfwTextView.setBackgroundColor(getResources().getColor(R.color.colorAccent));
}
} else {
+ isPosting = false;
+
if(getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) {
subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME);
subreditNameTextView.setText(subredditName);
@@ -302,6 +310,11 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.post_image_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);
+ }
return true;
}
@@ -322,10 +335,12 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
return true;
}
+ isPosting = true;
+
item.setEnabled(false);
item.getIcon().setAlpha(130);
- Snackbar postingSnackbar = Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_INDEFINITE);
- postingSnackbar.show();
+ mPostingSnackbar = Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_INDEFINITE);
+ mPostingSnackbar.show();
String subredditName;
if(subredditIsUser) {
@@ -334,76 +349,27 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
subredditName = subreditNameTextView.getText().toString();
}
- Glide.with(this)
- .asBitmap()
- .load(imageUri)
- .into(new CustomTarget<Bitmap>() {
-
- @Override
- public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
- SubmitPost.submitImagePost(mOauthRetrofit, mUploadMediaRetrofit, sharedPreferences,
- mLocale, subredditName, titleEditText.getText().toString(), resource,
- flair, isSpoiler, isNSFW, 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()) {
- Toast.makeText(PostImageActivity.this, R.string.image_is_processing, Toast.LENGTH_SHORT).show();
- Intent intent = new Intent(PostImageActivity.this, ViewUserDetailActivity.class);
- intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY,
- mUserInfoSharedPreferences.getString(SharedPreferencesUtils.USER_KEY, ""));
- startActivity(intent);
- finish();
- } 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 onLoadCleared(@Nullable Drawable placeholder) {
-
- }
- });
+ Intent intent = new Intent(this, PostMediaService.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);
+
+ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ startForegroundService(intent);
+ } else {
+ startService(intent);
+ }
+
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);
@@ -415,6 +381,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
outState.putString(IMAGE_URI_STATE, imageUri.toString());
}
outState.putBoolean(LOAD_SUBREDDIT_ICON_STATE, loadSubredditIconSuccessful);
+ outState.putBoolean(IS_POSTING_STATE, isPosting);
outState.putString(FLAIR_STATE, flair);
outState.putBoolean(IS_SPOILER_STATE, isSpoiler);
outState.putBoolean(IS_NSFW_STATE, isNSFW);
@@ -457,10 +424,37 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
}
@Override
+ protected void onDestroy() {
+ EventBus.getDefault().unregister(this);
+ super.onDestroy();
+ }
+
+ @Override
public void flairSelected(String flair) {
this.flair = flair;
flairTextView.setText(flair);
flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark));
flairSelectionBottomSheetFragment.dismiss();
}
+
+ @Subscribe
+ public void onSubmitImagePostEvent(SubmitImagePostEvent submitImagePostEvent) {
+ isPosting = false;
+ if(submitImagePostEvent.postSuccess) {
+ Intent intent = new Intent(this, ViewUserDetailActivity.class);
+ intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY,
+ mUserInfoSharedPreferences.getString(SharedPreferencesUtils.USER_KEY, ""));
+ startActivity(intent);
+ finish();
+ } else {
+ mPostingSnackbar.dismiss();
+ mMemu.getItem(R.id.action_send_post_image_activity).setEnabled(true);
+ mMemu.getItem(R.id.action_send_post_image_activity).getIcon().setAlpha(255);
+ if (submitImagePostEvent.errorMessage == null || submitImagePostEvent.errorMessage.equals("")) {
+ Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show();
+ } else {
+ Snackbar.make(coordinatorLayout, submitImagePostEvent.errorMessage, Snackbar.LENGTH_SHORT).show();
+ }
+ }
+ }
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostMediaService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostMediaService.java
new file mode 100644
index 00000000..fa02eb41
--- /dev/null
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostMediaService.java
@@ -0,0 +1,203 @@
+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_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_TYPE_IMAGE = 0;
+ static final int EXTRA_POST_TYPE_VIDEO = 1;
+
+ @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);
+
+ Uri mediaUri = intent.getData();
+ 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_TYPE_IMAGE) {
+ startForeground(1, createNotification(R.string.posting_image));
+ submitImagePost(mediaUri, subredditName, title, flair, isSpoiler, isNSFW);
+ } else {
+ startForeground(1, createNotification(R.string.posting_video));
+ submitVideoPost(mediaUri, 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 submitImagePost(Uri imageUri, String subredditName, String title, String flair,
+ boolean isSpoiler, boolean isNSFW) {
+ Glide.with(this)
+ .asBitmap()
+ .load(imageUri)
+ .into(new CustomTarget<Bitmap>() {
+
+ @Override
+ public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> 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<Bitmap>() {
+ @Override
+ public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> 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/PostVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java
index 349d1c46..9f3f14fb 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java
@@ -2,11 +2,9 @@ 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.Build;
import android.os.Bundle;
-import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
@@ -14,7 +12,6 @@ import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
-import android.widget.Toast;
import android.widget.VideoView;
import androidx.annotation.NonNull;
@@ -27,15 +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 com.libRG.CustomTextView;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.util.Locale;
+import org.greenrobot.eventbus.EventBus;
+import org.greenrobot.eventbus.Subscribe;
import javax.inject.Inject;
import javax.inject.Named;
@@ -57,6 +51,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
private static final String SUBREDDIT_IS_USER_STATE = "SIUS";
private static final String VIDEO_URI_STATE = "IUS";
private static final String LOAD_SUBREDDIT_ICON_STATE = "LSIS";
+ private static final String IS_POSTING_STATE = "IPS";
private static final String FLAIR_STATE = "FS";
private static final String IS_SPOILER_STATE = "ISS";
private static final String IS_NSFW_STATE = "INS";
@@ -86,14 +81,16 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
private boolean subredditIsUser;
private Uri videoUri;
private boolean loadSubredditIconSuccessful = true;
+ private boolean isPosting;
private String flair = null;
private boolean isSpoiler = false;
private boolean isNSFW = false;
+ private Menu mMemu;
private RequestManager mGlide;
- private Locale mLocale;
- private FlairBottomSheetFragment flairSelectionBottomSheetFragment;
+ private FlairBottomSheetFragment mFlairSelectionBottomSheetFragment;
+ private Snackbar mPostingSnackbar;
@Inject
@Named("no_oauth")
@@ -126,13 +123,14 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
ButterKnife.bind(this);
+ EventBus.getDefault().register(this);
+
((Infinity) getApplication()).getmAppComponent().inject(this);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mGlide = Glide.with(this);
- mLocale = getResources().getConfiguration().locale;
if(savedInstanceState != null) {
subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE);
@@ -140,13 +138,14 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
subredditSelected = savedInstanceState.getBoolean(SUBREDDIT_SELECTED_STATE);
subredditIsUser = savedInstanceState.getBoolean(SUBREDDIT_IS_USER_STATE);
loadSubredditIconSuccessful = savedInstanceState.getBoolean(LOAD_SUBREDDIT_ICON_STATE);
+ isPosting = savedInstanceState.getBoolean(IS_POSTING_STATE);
flair = savedInstanceState.getString(FLAIR_STATE);
isSpoiler = savedInstanceState.getBoolean(IS_SPOILER_STATE);
isNSFW = savedInstanceState.getBoolean(IS_NSFW_STATE);
if(savedInstanceState.getString(VIDEO_URI_STATE) != null) {
videoUri = Uri.parse(savedInstanceState.getString(VIDEO_URI_STATE));
- loadImage();
+ loadVideo();
}
if(subredditName != null) {
@@ -158,6 +157,11 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
}
displaySubredditIcon();
+ if(isPosting) {
+ mPostingSnackbar = Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_INDEFINITE);
+ mPostingSnackbar.show();
+ }
+
if(flair != null) {
flairTextView.setText(flair);
flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark));
@@ -169,6 +173,8 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
nsfwTextView.setBackgroundColor(getResources().getColor(R.color.colorAccent));
}
} else {
+ isPosting = false;
+
if(getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) {
subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME);
subreditNameTextView.setText(subredditName);
@@ -206,11 +212,11 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
flairTextView.setOnClickListener(view -> {
if(flair == null) {
- flairSelectionBottomSheetFragment = new FlairBottomSheetFragment();
+ mFlairSelectionBottomSheetFragment = new FlairBottomSheetFragment();
Bundle bundle = new Bundle();
bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, subredditName);
- flairSelectionBottomSheetFragment.setArguments(bundle);
- flairSelectionBottomSheetFragment.show(getSupportFragmentManager(), flairSelectionBottomSheetFragment.getTag());
+ mFlairSelectionBottomSheetFragment.setArguments(bundle);
+ mFlairSelectionBottomSheetFragment.show(getSupportFragmentManager(), mFlairSelectionBottomSheetFragment.getTag());
} else {
flairTextView.setBackgroundColor(getResources().getColor(android.R.color.transparent));
flairTextView.setText(getString(R.string.flair));
@@ -266,7 +272,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
});
}
- private void loadImage() {
+ private void loadVideo() {
constraintLayout.setVisibility(View.GONE);
videoView.setVisibility(View.VISIBLE);
selectAgainTextView.setVisibility(View.VISIBLE);
@@ -299,7 +305,12 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
@Override
public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.post_image_activity, menu);
+ getMenuInflater().inflate(R.menu.post_video_activity, menu);
+ mMemu = menu;
+ if(isPosting) {
+ mMemu.findItem(R.id.action_send_post_video_activity).setEnabled(false);
+ mMemu.findItem(R.id.action_send_post_video_activity).getIcon().setAlpha(130);
+ }
return true;
}
@@ -309,7 +320,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
case android.R.id.home:
finish();
return true;
- case R.id.action_send_post_image_activity:
+ case R.id.action_send_post_video_activity:
if(!subredditSelected) {
Snackbar.make(coordinatorLayout, R.string.select_a_subreddit, Snackbar.LENGTH_SHORT).show();
return true;
@@ -320,10 +331,12 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
return true;
}
+ isPosting = true;
+
item.setEnabled(false);
item.getIcon().setAlpha(130);
- Snackbar postingSnackbar = Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_INDEFINITE);
- postingSnackbar.show();
+ mPostingSnackbar = Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_INDEFINITE);
+ mPostingSnackbar.show();
String subredditName;
if(subredditIsUser) {
@@ -332,55 +345,21 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
subredditName = subreditNameTextView.getText().toString();
}
- 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<Bitmap>() {
- @Override
- public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
- SubmitPost.submitVideoPost(mOauthRetrofit, mUploadMediaRetrofit, mUploadVideoRetrofit,
- sharedPreferences, mLocale, subredditName, titleEditText.getText().toString(),
- buffer, getContentResolver().getType(videoUri), resource, flair, isSpoiler, isNSFW,
- new SubmitPost.SubmitPostListener() {
- @Override
- public void submitSuccessful(Post post) {
- Toast.makeText(PostVideoActivity.this, R.string.video_is_processing, Toast.LENGTH_SHORT).show();
- Intent intent = new Intent(PostVideoActivity.this, ViewUserDetailActivity.class);
- intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY,
- mUserInfoSharedPreferences.getString(SharedPreferencesUtils.USER_KEY, ""));
- startActivity(intent);
- finish();
- }
-
- @Override
- public void submitFailed(@Nullable String errorMessage) {
- postingSnackbar.dismiss();
- item.setEnabled(true);
- item.getIcon().setAlpha(255);
- if (errorMessage == null || errorMessage.equals("")) {
- Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show();
- } else {
- Snackbar.make(coordinatorLayout, errorMessage, Snackbar.LENGTH_SHORT).show();
- }
- }
- });
- }
-
- @Override
- public void onLoadCleared(@Nullable Drawable placeholder) {
-
- }
- });
- } catch (IOException e) {
- e.printStackTrace();
- Snackbar.make(coordinatorLayout, R.string.error_processing_video, Snackbar.LENGTH_SHORT).show();
+ Intent intent = new Intent(this, PostMediaService.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);
+
+ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ startForegroundService(intent);
+ } else {
+ startService(intent);
}
+
return true;
}
@@ -404,6 +383,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
outState.putString(VIDEO_URI_STATE, videoUri.toString());
}
outState.putBoolean(LOAD_SUBREDDIT_ICON_STATE, loadSubredditIconSuccessful);
+ outState.putBoolean(IS_POSTING_STATE, isPosting);
outState.putString(FLAIR_STATE, flair);
outState.putBoolean(IS_SPOILER_STATE, isSpoiler);
outState.putBoolean(IS_NSFW_STATE, isNSFW);
@@ -436,19 +416,48 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
}
videoUri = data.getData();
- loadImage();
+ loadVideo();
}
} else if (requestCode == CAPTURE_VIDEO_REQUEST_CODE) {
videoUri = data.getData();
- loadImage();
+ loadVideo();
}
}
@Override
+ protected void onDestroy() {
+ EventBus.getDefault().unregister(this);
+ super.onDestroy();
+ }
+
+ @Override
public void flairSelected(String flair) {
this.flair = flair;
flairTextView.setText(flair);
flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark));
- flairSelectionBottomSheetFragment.dismiss();
+ mFlairSelectionBottomSheetFragment.dismiss();
+ }
+
+ @Subscribe
+ public void onSubmitVideoPostEvent(SubmitVideoPostEvent submitVideoPostEvent) {
+ isPosting = false;
+ if(submitVideoPostEvent.postSuccess) {
+ Intent intent = new Intent(this, ViewUserDetailActivity.class);
+ intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY,
+ mUserInfoSharedPreferences.getString(SharedPreferencesUtils.USER_KEY, ""));
+ startActivity(intent);
+ finish();
+ } else if(submitVideoPostEvent.errorProcessingVideo) {
+ Snackbar.make(coordinatorLayout, R.string.error_processing_video, Snackbar.LENGTH_SHORT).show();
+ } else {
+ mPostingSnackbar.dismiss();
+ mMemu.getItem(R.id.action_send_post_video_activity).setEnabled(true);
+ mMemu.getItem(R.id.action_send_post_video_activity).getIcon().setAlpha(255);
+ if (submitVideoPostEvent.errorMessage == null || submitVideoPostEvent.errorMessage.equals("")) {
+ Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show();
+ } else {
+ Snackbar.make(coordinatorLayout, submitVideoPostEvent.errorMessage, Snackbar.LENGTH_SHORT).show();
+ }
+ }
}
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitImagePostEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitImagePostEvent.java
new file mode 100644
index 00000000..3432628c
--- /dev/null
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitImagePostEvent.java
@@ -0,0 +1,11 @@
+package ml.docilealligator.infinityforreddit;
+
+public class SubmitImagePostEvent {
+ public boolean postSuccess;
+ public String errorMessage;
+
+ public SubmitImagePostEvent(boolean postSuccess, String errorMessage) {
+ this.postSuccess = postSuccess;
+ this.errorMessage = errorMessage;
+ }
+}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java
index 50a1107b..e2ce499c 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java
@@ -83,7 +83,6 @@ class SubmitPost {
uploadImageParams.put(RedditUtils.FILEPATH_KEY, "post_video." + fileType);
uploadImageParams.put(RedditUtils.MIMETYPE_KEY, mimeType);
- Log.i("map", RedditUtils.getOAuthHeader(accessToken).toString());
Call<String> uploadImageCall = api.uploadImage(RedditUtils.getOAuthHeader(accessToken), uploadImageParams);
uploadImageCall.enqueue(new Callback<String>() {
@Override
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitVideoPostEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitVideoPostEvent.java
new file mode 100644
index 00000000..6699135f
--- /dev/null
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitVideoPostEvent.java
@@ -0,0 +1,13 @@
+package ml.docilealligator.infinityforreddit;
+
+public class SubmitVideoPostEvent {
+ public boolean postSuccess;
+ public boolean errorProcessingVideo;
+ public String errorMessage;
+
+ public SubmitVideoPostEvent(boolean postSuccess, boolean errorProcessingVideo, String errorMessage) {
+ this.postSuccess = postSuccess;
+ this.errorProcessingVideo = errorProcessingVideo;
+ this.errorMessage = errorMessage;
+ }
+}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java
index d7799bfa..2381c59a 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java
@@ -63,7 +63,6 @@ public class ViewUserDetailActivity extends AppCompatActivity {
private SectionsPagerAdapter sectionsPagerAdapter;
- private Fragment mFragment;
private SubscribedUserDao subscribedUserDao;
private RequestManager glide;
private UserViewModel userViewModel;
@@ -330,15 +329,12 @@ public class ViewUserDetailActivity extends AppCompatActivity {
intent.putExtra(SearchActivity.EXTRA_SUBREDDIT_IS_USER, true);
intent.putExtra(SearchActivity.EXTRA_SEARCH_ONLY_SUBREDDITS, false);
startActivity(intent);
- break;
+ return true;
case R.id.action_refresh_view_user_detail_activity:
- if (mFragment instanceof FragmentCommunicator) {
- ((FragmentCommunicator) mFragment).refresh();
- return true;
- }
- break;
+ sectionsPagerAdapter.refresh();
+ return true;
case R.id.action_lazy_mode_view_user_detail_activity:
- MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_view_user_detail_activity);
+ /*MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_view_user_detail_activity);
if(isInLazyMode) {
isInLazyMode = false;
((FragmentCommunicator) mFragment).stopLazyMode();
@@ -354,7 +350,7 @@ public class ViewUserDetailActivity extends AppCompatActivity {
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
collapsingToolbarLayout.setLayoutParams(params);
}
- return true;
+ return true;*/
}
return false;
}
@@ -484,11 +480,10 @@ public class ViewUserDetailActivity extends AppCompatActivity {
}
public void refresh() {
- if(postFragment != null) {
- ((FragmentCommunicator) postFragment).refresh();
- }
- if(commentsListingFragment != null) {
- ((FragmentCommunicator) commentsListingFragment).refresh();
+ if(viewPager.getCurrentItem() == 0) {
+ postFragment.refresh();
+ } else {
+ commentsListingFragment.refresh();
}
}
}