Fix crash when downloading media. Version 4.1.0-beta6.

This commit is contained in:
Alex Ning 2020-11-20 00:08:54 +08:00
parent 1a8e888243
commit 121da39592
4 changed files with 59 additions and 59 deletions

View File

@ -6,8 +6,8 @@ android {
applicationId "ml.docilealligator.infinityforreddit" applicationId "ml.docilealligator.infinityforreddit"
minSdkVersion 21 minSdkVersion 21
targetSdkVersion 30 targetSdkVersion 30
versionCode 48 versionCode 49
versionName "4.1.0-beta5" versionName "4.1.0-beta6"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
javaCompileOptions { javaCompileOptions {
annotationProcessorOptions { annotationProcessorOptions {

View File

@ -33,12 +33,12 @@ import java.io.OutputStream;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import ml.docilealligator.infinityforreddit.apis.DownloadFile;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.DownloadProgressResponseBody; import ml.docilealligator.infinityforreddit.DownloadProgressResponseBody;
import ml.docilealligator.infinityforreddit.events.DownloadMediaEvent;
import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.apis.DownloadFile;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.events.DownloadMediaEvent;
import ml.docilealligator.infinityforreddit.utils.NotificationUtils; import ml.docilealligator.infinityforreddit.utils.NotificationUtils;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
@ -133,9 +133,6 @@ public class DownloadMediaService extends Service {
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
((Infinity) getApplication()).getAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
notificationManager = NotificationManagerCompat.from(this);
builder = new NotificationCompat.Builder(this, getNotificationChannelId());
downloadFinished = false; downloadFinished = false;
String fileUrl = intent.getStringExtra(EXTRA_URL); String fileUrl = intent.getStringExtra(EXTRA_URL);
final String[] fileName = {intent.getStringExtra(EXTRA_FILE_NAME)}; final String[] fileName = {intent.getStringExtra(EXTRA_FILE_NAME)};
@ -143,32 +140,8 @@ public class DownloadMediaService extends Service {
mediaType = intent.getIntExtra(EXTRA_MEDIA_TYPE, EXTRA_MEDIA_TYPE_IMAGE); mediaType = intent.getIntExtra(EXTRA_MEDIA_TYPE, EXTRA_MEDIA_TYPE_IMAGE);
mimeType = mediaType == EXTRA_MEDIA_TYPE_VIDEO ? "video/*" : "image/*"; mimeType = mediaType == EXTRA_MEDIA_TYPE_VIDEO ? "video/*" : "image/*";
final DownloadProgressResponseBody.ProgressListener progressListener = new DownloadProgressResponseBody.ProgressListener() { notificationManager = NotificationManagerCompat.from(this);
long time = 0; builder = new NotificationCompat.Builder(this, getNotificationChannelId());
@Override public void update(long bytesRead, long contentLength, boolean done) {
if (!done) {
if (contentLength != -1) {
long currentTime = System.currentTimeMillis();
if (currentTime - time > 1000) {
time = currentTime;
updateNotification(0, (int) ((100 * bytesRead) / contentLength), null);
}
}
}
}
};
OkHttpClient client = new OkHttpClient.Builder()
.addNetworkInterceptor(chain -> {
okhttp3.Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder()
.body(new DownloadProgressResponseBody(originalResponse.body(), progressListener))
.build();
})
.build();
retrofit = retrofit.newBuilder().client(client).build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel; NotificationChannel serviceChannel;
@ -201,6 +174,33 @@ public class DownloadMediaService extends Service {
); );
} }
final DownloadProgressResponseBody.ProgressListener progressListener = new DownloadProgressResponseBody.ProgressListener() {
long time = 0;
@Override public void update(long bytesRead, long contentLength, boolean done) {
if (!done) {
if (contentLength != -1) {
long currentTime = System.currentTimeMillis();
if (currentTime - time > 1000) {
time = currentTime;
updateNotification(0, (int) ((100 * bytesRead) / contentLength), null);
}
}
}
}
};
OkHttpClient client = new OkHttpClient.Builder()
.addNetworkInterceptor(chain -> {
okhttp3.Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder()
.body(new DownloadProgressResponseBody(originalResponse.body(), progressListener))
.build();
})
.build();
retrofit = retrofit.newBuilder().client(client).build();
boolean separateDownloadFolder = mSharedPreferences.getBoolean(SharedPreferencesUtils.SEPARATE_FOLDER_FOR_EACH_SUBREDDIT, false); boolean separateDownloadFolder = mSharedPreferences.getBoolean(SharedPreferencesUtils.SEPARATE_FOLDER_FOR_EACH_SUBREDDIT, false);
retrofit.create(DownloadFile.class).downloadFile(fileUrl).enqueue(new Callback<ResponseBody>() { retrofit.create(DownloadFile.class).downloadFile(fileUrl).enqueue(new Callback<ResponseBody>() {

View File

@ -39,12 +39,12 @@ import java.nio.ByteBuffer;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import ml.docilealligator.infinityforreddit.apis.DownloadFile;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.DownloadProgressResponseBody; import ml.docilealligator.infinityforreddit.DownloadProgressResponseBody;
import ml.docilealligator.infinityforreddit.events.DownloadRedditVideoEvent;
import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.apis.DownloadFile;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.events.DownloadRedditVideoEvent;
import ml.docilealligator.infinityforreddit.utils.NotificationUtils; import ml.docilealligator.infinityforreddit.utils.NotificationUtils;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
@ -95,9 +95,30 @@ public class DownloadRedditVideoService extends Service {
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
((Infinity) getApplication()).getAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
String videoUrl = intent.getStringExtra(EXTRA_VIDEO_URL);
String audioUrl = videoUrl.substring(0, videoUrl.lastIndexOf('/')) + "/DASH_audio.mp4";
String subredditName = intent.getStringExtra(EXTRA_SUBREDDIT);
final String[] fileNameWithoutExtension = {subredditName + "-" + intent.getStringExtra(EXTRA_POST_ID)};
notificationManager = NotificationManagerCompat.from(this); notificationManager = NotificationManagerCompat.from(this);
builder = new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO); builder = new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel;
serviceChannel = new NotificationChannel(
NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO,
NotificationUtils.CHANNEL_DOWNLOAD_REDDIT_VIDEO,
NotificationManager.IMPORTANCE_LOW
);
notificationManager.createNotificationChannel(serviceChannel);
}
startForeground(
NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID,
createNotification(fileNameWithoutExtension[0] + ".mp4")
);
final DownloadProgressResponseBody.ProgressListener progressListener = new DownloadProgressResponseBody.ProgressListener() { final DownloadProgressResponseBody.ProgressListener progressListener = new DownloadProgressResponseBody.ProgressListener() {
long time = 0; long time = 0;
@ -125,27 +146,6 @@ public class DownloadRedditVideoService extends Service {
retrofit = retrofit.newBuilder().client(client).build(); retrofit = retrofit.newBuilder().client(client).build();
String videoUrl = intent.getStringExtra(EXTRA_VIDEO_URL);
String audioUrl = videoUrl.substring(0, videoUrl.lastIndexOf('/')) + "/DASH_audio.mp4";
String subredditName = intent.getStringExtra(EXTRA_SUBREDDIT);
final String[] fileNameWithoutExtension = {subredditName + "-" + intent.getStringExtra(EXTRA_POST_ID)};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel;
serviceChannel = new NotificationChannel(
NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO,
NotificationUtils.CHANNEL_DOWNLOAD_REDDIT_VIDEO,
NotificationManager.IMPORTANCE_LOW
);
notificationManager.createNotificationChannel(serviceChannel);
}
startForeground(
NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID,
createNotification(fileNameWithoutExtension[0] + ".mp4")
);
DownloadFile downloadFile = retrofit.create(DownloadFile.class); DownloadFile downloadFile = retrofit.create(DownloadFile.class);
boolean separateDownloadFolder = sharedPreferences.getBoolean(SharedPreferencesUtils.SEPARATE_FOLDER_FOR_EACH_SUBREDDIT, false); boolean separateDownloadFolder = sharedPreferences.getBoolean(SharedPreferencesUtils.SEPARATE_FOLDER_FOR_EACH_SUBREDDIT, false);

View File

@ -41,7 +41,7 @@ public class APIUtils {
public static final String AUTHORIZATION_KEY = "Authorization"; public static final String AUTHORIZATION_KEY = "Authorization";
public static final String AUTHORIZATION_BASE = "bearer "; public static final String AUTHORIZATION_BASE = "bearer ";
public static final String USER_AGENT_KEY = "User-Agent"; public static final String USER_AGENT_KEY = "User-Agent";
public static final String USER_AGENT = "android:ml.docilealligator.infinityforreddit:v4.1.0-beta5 (by /u/Hostilenemy)"; public static final String USER_AGENT = "android:ml.docilealligator.infinityforreddit:v4.1.0-beta6 (by /u/Hostilenemy)";
public static final String GRANT_TYPE_KEY = "grant_type"; public static final String GRANT_TYPE_KEY = "grant_type";
public static final String GRANT_TYPE_REFRESH_TOKEN = "refresh_token"; public static final String GRANT_TYPE_REFRESH_TOKEN = "refresh_token";