Receiving images, videos and texts to directly submitting different kinds of posts. Fixed video was not shown after selected from picker in PostVideoActivity. Handle some cases that the posts cannot be submitted. Minor bugs fixed.

This commit is contained in:
Alex Ning 2019-08-28 16:41:36 +08:00
parent 30d2abe9c6
commit cc0d78aedd
13 changed files with 275 additions and 103 deletions

Binary file not shown.

View File

@ -2,15 +2,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ml.docilealligator.infinityforreddit"> package="ml.docilealligator.infinityforreddit">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission-sdk-23 android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="22" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application <application
android:name=".Infinity" android:name=".Infinity"
android:allowBackup="true" android:allowBackup="true"
@ -20,6 +11,23 @@
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme" android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"> android:usesCleartextTraffic="true">
<activity android:name=".ShareDataResolverActivity">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="video/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
<activity <activity
android:name=".SettingsActivity" android:name=".SettingsActivity"
android:label="@string/search_activity_label" android:label="@string/search_activity_label"
@ -47,15 +55,11 @@
android:theme="@style/AppTheme.NoActionBar" android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustResize" /> android:windowSoftInputMode="adjustResize" />
<service
android:name=".SubmitPostService"
android:enabled="true"
android:exported="false" />
<activity <activity
android:name=".FilteredThingActivity" android:name=".FilteredThingActivity"
android:parentActivityName=".MainActivity" android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBar" /> android:theme="@style/AppTheme.NoActionBar" />
<activity <activity
android:name=".SearchSubredditsResultActivity" android:name=".SearchSubredditsResultActivity"
android:label="@string/search_subreddits_activity_label" android:label="@string/search_subreddits_activity_label"
@ -172,7 +176,6 @@
android:name=".ViewUserDetailActivity" android:name=".ViewUserDetailActivity"
android:parentActivityName=".MainActivity" android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBarWithTranslucentWindow" /> android:theme="@style/AppTheme.NoActionBarWithTranslucentWindow" />
<provider <provider
android:name="androidx.core.content.FileProvider" android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider" android:authorities="${applicationId}.provider"
@ -182,6 +185,20 @@
android:name="android.support.FILE_PROVIDER_PATHS" android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" /> android:resource="@xml/file_paths" />
</provider> </provider>
<service
android:name=".SubmitPostService"
android:enabled="true"
android:exported="false" />
</application> </application>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="22" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission-sdk-23 android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest> </manifest>

View File

@ -239,6 +239,11 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.into(iconGifImageView); .into(iconGifImageView);
} }
imageUri = getIntent().getData();
if(imageUri != null) {
loadImage();
}
} }
iconGifImageView.setOnClickListener(view -> { iconGifImageView.setOnClickListener(view -> {
@ -448,6 +453,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
intent.putExtra(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler); intent.putExtra(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler);
intent.putExtra(SubmitPostService.EXTRA_IS_NSFW, isNSFW); intent.putExtra(SubmitPostService.EXTRA_IS_NSFW, isNSFW);
intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TYPE_IMAGE); intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TYPE_IMAGE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent); startForegroundService(intent);
@ -563,7 +569,8 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
if (submitImagePostEvent.errorMessage == null || submitImagePostEvent.errorMessage.equals("")) { if (submitImagePostEvent.errorMessage == null || submitImagePostEvent.errorMessage.equals("")) {
Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show(); Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show();
} else { } else {
Snackbar.make(coordinatorLayout, submitImagePostEvent.errorMessage, Snackbar.LENGTH_SHORT).show(); Snackbar.make(coordinatorLayout, submitImagePostEvent.errorMessage.substring(0, 1).toUpperCase()
+ submitImagePostEvent.errorMessage.substring(1), Snackbar.LENGTH_SHORT).show();
} }
} }
} }

View File

@ -48,6 +48,7 @@ import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES;
public class PostLinkActivity extends AppCompatActivity implements FlairBottomSheetFragment.FlairSelectionCallback { public class PostLinkActivity extends AppCompatActivity implements FlairBottomSheetFragment.FlairSelectionCallback {
static final String EXTRA_SUBREDDIT_NAME = "ESN"; static final String EXTRA_SUBREDDIT_NAME = "ESN";
static final String EXTRA_LINK = "EL";
private static final String SUBREDDIT_NAME_STATE = "SNS"; private static final String SUBREDDIT_NAME_STATE = "SNS";
private static final String SUBREDDIT_ICON_STATE = "SIS"; private static final String SUBREDDIT_ICON_STATE = "SIS";
@ -210,6 +211,11 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.into(iconGifImageView); .into(iconGifImageView);
} }
String link = getIntent().getStringExtra(EXTRA_LINK);
if(link != null) {
contentEditText.setText(link);
}
} }
iconGifImageView.setOnClickListener(view -> { iconGifImageView.setOnClickListener(view -> {
@ -472,7 +478,8 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh
if(submitTextOrLinkPostEvent.errorMessage == null) { if(submitTextOrLinkPostEvent.errorMessage == null) {
Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show(); Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show();
} else { } else {
Snackbar.make(coordinatorLayout, submitTextOrLinkPostEvent.errorMessage, Snackbar.LENGTH_SHORT).show(); Snackbar.make(coordinatorLayout, submitTextOrLinkPostEvent.errorMessage.substring(0, 1).toUpperCase()
+ submitTextOrLinkPostEvent.errorMessage.substring(1), Snackbar.LENGTH_SHORT).show();
} }
} }
} }

View File

@ -48,6 +48,7 @@ import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES;
public class PostTextActivity extends AppCompatActivity implements FlairBottomSheetFragment.FlairSelectionCallback { public class PostTextActivity extends AppCompatActivity implements FlairBottomSheetFragment.FlairSelectionCallback {
static final String EXTRA_SUBREDDIT_NAME = "ESN"; static final String EXTRA_SUBREDDIT_NAME = "ESN";
static final String EXTRA_CONTENT = "EC";
private static final String SUBREDDIT_NAME_STATE = "SNS"; private static final String SUBREDDIT_NAME_STATE = "SNS";
private static final String SUBREDDIT_ICON_STATE = "SIS"; private static final String SUBREDDIT_ICON_STATE = "SIS";
@ -210,6 +211,11 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.into(iconGifImageView); .into(iconGifImageView);
} }
String text = getIntent().getStringExtra(EXTRA_CONTENT);
if(text != null) {
contentEditText.setText(text);
}
} }
iconGifImageView.setOnClickListener(view -> { iconGifImageView.setOnClickListener(view -> {
@ -472,7 +478,8 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh
if(submitTextOrLinkPostEvent.errorMessage == null) { if(submitTextOrLinkPostEvent.errorMessage == null) {
Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show(); Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show();
} else { } else {
Snackbar.make(coordinatorLayout, submitTextOrLinkPostEvent.errorMessage, Snackbar.LENGTH_SHORT).show(); Snackbar.make(coordinatorLayout, submitTextOrLinkPostEvent.errorMessage.substring(0, 1).toUpperCase()
+ submitTextOrLinkPostEvent.errorMessage.substring(1), Snackbar.LENGTH_SHORT).show();
} }
} }
} }

View File

@ -14,7 +14,6 @@ import android.view.Window;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
import android.widget.VideoView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -28,6 +27,14 @@ import androidx.core.content.ContextCompat;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager; import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.RequestOptions;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
@ -85,7 +92,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
@BindView(R.id.capture_fab_post_video_activity) FloatingActionButton captureFab; @BindView(R.id.capture_fab_post_video_activity) FloatingActionButton captureFab;
@BindView(R.id.select_from_library_fab_post_video_activity) FloatingActionButton selectFromLibraryFab; @BindView(R.id.select_from_library_fab_post_video_activity) FloatingActionButton selectFromLibraryFab;
@BindView(R.id.select_again_text_view_post_video_activity) TextView selectAgainTextView; @BindView(R.id.select_again_text_view_post_video_activity) TextView selectAgainTextView;
@BindView(R.id.video_view_post_video_activity) VideoView videoView; @BindView(R.id.player_view_post_video_activity) PlayerView videoPlayerView;
private boolean mNullAccessToken = false; private boolean mNullAccessToken = false;
private String mAccessToken; private String mAccessToken;
@ -97,6 +104,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
private Uri videoUri; private Uri videoUri;
private boolean loadSubredditIconSuccessful = true; private boolean loadSubredditIconSuccessful = true;
private boolean isPosting; private boolean isPosting;
private boolean wasPlaying;
private Flair flair; private Flair flair;
private boolean isSpoiler = false; private boolean isSpoiler = false;
@ -107,6 +115,9 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
private FlairBottomSheetFragment mFlairSelectionBottomSheetFragment; private FlairBottomSheetFragment mFlairSelectionBottomSheetFragment;
private Snackbar mPostingSnackbar; private Snackbar mPostingSnackbar;
private DataSource.Factory dataSourceFactory;
private SimpleExoPlayer player;
@Inject @Inject
@Named("no_oauth") @Named("no_oauth")
Retrofit mRetrofit; Retrofit mRetrofit;
@ -171,6 +182,12 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
mGlide = Glide.with(this); mGlide = Glide.with(this);
player = ExoPlayerFactory.newSimpleInstance(this);
videoPlayerView.setPlayer(player);
dataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, "Infinity"));
player.setRepeatMode(Player.REPEAT_MODE_ALL);
mPostingSnackbar = Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_INDEFINITE); mPostingSnackbar = Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_INDEFINITE);
if(savedInstanceState != null) { if(savedInstanceState != null) {
@ -237,6 +254,11 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.into(iconGifImageView); .into(iconGifImageView);
} }
videoUri = getIntent().getData();
if(videoUri != null) {
loadVideo();
}
} }
iconGifImageView.setOnClickListener(view -> { iconGifImageView.setOnClickListener(view -> {
@ -312,16 +334,12 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
startActivityForResult(Intent.createChooser(intent,getResources().getString(R.string.select_from_gallery)), PICK_VIDEO_REQUEST_CODE); startActivityForResult(Intent.createChooser(intent,getResources().getString(R.string.select_from_gallery)), PICK_VIDEO_REQUEST_CODE);
}); });
videoView.setOnPreparedListener(mediaPlayer -> {
mediaPlayer.setLooping(true);
mediaPlayer.setVolume(0, 0);
});
selectAgainTextView.setOnClickListener(view -> { selectAgainTextView.setOnClickListener(view -> {
wasPlaying = false;
player.setPlayWhenReady(false);
videoUri = null; videoUri = null;
videoPlayerView.setVisibility(View.GONE);
selectAgainTextView.setVisibility(View.GONE); selectAgainTextView.setVisibility(View.GONE);
videoView.stopPlayback();
videoView.setVisibility(View.GONE);
constraintLayout.setVisibility(View.VISIBLE); constraintLayout.setVisibility(View.VISIBLE);
}); });
} }
@ -339,10 +357,11 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
private void loadVideo() { private void loadVideo() {
constraintLayout.setVisibility(View.GONE); constraintLayout.setVisibility(View.GONE);
videoView.setVisibility(View.VISIBLE);
selectAgainTextView.setVisibility(View.VISIBLE); selectAgainTextView.setVisibility(View.VISIBLE);
videoView.setVideoURI(videoUri); videoPlayerView.setVisibility(View.VISIBLE);
videoView.start(); player.prepare(new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(videoUri));
player.setPlayWhenReady(true);
wasPlaying = true;
} }
private void displaySubredditIcon() { private void displaySubredditIcon() {
@ -442,6 +461,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
intent.putExtra(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler); intent.putExtra(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler);
intent.putExtra(SubmitPostService.EXTRA_IS_NSFW, isNSFW); intent.putExtra(SubmitPostService.EXTRA_IS_NSFW, isNSFW);
intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TYPE_VIDEO); intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TYPE_VIDEO);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent); startForegroundService(intent);
@ -468,10 +488,18 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
} }
} }
@Override
protected void onStart() {
super.onStart();
if(wasPlaying) {
player.setPlayWhenReady(true);
}
}
@Override @Override
protected void onStop() { protected void onStop() {
super.onStop(); super.onStop();
videoView.stopPlayback(); player.setPlayWhenReady(false);
} }
@Override @Override
@ -526,11 +554,13 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
loadVideo(); loadVideo();
} }
} else if (requestCode == CAPTURE_VIDEO_REQUEST_CODE) { } else if (requestCode == CAPTURE_VIDEO_REQUEST_CODE) {
if(data != null) { if(resultCode == RESULT_OK) {
videoUri = data.getData(); if(data != null && data.getData() != null) {
loadVideo(); videoUri = data.getData();
} else { loadVideo();
Snackbar.make(coordinatorLayout, R.string.error_getting_video, Snackbar.LENGTH_SHORT).show(); } else {
Snackbar.make(coordinatorLayout, R.string.error_getting_video, Snackbar.LENGTH_SHORT).show();
}
} }
} }
} }
@ -539,6 +569,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
protected void onDestroy() { protected void onDestroy() {
EventBus.getDefault().unregister(this); EventBus.getDefault().unregister(this);
super.onDestroy(); super.onDestroy();
player.release();
} }
@Override @Override
@ -572,7 +603,8 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
if (submitVideoPostEvent.errorMessage == null || submitVideoPostEvent.errorMessage.equals("")) { if (submitVideoPostEvent.errorMessage == null || submitVideoPostEvent.errorMessage.equals("")) {
Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show(); Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show();
} else { } else {
Snackbar.make(coordinatorLayout, submitVideoPostEvent.errorMessage, Snackbar.LENGTH_SHORT).show(); Snackbar.make(coordinatorLayout, submitVideoPostEvent.errorMessage.substring(0, 1).toUpperCase()
+ submitVideoPostEvent.errorMessage.substring(1), Snackbar.LENGTH_SHORT).show();
} }
} }
} }

View File

@ -0,0 +1,77 @@
package ml.docilealligator.infinityforreddit;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Patterns;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class ShareDataResolverActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent receivedIntent = getIntent();
String action = receivedIntent.getAction();
String type = receivedIntent.getType();
if(Intent.ACTION_SEND.equals(action) && type != null) {
if("text/plain".equals(type)) {
String text = receivedIntent.getStringExtra(Intent.EXTRA_TEXT);
if(text != null) {
if(Patterns.WEB_URL.matcher(text).matches()) {
//It's a link
Intent intent = new Intent(this, PostLinkActivity.class);
intent.putExtra(PostLinkActivity.EXTRA_LINK, text);
startActivity(intent);
} else {
Intent intent = new Intent(this, PostTextActivity.class);
intent.putExtra(PostTextActivity.EXTRA_CONTENT, text);
startActivity(intent);
}
} else {
Toast.makeText(this, R.string.no_data_received, Toast.LENGTH_SHORT).show();
}
} else if(type.equals("image/gif")) {
Uri videoUri = receivedIntent.getParcelableExtra(Intent.EXTRA_STREAM);
if(videoUri != null) {
Intent intent = new Intent(this, PostVideoActivity.class);
intent.setData(videoUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
} else {
Toast.makeText(this, R.string.no_video_path_received, Toast.LENGTH_SHORT).show();
}
} else if(type.startsWith("image/")) {
Uri imageUri = receivedIntent.getParcelableExtra(Intent.EXTRA_STREAM);
if(imageUri != null) {
Intent intent = new Intent(this, PostImageActivity.class);
intent.setData(imageUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
} else {
Toast.makeText(this, R.string.no_image_path_received, Toast.LENGTH_SHORT).show();
}
} else if(type.startsWith("video/")) {
Uri videoUri = receivedIntent.getParcelableExtra(Intent.EXTRA_STREAM);
if(videoUri != null) {
Intent intent = new Intent(this, PostVideoActivity.class);
intent.setData(videoUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
} else {
Toast.makeText(this, R.string.no_video_path_received, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, R.string.cannot_handle_intent, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, R.string.cannot_handle_intent, Toast.LENGTH_SHORT).show();
}
finish();
}
}

View File

@ -135,7 +135,7 @@ class SubmitPost {
} }
}).execute(); }).execute();
} else { } else {
submitPostListener.submitFailed("Error: " + response.code()); submitPostListener.submitFailed(response.message());
} }
} }
@ -234,7 +234,6 @@ class SubmitPost {
uploadImageParams.put(RedditUtils.FILEPATH_KEY, "post_image.jpg"); uploadImageParams.put(RedditUtils.FILEPATH_KEY, "post_image.jpg");
uploadImageParams.put(RedditUtils.MIMETYPE_KEY, "image/jpeg"); uploadImageParams.put(RedditUtils.MIMETYPE_KEY, "image/jpeg");
Log.i("map", RedditUtils.getOAuthHeader(accessToken).toString());
Call<String> uploadImageCall = api.uploadImage(RedditUtils.getOAuthHeader(accessToken), uploadImageParams); Call<String> uploadImageCall = api.uploadImage(RedditUtils.getOAuthHeader(accessToken), uploadImageParams);
uploadImageCall.enqueue(new Callback<String>() { uploadImageCall.enqueue(new Callback<String>() {
@Override @Override
@ -417,7 +416,6 @@ class SubmitPost {
} else { } else {
errorString = error.getString(0); errorString = error.getString(0);
} }
errorString = errorString.substring(0, 1).toUpperCase() + errorString.substring(1);
submitPostListener.submitFailed(errorString); submitPostListener.submitFailed(errorString);
} else { } else {
submitPostListener.submitFailed(null); submitPostListener.submitFailed(null);

View File

@ -16,6 +16,7 @@ import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget; import com.bumptech.glide.request.target.CustomTarget;
@ -93,13 +94,13 @@ public class SubmitPostService extends Service {
NotificationManager.IMPORTANCE_LOW NotificationManager.IMPORTANCE_LOW
); );
NotificationManager manager = getSystemService(NotificationManager.class); NotificationManagerCompat manager = NotificationManagerCompat.from(this);
manager.createNotificationChannel(serviceChannel); manager.createNotificationChannel(serviceChannel);
} }
if(postType == EXTRA_POST_TEXT_OR_LINK) { if(postType == EXTRA_POST_TEXT_OR_LINK) {
content = intent.getExtras().getString(EXTRA_CONTENT); content = intent.getStringExtra(EXTRA_CONTENT);
kind = intent.getExtras().getString(EXTRA_KIND); kind = intent.getStringExtra(EXTRA_KIND);
startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID, createNotification(R.string.posting)); startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID, createNotification(R.string.posting));
submitTextOrLinkPost(); submitTextOrLinkPost();
} else if(postType == EXTRA_POST_TYPE_IMAGE) { } else if(postType == EXTRA_POST_TYPE_IMAGE) {
@ -171,6 +172,13 @@ public class SubmitPostService extends Service {
}); });
} }
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
EventBus.getDefault().post(new SubmitImagePostEvent(false, getString(R.string.error_processing_image)));
stopService();
}
@Override @Override
public void onLoadCleared(@Nullable Drawable placeholder) { public void onLoadCleared(@Nullable Drawable placeholder) {
@ -180,43 +188,59 @@ public class SubmitPostService extends Service {
private void submitVideoPost() { private void submitVideoPost() {
try (ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(mediaUri, "r")) { try (ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(mediaUri, "r")) {
FileInputStream in = new FileInputStream(pfd.getFileDescriptor()); if(pfd != null) {
byte[] buffer; FileInputStream in = new FileInputStream(pfd.getFileDescriptor());
buffer = new byte[in.available()]; byte[] buffer;
while (in.read(buffer) != -1); buffer = new byte[in.available()];
while (in.read(buffer) != -1);
Glide.with(this) Glide.with(this)
.asBitmap() .asBitmap()
.load(mediaUri) .load(mediaUri)
.into(new CustomTarget<Bitmap>() { .into(new CustomTarget<Bitmap>() {
@Override @Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
SubmitPost.submitVideoPost(mOauthRetrofit, mUploadMediaRetrofit, mUploadVideoRetrofit, String type = getContentResolver().getType(mediaUri);
mAccessToken, getResources().getConfiguration().locale, subredditName, title, if(type != null) {
buffer, getContentResolver().getType(mediaUri), resource, flair, isSpoiler, isNSFW, SubmitPost.submitVideoPost(mOauthRetrofit, mUploadMediaRetrofit, mUploadVideoRetrofit,
new SubmitPost.SubmitPostListener() { mAccessToken, getResources().getConfiguration().locale, subredditName, title,
@Override buffer, type, resource, flair, isSpoiler, isNSFW,
public void submitSuccessful(Post post) { new SubmitPost.SubmitPostListener() {
EventBus.getDefault().post(new SubmitVideoPostEvent(true, false, null)); @Override
Toast.makeText(SubmitPostService.this, R.string.video_is_processing, Toast.LENGTH_SHORT).show(); 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(); stopService();
} }
@Override @Override
public void submitFailed(@Nullable String errorMessage) { public void submitFailed(@Nullable String errorMessage) {
EventBus.getDefault().post(new SubmitVideoPostEvent(false, false, errorMessage)); EventBus.getDefault().post(new SubmitVideoPostEvent(false, false, errorMessage));
stopService(); stopService();
} }
}); });
} } else {
EventBus.getDefault().post(new SubmitVideoPostEvent(false, true, null));
}
}
@Override @Override
public void onLoadCleared(@Nullable Drawable placeholder) { 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) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
EventBus.getDefault().post(new SubmitVideoPostEvent(false, true, null)); EventBus.getDefault().post(new SubmitVideoPostEvent(false, true, null));

View File

@ -44,7 +44,6 @@ import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection; import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.trackselection.TrackSelector; import com.google.android.exoplayer2.trackselection.TrackSelector;
import com.google.android.exoplayer2.ui.PlayerControlView;
import com.google.android.exoplayer2.ui.PlayerView; import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter; import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
@ -257,25 +256,22 @@ public class ViewVideoActivity extends AppCompatActivity {
} }
}); });
videoPlayerView.setControllerVisibilityListener(new PlayerControlView.VisibilityListener() { videoPlayerView.setControllerVisibilityListener(visibility -> {
@Override switch (visibility) {
public void onVisibilityChange(int visibility) { case View.GONE:
switch (visibility) { getWindow().getDecorView().setSystemUiVisibility(
case View.GONE: View.SYSTEM_UI_FLAG_LAYOUT_STABLE
getWindow().getDecorView().setSystemUiVisibility( | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE);
| View.SYSTEM_UI_FLAG_FULLSCREEN break;
| View.SYSTEM_UI_FLAG_IMMERSIVE); case View.VISIBLE:
break; getWindow().getDecorView().setSystemUiVisibility(
case View.VISIBLE: View.SYSTEM_UI_FLAG_LAYOUT_STABLE
getWindow().getDecorView().setSystemUiVisibility( | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
} }
}); });
@ -371,7 +367,7 @@ public class ViewVideoActivity extends AppCompatActivity {
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE && grantResults.length > 0) { if(requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE && grantResults.length > 0) {
if(grantResults[0] == PackageManager.PERMISSION_DENIED) { if(grantResults[0] == PackageManager.PERMISSION_DENIED) {
Toast.makeText(this, "No storage permission to save this file", Toast.LENGTH_SHORT).show(); Toast.makeText(this, R.string.no_storage_permission, Toast.LENGTH_SHORT).show();
} else if(grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) { } else if(grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) {
download(); download();
} }

View File

@ -184,11 +184,12 @@
android:textColor="@color/colorAccent" android:textColor="@color/colorAccent"
android:visibility="gone" /> android:visibility="gone" />
<VideoView <com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/video_view_post_video_activity" android:id="@+id/player_view_post_video_activity"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="400dp"
android:visibility="gone"/> android:visibility="gone"
app:controller_layout_id="@layout/exo_playback_control_view" />
</LinearLayout> </LinearLayout>

View File

@ -118,7 +118,8 @@
<string name="select_an_image">Please select an image first</string> <string name="select_an_image">Please select an image first</string>
<string name="posting">Posting</string> <string name="posting">Posting</string>
<string name="post_failed">Could not post it</string> <string name="post_failed">Could not post it</string>
<string name="error_processing_video">Error processing this video</string> <string name="error_processing_image">Error processing image</string>
<string name="error_processing_video">Error processing video</string>
<string name="download_completed">Download completed</string> <string name="download_completed">Download completed</string>
<string name="download_failed">Download Failed</string> <string name="download_failed">Download Failed</string>
@ -197,8 +198,8 @@
<string name="best">Best</string> <string name="best">Best</string>
<string name="search">Search</string> <string name="search">Search</string>
<string name="posting_video">Posting video</string> <string name="posting_video">Posting Video</string>
<string name="posting_image">Posting image</string> <string name="posting_image">Posting Image</string>
<string name="please_wait">Please wait.</string> <string name="please_wait">Please wait.</string>
<string name="add_account">Add account</string> <string name="add_account">Add account</string>
@ -283,7 +284,12 @@
<string name="exit_when_submit_post">Leave?</string> <string name="exit_when_submit_post">Leave?</string>
<string name="exit_when_submit_post_detail">The post will still be submitted even if you leave here.</string> <string name="exit_when_submit_post_detail">The post will still be submitted even if you leave here.</string>
<string name="discard_post">Discard?</string> <string name="discard_post">Discard?</string>
<string name="discard_post_detail">All the draft will not be saved.</string> <string name="discard_post_detail">All the draft will NOT be saved.</string>
<string name="yes">Yes</string> <string name="yes">Yes</string>
<string name="no">No</string> <string name="no">No</string>
<string name="no_data_received">No data received</string>
<string name="no_image_path_received">No image path received</string>
<string name="no_video_path_received">No video path received</string>
<string name="cannot_handle_intent">Could not handle the share request</string>
</resources> </resources>