diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 000000000..2c79d62cd --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +liberapay: TeamNewPipe diff --git a/.gitignore b/.gitignore index b1746282e..f4f47c5ee 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,6 @@ /app/app.iml /.idea /*.iml -gradle.properties *~ .weblate *.class diff --git a/README.md b/README.md index 6c1aa3d4b..d4952b2b8 100644 --- a/README.md +++ b/README.md @@ -66,15 +66,22 @@ NewPipe does not use any Google framework libraries, nor the YouTube API. Websit * Enqueue videos * Local playlists * Subtitles -* Multi-service support (e.g. SoundCloud \[beta\]) * Livestream support +* Show comments ### Coming Features * Cast to UPnP and Cast -* Show comments * … and many more +### Supported Services + +NewPipe supports multiple services. Our [docs](https://teamnewpipe.github.io/documentation/) provide more info on how a new service can be added to the app and the extractor. Please get in touch with us if you intend to add a new one. Currently supported services are: + +* YouTube +* SoundCloud \[beta\] +* media.ccc.de \[beta\] + ## Updates When a change to the NewPipe code occurs (due to either adding features or bug fixing), eventually a release will occur. These are in the format x.xx.x . In order to get this new version, you can: * Build a debug APK yourself. This is the fastest way to get new features on your device, but is much more complicated, so we recommend using one of the other methods. diff --git a/app/build.gradle b/app/build.gradle index d037127d9..0152c675a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,4 +1,7 @@ apply plugin: 'com.android.application' +apply plugin: 'kotlin-android' +apply plugin: 'kotlin-android-extensions' +apply plugin: 'kotlin-kapt' android { compileSdkVersion 28 @@ -8,10 +11,10 @@ android { applicationId "org.schabi.newpipe" minSdkVersion 19 targetSdkVersion 28 - versionCode 720 - versionName "0.16.0" + versionCode 790 + versionName "0.17.4" - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true } @@ -43,38 +46,43 @@ android { } ext { - supportLibVersion = '28.0.0' - exoPlayerLibVersion = '2.8.4' //2.9.0 - roomDbLibVersion = '1.1.1' + androidxLibVersion = '1.0.0' + exoPlayerLibVersion = '2.10.7' + roomDbLibVersion = '2.1.0' leakCanaryLibVersion = '1.5.4' //1.6.1 - okHttpLibVersion = '3.11.0' + okHttpLibVersion = '3.12.6' icepickLibVersion = '3.2.0' stethoLibVersion = '1.5.0' } dependencies { - androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.2', { + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + + androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', { exclude module: 'support-annotations' }) - implementation 'com.github.yausername:NewPipeExtractor:f60c973' + implementation 'com.github.yausername:NewPipeExtractor:318f600' testImplementation 'junit:junit:4.12' testImplementation 'org.mockito:mockito-core:2.23.0' - implementation "com.android.support:appcompat-v7:${supportLibVersion}" - implementation "com.android.support:support-v4:${supportLibVersion}" - implementation "com.android.support:design:${supportLibVersion}" - implementation "com.android.support:recyclerview-v7:${supportLibVersion}" - implementation "com.android.support:preference-v14:${supportLibVersion}" - implementation "com.android.support:cardview-v7:${supportLibVersion}" - implementation 'com.android.support.constraint:constraint-layout:1.1.3' + implementation 'androidx.appcompat:appcompat:1.1.0' + implementation "androidx.legacy:legacy-support-v4:${androidxLibVersion}" + implementation "com.google.android.material:material:${androidxLibVersion}" + implementation "androidx.recyclerview:recyclerview:${androidxLibVersion}" + implementation "androidx.legacy:legacy-preference-v14:${androidxLibVersion}" + implementation "androidx.cardview:cardview:${androidxLibVersion}" + implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + + // Originally in NewPipeExtractor + implementation 'com.grack:nanojson:1.1' + implementation 'org.jsoup:jsoup:1.9.2' implementation 'ch.acra:acra:4.9.2' //4.11 implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' implementation 'de.hdodenhof:circleimageview:2.2.0' - implementation 'com.github.nirhart:ParallaxScroll:dd53d1f9d1' implementation 'com.nononsenseapps:filepicker:4.2.1' implementation "com.google.android.exoplayer:exoplayer:${exoPlayerLibVersion}" @@ -82,18 +90,18 @@ dependencies { debugImplementation "com.facebook.stetho:stetho:${stethoLibVersion}" debugImplementation "com.facebook.stetho:stetho-urlconnection:${stethoLibVersion}" - debugImplementation 'com.android.support:multidex:1.0.3' + debugImplementation 'androidx.multidex:multidex:2.0.1' implementation 'io.reactivex.rxjava2:rxjava:2.2.2' implementation 'io.reactivex.rxjava2:rxandroid:2.1.0' implementation 'com.jakewharton.rxbinding2:rxbinding:2.1.1' - implementation "android.arch.persistence.room:runtime:${roomDbLibVersion}" - implementation "android.arch.persistence.room:rxjava2:${roomDbLibVersion}" - annotationProcessor "android.arch.persistence.room:compiler:${roomDbLibVersion}" + implementation "androidx.room:room-runtime:${roomDbLibVersion}" + implementation "androidx.room:room-rxjava2:${roomDbLibVersion}" + kapt "androidx.room:room-compiler:${roomDbLibVersion}" implementation "frankiesardo:icepick:${icepickLibVersion}" - annotationProcessor "frankiesardo:icepick-processor:${icepickLibVersion}" + kapt "frankiesardo:icepick-processor:${icepickLibVersion}" debugImplementation "com.squareup.leakcanary:leakcanary-android:${leakCanaryLibVersion}" releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:${leakCanaryLibVersion}" diff --git a/app/src/androidTest/java/org/schabi/newpipe/report/ErrorInfoTest.java b/app/src/androidTest/java/org/schabi/newpipe/report/ErrorInfoTest.java index cdeb47483..6e51136c0 100644 --- a/app/src/androidTest/java/org/schabi/newpipe/report/ErrorInfoTest.java +++ b/app/src/androidTest/java/org/schabi/newpipe/report/ErrorInfoTest.java @@ -1,8 +1,8 @@ package org.schabi.newpipe.report; import android.os.Parcel; -import android.support.test.filters.LargeTest; -import android.support.test.runner.AndroidJUnit4; +import androidx.test.filters.LargeTest; +import androidx.test.ext.junit.runners.AndroidJUnit4; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/app/src/debug/java/org/schabi/newpipe/DebugApp.java b/app/src/debug/java/org/schabi/newpipe/DebugApp.java index aff354a69..154fb5a8c 100644 --- a/app/src/debug/java/org/schabi/newpipe/DebugApp.java +++ b/app/src/debug/java/org/schabi/newpipe/DebugApp.java @@ -3,8 +3,8 @@ package org.schabi.newpipe; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; -import android.support.annotation.NonNull; -import android.support.multidex.MultiDex; +import androidx.annotation.NonNull; +import androidx.multidex.MultiDex; import com.facebook.stetho.Stetho; import com.facebook.stetho.okhttp3.StethoInterceptor; diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4cd8e83f9..9052dabab 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -29,7 +29,7 @@ - + @@ -115,7 +115,7 @@ android:label="@string/reCaptchaActivity"/> @@ -228,7 +228,20 @@ + + + + + + + + + + + + + diff --git a/app/src/main/java/android/support/design/widget/FlingBehavior.java b/app/src/main/java/android/support/design/widget/FlingBehavior.java deleted file mode 100644 index 59eb08294..000000000 --- a/app/src/main/java/android/support/design/widget/FlingBehavior.java +++ /dev/null @@ -1,116 +0,0 @@ -package android.support.design.widget; - -import android.animation.ValueAnimator; -import android.content.Context; -import android.support.annotation.NonNull; -import android.support.design.animation.AnimationUtils; -import android.util.AttributeSet; -import android.view.View; - -// check this https://github.com/ToDou/appbarlayout-spring-behavior/blob/master/appbarspring/src/main/java/android/support/design/widget/AppBarFlingFixBehavior.java -public final class FlingBehavior extends AppBarLayout.Behavior { - - private ValueAnimator mOffsetAnimator; - private static final int MAX_OFFSET_ANIMATION_DURATION = 600; // ms - - public FlingBehavior() { - } - - public FlingBehavior(Context context, AttributeSet attrs) { - super(context, attrs); - } - - @Override - public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed, int type) { - if (dy != 0) { - int val = child.getBottom(); - if (val != 0) { - int min, max; - if (dy < 0) { - // We're scrolling down - } else { - // We're scrolling up - if (mOffsetAnimator != null && mOffsetAnimator.isRunning()) { - mOffsetAnimator.cancel(); - } - min = -child.getUpNestedPreScrollRange(); - max = 0; - consumed[1] = scroll(coordinatorLayout, child, dy, min, max); - } - } - } - } - - @Override - public boolean onNestedPreFling(@NonNull CoordinatorLayout coordinatorLayout, @NonNull AppBarLayout child, @NonNull View target, float velocityX, float velocityY) { - - if (velocityY != 0) { - if (velocityY < 0) { - // We're flinging down - int val = child.getBottom(); - if (val != 0) { - final int targetScroll = - +child.getDownNestedPreScrollRange(); - animateOffsetTo(coordinatorLayout, child, targetScroll, velocityY); - } - - } else { - // We're flinging up - int val = child.getBottom(); - if (val != 0) { - final int targetScroll = -child.getUpNestedPreScrollRange(); - if (getTopBottomOffsetForScrollingSibling() > targetScroll) { - animateOffsetTo(coordinatorLayout, child, targetScroll, velocityY); - } - } - } - } - - return super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY); - } - - private void animateOffsetTo(final CoordinatorLayout coordinatorLayout, - final AppBarLayout child, final int offset, float velocity) { - final int distance = Math.abs(getTopBottomOffsetForScrollingSibling() - offset); - - final int duration; - velocity = Math.abs(velocity); - if (velocity > 0) { - duration = 3 * Math.round(1000 * (distance / velocity)); - } else { - final float distanceRatio = (float) distance / child.getHeight(); - duration = (int) ((distanceRatio + 1) * 150); - } - - animateOffsetWithDuration(coordinatorLayout, child, offset, duration); - } - - private void animateOffsetWithDuration(final CoordinatorLayout coordinatorLayout, - final AppBarLayout child, final int offset, final int duration) { - final int currentOffset = getTopBottomOffsetForScrollingSibling(); - if (currentOffset == offset) { - if (mOffsetAnimator != null && mOffsetAnimator.isRunning()) { - mOffsetAnimator.cancel(); - } - return; - } - - if (mOffsetAnimator == null) { - mOffsetAnimator = new ValueAnimator(); - mOffsetAnimator.setInterpolator(AnimationUtils.DECELERATE_INTERPOLATOR); - mOffsetAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { - @Override - public void onAnimationUpdate(ValueAnimator animator) { - setHeaderTopBottomOffset(coordinatorLayout, child, - (Integer) animator.getAnimatedValue()); - } - }); - } else { - mOffsetAnimator.cancel(); - } - - mOffsetAnimator.setDuration(Math.min(duration, MAX_OFFSET_ANIMATION_DURATION)); - mOffsetAnimator.setIntValues(currentOffset, offset); - mOffsetAnimator.start(); - } -} \ No newline at end of file diff --git a/app/src/main/java/com/google/android/material/appbar/FlingBehavior.java b/app/src/main/java/com/google/android/material/appbar/FlingBehavior.java new file mode 100644 index 000000000..4a2662f53 --- /dev/null +++ b/app/src/main/java/com/google/android/material/appbar/FlingBehavior.java @@ -0,0 +1,82 @@ +package com.google.android.material.appbar; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.widget.OverScroller; + +import androidx.annotation.Nullable; +import androidx.coordinatorlayout.widget.CoordinatorLayout; + +import java.lang.reflect.Field; + +// check this https://stackoverflow.com/questions/56849221/recyclerview-fling-causes-laggy-while-appbarlayout-is-scrolling/57997489#57997489 +public final class FlingBehavior extends AppBarLayout.Behavior { + + public FlingBehavior(Context context, AttributeSet attrs) { + super(context, attrs); + } + + @Override + public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) { + switch (ev.getActionMasked()) { + case MotionEvent.ACTION_DOWN: + // remove reference to old nested scrolling child + resetNestedScrollingChild(); + // Stop fling when your finger touches the screen + stopAppBarLayoutFling(); + break; + default: + break; + } + return super.onInterceptTouchEvent(parent, child, ev); + } + + @Nullable + private OverScroller getScrollerField() { + try { + Class headerBehaviorType = this.getClass().getSuperclass().getSuperclass().getSuperclass(); + if (headerBehaviorType != null) { + Field field = headerBehaviorType.getDeclaredField("scroller"); + field.setAccessible(true); + return ((OverScroller) field.get(this)); + } + } catch (NoSuchFieldException | IllegalAccessException e) { + // ? + } + return null; + } + + @Nullable + private Field getLastNestedScrollingChildRefField() { + try { + Class headerBehaviorType = this.getClass().getSuperclass().getSuperclass(); + if (headerBehaviorType != null) { + Field field = headerBehaviorType.getDeclaredField("lastNestedScrollingChildRef"); + field.setAccessible(true); + return field; + } + } catch (NoSuchFieldException e) { + // ? + } + return null; + } + + private void resetNestedScrollingChild(){ + Field field = getLastNestedScrollingChildRefField(); + if(field != null){ + try { + Object value = field.get(this); + if(value != null) field.set(this, null); + } catch (IllegalAccessException e) { + // ? + } + } + } + + private void stopAppBarLayoutFling() { + OverScroller scroller = getScrollerField(); + if (scroller != null) scroller.forceFinished(true); + } + +} \ No newline at end of file diff --git a/app/src/main/java/org/schabi/newpipe/App.java b/app/src/main/java/org/schabi/newpipe/App.java index fed38f1eb..02411b68c 100644 --- a/app/src/main/java/org/schabi/newpipe/App.java +++ b/app/src/main/java/org/schabi/newpipe/App.java @@ -6,7 +6,7 @@ import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.Context; import android.os.Build; -import android.support.annotation.Nullable; +import androidx.annotation.Nullable; import android.util.Log; import com.nostra13.universalimageloader.cache.memory.impl.LRULimitedMemoryCache; diff --git a/app/src/main/java/org/schabi/newpipe/BaseFragment.java b/app/src/main/java/org/schabi/newpipe/BaseFragment.java index 4e4cdcc0d..ccdb806ef 100644 --- a/app/src/main/java/org/schabi/newpipe/BaseFragment.java +++ b/app/src/main/java/org/schabi/newpipe/BaseFragment.java @@ -2,10 +2,10 @@ package org.schabi.newpipe; import android.content.Context; import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; -import android.support.v7.app.AppCompatActivity; +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.appcompat.app.AppCompatActivity; import android.util.Log; import android.view.View; diff --git a/app/src/main/java/org/schabi/newpipe/CheckForNewAppVersionTask.java b/app/src/main/java/org/schabi/newpipe/CheckForNewAppVersionTask.java index 6a6d1b9c2..22f7bc558 100644 --- a/app/src/main/java/org/schabi/newpipe/CheckForNewAppVersionTask.java +++ b/app/src/main/java/org/schabi/newpipe/CheckForNewAppVersionTask.java @@ -12,21 +12,18 @@ import android.net.ConnectivityManager; import android.net.Uri; import android.os.AsyncTask; import android.preference.PreferenceManager; -import android.support.v4.app.NotificationCompat; -import android.support.v4.app.NotificationManagerCompat; +import androidx.core.app.NotificationCompat; +import androidx.core.app.NotificationManagerCompat; +import android.util.Log; import org.json.JSONException; import org.json.JSONObject; import org.schabi.newpipe.report.ErrorActivity; import org.schabi.newpipe.report.UserAction; -import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateEncodingException; @@ -37,7 +34,6 @@ import java.util.concurrent.TimeUnit; import okhttp3.OkHttpClient; import okhttp3.Request; -import okhttp3.RequestBody; import okhttp3.Response; /** @@ -47,6 +43,8 @@ import okhttp3.Response; */ public class CheckForNewAppVersionTask extends AsyncTask { + private static final boolean DEBUG = MainActivity.DEBUG; + private static final String TAG = CheckForNewAppVersionTask.class.getSimpleName(); private static final Application app = App.getApp(); private static final String GITHUB_APK_SHA1 = "B0:2E:90:7C:1C:D6:FC:57:C3:35:F0:88:D0:8F:50:5F:94:E4:D2:15"; private static final String newPipeApiUrl = "https://newpipe.schabi.org/api/data.json"; @@ -90,9 +88,8 @@ public class CheckForNewAppVersionTask extends AsyncTask { Response response = client.newCall(request).execute(); return response.body().string(); } catch (IOException ex) { - ErrorActivity.reportError(app, ex, null, null, - ErrorActivity.ErrorInfo.make(UserAction.SOMETHING_ELSE, "none", - "app update API fail", R.string.app_ui_crash)); + // connectivity problems, do not alarm user and fail silently + if (DEBUG) Log.w(TAG, Log.getStackTraceString(ex)); } return null; @@ -117,9 +114,8 @@ public class CheckForNewAppVersionTask extends AsyncTask { compareAppVersionAndShowNotification(versionName, apkLocationUrl, versionCode); } catch (JSONException ex) { - ErrorActivity.reportError(app, ex, null, null, - ErrorActivity.ErrorInfo.make(UserAction.SOMETHING_ELSE, "none", - "could not parse app update JSON data", R.string.app_ui_crash)); + // connectivity problems, do not alarm user and fail silently + if (DEBUG) Log.w(TAG, Log.getStackTraceString(ex)); } } } diff --git a/app/src/main/java/org/schabi/newpipe/Downloader.java b/app/src/main/java/org/schabi/newpipe/Downloader.java index 531a52b78..43746f359 100644 --- a/app/src/main/java/org/schabi/newpipe/Downloader.java +++ b/app/src/main/java/org/schabi/newpipe/Downloader.java @@ -1,8 +1,10 @@ package org.schabi.newpipe; -import android.support.annotation.Nullable; import android.text.TextUtils; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + import org.schabi.newpipe.extractor.DownloadRequest; import org.schabi.newpipe.extractor.DownloadResponse; import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; @@ -10,6 +12,7 @@ import org.schabi.newpipe.extractor.utils.Localization; import java.io.IOException; import java.io.InputStream; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -23,6 +26,8 @@ import okhttp3.RequestBody; import okhttp3.Response; import okhttp3.ResponseBody; +import static java.util.Collections.singletonList; + /* * Created by Christian Schabesberger on 28.01.16. @@ -163,7 +168,7 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { final ResponseBody body = response.body(); if (response.code() == 429) { - throw new ReCaptchaException("reCaptcha Challenge requested"); + throw new ReCaptchaException("reCaptcha Challenge requested", siteUrl); } if (body == null) { @@ -213,7 +218,7 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { final ResponseBody body = response.body(); if (response.code() == 429) { - throw new ReCaptchaException("reCaptcha Challenge requested"); + throw new ReCaptchaException("reCaptcha Challenge requested", siteUrl); } if (body == null) { @@ -221,7 +226,7 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { return null; } - return new DownloadResponse(body.string(), response.headers().toMultimap()); + return new DownloadResponse(response.code(), body.string(), response.headers().toMultimap()); } @Override @@ -241,7 +246,7 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { String contentType = requestHeaders.get("Content-Type").get(0); RequestBody okRequestBody = null; - if(null != request.getRequestBody()){ + if (null != request.getRequestBody()) { okRequestBody = RequestBody.create(MediaType.parse(contentType), request.getRequestBody()); } final Request.Builder requestBuilder = new Request.Builder() @@ -267,7 +272,7 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { final ResponseBody body = response.body(); if (response.code() == 429) { - throw new ReCaptchaException("reCaptcha Challenge requested"); + throw new ReCaptchaException("reCaptcha Challenge requested", siteUrl); } if (body == null) { @@ -275,6 +280,30 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { return null; } - return new DownloadResponse(body.string(), response.headers().toMultimap()); + return new DownloadResponse(response.code(), body.string(), response.headers().toMultimap()); } + + @Override + public DownloadResponse head(String siteUrl) throws IOException, ReCaptchaException { + final Request request = new Request.Builder() + .head().url(siteUrl) + .addHeader("User-Agent", USER_AGENT) + .build(); + final Response response = client.newCall(request).execute(); + + if (response.code() == 429) { + throw new ReCaptchaException("reCaptcha Challenge requested", siteUrl); + } + + return new DownloadResponse(response.code(), null, response.headers().toMultimap()); + } + + @Override + public DownloadResponse get(String siteUrl, @NonNull Localization localization) throws IOException, ReCaptchaException { + final Map> requestHeaders = new HashMap<>(); + requestHeaders.put("Accept-Language", singletonList(localization.getLanguage())); + + return get(siteUrl, new DownloadRequest(null, requestHeaders)); + } + } \ No newline at end of file diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index 3821af896..82d4e4063 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -28,17 +28,7 @@ import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.preference.PreferenceManager; -import android.support.annotation.NonNull; -import android.support.design.widget.NavigationView; -import android.support.v4.app.Fragment; -import android.support.v4.view.GravityCompat; -import android.support.v4.widget.DrawerLayout; -import android.support.v7.app.ActionBar; -import android.support.v7.app.ActionBarDrawerToggle; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; import android.util.Log; -import android.view.Gravity; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; @@ -49,6 +39,17 @@ import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; +import androidx.annotation.NonNull; +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.ActionBarDrawerToggle; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import androidx.core.view.GravityCompat; +import androidx.drawerlayout.widget.DrawerLayout; +import androidx.fragment.app.Fragment; + +import com.google.android.material.navigation.NavigationView; + import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.exceptions.ExtractionException; @@ -73,6 +74,7 @@ public class MainActivity extends AppCompatActivity { private DrawerLayout drawer = null; private NavigationView drawerItems = null; private TextView headerServiceView = null; + private Button toggleServiceButton = null; private boolean servicesShown = false; private ImageView serviceArrow; @@ -266,8 +268,8 @@ public class MainActivity extends AppCompatActivity { serviceArrow = hView.findViewById(R.id.drawer_arrow); headerServiceView = hView.findViewById(R.id.drawer_header_service_view); - Button action = hView.findViewById(R.id.drawer_header_action_button); - action.setOnClickListener(view -> { + toggleServiceButton = hView.findViewById(R.id.drawer_header_action_button); + toggleServiceButton.setOnClickListener(view -> { toggleServices(); }); } @@ -279,6 +281,7 @@ public class MainActivity extends AppCompatActivity { drawerItems.getMenu().removeGroup(R.id.menu_tabs_group); drawerItems.getMenu().removeGroup(R.id.menu_options_about_group); + if(servicesShown) { showServices(); } else { @@ -359,12 +362,14 @@ public class MainActivity extends AppCompatActivity { // close drawer on return, and don't show animation, so its looks like the drawer isn't open // when the user returns to MainActivity - drawer.closeDrawer(Gravity.START, false); + drawer.closeDrawer(GravityCompat.START, false); try { String selectedServiceName = NewPipe.getService( ServiceHelper.getSelectedServiceId(this)).getServiceInfo().getName(); headerServiceView.setText(selectedServiceName); headerServiceView.post(() -> headerServiceView.setSelected(true)); + toggleServiceButton.setContentDescription( + getString(R.string.drawer_header_description) + selectedServiceName); } catch (Exception e) { ErrorActivity.reportUiError(this, e); } @@ -558,6 +563,14 @@ public class MainActivity extends AppCompatActivity { } } + private void updateDrawerHeaderString(String content) { + NavigationView navigationView = findViewById(R.id.navigation); + View hView = navigationView.getHeaderView(0); + Button action = hView.findViewById(R.id.drawer_header_action_button); + + action.setContentDescription(content); + } + private void handleIntent(Intent intent) { try { if (DEBUG) Log.d(TAG, "handleIntent() called with: intent = [" + intent + "]"); diff --git a/app/src/main/java/org/schabi/newpipe/NewPipeDatabase.java b/app/src/main/java/org/schabi/newpipe/NewPipeDatabase.java index 189e5aeab..f3356d6e8 100644 --- a/app/src/main/java/org/schabi/newpipe/NewPipeDatabase.java +++ b/app/src/main/java/org/schabi/newpipe/NewPipeDatabase.java @@ -1,8 +1,8 @@ package org.schabi.newpipe; -import android.arch.persistence.room.Room; +import androidx.room.Room; import android.content.Context; -import android.support.annotation.NonNull; +import androidx.annotation.NonNull; import org.schabi.newpipe.database.AppDatabase; diff --git a/app/src/main/java/org/schabi/newpipe/ReCaptchaActivity.java b/app/src/main/java/org/schabi/newpipe/ReCaptchaActivity.java index 74c818bf9..7f6af89c1 100644 --- a/app/src/main/java/org/schabi/newpipe/ReCaptchaActivity.java +++ b/app/src/main/java/org/schabi/newpipe/ReCaptchaActivity.java @@ -5,13 +5,12 @@ import android.content.Intent; import android.graphics.Bitmap; import android.os.Build; import android.os.Bundle; -import android.support.v4.app.NavUtils; -import android.support.v7.app.ActionBar; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; +import androidx.core.app.NavUtils; +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; import android.view.MenuItem; import android.webkit.CookieManager; -import android.webkit.ValueCallback; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; @@ -37,15 +36,24 @@ import android.webkit.WebViewClient; */ public class ReCaptchaActivity extends AppCompatActivity { public static final int RECAPTCHA_REQUEST = 10; + public static final String RECAPTCHA_URL_EXTRA = "recaptcha_url_extra"; public static final String TAG = ReCaptchaActivity.class.toString(); public static final String YT_URL = "https://www.youtube.com"; + private String url; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recaptcha); + url = getIntent().getStringExtra(RECAPTCHA_URL_EXTRA); + if (url == null || url.isEmpty()) { + url = YT_URL; + } + + // Set return to Cancel by default setResult(RESULT_CANCELED); @@ -73,15 +81,12 @@ public class ReCaptchaActivity extends AppCompatActivity { myWebView.clearHistory(); android.webkit.CookieManager cookieManager = CookieManager.getInstance(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - cookieManager.removeAllCookies(new ValueCallback() { - @Override - public void onReceiveValue(Boolean aBoolean) {} - }); + cookieManager.removeAllCookies(aBoolean -> {}); } else { cookieManager.removeAllCookie(); } - myWebView.loadUrl(YT_URL); + myWebView.loadUrl(url); } private class ReCaptchaWebViewClient extends WebViewClient { diff --git a/app/src/main/java/org/schabi/newpipe/RouterActivity.java b/app/src/main/java/org/schabi/newpipe/RouterActivity.java index f040dc8b4..1be6e096a 100644 --- a/app/src/main/java/org/schabi/newpipe/RouterActivity.java +++ b/app/src/main/java/org/schabi/newpipe/RouterActivity.java @@ -9,12 +9,12 @@ import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.os.Bundle; import android.preference.PreferenceManager; -import android.support.annotation.DrawableRes; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.app.NotificationCompat; -import android.support.v7.app.AlertDialog; -import android.support.v7.app.AppCompatActivity; +import androidx.annotation.DrawableRes; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.app.NotificationCompat; +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.app.AppCompatActivity; import android.text.TextUtils; import android.view.ContextThemeWrapper; import android.view.LayoutInflater; @@ -26,6 +26,8 @@ import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; +import androidx.fragment.app.FragmentManager; + import org.schabi.newpipe.download.DownloadDialog; import org.schabi.newpipe.extractor.Info; import org.schabi.newpipe.extractor.NewPipe; @@ -74,10 +76,13 @@ import static org.schabi.newpipe.util.ThemeHelper.resolveResourceIdFromAttr; */ public class RouterActivity extends AppCompatActivity { - @State protected int currentServiceId = -1; + @State + protected int currentServiceId = -1; private StreamingService currentService; - @State protected LinkType currentLinkType; - @State protected int selectedRadioPosition = -1; + @State + protected LinkType currentLinkType; + @State + protected int selectedRadioPosition = -1; protected int selectedPreviously = -1; protected String currentUrl; @@ -257,7 +262,7 @@ public class RouterActivity extends AppCompatActivity { .setNegativeButton(R.string.just_once, dialogButtonsClickListener) .setPositiveButton(R.string.always, dialogButtonsClickListener) .setOnDismissListener((dialog) -> { - if(!selectionIsDownload) finish(); + if (!selectionIsDownload) finish(); }) .create(); @@ -358,13 +363,13 @@ public class RouterActivity extends AppCompatActivity { positiveButton.setEnabled(state); } - private void handleText(){ + private void handleText() { String searchString = getIntent().getStringExtra(Intent.EXTRA_TEXT); int serviceId = getIntent().getIntExtra(Constants.KEY_SERVICE_ID, 0); Intent intent = new Intent(getThemeWrapperContext(), MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); - NavigationHelper.openSearch(getThemeWrapperContext(),serviceId,searchString); + NavigationHelper.openSearch(getThemeWrapperContext(), serviceId, searchString); } private void handleChoice(final String selectedChoiceKey) { @@ -397,7 +402,7 @@ public class RouterActivity extends AppCompatActivity { .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(intent -> { - if(!internalRoute){ + if (!internalRoute) { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); } @@ -430,7 +435,7 @@ public class RouterActivity extends AppCompatActivity { int selectedVideoStreamIndex = ListHelper.getDefaultResolutionIndex(this, sortedVideoStreams); - android.support.v4.app.FragmentManager fm = getSupportFragmentManager(); + FragmentManager fm = getSupportFragmentManager(); DownloadDialog downloadDialog = DownloadDialog.newInstance(result); downloadDialog.setVideoStreams(sortedVideoStreams); downloadDialog.setAudioStreams(result.getAudioStreams()); @@ -447,8 +452,8 @@ public class RouterActivity extends AppCompatActivity { @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { - for (int i: grantResults){ - if (i == PackageManager.PERMISSION_DENIED){ + for (int i : grantResults) { + if (i == PackageManager.PERMISSION_DENIED) { finish(); return; } @@ -460,7 +465,8 @@ public class RouterActivity extends AppCompatActivity { private static class AdapterChoiceItem { final String description, key; - @DrawableRes final int icon; + @DrawableRes + final int icon; AdapterChoiceItem(String key, String description, int icon) { this.description = description; @@ -558,7 +564,8 @@ public class RouterActivity extends AppCompatActivity { final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); boolean isExtVideoEnabled = preferences.getBoolean(getString(R.string.use_external_video_player_key), false); - boolean isExtAudioEnabled = preferences.getBoolean(getString(R.string.use_external_audio_player_key), false);; + boolean isExtAudioEnabled = preferences.getBoolean(getString(R.string.use_external_audio_player_key), false); + ; PlayQueue playQueue; String playerChoice = choice.playerChoice; @@ -574,7 +581,7 @@ public class RouterActivity extends AppCompatActivity { playQueue = new SinglePlayQueue((StreamInfo) info); if (playerChoice.equals(videoPlayerKey)) { - NavigationHelper.playOnMainPlayer(this, playQueue); + NavigationHelper.playOnMainPlayer(this, playQueue, true); } else if (playerChoice.equals(backgroundPlayerKey)) { NavigationHelper.enqueueOnBackgroundPlayer(this, playQueue, true); } else if (playerChoice.equals(popupPlayerKey)) { @@ -587,11 +594,11 @@ public class RouterActivity extends AppCompatActivity { playQueue = info instanceof ChannelInfo ? new ChannelPlayQueue((ChannelInfo) info) : new PlaylistPlayQueue((PlaylistInfo) info); if (playerChoice.equals(videoPlayerKey)) { - NavigationHelper.playOnMainPlayer(this, playQueue); + NavigationHelper.playOnMainPlayer(this, playQueue, true); } else if (playerChoice.equals(backgroundPlayerKey)) { - NavigationHelper.playOnBackgroundPlayer(this, playQueue); + NavigationHelper.playOnBackgroundPlayer(this, playQueue, true); } else if (playerChoice.equals(popupPlayerKey)) { - NavigationHelper.playOnPopupPlayer(this, playQueue); + NavigationHelper.playOnPopupPlayer(this, playQueue, true); } } }; diff --git a/app/src/main/java/org/schabi/newpipe/about/AboutActivity.java b/app/src/main/java/org/schabi/newpipe/about/AboutActivity.java index 0450290d2..2326e795e 100644 --- a/app/src/main/java/org/schabi/newpipe/about/AboutActivity.java +++ b/app/src/main/java/org/schabi/newpipe/about/AboutActivity.java @@ -4,13 +4,15 @@ import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; -import android.support.design.widget.TabLayout; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentPagerAdapter; -import android.support.v4.view.ViewPager; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; +import com.google.android.material.tabs.TabLayout; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentPagerAdapter; +import androidx.fragment.app.FragmentStatePagerAdapter; +import androidx.viewpager.widget.PagerAdapter; +import androidx.viewpager.widget.ViewPager; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; @@ -36,7 +38,6 @@ public class AboutActivity extends AppCompatActivity { new SoftwareComponent("ACRA", "2013", "Kevin Gaudin", "http://www.acra.ch", StandardLicenses.APACHE2), new SoftwareComponent("Universal Image Loader", "2011 - 2015", "Sergey Tarasevich", "https://github.com/nostra13/Android-Universal-Image-Loader", StandardLicenses.APACHE2), new SoftwareComponent("CircleImageView", "2014 - 2017", "Henning Dodenhof", "https://github.com/hdodenhof/CircleImageView", StandardLicenses.APACHE2), - new SoftwareComponent("ParalaxScrollView", "2014", "Nir Hartmann", "https://github.com/nirhart/ParallaxScroll", StandardLicenses.MIT), new SoftwareComponent("NoNonsense-FilePicker", "2016", "Jonas Kalderstam", "https://github.com/spacecowboy/NoNonsense-FilePicker", StandardLicenses.MPL2), new SoftwareComponent("ExoPlayer", "2014-2017", "Google Inc", "https://github.com/google/ExoPlayer", StandardLicenses.APACHE2), new SoftwareComponent("RxAndroid", "2015", "The RxAndroid authors", "https://github.com/ReactiveX/RxAndroid", StandardLicenses.APACHE2), @@ -45,12 +46,12 @@ public class AboutActivity extends AppCompatActivity { }; /** - * The {@link android.support.v4.view.PagerAdapter} that will provide + * The {@link PagerAdapter} that will provide * fragments for each of the sections. We use a * {@link FragmentPagerAdapter} derivative, which will keep every * loaded fragment in memory. If this becomes too memory intensive, it * may be best to switch to a - * {@link android.support.v4.app.FragmentStatePagerAdapter}. + * {@link FragmentStatePagerAdapter}. */ private SectionsPagerAdapter mSectionsPagerAdapter; diff --git a/app/src/main/java/org/schabi/newpipe/about/LicenseFragment.java b/app/src/main/java/org/schabi/newpipe/about/LicenseFragment.java index 4f6a69dcc..fe78ff9f1 100644 --- a/app/src/main/java/org/schabi/newpipe/about/LicenseFragment.java +++ b/app/src/main/java/org/schabi/newpipe/about/LicenseFragment.java @@ -5,8 +5,8 @@ import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; -import android.support.annotation.Nullable; -import android.support.v4.app.Fragment; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; import android.view.*; import android.widget.TextView; import org.schabi.newpipe.R; diff --git a/app/src/main/java/org/schabi/newpipe/about/LicenseFragmentHelper.java b/app/src/main/java/org/schabi/newpipe/about/LicenseFragmentHelper.java index 09d33bd8a..eeafc1f57 100644 --- a/app/src/main/java/org/schabi/newpipe/about/LicenseFragmentHelper.java +++ b/app/src/main/java/org/schabi/newpipe/about/LicenseFragmentHelper.java @@ -4,8 +4,8 @@ import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.os.AsyncTask; -import android.support.annotation.Nullable; -import android.support.v7.app.AlertDialog; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; import android.webkit.WebView; import org.schabi.newpipe.R; import org.schabi.newpipe.util.ThemeHelper; diff --git a/app/src/main/java/org/schabi/newpipe/database/AppDatabase.java b/app/src/main/java/org/schabi/newpipe/database/AppDatabase.java index 145a77c70..d374f254b 100644 --- a/app/src/main/java/org/schabi/newpipe/database/AppDatabase.java +++ b/app/src/main/java/org/schabi/newpipe/database/AppDatabase.java @@ -1,8 +1,8 @@ package org.schabi.newpipe.database; -import android.arch.persistence.room.Database; -import android.arch.persistence.room.RoomDatabase; -import android.arch.persistence.room.TypeConverters; +import androidx.room.Database; +import androidx.room.RoomDatabase; +import androidx.room.TypeConverters; import org.schabi.newpipe.database.history.dao.SearchHistoryDAO; import org.schabi.newpipe.database.history.dao.StreamHistoryDAO; diff --git a/app/src/main/java/org/schabi/newpipe/database/BasicDAO.java b/app/src/main/java/org/schabi/newpipe/database/BasicDAO.java index 13117145a..b7381b9f1 100644 --- a/app/src/main/java/org/schabi/newpipe/database/BasicDAO.java +++ b/app/src/main/java/org/schabi/newpipe/database/BasicDAO.java @@ -1,10 +1,10 @@ package org.schabi.newpipe.database; -import android.arch.persistence.room.Dao; -import android.arch.persistence.room.Delete; -import android.arch.persistence.room.Insert; -import android.arch.persistence.room.OnConflictStrategy; -import android.arch.persistence.room.Update; +import androidx.room.Dao; +import androidx.room.Delete; +import androidx.room.Insert; +import androidx.room.OnConflictStrategy; +import androidx.room.Update; import java.util.Collection; import java.util.List; diff --git a/app/src/main/java/org/schabi/newpipe/database/Converters.java b/app/src/main/java/org/schabi/newpipe/database/Converters.java index d48fbfaf1..bb781d194 100644 --- a/app/src/main/java/org/schabi/newpipe/database/Converters.java +++ b/app/src/main/java/org/schabi/newpipe/database/Converters.java @@ -1,6 +1,6 @@ package org.schabi.newpipe.database; -import android.arch.persistence.room.TypeConverter; +import androidx.room.TypeConverter; import org.schabi.newpipe.extractor.stream.StreamType; diff --git a/app/src/main/java/org/schabi/newpipe/database/Migrations.java b/app/src/main/java/org/schabi/newpipe/database/Migrations.java index 239fc02bb..07d9749b2 100644 --- a/app/src/main/java/org/schabi/newpipe/database/Migrations.java +++ b/app/src/main/java/org/schabi/newpipe/database/Migrations.java @@ -1,17 +1,26 @@ package org.schabi.newpipe.database; -import android.arch.persistence.db.SupportSQLiteDatabase; -import android.arch.persistence.room.migration.Migration; -import android.support.annotation.NonNull; +import androidx.sqlite.db.SupportSQLiteDatabase; +import androidx.room.migration.Migration; +import androidx.annotation.NonNull; +import android.util.Log; + +import org.schabi.newpipe.BuildConfig; public class Migrations { public static final int DB_VER_11_0 = 1; public static final int DB_VER_12_0 = 2; + public static final boolean DEBUG = !BuildConfig.BUILD_TYPE.equals("release"); + private static final String TAG = Migrations.class.getName(); + public static final Migration MIGRATION_11_12 = new Migration(DB_VER_11_0, DB_VER_12_0) { @Override public void migrate(@NonNull SupportSQLiteDatabase database) { + if(DEBUG) { + Log.d(TAG, "Start migrating database"); + } /* * Unfortunately these queries must be hardcoded due to the possibility of * schema and names changing at a later date, thus invalidating the older migration @@ -56,6 +65,10 @@ public class Migrations { "ORDER BY creation_date DESC"); database.execSQL("DROP TABLE IF EXISTS watch_history"); + + if(DEBUG) { + Log.d(TAG, "Stop migrating database"); + } } }; } diff --git a/app/src/main/java/org/schabi/newpipe/database/history/dao/SearchHistoryDAO.java b/app/src/main/java/org/schabi/newpipe/database/history/dao/SearchHistoryDAO.java index 83e629e48..df8094830 100644 --- a/app/src/main/java/org/schabi/newpipe/database/history/dao/SearchHistoryDAO.java +++ b/app/src/main/java/org/schabi/newpipe/database/history/dao/SearchHistoryDAO.java @@ -1,8 +1,8 @@ package org.schabi.newpipe.database.history.dao; -import android.arch.persistence.room.Dao; -import android.arch.persistence.room.Query; -import android.support.annotation.Nullable; +import androidx.room.Dao; +import androidx.room.Query; +import androidx.annotation.Nullable; import org.schabi.newpipe.database.history.model.SearchHistoryEntry; diff --git a/app/src/main/java/org/schabi/newpipe/database/history/dao/StreamHistoryDAO.java b/app/src/main/java/org/schabi/newpipe/database/history/dao/StreamHistoryDAO.java index 847153e12..2703b9783 100644 --- a/app/src/main/java/org/schabi/newpipe/database/history/dao/StreamHistoryDAO.java +++ b/app/src/main/java/org/schabi/newpipe/database/history/dao/StreamHistoryDAO.java @@ -1,9 +1,9 @@ package org.schabi.newpipe.database.history.dao; -import android.arch.persistence.room.Dao; -import android.arch.persistence.room.Query; -import android.support.annotation.Nullable; +import androidx.room.Dao; +import androidx.room.Query; +import androidx.annotation.Nullable; import org.schabi.newpipe.database.history.model.StreamHistoryEntry; import org.schabi.newpipe.database.stream.StreamStatisticsEntry; @@ -50,6 +50,11 @@ public abstract class StreamHistoryDAO implements HistoryDAO> getHistory(); + @Query("SELECT * FROM " + STREAM_HISTORY_TABLE + " WHERE " + JOIN_STREAM_ID + + " = :streamId ORDER BY " + STREAM_ACCESS_DATE + " DESC LIMIT 1") + @Nullable + public abstract StreamHistoryEntity getLatestEntry(final long streamId); + @Query("DELETE FROM " + STREAM_HISTORY_TABLE + " WHERE " + JOIN_STREAM_ID + " = :streamId") public abstract int deleteStreamHistory(final long streamId); diff --git a/app/src/main/java/org/schabi/newpipe/database/history/model/SearchHistoryEntry.java b/app/src/main/java/org/schabi/newpipe/database/history/model/SearchHistoryEntry.java index dcfff99b8..222ef0a59 100644 --- a/app/src/main/java/org/schabi/newpipe/database/history/model/SearchHistoryEntry.java +++ b/app/src/main/java/org/schabi/newpipe/database/history/model/SearchHistoryEntry.java @@ -1,10 +1,10 @@ package org.schabi.newpipe.database.history.model; -import android.arch.persistence.room.ColumnInfo; -import android.arch.persistence.room.Entity; -import android.arch.persistence.room.Ignore; -import android.arch.persistence.room.Index; -import android.arch.persistence.room.PrimaryKey; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.Ignore; +import androidx.room.Index; +import androidx.room.PrimaryKey; import java.util.Date; diff --git a/app/src/main/java/org/schabi/newpipe/database/history/model/StreamHistoryEntity.java b/app/src/main/java/org/schabi/newpipe/database/history/model/StreamHistoryEntity.java index b553f437d..64bdf34de 100644 --- a/app/src/main/java/org/schabi/newpipe/database/history/model/StreamHistoryEntity.java +++ b/app/src/main/java/org/schabi/newpipe/database/history/model/StreamHistoryEntity.java @@ -1,17 +1,17 @@ package org.schabi.newpipe.database.history.model; -import android.arch.persistence.room.ColumnInfo; -import android.arch.persistence.room.Entity; -import android.arch.persistence.room.ForeignKey; -import android.arch.persistence.room.Ignore; -import android.arch.persistence.room.Index; -import android.support.annotation.NonNull; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.ForeignKey; +import androidx.room.Ignore; +import androidx.room.Index; +import androidx.annotation.NonNull; import org.schabi.newpipe.database.stream.model.StreamEntity; import java.util.Date; -import static android.arch.persistence.room.ForeignKey.CASCADE; +import static androidx.room.ForeignKey.CASCADE; import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.STREAM_HISTORY_TABLE; import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.JOIN_STREAM_ID; import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.STREAM_ACCESS_DATE; diff --git a/app/src/main/java/org/schabi/newpipe/database/history/model/StreamHistoryEntry.java b/app/src/main/java/org/schabi/newpipe/database/history/model/StreamHistoryEntry.java index 772b96cc4..ad66451e4 100644 --- a/app/src/main/java/org/schabi/newpipe/database/history/model/StreamHistoryEntry.java +++ b/app/src/main/java/org/schabi/newpipe/database/history/model/StreamHistoryEntry.java @@ -1,6 +1,6 @@ package org.schabi.newpipe.database.history.model; -import android.arch.persistence.room.ColumnInfo; +import androidx.room.ColumnInfo; import org.schabi.newpipe.database.stream.model.StreamEntity; import org.schabi.newpipe.extractor.stream.StreamType; diff --git a/app/src/main/java/org/schabi/newpipe/database/playlist/PlaylistMetadataEntry.java b/app/src/main/java/org/schabi/newpipe/database/playlist/PlaylistMetadataEntry.java index 6d9fc2213..252ca07f0 100644 --- a/app/src/main/java/org/schabi/newpipe/database/playlist/PlaylistMetadataEntry.java +++ b/app/src/main/java/org/schabi/newpipe/database/playlist/PlaylistMetadataEntry.java @@ -1,6 +1,6 @@ package org.schabi.newpipe.database.playlist; -import android.arch.persistence.room.ColumnInfo; +import androidx.room.ColumnInfo; import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_ID; import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_NAME; diff --git a/app/src/main/java/org/schabi/newpipe/database/playlist/PlaylistStreamEntry.java b/app/src/main/java/org/schabi/newpipe/database/playlist/PlaylistStreamEntry.java index b6ecfe1f0..fb45c3564 100644 --- a/app/src/main/java/org/schabi/newpipe/database/playlist/PlaylistStreamEntry.java +++ b/app/src/main/java/org/schabi/newpipe/database/playlist/PlaylistStreamEntry.java @@ -1,6 +1,6 @@ package org.schabi.newpipe.database.playlist; -import android.arch.persistence.room.ColumnInfo; +import androidx.room.ColumnInfo; import org.schabi.newpipe.database.LocalItem; import org.schabi.newpipe.database.playlist.model.PlaylistStreamEntity; diff --git a/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistDAO.java b/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistDAO.java index 7a6282f96..f5a685a7c 100644 --- a/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistDAO.java +++ b/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistDAO.java @@ -1,7 +1,7 @@ package org.schabi.newpipe.database.playlist.dao; -import android.arch.persistence.room.Dao; -import android.arch.persistence.room.Query; +import androidx.room.Dao; +import androidx.room.Query; import org.schabi.newpipe.database.BasicDAO; import org.schabi.newpipe.database.playlist.model.PlaylistEntity; diff --git a/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistRemoteDAO.java b/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistRemoteDAO.java index 82d767b07..b7ccf42f7 100644 --- a/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistRemoteDAO.java +++ b/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistRemoteDAO.java @@ -1,8 +1,8 @@ package org.schabi.newpipe.database.playlist.dao; -import android.arch.persistence.room.Dao; -import android.arch.persistence.room.Query; -import android.arch.persistence.room.Transaction; +import androidx.room.Dao; +import androidx.room.Query; +import androidx.room.Transaction; import org.schabi.newpipe.database.BasicDAO; import org.schabi.newpipe.database.playlist.model.PlaylistRemoteEntity; diff --git a/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistStreamDAO.java b/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistStreamDAO.java index 8b6d62ca4..656fc27fc 100644 --- a/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistStreamDAO.java +++ b/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistStreamDAO.java @@ -1,8 +1,8 @@ package org.schabi.newpipe.database.playlist.dao; -import android.arch.persistence.room.Dao; -import android.arch.persistence.room.Query; -import android.arch.persistence.room.Transaction; +import androidx.room.Dao; +import androidx.room.Query; +import androidx.room.Transaction; import org.schabi.newpipe.database.BasicDAO; import org.schabi.newpipe.database.playlist.PlaylistMetadataEntry; diff --git a/app/src/main/java/org/schabi/newpipe/database/playlist/model/PlaylistEntity.java b/app/src/main/java/org/schabi/newpipe/database/playlist/model/PlaylistEntity.java index bfda8eeec..9d7989b21 100644 --- a/app/src/main/java/org/schabi/newpipe/database/playlist/model/PlaylistEntity.java +++ b/app/src/main/java/org/schabi/newpipe/database/playlist/model/PlaylistEntity.java @@ -1,9 +1,9 @@ package org.schabi.newpipe.database.playlist.model; -import android.arch.persistence.room.ColumnInfo; -import android.arch.persistence.room.Entity; -import android.arch.persistence.room.Index; -import android.arch.persistence.room.PrimaryKey; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.Index; +import androidx.room.PrimaryKey; import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_NAME; import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_TABLE; diff --git a/app/src/main/java/org/schabi/newpipe/database/playlist/model/PlaylistRemoteEntity.java b/app/src/main/java/org/schabi/newpipe/database/playlist/model/PlaylistRemoteEntity.java index ab917a22b..bf446ca1f 100644 --- a/app/src/main/java/org/schabi/newpipe/database/playlist/model/PlaylistRemoteEntity.java +++ b/app/src/main/java/org/schabi/newpipe/database/playlist/model/PlaylistRemoteEntity.java @@ -1,10 +1,10 @@ package org.schabi.newpipe.database.playlist.model; -import android.arch.persistence.room.ColumnInfo; -import android.arch.persistence.room.Entity; -import android.arch.persistence.room.Ignore; -import android.arch.persistence.room.Index; -import android.arch.persistence.room.PrimaryKey; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.Ignore; +import androidx.room.Index; +import androidx.room.PrimaryKey; import org.schabi.newpipe.database.playlist.PlaylistLocalItem; import org.schabi.newpipe.extractor.playlist.PlaylistInfo; diff --git a/app/src/main/java/org/schabi/newpipe/database/playlist/model/PlaylistStreamEntity.java b/app/src/main/java/org/schabi/newpipe/database/playlist/model/PlaylistStreamEntity.java index a5b2e8248..87afdb4f9 100644 --- a/app/src/main/java/org/schabi/newpipe/database/playlist/model/PlaylistStreamEntity.java +++ b/app/src/main/java/org/schabi/newpipe/database/playlist/model/PlaylistStreamEntity.java @@ -1,13 +1,13 @@ package org.schabi.newpipe.database.playlist.model; -import android.arch.persistence.room.ColumnInfo; -import android.arch.persistence.room.Entity; -import android.arch.persistence.room.ForeignKey; -import android.arch.persistence.room.Index; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.ForeignKey; +import androidx.room.Index; import org.schabi.newpipe.database.stream.model.StreamEntity; -import static android.arch.persistence.room.ForeignKey.CASCADE; +import static androidx.room.ForeignKey.CASCADE; import static org.schabi.newpipe.database.playlist.model.PlaylistStreamEntity.JOIN_INDEX; import static org.schabi.newpipe.database.playlist.model.PlaylistStreamEntity.JOIN_PLAYLIST_ID; import static org.schabi.newpipe.database.playlist.model.PlaylistStreamEntity.JOIN_STREAM_ID; diff --git a/app/src/main/java/org/schabi/newpipe/database/stream/StreamStatisticsEntry.java b/app/src/main/java/org/schabi/newpipe/database/stream/StreamStatisticsEntry.java index 6909f3397..9b61eb469 100644 --- a/app/src/main/java/org/schabi/newpipe/database/stream/StreamStatisticsEntry.java +++ b/app/src/main/java/org/schabi/newpipe/database/stream/StreamStatisticsEntry.java @@ -1,6 +1,6 @@ package org.schabi.newpipe.database.stream; -import android.arch.persistence.room.ColumnInfo; +import androidx.room.ColumnInfo; import org.schabi.newpipe.database.LocalItem; import org.schabi.newpipe.database.history.model.StreamHistoryEntity; diff --git a/app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamDAO.java b/app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamDAO.java index 396a29fca..c89f6163f 100644 --- a/app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamDAO.java +++ b/app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamDAO.java @@ -1,10 +1,10 @@ package org.schabi.newpipe.database.stream.dao; -import android.arch.persistence.room.Dao; -import android.arch.persistence.room.Insert; -import android.arch.persistence.room.OnConflictStrategy; -import android.arch.persistence.room.Query; -import android.arch.persistence.room.Transaction; +import androidx.room.Dao; +import androidx.room.Insert; +import androidx.room.OnConflictStrategy; +import androidx.room.Query; +import androidx.room.Transaction; import org.schabi.newpipe.database.BasicDAO; import org.schabi.newpipe.database.playlist.model.PlaylistStreamEntity; diff --git a/app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamStateDAO.java b/app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamStateDAO.java index 1c06f4df9..c85810984 100644 --- a/app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamStateDAO.java +++ b/app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamStateDAO.java @@ -1,10 +1,10 @@ package org.schabi.newpipe.database.stream.dao; -import android.arch.persistence.room.Dao; -import android.arch.persistence.room.Insert; -import android.arch.persistence.room.OnConflictStrategy; -import android.arch.persistence.room.Query; -import android.arch.persistence.room.Transaction; +import androidx.room.Dao; +import androidx.room.Insert; +import androidx.room.OnConflictStrategy; +import androidx.room.Query; +import androidx.room.Transaction; import org.schabi.newpipe.database.BasicDAO; import org.schabi.newpipe.database.stream.model.StreamStateEntity; diff --git a/app/src/main/java/org/schabi/newpipe/database/stream/model/StreamEntity.java b/app/src/main/java/org/schabi/newpipe/database/stream/model/StreamEntity.java index d6cab1090..1f26e214d 100644 --- a/app/src/main/java/org/schabi/newpipe/database/stream/model/StreamEntity.java +++ b/app/src/main/java/org/schabi/newpipe/database/stream/model/StreamEntity.java @@ -1,10 +1,10 @@ package org.schabi.newpipe.database.stream.model; -import android.arch.persistence.room.ColumnInfo; -import android.arch.persistence.room.Entity; -import android.arch.persistence.room.Ignore; -import android.arch.persistence.room.Index; -import android.arch.persistence.room.PrimaryKey; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.Ignore; +import androidx.room.Index; +import androidx.room.PrimaryKey; import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.extractor.stream.StreamInfoItem; diff --git a/app/src/main/java/org/schabi/newpipe/database/stream/model/StreamStateEntity.java b/app/src/main/java/org/schabi/newpipe/database/stream/model/StreamStateEntity.java index 15940a964..8630bfa53 100644 --- a/app/src/main/java/org/schabi/newpipe/database/stream/model/StreamStateEntity.java +++ b/app/src/main/java/org/schabi/newpipe/database/stream/model/StreamStateEntity.java @@ -1,11 +1,14 @@ package org.schabi.newpipe.database.stream.model; -import android.arch.persistence.room.ColumnInfo; -import android.arch.persistence.room.Entity; -import android.arch.persistence.room.ForeignKey; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.ForeignKey; +import androidx.annotation.Nullable; -import static android.arch.persistence.room.ForeignKey.CASCADE; +import java.util.concurrent.TimeUnit; + +import static androidx.room.ForeignKey.CASCADE; import static org.schabi.newpipe.database.stream.model.StreamStateEntity.JOIN_STREAM_ID; import static org.schabi.newpipe.database.stream.model.StreamStateEntity.STREAM_STATE_TABLE; @@ -22,6 +25,12 @@ public class StreamStateEntity { final public static String JOIN_STREAM_ID = "stream_id"; final public static String STREAM_PROGRESS_TIME = "progress_time"; + + /** Playback state will not be saved, if playback time less than this threshold */ + private static final int PLAYBACK_SAVE_THRESHOLD_START_SECONDS = 5; + /** Playback state will not be saved, if time left less than this threshold */ + private static final int PLAYBACK_SAVE_THRESHOLD_END_SECONDS = 10; + @ColumnInfo(name = JOIN_STREAM_ID) private long streamUid; @@ -48,4 +57,18 @@ public class StreamStateEntity { public void setProgressTime(long progressTime) { this.progressTime = progressTime; } + + public boolean isValid(int durationInSeconds) { + final int seconds = (int) TimeUnit.MILLISECONDS.toSeconds(progressTime); + return seconds > PLAYBACK_SAVE_THRESHOLD_START_SECONDS + && seconds < durationInSeconds - PLAYBACK_SAVE_THRESHOLD_END_SECONDS; + } + + @Override + public boolean equals(@Nullable Object obj) { + if (obj instanceof StreamStateEntity) { + return ((StreamStateEntity) obj).streamUid == streamUid + && ((StreamStateEntity) obj).progressTime == progressTime; + } else return false; + } } diff --git a/app/src/main/java/org/schabi/newpipe/database/subscription/SubscriptionDAO.java b/app/src/main/java/org/schabi/newpipe/database/subscription/SubscriptionDAO.java index ee6dea9fe..0869d60ff 100644 --- a/app/src/main/java/org/schabi/newpipe/database/subscription/SubscriptionDAO.java +++ b/app/src/main/java/org/schabi/newpipe/database/subscription/SubscriptionDAO.java @@ -1,10 +1,10 @@ package org.schabi.newpipe.database.subscription; -import android.arch.persistence.room.Dao; -import android.arch.persistence.room.Insert; -import android.arch.persistence.room.OnConflictStrategy; -import android.arch.persistence.room.Query; -import android.arch.persistence.room.Transaction; +import androidx.room.Dao; +import androidx.room.Insert; +import androidx.room.OnConflictStrategy; +import androidx.room.Query; +import androidx.room.Transaction; import org.schabi.newpipe.database.BasicDAO; diff --git a/app/src/main/java/org/schabi/newpipe/database/subscription/SubscriptionEntity.java b/app/src/main/java/org/schabi/newpipe/database/subscription/SubscriptionEntity.java index 9328fff6a..1e69567e1 100644 --- a/app/src/main/java/org/schabi/newpipe/database/subscription/SubscriptionEntity.java +++ b/app/src/main/java/org/schabi/newpipe/database/subscription/SubscriptionEntity.java @@ -1,11 +1,11 @@ package org.schabi.newpipe.database.subscription; -import android.arch.persistence.room.ColumnInfo; -import android.arch.persistence.room.Entity; -import android.arch.persistence.room.Ignore; -import android.arch.persistence.room.Index; -import android.arch.persistence.room.PrimaryKey; -import android.support.annotation.NonNull; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.Ignore; +import androidx.room.Index; +import androidx.room.PrimaryKey; +import androidx.annotation.NonNull; import org.schabi.newpipe.extractor.channel.ChannelInfo; import org.schabi.newpipe.extractor.channel.ChannelInfoItem; diff --git a/app/src/main/java/org/schabi/newpipe/download/DownloadActivity.java b/app/src/main/java/org/schabi/newpipe/download/DownloadActivity.java index 251e4c730..449a790e8 100644 --- a/app/src/main/java/org/schabi/newpipe/download/DownloadActivity.java +++ b/app/src/main/java/org/schabi/newpipe/download/DownloadActivity.java @@ -3,9 +3,9 @@ package org.schabi.newpipe.download; import android.app.FragmentTransaction; import android.content.Intent; import android.os.Bundle; -import android.support.v7.app.ActionBar; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; @@ -47,7 +47,7 @@ public class DownloadActivity extends AppCompatActivity { @Override public void onGlobalLayout() { updateFragments(); - getWindow().getDecorView().getViewTreeObserver().removeGlobalOnLayoutListener(this); + getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(this); } }); } @@ -55,12 +55,13 @@ public class DownloadActivity extends AppCompatActivity { private void updateFragments() { MissionsFragment fragment = new MissionsFragment(); - getFragmentManager().beginTransaction() + getSupportFragmentManager().beginTransaction() .replace(R.id.frame, fragment, MISSIONS_FRAGMENT_TAG) .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) .commit(); } + @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuInflater inflater = getMenuInflater(); @@ -86,9 +87,4 @@ public class DownloadActivity extends AppCompatActivity { return super.onOptionsItemSelected(item); } } - - @Override - public void onRestoreInstanceState(Bundle inState){ - super.onRestoreInstanceState(inState); - } } diff --git a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java index ec6d42b29..1d536ea1a 100644 --- a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java +++ b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java @@ -1,15 +1,25 @@ package org.schabi.newpipe.download; +import android.app.Activity; +import android.content.ComponentName; import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; import android.content.SharedPreferences; +import android.net.Uri; import android.os.Bundle; +import android.os.Environment; +import android.os.IBinder; import android.preference.PreferenceManager; -import android.support.annotation.IdRes; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.app.DialogFragment; -import android.support.v7.app.AlertDialog; -import android.support.v7.widget.Toolbar; +import androidx.annotation.IdRes; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.annotation.StringRes; +import androidx.fragment.app.DialogFragment; +import androidx.documentfile.provider.DocumentFile; +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.view.menu.ActionMenuItemView; +import androidx.appcompat.widget.Toolbar; import android.util.Log; import android.util.SparseArray; import android.view.LayoutInflater; @@ -24,6 +34,8 @@ import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; +import com.nononsenseapps.filepicker.Utils; + import org.schabi.newpipe.MainActivity; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.MediaFormat; @@ -34,7 +46,10 @@ import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.extractor.stream.SubtitlesStream; import org.schabi.newpipe.extractor.stream.VideoStream; import org.schabi.newpipe.extractor.utils.Localization; +import org.schabi.newpipe.report.ErrorActivity; +import org.schabi.newpipe.report.UserAction; import org.schabi.newpipe.settings.NewPipeSettings; +import org.schabi.newpipe.util.FilePickerActivityHelper; import org.schabi.newpipe.util.FilenameUtils; import org.schabi.newpipe.util.ListHelper; import org.schabi.newpipe.util.PermissionHelper; @@ -43,19 +58,28 @@ import org.schabi.newpipe.util.StreamItemAdapter; import org.schabi.newpipe.util.StreamItemAdapter.StreamSizeWrapper; import org.schabi.newpipe.util.ThemeHelper; +import java.io.File; +import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Locale; import icepick.Icepick; import icepick.State; import io.reactivex.disposables.CompositeDisposable; +import us.shandian.giga.io.StoredDirectoryHelper; +import us.shandian.giga.io.StoredFileHelper; import us.shandian.giga.postprocessing.Postprocessing; +import us.shandian.giga.service.DownloadManager; import us.shandian.giga.service.DownloadManagerService; +import us.shandian.giga.service.DownloadManagerService.DownloadManagerBinder; +import us.shandian.giga.service.MissionState; public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheckedChangeListener, AdapterView.OnItemSelectedListener { private static final String TAG = "DialogFragment"; private static final boolean DEBUG = MainActivity.DEBUG; + private static final int REQUEST_DOWNLOAD_SAVE_AS = 0x1230; @State protected StreamInfo currentInfo; @@ -80,7 +104,7 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck private EditText nameEditText; private Spinner streamsSpinner; - private RadioGroup radioVideoAudioGroup; + private RadioGroup radioStreamsGroup; private TextView threadsCountTextView; private SeekBar threadsSeekBar; @@ -155,12 +179,15 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck super.onCreate(savedInstanceState); if (DEBUG) Log.d(TAG, "onCreate() called with: savedInstanceState = [" + savedInstanceState + "]"); + if (!PermissionHelper.checkStoragePermissions(getActivity(), PermissionHelper.DOWNLOAD_DIALOG_REQUEST_CODE)) { getDialog().dismiss(); return; } - setStyle(STYLE_NO_TITLE, ThemeHelper.getDialogTheme(getContext())); + context = getContext(); + + setStyle(STYLE_NO_TITLE, ThemeHelper.getDialogTheme(context)); Icepick.restoreInstanceState(this, savedInstanceState); SparseArray> secondaryStreams = new SparseArray<>(4); @@ -177,9 +204,33 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck } } - this.videoStreamsAdapter = new StreamItemAdapter<>(getContext(), wrappedVideoStreams, secondaryStreams); - this.audioStreamsAdapter = new StreamItemAdapter<>(getContext(), wrappedAudioStreams); - this.subtitleStreamsAdapter = new StreamItemAdapter<>(getContext(), wrappedSubtitleStreams); + this.videoStreamsAdapter = new StreamItemAdapter<>(context, wrappedVideoStreams, secondaryStreams); + this.audioStreamsAdapter = new StreamItemAdapter<>(context, wrappedAudioStreams); + this.subtitleStreamsAdapter = new StreamItemAdapter<>(context, wrappedSubtitleStreams); + + Intent intent = new Intent(context, DownloadManagerService.class); + context.startService(intent); + + context.bindService(intent, new ServiceConnection() { + @Override + public void onServiceConnected(ComponentName cname, IBinder service) { + DownloadManagerBinder mgr = (DownloadManagerBinder) service; + + mainStorageAudio = mgr.getMainStorageAudio(); + mainStorageVideo = mgr.getMainStorageVideo(); + downloadManager = mgr.getDownloadManager(); + askForSavePath = mgr.askForSavePath(); + + okButton.setEnabled(true); + + context.unbindService(this); + } + + @Override + public void onServiceDisconnected(ComponentName name) { + // nothing to do + } + }, Context.BIND_AUTO_CREATE); } @Override @@ -204,8 +255,8 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck threadsCountTextView = view.findViewById(R.id.threads_count); threadsSeekBar = view.findViewById(R.id.threads); - radioVideoAudioGroup = view.findViewById(R.id.video_audio_group); - radioVideoAudioGroup.setOnCheckedChangeListener(this); + radioStreamsGroup = view.findViewById(R.id.video_audio_group); + radioStreamsGroup.setOnCheckedChangeListener(this); initToolbar(view.findViewById(R.id.toolbar)); setupDownloadOptions(); @@ -240,17 +291,17 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck disposables.clear(); disposables.add(StreamSizeWrapper.fetchSizeForWrapper(wrappedVideoStreams).subscribe(result -> { - if (radioVideoAudioGroup.getCheckedRadioButtonId() == R.id.video_button) { + if (radioStreamsGroup.getCheckedRadioButtonId() == R.id.video_button) { setupVideoSpinner(); } })); disposables.add(StreamSizeWrapper.fetchSizeForWrapper(wrappedAudioStreams).subscribe(result -> { - if (radioVideoAudioGroup.getCheckedRadioButtonId() == R.id.audio_button) { + if (radioStreamsGroup.getCheckedRadioButtonId() == R.id.audio_button) { setupAudioSpinner(); } })); disposables.add(StreamSizeWrapper.fetchSizeForWrapper(wrappedSubtitleStreams).subscribe(result -> { - if (radioVideoAudioGroup.getCheckedRadioButtonId() == R.id.subtitle_button) { + if (radioStreamsGroup.getCheckedRadioButtonId() == R.id.subtitle_button) { setupSubtitleSpinner(); } })); @@ -263,21 +314,55 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck } @Override - public void onSaveInstanceState(Bundle outState) { + public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); Icepick.saveInstanceState(this, outState); } + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + + if (requestCode == REQUEST_DOWNLOAD_SAVE_AS && resultCode == Activity.RESULT_OK) { + if (data.getData() == null) { + showFailedDialog(R.string.general_error); + return; + } + + if (FilePickerActivityHelper.isOwnFileUri(context, data.getData())) { + File file = Utils.getFileForUri(data.getData()); + checkSelectedDownload(null, Uri.fromFile(file), file.getName(), StoredFileHelper.DEFAULT_MIME); + return; + } + + DocumentFile docFile = DocumentFile.fromSingleUri(context, data.getData()); + if (docFile == null) { + showFailedDialog(R.string.general_error); + return; + } + + // check if the selected file was previously used + checkSelectedDownload(null, data.getData(), docFile.getName(), docFile.getType()); + } + } + /*////////////////////////////////////////////////////////////////////////// // Inits //////////////////////////////////////////////////////////////////////////*/ private void initToolbar(Toolbar toolbar) { if (DEBUG) Log.d(TAG, "initToolbar() called with: toolbar = [" + toolbar + "]"); + + boolean isLight = ThemeHelper.isLightThemeSelected(getActivity()); + toolbar.setTitle(R.string.download_dialog_title); - toolbar.setNavigationIcon(ThemeHelper.isLightThemeSelected(getActivity()) ? R.drawable.ic_arrow_back_black_24dp : R.drawable.ic_arrow_back_white_24dp); + toolbar.setNavigationIcon(isLight ? R.drawable.ic_arrow_back_black_24dp : R.drawable.ic_arrow_back_white_24dp); toolbar.inflateMenu(R.menu.dialog_url); toolbar.setNavigationOnClickListener(v -> getDialog().dismiss()); + toolbar.setNavigationContentDescription(R.string.cancel); + + okButton = toolbar.findViewById(R.id.okay); + okButton.setEnabled(false);// disable until the download service connection is done toolbar.setOnMenuItemClickListener(item -> { if (item.getItemId() == R.id.okay) { @@ -346,7 +431,7 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck public void onItemSelected(AdapterView parent, View view, int position, long id) { if (DEBUG) Log.d(TAG, "onItemSelected() called with: parent = [" + parent + "], view = [" + view + "], position = [" + position + "], id = [" + id + "]"); - switch (radioVideoAudioGroup.getCheckedRadioButtonId()) { + switch (radioStreamsGroup.getCheckedRadioButtonId()) { case R.id.audio_button: selectedAudioIndex = position; break; @@ -370,9 +455,9 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck protected void setupDownloadOptions() { setRadioButtonsState(false); - final RadioButton audioButton = radioVideoAudioGroup.findViewById(R.id.audio_button); - final RadioButton videoButton = radioVideoAudioGroup.findViewById(R.id.video_button); - final RadioButton subtitleButton = radioVideoAudioGroup.findViewById(R.id.subtitle_button); + final RadioButton audioButton = radioStreamsGroup.findViewById(R.id.audio_button); + final RadioButton videoButton = radioStreamsGroup.findViewById(R.id.video_button); + final RadioButton subtitleButton = radioStreamsGroup.findViewById(R.id.subtitle_button); final boolean isVideoStreamsAvailable = videoStreamsAdapter.getCount() > 0; final boolean isAudioStreamsAvailable = audioStreamsAdapter.getCount() > 0; final boolean isSubtitleStreamsAvailable = subtitleStreamsAdapter.getCount() > 0; @@ -397,9 +482,9 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck } private void setRadioButtonsState(boolean enabled) { - radioVideoAudioGroup.findViewById(R.id.audio_button).setEnabled(enabled); - radioVideoAudioGroup.findViewById(R.id.video_button).setEnabled(enabled); - radioVideoAudioGroup.findViewById(R.id.subtitle_button).setEnabled(enabled); + radioStreamsGroup.findViewById(R.id.audio_button).setEnabled(enabled); + radioStreamsGroup.findViewById(R.id.video_button).setEnabled(enabled); + radioStreamsGroup.findViewById(R.id.subtitle_button).setEnabled(enabled); } private int getSubtitleIndexBy(List streams) { @@ -434,98 +519,320 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck return 0; } + StoredDirectoryHelper mainStorageAudio = null; + StoredDirectoryHelper mainStorageVideo = null; + DownloadManager downloadManager = null; + ActionMenuItemView okButton = null; + Context context; + boolean askForSavePath; + + private String getNameEditText() { + String str = nameEditText.getText().toString().trim(); + + return FilenameUtils.createFilename(context, str.isEmpty() ? currentInfo.getName() : str); + } + + private void showFailedDialog(@StringRes int msg) { + new AlertDialog.Builder(context) + .setTitle(R.string.general_error) + .setMessage(msg) + .setNegativeButton(android.R.string.ok, null) + .create() + .show(); + } + + private void showErrorActivity(Exception e) { + ErrorActivity.reportError( + context, + Collections.singletonList(e), + null, + null, + ErrorActivity.ErrorInfo.make(UserAction.SOMETHING_ELSE, "-", "-", R.string.general_error) + ); + } + private void prepareSelectedDownload() { - final Context context = getContext(); - Stream stream; - String location; - char kind; + StoredDirectoryHelper mainStorage; + MediaFormat format; + String mime; - String fileName = nameEditText.getText().toString().trim(); - if (fileName.isEmpty()) - fileName = FilenameUtils.createFilename(context, currentInfo.getName()); + // first, build the filename and get the output folder (if possible) + // later, run a very very very large file checking logic - switch (radioVideoAudioGroup.getCheckedRadioButtonId()) { + String filename = getNameEditText().concat("."); + + switch (radioStreamsGroup.getCheckedRadioButtonId()) { case R.id.audio_button: - stream = audioStreamsAdapter.getItem(selectedAudioIndex); - location = NewPipeSettings.getAudioDownloadPath(context); - kind = 'a'; + mainStorage = mainStorageAudio; + format = audioStreamsAdapter.getItem(selectedAudioIndex).getFormat(); + mime = format.mimeType; + filename += format.suffix; break; case R.id.video_button: - stream = videoStreamsAdapter.getItem(selectedVideoIndex); - location = NewPipeSettings.getVideoDownloadPath(context); - kind = 'v'; + mainStorage = mainStorageVideo; + format = videoStreamsAdapter.getItem(selectedVideoIndex).getFormat(); + mime = format.mimeType; + filename += format.suffix; break; case R.id.subtitle_button: - stream = subtitleStreamsAdapter.getItem(selectedSubtitleIndex); - location = NewPipeSettings.getVideoDownloadPath(context);// assume that subtitle & video files go together - kind = 's'; + mainStorage = mainStorageVideo;// subtitle & video files go together + format = subtitleStreamsAdapter.getItem(selectedSubtitleIndex).getFormat(); + mime = format.mimeType; + filename += format == MediaFormat.TTML ? MediaFormat.SRT.suffix : format.suffix; + break; + default: + throw new RuntimeException("No stream selected"); + } + + if (mainStorage == null || askForSavePath) { + // This part is called if with SAF preferred: + // * older android version running + // * save path not defined (via download settings) + // * the user checked the "ask where to download" option + + if (!askForSavePath) + Toast.makeText(context, getString(R.string.no_available_dir), Toast.LENGTH_LONG).show(); + + if (NewPipeSettings.useStorageAccessFramework(context)) { + StoredFileHelper.requestSafWithFileCreation(this, REQUEST_DOWNLOAD_SAVE_AS, filename, mime); + } else { + File initialSavePath; + if (radioStreamsGroup.getCheckedRadioButtonId() == R.id.audio_button) + initialSavePath = NewPipeSettings.getDir(Environment.DIRECTORY_MUSIC); + else + initialSavePath = NewPipeSettings.getDir(Environment.DIRECTORY_MOVIES); + + initialSavePath = new File(initialSavePath, filename); + startActivityForResult( + FilePickerActivityHelper.chooseFileToSave(context, initialSavePath.getAbsolutePath()), + REQUEST_DOWNLOAD_SAVE_AS + ); + } + + return; + } + + // check for existing file with the same name + checkSelectedDownload(mainStorage, mainStorage.findFile(filename), filename, mime); + } + + private void checkSelectedDownload(StoredDirectoryHelper mainStorage, Uri targetFile, String filename, String mime) { + StoredFileHelper storage; + + try { + if (mainStorage == null) { + // using SAF on older android version + storage = new StoredFileHelper(context, null, targetFile, ""); + } else if (targetFile == null) { + // the file does not exist, but it is probably used in a pending download + storage = new StoredFileHelper(mainStorage.getUri(), filename, mime, mainStorage.getTag()); + } else { + // the target filename is already use, attempt to use it + storage = new StoredFileHelper(context, mainStorage.getUri(), targetFile, mainStorage.getTag()); + } + } catch (Exception e) { + showErrorActivity(e); + return; + } + + // check if is our file + MissionState state = downloadManager.checkForExistingMission(storage); + @StringRes int msgBtn; + @StringRes int msgBody; + + switch (state) { + case Finished: + msgBtn = R.string.overwrite; + msgBody = R.string.overwrite_finished_warning; + break; + case Pending: + msgBtn = R.string.overwrite; + msgBody = R.string.download_already_pending; + break; + case PendingRunning: + msgBtn = R.string.generate_unique_name; + msgBody = R.string.download_already_running; + break; + case None: + if (mainStorage == null) { + // This part is called if: + // * using SAF on older android version + // * save path not defined + // * if the file exists overwrite it, is not necessary ask + if (!storage.existsAsFile() && !storage.create()) { + showFailedDialog(R.string.error_file_creation); + return; + } + continueSelectedDownload(storage); + return; + } else if (targetFile == null) { + // This part is called if: + // * the filename is not used in a pending/finished download + // * the file does not exists, create + + if (!mainStorage.mkdirs()) { + showFailedDialog(R.string.error_path_creation); + return; + } + + storage = mainStorage.createFile(filename, mime); + if (storage == null || !storage.canWrite()) { + showFailedDialog(R.string.error_file_creation); + return; + } + + continueSelectedDownload(storage); + return; + } + msgBtn = R.string.overwrite; + msgBody = R.string.overwrite_unrelated_warning; break; default: return; } - int threads; - if (radioVideoAudioGroup.getCheckedRadioButtonId() == R.id.subtitle_button) { - threads = 1;// use unique thread for subtitles due small file size - fileName += ".srt";// final subtitle format - } else { - threads = threadsSeekBar.getProgress() + 1; - fileName += "." + stream.getFormat().getSuffix(); + AlertDialog.Builder askDialog = new AlertDialog.Builder(context) + .setTitle(R.string.download_dialog_title) + .setMessage(msgBody) + .setNegativeButton(android.R.string.cancel, null); + final StoredFileHelper finalStorage = storage; + + + if (mainStorage == null) { + // This part is called if: + // * using SAF on older android version + // * save path not defined + switch (state) { + case Pending: + case Finished: + askDialog.setPositiveButton(msgBtn, (dialog, which) -> { + dialog.dismiss(); + downloadManager.forgetMission(finalStorage); + continueSelectedDownload(finalStorage); + }); + break; + } + + askDialog.create().show(); + return; } - final String finalFileName = fileName; + askDialog.setPositiveButton(msgBtn, (dialog, which) -> { + dialog.dismiss(); - DownloadManagerService.checkForRunningMission(context, location, fileName, (listed, finished) -> { - if (listed) { - AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setTitle(R.string.download_dialog_title) - .setMessage(finished ? R.string.overwrite_warning : R.string.download_already_running) - .setPositiveButton( - finished ? R.string.overwrite : R.string.generate_unique_name, - (dialog, which) -> downloadSelected(context, stream, location, finalFileName, kind, threads) - ) - .setNegativeButton(android.R.string.cancel, (dialog, which) -> { - dialog.cancel(); - }) - .create() - .show(); - } else { - downloadSelected(context, stream, location, finalFileName, kind, threads); + StoredFileHelper storageNew; + switch (state) { + case Finished: + case Pending: + downloadManager.forgetMission(finalStorage); + case None: + if (targetFile == null) { + storageNew = mainStorage.createFile(filename, mime); + } else { + try { + // try take (or steal) the file + storageNew = new StoredFileHelper(context, mainStorage.getUri(), targetFile, mainStorage.getTag()); + } catch (IOException e) { + Log.e(TAG, "Failed to take (or steal) the file in " + targetFile.toString()); + storageNew = null; + } + } + + if (storageNew != null && storageNew.canWrite()) + continueSelectedDownload(storageNew); + else + showFailedDialog(R.string.error_file_creation); + break; + case PendingRunning: + storageNew = mainStorage.createUniqueFile(filename, mime); + if (storageNew == null) + showFailedDialog(R.string.error_file_creation); + else + continueSelectedDownload(storageNew); + break; } }); + + askDialog.create().show(); } - private void downloadSelected(Context context, Stream selectedStream, String location, String fileName, char kind, int threads) { + private void continueSelectedDownload(@NonNull StoredFileHelper storage) { + if (!storage.canWrite()) { + showFailedDialog(R.string.permission_denied); + return; + } + + // check if the selected file has to be overwritten, by simply checking its length + try { + if (storage.length() > 0) storage.truncate(); + } catch (IOException e) { + Log.e(TAG, "failed to truncate the file: " + storage.getUri().toString(), e); + showFailedDialog(R.string.overwrite_failed); + return; + } + + Stream selectedStream; + char kind; + int threads = threadsSeekBar.getProgress() + 1; String[] urls; String psName = null; String[] psArgs = null; String secondaryStreamUrl = null; long nearLength = 0; - if (selectedStream instanceof VideoStream) { - SecondaryStreamHelper secondaryStream = videoStreamsAdapter - .getAllSecondary() - .get(wrappedVideoStreams.getStreamsList().indexOf(selectedStream)); + // more download logic: select muxer, subtitle converter, etc. + switch (radioStreamsGroup.getCheckedRadioButtonId()) { + case R.id.audio_button: + kind = 'a'; + selectedStream = audioStreamsAdapter.getItem(selectedAudioIndex); - if (secondaryStream != null) { - secondaryStreamUrl = secondaryStream.getStream().getUrl(); - psName = selectedStream.getFormat() == MediaFormat.MPEG_4 ? Postprocessing.ALGORITHM_MP4_MUXER : Postprocessing.ALGORITHM_WEBM_MUXER; - psArgs = null; - long videoSize = wrappedVideoStreams.getSizeInBytes((VideoStream) selectedStream); - - // set nearLength, only, if both sizes are fetched or known. this probably does not work on slow networks - if (secondaryStream.getSizeInBytes() > 0 && videoSize > 0) { - nearLength = secondaryStream.getSizeInBytes() + videoSize; + if (selectedStream.getFormat() == MediaFormat.M4A) { + psName = Postprocessing.ALGORITHM_M4A_NO_DASH; } - } - } else if ((selectedStream instanceof SubtitlesStream) && selectedStream.getFormat() == MediaFormat.TTML) { - psName = Postprocessing.ALGORITHM_TTML_CONVERTER; - psArgs = new String[]{ - selectedStream.getFormat().getSuffix(), - "false",// ignore empty frames - "false",// detect youtube duplicate lines - }; + break; + case R.id.video_button: + kind = 'v'; + selectedStream = videoStreamsAdapter.getItem(selectedVideoIndex); + + SecondaryStreamHelper secondaryStream = videoStreamsAdapter + .getAllSecondary() + .get(wrappedVideoStreams.getStreamsList().indexOf(selectedStream)); + + if (secondaryStream != null) { + secondaryStreamUrl = secondaryStream.getStream().getUrl(); + + if (selectedStream.getFormat() == MediaFormat.MPEG_4) + psName = Postprocessing.ALGORITHM_MP4_FROM_DASH_MUXER; + else + psName = Postprocessing.ALGORITHM_WEBM_MUXER; + + psArgs = null; + long videoSize = wrappedVideoStreams.getSizeInBytes((VideoStream) selectedStream); + + // set nearLength, only, if both sizes are fetched or known. This probably + // does not work on slow networks but is later updated in the downloader + if (secondaryStream.getSizeInBytes() > 0 && videoSize > 0) { + nearLength = secondaryStream.getSizeInBytes() + videoSize; + } + } + break; + case R.id.subtitle_button: + threads = 1;// use unique thread for subtitles due small file size + kind = 's'; + selectedStream = subtitleStreamsAdapter.getItem(selectedSubtitleIndex); + + if (selectedStream.getFormat() == MediaFormat.TTML) { + psName = Postprocessing.ALGORITHM_TTML_CONVERTER; + psArgs = new String[]{ + selectedStream.getFormat().getSuffix(), + "false",// ignore empty frames + "false",// detect youtube duplicate lines + }; + } + break; + default: + return; } if (secondaryStreamUrl == null) { @@ -534,8 +841,8 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck urls = new String[]{selectedStream.getUrl(), secondaryStreamUrl}; } - DownloadManagerService.startMission(context, urls, location, fileName, kind, threads, currentInfo.getUrl(), psName, psArgs, nearLength); + DownloadManagerService.startMission(context, urls, storage, kind, threads, currentInfo.getUrl(), psName, psArgs, nearLength); - getDialog().dismiss(); + dismiss(); } } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/BaseStateFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/BaseStateFragment.java index acee1f111..8e328266e 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/BaseStateFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/BaseStateFragment.java @@ -1,10 +1,9 @@ package org.schabi.newpipe.fragments; import android.content.Intent; -import android.net.Uri; import android.os.Bundle; -import android.support.annotation.Nullable; -import android.support.annotation.StringRes; +import androidx.annotation.Nullable; +import androidx.annotation.StringRes; import android.util.Log; import android.view.View; import android.widget.Button; @@ -180,7 +179,7 @@ public abstract class BaseStateFragment extends BaseFragment implements ViewC } if (exception instanceof ReCaptchaException) { - onReCaptchaException(); + onReCaptchaException((ReCaptchaException) exception); return true; } else if (exception instanceof IOException) { showError(getString(R.string.network_error), true); @@ -190,11 +189,13 @@ public abstract class BaseStateFragment extends BaseFragment implements ViewC return false; } - public void onReCaptchaException() { + public void onReCaptchaException(ReCaptchaException exception) { if (DEBUG) Log.d(TAG, "onReCaptchaException() called"); Toast.makeText(activity, R.string.recaptcha_request_toast, Toast.LENGTH_LONG).show(); // Starting ReCaptcha Challenge Activity - startActivityForResult(new Intent(activity, ReCaptchaActivity.class), ReCaptchaActivity.RECAPTCHA_REQUEST); + Intent intent = new Intent(activity, ReCaptchaActivity.class); + intent.putExtra(ReCaptchaActivity.RECAPTCHA_URL_EXTRA, exception.getUrl()); + startActivityForResult(intent, ReCaptchaActivity.RECAPTCHA_REQUEST); showError(getString(R.string.recaptcha_request_toast), false); } @@ -230,21 +231,4 @@ public abstract class BaseStateFragment extends BaseFragment implements ViewC ErrorActivity.reportError(getContext(), exception, MainActivity.class, rootView, ErrorActivity.ErrorInfo.make(userAction, serviceName, request, errorId)); } - - /*////////////////////////////////////////////////////////////////////////// - // Utils - //////////////////////////////////////////////////////////////////////////*/ - - protected void openUrlInBrowser(String url) { - Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); - startActivity(Intent.createChooser(intent, activity.getString(R.string.share_dialog_title))); - } - - protected void shareUrl(String subject, String url) { - Intent intent = new Intent(Intent.ACTION_SEND); - intent.setType("text/plain"); - intent.putExtra(Intent.EXTRA_SUBJECT, subject); - intent.putExtra(Intent.EXTRA_TEXT, url); - startActivity(Intent.createChooser(intent, getString(R.string.share_dialog_title))); - } } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/BlankFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/BlankFragment.java index 948e9377d..1e284c711 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/BlankFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/BlankFragment.java @@ -1,7 +1,7 @@ package org.schabi.newpipe.fragments; import android.os.Bundle; -import android.support.annotation.Nullable; +import androidx.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; diff --git a/app/src/main/java/org/schabi/newpipe/fragments/EmptyFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/EmptyFragment.java index 0666667d6..de9716f28 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/EmptyFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/EmptyFragment.java @@ -1,7 +1,7 @@ package org.schabi.newpipe.fragments; import android.os.Bundle; -import android.support.annotation.Nullable; +import androidx.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; diff --git a/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java index de14997ef..e0661a49f 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java @@ -1,15 +1,6 @@ package org.schabi.newpipe.fragments; import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.design.widget.TabLayout; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentPagerAdapter; -import android.support.v4.view.ViewPager; -import android.support.v7.app.ActionBar; -import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; @@ -18,6 +9,17 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AppCompatActivity; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentPagerAdapter; +import androidx.viewpager.widget.ViewPager; + +import com.google.android.material.tabs.TabLayout; + import org.schabi.newpipe.BaseFragment; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.exceptions.ExtractionException; @@ -50,6 +52,8 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte super.onCreate(savedInstanceState); setHasOptionsMenu(true); + destroyOldFragments(); + tabsManager = TabsManager.getManager(activity); tabsManager.setSavedTabsListener(() -> { if (DEBUG) { @@ -63,6 +67,17 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte }); } + private void destroyOldFragments() { + for (Fragment fragment : getChildFragmentManager().getFragments()) { + if (fragment != null) { + getChildFragmentManager() + .beginTransaction() + .remove(fragment) + .commitNowAllowingStateLoss(); + } + } + } + @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_main, container, false); @@ -98,6 +113,7 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte public void onDestroy() { super.onDestroy(); tabsManager.unsetSavedTabsListener(); + if (viewPager != null) viewPager.setAdapter(null); } /*////////////////////////////////////////////////////////////////////////// @@ -144,6 +160,7 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte viewPager.setOffscreenPageLimit(pagerAdapter.getCount()); updateTabsIcon(); + updateTabsContentDescription(); updateCurrentTitle(); } @@ -156,6 +173,17 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte } } + private void updateTabsContentDescription() { + for (int i = 0; i < tabsList.size(); i++) { + final TabLayout.Tab tabToSet = tabLayout.getTabAt(i); + if (tabToSet != null) { + final Tab t = tabsList.get(i); + tabToSet.setIcon(t.getTabIconRes(activity)); + tabToSet.setContentDescription(t.getTabName(activity)); + } + } + } + private void updateCurrentTitle() { setTitle(tabsList.get(viewPager.getCurrentItem()).getTabName(requireContext())); } @@ -177,6 +205,7 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte } private class SelectedTabsPagerAdapter extends FragmentPagerAdapter { + private SelectedTabsPagerAdapter(FragmentManager fragmentManager) { super(fragmentManager); } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/OnScrollBelowItemsListener.java b/app/src/main/java/org/schabi/newpipe/fragments/OnScrollBelowItemsListener.java index 774e6cc03..887097679 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/OnScrollBelowItemsListener.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/OnScrollBelowItemsListener.java @@ -1,8 +1,8 @@ package org.schabi.newpipe.fragments; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.support.v7.widget.StaggeredGridLayoutManager; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; +import androidx.recyclerview.widget.StaggeredGridLayoutManager; /** * Recycler view scroll listener which calls the method {@link #onScrolledDown(RecyclerView)} diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/TabAdaptor.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/TabAdaptor.java index 27cc3ec8a..d86226e92 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/TabAdaptor.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/TabAdaptor.java @@ -1,8 +1,9 @@ package org.schabi.newpipe.fragments.detail; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentPagerAdapter; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentPagerAdapter; import android.view.ViewGroup; import java.util.ArrayList; @@ -61,6 +62,18 @@ public class TabAdaptor extends FragmentPagerAdapter { else return POSITION_NONE; } + public int getItemPositionByTitle(String title) { + return mFragmentTitleList.indexOf(title); + } + + @Nullable + public String getItemTitle(int position) { + if (position < 0 || position >= mFragmentTitleList.size()) { + return null; + } + return mFragmentTitleList.get(position); + } + public void notifyDataSetUpdate(){ notifyDataSetChanged(); } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index 8c87c1875..37d8851ea 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -9,17 +9,17 @@ import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; -import android.support.annotation.DrawableRes; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.design.widget.AppBarLayout; -import android.support.design.widget.TabLayout; -import android.support.v4.app.Fragment; -import android.support.v4.content.ContextCompat; -import android.support.v4.view.ViewPager; -import android.support.v7.app.ActionBar; -import android.support.v7.app.AlertDialog; -import android.support.v7.app.AppCompatActivity; +import androidx.annotation.DrawableRes; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import com.google.android.material.appbar.AppBarLayout; +import com.google.android.material.tabs.TabLayout; +import androidx.fragment.app.Fragment; +import androidx.core.content.ContextCompat; +import androidx.viewpager.widget.ViewPager; +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.app.AppCompatActivity; import android.text.Html; import android.text.Spanned; import android.text.TextUtils; @@ -60,7 +60,6 @@ import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExt import org.schabi.newpipe.extractor.stream.AudioStream; import org.schabi.newpipe.extractor.stream.Stream; import org.schabi.newpipe.extractor.stream.StreamInfo; -import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.stream.StreamType; import org.schabi.newpipe.extractor.stream.VideoStream; import org.schabi.newpipe.fragments.BackPressable; @@ -68,7 +67,6 @@ import org.schabi.newpipe.fragments.BaseStateFragment; import org.schabi.newpipe.fragments.EmptyFragment; import org.schabi.newpipe.fragments.list.comments.CommentsFragment; import org.schabi.newpipe.fragments.list.videos.RelatedVideosFragment; -import org.schabi.newpipe.info_list.InfoItemDialog; import org.schabi.newpipe.local.dialog.PlaylistAppendDialog; import org.schabi.newpipe.local.history.HistoryRecordManager; import org.schabi.newpipe.player.MainVideoPlayer; @@ -77,7 +75,6 @@ import org.schabi.newpipe.player.playqueue.PlayQueue; import org.schabi.newpipe.player.playqueue.SinglePlayQueue; import org.schabi.newpipe.report.ErrorActivity; import org.schabi.newpipe.report.UserAction; -import org.schabi.newpipe.util.AnimationUtils; import org.schabi.newpipe.util.Constants; import org.schabi.newpipe.util.ExtractorHelper; import org.schabi.newpipe.util.ImageDisplayConstants; @@ -86,14 +83,16 @@ import org.schabi.newpipe.util.ListHelper; import org.schabi.newpipe.util.Localization; import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.PermissionHelper; +import org.schabi.newpipe.util.ShareUtils; import org.schabi.newpipe.util.StreamItemAdapter; import org.schabi.newpipe.util.StreamItemAdapter.StreamSizeWrapper; +import org.schabi.newpipe.views.AnimatedProgressBar; import java.io.Serializable; import java.util.Collection; -import java.util.Collections; import java.util.LinkedList; import java.util.List; +import java.util.concurrent.TimeUnit; import icepick.State; import io.reactivex.Single; @@ -117,11 +116,12 @@ public class VideoDetailFragment private static final int RELATED_STREAMS_UPDATE_FLAG = 0x1; private static final int RESOLUTIONS_MENU_UPDATE_FLAG = 0x2; private static final int TOOLBAR_ITEMS_UPDATE_FLAG = 0x4; - private static final int COMMENTS_UPDATE_FLAG = 0x4; + private static final int COMMENTS_UPDATE_FLAG = 0x8; private boolean autoPlayEnabled; private boolean showRelatedStreams; private boolean showComments; + private String selectedTabTag; @State protected int serviceId = Constants.NO_SERVICE_ID; @@ -134,6 +134,8 @@ public class VideoDetailFragment private Disposable currentWorker; @NonNull private CompositeDisposable disposables = new CompositeDisposable(); + @Nullable + private Disposable positionSubscriber = null; private List sortedVideoStreams; private int selectedVideoStreamIndex = -1; @@ -151,6 +153,7 @@ public class VideoDetailFragment private View thumbnailBackgroundButton; private ImageView thumbnailImageView; private ImageView thumbnailPlayButton; + private AnimatedProgressBar positionView; private View videoTitleRoot; private TextView videoTitleTextView; @@ -163,6 +166,7 @@ public class VideoDetailFragment private TextView detailControlsDownload; private TextView appendControlsDetail; private TextView detailDurationView; + private TextView detailPositionView; private LinearLayout videoDescriptionRootLayout; private TextView videoUploadDateView; @@ -213,6 +217,9 @@ public class VideoDetailFragment showComments = PreferenceManager.getDefaultSharedPreferences(activity) .getBoolean(getString(R.string.show_comments_key), true); + selectedTabTag = PreferenceManager.getDefaultSharedPreferences(activity) + .getString(getString(R.string.stream_info_selected_tab_key), COMMENTS_TAB_TAG); + PreferenceManager.getDefaultSharedPreferences(activity) .registerOnSharedPreferenceChangeListener(this); } @@ -226,6 +233,10 @@ public class VideoDetailFragment public void onPause() { super.onPause(); if (currentWorker != null) currentWorker.dispose(); + PreferenceManager.getDefaultSharedPreferences(getContext()) + .edit() + .putString(getString(R.string.stream_info_selected_tab_key), pageAdapter.getItemTitle(viewPager.getCurrentItem())) + .apply(); } @Override @@ -250,6 +261,8 @@ public class VideoDetailFragment // Check if it was loading when the fragment was stopped/paused, if (wasLoading.getAndSet(false)) { selectAndLoadVideo(serviceId, url, name); + } else if (currentInfo != null) { + updateProgressInfo(currentInfo); } } @@ -259,8 +272,10 @@ public class VideoDetailFragment PreferenceManager.getDefaultSharedPreferences(activity) .unregisterOnSharedPreferenceChangeListener(this); + if (positionSubscriber != null) positionSubscriber.dispose(); if (currentWorker != null) currentWorker.dispose(); if (disposables != null) disposables.clear(); + positionSubscriber = null; currentWorker = null; disposables = null; } @@ -453,6 +468,7 @@ public class VideoDetailFragment videoTitleTextView = rootView.findViewById(R.id.detail_video_title_view); videoTitleToggleArrow = rootView.findViewById(R.id.detail_toggle_description_view); videoCountView = rootView.findViewById(R.id.detail_view_count_view); + positionView = rootView.findViewById(R.id.position_view); detailControlsBackground = rootView.findViewById(R.id.detail_controls_background); detailControlsPopup = rootView.findViewById(R.id.detail_controls_popup); @@ -460,6 +476,7 @@ public class VideoDetailFragment detailControlsDownload = rootView.findViewById(R.id.detail_controls_download); appendControlsDetail = rootView.findViewById(R.id.touch_append_detail); detailDurationView = rootView.findViewById(R.id.detail_duration_view); + detailPositionView = rootView.findViewById(R.id.detail_position_view); videoDescriptionRootLayout = rootView.findViewById(R.id.detail_description_root_layout); videoUploadDateView = rootView.findViewById(R.id.detail_upload_date_view); @@ -467,7 +484,6 @@ public class VideoDetailFragment videoDescriptionView.setMovementMethod(LinkMovementMethod.getInstance()); videoDescriptionView.setAutoLinkMask(Linkify.WEB_URLS); - //thumbsRootLayout = rootView.findViewById(R.id.detail_thumbs_root_layout); thumbsUpTextView = rootView.findViewById(R.id.detail_thumbs_up_count_view); thumbsUpImageView = rootView.findViewById(R.id.detail_thumbs_up_img_view); thumbsDownTextView = rootView.findViewById(R.id.detail_thumbs_down_count_view); @@ -513,42 +529,6 @@ public class VideoDetailFragment detailControlsPopup.setOnTouchListener(getOnControlsTouchListener()); } - private void showStreamDialog(final StreamInfoItem item) { - final Context context = getContext(); - if (context == null || context.getResources() == null || getActivity() == null) return; - - final String[] commands = new String[]{ - context.getResources().getString(R.string.enqueue_on_background), - context.getResources().getString(R.string.enqueue_on_popup), - context.getResources().getString(R.string.append_playlist), - context.getResources().getString(R.string.share) - }; - - final DialogInterface.OnClickListener actions = (DialogInterface dialogInterface, int i) -> { - switch (i) { - case 0: - NavigationHelper.enqueueOnBackgroundPlayer(context, new SinglePlayQueue(item)); - break; - case 1: - NavigationHelper.enqueueOnPopupPlayer(getActivity(), new SinglePlayQueue(item)); - break; - case 2: - if (getFragmentManager() != null) { - PlaylistAppendDialog.fromStreamInfoItems(Collections.singletonList(item)) - .show(getFragmentManager(), TAG); - } - break; - case 3: - shareUrl(item.getName(), item.getUrl()); - break; - default: - break; - } - }; - - new InfoItemDialog(getActivity(), item, commands, actions).show(); - } - private View.OnTouchListener getOnControlsTouchListener() { return (View view, MotionEvent motionEvent) -> { if (!PreferenceManager.getDefaultSharedPreferences(activity) @@ -628,13 +608,13 @@ public class VideoDetailFragment switch (id) { case R.id.menu_item_share: { if (currentInfo != null) { - shareUrl(currentInfo.getName(), currentInfo.getOriginalUrl()); + ShareUtils.shareUrl(this.getContext(), currentInfo.getName(), currentInfo.getOriginalUrl()); } return true; } case R.id.menu_item_openInBrowser: { if (currentInfo != null) { - openUrlInBrowser(currentInfo.getOriginalUrl()); + ShareUtils.openUrlInBrowser(this.getContext(), currentInfo.getOriginalUrl()); } return true; } @@ -815,6 +795,9 @@ public class VideoDetailFragment } private void initTabs() { + if (pageAdapter.getCount() != 0) { + selectedTabTag = pageAdapter.getItemTitle(viewPager.getCurrentItem()); + } pageAdapter.clearAllItems(); if(shouldShowComments()){ @@ -835,6 +818,8 @@ public class VideoDetailFragment if(pageAdapter.getCount() < 2){ tabLayout.setVisibility(View.GONE); }else{ + int position = pageAdapter.getItemPositionByTitle(selectedTabTag); + if(position != -1) viewPager.setCurrentItem(position); tabLayout.setVisibility(View.VISIBLE); } } @@ -876,11 +861,11 @@ public class VideoDetailFragment final PlayQueue itemQueue = new SinglePlayQueue(currentInfo); if (append) { - NavigationHelper.enqueueOnPopupPlayer(activity, itemQueue); + NavigationHelper.enqueueOnPopupPlayer(activity, itemQueue, false); } else { Toast.makeText(activity, R.string.popup_playing_toast, Toast.LENGTH_SHORT).show(); final Intent intent = NavigationHelper.getPlayerIntent( - activity, PopupVideoPlayer.class, itemQueue, getSelectedVideoStream().resolution + activity, PopupVideoPlayer.class, itemQueue, getSelectedVideoStream().resolution, true ); activity.startService(intent); } @@ -900,9 +885,9 @@ public class VideoDetailFragment private void openNormalBackgroundPlayer(final boolean append) { final PlayQueue itemQueue = new SinglePlayQueue(currentInfo); if (append) { - NavigationHelper.enqueueOnBackgroundPlayer(activity, itemQueue); + NavigationHelper.enqueueOnBackgroundPlayer(activity, itemQueue, false); } else { - NavigationHelper.playOnBackgroundPlayer(activity, itemQueue); + NavigationHelper.playOnBackgroundPlayer(activity, itemQueue, true); } } @@ -912,7 +897,7 @@ public class VideoDetailFragment mIntent = NavigationHelper.getPlayerIntent(activity, MainVideoPlayer.class, playQueue, - getSelectedVideoStream().getResolution()); + getSelectedVideoStream().getResolution(), true); startActivity(mIntent); } @@ -979,7 +964,7 @@ public class VideoDetailFragment } private void showContent() { - AnimationUtils.slideUp(contentRootLayoutHiding,120, 96, 0.06f); + contentRootLayoutHiding.setVisibility(View.VISIBLE); } protected void setInitialData(int serviceId, String url, String name) { @@ -1012,12 +997,19 @@ public class VideoDetailFragment @Override public void showLoading() { + super.showLoading(); - contentRootLayoutHiding.setVisibility(View.INVISIBLE); + //if data is already cached, transition from VISIBLE -> INVISIBLE -> VISIBLE is not required + if(!ExtractorHelper.isCached(serviceId, url, InfoItem.InfoType.STREAM)){ + contentRootLayoutHiding.setVisibility(View.INVISIBLE); + } + animateView(spinnerToolbar, false, 200); animateView(thumbnailPlayButton, false, 50); animateView(detailDurationView, false, 100); + animateView(detailPositionView, false, 100); + animateView(positionView, false, 50); videoTitleTextView.setText(name != null ? name : ""); videoTitleTextView.setMaxLines(1); @@ -1132,6 +1124,7 @@ public class VideoDetailFragment videoUploadDateView.setText(Localization.localizeDate(activity, info.getUploadDate())); } prepareDescription(info.getDescription()); + updateProgressInfo(info); animateView(spinnerToolbar, true, 500); setupActionBar(info); @@ -1181,7 +1174,7 @@ public class VideoDetailFragment downloadDialog.setSelectedVideoStream(selectedVideoStreamIndex); downloadDialog.setSubtitleStreams(currentInfo.getSubtitles()); - downloadDialog.show(activity.getSupportFragmentManager(), "downloadDialog"); + downloadDialog.show(getActivity().getSupportFragmentManager(), "downloadDialog"); } catch (Exception e) { ErrorActivity.ErrorInfo info = ErrorActivity.ErrorInfo.make(UserAction.UI_ERROR, ServiceList.all() @@ -1206,9 +1199,7 @@ public class VideoDetailFragment protected boolean onError(Throwable exception) { if (super.onError(exception)) return true; - if (exception instanceof YoutubeStreamExtractor.GemaException) { - onBlockedByGemaError(); - } else if (exception instanceof ContentNotAvailableException) { + else if (exception instanceof ContentNotAvailableException) { showError(getString(R.string.content_not_available), false); } else { int errorId = exception instanceof YoutubeStreamExtractor.DecryptException @@ -1226,14 +1217,36 @@ public class VideoDetailFragment return true; } - public void onBlockedByGemaError() { - thumbnailBackgroundButton.setOnClickListener((View v) -> { - Intent intent = new Intent(); - intent.setAction(Intent.ACTION_VIEW); - intent.setData(Uri.parse(getString(R.string.c3s_url))); - startActivity(intent); - }); - - showError(getString(R.string.blocked_by_gema), false, R.drawable.gruese_die_gema); + private void updateProgressInfo(@NonNull final StreamInfo info) { + if (positionSubscriber != null) { + positionSubscriber.dispose(); + } + final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity); + final boolean playbackResumeEnabled = + prefs.getBoolean(activity.getString(R.string.enable_watch_history_key), true) + && prefs.getBoolean(activity.getString(R.string.enable_playback_resume_key), true); + if (!playbackResumeEnabled || info.getDuration() <= 0) { + positionView.setVisibility(View.INVISIBLE); + detailPositionView.setVisibility(View.GONE); + return; + } + final HistoryRecordManager recordManager = new HistoryRecordManager(requireContext()); + positionSubscriber = recordManager.loadStreamState(info) + .subscribeOn(Schedulers.io()) + .onErrorComplete() + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(state -> { + final int seconds = (int) TimeUnit.MILLISECONDS.toSeconds(state.getProgressTime()); + positionView.setMax((int) info.getDuration()); + positionView.setProgressAnimated(seconds); + detailPositionView.setText(Localization.getDurationString(seconds)); + animateView(positionView, true, 500); + animateView(detailPositionView, true, 500); + }, e -> { + if (DEBUG) e.printStackTrace(); + }, () -> { + animateView(positionView, false, 500); + animateView(detailPositionView, false, 500); + }); } } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java index b61fe0d02..d6fd1dd00 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java @@ -2,18 +2,17 @@ package org.schabi.newpipe.fragments.list; import android.app.Activity; import android.content.Context; -import android.content.DialogInterface; import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.Resources; import android.os.Bundle; import android.preference.PreferenceManager; -import android.support.annotation.NonNull; -import android.support.v7.app.ActionBar; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.GridLayoutManager; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; +import androidx.annotation.NonNull; +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AppCompatActivity; +import androidx.recyclerview.widget.GridLayoutManager; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; @@ -25,18 +24,17 @@ import org.schabi.newpipe.extractor.channel.ChannelInfoItem; import org.schabi.newpipe.extractor.comments.CommentsInfoItem; import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem; import org.schabi.newpipe.extractor.stream.StreamInfoItem; +import org.schabi.newpipe.extractor.stream.StreamType; import org.schabi.newpipe.fragments.BaseStateFragment; import org.schabi.newpipe.fragments.OnScrollBelowItemsListener; import org.schabi.newpipe.info_list.InfoItemDialog; import org.schabi.newpipe.info_list.InfoListAdapter; -import org.schabi.newpipe.local.dialog.PlaylistAppendDialog; -import org.schabi.newpipe.player.playqueue.SinglePlayQueue; import org.schabi.newpipe.report.ErrorActivity; import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.OnClickGesture; import org.schabi.newpipe.util.StateSaver; +import org.schabi.newpipe.util.StreamDialogEntry; -import java.util.Collections; import java.util.List; import java.util.Queue; @@ -64,6 +62,11 @@ public abstract class BaseListFragment extends BaseStateFragment implem infoListAdapter = new InfoListAdapter(activity); } + @Override + public void onDetach() { + super.onDetach(); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -249,41 +252,32 @@ public abstract class BaseListFragment extends BaseStateFragment implem } } + + + protected void showStreamDialog(final StreamInfoItem item) { final Context context = getContext(); final Activity activity = getActivity(); - if (context == null || context.getResources() == null || getActivity() == null) return; + if (context == null || context.getResources() == null || activity == null) return; - final String[] commands = new String[]{ - context.getResources().getString(R.string.enqueue_on_background), - context.getResources().getString(R.string.enqueue_on_popup), - context.getResources().getString(R.string.append_playlist), - context.getResources().getString(R.string.share) - }; + if (item.getStreamType() == StreamType.AUDIO_STREAM) { + StreamDialogEntry.setEnabledEntries( + StreamDialogEntry.enqueue_on_background, + StreamDialogEntry.start_here_on_background, + StreamDialogEntry.append_playlist, + StreamDialogEntry.share); + } else { + StreamDialogEntry.setEnabledEntries( + StreamDialogEntry.enqueue_on_background, + StreamDialogEntry.enqueue_on_popup, + StreamDialogEntry.start_here_on_background, + StreamDialogEntry.start_here_on_popup, + StreamDialogEntry.append_playlist, + StreamDialogEntry.share); + } - final DialogInterface.OnClickListener actions = (dialogInterface, i) -> { - switch (i) { - case 0: - NavigationHelper.enqueueOnBackgroundPlayer(context, new SinglePlayQueue(item)); - break; - case 1: - NavigationHelper.enqueueOnPopupPlayer(activity, new SinglePlayQueue(item)); - break; - case 2: - if (getFragmentManager() != null) { - PlaylistAppendDialog.fromStreamInfoItems(Collections.singletonList(item)) - .show(getFragmentManager(), TAG); - } - break; - case 3: - shareUrl(item.getName(), item.getUrl()); - break; - default: - break; - } - }; - - new InfoItemDialog(getActivity(), item, commands, actions).show(); + new InfoItemDialog(activity, item, StreamDialogEntry.getCommands(context), (dialog, which) -> + StreamDialogEntry.clickOn(which, this, item)).show(); } /*////////////////////////////////////////////////////////////////////////// diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListInfoFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListInfoFragment.java index 5d042c949..764af271a 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListInfoFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListInfoFragment.java @@ -1,11 +1,12 @@ package org.schabi.newpipe.fragments.list; import android.os.Bundle; -import android.support.annotation.NonNull; import android.text.TextUtils; import android.util.Log; import android.view.View; +import androidx.annotation.NonNull; + import org.schabi.newpipe.extractor.ListExtractor; import org.schabi.newpipe.extractor.ListInfo; import org.schabi.newpipe.util.Constants; @@ -61,8 +62,10 @@ public abstract class BaseListInfoFragment @Override public void onDestroy() { super.onDestroy(); - if (currentWorker != null) currentWorker.dispose(); - currentWorker = null; + if (currentWorker != null) { + currentWorker.dispose(); + currentWorker = null; + } } /*////////////////////////////////////////////////////////////////////////// diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java index d9bfe842f..32bc6aeb3 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java @@ -1,15 +1,13 @@ package org.schabi.newpipe.fragments.list.channel; -import android.app.Activity; import android.content.Context; -import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.content.ContextCompat; -import android.support.v7.app.ActionBar; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; +import androidx.appcompat.app.ActionBar; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; @@ -35,21 +33,18 @@ import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.fragments.list.BaseListInfoFragment; -import org.schabi.newpipe.info_list.InfoItemDialog; -import org.schabi.newpipe.local.dialog.PlaylistAppendDialog; import org.schabi.newpipe.local.subscription.SubscriptionService; import org.schabi.newpipe.player.playqueue.ChannelPlayQueue; import org.schabi.newpipe.player.playqueue.PlayQueue; -import org.schabi.newpipe.player.playqueue.SinglePlayQueue; import org.schabi.newpipe.report.UserAction; import org.schabi.newpipe.util.AnimationUtils; import org.schabi.newpipe.util.ExtractorHelper; import org.schabi.newpipe.util.ImageDisplayConstants; import org.schabi.newpipe.util.Localization; import org.schabi.newpipe.util.NavigationHelper; +import org.schabi.newpipe.util.ShareUtils; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; @@ -150,56 +145,6 @@ public class ChannelFragment extends BaseListInfoFragment { return headerRootLayout; } - @Override - protected void showStreamDialog(final StreamInfoItem item) { - final Activity activity = getActivity(); - final Context context = getContext(); - if (context == null || context.getResources() == null || getActivity() == null) return; - - final String[] commands = new String[]{ - context.getResources().getString(R.string.enqueue_on_background), - context.getResources().getString(R.string.enqueue_on_popup), - context.getResources().getString(R.string.start_here_on_main), - context.getResources().getString(R.string.start_here_on_background), - context.getResources().getString(R.string.start_here_on_popup), - context.getResources().getString(R.string.append_playlist), - context.getResources().getString(R.string.share) - }; - - final DialogInterface.OnClickListener actions = (DialogInterface dialogInterface, int i) -> { - final int index = Math.max(infoListAdapter.getItemsList().indexOf(item), 0); - switch (i) { - case 0: - NavigationHelper.enqueueOnBackgroundPlayer(context, new SinglePlayQueue(item)); - break; - case 1: - NavigationHelper.enqueueOnPopupPlayer(activity, new SinglePlayQueue(item)); - break; - case 2: - NavigationHelper.playOnMainPlayer(context, getPlayQueue(index)); - break; - case 3: - NavigationHelper.playOnBackgroundPlayer(context, getPlayQueue(index)); - break; - case 4: - NavigationHelper.playOnPopupPlayer(activity, getPlayQueue(index)); - break; - case 5: - if (getFragmentManager() != null) { - PlaylistAppendDialog.fromStreamInfoItems(Collections.singletonList(item)) - .show(getFragmentManager(), TAG); - } - break; - case 6: - shareUrl(item.getName(), item.getUrl()); - break; - default: - break; - } - }; - - new InfoItemDialog(getActivity(), item, commands, actions).show(); - } /*////////////////////////////////////////////////////////////////////////// // Menu //////////////////////////////////////////////////////////////////////////*/ @@ -234,10 +179,10 @@ public class ChannelFragment extends BaseListInfoFragment { openRssFeed(); break; case R.id.menu_item_openInBrowser: - openUrlInBrowser(currentInfo.getOriginalUrl()); + ShareUtils.openUrlInBrowser(this.getContext(), currentInfo.getOriginalUrl()); break; case R.id.menu_item_share: - shareUrl(name, currentInfo.getOriginalUrl()); + ShareUtils.shareUrl(this.getContext(), name, currentInfo.getOriginalUrl()); break; default: return super.onOptionsItemSelected(item); @@ -440,11 +385,11 @@ public class ChannelFragment extends BaseListInfoFragment { monitorSubscription(result); headerPlayAllButton.setOnClickListener( - view -> NavigationHelper.playOnMainPlayer(activity, getPlayQueue())); + view -> NavigationHelper.playOnMainPlayer(activity, getPlayQueue(), false)); headerPopupButton.setOnClickListener( - view -> NavigationHelper.playOnPopupPlayer(activity, getPlayQueue())); + view -> NavigationHelper.playOnPopupPlayer(activity, getPlayQueue(), false)); headerBackgroundButton.setOnClickListener( - view -> NavigationHelper.playOnBackgroundPlayer(activity, getPlayQueue())); + view -> NavigationHelper.playOnBackgroundPlayer(activity, getPlayQueue(), false)); } private PlayQueue getPlayQueue() { diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentsFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentsFragment.java index 956e6c1c8..edaf0ec2b 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentsFragment.java @@ -2,8 +2,8 @@ package org.schabi.newpipe.fragments.list.comments; import android.content.Context; import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -93,7 +93,7 @@ public class CommentsFragment extends BaseListInfoFragment { public void handleResult(@NonNull CommentsInfo result) { super.handleResult(result); - AnimationUtils.slideUp(getView(),120, 96, 0.06f); + AnimationUtils.slideUp(getView(),120, 150, 0.06f); if (!result.getErrors().isEmpty()) { showSnackBarError(result.getErrors(), UserAction.REQUESTED_COMMENTS, NewPipe.getNameOfService(result.getServiceId()), result.getUrl(), 0); diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/kiosk/KioskFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/kiosk/KioskFragment.java index 7d4500691..6eaa2ea70 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/kiosk/KioskFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/kiosk/KioskFragment.java @@ -1,10 +1,9 @@ package org.schabi.newpipe.fragments.list.kiosk; import android.os.Bundle; -import android.preference.PreferenceManager; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v7.app.ActionBar; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.ActionBar; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -155,9 +154,7 @@ public class KioskFragment extends BaseListInfoFragment { super.handleResult(result); name = kioskTranslatedName; - if(!useAsFrontPage) { - setTitle(kioskTranslatedName); - } + setTitle(kioskTranslatedName); if (!result.getErrors().isEmpty()) { showSnackBarError(result.getErrors(), diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/playlist/PlaylistFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/playlist/PlaylistFragment.java index d552b4e66..32b83bb22 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/playlist/PlaylistFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/playlist/PlaylistFragment.java @@ -2,11 +2,10 @@ package org.schabi.newpipe.fragments.list.playlist; import android.app.Activity; import android.content.Context; -import android.content.DialogInterface; import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v7.app.AppCompatActivity; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; @@ -29,17 +28,19 @@ import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.playlist.PlaylistInfo; import org.schabi.newpipe.extractor.stream.StreamInfoItem; +import org.schabi.newpipe.extractor.stream.StreamType; import org.schabi.newpipe.fragments.list.BaseListInfoFragment; import org.schabi.newpipe.info_list.InfoItemDialog; import org.schabi.newpipe.local.playlist.RemotePlaylistManager; import org.schabi.newpipe.player.playqueue.PlayQueue; import org.schabi.newpipe.player.playqueue.PlaylistPlayQueue; -import org.schabi.newpipe.player.playqueue.SinglePlayQueue; import org.schabi.newpipe.report.ErrorActivity; import org.schabi.newpipe.report.UserAction; import org.schabi.newpipe.util.ExtractorHelper; import org.schabi.newpipe.util.ImageDisplayConstants; import org.schabi.newpipe.util.NavigationHelper; +import org.schabi.newpipe.util.ShareUtils; +import org.schabi.newpipe.util.StreamDialogEntry; import org.schabi.newpipe.util.ThemeHelper; import java.util.ArrayList; @@ -134,48 +135,40 @@ public class PlaylistFragment extends BaseListInfoFragment { infoListAdapter.useMiniItemVariants(true); } + private PlayQueue getPlayQueueStartingAt(StreamInfoItem infoItem) { + return getPlayQueue(Math.max(infoListAdapter.getItemsList().indexOf(infoItem), 0)); + } + @Override - protected void showStreamDialog(final StreamInfoItem item) { + protected void showStreamDialog(StreamInfoItem item) { final Context context = getContext(); final Activity activity = getActivity(); - if (context == null || context.getResources() == null || getActivity() == null) return; + if (context == null || context.getResources() == null || activity == null) return; - final String[] commands = new String[]{ - context.getResources().getString(R.string.enqueue_on_background), - context.getResources().getString(R.string.enqueue_on_popup), - context.getResources().getString(R.string.start_here_on_main), - context.getResources().getString(R.string.start_here_on_background), - context.getResources().getString(R.string.start_here_on_popup), - context.getResources().getString(R.string.share) - }; + if (item.getStreamType() == StreamType.AUDIO_STREAM) { + StreamDialogEntry.setEnabledEntries( + StreamDialogEntry.enqueue_on_background, + StreamDialogEntry.start_here_on_background, + StreamDialogEntry.append_playlist, + StreamDialogEntry.share); + } else { + StreamDialogEntry.setEnabledEntries( + StreamDialogEntry.enqueue_on_background, + StreamDialogEntry.enqueue_on_popup, + StreamDialogEntry.start_here_on_background, + StreamDialogEntry.start_here_on_popup, + StreamDialogEntry.append_playlist, + StreamDialogEntry.share); - final DialogInterface.OnClickListener actions = (dialogInterface, i) -> { - final int index = Math.max(infoListAdapter.getItemsList().indexOf(item), 0); - switch (i) { - case 0: - NavigationHelper.enqueueOnBackgroundPlayer(context, new SinglePlayQueue(item)); - break; - case 1: - NavigationHelper.enqueueOnPopupPlayer(activity, new SinglePlayQueue(item)); - break; - case 2: - NavigationHelper.playOnMainPlayer(context, getPlayQueue(index)); - break; - case 3: - NavigationHelper.playOnBackgroundPlayer(context, getPlayQueue(index)); - break; - case 4: - NavigationHelper.playOnPopupPlayer(activity, getPlayQueue(index)); - break; - case 5: - shareUrl(item.getName(), item.getUrl()); - break; - default: - break; - } - }; + StreamDialogEntry.start_here_on_popup.setCustomAction( + (fragment, infoItem) -> NavigationHelper.playOnPopupPlayer(context, getPlayQueueStartingAt(infoItem), true)); + } - new InfoItemDialog(getActivity(), item, commands, actions).show(); + StreamDialogEntry.start_here_on_background.setCustomAction( + (fragment, infoItem) -> NavigationHelper.playOnBackgroundPlayer(context, getPlayQueueStartingAt(infoItem), true)); + + new InfoItemDialog(activity, item, StreamDialogEntry.getCommands(context), (dialog, which) -> + StreamDialogEntry.clickOn(which, this, item)).show(); } @Override @@ -230,10 +223,10 @@ public class PlaylistFragment extends BaseListInfoFragment { public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_item_openInBrowser: - openUrlInBrowser(url); + ShareUtils.openUrlInBrowser(this.getContext(), url); break; case R.id.menu_item_share: - shareUrl(name, url); + ShareUtils.shareUrl(this.getContext(), name, url); break; case R.id.menu_item_bookmark: onBookmarkClicked(); @@ -300,19 +293,19 @@ public class PlaylistFragment extends BaseListInfoFragment { .subscribe(getPlaylistBookmarkSubscriber()); headerPlayAllButton.setOnClickListener(view -> - NavigationHelper.playOnMainPlayer(activity, getPlayQueue())); + NavigationHelper.playOnMainPlayer(activity, getPlayQueue(), false)); headerPopupButton.setOnClickListener(view -> - NavigationHelper.playOnPopupPlayer(activity, getPlayQueue())); + NavigationHelper.playOnPopupPlayer(activity, getPlayQueue(), false)); headerBackgroundButton.setOnClickListener(view -> - NavigationHelper.playOnBackgroundPlayer(activity, getPlayQueue())); + NavigationHelper.playOnBackgroundPlayer(activity, getPlayQueue(), false)); headerPopupButton.setOnLongClickListener(view -> { - NavigationHelper.enqueueOnPopupPlayer(activity, getPlayQueue()); + NavigationHelper.enqueueOnPopupPlayer(activity, getPlayQueue(), true); return true; }); headerBackgroundButton.setOnLongClickListener(view -> { - NavigationHelper.enqueueOnBackgroundPlayer(activity, getPlayQueue()); + NavigationHelper.enqueueOnBackgroundPlayer(activity, getPlayQueue(), true); return true; }); } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java index a3b01f251..f2e8aa244 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java @@ -6,13 +6,13 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v7.app.ActionBar; -import android.support.v7.app.AlertDialog; -import android.support.v7.widget.RecyclerView; -import android.support.v7.widget.TooltipCompat; -import android.support.v7.widget.helper.ItemTouchHelper; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AlertDialog; +import androidx.recyclerview.widget.RecyclerView; +import androidx.appcompat.widget.TooltipCompat; +import androidx.recyclerview.widget.ItemTouchHelper; import android.text.Editable; import android.text.TextUtils; import android.text.TextWatcher; @@ -73,7 +73,7 @@ import io.reactivex.disposables.Disposable; import io.reactivex.schedulers.Schedulers; import io.reactivex.subjects.PublishSubject; -import static android.support.v7.widget.helper.ItemTouchHelper.Callback.makeMovementFlags; +import static androidx.recyclerview.widget.ItemTouchHelper.Callback.makeMovementFlags; import static java.util.Arrays.asList; import static org.schabi.newpipe.util.AnimationUtils.animateView; diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/search/SuggestionListAdapter.java b/app/src/main/java/org/schabi/newpipe/fragments/list/search/SuggestionListAdapter.java index 3f4e9af0b..d46f4bb31 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/search/SuggestionListAdapter.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/search/SuggestionListAdapter.java @@ -2,8 +2,8 @@ package org.schabi.newpipe.fragments.list.search; import android.content.Context; import android.content.res.TypedArray; -import android.support.annotation.AttrRes; -import android.support.v7.widget.RecyclerView; +import androidx.annotation.AttrRes; +import androidx.recyclerview.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedVideosFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedVideosFragment.java index 69a59c2f1..2186efda5 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedVideosFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedVideosFragment.java @@ -4,8 +4,8 @@ import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; diff --git a/app/src/main/java/org/schabi/newpipe/info_list/InfoItemBuilder.java b/app/src/main/java/org/schabi/newpipe/info_list/InfoItemBuilder.java index 0e9fd3277..2a635bc74 100644 --- a/app/src/main/java/org/schabi/newpipe/info_list/InfoItemBuilder.java +++ b/app/src/main/java/org/schabi/newpipe/info_list/InfoItemBuilder.java @@ -1,8 +1,7 @@ package org.schabi.newpipe.info_list; import android.content.Context; -import android.support.annotation.NonNull; -import android.util.Log; +import androidx.annotation.NonNull; import android.view.View; import android.view.ViewGroup; @@ -22,6 +21,7 @@ import org.schabi.newpipe.info_list.holder.PlaylistInfoItemHolder; import org.schabi.newpipe.info_list.holder.PlaylistMiniInfoItemHolder; import org.schabi.newpipe.info_list.holder.StreamInfoItemHolder; import org.schabi.newpipe.info_list.holder.StreamMiniInfoItemHolder; +import org.schabi.newpipe.local.history.HistoryRecordManager; import org.schabi.newpipe.util.OnClickGesture; /* @@ -59,13 +59,14 @@ public class InfoItemBuilder { this.context = context; } - public View buildView(@NonNull ViewGroup parent, @NonNull final InfoItem infoItem) { - return buildView(parent, infoItem, false); + public View buildView(@NonNull ViewGroup parent, @NonNull final InfoItem infoItem, final HistoryRecordManager historyRecordManager) { + return buildView(parent, infoItem, historyRecordManager, false); } - public View buildView(@NonNull ViewGroup parent, @NonNull final InfoItem infoItem, boolean useMiniVariant) { + public View buildView(@NonNull ViewGroup parent, @NonNull final InfoItem infoItem, + final HistoryRecordManager historyRecordManager, boolean useMiniVariant) { InfoItemHolder holder = holderFromInfoType(parent, infoItem.getInfoType(), useMiniVariant); - holder.updateFromItem(infoItem); + holder.updateFromItem(infoItem, historyRecordManager); return holder.itemView; } @@ -80,7 +81,6 @@ public class InfoItemBuilder { case COMMENT: return useMiniVariant ? new CommentsMiniInfoItemHolder(this, parent) : new CommentsInfoItemHolder(this, parent); default: - Log.e(TAG, "Trollolo"); throw new RuntimeException("InfoType not expected = " + infoType.name()); } } diff --git a/app/src/main/java/org/schabi/newpipe/info_list/InfoItemDialog.java b/app/src/main/java/org/schabi/newpipe/info_list/InfoItemDialog.java index fd0e9f528..a7f961e7d 100644 --- a/app/src/main/java/org/schabi/newpipe/info_list/InfoItemDialog.java +++ b/app/src/main/java/org/schabi/newpipe/info_list/InfoItemDialog.java @@ -3,8 +3,8 @@ package org.schabi.newpipe.info_list; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import android.view.View; import android.widget.TextView; diff --git a/app/src/main/java/org/schabi/newpipe/info_list/InfoListAdapter.java b/app/src/main/java/org/schabi/newpipe/info_list/InfoListAdapter.java index 5e7095c7d..594ec81af 100644 --- a/app/src/main/java/org/schabi/newpipe/info_list/InfoListAdapter.java +++ b/app/src/main/java/org/schabi/newpipe/info_list/InfoListAdapter.java @@ -1,22 +1,25 @@ package org.schabi.newpipe.info_list; -import android.app.Activity; -import android.support.v7.widget.GridLayoutManager; -import android.support.v7.widget.RecyclerView; +import android.content.Context; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.recyclerview.widget.GridLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import android.util.Log; import android.view.View; import android.view.ViewGroup; +import org.schabi.newpipe.database.stream.model.StreamStateEntity; import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.channel.ChannelInfoItem; import org.schabi.newpipe.extractor.comments.CommentsInfoItem; import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem; import org.schabi.newpipe.extractor.stream.StreamInfoItem; +import org.schabi.newpipe.info_list.holder.ChannelGridInfoItemHolder; import org.schabi.newpipe.info_list.holder.ChannelInfoItemHolder; import org.schabi.newpipe.info_list.holder.ChannelMiniInfoItemHolder; import org.schabi.newpipe.info_list.holder.CommentsInfoItemHolder; import org.schabi.newpipe.info_list.holder.CommentsMiniInfoItemHolder; -import org.schabi.newpipe.info_list.holder.ChannelGridInfoItemHolder; import org.schabi.newpipe.info_list.holder.InfoItemHolder; import org.schabi.newpipe.info_list.holder.PlaylistGridInfoItemHolder; import org.schabi.newpipe.info_list.holder.PlaylistInfoItemHolder; @@ -24,6 +27,7 @@ import org.schabi.newpipe.info_list.holder.PlaylistMiniInfoItemHolder; import org.schabi.newpipe.info_list.holder.StreamGridInfoItemHolder; import org.schabi.newpipe.info_list.holder.StreamInfoItemHolder; import org.schabi.newpipe.info_list.holder.StreamMiniInfoItemHolder; +import org.schabi.newpipe.local.history.HistoryRecordManager; import org.schabi.newpipe.util.FallbackViewHolder; import org.schabi.newpipe.util.OnClickGesture; @@ -71,6 +75,8 @@ public class InfoListAdapter extends RecyclerView.Adapter infoItemList; + private final HistoryRecordManager recordManager; + private boolean useMiniVariant = false; private boolean useGridVariant = false; private boolean showFooter = false; @@ -86,8 +92,9 @@ public class InfoListAdapter extends RecyclerView.Adapter(); } @@ -115,50 +122,53 @@ public class InfoListAdapter extends RecyclerView.Adapter data) { - if (data != null) { - if (DEBUG) { - Log.d(TAG, "addInfoItemList() before > infoItemList.size() = " + infoItemList.size() + ", data.size() = " + data.size()); - } + public void addInfoItemList(@Nullable final List data) { + if (data == null) { + return; + } + if (DEBUG) Log.d(TAG, "addInfoItemList() before > infoItemList.size() = " + + infoItemList.size() + ", data.size() = " + data.size()); - int offsetStart = sizeConsideringHeaderOffset(); - infoItemList.addAll(data); + int offsetStart = sizeConsideringHeaderOffset(); + infoItemList.addAll(data); - if (DEBUG) { - Log.d(TAG, "addInfoItemList() after > offsetStart = " + offsetStart + ", infoItemList.size() = " + infoItemList.size() + ", header = " + header + ", footer = " + footer + ", showFooter = " + showFooter); - } + if (DEBUG) Log.d(TAG, "addInfoItemList() after > offsetStart = " + offsetStart + + ", infoItemList.size() = " + infoItemList.size() + + ", header = " + header + ", footer = " + footer + + ", showFooter = " + showFooter); + notifyItemRangeInserted(offsetStart, data.size()); - notifyItemRangeInserted(offsetStart, data.size()); + if (footer != null && showFooter) { + int footerNow = sizeConsideringHeaderOffset(); + notifyItemMoved(offsetStart, footerNow); - if (footer != null && showFooter) { - int footerNow = sizeConsideringHeaderOffset(); - notifyItemMoved(offsetStart, footerNow); - - if (DEBUG) Log.d(TAG, "addInfoItemList() footer from " + offsetStart + " to " + footerNow); - } + if (DEBUG) Log.d(TAG, "addInfoItemList() footer from " + offsetStart + + " to " + footerNow); } } - public void addInfoItem(InfoItem data) { - if (data != null) { - if (DEBUG) { - Log.d(TAG, "addInfoItem() before > infoItemList.size() = " + infoItemList.size() + ", thread = " + Thread.currentThread()); - } + public void addInfoItem(@Nullable InfoItem data) { + if (data == null) { + return; + } + if (DEBUG) Log.d(TAG, "addInfoItem() before > infoItemList.size() = " + + infoItemList.size() + ", thread = " + Thread.currentThread()); - int positionInserted = sizeConsideringHeaderOffset(); - infoItemList.add(data); + int positionInserted = sizeConsideringHeaderOffset(); + infoItemList.add(data); - if (DEBUG) { - Log.d(TAG, "addInfoItem() after > position = " + positionInserted + ", infoItemList.size() = " + infoItemList.size() + ", header = " + header + ", footer = " + footer + ", showFooter = " + showFooter); - } - notifyItemInserted(positionInserted); + if (DEBUG) Log.d(TAG, "addInfoItem() after > position = " + positionInserted + + ", infoItemList.size() = " + infoItemList.size() + + ", header = " + header + ", footer = " + footer + + ", showFooter = " + showFooter); + notifyItemInserted(positionInserted); - if (footer != null && showFooter) { - int footerNow = sizeConsideringHeaderOffset(); - notifyItemMoved(positionInserted, footerNow); + if (footer != null && showFooter) { + int footerNow = sizeConsideringHeaderOffset(); + notifyItemMoved(positionInserted, footerNow); - if (DEBUG) Log.d(TAG, "addInfoItem() footer from " + positionInserted + " to " + footerNow); - } + if (DEBUG) Log.d(TAG, "addInfoItem() footer from " + positionInserted + + " to " + footerNow); } } @@ -235,13 +245,13 @@ public class InfoListAdapter extends RecyclerView.Adapter payloads) { + if (!payloads.isEmpty() && holder instanceof InfoItemHolder) { + for (Object payload : payloads) { + if (payload instanceof StreamStateEntity) { + ((InfoItemHolder) holder).updateState(infoItemList.get(header == null ? position : position - 1), recordManager); + } else if (payload instanceof Boolean) { + ((InfoItemHolder) holder).updateState(infoItemList.get(header == null ? position : position - 1), recordManager); + } + } + } else { + onBindViewHolder(holder, position); + } + } + public GridLayoutManager.SpanSizeLookup getSpanSizeLookup(final int spanCount) { return new GridLayoutManager.SpanSizeLookup() { @Override diff --git a/app/src/main/java/org/schabi/newpipe/info_list/holder/ChannelInfoItemHolder.java b/app/src/main/java/org/schabi/newpipe/info_list/holder/ChannelInfoItemHolder.java index 1e5c726a4..956bc47a6 100644 --- a/app/src/main/java/org/schabi/newpipe/info_list/holder/ChannelInfoItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/info_list/holder/ChannelInfoItemHolder.java @@ -7,6 +7,7 @@ import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.channel.ChannelInfoItem; import org.schabi.newpipe.info_list.InfoItemBuilder; +import org.schabi.newpipe.local.history.HistoryRecordManager; import org.schabi.newpipe.util.Localization; /* @@ -38,8 +39,8 @@ public class ChannelInfoItemHolder extends ChannelMiniInfoItemHolder { } @Override - public void updateFromItem(final InfoItem infoItem) { - super.updateFromItem(infoItem); + public void updateFromItem(final InfoItem infoItem, final HistoryRecordManager historyRecordManager) { + super.updateFromItem(infoItem, historyRecordManager); if (!(infoItem instanceof ChannelInfoItem)) return; final ChannelInfoItem item = (ChannelInfoItem) infoItem; diff --git a/app/src/main/java/org/schabi/newpipe/info_list/holder/ChannelMiniInfoItemHolder.java b/app/src/main/java/org/schabi/newpipe/info_list/holder/ChannelMiniInfoItemHolder.java index ca783833a..3f4e4e398 100644 --- a/app/src/main/java/org/schabi/newpipe/info_list/holder/ChannelMiniInfoItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/info_list/holder/ChannelMiniInfoItemHolder.java @@ -7,6 +7,7 @@ import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.channel.ChannelInfoItem; import org.schabi.newpipe.info_list.InfoItemBuilder; +import org.schabi.newpipe.local.history.HistoryRecordManager; import org.schabi.newpipe.util.ImageDisplayConstants; import org.schabi.newpipe.util.Localization; @@ -30,7 +31,7 @@ public class ChannelMiniInfoItemHolder extends InfoItemHolder { } @Override - public void updateFromItem(final InfoItem infoItem) { + public void updateFromItem(final InfoItem infoItem, final HistoryRecordManager historyRecordManager) { if (!(infoItem instanceof ChannelInfoItem)) return; final ChannelInfoItem item = (ChannelInfoItem) infoItem; diff --git a/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentsInfoItemHolder.java b/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentsInfoItemHolder.java index 46e4b4563..90212ea31 100644 --- a/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentsInfoItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentsInfoItemHolder.java @@ -5,10 +5,9 @@ import android.widget.TextView; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.InfoItem; -import org.schabi.newpipe.extractor.channel.ChannelInfoItem; import org.schabi.newpipe.extractor.comments.CommentsInfoItem; import org.schabi.newpipe.info_list.InfoItemBuilder; -import org.schabi.newpipe.util.Localization; +import org.schabi.newpipe.local.history.HistoryRecordManager; /* * Created by Christian Schabesberger on 12.02.17. @@ -41,8 +40,8 @@ public class CommentsInfoItemHolder extends CommentsMiniInfoItemHolder { } @Override - public void updateFromItem(final InfoItem infoItem) { - super.updateFromItem(infoItem); + public void updateFromItem(final InfoItem infoItem, final HistoryRecordManager historyRecordManager) { + super.updateFromItem(infoItem, historyRecordManager); if (!(infoItem instanceof CommentsInfoItem)) return; final CommentsInfoItem item = (CommentsInfoItem) infoItem; diff --git a/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentsMiniInfoItemHolder.java b/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentsMiniInfoItemHolder.java index 9be272198..f2bf5df39 100644 --- a/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentsMiniInfoItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentsMiniInfoItemHolder.java @@ -1,15 +1,16 @@ package org.schabi.newpipe.info_list.holder; -import android.support.v7.app.AppCompatActivity; +import androidx.appcompat.app.AppCompatActivity; import android.text.util.Linkify; -import android.view.View; import android.view.ViewGroup; import android.widget.TextView; +import org.jsoup.helper.StringUtil; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.comments.CommentsInfoItem; import org.schabi.newpipe.info_list.InfoItemBuilder; +import org.schabi.newpipe.local.history.HistoryRecordManager; import org.schabi.newpipe.report.ErrorActivity; import org.schabi.newpipe.util.CommentTextOnTouchListener; import org.schabi.newpipe.util.ImageDisplayConstants; @@ -45,7 +46,7 @@ public class CommentsMiniInfoItemHolder extends InfoItemHolder { if(hours != null) timestamp += (Integer.parseInt(hours.replace(":", ""))*3600); if(minutes != null) timestamp += (Integer.parseInt(minutes.replace(":", ""))*60); if(seconds != null) timestamp += (Integer.parseInt(seconds)); - return streamUrl + url.replace(match.group(0), "&t=" + String.valueOf(timestamp)); + return streamUrl + url.replace(match.group(0), "#timestamp=" + timestamp); } }; @@ -64,7 +65,7 @@ public class CommentsMiniInfoItemHolder extends InfoItemHolder { } @Override - public void updateFromItem(final InfoItem infoItem) { + public void updateFromItem(final InfoItem infoItem, final HistoryRecordManager historyRecordManager) { if (!(infoItem instanceof CommentsInfoItem)) return; final CommentsInfoItem item = (CommentsInfoItem) infoItem; @@ -73,33 +74,30 @@ public class CommentsMiniInfoItemHolder extends InfoItemHolder { itemThumbnailView, ImageDisplayConstants.DISPLAY_THUMBNAIL_OPTIONS); - itemThumbnailView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - try { - final AppCompatActivity activity = (AppCompatActivity) itemBuilder.getContext(); - NavigationHelper.openChannelFragment( - activity.getSupportFragmentManager(), - item.getServiceId(), - item.getAuthorEndpoint(), - item.getAuthorName()); - } catch (Exception e) { - ErrorActivity.reportUiError((AppCompatActivity) itemBuilder.getContext(), e); - } + itemThumbnailView.setOnClickListener(view -> { + if(StringUtil.isBlank(item.getAuthorEndpoint())) return; + try { + final AppCompatActivity activity = (AppCompatActivity) itemBuilder.getContext(); + NavigationHelper.openChannelFragment( + activity.getSupportFragmentManager(), + item.getServiceId(), + item.getAuthorEndpoint(), + item.getAuthorName()); + } catch (Exception e) { + ErrorActivity.reportUiError((AppCompatActivity) itemBuilder.getContext(), e); } }); streamUrl = item.getUrl(); - itemContentView.setMaxLines(commentDefaultLines); + itemContentView.setLines(commentDefaultLines); commentText = item.getCommentText(); itemContentView.setText(commentText); - linkify(); itemContentView.setOnTouchListener(CommentTextOnTouchListener.INSTANCE); - if(itemContentView.getLineCount() == 0){ - itemContentView.post(() -> ellipsize()); - }else{ + if (itemContentView.getLineCount() == 0) { + itemContentView.post(this::ellipsize); + } else { ellipsize(); } @@ -119,15 +117,17 @@ public class CommentsMiniInfoItemHolder extends InfoItemHolder { private void ellipsize() { if (itemContentView.getLineCount() > commentDefaultLines){ int endOfLastLine = itemContentView.getLayout().getLineEnd(commentDefaultLines - 1); - String newVal = itemContentView.getText().subSequence(0, endOfLastLine - 3) + "..."; + int end = itemContentView.getText().toString().lastIndexOf(' ', endOfLastLine -2); + if(end == -1) end = Math.max(endOfLastLine -2, 0); + String newVal = itemContentView.getText().subSequence(0, end) + " …"; itemContentView.setText(newVal); - linkify(); } + linkify(); } private void toggleEllipsize() { if (itemContentView.getText().toString().equals(commentText)) { - ellipsize(); + if (itemContentView.getLineCount() > commentDefaultLines) ellipsize(); } else { expand(); } diff --git a/app/src/main/java/org/schabi/newpipe/info_list/holder/InfoItemHolder.java b/app/src/main/java/org/schabi/newpipe/info_list/holder/InfoItemHolder.java index ebb5b4114..1b97e2d27 100644 --- a/app/src/main/java/org/schabi/newpipe/info_list/holder/InfoItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/info_list/holder/InfoItemHolder.java @@ -1,11 +1,12 @@ package org.schabi.newpipe.info_list.holder; -import android.support.v7.widget.RecyclerView; +import androidx.recyclerview.widget.RecyclerView; import android.view.LayoutInflater; import android.view.ViewGroup; import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.info_list.InfoItemBuilder; +import org.schabi.newpipe.local.history.HistoryRecordManager; /* * Created by Christian Schabesberger on 12.02.17. @@ -35,5 +36,8 @@ public abstract class InfoItemHolder extends RecyclerView.ViewHolder { this.itemBuilder = infoItemBuilder; } - public abstract void updateFromItem(final InfoItem infoItem); + public abstract void updateFromItem(final InfoItem infoItem, final HistoryRecordManager historyRecordManager); + + public void updateState(final InfoItem infoItem, final HistoryRecordManager historyRecordManager) { + } } diff --git a/app/src/main/java/org/schabi/newpipe/info_list/holder/PlaylistMiniInfoItemHolder.java b/app/src/main/java/org/schabi/newpipe/info_list/holder/PlaylistMiniInfoItemHolder.java index b6bd2f389..b73f22d93 100644 --- a/app/src/main/java/org/schabi/newpipe/info_list/holder/PlaylistMiniInfoItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/info_list/holder/PlaylistMiniInfoItemHolder.java @@ -8,6 +8,7 @@ import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem; import org.schabi.newpipe.info_list.InfoItemBuilder; +import org.schabi.newpipe.local.history.HistoryRecordManager; import org.schabi.newpipe.util.ImageDisplayConstants; public class PlaylistMiniInfoItemHolder extends InfoItemHolder { @@ -30,7 +31,7 @@ public class PlaylistMiniInfoItemHolder extends InfoItemHolder { } @Override - public void updateFromItem(final InfoItem infoItem) { + public void updateFromItem(final InfoItem infoItem, final HistoryRecordManager historyRecordManager) { if (!(infoItem instanceof PlaylistInfoItem)) return; final PlaylistInfoItem item = (PlaylistInfoItem) infoItem; diff --git a/app/src/main/java/org/schabi/newpipe/info_list/holder/StreamInfoItemHolder.java b/app/src/main/java/org/schabi/newpipe/info_list/holder/StreamInfoItemHolder.java index 0a7705427..ea058bc0e 100644 --- a/app/src/main/java/org/schabi/newpipe/info_list/holder/StreamInfoItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/info_list/holder/StreamInfoItemHolder.java @@ -8,6 +8,7 @@ import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.info_list.InfoItemBuilder; +import org.schabi.newpipe.local.history.HistoryRecordManager; import org.schabi.newpipe.util.Localization; /* @@ -40,8 +41,8 @@ public class StreamInfoItemHolder extends StreamMiniInfoItemHolder { } @Override - public void updateFromItem(final InfoItem infoItem) { - super.updateFromItem(infoItem); + public void updateFromItem(final InfoItem infoItem, final HistoryRecordManager historyRecordManager) { + super.updateFromItem(infoItem, historyRecordManager); if (!(infoItem instanceof StreamInfoItem)) return; final StreamInfoItem item = (StreamInfoItem) infoItem; diff --git a/app/src/main/java/org/schabi/newpipe/info_list/holder/StreamMiniInfoItemHolder.java b/app/src/main/java/org/schabi/newpipe/info_list/holder/StreamMiniInfoItemHolder.java index 048b907af..6173e53f9 100644 --- a/app/src/main/java/org/schabi/newpipe/info_list/holder/StreamMiniInfoItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/info_list/holder/StreamMiniInfoItemHolder.java @@ -1,18 +1,24 @@ package org.schabi.newpipe.info_list.holder; -import android.support.v4.content.ContextCompat; +import androidx.core.content.ContextCompat; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import org.schabi.newpipe.R; +import org.schabi.newpipe.database.stream.model.StreamStateEntity; import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.stream.StreamType; import org.schabi.newpipe.info_list.InfoItemBuilder; +import org.schabi.newpipe.local.history.HistoryRecordManager; +import org.schabi.newpipe.util.AnimationUtils; import org.schabi.newpipe.util.ImageDisplayConstants; import org.schabi.newpipe.util.Localization; +import org.schabi.newpipe.views.AnimatedProgressBar; + +import java.util.concurrent.TimeUnit; public class StreamMiniInfoItemHolder extends InfoItemHolder { @@ -20,6 +26,7 @@ public class StreamMiniInfoItemHolder extends InfoItemHolder { public final TextView itemVideoTitleView; public final TextView itemUploaderView; public final TextView itemDurationView; + public final AnimatedProgressBar itemProgressView; StreamMiniInfoItemHolder(InfoItemBuilder infoItemBuilder, int layoutId, ViewGroup parent) { super(infoItemBuilder, layoutId, parent); @@ -28,6 +35,7 @@ public class StreamMiniInfoItemHolder extends InfoItemHolder { itemVideoTitleView = itemView.findViewById(R.id.itemVideoTitleView); itemUploaderView = itemView.findViewById(R.id.itemUploaderView); itemDurationView = itemView.findViewById(R.id.itemDurationView); + itemProgressView = itemView.findViewById(R.id.itemProgressView); } public StreamMiniInfoItemHolder(InfoItemBuilder infoItemBuilder, ViewGroup parent) { @@ -35,7 +43,7 @@ public class StreamMiniInfoItemHolder extends InfoItemHolder { } @Override - public void updateFromItem(final InfoItem infoItem) { + public void updateFromItem(final InfoItem infoItem, final HistoryRecordManager historyRecordManager) { if (!(infoItem instanceof StreamInfoItem)) return; final StreamInfoItem item = (StreamInfoItem) infoItem; @@ -47,13 +55,24 @@ public class StreamMiniInfoItemHolder extends InfoItemHolder { itemDurationView.setBackgroundColor(ContextCompat.getColor(itemBuilder.getContext(), R.color.duration_background_color)); itemDurationView.setVisibility(View.VISIBLE); + + StreamStateEntity state2 = historyRecordManager.loadStreamState(infoItem).blockingGet()[0]; + if (state2 != null) { + itemProgressView.setVisibility(View.VISIBLE); + itemProgressView.setMax((int) item.getDuration()); + itemProgressView.setProgress((int) TimeUnit.MILLISECONDS.toSeconds(state2.getProgressTime())); + } else { + itemProgressView.setVisibility(View.GONE); + } } else if (item.getStreamType() == StreamType.LIVE_STREAM) { itemDurationView.setText(R.string.duration_live); itemDurationView.setBackgroundColor(ContextCompat.getColor(itemBuilder.getContext(), R.color.live_duration_background_color)); itemDurationView.setVisibility(View.VISIBLE); + itemProgressView.setVisibility(View.GONE); } else { itemDurationView.setVisibility(View.GONE); + itemProgressView.setVisibility(View.GONE); } // Default thumbnail is shown on error, while loading and if the url is empty @@ -83,6 +102,24 @@ public class StreamMiniInfoItemHolder extends InfoItemHolder { } } + @Override + public void updateState(final InfoItem infoItem, final HistoryRecordManager historyRecordManager) { + final StreamInfoItem item = (StreamInfoItem) infoItem; + + StreamStateEntity state = historyRecordManager.loadStreamState(infoItem).blockingGet()[0]; + if (state != null && item.getDuration() > 0 && item.getStreamType() != StreamType.LIVE_STREAM) { + itemProgressView.setMax((int) item.getDuration()); + if (itemProgressView.getVisibility() == View.VISIBLE) { + itemProgressView.setProgressAnimated((int) TimeUnit.MILLISECONDS.toSeconds(state.getProgressTime())); + } else { + itemProgressView.setProgress((int) TimeUnit.MILLISECONDS.toSeconds(state.getProgressTime())); + AnimationUtils.animateView(itemProgressView, true, 500); + } + } else if (itemProgressView.getVisibility() == View.VISIBLE) { + AnimationUtils.animateView(itemProgressView, false, 500); + } + } + private void enableLongClick(final StreamInfoItem item) { itemView.setLongClickable(true); itemView.setOnLongClickListener(view -> { @@ -97,4 +134,4 @@ public class StreamMiniInfoItemHolder extends InfoItemHolder { itemView.setLongClickable(false); itemView.setOnLongClickListener(null); } -} +} \ No newline at end of file diff --git a/app/src/main/java/org/schabi/newpipe/local/BaseLocalListFragment.java b/app/src/main/java/org/schabi/newpipe/local/BaseLocalListFragment.java index abdf82353..414a9b6b5 100644 --- a/app/src/main/java/org/schabi/newpipe/local/BaseLocalListFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/BaseLocalListFragment.java @@ -5,11 +5,11 @@ import android.content.res.Configuration; import android.content.res.Resources; import android.os.Bundle; import android.preference.PreferenceManager; -import android.support.v4.app.Fragment; -import android.support.v7.app.ActionBar; -import android.support.v7.widget.GridLayoutManager; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; +import androidx.fragment.app.Fragment; +import androidx.appcompat.app.ActionBar; +import androidx.recyclerview.widget.GridLayoutManager; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; diff --git a/app/src/main/java/org/schabi/newpipe/local/HeaderFooterHolder.java b/app/src/main/java/org/schabi/newpipe/local/HeaderFooterHolder.java index 49a552b3e..9ee33b3c4 100644 --- a/app/src/main/java/org/schabi/newpipe/local/HeaderFooterHolder.java +++ b/app/src/main/java/org/schabi/newpipe/local/HeaderFooterHolder.java @@ -1,6 +1,6 @@ package org.schabi.newpipe.local; -import android.support.v7.widget.RecyclerView; +import androidx.recyclerview.widget.RecyclerView; import android.view.View; public class HeaderFooterHolder extends RecyclerView.ViewHolder { diff --git a/app/src/main/java/org/schabi/newpipe/local/LocalItemListAdapter.java b/app/src/main/java/org/schabi/newpipe/local/LocalItemListAdapter.java index e298dedd3..89c1267c8 100644 --- a/app/src/main/java/org/schabi/newpipe/local/LocalItemListAdapter.java +++ b/app/src/main/java/org/schabi/newpipe/local/LocalItemListAdapter.java @@ -1,13 +1,17 @@ package org.schabi.newpipe.local; -import android.app.Activity; -import android.support.v7.widget.GridLayoutManager; -import android.support.v7.widget.RecyclerView; +import android.content.Context; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.recyclerview.widget.GridLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import android.util.Log; import android.view.View; import android.view.ViewGroup; import org.schabi.newpipe.database.LocalItem; +import org.schabi.newpipe.database.stream.model.StreamStateEntity; +import org.schabi.newpipe.local.history.HistoryRecordManager; import org.schabi.newpipe.local.holder.LocalItemHolder; import org.schabi.newpipe.local.holder.LocalPlaylistGridItemHolder; import org.schabi.newpipe.local.holder.LocalPlaylistItemHolder; @@ -64,6 +68,7 @@ public class LocalItemListAdapter extends RecyclerView.Adapter localItems; + private final HistoryRecordManager recordManager; private final DateFormat dateFormat; private boolean showFooter = false; @@ -71,11 +76,12 @@ public class LocalItemListAdapter extends RecyclerView.Adapter(); dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, - Localization.getPreferredLocale(activity)); + Localization.getPreferredLocale(context)); } public void setSelectedListener(OnClickGesture listener) { @@ -86,38 +92,33 @@ public class LocalItemListAdapter extends RecyclerView.Adapter data) { - if (data != null) { - if (DEBUG) { - Log.d(TAG, "addItems() before > localItems.size() = " + - localItems.size() + ", data.size() = " + data.size()); - } + public void addItems(@Nullable List data) { + if (data == null) { + return; + } + if (DEBUG) Log.d(TAG, "addItems() before > localItems.size() = " + + localItems.size() + ", data.size() = " + data.size()); - int offsetStart = sizeConsideringHeader(); - localItems.addAll(data); + int offsetStart = sizeConsideringHeader(); + localItems.addAll(data); - if (DEBUG) { - Log.d(TAG, "addItems() after > offsetStart = " + offsetStart + - ", localItems.size() = " + localItems.size() + - ", header = " + header + ", footer = " + footer + - ", showFooter = " + showFooter); - } + if (DEBUG) Log.d(TAG, "addItems() after > offsetStart = " + offsetStart + + ", localItems.size() = " + localItems.size() + + ", header = " + header + ", footer = " + footer + + ", showFooter = " + showFooter); + notifyItemRangeInserted(offsetStart, data.size()); - notifyItemRangeInserted(offsetStart, data.size()); + if (footer != null && showFooter) { + int footerNow = sizeConsideringHeader(); + notifyItemMoved(offsetStart, footerNow); - if (footer != null && showFooter) { - int footerNow = sizeConsideringHeader(); - notifyItemMoved(offsetStart, footerNow); - - if (DEBUG) Log.d(TAG, "addItems() footer from " + offsetStart + - " to " + footerNow); - } + if (DEBUG) Log.d(TAG, "addItems() footer from " + offsetStart + + " to " + footerNow); } } public void removeItem(final LocalItem data) { final int index = localItems.indexOf(data); - localItems.remove(index); notifyItemRemoved(index + (header != null ? 1 : 0)); } @@ -219,8 +220,9 @@ public class LocalItemListAdapter extends RecyclerView.Adapter payloads) { + if (!payloads.isEmpty() && holder instanceof LocalItemHolder) { + for (Object payload : payloads) { + if (payload instanceof StreamStateEntity) { + ((LocalItemHolder) holder).updateState(localItems.get(header == null ? position : position - 1), recordManager); + } else if (payload instanceof Boolean) { + ((LocalItemHolder) holder).updateState(localItems.get(header == null ? position : position - 1), recordManager); + } + } + } else { + onBindViewHolder(holder, position); + } + } + public GridLayoutManager.SpanSizeLookup getSpanSizeLookup(final int spanCount) { return new GridLayoutManager.SpanSizeLookup() { @Override diff --git a/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java b/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java index 99bd70f5b..8f67367aa 100644 --- a/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java @@ -3,9 +3,9 @@ package org.schabi.newpipe.local.bookmark; import android.app.AlertDialog; import android.os.Bundle; import android.os.Parcelable; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.app.FragmentManager; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.FragmentManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; diff --git a/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistAppendDialog.java b/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistAppendDialog.java index e422b332a..ac02b0b37 100644 --- a/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistAppendDialog.java +++ b/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistAppendDialog.java @@ -1,11 +1,10 @@ package org.schabi.newpipe.local.dialog; -import android.annotation.SuppressLint; import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -28,7 +27,7 @@ import java.util.Collections; import java.util.List; import io.reactivex.android.schedulers.AndroidSchedulers; -import io.reactivex.disposables.Disposable; +import io.reactivex.disposables.CompositeDisposable; public final class PlaylistAppendDialog extends PlaylistDialog { private static final String TAG = PlaylistAppendDialog.class.getCanonicalName(); @@ -36,7 +35,7 @@ public final class PlaylistAppendDialog extends PlaylistDialog { private RecyclerView playlistRecyclerView; private LocalItemListAdapter playlistAdapter; - private Disposable playlistReactor; + private CompositeDisposable playlistDisposables = new CompositeDisposable(); public static PlaylistAppendDialog fromStreamInfo(final StreamInfo info) { PlaylistAppendDialog dialog = new PlaylistAppendDialog(); @@ -99,9 +98,9 @@ public final class PlaylistAppendDialog extends PlaylistDialog { final View newPlaylistButton = view.findViewById(R.id.newPlaylist); newPlaylistButton.setOnClickListener(ignored -> openCreatePlaylistDialog()); - playlistReactor = playlistManager.getPlaylists() + playlistDisposables.add(playlistManager.getPlaylists() .observeOn(AndroidSchedulers.mainThread()) - .subscribe(this::onPlaylistsReceived); + .subscribe(this::onPlaylistsReceived)); } /*////////////////////////////////////////////////////////////////////////// @@ -111,10 +110,12 @@ public final class PlaylistAppendDialog extends PlaylistDialog { @Override public void onDestroyView() { super.onDestroyView(); - if (playlistReactor != null) playlistReactor.dispose(); - if (playlistAdapter != null) playlistAdapter.unsetSelectedListener(); + playlistDisposables.dispose(); + if (playlistAdapter != null) { + playlistAdapter.unsetSelectedListener(); + } - playlistReactor = null; + playlistDisposables.clear(); playlistRecyclerView = null; playlistAdapter = null; } @@ -148,13 +149,12 @@ public final class PlaylistAppendDialog extends PlaylistDialog { @NonNull List streams) { if (getStreams() == null) return; - @SuppressLint("ShowToast") final Toast successToast = Toast.makeText(getContext(), R.string.playlist_add_stream_success, Toast.LENGTH_SHORT); - manager.appendToPlaylist(playlist.uid, streams) + playlistDisposables.add(manager.appendToPlaylist(playlist.uid, streams) .observeOn(AndroidSchedulers.mainThread()) - .subscribe(ignored -> successToast.show()); + .subscribe(ignored -> successToast.show())); getDialog().dismiss(); } diff --git a/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistCreationDialog.java b/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistCreationDialog.java index ddbe65399..0507d3dd0 100644 --- a/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistCreationDialog.java +++ b/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistCreationDialog.java @@ -3,8 +3,8 @@ package org.schabi.newpipe.local.dialog; import android.app.AlertDialog; import android.app.Dialog; import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import android.view.View; import android.widget.EditText; import android.widget.Toast; diff --git a/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistDialog.java b/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistDialog.java index df98fb3eb..12e57808e 100644 --- a/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistDialog.java +++ b/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistDialog.java @@ -2,9 +2,9 @@ package org.schabi.newpipe.local.dialog; import android.app.Dialog; import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.app.DialogFragment; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.DialogFragment; import android.view.Window; import org.schabi.newpipe.database.stream.model.StreamEntity; diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.java b/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.java index f1bb01734..04406c3da 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.java @@ -2,9 +2,9 @@ package org.schabi.newpipe.local.feed; import android.os.Bundle; import android.os.Handler; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v7.app.ActionBar; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.ActionBar; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; @@ -21,8 +21,8 @@ import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.channel.ChannelInfo; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.fragments.list.BaseListFragment; -import org.schabi.newpipe.report.UserAction; import org.schabi.newpipe.local.subscription.SubscriptionService; +import org.schabi.newpipe.report.UserAction; import java.util.Collections; import java.util.HashSet; @@ -183,7 +183,7 @@ public class FeedFragment extends BaseListFragment, Voi } @Override - public void handleResult(@android.support.annotation.NonNull List result) { + public void handleResult(@androidx.annotation.NonNull List result) { super.handleResult(result); if (result.isEmpty()) { @@ -262,7 +262,7 @@ public class FeedFragment extends BaseListFragment, Voi * If chosen feed already displayed, then we request another feed from another * subscription, until the subscription table runs out of new items. *

- * This Observer is self-contained and will dispose itself when complete. However, this + * This Observer is self-contained and will close itself when complete. However, this * does not obey the fragment lifecycle and may continue running in the background * until it is complete. This is done due to RxJava2 no longer propagate errors once * an observer is unsubscribed while the thread process is still running. diff --git a/app/src/main/java/org/schabi/newpipe/local/history/HistoryEntryAdapter.java b/app/src/main/java/org/schabi/newpipe/local/history/HistoryEntryAdapter.java index 09549346b..c4ca08a0a 100644 --- a/app/src/main/java/org/schabi/newpipe/local/history/HistoryEntryAdapter.java +++ b/app/src/main/java/org/schabi/newpipe/local/history/HistoryEntryAdapter.java @@ -1,9 +1,9 @@ package org.schabi.newpipe.local.history; import android.content.Context; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v7.widget.RecyclerView; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.recyclerview.widget.RecyclerView; import org.schabi.newpipe.util.Localization; diff --git a/app/src/main/java/org/schabi/newpipe/local/history/HistoryListener.java b/app/src/main/java/org/schabi/newpipe/local/history/HistoryListener.java index 6e4b85713..fc039f770 100644 --- a/app/src/main/java/org/schabi/newpipe/local/history/HistoryListener.java +++ b/app/src/main/java/org/schabi/newpipe/local/history/HistoryListener.java @@ -1,6 +1,6 @@ package org.schabi.newpipe.local.history; -import android.support.annotation.Nullable; +import androidx.annotation.Nullable; import org.schabi.newpipe.extractor.stream.AudioStream; import org.schabi.newpipe.extractor.stream.StreamInfo; diff --git a/app/src/main/java/org/schabi/newpipe/local/history/HistoryRecordManager.java b/app/src/main/java/org/schabi/newpipe/local/history/HistoryRecordManager.java index 56453773a..d84fe0195 100644 --- a/app/src/main/java/org/schabi/newpipe/local/history/HistoryRecordManager.java +++ b/app/src/main/java/org/schabi/newpipe/local/history/HistoryRecordManager.java @@ -21,28 +21,34 @@ package org.schabi.newpipe.local.history; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; -import android.support.annotation.NonNull; +import androidx.annotation.NonNull; import org.schabi.newpipe.NewPipeDatabase; import org.schabi.newpipe.R; import org.schabi.newpipe.database.AppDatabase; +import org.schabi.newpipe.database.LocalItem; import org.schabi.newpipe.database.history.dao.SearchHistoryDAO; import org.schabi.newpipe.database.history.dao.StreamHistoryDAO; import org.schabi.newpipe.database.history.model.SearchHistoryEntry; import org.schabi.newpipe.database.history.model.StreamHistoryEntity; import org.schabi.newpipe.database.history.model.StreamHistoryEntry; +import org.schabi.newpipe.database.playlist.PlaylistStreamEntry; +import org.schabi.newpipe.database.playlist.model.PlaylistStreamEntity; import org.schabi.newpipe.database.stream.StreamStatisticsEntry; import org.schabi.newpipe.database.stream.dao.StreamDAO; import org.schabi.newpipe.database.stream.dao.StreamStateDAO; import org.schabi.newpipe.database.stream.model.StreamEntity; import org.schabi.newpipe.database.stream.model.StreamStateEntity; +import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.stream.StreamInfo; +import org.schabi.newpipe.player.playqueue.PlayQueueItem; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.List; +import io.reactivex.Completable; import io.reactivex.Flowable; import io.reactivex.Maybe; import io.reactivex.Single; @@ -80,9 +86,9 @@ public class HistoryRecordManager { final Date currentTime = new Date(); return Maybe.fromCallable(() -> database.runInTransaction(() -> { final long streamId = streamTable.upsert(new StreamEntity(info)); - StreamHistoryEntity latestEntry = streamHistoryTable.getLatestEntry(); + StreamHistoryEntity latestEntry = streamHistoryTable.getLatestEntry(streamId); - if (latestEntry != null && latestEntry.getStreamUid() == streamId) { + if (latestEntry != null) { streamHistoryTable.delete(latestEntry); latestEntry.setAccessDate(currentTime); latestEntry.setRepeatCount(latestEntry.getRepeatCount() + 1); @@ -99,7 +105,12 @@ public class HistoryRecordManager { } public Single deleteWholeStreamHistory() { - return Single.fromCallable(() -> streamHistoryTable.deleteAll()) + return Single.fromCallable(streamHistoryTable::deleteAll) + .subscribeOn(Schedulers.io()); + } + + public Single deleteCompelteStreamStateHistory() { + return Single.fromCallable(streamStateTable::deleteAll) .subscribeOn(Schedulers.io()); } @@ -159,8 +170,8 @@ public class HistoryRecordManager { .subscribeOn(Schedulers.io()); } - public Single deleteWholeSearchHistory() { - return Single.fromCallable(() -> searchHistoryTable.deleteAll()) + public Single deleteCompleteSearchHistory() { + return Single.fromCallable(searchHistoryTable::deleteAll) .subscribeOn(Schedulers.io()); } @@ -180,21 +191,104 @@ public class HistoryRecordManager { // Stream State History /////////////////////////////////////////////////////// - @SuppressWarnings("unused") - public Maybe loadStreamState(final StreamInfo info) { - return Maybe.fromCallable(() -> streamTable.upsert(new StreamEntity(info))) - .flatMap(streamId -> streamStateTable.getState(streamId).firstElement()) - .flatMap(states -> states.isEmpty() ? Maybe.empty() : Maybe.just(states.get(0))) + public Maybe getStreamHistory(final StreamInfo info) { + return Maybe.fromCallable(() -> { + final long streamId = streamTable.upsert(new StreamEntity(info)); + return streamHistoryTable.getLatestEntry(streamId); + }).subscribeOn(Schedulers.io()); + } + + public Maybe loadStreamState(final PlayQueueItem queueItem) { + return queueItem.getStream() + .map((info) -> streamTable.upsert(new StreamEntity(info))) + .flatMapPublisher(streamStateTable::getState) + .firstElement() + .flatMap(list -> list.isEmpty() ? Maybe.empty() : Maybe.just(list.get(0))) + .filter(state -> state.isValid((int) queueItem.getDuration())) .subscribeOn(Schedulers.io()); } - public Maybe saveStreamState(@NonNull final StreamInfo info, final long progressTime) { - return Maybe.fromCallable(() -> database.runInTransaction(() -> { + public Maybe loadStreamState(final StreamInfo info) { + return Single.fromCallable(() -> streamTable.upsert(new StreamEntity(info))) + .flatMapPublisher(streamStateTable::getState) + .firstElement() + .flatMap(list -> list.isEmpty() ? Maybe.empty() : Maybe.just(list.get(0))) + .filter(state -> state.isValid((int) info.getDuration())) + .subscribeOn(Schedulers.io()); + } + + public Completable saveStreamState(@NonNull final StreamInfo info, final long progressTime) { + return Completable.fromAction(() -> database.runInTransaction(() -> { final long streamId = streamTable.upsert(new StreamEntity(info)); - return streamStateTable.upsert(new StreamStateEntity(streamId, progressTime)); + final StreamStateEntity state = new StreamStateEntity(streamId, progressTime); + if (state.isValid((int) info.getDuration())) { + streamStateTable.upsert(state); + } else { + streamStateTable.deleteState(streamId); + } })).subscribeOn(Schedulers.io()); } + public Single loadStreamState(final InfoItem info) { + return Single.fromCallable(() -> { + final List entities = streamTable.getStream(info.getServiceId(), info.getUrl()).blockingFirst(); + if (entities.isEmpty()) { + return new StreamStateEntity[]{null}; + } + final List states = streamStateTable.getState(entities.get(0).getUid()).blockingFirst(); + if (states.isEmpty()) { + return new StreamStateEntity[]{null}; + } + return new StreamStateEntity[]{states.get(0)}; + }).subscribeOn(Schedulers.io()); + } + + public Single> loadStreamStateBatch(final List infos) { + return Single.fromCallable(() -> { + final List result = new ArrayList<>(infos.size()); + for (InfoItem info : infos) { + final List entities = streamTable.getStream(info.getServiceId(), info.getUrl()).blockingFirst(); + if (entities.isEmpty()) { + result.add(null); + continue; + } + final List states = streamStateTable.getState(entities.get(0).getUid()).blockingFirst(); + if (states.isEmpty()) { + result.add(null); + continue; + } + result.add(states.get(0)); + } + return result; + }).subscribeOn(Schedulers.io()); + } + + public Single> loadLocalStreamStateBatch(final List items) { + return Single.fromCallable(() -> { + final List result = new ArrayList<>(items.size()); + for (LocalItem item : items) { + long streamId; + if (item instanceof StreamStatisticsEntry) { + streamId = ((StreamStatisticsEntry) item).streamId; + } else if (item instanceof PlaylistStreamEntity) { + streamId = ((PlaylistStreamEntity) item).getStreamUid(); + } else if (item instanceof PlaylistStreamEntry) { + streamId = ((PlaylistStreamEntry) item).streamId; + } else { + result.add(null); + continue; + } + final List states = streamStateTable.getState(streamId).blockingFirst(); + if (states.isEmpty()) { + result.add(null); + continue; + } + result.add(states.get(0)); + } + return result; + }).subscribeOn(Schedulers.io()); + } + /////////////////////////////////////////////////////// // Utility /////////////////////////////////////////////////////// @@ -202,4 +296,5 @@ public class HistoryRecordManager { public Single removeOrphanedRecords() { return Single.fromCallable(streamTable::deleteOrphans).subscribeOn(Schedulers.io()); } + } diff --git a/app/src/main/java/org/schabi/newpipe/local/history/StatisticsPlaylistFragment.java b/app/src/main/java/org/schabi/newpipe/local/history/StatisticsPlaylistFragment.java index 5436913dc..31ae70954 100644 --- a/app/src/main/java/org/schabi/newpipe/local/history/StatisticsPlaylistFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/history/StatisticsPlaylistFragment.java @@ -2,13 +2,12 @@ package org.schabi.newpipe.local.history; import android.app.Activity; import android.content.Context; -import android.content.DialogInterface; import android.os.Bundle; import android.os.Parcelable; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.design.widget.Snackbar; -import android.support.v7.app.AlertDialog; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import com.google.android.material.snackbar.Snackbar; +import androidx.appcompat.app.AlertDialog; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -25,6 +24,7 @@ import org.schabi.newpipe.R; import org.schabi.newpipe.database.LocalItem; import org.schabi.newpipe.database.stream.StreamStatisticsEntry; import org.schabi.newpipe.extractor.stream.StreamInfoItem; +import org.schabi.newpipe.extractor.stream.StreamType; import org.schabi.newpipe.info_list.InfoItemDialog; import org.schabi.newpipe.local.BaseLocalListFragment; import org.schabi.newpipe.player.playqueue.PlayQueue; @@ -34,6 +34,7 @@ import org.schabi.newpipe.report.UserAction; import org.schabi.newpipe.settings.SettingsActivity; import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.OnClickGesture; +import org.schabi.newpipe.util.StreamDialogEntry; import org.schabi.newpipe.util.ThemeHelper; import java.util.ArrayList; @@ -179,7 +180,7 @@ public class StatisticsPlaylistFragment .observeOn(AndroidSchedulers.mainThread()) .subscribe( howManyDeleted -> Toast.makeText(getContext(), - R.string.view_history_deleted, + R.string.watch_history_deleted, Toast.LENGTH_SHORT).show(), throwable -> ErrorActivity.reportError(getContext(), throwable, @@ -309,11 +310,11 @@ public class StatisticsPlaylistFragment } headerPlayAllButton.setOnClickListener(view -> - NavigationHelper.playOnMainPlayer(activity, getPlayQueue())); + NavigationHelper.playOnMainPlayer(activity, getPlayQueue(), false)); headerPopupButton.setOnClickListener(view -> - NavigationHelper.playOnPopupPlayer(activity, getPlayQueue())); + NavigationHelper.playOnPopupPlayer(activity, getPlayQueue(), false)); headerBackgroundButton.setOnClickListener(view -> - NavigationHelper.playOnBackgroundPlayer(activity, getPlayQueue())); + NavigationHelper.playOnBackgroundPlayer(activity, getPlayQueue(), false)); sortButton.setOnClickListener(view -> toggleSortMode()); hideLoading(); @@ -356,52 +357,44 @@ public class StatisticsPlaylistFragment startLoading(true); } + private PlayQueue getPlayQueueStartingAt(StreamStatisticsEntry infoItem) { + return getPlayQueue(Math.max(itemListAdapter.getItemsList().indexOf(infoItem), 0)); + } + private void showStreamDialog(final StreamStatisticsEntry item) { final Context context = getContext(); final Activity activity = getActivity(); - if (context == null || context.getResources() == null || getActivity() == null) return; + if (context == null || context.getResources() == null || activity == null) return; final StreamInfoItem infoItem = item.toStreamInfoItem(); - final String[] commands = new String[]{ - context.getResources().getString(R.string.enqueue_on_background), - context.getResources().getString(R.string.enqueue_on_popup), - context.getResources().getString(R.string.start_here_on_main), - context.getResources().getString(R.string.start_here_on_background), - context.getResources().getString(R.string.start_here_on_popup), - context.getResources().getString(R.string.delete), - context.getResources().getString(R.string.share) - }; + if (infoItem.getStreamType() == StreamType.AUDIO_STREAM) { + StreamDialogEntry.setEnabledEntries( + StreamDialogEntry.enqueue_on_background, + StreamDialogEntry.start_here_on_background, + StreamDialogEntry.delete, + StreamDialogEntry.append_playlist, + StreamDialogEntry.share); + } else { + StreamDialogEntry.setEnabledEntries( + StreamDialogEntry.enqueue_on_background, + StreamDialogEntry.enqueue_on_popup, + StreamDialogEntry.start_here_on_background, + StreamDialogEntry.start_here_on_popup, + StreamDialogEntry.delete, + StreamDialogEntry.append_playlist, + StreamDialogEntry.share); - final DialogInterface.OnClickListener actions = (dialogInterface, i) -> { - final int index = Math.max(itemListAdapter.getItemsList().indexOf(item), 0); - switch (i) { - case 0: - NavigationHelper.enqueueOnBackgroundPlayer(context, new SinglePlayQueue(infoItem)); - break; - case 1: - NavigationHelper.enqueueOnPopupPlayer(activity, new SinglePlayQueue(infoItem)); - break; - case 2: - NavigationHelper.playOnMainPlayer(context, getPlayQueue(index)); - break; - case 3: - NavigationHelper.playOnBackgroundPlayer(context, getPlayQueue(index)); - break; - case 4: - NavigationHelper.playOnPopupPlayer(activity, getPlayQueue(index)); - break; - case 5: - deleteEntry(index); - break; - case 6: - shareUrl(item.toStreamInfoItem().getName(), item.toStreamInfoItem().getUrl()); - break; - default: - break; - } - }; + StreamDialogEntry.start_here_on_popup.setCustomAction( + (fragment, infoItemDuplicate) -> NavigationHelper.playOnPopupPlayer(context, getPlayQueueStartingAt(item), true)); + } - new InfoItemDialog(getActivity(), infoItem, commands, actions).show(); + StreamDialogEntry.start_here_on_background.setCustomAction( + (fragment, infoItemDuplicate) -> NavigationHelper.playOnBackgroundPlayer(context, getPlayQueueStartingAt(item), true)); + StreamDialogEntry.delete.setCustomAction((fragment, infoItemDuplicate) -> + deleteEntry(Math.max(itemListAdapter.getItemsList().indexOf(item), 0))); + + new InfoItemDialog(activity, infoItem, StreamDialogEntry.getCommands(context), (dialog, which) -> + StreamDialogEntry.clickOn(which, this, infoItem)).show(); } private void deleteEntry(final int index) { diff --git a/app/src/main/java/org/schabi/newpipe/local/holder/LocalItemHolder.java b/app/src/main/java/org/schabi/newpipe/local/holder/LocalItemHolder.java index 889751afa..f9da969a5 100644 --- a/app/src/main/java/org/schabi/newpipe/local/holder/LocalItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/local/holder/LocalItemHolder.java @@ -1,11 +1,12 @@ package org.schabi.newpipe.local.holder; -import android.support.v7.widget.RecyclerView; +import androidx.recyclerview.widget.RecyclerView; import android.view.LayoutInflater; import android.view.ViewGroup; import org.schabi.newpipe.database.LocalItem; import org.schabi.newpipe.local.LocalItemBuilder; +import org.schabi.newpipe.local.history.HistoryRecordManager; import java.text.DateFormat; @@ -38,5 +39,8 @@ public abstract class LocalItemHolder extends RecyclerView.ViewHolder { this.itemBuilder = itemBuilder; } - public abstract void updateFromItem(final LocalItem item, final DateFormat dateFormat); + public abstract void updateFromItem(final LocalItem item, HistoryRecordManager historyRecordManager, final DateFormat dateFormat); + + public void updateState(final LocalItem localItem, HistoryRecordManager historyRecordManager) { + } } diff --git a/app/src/main/java/org/schabi/newpipe/local/holder/LocalPlaylistItemHolder.java b/app/src/main/java/org/schabi/newpipe/local/holder/LocalPlaylistItemHolder.java index 8743684ee..1366bd02e 100644 --- a/app/src/main/java/org/schabi/newpipe/local/holder/LocalPlaylistItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/local/holder/LocalPlaylistItemHolder.java @@ -6,6 +6,7 @@ import android.view.ViewGroup; import org.schabi.newpipe.database.LocalItem; import org.schabi.newpipe.database.playlist.PlaylistMetadataEntry; import org.schabi.newpipe.local.LocalItemBuilder; +import org.schabi.newpipe.local.history.HistoryRecordManager; import org.schabi.newpipe.util.ImageDisplayConstants; import java.text.DateFormat; @@ -21,7 +22,7 @@ public class LocalPlaylistItemHolder extends PlaylistItemHolder { } @Override - public void updateFromItem(final LocalItem localItem, final DateFormat dateFormat) { + public void updateFromItem(final LocalItem localItem, HistoryRecordManager historyRecordManager, final DateFormat dateFormat) { if (!(localItem instanceof PlaylistMetadataEntry)) return; final PlaylistMetadataEntry item = (PlaylistMetadataEntry) localItem; @@ -32,6 +33,6 @@ public class LocalPlaylistItemHolder extends PlaylistItemHolder { itemBuilder.displayImage(item.thumbnailUrl, itemThumbnailView, ImageDisplayConstants.DISPLAY_PLAYLIST_OPTIONS); - super.updateFromItem(localItem, dateFormat); + super.updateFromItem(localItem, historyRecordManager, dateFormat); } } diff --git a/app/src/main/java/org/schabi/newpipe/local/holder/LocalPlaylistStreamItemHolder.java b/app/src/main/java/org/schabi/newpipe/local/holder/LocalPlaylistStreamItemHolder.java index e591b73e5..30cc6de32 100644 --- a/app/src/main/java/org/schabi/newpipe/local/holder/LocalPlaylistStreamItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/local/holder/LocalPlaylistStreamItemHolder.java @@ -1,6 +1,6 @@ package org.schabi.newpipe.local.holder; -import android.support.v4.content.ContextCompat; +import androidx.core.content.ContextCompat; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; @@ -10,12 +10,18 @@ import android.widget.TextView; import org.schabi.newpipe.R; import org.schabi.newpipe.database.LocalItem; import org.schabi.newpipe.database.playlist.PlaylistStreamEntry; +import org.schabi.newpipe.database.stream.model.StreamStateEntity; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.local.LocalItemBuilder; +import org.schabi.newpipe.local.history.HistoryRecordManager; +import org.schabi.newpipe.util.AnimationUtils; import org.schabi.newpipe.util.ImageDisplayConstants; import org.schabi.newpipe.util.Localization; +import org.schabi.newpipe.views.AnimatedProgressBar; import java.text.DateFormat; +import java.util.ArrayList; +import java.util.concurrent.TimeUnit; public class LocalPlaylistStreamItemHolder extends LocalItemHolder { @@ -24,6 +30,7 @@ public class LocalPlaylistStreamItemHolder extends LocalItemHolder { public final TextView itemAdditionalDetailsView; public final TextView itemDurationView; public final View itemHandleView; + public final AnimatedProgressBar itemProgressView; LocalPlaylistStreamItemHolder(LocalItemBuilder infoItemBuilder, int layoutId, ViewGroup parent) { super(infoItemBuilder, layoutId, parent); @@ -33,6 +40,7 @@ public class LocalPlaylistStreamItemHolder extends LocalItemHolder { itemAdditionalDetailsView = itemView.findViewById(R.id.itemAdditionalDetails); itemDurationView = itemView.findViewById(R.id.itemDurationView); itemHandleView = itemView.findViewById(R.id.itemHandle); + itemProgressView = itemView.findViewById(R.id.itemProgressView); } public LocalPlaylistStreamItemHolder(LocalItemBuilder infoItemBuilder, ViewGroup parent) { @@ -40,7 +48,7 @@ public class LocalPlaylistStreamItemHolder extends LocalItemHolder { } @Override - public void updateFromItem(final LocalItem localItem, final DateFormat dateFormat) { + public void updateFromItem(final LocalItem localItem, HistoryRecordManager historyRecordManager, final DateFormat dateFormat) { if (!(localItem instanceof PlaylistStreamEntry)) return; final PlaylistStreamEntry item = (PlaylistStreamEntry) localItem; @@ -53,6 +61,15 @@ public class LocalPlaylistStreamItemHolder extends LocalItemHolder { itemDurationView.setBackgroundColor(ContextCompat.getColor(itemBuilder.getContext(), R.color.duration_background_color)); itemDurationView.setVisibility(View.VISIBLE); + + StreamStateEntity state = historyRecordManager.loadLocalStreamStateBatch(new ArrayList() {{ add(localItem); }}).blockingGet().get(0); + if (state != null) { + itemProgressView.setVisibility(View.VISIBLE); + itemProgressView.setMax((int) item.duration); + itemProgressView.setProgress((int) TimeUnit.MILLISECONDS.toSeconds(state.getProgressTime())); + } else { + itemProgressView.setVisibility(View.GONE); + } } else { itemDurationView.setVisibility(View.GONE); } @@ -79,6 +96,25 @@ public class LocalPlaylistStreamItemHolder extends LocalItemHolder { itemHandleView.setOnTouchListener(getOnTouchListener(item)); } + @Override + public void updateState(LocalItem localItem, HistoryRecordManager historyRecordManager) { + if (!(localItem instanceof PlaylistStreamEntry)) return; + final PlaylistStreamEntry item = (PlaylistStreamEntry) localItem; + + StreamStateEntity state = historyRecordManager.loadLocalStreamStateBatch(new ArrayList() {{ add(localItem); }}).blockingGet().get(0); + if (state != null && item.duration > 0) { + itemProgressView.setMax((int) item.duration); + if (itemProgressView.getVisibility() == View.VISIBLE) { + itemProgressView.setProgressAnimated((int) TimeUnit.MILLISECONDS.toSeconds(state.getProgressTime())); + } else { + itemProgressView.setProgress((int) TimeUnit.MILLISECONDS.toSeconds(state.getProgressTime())); + AnimationUtils.animateView(itemProgressView, true, 500); + } + } else if (itemProgressView.getVisibility() == View.VISIBLE) { + AnimationUtils.animateView(itemProgressView, false, 500); + } + } + private View.OnTouchListener getOnTouchListener(final PlaylistStreamEntry item) { return (view, motionEvent) -> { view.performClick(); diff --git a/app/src/main/java/org/schabi/newpipe/local/holder/LocalStatisticStreamItemHolder.java b/app/src/main/java/org/schabi/newpipe/local/holder/LocalStatisticStreamItemHolder.java index 57a5794e3..75fbf13ea 100644 --- a/app/src/main/java/org/schabi/newpipe/local/holder/LocalStatisticStreamItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/local/holder/LocalStatisticStreamItemHolder.java @@ -1,7 +1,7 @@ package org.schabi.newpipe.local.holder; -import android.support.annotation.Nullable; -import android.support.v4.content.ContextCompat; +import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; @@ -10,12 +10,18 @@ import android.widget.TextView; import org.schabi.newpipe.R; import org.schabi.newpipe.database.LocalItem; import org.schabi.newpipe.database.stream.StreamStatisticsEntry; +import org.schabi.newpipe.database.stream.model.StreamStateEntity; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.local.LocalItemBuilder; +import org.schabi.newpipe.local.history.HistoryRecordManager; +import org.schabi.newpipe.util.AnimationUtils; import org.schabi.newpipe.util.ImageDisplayConstants; import org.schabi.newpipe.util.Localization; +import org.schabi.newpipe.views.AnimatedProgressBar; import java.text.DateFormat; +import java.util.ArrayList; +import java.util.concurrent.TimeUnit; /* * Created by Christian Schabesberger on 01.08.16. @@ -45,6 +51,7 @@ public class LocalStatisticStreamItemHolder extends LocalItemHolder { public final TextView itemDurationView; @Nullable public final TextView itemAdditionalDetails; + public final AnimatedProgressBar itemProgressView; public LocalStatisticStreamItemHolder(LocalItemBuilder itemBuilder, ViewGroup parent) { this(itemBuilder, R.layout.list_stream_item, parent); @@ -58,6 +65,7 @@ public class LocalStatisticStreamItemHolder extends LocalItemHolder { itemUploaderView = itemView.findViewById(R.id.itemUploaderView); itemDurationView = itemView.findViewById(R.id.itemDurationView); itemAdditionalDetails = itemView.findViewById(R.id.itemAdditionalDetails); + itemProgressView = itemView.findViewById(R.id.itemProgressView); } private String getStreamInfoDetailLine(final StreamStatisticsEntry entry, @@ -70,7 +78,7 @@ public class LocalStatisticStreamItemHolder extends LocalItemHolder { } @Override - public void updateFromItem(final LocalItem localItem, final DateFormat dateFormat) { + public void updateFromItem(final LocalItem localItem, HistoryRecordManager historyRecordManager, final DateFormat dateFormat) { if (!(localItem instanceof StreamStatisticsEntry)) return; final StreamStatisticsEntry item = (StreamStatisticsEntry) localItem; @@ -82,8 +90,18 @@ public class LocalStatisticStreamItemHolder extends LocalItemHolder { itemDurationView.setBackgroundColor(ContextCompat.getColor(itemBuilder.getContext(), R.color.duration_background_color)); itemDurationView.setVisibility(View.VISIBLE); + + StreamStateEntity state = historyRecordManager.loadLocalStreamStateBatch(new ArrayList() {{ add(localItem); }}).blockingGet().get(0); + if (state != null) { + itemProgressView.setVisibility(View.VISIBLE); + itemProgressView.setMax((int) item.duration); + itemProgressView.setProgress((int) TimeUnit.MILLISECONDS.toSeconds(state.getProgressTime())); + } else { + itemProgressView.setVisibility(View.GONE); + } } else { itemDurationView.setVisibility(View.GONE); + itemProgressView.setVisibility(View.GONE); } if (itemAdditionalDetails != null) { @@ -108,4 +126,23 @@ public class LocalStatisticStreamItemHolder extends LocalItemHolder { return true; }); } + + @Override + public void updateState(LocalItem localItem, HistoryRecordManager historyRecordManager) { + if (!(localItem instanceof StreamStatisticsEntry)) return; + final StreamStatisticsEntry item = (StreamStatisticsEntry) localItem; + + StreamStateEntity state = historyRecordManager.loadLocalStreamStateBatch(new ArrayList() {{ add(localItem); }}).blockingGet().get(0); + if (state != null && item.duration > 0) { + itemProgressView.setMax((int) item.duration); + if (itemProgressView.getVisibility() == View.VISIBLE) { + itemProgressView.setProgressAnimated((int) TimeUnit.MILLISECONDS.toSeconds(state.getProgressTime())); + } else { + itemProgressView.setProgress((int) TimeUnit.MILLISECONDS.toSeconds(state.getProgressTime())); + AnimationUtils.animateView(itemProgressView, true, 500); + } + } else if (itemProgressView.getVisibility() == View.VISIBLE) { + AnimationUtils.animateView(itemProgressView, false, 500); + } + } } diff --git a/app/src/main/java/org/schabi/newpipe/local/holder/PlaylistItemHolder.java b/app/src/main/java/org/schabi/newpipe/local/holder/PlaylistItemHolder.java index 5d6f192e1..c5f1813c7 100644 --- a/app/src/main/java/org/schabi/newpipe/local/holder/PlaylistItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/local/holder/PlaylistItemHolder.java @@ -7,6 +7,7 @@ import android.widget.TextView; import org.schabi.newpipe.R; import org.schabi.newpipe.database.LocalItem; import org.schabi.newpipe.local.LocalItemBuilder; +import org.schabi.newpipe.local.history.HistoryRecordManager; import java.text.DateFormat; @@ -31,7 +32,7 @@ public abstract class PlaylistItemHolder extends LocalItemHolder { } @Override - public void updateFromItem(final LocalItem localItem, final DateFormat dateFormat) { + public void updateFromItem(final LocalItem localItem, HistoryRecordManager historyRecordManager, final DateFormat dateFormat) { itemView.setOnClickListener(view -> { if (itemBuilder.getOnItemSelectedListener() != null) { itemBuilder.getOnItemSelectedListener().selected(localItem); diff --git a/app/src/main/java/org/schabi/newpipe/local/holder/RemotePlaylistItemHolder.java b/app/src/main/java/org/schabi/newpipe/local/holder/RemotePlaylistItemHolder.java index 5b2a88d38..f9542850e 100644 --- a/app/src/main/java/org/schabi/newpipe/local/holder/RemotePlaylistItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/local/holder/RemotePlaylistItemHolder.java @@ -6,6 +6,7 @@ import org.schabi.newpipe.database.LocalItem; import org.schabi.newpipe.database.playlist.model.PlaylistRemoteEntity; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.local.LocalItemBuilder; +import org.schabi.newpipe.local.history.HistoryRecordManager; import org.schabi.newpipe.util.ImageDisplayConstants; import org.schabi.newpipe.util.Localization; @@ -21,7 +22,7 @@ public class RemotePlaylistItemHolder extends PlaylistItemHolder { } @Override - public void updateFromItem(final LocalItem localItem, final DateFormat dateFormat) { + public void updateFromItem(final LocalItem localItem, HistoryRecordManager historyRecordManager, final DateFormat dateFormat) { if (!(localItem instanceof PlaylistRemoteEntity)) return; final PlaylistRemoteEntity item = (PlaylistRemoteEntity) localItem; @@ -33,6 +34,6 @@ public class RemotePlaylistItemHolder extends PlaylistItemHolder { itemBuilder.displayImage(item.getThumbnailUrl(), itemThumbnailView, ImageDisplayConstants.DISPLAY_PLAYLIST_OPTIONS); - super.updateFromItem(localItem, dateFormat); + super.updateFromItem(localItem, historyRecordManager, dateFormat); } } diff --git a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java index f400061e1..9e72838ad 100644 --- a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java @@ -2,14 +2,13 @@ package org.schabi.newpipe.local.playlist; import android.app.Activity; import android.content.Context; -import android.content.DialogInterface; import android.os.Bundle; import android.os.Parcelable; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v7.app.AlertDialog; -import android.support.v7.widget.RecyclerView; -import android.support.v7.widget.helper.ItemTouchHelper; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; +import androidx.recyclerview.widget.RecyclerView; +import androidx.recyclerview.widget.ItemTouchHelper; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; @@ -26,14 +25,16 @@ import org.schabi.newpipe.R; import org.schabi.newpipe.database.LocalItem; import org.schabi.newpipe.database.playlist.PlaylistStreamEntry; import org.schabi.newpipe.extractor.stream.StreamInfoItem; -import org.schabi.newpipe.local.BaseLocalListFragment; +import org.schabi.newpipe.extractor.stream.StreamType; import org.schabi.newpipe.info_list.InfoItemDialog; +import org.schabi.newpipe.local.BaseLocalListFragment; import org.schabi.newpipe.player.playqueue.PlayQueue; import org.schabi.newpipe.player.playqueue.SinglePlayQueue; import org.schabi.newpipe.report.UserAction; import org.schabi.newpipe.util.Localization; import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.OnClickGesture; +import org.schabi.newpipe.util.StreamDialogEntry; import java.util.ArrayList; import java.util.Collections; @@ -318,11 +319,11 @@ public class LocalPlaylistFragment extends BaseLocalListFragment - NavigationHelper.playOnMainPlayer(activity, getPlayQueue())); + NavigationHelper.playOnMainPlayer(activity, getPlayQueue(), false)); headerPopupButton.setOnClickListener(view -> - NavigationHelper.playOnPopupPlayer(activity, getPlayQueue())); + NavigationHelper.playOnPopupPlayer(activity, getPlayQueue(), false)); headerBackgroundButton.setOnClickListener(view -> - NavigationHelper.playOnBackgroundPlayer(activity, getPlayQueue())); + NavigationHelper.playOnBackgroundPlayer(activity, getPlayQueue(), false)); hideLoading(); } @@ -510,59 +511,48 @@ public class LocalPlaylistFragment extends BaseLocalListFragment { - final int index = Math.max(itemListAdapter.getItemsList().indexOf(item), 0); - switch (i) { - case 0: - NavigationHelper.enqueueOnBackgroundPlayer(context, - new SinglePlayQueue(infoItem)); - break; - case 1: - NavigationHelper.enqueueOnPopupPlayer(activity, new - SinglePlayQueue(infoItem)); - break; - case 2: - NavigationHelper.playOnMainPlayer(context, getPlayQueue(index)); - break; - case 3: - NavigationHelper.playOnBackgroundPlayer(context, getPlayQueue(index)); - break; - case 4: - NavigationHelper.playOnPopupPlayer(activity, getPlayQueue(index)); - break; - case 5: - changeThumbnailUrl(item.thumbnailUrl); - break; - case 6: - deleteItem(item); - break; - case 7: - shareUrl(item.toStreamInfoItem().getName(), item.toStreamInfoItem().getUrl()); - break; - default: - break; - } - }; + StreamDialogEntry.start_here_on_popup.setCustomAction( + (fragment, infoItemDuplicate) -> NavigationHelper.playOnPopupPlayer(context, getPlayQueueStartingAt(item), true)); + } - new InfoItemDialog(getActivity(), infoItem, commands, actions).show(); + StreamDialogEntry.start_here_on_background.setCustomAction( + (fragment, infoItemDuplicate) -> NavigationHelper.playOnBackgroundPlayer(context, getPlayQueueStartingAt(item), true)); + StreamDialogEntry.set_as_playlist_thumbnail.setCustomAction( + (fragment, infoItemDuplicate) -> changeThumbnailUrl(item.thumbnailUrl)); + StreamDialogEntry.delete.setCustomAction( + (fragment, infoItemDuplicate) -> deleteItem(item)); + + new InfoItemDialog(activity, infoItem, StreamDialogEntry.getCommands(context), (dialog, which) -> + StreamDialogEntry.clickOn(which, this, infoItem)).show(); } private void setInitialData(long playlistId, String name) { diff --git a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistManager.java b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistManager.java index 3ed1b864c..c025b360a 100644 --- a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistManager.java +++ b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistManager.java @@ -1,6 +1,6 @@ package org.schabi.newpipe.local.playlist; -import android.support.annotation.Nullable; +import androidx.annotation.Nullable; import org.schabi.newpipe.database.AppDatabase; import org.schabi.newpipe.database.playlist.PlaylistMetadataEntry; diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/ImportConfirmationDialog.java b/app/src/main/java/org/schabi/newpipe/local/subscription/ImportConfirmationDialog.java index 6ff151dc1..fbcf5d70e 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/ImportConfirmationDialog.java +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/ImportConfirmationDialog.java @@ -4,10 +4,10 @@ import android.app.AlertDialog; import android.app.Dialog; import android.content.Intent; import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.app.DialogFragment; -import android.support.v4.app.Fragment; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.DialogFragment; +import androidx.fragment.app.Fragment; import org.schabi.newpipe.R; import org.schabi.newpipe.util.ThemeHelper; diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/ImportExportJsonHelper.java b/app/src/main/java/org/schabi/newpipe/local/subscription/ImportExportJsonHelper.java index 6c8e8ea6c..ebfff9fe2 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/ImportExportJsonHelper.java +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/ImportExportJsonHelper.java @@ -19,7 +19,7 @@ package org.schabi.newpipe.local.subscription; -import android.support.annotation.Nullable; +import androidx.annotation.Nullable; import com.grack.nanojson.JsonAppendableWriter; import com.grack.nanojson.JsonArray; diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.java b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.java index a304c2f0c..bff6c1b3a 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.java @@ -17,16 +17,15 @@ import android.os.Bundle; import android.os.Environment; import android.os.Parcelable; import android.preference.PreferenceManager; -import android.support.annotation.DrawableRes; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.app.FragmentManager; -import android.support.v4.content.LocalBroadcastManager; -import android.support.v7.app.ActionBar; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.GridLayoutManager; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; +import androidx.annotation.DrawableRes; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.FragmentManager; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; +import androidx.appcompat.app.ActionBar; +import androidx.recyclerview.widget.GridLayoutManager; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -48,15 +47,14 @@ import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor; import org.schabi.newpipe.fragments.BaseStateFragment; import org.schabi.newpipe.info_list.InfoListAdapter; -import org.schabi.newpipe.report.UserAction; import org.schabi.newpipe.local.subscription.services.SubscriptionsExportService; import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService; -import org.schabi.newpipe.report.ErrorActivity; import org.schabi.newpipe.report.UserAction; import org.schabi.newpipe.util.FilePickerActivityHelper; import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.OnClickGesture; import org.schabi.newpipe.util.ServiceHelper; +import org.schabi.newpipe.util.ShareUtils; import org.schabi.newpipe.util.ThemeHelper; import org.schabi.newpipe.views.CollapsibleView; @@ -130,6 +128,11 @@ public class SubscriptionFragment extends BaseStateFragment { FragmentManager fragmentManager = getFM(); NavigationHelper.openWhatsNewFragment(fragmentManager); @@ -390,17 +392,17 @@ public class SubscriptionFragment extends BaseStateFragment { switch (i) { case 0: - shareChannel(selectedItem); + deleteChannel(selectedItem); break; case 1: - deleteChannel(selectedItem); + shareChannel(selectedItem); break; default: break; @@ -424,12 +426,12 @@ public class SubscriptionFragment extends BaseStateFragment> getDeleteObserver(){ + private Observer> getDeleteObserver() { return new Observer>() { @Override public void onSubscribe(Disposable d) { diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionService.java b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionService.java index 7e80264e6..7d6fa5158 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionService.java +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionService.java @@ -1,7 +1,7 @@ package org.schabi.newpipe.local.subscription; import android.content.Context; -import android.support.annotation.NonNull; +import androidx.annotation.NonNull; import android.util.Log; import org.schabi.newpipe.MainActivity; @@ -93,7 +93,7 @@ public class SubscriptionService { * in the cooldown interval, only the latest changes are emitted to the subscribers. * This reduces the amount of observations caused by frequent updates to the database. */ - @android.support.annotation.NonNull + @androidx.annotation.NonNull public Flowable> getSubscription() { return subscription; } diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionsImportFragment.java b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionsImportFragment.java index ed694c70e..0a45e680a 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionsImportFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionsImportFragment.java @@ -3,11 +3,11 @@ package org.schabi.newpipe.local.subscription; import android.app.Activity; import android.content.Intent; import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.annotation.StringRes; -import android.support.v4.text.util.LinkifyCompat; -import android.support.v7.app.ActionBar; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.annotation.StringRes; +import androidx.core.text.util.LinkifyCompat; +import androidx.appcompat.app.ActionBar; import android.text.TextUtils; import android.text.util.Linkify; import android.view.LayoutInflater; diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/services/BaseImportExportService.java b/app/src/main/java/org/schabi/newpipe/local/subscription/services/BaseImportExportService.java index e3db6e12c..6b607cdca 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/services/BaseImportExportService.java +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/services/BaseImportExportService.java @@ -23,11 +23,11 @@ import android.app.Service; import android.content.Intent; import android.os.Build; import android.os.IBinder; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.annotation.StringRes; -import android.support.v4.app.NotificationCompat; -import android.support.v4.app.NotificationManagerCompat; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.annotation.StringRes; +import androidx.core.app.NotificationCompat; +import androidx.core.app.NotificationManagerCompat; import android.text.TextUtils; import android.widget.Toast; diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/services/SubscriptionsExportService.java b/app/src/main/java/org/schabi/newpipe/local/subscription/services/SubscriptionsExportService.java index 35da7dd4f..31cd4b603 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/services/SubscriptionsExportService.java +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/services/SubscriptionsExportService.java @@ -20,7 +20,7 @@ package org.schabi.newpipe.local.subscription.services; import android.content.Intent; -import android.support.v4.content.LocalBroadcastManager; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; import android.text.TextUtils; import android.util.Log; diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/services/SubscriptionsImportService.java b/app/src/main/java/org/schabi/newpipe/local/subscription/services/SubscriptionsImportService.java index 48410205a..62c1dfeb9 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/services/SubscriptionsImportService.java +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/services/SubscriptionsImportService.java @@ -20,9 +20,9 @@ package org.schabi.newpipe.local.subscription.services; import android.content.Intent; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.content.LocalBroadcastManager; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; import android.text.TextUtils; import android.util.Log; diff --git a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java index 3989581fd..ab07ded22 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java @@ -28,9 +28,9 @@ import android.content.IntentFilter; import android.graphics.Bitmap; import android.os.Build; import android.os.IBinder; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.app.NotificationCompat; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.app.NotificationCompat; import android.util.Log; import android.view.View; import android.widget.RemoteViews; @@ -150,6 +150,7 @@ public final class BackgroundPlayer extends Service { lockManager.releaseWifiAndCpu(); } if (basePlayerImpl != null) { + basePlayerImpl.savePlaybackState(); basePlayerImpl.stopActivityBinding(); basePlayerImpl.destroy(); } diff --git a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java index d1b06c9c5..b3c5716bc 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java @@ -23,15 +23,18 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.media.AudioManager; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import android.preference.PreferenceManager; import android.util.Log; import android.view.View; import android.widget.Toast; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + import com.google.android.exoplayer2.DefaultRenderersFactory; import com.google.android.exoplayer2.ExoPlaybackException; import com.google.android.exoplayer2.ExoPlayerFactory; @@ -145,6 +148,8 @@ public abstract class BasePlayer implements @NonNull public static final String APPEND_ONLY = "append_only"; @NonNull + public static final String RESUME_PLAYBACK = "resume_playback"; + @NonNull public static final String SELECT_ON_APPEND = "select_on_append"; /*////////////////////////////////////////////////////////////////////////// @@ -183,6 +188,7 @@ public abstract class BasePlayer implements protected MediaSessionManager mediaSessionManager; private boolean isPrepared = false; + private Disposable stateLoader; //////////////////////////////////////////////////////////////////////////*/ @@ -204,11 +210,10 @@ public abstract class BasePlayer implements this.databaseUpdateReactor = new CompositeDisposable(); final String userAgent = Downloader.USER_AGENT; - final DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); + final DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter.Builder(context).build(); this.dataSource = new PlayerDataSource(context, userAgent, bandwidthMeter); - final TrackSelection.Factory trackSelectionFactory = - PlayerHelper.getQualitySelector(context, bandwidthMeter); + final TrackSelection.Factory trackSelectionFactory = PlayerHelper.getQualitySelector(context); this.trackSelector = new CustomTrackSelector(trackSelectionFactory); this.loadControl = new LoadController(context); @@ -225,7 +230,7 @@ public abstract class BasePlayer implements public void initPlayer(final boolean playOnReady) { if (DEBUG) Log.d(TAG, "initPlayer() called with: context = [" + context + "]"); - simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(renderFactory, trackSelector, loadControl); + simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(context, renderFactory, trackSelector, loadControl); simpleExoPlayer.addListener(this); simpleExoPlayer.setPlayWhenReady(playOnReady); simpleExoPlayer.setSeekParameters(PlayerHelper.getSeekParameters(context)); @@ -270,6 +275,34 @@ public abstract class BasePlayer implements final boolean playbackSkipSilence = intent.getBooleanExtra(PLAYBACK_SKIP_SILENCE, getPlaybackSkipSilence()); + // seek to timestamp if stream is already playing + if (simpleExoPlayer != null + && queue.size() == 1 + && playQueue != null + && playQueue.getItem() != null + && queue.getItem().getUrl().equals(playQueue.getItem().getUrl()) + && queue.getItem().getRecoveryPosition() != PlayQueueItem.RECOVERY_UNSET + ) { + simpleExoPlayer.seekTo(playQueue.getIndex(), queue.getItem().getRecoveryPosition()); + return; + + } else if (intent.getBooleanExtra(RESUME_PLAYBACK, false) && isPlaybackResumeEnabled()) { + final PlayQueueItem item = queue.getItem(); + if (item != null && item.getRecoveryPosition() == PlayQueueItem.RECOVERY_UNSET) { + stateLoader = recordManager.loadStreamState(item) + .observeOn(AndroidSchedulers.mainThread()) + .doFinally(() -> initPlayback(queue, repeatMode, playbackSpeed, playbackPitch, playbackSkipSilence, + /*playOnInit=*/true)) + .subscribe( + state -> queue.setRecovery(queue.getIndex(), state.getProgressTime()), + error -> { + if (DEBUG) error.printStackTrace(); + } + ); + databaseUpdateReactor.add(stateLoader); + return; + } + } // Good to go... initPlayback(queue, repeatMode, playbackSpeed, playbackPitch, playbackSkipSilence, /*playOnInit=*/true); @@ -307,6 +340,7 @@ public abstract class BasePlayer implements if (audioReactor != null) audioReactor.dispose(); if (playbackManager != null) playbackManager.dispose(); if (mediaSessionManager != null) mediaSessionManager.dispose(); + if (stateLoader != null) stateLoader.dispose(); if (playQueueAdapter != null) { playQueueAdapter.unsetSelectedListener(); @@ -604,6 +638,9 @@ public abstract class BasePlayer implements break; case Player.STATE_ENDED: // 4 changeState(STATE_COMPLETED); + if (currentMetadata != null) { + resetPlaybackState(currentMetadata.getMetadata()); + } isPrepared = false; break; } @@ -710,6 +747,7 @@ public abstract class BasePlayer implements case DISCONTINUITY_REASON_SEEK_ADJUSTMENT: case DISCONTINUITY_REASON_INTERNAL: if (playQueue.getIndex() != newWindowIndex) { + resetPlaybackState(playQueue.getItem()); playQueue.setIndex(newWindowIndex); } break; @@ -739,6 +777,9 @@ public abstract class BasePlayer implements @Override public void onSeekProcessed() { if (DEBUG) Log.d(TAG, "ExoPlayer - onSeekProcessed() called"); + if (isPrepared) { + savePlaybackState(); + } } /*////////////////////////////////////////////////////////////////////////// // Playback Listener @@ -1006,27 +1047,46 @@ public abstract class BasePlayer implements } } - protected void savePlaybackState(final StreamInfo info, final long progress) { + private void savePlaybackState(final StreamInfo info, final long progress) { if (info == null) return; - final Disposable stateSaver = recordManager.saveStreamState(info, progress) - .observeOn(AndroidSchedulers.mainThread()) - .onErrorComplete() - .subscribe( - ignored -> {/* successful */}, - error -> Log.e(TAG, "savePlaybackState() failure: ", error) - ); - databaseUpdateReactor.add(stateSaver); + if (DEBUG) Log.d(TAG, "savePlaybackState() called"); + final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + if (prefs.getBoolean(context.getString(R.string.enable_watch_history_key), true)) { + final Disposable stateSaver = recordManager.saveStreamState(info, progress) + .observeOn(AndroidSchedulers.mainThread()) + .doOnError((e) -> { + if (DEBUG) e.printStackTrace(); + }) + .onErrorComplete() + .subscribe(); + databaseUpdateReactor.add(stateSaver); + } } - private void savePlaybackState() { + private void resetPlaybackState(final PlayQueueItem queueItem) { + if (queueItem == null) return; + final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + if (prefs.getBoolean(context.getString(R.string.enable_watch_history_key), true)) { + final Disposable stateSaver = queueItem.getStream() + .flatMapCompletable(info -> recordManager.saveStreamState(info, 0)) + .observeOn(AndroidSchedulers.mainThread()) + .doOnError((e) -> { + if (DEBUG) e.printStackTrace(); + }) + .onErrorComplete() + .subscribe(); + databaseUpdateReactor.add(stateSaver); + } + } + + public void resetPlaybackState(final StreamInfo info) { + savePlaybackState(info, 0); + } + + public void savePlaybackState() { if (simpleExoPlayer == null || currentMetadata == null) return; final StreamInfo currentInfo = currentMetadata.getMetadata(); - - if (simpleExoPlayer.getCurrentPosition() > RECOVERY_SKIP_THRESHOLD_MILLIS && - simpleExoPlayer.getCurrentPosition() < - simpleExoPlayer.getDuration() - RECOVERY_SKIP_THRESHOLD_MILLIS) { - savePlaybackState(currentInfo, simpleExoPlayer.getCurrentPosition()); - } + savePlaybackState(currentInfo, simpleExoPlayer.getCurrentPosition()); } private void maybeUpdateCurrentMetadata() { @@ -1134,10 +1194,7 @@ public abstract class BasePlayer implements } public boolean isPlaying() { - if (simpleExoPlayer == null) return false; - final int state = simpleExoPlayer.getPlaybackState(); - return (state == Player.STATE_READY || state == Player.STATE_BUFFERING) - && simpleExoPlayer.getPlayWhenReady(); + return simpleExoPlayer != null && simpleExoPlayer.isPlaying(); } @Player.RepeatMode @@ -1214,4 +1271,10 @@ public abstract class BasePlayer implements public boolean gotDestroyed() { return simpleExoPlayer == null; } + + private boolean isPlaybackResumeEnabled() { + final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + return prefs.getBoolean(context.getString(R.string.enable_watch_history_key), true) + && prefs.getBoolean(context.getString(R.string.enable_playback_resume_key), true); + } } diff --git a/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java b/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java index 5dd931b54..7a3e60c66 100644 --- a/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java @@ -24,21 +24,23 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ActivityInfo; import android.content.res.Configuration; +import android.database.ContentObserver; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.media.AudioManager; import android.os.Build; import android.os.Bundle; +import android.os.Handler; import android.preference.PreferenceManager; import android.provider.Settings; -import android.support.annotation.ColorInt; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.app.ActivityCompat; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.content.res.AppCompatResources; -import android.support.v7.widget.RecyclerView; -import android.support.v7.widget.helper.ItemTouchHelper; +import androidx.annotation.ColorInt; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.app.ActivityCompat; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.content.res.AppCompatResources; +import androidx.recyclerview.widget.RecyclerView; +import androidx.recyclerview.widget.ItemTouchHelper; import android.util.DisplayMetrics; import android.util.Log; import android.util.TypedValue; @@ -46,6 +48,7 @@ import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; +import android.widget.Button; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.PopupMenu; @@ -75,6 +78,7 @@ import org.schabi.newpipe.util.AnimationUtils; import org.schabi.newpipe.util.ListHelper; import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.PermissionHelper; +import org.schabi.newpipe.util.ShareUtils; import org.schabi.newpipe.util.StateSaver; import org.schabi.newpipe.util.ThemeHelper; @@ -111,6 +115,8 @@ public final class MainVideoPlayer extends AppCompatActivity private boolean isInMultiWindow; private boolean isBackPressed; + private ContentObserver rotationObserver; + /*////////////////////////////////////////////////////////////////////////// // Activity LifeCycle //////////////////////////////////////////////////////////////////////////*/ @@ -145,6 +151,23 @@ public final class MainVideoPlayer extends AppCompatActivity Toast.makeText(this, R.string.general_error, Toast.LENGTH_SHORT).show(); finish(); } + + rotationObserver = new ContentObserver(new Handler()) { + @Override + public void onChange(boolean selfChange) { + super.onChange(selfChange); + if (globalScreenOrientationLocked()) { + final boolean lastOrientationWasLandscape = defaultPreferences.getBoolean( + getString(R.string.last_orientation_landscape_key), false); + setLandscape(lastOrientationWasLandscape); + } else { + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); + } + } + }; + getContentResolver().registerContentObserver( + Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), + false, rotationObserver); } @Override @@ -237,6 +260,9 @@ public final class MainVideoPlayer extends AppCompatActivity playerState = createPlayerState(); playerImpl.destroy(); + if (rotationObserver != null) + getContentResolver().unregisterContentObserver(rotationObserver); + isInMultiWindow = false; isBackPressed = false; } @@ -246,6 +272,12 @@ public final class MainVideoPlayer extends AppCompatActivity super.attachBaseContext(AudioServiceLeakFix.preventLeakOf(newBase)); } + @Override + protected void onPause() { + playerImpl.savePlaybackState(); + super.onPause(); + } + /*////////////////////////////////////////////////////////////////////////// // State Saving //////////////////////////////////////////////////////////////////////////*/ @@ -282,14 +314,9 @@ public final class MainVideoPlayer extends AppCompatActivity if (DEBUG) Log.d(TAG, "showSystemUi() called"); if (playerImpl != null && playerImpl.queueVisible) return; - final int visibility; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - visibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE - | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN - | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; - } else { - visibility = View.STATUS_BAR_VISIBLE; - } + final int visibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE + | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN + | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { @ColorInt final int systenUiColor = @@ -358,11 +385,7 @@ public final class MainVideoPlayer extends AppCompatActivity protected void setShuffleButton(final ImageButton shuffleButton, final boolean shuffled) { final int shuffleAlpha = shuffled ? 255 : 77; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - shuffleButton.setImageAlpha(shuffleAlpha); - } else { - shuffleButton.setAlpha(shuffleAlpha); - } + shuffleButton.setImageAlpha(shuffleAlpha); } private boolean isInMultiWindow() { @@ -402,6 +425,7 @@ public final class MainVideoPlayer extends AppCompatActivity private ImageButton playPauseButton; private ImageButton playPreviousButton; private ImageButton playNextButton; + private Button closeButton; private RelativeLayout queueLayout; private ImageButton itemsListCloseButton; @@ -411,6 +435,7 @@ public final class MainVideoPlayer extends AppCompatActivity private boolean queueVisible; private ImageButton moreOptionsButton; + private ImageButton shareButton; private ImageButton toggleOrientationButton; private ImageButton switchPopupButton; private ImageButton switchBackgroundButton; @@ -442,9 +467,11 @@ public final class MainVideoPlayer extends AppCompatActivity this.playPauseButton = rootView.findViewById(R.id.playPauseButton); this.playPreviousButton = rootView.findViewById(R.id.playPreviousButton); this.playNextButton = rootView.findViewById(R.id.playNextButton); + this.closeButton = rootView.findViewById(R.id.closeButton); this.moreOptionsButton = rootView.findViewById(R.id.moreOptionsButton); this.secondaryControls = rootView.findViewById(R.id.secondaryControls); + this.shareButton = rootView.findViewById(R.id.share); this.toggleOrientationButton = rootView.findViewById(R.id.toggleOrientation); this.switchBackgroundButton = rootView.findViewById(R.id.switchBackground); this.switchPopupButton = rootView.findViewById(R.id.switchPopup); @@ -488,8 +515,10 @@ public final class MainVideoPlayer extends AppCompatActivity playPauseButton.setOnClickListener(this); playPreviousButton.setOnClickListener(this); playNextButton.setOnClickListener(this); + closeButton.setOnClickListener(this); moreOptionsButton.setOnClickListener(this); + shareButton.setOnClickListener(this); toggleOrientationButton.setOnClickListener(this); switchBackgroundButton.setOnClickListener(this); switchPopupButton.setOnClickListener(this); @@ -584,7 +613,8 @@ public final class MainVideoPlayer extends AppCompatActivity this.getPlaybackSpeed(), this.getPlaybackPitch(), this.getPlaybackSkipSilence(), - this.getPlaybackQuality() + this.getPlaybackQuality(), + false ); context.startService(intent); @@ -606,7 +636,8 @@ public final class MainVideoPlayer extends AppCompatActivity this.getPlaybackSpeed(), this.getPlaybackPitch(), this.getPlaybackSkipSilence(), - this.getPlaybackQuality() + this.getPlaybackQuality(), + false ); context.startService(intent); @@ -640,6 +671,9 @@ public final class MainVideoPlayer extends AppCompatActivity } else if (v.getId() == moreOptionsButton.getId()) { onMoreOptionsClicked(); + } else if (v.getId() == shareButton.getId()) { + onShareClicked(); + } else if (v.getId() == toggleOrientationButton.getId()) { onScreenRotationClicked(); @@ -649,6 +683,9 @@ public final class MainVideoPlayer extends AppCompatActivity } else if (v.getId() == switchBackgroundButton.getId()) { onPlayBackgroundButtonClicked(); + } else if (v.getId() == closeButton.getId()) { + onPlaybackShutdown(); + return; } if (getCurrentState() != STATE_COMPLETED) { @@ -693,6 +730,13 @@ public final class MainVideoPlayer extends AppCompatActivity showControls(DEFAULT_CONTROLS_DURATION); } + private void onShareClicked() { + // share video at the current time (youtube.com/watch?v=ID&t=SECONDS) + ShareUtils.shareUrl(MainVideoPlayer.this, + playerImpl.getVideoTitle(), + playerImpl.getVideoUrl() + "&t=" + String.valueOf(playerImpl.getPlaybackSeekBar().getProgress()/1000)); + } + private void onScreenRotationClicked() { if (DEBUG) Log.d(TAG, "onScreenRotationClicked() called"); toggleOrientation(); @@ -775,6 +819,7 @@ public final class MainVideoPlayer extends AppCompatActivity super.onBlocked(); playPauseButton.setImageResource(R.drawable.ic_pause_white); animatePlayButtons(false, 100); + animateView(closeButton, false, DEFAULT_CONTROLS_DURATION); getRootView().setKeepScreenOn(true); } @@ -790,6 +835,7 @@ public final class MainVideoPlayer extends AppCompatActivity animateView(playPauseButton, AnimationUtils.Type.SCALE_AND_ALPHA, false, 80, 0, () -> { playPauseButton.setImageResource(R.drawable.ic_pause_white); animatePlayButtons(true, 200); + animateView(closeButton, false, DEFAULT_CONTROLS_DURATION); }); getRootView().setKeepScreenOn(true); @@ -801,6 +847,7 @@ public final class MainVideoPlayer extends AppCompatActivity animateView(playPauseButton, AnimationUtils.Type.SCALE_AND_ALPHA, false, 80, 0, () -> { playPauseButton.setImageResource(R.drawable.ic_play_arrow_white); animatePlayButtons(true, 200); + animateView(closeButton, false, DEFAULT_CONTROLS_DURATION); }); showSystemUi(); @@ -820,8 +867,8 @@ public final class MainVideoPlayer extends AppCompatActivity animateView(playPauseButton, AnimationUtils.Type.SCALE_AND_ALPHA, false, 0, 0, () -> { playPauseButton.setImageResource(R.drawable.ic_replay_white); animatePlayButtons(true, DEFAULT_CONTROLS_DURATION); + animateView(closeButton, true, DEFAULT_CONTROLS_DURATION); }); - getRootView().setKeepScreenOn(false); super.onCompleted(); } @@ -856,8 +903,8 @@ public final class MainVideoPlayer extends AppCompatActivity if (DEBUG) Log.d(TAG, "hideControls() called with: delay = [" + delay + "]"); getControlsVisibilityHandler().removeCallbacksAndMessages(null); getControlsVisibilityHandler().postDelayed(() -> - animateView(getControlsRoot(), false, duration, 0, - MainVideoPlayer.this::hideSystemUi), + animateView(getControlsRoot(), false, duration, 0, + MainVideoPlayer.this::hideSystemUi), /*delayMillis=*/delay ); } @@ -1061,9 +1108,9 @@ public final class MainVideoPlayer extends AppCompatActivity final int resId = currentProgressPercent <= 0 ? R.drawable.ic_volume_off_white_72dp - : currentProgressPercent < 0.25 ? R.drawable.ic_volume_mute_white_72dp - : currentProgressPercent < 0.75 ? R.drawable.ic_volume_down_white_72dp - : R.drawable.ic_volume_up_white_72dp; + : currentProgressPercent < 0.25 ? R.drawable.ic_volume_mute_white_72dp + : currentProgressPercent < 0.75 ? R.drawable.ic_volume_down_white_72dp + : R.drawable.ic_volume_up_white_72dp; playerImpl.getVolumeImageView().setImageDrawable( AppCompatResources.getDrawable(getApplicationContext(), resId) @@ -1087,8 +1134,8 @@ public final class MainVideoPlayer extends AppCompatActivity final int resId = currentProgressPercent < 0.25 ? R.drawable.ic_brightness_low_white_72dp - : currentProgressPercent < 0.75 ? R.drawable.ic_brightness_medium_white_72dp - : R.drawable.ic_brightness_high_white_72dp; + : currentProgressPercent < 0.75 ? R.drawable.ic_brightness_medium_white_72dp + : R.drawable.ic_brightness_high_white_72dp; playerImpl.getBrightnessImageView().setImageDrawable( AppCompatResources.getDrawable(getApplicationContext(), resId) diff --git a/app/src/main/java/org/schabi/newpipe/player/PlayerServiceBinder.java b/app/src/main/java/org/schabi/newpipe/player/PlayerServiceBinder.java index 80c27be7f..ef9d92aa0 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PlayerServiceBinder.java +++ b/app/src/main/java/org/schabi/newpipe/player/PlayerServiceBinder.java @@ -1,7 +1,7 @@ package org.schabi.newpipe.player; import android.os.Binder; -import android.support.annotation.NonNull; +import androidx.annotation.NonNull; class PlayerServiceBinder extends Binder { private final BasePlayer basePlayer; diff --git a/app/src/main/java/org/schabi/newpipe/player/PlayerState.java b/app/src/main/java/org/schabi/newpipe/player/PlayerState.java index 359159809..308e8100e 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PlayerState.java +++ b/app/src/main/java/org/schabi/newpipe/player/PlayerState.java @@ -1,7 +1,7 @@ package org.schabi.newpipe.player; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import org.schabi.newpipe.player.playqueue.PlayQueue; diff --git a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java index 7578c444c..68446ed9f 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java @@ -35,9 +35,9 @@ import android.graphics.PixelFormat; import android.os.Build; import android.os.IBinder; import android.preference.PreferenceManager; -import android.support.annotation.NonNull; -import android.support.design.widget.FloatingActionButton; -import android.support.v4.app.NotificationCompat; +import androidx.annotation.NonNull; +import com.google.android.material.floatingactionbutton.FloatingActionButton; +import androidx.core.app.NotificationCompat; import android.util.DisplayMetrics; import android.util.Log; import android.view.GestureDetector; @@ -325,6 +325,7 @@ public final class PopupVideoPlayer extends Service { isPopupClosing = true; if (playerImpl != null) { + playerImpl.savePlaybackState(); if (playerImpl.getRootView() != null) { windowManager.removeView(playerImpl.getRootView()); } @@ -565,7 +566,8 @@ public final class PopupVideoPlayer extends Service { this.getPlaybackSpeed(), this.getPlaybackPitch(), this.getPlaybackSkipSilence(), - this.getPlaybackQuality() + this.getPlaybackQuality(), + false ); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); diff --git a/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java b/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java index 2ec4275fc..2207808ac 100644 --- a/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java +++ b/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java @@ -3,15 +3,14 @@ package org.schabi.newpipe.player; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; -import android.os.Build; import android.os.Bundle; import android.os.IBinder; import android.provider.Settings; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.support.v7.widget.Toolbar; -import android.support.v7.widget.helper.ItemTouchHelper; +import androidx.appcompat.app.AppCompatActivity; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; +import androidx.appcompat.widget.Toolbar; +import androidx.recyclerview.widget.ItemTouchHelper; import android.util.Log; import android.view.Menu; import android.view.MenuItem; @@ -189,7 +188,8 @@ public abstract class ServicePlayerActivity extends AppCompatActivity this.player.getPlaybackSpeed(), this.player.getPlaybackPitch(), this.player.getPlaybackSkipSilence(), - null + null, + false ).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } @@ -653,11 +653,7 @@ public abstract class ServicePlayerActivity extends AppCompatActivity } final int shuffleAlpha = shuffled ? 255 : 77; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - shuffleButton.setImageAlpha(shuffleAlpha); - } else { - shuffleButton.setAlpha(shuffleAlpha); - } + shuffleButton.setImageAlpha(shuffleAlpha); } private void onPlaybackParameterChanged(final PlaybackParameters parameters) { diff --git a/app/src/main/java/org/schabi/newpipe/player/VideoPlayer.java b/app/src/main/java/org/schabi/newpipe/player/VideoPlayer.java index d30d9b8be..360475ba2 100644 --- a/app/src/main/java/org/schabi/newpipe/player/VideoPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/VideoPlayer.java @@ -31,9 +31,9 @@ import android.graphics.Color; import android.graphics.PorterDuff; import android.os.Build; import android.os.Handler; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.content.ContextCompat; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; import android.util.Log; import android.view.Menu; import android.view.MenuItem; @@ -212,7 +212,6 @@ public abstract class VideoPlayer extends BasePlayer @Override public void initListeners() { - super.initListeners(); playbackSeekBar.setOnSeekBarChangeListener(this); playbackSpeedTextView.setOnClickListener(this); qualityTextView.setOnClickListener(this); @@ -316,7 +315,6 @@ public abstract class VideoPlayer extends BasePlayer captionPopupMenu.setOnDismissListener(this); } - private void updateStreamRelatedViews() { if (getCurrentMetadata() == null) return; @@ -510,11 +508,11 @@ public abstract class VideoPlayer extends BasePlayer // Normalize mismatching language strings final String preferredLanguage = trackSelector.getPreferredTextLanguage(); - // Build UI buildCaptionMenu(availableLanguages); if (trackSelector.getParameters().getRendererDisabled(textRenderer) || - preferredLanguage == null || !availableLanguages.contains(preferredLanguage)) { + preferredLanguage == null || (!availableLanguages.contains(preferredLanguage) + && !containsCaseInsensitive(availableLanguages, preferredLanguage))) { captionTextView.setText(R.string.caption_none); } else { captionTextView.setText(preferredLanguage); @@ -522,6 +520,15 @@ public abstract class VideoPlayer extends BasePlayer captionTextView.setVisibility(availableLanguages.isEmpty() ? View.GONE : View.VISIBLE); } + // workaround to match normalized captions like english to English or deutsch to Deutsch + private static boolean containsCaseInsensitive(List list, String toFind) { + for(String i : list){ + if(i.equalsIgnoreCase(toFind)) + return true; + } + return false; + } + /*////////////////////////////////////////////////////////////////////////// // General Player //////////////////////////////////////////////////////////////////////////*/ @@ -535,6 +542,11 @@ public abstract class VideoPlayer extends BasePlayer playbackSpeedTextView.setText(formatSpeed(getPlaybackSpeed())); super.onPrepared(playWhenReady); + + if (simpleExoPlayer.getCurrentPosition() != 0 && !isControlsVisible()) { + controlsVisibilityHandler.removeCallbacksAndMessages(null); + controlsVisibilityHandler.postDelayed(this::showControlsThenHide, DEFAULT_CONTROLS_DURATION); + } } @Override diff --git a/app/src/main/java/org/schabi/newpipe/player/helper/AudioReactor.java b/app/src/main/java/org/schabi/newpipe/player/helper/AudioReactor.java index f148aed27..8f344390a 100644 --- a/app/src/main/java/org/schabi/newpipe/player/helper/AudioReactor.java +++ b/app/src/main/java/org/schabi/newpipe/player/helper/AudioReactor.java @@ -9,16 +9,14 @@ import android.media.AudioFocusRequest; import android.media.AudioManager; import android.media.audiofx.AudioEffect; import android.os.Build; -import android.support.annotation.NonNull; +import androidx.annotation.NonNull; import android.util.Log; -import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.SimpleExoPlayer; -import com.google.android.exoplayer2.audio.AudioRendererEventListener; -import com.google.android.exoplayer2.decoder.DecoderCounters; +import com.google.android.exoplayer2.analytics.AnalyticsListener; public class AudioReactor implements AudioManager.OnAudioFocusChangeListener, - AudioRendererEventListener { + AnalyticsListener { private static final String TAG = "AudioFocusReactor"; @@ -42,7 +40,7 @@ public class AudioReactor implements AudioManager.OnAudioFocusChangeListener, this.player = player; this.context = context; this.audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); - player.addAudioDebugListener(this); + player.addAnalyticsListener(this); if (SHOULD_BUILD_FOCUS_REQUEST) { request = new AudioFocusRequest.Builder(FOCUS_GAIN_TYPE) @@ -57,7 +55,7 @@ public class AudioReactor implements AudioManager.OnAudioFocusChangeListener, public void dispose() { abandonAudioFocus(); - player.removeAudioDebugListener(this); + player.removeAnalyticsListener(this); } /*////////////////////////////////////////////////////////////////////////// @@ -164,29 +162,12 @@ public class AudioReactor implements AudioManager.OnAudioFocusChangeListener, //////////////////////////////////////////////////////////////////////////*/ @Override - public void onAudioSessionId(int i) { + public void onAudioSessionId(EventTime eventTime, int audioSessionId) { if (!PlayerHelper.isUsingDSP(context)) return; final Intent intent = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION); - intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, i); + intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, audioSessionId); intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, context.getPackageName()); context.sendBroadcast(intent); } - - @Override - public void onAudioEnabled(DecoderCounters decoderCounters) {} - - @Override - public void onAudioDecoderInitialized(String s, long l, long l1) {} - - @Override - public void onAudioInputFormatChanged(Format format) {} - - @Override - public void onAudioSinkUnderrun(int bufferSize, - long bufferSizeMs, - long elapsedSinceLastFeedMs) {} - - @Override - public void onAudioDisabled(DecoderCounters decoderCounters) {} } diff --git a/app/src/main/java/org/schabi/newpipe/player/helper/CacheFactory.java b/app/src/main/java/org/schabi/newpipe/player/helper/CacheFactory.java index b8d8dc12f..8160640cb 100644 --- a/app/src/main/java/org/schabi/newpipe/player/helper/CacheFactory.java +++ b/app/src/main/java/org/schabi/newpipe/player/helper/CacheFactory.java @@ -1,9 +1,11 @@ package org.schabi.newpipe.player.helper; import android.content.Context; -import android.support.annotation.NonNull; import android.util.Log; +import androidx.annotation.NonNull; + +import com.google.android.exoplayer2.database.ExoDatabaseProvider; import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DefaultDataSource; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; @@ -33,14 +35,14 @@ import java.io.File; public CacheFactory(@NonNull final Context context, @NonNull final String userAgent, - @NonNull final TransferListener transferListener) { + @NonNull final TransferListener transferListener) { this(context, userAgent, transferListener, PlayerHelper.getPreferredCacheSize(context), PlayerHelper.getPreferredFileSize(context)); } private CacheFactory(@NonNull final Context context, @NonNull final String userAgent, - @NonNull final TransferListener transferListener, + @NonNull final TransferListener transferListener, final long maxCacheSize, final long maxFileSize) { this.maxFileSize = maxFileSize; @@ -54,7 +56,7 @@ import java.io.File; if (cache == null) { final LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor(maxCacheSize); - cache = new SimpleCache(cacheDir, evictor); + cache = new SimpleCache(cacheDir, evictor, new ExoDatabaseProvider(context)); } } diff --git a/app/src/main/java/org/schabi/newpipe/player/helper/LoadController.java b/app/src/main/java/org/schabi/newpipe/player/helper/LoadController.java index 7670deb98..4239dd62f 100644 --- a/app/src/main/java/org/schabi/newpipe/player/helper/LoadController.java +++ b/app/src/main/java/org/schabi/newpipe/player/helper/LoadController.java @@ -2,17 +2,12 @@ package org.schabi.newpipe.player.helper; import android.content.Context; -import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.DefaultLoadControl; import com.google.android.exoplayer2.LoadControl; import com.google.android.exoplayer2.Renderer; import com.google.android.exoplayer2.source.TrackGroupArray; import com.google.android.exoplayer2.trackselection.TrackSelectionArray; import com.google.android.exoplayer2.upstream.Allocator; -import com.google.android.exoplayer2.upstream.DefaultAllocator; - -import static com.google.android.exoplayer2.DefaultLoadControl.DEFAULT_PRIORITIZE_TIME_OVER_SIZE_THRESHOLDS; -import static com.google.android.exoplayer2.DefaultLoadControl.DEFAULT_TARGET_BUFFER_BYTES; public class LoadController implements LoadControl { @@ -36,15 +31,10 @@ public class LoadController implements LoadControl { final int optimalPlaybackBufferMs) { this.initialPlaybackBufferUs = initialPlaybackBufferMs * 1000; - final DefaultAllocator allocator = new DefaultAllocator(true, - C.DEFAULT_BUFFER_SEGMENT_SIZE); - - internalLoadControl = new DefaultLoadControl(allocator, - /*minBufferMs=*/minimumPlaybackbufferMs, - /*maxBufferMs=*/optimalPlaybackBufferMs, - /*bufferForPlaybackMs=*/initialPlaybackBufferMs, - /*bufferForPlaybackAfterRebufferMs=*/initialPlaybackBufferMs, - DEFAULT_TARGET_BUFFER_BYTES, DEFAULT_PRIORITIZE_TIME_OVER_SIZE_THRESHOLDS); + DefaultLoadControl.Builder builder = new DefaultLoadControl.Builder(); + builder.setBufferDurationsMs(minimumPlaybackbufferMs, optimalPlaybackBufferMs, + initialPlaybackBufferMs, initialPlaybackBufferMs); + internalLoadControl = builder.createDefaultLoadControl(); } /*////////////////////////////////////////////////////////////////////////// diff --git a/app/src/main/java/org/schabi/newpipe/player/helper/MediaSessionManager.java b/app/src/main/java/org/schabi/newpipe/player/helper/MediaSessionManager.java index 63c0bf333..a5c703837 100644 --- a/app/src/main/java/org/schabi/newpipe/player/helper/MediaSessionManager.java +++ b/app/src/main/java/org/schabi/newpipe/player/helper/MediaSessionManager.java @@ -2,12 +2,13 @@ package org.schabi.newpipe.player.helper; import android.content.Context; import android.content.Intent; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.media.session.MediaButtonReceiver; import android.support.v4.media.session.MediaSessionCompat; import android.view.KeyEvent; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.media.session.MediaButtonReceiver; + import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector; @@ -27,10 +28,10 @@ public class MediaSessionManager { this.mediaSession = new MediaSessionCompat(context, TAG); this.mediaSession.setActive(true); - this.sessionConnector = new MediaSessionConnector(mediaSession, - new PlayQueuePlaybackController(callback)); + this.sessionConnector = new MediaSessionConnector(mediaSession); + this.sessionConnector.setControlDispatcher(new PlayQueuePlaybackController(callback)); this.sessionConnector.setQueueNavigator(new PlayQueueNavigator(mediaSession, callback)); - this.sessionConnector.setPlayer(player, null); + this.sessionConnector.setPlayer(player); } @Nullable @@ -43,7 +44,7 @@ public class MediaSessionManager { * Should be called on player destruction to prevent leakage. * */ public void dispose() { - this.sessionConnector.setPlayer(null, null); + this.sessionConnector.setPlayer(null); this.sessionConnector.setQueueNavigator(null); this.mediaSession.setActive(false); this.mediaSession.release(); diff --git a/app/src/main/java/org/schabi/newpipe/player/helper/PlaybackParameterDialog.java b/app/src/main/java/org/schabi/newpipe/player/helper/PlaybackParameterDialog.java index f49ca3330..4feed74fe 100644 --- a/app/src/main/java/org/schabi/newpipe/player/helper/PlaybackParameterDialog.java +++ b/app/src/main/java/org/schabi/newpipe/player/helper/PlaybackParameterDialog.java @@ -3,10 +3,10 @@ package org.schabi.newpipe.player.helper; import android.app.Dialog; import android.content.Context; import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.app.DialogFragment; -import android.support.v7.app.AlertDialog; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.DialogFragment; +import androidx.appcompat.app.AlertDialog; import android.util.Log; import android.view.View; import android.widget.CheckBox; diff --git a/app/src/main/java/org/schabi/newpipe/player/helper/PlayerDataSource.java b/app/src/main/java/org/schabi/newpipe/player/helper/PlayerDataSource.java index 133121269..5aa331dc5 100644 --- a/app/src/main/java/org/schabi/newpipe/player/helper/PlayerDataSource.java +++ b/app/src/main/java/org/schabi/newpipe/player/helper/PlayerDataSource.java @@ -1,9 +1,10 @@ package org.schabi.newpipe.player.helper; import android.content.Context; -import android.support.annotation.NonNull; -import com.google.android.exoplayer2.source.ExtractorMediaSource; +import androidx.annotation.NonNull; + +import com.google.android.exoplayer2.source.ProgressiveMediaSource; import com.google.android.exoplayer2.source.SingleSampleMediaSource; import com.google.android.exoplayer2.source.dash.DashMediaSource; import com.google.android.exoplayer2.source.dash.DefaultDashChunkSource; @@ -12,6 +13,7 @@ import com.google.android.exoplayer2.source.smoothstreaming.DefaultSsChunkSource import com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource; import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; +import com.google.android.exoplayer2.upstream.DefaultLoadErrorHandlingPolicy; import com.google.android.exoplayer2.upstream.TransferListener; public class PlayerDataSource { @@ -24,7 +26,7 @@ public class PlayerDataSource { public PlayerDataSource(@NonNull final Context context, @NonNull final String userAgent, - @NonNull final TransferListener transferListener) { + @NonNull final TransferListener transferListener) { cacheDataSourceFactory = new CacheFactory(context, userAgent, transferListener); cachelessDataSourceFactory = new DefaultDataSourceFactory(context, userAgent, transferListener); } @@ -32,21 +34,21 @@ public class PlayerDataSource { public SsMediaSource.Factory getLiveSsMediaSourceFactory() { return new SsMediaSource.Factory(new DefaultSsChunkSource.Factory( cachelessDataSourceFactory), cachelessDataSourceFactory) - .setMinLoadableRetryCount(MANIFEST_MINIMUM_RETRY) + .setLoadErrorHandlingPolicy(new DefaultLoadErrorHandlingPolicy(MANIFEST_MINIMUM_RETRY)) .setLivePresentationDelayMs(LIVE_STREAM_EDGE_GAP_MILLIS); } public HlsMediaSource.Factory getLiveHlsMediaSourceFactory() { return new HlsMediaSource.Factory(cachelessDataSourceFactory) .setAllowChunklessPreparation(true) - .setMinLoadableRetryCount(MANIFEST_MINIMUM_RETRY); + .setLoadErrorHandlingPolicy(new DefaultLoadErrorHandlingPolicy(MANIFEST_MINIMUM_RETRY)); } public DashMediaSource.Factory getLiveDashMediaSourceFactory() { return new DashMediaSource.Factory(new DefaultDashChunkSource.Factory( cachelessDataSourceFactory), cachelessDataSourceFactory) - .setMinLoadableRetryCount(MANIFEST_MINIMUM_RETRY) - .setLivePresentationDelayMs(LIVE_STREAM_EDGE_GAP_MILLIS); + .setLoadErrorHandlingPolicy(new DefaultLoadErrorHandlingPolicy(MANIFEST_MINIMUM_RETRY)) + .setLivePresentationDelayMs(LIVE_STREAM_EDGE_GAP_MILLIS, true); } public SsMediaSource.Factory getSsMediaSourceFactory() { @@ -63,12 +65,12 @@ public class PlayerDataSource { cacheDataSourceFactory), cacheDataSourceFactory); } - public ExtractorMediaSource.Factory getExtractorMediaSourceFactory() { - return new ExtractorMediaSource.Factory(cacheDataSourceFactory) - .setMinLoadableRetryCount(EXTRACTOR_MINIMUM_RETRY); + public ProgressiveMediaSource.Factory getExtractorMediaSourceFactory() { + return new ProgressiveMediaSource.Factory(cacheDataSourceFactory) + .setLoadErrorHandlingPolicy(new DefaultLoadErrorHandlingPolicy(EXTRACTOR_MINIMUM_RETRY)); } - public ExtractorMediaSource.Factory getExtractorMediaSourceFactory(@NonNull final String key) { + public ProgressiveMediaSource.Factory getExtractorMediaSourceFactory(@NonNull final String key) { return getExtractorMediaSourceFactory().setCustomCacheKey(key); } diff --git a/app/src/main/java/org/schabi/newpipe/player/helper/PlayerHelper.java b/app/src/main/java/org/schabi/newpipe/player/helper/PlayerHelper.java index 7752295d7..5ca02980d 100644 --- a/app/src/main/java/org/schabi/newpipe/player/helper/PlayerHelper.java +++ b/app/src/main/java/org/schabi/newpipe/player/helper/PlayerHelper.java @@ -4,9 +4,9 @@ import android.content.Context; import android.content.SharedPreferences; import android.os.Build; import android.preference.PreferenceManager; -import android.support.annotation.IntDef; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import androidx.annotation.IntDef; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import android.view.accessibility.CaptioningManager; import com.google.android.exoplayer2.SeekParameters; @@ -14,7 +14,6 @@ import com.google.android.exoplayer2.text.CaptionStyleCompat; import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection; import com.google.android.exoplayer2.trackselection.TrackSelection; import com.google.android.exoplayer2.ui.AspectRatioFrameLayout; -import com.google.android.exoplayer2.upstream.BandwidthMeter; import com.google.android.exoplayer2.util.MimeTypes; import org.schabi.newpipe.R; @@ -240,9 +239,8 @@ public class PlayerHelper { return 60000; } - public static TrackSelection.Factory getQualitySelector(@NonNull final Context context, - @NonNull final BandwidthMeter meter) { - return new AdaptiveTrackSelection.Factory(meter, + public static TrackSelection.Factory getQualitySelector(@NonNull final Context context) { + return new AdaptiveTrackSelection.Factory( /*bufferDurationRequiredForQualityIncrease=*/1000, AdaptiveTrackSelection.DEFAULT_MAX_DURATION_FOR_QUALITY_DECREASE_MS, AdaptiveTrackSelection.DEFAULT_MIN_DURATION_TO_RETAIN_AFTER_DISCARD_MS, diff --git a/app/src/main/java/org/schabi/newpipe/player/mediasession/PlayQueueNavigator.java b/app/src/main/java/org/schabi/newpipe/player/mediasession/PlayQueueNavigator.java index 3d1fd171f..ab0de08be 100644 --- a/app/src/main/java/org/schabi/newpipe/player/mediasession/PlayQueueNavigator.java +++ b/app/src/main/java/org/schabi/newpipe/player/mediasession/PlayQueueNavigator.java @@ -2,10 +2,12 @@ package org.schabi.newpipe.player.mediasession; import android.os.Bundle; import android.os.ResultReceiver; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; import android.support.v4.media.session.MediaSessionCompat; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import com.google.android.exoplayer2.ControlDispatcher; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector; import com.google.android.exoplayer2.util.Util; @@ -63,17 +65,17 @@ public class PlayQueueNavigator implements MediaSessionConnector.QueueNavigator } @Override - public void onSkipToPrevious(Player player) { + public void onSkipToPrevious(Player player, ControlDispatcher controlDispatcher) { callback.onSkipToPrevious(); } @Override - public void onSkipToQueueItem(Player player, long id) { + public void onSkipToQueueItem(Player player, ControlDispatcher controlDispatcher, long id) { callback.onSkipToIndex((int) id); } @Override - public void onSkipToNext(Player player) { + public void onSkipToNext(Player player, ControlDispatcher controlDispatcher) { callback.onSkipToNext(); } @@ -100,12 +102,7 @@ public class PlayQueueNavigator implements MediaSessionConnector.QueueNavigator } @Override - public String[] getCommands() { - return new String[0]; - } - - @Override - public void onCommand(Player player, String command, Bundle extras, ResultReceiver cb) { - + public boolean onCommand(Player player, ControlDispatcher controlDispatcher, String command, Bundle extras, ResultReceiver cb) { + return false; } } diff --git a/app/src/main/java/org/schabi/newpipe/player/mediasession/PlayQueuePlaybackController.java b/app/src/main/java/org/schabi/newpipe/player/mediasession/PlayQueuePlaybackController.java index a460a1653..b7f0638e3 100644 --- a/app/src/main/java/org/schabi/newpipe/player/mediasession/PlayQueuePlaybackController.java +++ b/app/src/main/java/org/schabi/newpipe/player/mediasession/PlayQueuePlaybackController.java @@ -1,9 +1,9 @@ package org.schabi.newpipe.player.mediasession; +import com.google.android.exoplayer2.DefaultControlDispatcher; import com.google.android.exoplayer2.Player; -import com.google.android.exoplayer2.ext.mediasession.DefaultPlaybackController; -public class PlayQueuePlaybackController extends DefaultPlaybackController { +public class PlayQueuePlaybackController extends DefaultControlDispatcher { private final MediaSessionCallback callback; public PlayQueuePlaybackController(final MediaSessionCallback callback) { @@ -12,12 +12,12 @@ public class PlayQueuePlaybackController extends DefaultPlaybackController { } @Override - public void onPlay(Player player) { - callback.onPlay(); - } - - @Override - public void onPause(Player player) { - callback.onPause(); + public boolean dispatchSetPlayWhenReady(Player player, boolean playWhenReady) { + if (playWhenReady) { + callback.onPlay(); + } else { + callback.onPause(); + } + return true; } } diff --git a/app/src/main/java/org/schabi/newpipe/player/mediasource/FailedMediaSource.java b/app/src/main/java/org/schabi/newpipe/player/mediasource/FailedMediaSource.java index 2f233c464..b99047417 100644 --- a/app/src/main/java/org/schabi/newpipe/player/mediasource/FailedMediaSource.java +++ b/app/src/main/java/org/schabi/newpipe/player/mediasource/FailedMediaSource.java @@ -1,12 +1,13 @@ package org.schabi.newpipe.player.mediasource; -import android.support.annotation.NonNull; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import android.util.Log; -import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.source.BaseMediaSource; import com.google.android.exoplayer2.source.MediaPeriod; import com.google.android.exoplayer2.upstream.Allocator; +import com.google.android.exoplayer2.upstream.TransferListener; import org.schabi.newpipe.player.playqueue.PlayQueueItem; @@ -79,7 +80,7 @@ public class FailedMediaSource extends BaseMediaSource implements ManagedMediaSo } @Override - public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator) { + public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator, long startPositionUs) { return null; } @@ -88,7 +89,7 @@ public class FailedMediaSource extends BaseMediaSource implements ManagedMediaSo @Override - protected void prepareSourceInternal(ExoPlayer player, boolean isTopLevelSource) { + protected void prepareSourceInternal(@Nullable TransferListener mediaTransferListener) { Log.e(TAG, "Loading failed source: ", error); } diff --git a/app/src/main/java/org/schabi/newpipe/player/mediasource/LoadedMediaSource.java b/app/src/main/java/org/schabi/newpipe/player/mediasource/LoadedMediaSource.java index c39b0a03d..1519103c2 100644 --- a/app/src/main/java/org/schabi/newpipe/player/mediasource/LoadedMediaSource.java +++ b/app/src/main/java/org/schabi/newpipe/player/mediasource/LoadedMediaSource.java @@ -1,13 +1,14 @@ package org.schabi.newpipe.player.mediasource; import android.os.Handler; -import android.support.annotation.NonNull; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; -import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.source.MediaPeriod; import com.google.android.exoplayer2.source.MediaSource; import com.google.android.exoplayer2.source.MediaSourceEventListener; import com.google.android.exoplayer2.upstream.Allocator; +import com.google.android.exoplayer2.upstream.TransferListener; import org.schabi.newpipe.player.playqueue.PlayQueueItem; @@ -36,9 +37,8 @@ public class LoadedMediaSource implements ManagedMediaSource { } @Override - public void prepareSource(ExoPlayer player, boolean isTopLevelSource, - SourceInfoRefreshListener listener) { - source.prepareSource(player, isTopLevelSource, listener); + public void prepareSource(SourceInfoRefreshListener listener, @Nullable TransferListener mediaTransferListener) { + source.prepareSource(listener, mediaTransferListener); } @Override @@ -47,8 +47,8 @@ public class LoadedMediaSource implements ManagedMediaSource { } @Override - public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator) { - return source.createPeriod(id, allocator); + public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator, long startPositionUs) { + return source.createPeriod(id, allocator, startPositionUs); } @Override diff --git a/app/src/main/java/org/schabi/newpipe/player/mediasource/ManagedMediaSource.java b/app/src/main/java/org/schabi/newpipe/player/mediasource/ManagedMediaSource.java index 8837e7529..b180ca9f2 100644 --- a/app/src/main/java/org/schabi/newpipe/player/mediasource/ManagedMediaSource.java +++ b/app/src/main/java/org/schabi/newpipe/player/mediasource/ManagedMediaSource.java @@ -1,6 +1,6 @@ package org.schabi.newpipe.player.mediasource; -import android.support.annotation.NonNull; +import androidx.annotation.NonNull; import com.google.android.exoplayer2.source.MediaSource; diff --git a/app/src/main/java/org/schabi/newpipe/player/mediasource/ManagedMediaSourcePlaylist.java b/app/src/main/java/org/schabi/newpipe/player/mediasource/ManagedMediaSourcePlaylist.java index 5fe107657..76f097665 100644 --- a/app/src/main/java/org/schabi/newpipe/player/mediasource/ManagedMediaSourcePlaylist.java +++ b/app/src/main/java/org/schabi/newpipe/player/mediasource/ManagedMediaSourcePlaylist.java @@ -1,7 +1,7 @@ package org.schabi.newpipe.player.mediasource; - -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import android.os.Handler; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import com.google.android.exoplayer2.source.ConcatenatingMediaSource; import com.google.android.exoplayer2.source.ShuffleOrder; @@ -86,21 +86,22 @@ public class ManagedMediaSourcePlaylist { /** * Invalidates the {@link ManagedMediaSource} at the given index by replacing it * with a {@link PlaceholderMediaSource}. - * @see #update(int, ManagedMediaSource, Runnable) + * @see #update(int, ManagedMediaSource, Handler, Runnable) * */ public synchronized void invalidate(final int index, + @Nullable final Handler handler, @Nullable final Runnable finalizingAction) { if (get(index) instanceof PlaceholderMediaSource) return; - update(index, new PlaceholderMediaSource(), finalizingAction); + update(index, new PlaceholderMediaSource(), handler, finalizingAction); } /** * Updates the {@link ManagedMediaSource} in {@link ConcatenatingMediaSource} * at the given index with a given {@link ManagedMediaSource}. - * @see #update(int, ManagedMediaSource, Runnable) + * @see #update(int, ManagedMediaSource, Handler, Runnable) * */ public synchronized void update(final int index, @NonNull final ManagedMediaSource source) { - update(index, source, /*doNothing=*/null); + update(index, source, null, /*doNothing=*/null); } /** @@ -108,9 +109,10 @@ public class ManagedMediaSourcePlaylist { * at the given index with a given {@link ManagedMediaSource}. If the index is out of bound, * then the replacement is ignored. * @see ConcatenatingMediaSource#addMediaSource - * @see ConcatenatingMediaSource#removeMediaSource(int, Runnable) + * @see ConcatenatingMediaSource#removeMediaSource(int, Handler, Runnable) * */ public synchronized void update(final int index, @NonNull final ManagedMediaSource source, + @Nullable final Handler handler, @Nullable final Runnable finalizingAction) { if (index < 0 || index >= internalSource.getSize()) return; @@ -126,6 +128,6 @@ public class ManagedMediaSourcePlaylist { // Because of the above race condition, it is thus only safe to synchronize the player // in the finalizing action AFTER the removal is complete and the timeline has changed. - internalSource.removeMediaSource(index, finalizingAction); + internalSource.removeMediaSource(index, handler, finalizingAction); } } diff --git a/app/src/main/java/org/schabi/newpipe/player/mediasource/PlaceholderMediaSource.java b/app/src/main/java/org/schabi/newpipe/player/mediasource/PlaceholderMediaSource.java index bfd734393..48179aed5 100644 --- a/app/src/main/java/org/schabi/newpipe/player/mediasource/PlaceholderMediaSource.java +++ b/app/src/main/java/org/schabi/newpipe/player/mediasource/PlaceholderMediaSource.java @@ -1,20 +1,21 @@ package org.schabi.newpipe.player.mediasource; -import android.support.annotation.NonNull; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; -import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.source.BaseMediaSource; import com.google.android.exoplayer2.source.MediaPeriod; import com.google.android.exoplayer2.upstream.Allocator; +import com.google.android.exoplayer2.upstream.TransferListener; import org.schabi.newpipe.player.playqueue.PlayQueueItem; public class PlaceholderMediaSource extends BaseMediaSource implements ManagedMediaSource { // Do nothing, so this will stall the playback @Override public void maybeThrowSourceInfoRefreshError() {} - @Override public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator) { return null; } + @Override public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator, long startPositionUs) { return null; } @Override public void releasePeriod(MediaPeriod mediaPeriod) {} - @Override protected void prepareSourceInternal(ExoPlayer player, boolean isTopLevelSource) {} + @Override protected void prepareSourceInternal(@Nullable TransferListener mediaTransferListener) {} @Override protected void releaseSourceInternal() {} @Override diff --git a/app/src/main/java/org/schabi/newpipe/player/playback/BasePlayerMediaSession.java b/app/src/main/java/org/schabi/newpipe/player/playback/BasePlayerMediaSession.java index 657fe1327..7b55629b8 100644 --- a/app/src/main/java/org/schabi/newpipe/player/playback/BasePlayerMediaSession.java +++ b/app/src/main/java/org/schabi/newpipe/player/playback/BasePlayerMediaSession.java @@ -58,8 +58,11 @@ public class BasePlayerMediaSession implements MediaSessionCallback { // set additional metadata for A2DP/AVRCP Bundle additionalMetadata = new Bundle(); + additionalMetadata.putString(MediaMetadataCompat.METADATA_KEY_TITLE, item.getTitle()); additionalMetadata.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, item.getUploader()); - additionalMetadata.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, item.getDuration()); + additionalMetadata.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, item.getDuration() * 1000); + additionalMetadata.putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, index + 1); + additionalMetadata.putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, player.getPlayQueue().size()); descriptionBuilder.setExtras(additionalMetadata); final Uri thumbnailUri = Uri.parse(item.getThumbnailUrl()); diff --git a/app/src/main/java/org/schabi/newpipe/player/playback/CustomTrackSelector.java b/app/src/main/java/org/schabi/newpipe/player/playback/CustomTrackSelector.java index efe6f3a58..d51cf630d 100644 --- a/app/src/main/java/org/schabi/newpipe/player/playback/CustomTrackSelector.java +++ b/app/src/main/java/org/schabi/newpipe/player/playback/CustomTrackSelector.java @@ -1,14 +1,17 @@ package org.schabi.newpipe.player.playback; -import android.support.annotation.NonNull; import android.text.TextUtils; +import android.util.Pair; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.ExoPlaybackException; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.source.TrackGroup; import com.google.android.exoplayer2.source.TrackGroupArray; import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; -import com.google.android.exoplayer2.trackselection.FixedTrackSelection; import com.google.android.exoplayer2.trackselection.TrackSelection; import com.google.android.exoplayer2.util.Assertions; @@ -17,10 +20,9 @@ import com.google.android.exoplayer2.util.Assertions; * is mostly a copy-paste from {@link DefaultTrackSelector}. * * This is a hack and should be removed once ExoPlayer fixes language normalization to accept - * a broader set of languages. + * a broader set of languages. * */ public class CustomTrackSelector extends DefaultTrackSelector { - private static final int WITHIN_RENDERER_CAPABILITIES_BONUS = 1000; private String preferredTextLanguage; @@ -40,23 +42,22 @@ public class CustomTrackSelector extends DefaultTrackSelector { } } - /** @see DefaultTrackSelector#formatHasLanguage(Format, String)*/ - protected static boolean formatHasLanguage(Format format, String language) { + private static boolean formatHasLanguage(Format format, String language) { return language != null && TextUtils.equals(language, format.language); } - /** @see DefaultTrackSelector#formatHasNoLanguage(Format)*/ - protected static boolean formatHasNoLanguage(Format format) { - return TextUtils.isEmpty(format.language) || formatHasLanguage(format, C.LANGUAGE_UNDETERMINED); - } - - /** @see DefaultTrackSelector#selectTextTrack(TrackGroupArray, int[][], Parameters) */ @Override - protected TrackSelection selectTextTrack(TrackGroupArray groups, int[][] formatSupport, - Parameters params) { + @Nullable + protected Pair selectTextTrack( + TrackGroupArray groups, + int[][] formatSupport, + Parameters params, + @Nullable String selectedAudioLanguage) + throws ExoPlaybackException { TrackGroup selectedGroup = null; - int selectedTrackIndex = 0; - int selectedTrackScore = 0; + int selectedTrackIndex = C.INDEX_UNSET; + int newPipeTrackScore = 0; + TextTrackScore selectedTrackScore = null; for (int groupIndex = 0; groupIndex < groups.length; groupIndex++) { TrackGroup trackGroup = groups.get(groupIndex); int[] trackFormatSupport = formatSupport[groupIndex]; @@ -64,41 +65,17 @@ public class CustomTrackSelector extends DefaultTrackSelector { if (isSupported(trackFormatSupport[trackIndex], params.exceedRendererCapabilitiesIfNecessary)) { Format format = trackGroup.getFormat(trackIndex); - int maskedSelectionFlags = - format.selectionFlags & ~params.disabledTextTrackSelectionFlags; - boolean isDefault = (maskedSelectionFlags & C.SELECTION_FLAG_DEFAULT) != 0; - boolean isForced = (maskedSelectionFlags & C.SELECTION_FLAG_FORCED) != 0; - int trackScore; - boolean preferredLanguageFound = formatHasLanguage(format, preferredTextLanguage); - if (preferredLanguageFound - || (params.selectUndeterminedTextLanguage && formatHasNoLanguage(format))) { - if (isDefault) { - trackScore = 8; - } else if (!isForced) { - // Prefer non-forced to forced if a preferred text language has been specified. Where - // both are provided the non-forced track will usually contain the forced subtitles as - // a subset. - trackScore = 6; - } else { - trackScore = 4; - } - trackScore += preferredLanguageFound ? 1 : 0; - } else if (isDefault) { - trackScore = 3; - } else if (isForced) { - if (formatHasLanguage(format, params.preferredAudioLanguage)) { - trackScore = 2; - } else { - trackScore = 1; - } - } else { - // Track should not be selected. - continue; - } - if (isSupported(trackFormatSupport[trackIndex], false)) { - trackScore += WITHIN_RENDERER_CAPABILITIES_BONUS; - } - if (trackScore > selectedTrackScore) { + TextTrackScore trackScore = + new TextTrackScore( + format, params, trackFormatSupport[trackIndex], selectedAudioLanguage); + if (formatHasLanguage(format, preferredTextLanguage)) { + selectedGroup = trackGroup; + selectedTrackIndex = trackIndex; + selectedTrackScore = trackScore; + // found user selected match (perfect!) + break; + } else if (trackScore.isWithinConstraints + && (selectedTrackScore == null || trackScore.compareTo(selectedTrackScore) > 0)) { selectedGroup = trackGroup; selectedTrackIndex = trackIndex; selectedTrackScore = trackScore; @@ -106,7 +83,10 @@ public class CustomTrackSelector extends DefaultTrackSelector { } } } - return selectedGroup == null ? null - : new FixedTrackSelection(selectedGroup, selectedTrackIndex); + return selectedGroup == null + ? null + : Pair.create( + new TrackSelection.Definition(selectedGroup, selectedTrackIndex), + Assertions.checkNotNull(selectedTrackScore)); } } diff --git a/app/src/main/java/org/schabi/newpipe/player/playback/MediaSourceManager.java b/app/src/main/java/org/schabi/newpipe/player/playback/MediaSourceManager.java index 3c5642d51..85c852f57 100644 --- a/app/src/main/java/org/schabi/newpipe/player/playback/MediaSourceManager.java +++ b/app/src/main/java/org/schabi/newpipe/player/playback/MediaSourceManager.java @@ -1,8 +1,8 @@ package org.schabi.newpipe.player.playback; - -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.util.ArraySet; +import android.os.Handler; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.collection.ArraySet; import android.util.Log; import com.google.android.exoplayer2.source.MediaSource; @@ -103,6 +103,8 @@ public class MediaSourceManager { @NonNull private ManagedMediaSourcePlaylist playlist; + private Handler removeMediaSourceHandler = new Handler(); + public MediaSourceManager(@NonNull final PlaybackListener listener, @NonNull final PlayQueue playQueue) { this(listener, playQueue, /*loadDebounceMillis=*/400L, @@ -156,7 +158,7 @@ public class MediaSourceManager { * Dispose the manager and releases all message buses and loaders. * */ public void dispose() { - if (DEBUG) Log.d(TAG, "dispose() called."); + if (DEBUG) Log.d(TAG, "close() called."); debouncedSignal.onComplete(); debouncedLoader.dispose(); @@ -395,7 +397,7 @@ public class MediaSourceManager { if (isCorrectionNeeded(item)) { if (DEBUG) Log.d(TAG, "MediaSource - Updating index=[" + itemIndex + "] with " + "title=[" + item.getTitle() + "] at url=[" + item.getUrl() + "]"); - playlist.update(itemIndex, mediaSource, this::maybeSynchronizePlayer); + playlist.update(itemIndex, mediaSource, removeMediaSourceHandler, this::maybeSynchronizePlayer); } } @@ -441,7 +443,7 @@ public class MediaSourceManager { if (DEBUG) Log.d(TAG, "MediaSource - Reloading currently playing, " + "index=[" + currentIndex + "], item=[" + currentItem.getTitle() + "]"); - playlist.invalidate(currentIndex, this::loadImmediate); + playlist.invalidate(currentIndex, removeMediaSourceHandler, this::loadImmediate); } private void maybeClearLoaders() { diff --git a/app/src/main/java/org/schabi/newpipe/player/playback/PlaybackListener.java b/app/src/main/java/org/schabi/newpipe/player/playback/PlaybackListener.java index 4e79c6d75..9682ea15e 100644 --- a/app/src/main/java/org/schabi/newpipe/player/playback/PlaybackListener.java +++ b/app/src/main/java/org/schabi/newpipe/player/playback/PlaybackListener.java @@ -1,7 +1,7 @@ package org.schabi.newpipe.player.playback; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import com.google.android.exoplayer2.source.MediaSource; diff --git a/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueue.java b/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueue.java index 2a7c9f127..fcb1e2819 100644 --- a/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueue.java +++ b/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueue.java @@ -1,7 +1,7 @@ package org.schabi.newpipe.player.playqueue; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import android.util.Log; import org.reactivestreams.Subscriber; diff --git a/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueueAdapter.java b/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueueAdapter.java index eb2d678e0..b74736c49 100644 --- a/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueueAdapter.java +++ b/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueueAdapter.java @@ -1,7 +1,7 @@ package org.schabi.newpipe.player.playqueue; import android.content.Context; -import android.support.v7.widget.RecyclerView; +import androidx.recyclerview.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; diff --git a/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueueItem.java b/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueueItem.java index bd0218454..309f22ad5 100644 --- a/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueueItem.java +++ b/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueueItem.java @@ -1,7 +1,7 @@ package org.schabi.newpipe.player.playqueue; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.extractor.stream.StreamInfoItem; diff --git a/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueueItemHolder.java b/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueueItemHolder.java index effb9aae9..7ad34b91e 100644 --- a/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueueItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueueItemHolder.java @@ -1,6 +1,6 @@ package org.schabi.newpipe.player.playqueue; -import android.support.v7.widget.RecyclerView; +import androidx.recyclerview.widget.RecyclerView; import android.view.View; import android.widget.ImageView; import android.widget.TextView; diff --git a/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueueItemTouchCallback.java b/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueueItemTouchCallback.java index 26be83b98..38e8e092a 100644 --- a/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueueItemTouchCallback.java +++ b/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueueItemTouchCallback.java @@ -1,7 +1,7 @@ package org.schabi.newpipe.player.playqueue; -import android.support.v7.widget.RecyclerView; -import android.support.v7.widget.helper.ItemTouchHelper; +import androidx.recyclerview.widget.RecyclerView; +import androidx.recyclerview.widget.ItemTouchHelper; public abstract class PlayQueueItemTouchCallback extends ItemTouchHelper.SimpleCallback { private static final int MINIMUM_INITIAL_DRAG_VELOCITY = 10; diff --git a/app/src/main/java/org/schabi/newpipe/player/playqueue/SinglePlayQueue.java b/app/src/main/java/org/schabi/newpipe/player/playqueue/SinglePlayQueue.java index 5993481e2..40d1a11e7 100644 --- a/app/src/main/java/org/schabi/newpipe/player/playqueue/SinglePlayQueue.java +++ b/app/src/main/java/org/schabi/newpipe/player/playqueue/SinglePlayQueue.java @@ -16,6 +16,11 @@ public final class SinglePlayQueue extends PlayQueue { super(0, Collections.singletonList(new PlayQueueItem(info))); } + public SinglePlayQueue(final StreamInfo info, final long startPosition) { + super(0, Collections.singletonList(new PlayQueueItem(info))); + getItem().setRecoveryPosition(startPosition); + } + public SinglePlayQueue(final List items, final int index) { super(index, playQueueItemsOf(items)); } diff --git a/app/src/main/java/org/schabi/newpipe/player/resolver/AudioPlaybackResolver.java b/app/src/main/java/org/schabi/newpipe/player/resolver/AudioPlaybackResolver.java index 6bb556850..7e9199040 100644 --- a/app/src/main/java/org/schabi/newpipe/player/resolver/AudioPlaybackResolver.java +++ b/app/src/main/java/org/schabi/newpipe/player/resolver/AudioPlaybackResolver.java @@ -1,8 +1,8 @@ package org.schabi.newpipe.player.resolver; import android.content.Context; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import com.google.android.exoplayer2.source.MediaSource; diff --git a/app/src/main/java/org/schabi/newpipe/player/resolver/MediaSourceTag.java b/app/src/main/java/org/schabi/newpipe/player/resolver/MediaSourceTag.java index bbe5d33ca..d8c0c89b7 100644 --- a/app/src/main/java/org/schabi/newpipe/player/resolver/MediaSourceTag.java +++ b/app/src/main/java/org/schabi/newpipe/player/resolver/MediaSourceTag.java @@ -1,7 +1,7 @@ package org.schabi.newpipe.player.resolver; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.extractor.stream.VideoStream; diff --git a/app/src/main/java/org/schabi/newpipe/player/resolver/PlaybackResolver.java b/app/src/main/java/org/schabi/newpipe/player/resolver/PlaybackResolver.java index 1da3ec211..ef28f71ee 100644 --- a/app/src/main/java/org/schabi/newpipe/player/resolver/PlaybackResolver.java +++ b/app/src/main/java/org/schabi/newpipe/player/resolver/PlaybackResolver.java @@ -1,8 +1,8 @@ package org.schabi.newpipe.player.resolver; import android.net.Uri; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import android.text.TextUtils; import com.google.android.exoplayer2.C; diff --git a/app/src/main/java/org/schabi/newpipe/player/resolver/Resolver.java b/app/src/main/java/org/schabi/newpipe/player/resolver/Resolver.java index 4bd795574..d6af20ae2 100644 --- a/app/src/main/java/org/schabi/newpipe/player/resolver/Resolver.java +++ b/app/src/main/java/org/schabi/newpipe/player/resolver/Resolver.java @@ -1,7 +1,7 @@ package org.schabi.newpipe.player.resolver; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; public interface Resolver { @Nullable Product resolve(@NonNull Source source); diff --git a/app/src/main/java/org/schabi/newpipe/player/resolver/VideoPlaybackResolver.java b/app/src/main/java/org/schabi/newpipe/player/resolver/VideoPlaybackResolver.java index 7abebc49e..c503fe596 100644 --- a/app/src/main/java/org/schabi/newpipe/player/resolver/VideoPlaybackResolver.java +++ b/app/src/main/java/org/schabi/newpipe/player/resolver/VideoPlaybackResolver.java @@ -2,8 +2,8 @@ package org.schabi.newpipe.player.resolver; import android.content.Context; import android.net.Uri; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.source.MediaSource; @@ -13,7 +13,6 @@ import org.schabi.newpipe.extractor.MediaFormat; import org.schabi.newpipe.extractor.stream.SubtitlesStream; import org.schabi.newpipe.extractor.stream.AudioStream; import org.schabi.newpipe.extractor.stream.StreamInfo; -import org.schabi.newpipe.extractor.stream.SubtitlesStream; import org.schabi.newpipe.extractor.stream.VideoStream; import org.schabi.newpipe.player.helper.PlayerDataSource; import org.schabi.newpipe.player.helper.PlayerHelper; diff --git a/app/src/main/java/org/schabi/newpipe/report/AcraReportSender.java b/app/src/main/java/org/schabi/newpipe/report/AcraReportSender.java index a6a81474f..d8506fe6e 100644 --- a/app/src/main/java/org/schabi/newpipe/report/AcraReportSender.java +++ b/app/src/main/java/org/schabi/newpipe/report/AcraReportSender.java @@ -1,7 +1,7 @@ package org.schabi.newpipe.report; import android.content.Context; -import android.support.annotation.NonNull; +import androidx.annotation.NonNull; import org.acra.collector.CrashReportData; import org.acra.sender.ReportSender; diff --git a/app/src/main/java/org/schabi/newpipe/report/AcraReportSenderFactory.java b/app/src/main/java/org/schabi/newpipe/report/AcraReportSenderFactory.java index 89f0ec614..94b2e84a5 100644 --- a/app/src/main/java/org/schabi/newpipe/report/AcraReportSenderFactory.java +++ b/app/src/main/java/org/schabi/newpipe/report/AcraReportSenderFactory.java @@ -1,7 +1,7 @@ package org.schabi.newpipe.report; import android.content.Context; -import android.support.annotation.NonNull; +import androidx.annotation.NonNull; import org.acra.config.ACRAConfiguration; import org.acra.sender.ReportSender; diff --git a/app/src/main/java/org/schabi/newpipe/report/ErrorActivity.java b/app/src/main/java/org/schabi/newpipe/report/ErrorActivity.java index f852e0134..e7a6319e3 100644 --- a/app/src/main/java/org/schabi/newpipe/report/ErrorActivity.java +++ b/app/src/main/java/org/schabi/newpipe/report/ErrorActivity.java @@ -12,13 +12,13 @@ import android.os.Handler; import android.os.Parcel; import android.os.Parcelable; import android.preference.PreferenceManager; -import android.support.annotation.Nullable; -import android.support.annotation.StringRes; -import android.support.design.widget.Snackbar; -import android.support.v4.app.NavUtils; -import android.support.v7.app.ActionBar; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; +import androidx.annotation.Nullable; +import androidx.annotation.StringRes; +import com.google.android.material.snackbar.Snackbar; +import androidx.core.app.NavUtils; +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; diff --git a/app/src/main/java/org/schabi/newpipe/report/UserAction.java b/app/src/main/java/org/schabi/newpipe/report/UserAction.java index 2b2369ad3..2cca9305a 100644 --- a/app/src/main/java/org/schabi/newpipe/report/UserAction.java +++ b/app/src/main/java/org/schabi/newpipe/report/UserAction.java @@ -17,7 +17,9 @@ public enum UserAction { REQUESTED_KIOSK("requested kiosk"), REQUESTED_COMMENTS("requested comments"), DELETE_FROM_HISTORY("delete from history"), - PLAY_STREAM("Play stream"); + PLAY_STREAM("Play stream"), + DOWNLOAD_POSTPROCESSING("download post-processing"), + DOWNLOAD_FAILED("download failed"); private final String message; diff --git a/app/src/main/java/org/schabi/newpipe/settings/AppearanceSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/AppearanceSettingsFragment.java index 821636ee5..ce22b84e9 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/AppearanceSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/AppearanceSettingsFragment.java @@ -4,8 +4,8 @@ import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.provider.Settings; -import android.support.annotation.Nullable; -import android.support.v7.preference.Preference; +import androidx.annotation.Nullable; +import androidx.preference.Preference; import org.schabi.newpipe.R; import org.schabi.newpipe.util.Constants; diff --git a/app/src/main/java/org/schabi/newpipe/settings/BasePreferenceFragment.java b/app/src/main/java/org/schabi/newpipe/settings/BasePreferenceFragment.java index e4fae3e1f..056e9942a 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/BasePreferenceFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/BasePreferenceFragment.java @@ -3,10 +3,10 @@ package org.schabi.newpipe.settings; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; -import android.support.annotation.Nullable; -import android.support.v7.app.ActionBar; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.preference.PreferenceFragmentCompat; +import androidx.annotation.Nullable; +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AppCompatActivity; +import androidx.preference.PreferenceFragmentCompat; import android.view.View; import org.schabi.newpipe.MainActivity; diff --git a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java index 801a55fd7..2148fb9ae 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java @@ -7,10 +7,10 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v7.preference.EditTextPreference; -import android.support.v7.preference.Preference; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.preference.EditTextPreference; +import androidx.preference.Preference; import android.util.Log; import android.widget.Toast; @@ -329,7 +329,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment { else if (v instanceof String) prefEdit.putString(key, ((String) v)); } - prefEdit.apply(); + prefEdit.commit(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { diff --git a/app/src/main/java/org/schabi/newpipe/settings/DownloadSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/DownloadSettingsFragment.java index 8214d7b4b..8becc79a8 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/DownloadSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/DownloadSettingsFragment.java @@ -1,30 +1,76 @@ package org.schabi.newpipe.settings; import android.app.Activity; +import android.app.AlertDialog; +import android.content.ContentResolver; +import android.content.Context; import android.content.Intent; +import android.net.Uri; +import android.os.Build; import android.os.Bundle; -import android.support.annotation.Nullable; -import android.support.v7.preference.Preference; +import androidx.annotation.Nullable; +import androidx.annotation.StringRes; +import androidx.preference.Preference; import android.util.Log; +import android.widget.Toast; import com.nononsenseapps.filepicker.Utils; import org.schabi.newpipe.R; import org.schabi.newpipe.util.FilePickerActivityHelper; -public class DownloadSettingsFragment extends BasePreferenceFragment { - private static final int REQUEST_DOWNLOAD_PATH = 0x1235; - private static final int REQUEST_DOWNLOAD_AUDIO_PATH = 0x1236; +import java.io.File; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; - private String DOWNLOAD_PATH_PREFERENCE; +import us.shandian.giga.io.StoredDirectoryHelper; + +public class DownloadSettingsFragment extends BasePreferenceFragment { + private static final int REQUEST_DOWNLOAD_VIDEO_PATH = 0x1235; + private static final int REQUEST_DOWNLOAD_AUDIO_PATH = 0x1236; + public static final boolean IGNORE_RELEASE_ON_OLD_PATH = true; + + private String DOWNLOAD_PATH_VIDEO_PREFERENCE; private String DOWNLOAD_PATH_AUDIO_PREFERENCE; + private String STORAGE_USE_SAF_PREFERENCE; + + private Preference prefPathVideo; + private Preference prefPathAudio; + private Preference prefStorageAsk; + + private Context ctx; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - initKeys(); + DOWNLOAD_PATH_VIDEO_PREFERENCE = getString(R.string.download_path_video_key); + DOWNLOAD_PATH_AUDIO_PREFERENCE = getString(R.string.download_path_audio_key); + STORAGE_USE_SAF_PREFERENCE = getString(R.string.storage_use_saf); + final String downloadStorageAsk = getString(R.string.downloads_storage_ask); + + prefPathVideo = findPreference(DOWNLOAD_PATH_VIDEO_PREFERENCE); + prefPathAudio = findPreference(DOWNLOAD_PATH_AUDIO_PREFERENCE); + prefStorageAsk = findPreference(downloadStorageAsk); + updatePreferencesSummary(); + updatePathPickers(!defaultPreferences.getBoolean(downloadStorageAsk, false)); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + prefStorageAsk.setSummary(R.string.downloads_storage_ask_summary); + } + + if (hasInvalidPath(DOWNLOAD_PATH_VIDEO_PREFERENCE) || hasInvalidPath(DOWNLOAD_PATH_AUDIO_PREFERENCE)) { + updatePreferencesSummary(); + } + + prefStorageAsk.setOnPreferenceChangeListener((preference, value) -> { + updatePathPickers(!(boolean) value); + return true; + }); } @Override @@ -32,52 +78,187 @@ public class DownloadSettingsFragment extends BasePreferenceFragment { addPreferencesFromResource(R.xml.download_settings); } - private void initKeys() { - DOWNLOAD_PATH_PREFERENCE = getString(R.string.download_path_key); - DOWNLOAD_PATH_AUDIO_PREFERENCE = getString(R.string.download_path_audio_key); + @Override + public void onAttach(Context context) { + super.onAttach(context); + ctx = context; + } + + @Override + public void onDetach() { + super.onDetach(); + ctx = null; + prefStorageAsk.setOnPreferenceChangeListener(null); } private void updatePreferencesSummary() { - findPreference(DOWNLOAD_PATH_PREFERENCE).setSummary(defaultPreferences.getString(DOWNLOAD_PATH_PREFERENCE, getString(R.string.download_path_summary))); - findPreference(DOWNLOAD_PATH_AUDIO_PREFERENCE).setSummary(defaultPreferences.getString(DOWNLOAD_PATH_AUDIO_PREFERENCE, getString(R.string.download_path_audio_summary))); + showPathInSummary(DOWNLOAD_PATH_VIDEO_PREFERENCE, R.string.download_path_summary, prefPathVideo); + showPathInSummary(DOWNLOAD_PATH_AUDIO_PREFERENCE, R.string.download_path_audio_summary, prefPathAudio); + } + + private void showPathInSummary(String prefKey, @StringRes int defaultString, Preference target) { + String rawUri = defaultPreferences.getString(prefKey, null); + if (rawUri == null || rawUri.isEmpty()) { + target.setSummary(getString(defaultString)); + return; + } + + if (rawUri.charAt(0) == File.separatorChar) { + target.setSummary(rawUri); + return; + } + if (rawUri.startsWith(ContentResolver.SCHEME_FILE)) { + target.setSummary(new File(URI.create(rawUri)).getPath()); + return; + } + + try { + rawUri = URLDecoder.decode(rawUri, StandardCharsets.UTF_8.name()); + } catch (UnsupportedEncodingException e) { + // nothing to do + } + + target.setSummary(rawUri); + } + + private boolean isFileUri(String path) { + return path.charAt(0) == File.separatorChar || path.startsWith(ContentResolver.SCHEME_FILE); + } + + private boolean hasInvalidPath(String prefKey) { + String value = defaultPreferences.getString(prefKey, null); + return value == null || value.isEmpty(); + } + + private void updatePathPickers(boolean enabled) { + prefPathVideo.setEnabled(enabled); + prefPathAudio.setEnabled(enabled); + } + + // FIXME: after releasing the old path, all downloads created on the folder becomes inaccessible + private void forgetSAFTree(Context ctx, String oldPath) { + if (IGNORE_RELEASE_ON_OLD_PATH) { + return; + } + + if (oldPath == null || oldPath.isEmpty() || isFileUri(oldPath)) return; + + try { + Uri uri = Uri.parse(oldPath); + + ctx.getContentResolver().releasePersistableUriPermission(uri, StoredDirectoryHelper.PERMISSION_FLAGS); + ctx.revokeUriPermission(uri, StoredDirectoryHelper.PERMISSION_FLAGS); + + Log.i(TAG, "Revoke old path permissions success on " + oldPath); + } catch (Exception err) { + Log.e(TAG, "Error revoking old path permissions on " + oldPath, err); + } + } + + private void showMessageDialog(@StringRes int title, @StringRes int message) { + AlertDialog.Builder msg = new AlertDialog.Builder(ctx); + msg.setTitle(title); + msg.setMessage(message); + msg.setPositiveButton(android.R.string.ok, null); + msg.show(); } @Override public boolean onPreferenceTreeClick(Preference preference) { if (DEBUG) { - Log.d(TAG, "onPreferenceTreeClick() called with: preference = [" + preference + "]"); + Log.d(TAG, "onPreferenceTreeClick() called with: preference = [" + preference + "]"); } - if (preference.getKey().equals(DOWNLOAD_PATH_PREFERENCE) - || preference.getKey().equals(DOWNLOAD_PATH_AUDIO_PREFERENCE)) { - Intent i = new Intent(getActivity(), FilePickerActivityHelper.class) + String key = preference.getKey(); + int request; + + if (key.equals(STORAGE_USE_SAF_PREFERENCE)) { + Toast.makeText(getContext(), R.string.download_choose_new_path, Toast.LENGTH_LONG).show(); + return true; + } else if (key.equals(DOWNLOAD_PATH_VIDEO_PREFERENCE)) { + request = REQUEST_DOWNLOAD_VIDEO_PATH; + } else if (key.equals(DOWNLOAD_PATH_AUDIO_PREFERENCE)) { + request = REQUEST_DOWNLOAD_AUDIO_PATH; + } else { + return super.onPreferenceTreeClick(preference); + } + + Intent i; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && NewPipeSettings.useStorageAccessFramework(ctx)) { + i = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) + .putExtra("android.content.extra.SHOW_ADVANCED", true) + .addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION | StoredDirectoryHelper.PERMISSION_FLAGS); + } else { + i = new Intent(getActivity(), FilePickerActivityHelper.class) .putExtra(FilePickerActivityHelper.EXTRA_ALLOW_MULTIPLE, false) .putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, true) .putExtra(FilePickerActivityHelper.EXTRA_MODE, FilePickerActivityHelper.MODE_DIR); - if (preference.getKey().equals(DOWNLOAD_PATH_PREFERENCE)) { - startActivityForResult(i, REQUEST_DOWNLOAD_PATH); - } else if (preference.getKey().equals(DOWNLOAD_PATH_AUDIO_PREFERENCE)) { - startActivityForResult(i, REQUEST_DOWNLOAD_AUDIO_PATH); - } } - return super.onPreferenceTreeClick(preference); + startActivityForResult(i, request); + + return true; } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (DEBUG) { - Log.d(TAG, "onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]"); + Log.d(TAG, "onActivityResult() called with: requestCode = [" + requestCode + "], " + + "resultCode = [" + resultCode + "], data = [" + data + "]" + ); } - if ((requestCode == REQUEST_DOWNLOAD_PATH || requestCode == REQUEST_DOWNLOAD_AUDIO_PATH) - && resultCode == Activity.RESULT_OK && data.getData() != null) { - String key = getString(requestCode == REQUEST_DOWNLOAD_PATH ? R.string.download_path_key : R.string.download_path_audio_key); - String path = Utils.getFileForUri(data.getData()).getAbsolutePath(); + if (resultCode != Activity.RESULT_OK) return; - defaultPreferences.edit().putString(key, path).apply(); - updatePreferencesSummary(); + String key; + if (requestCode == REQUEST_DOWNLOAD_VIDEO_PATH) + key = DOWNLOAD_PATH_VIDEO_PREFERENCE; + else if (requestCode == REQUEST_DOWNLOAD_AUDIO_PATH) + key = DOWNLOAD_PATH_AUDIO_PREFERENCE; + else + return; + + Uri uri = data.getData(); + if (uri == null) { + showMessageDialog(R.string.general_error, R.string.invalid_directory); + return; } + + + // revoke permissions on the old save path (required for SAF only) + final Context ctx = getContext(); + if (ctx == null) throw new NullPointerException("getContext()"); + + forgetSAFTree(ctx, defaultPreferences.getString(key, "")); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !FilePickerActivityHelper.isOwnFileUri(ctx, uri)) { + // steps to acquire the selected path: + // 1. acquire permissions on the new save path + // 2. save the new path, if step(2) was successful + try { + ctx.grantUriPermission(ctx.getPackageName(), uri, StoredDirectoryHelper.PERMISSION_FLAGS); + + StoredDirectoryHelper mainStorage = new StoredDirectoryHelper(ctx, uri, null); + Log.i(TAG, "Acquiring tree success from " + uri.toString()); + + if (!mainStorage.canWrite()) + throw new IOException("No write permissions on " + uri.toString()); + } catch (IOException err) { + Log.e(TAG, "Error acquiring tree from " + uri.toString(), err); + showMessageDialog(R.string.general_error, R.string.no_available_dir); + return; + } + } else { + File target = Utils.getFileForUri(uri); + if (!target.canWrite()) { + showMessageDialog(R.string.download_to_sdcard_error_title, R.string.download_to_sdcard_error_message); + return; + } + uri = Uri.fromFile(target); + } + + defaultPreferences.edit().putString(key, uri.toString()).apply(); + updatePreferencesSummary(); } } diff --git a/app/src/main/java/org/schabi/newpipe/settings/HistorySettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/HistorySettingsFragment.java index d28c3179c..cdfbf54a7 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/HistorySettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/HistorySettingsFragment.java @@ -1,9 +1,9 @@ package org.schabi.newpipe.settings; import android.os.Bundle; -import android.support.annotation.Nullable; -import android.support.v7.app.AlertDialog; -import android.support.v7.preference.Preference; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; +import androidx.preference.Preference; import android.widget.Toast; import org.schabi.newpipe.R; @@ -18,7 +18,8 @@ import io.reactivex.disposables.Disposable; public class HistorySettingsFragment extends BasePreferenceFragment { private String cacheWipeKey; - private String viewsHistroyClearKey; + private String viewsHistoryClearKey; + private String playbackStatesClearKey; private String searchHistoryClearKey; private HistoryRecordManager recordManager; private CompositeDisposable disposables; @@ -27,7 +28,8 @@ public class HistorySettingsFragment extends BasePreferenceFragment { public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); cacheWipeKey = getString(R.string.metadata_cache_wipe_key); - viewsHistroyClearKey = getString(R.string.clear_views_history_key); + viewsHistoryClearKey = getString(R.string.clear_views_history_key); + playbackStatesClearKey = getString(R.string.clear_playback_states_key); searchHistoryClearKey = getString(R.string.clear_search_history_key); recordManager = new HistoryRecordManager(getActivity()); disposables = new CompositeDisposable(); @@ -46,16 +48,31 @@ public class HistorySettingsFragment extends BasePreferenceFragment { Toast.LENGTH_SHORT).show(); } - if (preference.getKey().equals(viewsHistroyClearKey)) { + if (preference.getKey().equals(viewsHistoryClearKey)) { new AlertDialog.Builder(getActivity()) .setTitle(R.string.delete_view_history_alert) .setNegativeButton(R.string.cancel, ((dialog, which) -> dialog.dismiss())) .setPositiveButton(R.string.delete, ((dialog, which) -> { + final Disposable onDeletePlaybackStates = recordManager.deleteCompelteStreamStateHistory() + .observeOn(AndroidSchedulers.mainThread()) + .subscribe( + howManyDeleted -> Toast.makeText(getActivity(), + R.string.watch_history_states_deleted, + Toast.LENGTH_SHORT).show(), + throwable -> ErrorActivity.reportError(getContext(), + throwable, + SettingsActivity.class, null, + ErrorActivity.ErrorInfo.make( + UserAction.DELETE_FROM_HISTORY, + "none", + "Delete playback states", + R.string.general_error))); + final Disposable onDelete = recordManager.deleteWholeStreamHistory() .observeOn(AndroidSchedulers.mainThread()) .subscribe( howManyDeleted -> Toast.makeText(getActivity(), - R.string.view_history_deleted, + R.string.watch_history_deleted, Toast.LENGTH_SHORT).show(), throwable -> ErrorActivity.reportError(getContext(), throwable, @@ -78,6 +95,7 @@ public class HistorySettingsFragment extends BasePreferenceFragment { "none", "Delete search history", R.string.general_error))); + disposables.add(onDeletePlaybackStates); disposables.add(onClearOrphans); disposables.add(onDelete); })) @@ -85,12 +103,39 @@ public class HistorySettingsFragment extends BasePreferenceFragment { .show(); } + if (preference.getKey().equals(playbackStatesClearKey)) { + new AlertDialog.Builder(getActivity()) + .setTitle(R.string.delete_playback_states_alert) + .setNegativeButton(R.string.cancel, ((dialog, which) -> dialog.dismiss())) + .setPositiveButton(R.string.delete, ((dialog, which) -> { + + final Disposable onDeletePlaybackStates = recordManager.deleteCompelteStreamStateHistory() + .observeOn(AndroidSchedulers.mainThread()) + .subscribe( + howManyDeleted -> Toast.makeText(getActivity(), + R.string.watch_history_states_deleted, + Toast.LENGTH_SHORT).show(), + throwable -> ErrorActivity.reportError(getContext(), + throwable, + SettingsActivity.class, null, + ErrorActivity.ErrorInfo.make( + UserAction.DELETE_FROM_HISTORY, + "none", + "Delete playback states", + R.string.general_error))); + + disposables.add(onDeletePlaybackStates); + })) + .create() + .show(); + } + if (preference.getKey().equals(searchHistoryClearKey)) { new AlertDialog.Builder(getActivity()) .setTitle(R.string.delete_search_history_alert) .setNegativeButton(R.string.cancel, ((dialog, which) -> dialog.dismiss())) .setPositiveButton(R.string.delete, ((dialog, which) -> { - final Disposable onDelete = recordManager.deleteWholeSearchHistory() + final Disposable onDelete = recordManager.deleteCompleteSearchHistory() .observeOn(AndroidSchedulers.mainThread()) .subscribe( howManyDeleted -> Toast.makeText(getActivity(), diff --git a/app/src/main/java/org/schabi/newpipe/settings/MainSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/MainSettingsFragment.java index 43270926e..70460509d 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/MainSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/MainSettingsFragment.java @@ -1,7 +1,7 @@ package org.schabi.newpipe.settings; import android.os.Bundle; -import android.support.v7.preference.Preference; +import androidx.preference.Preference; import org.schabi.newpipe.BuildConfig; import org.schabi.newpipe.CheckForNewAppVersionTask; diff --git a/app/src/main/java/org/schabi/newpipe/settings/NewPipeSettings.java b/app/src/main/java/org/schabi/newpipe/settings/NewPipeSettings.java index 2a0e2645b..e0003ccaa 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/NewPipeSettings.java +++ b/app/src/main/java/org/schabi/newpipe/settings/NewPipeSettings.java @@ -24,7 +24,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.os.Environment; import android.preference.PreferenceManager; -import android.support.annotation.NonNull; +import androidx.annotation.NonNull; import org.schabi.newpipe.R; @@ -70,57 +70,39 @@ public class NewPipeSettings { getAudioDownloadFolder(context); } - public static File getVideoDownloadFolder(Context context) { - return getDir(context, R.string.download_path_key, Environment.DIRECTORY_MOVIES); + private static void getVideoDownloadFolder(Context context) { + getDir(context, R.string.download_path_video_key, Environment.DIRECTORY_MOVIES); } - public static String getVideoDownloadPath(Context context) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - final String key = context.getString(R.string.download_path_key); - return prefs.getString(key, Environment.DIRECTORY_MOVIES); + private static void getAudioDownloadFolder(Context context) { + getDir(context, R.string.download_path_audio_key, Environment.DIRECTORY_MUSIC); } - public static File getAudioDownloadFolder(Context context) { - return getDir(context, R.string.download_path_audio_key, Environment.DIRECTORY_MUSIC); - } - - public static String getAudioDownloadPath(Context context) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - final String key = context.getString(R.string.download_path_audio_key); - return prefs.getString(key, Environment.DIRECTORY_MUSIC); - } - - private static File getDir(Context context, int keyID, String defaultDirectoryName) { + private static void getDir(Context context, int keyID, String defaultDirectoryName) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); final String key = context.getString(keyID); String downloadPath = prefs.getString(key, null); - if ((downloadPath != null) && (!downloadPath.isEmpty())) return new File(downloadPath.trim()); + if ((downloadPath != null) && (!downloadPath.isEmpty())) return; - final File dir = getDir(defaultDirectoryName); - SharedPreferences.Editor spEditor = prefs.edit(); - spEditor.putString(key, getNewPipeChildFolderPathForDir(dir)); - spEditor.apply(); - return dir; - } - - @NonNull - private static File getDir(String defaultDirectoryName) { - return new File(Environment.getExternalStorageDirectory(), defaultDirectoryName); - } - - public static void resetDownloadFolders(Context context) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - resetDownloadFolder(prefs, context.getString(R.string.download_path_audio_key), Environment.DIRECTORY_MUSIC); - resetDownloadFolder(prefs, context.getString(R.string.download_path_key), Environment.DIRECTORY_MOVIES); - } - - private static void resetDownloadFolder(SharedPreferences prefs, String key, String defaultDirectoryName) { SharedPreferences.Editor spEditor = prefs.edit(); spEditor.putString(key, getNewPipeChildFolderPathForDir(getDir(defaultDirectoryName))); spEditor.apply(); } - private static String getNewPipeChildFolderPathForDir(File dir) { - return new File(dir, "NewPipe").getAbsolutePath(); + @NonNull + public static File getDir(String defaultDirectoryName) { + return new File(Environment.getExternalStorageDirectory(), defaultDirectoryName); } + + private static String getNewPipeChildFolderPathForDir(File dir) { + return new File(dir, "NewPipe").toURI().toString(); + } + + public static boolean useStorageAccessFramework(Context context) { + final String key = context.getString(R.string.storage_use_saf); + final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + + return prefs.getBoolean(key, false); + } + } diff --git a/app/src/main/java/org/schabi/newpipe/settings/SelectChannelFragment.java b/app/src/main/java/org/schabi/newpipe/settings/SelectChannelFragment.java index 61bd4077e..7064aec33 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/SelectChannelFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/SelectChannelFragment.java @@ -3,10 +3,10 @@ package org.schabi.newpipe.settings; import android.app.Activity; import android.content.DialogInterface; import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.v4.app.DialogFragment; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; +import androidx.annotation.NonNull; +import androidx.fragment.app.DialogFragment; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; diff --git a/app/src/main/java/org/schabi/newpipe/settings/SelectKioskFragment.java b/app/src/main/java/org/schabi/newpipe/settings/SelectKioskFragment.java index 8c3bd56e7..d97e4f1b7 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/SelectKioskFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/SelectKioskFragment.java @@ -3,10 +3,10 @@ package org.schabi.newpipe.settings; import android.app.Activity; import android.content.DialogInterface; import android.os.Bundle; -import android.support.v4.app.DialogFragment; -import android.support.v4.content.ContextCompat; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; +import androidx.fragment.app.DialogFragment; +import androidx.core.content.ContextCompat; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -16,7 +16,6 @@ import android.widget.TextView; import org.schabi.newpipe.MainActivity; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.NewPipe; -import org.schabi.newpipe.extractor.ServiceList; import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.report.ErrorActivity; import org.schabi.newpipe.report.UserAction; @@ -124,9 +123,6 @@ public class SelectKioskFragment extends DialogFragment { throws Exception { for(StreamingService service : NewPipe.getServices()) { - //TODO: Multi-service support - if (service.getServiceId() != ServiceList.YouTube.getServiceId() && !DEBUG) continue; - for(String kioskId : service.getKioskList().getAvailableKiosks()) { String name = String.format(getString(R.string.service_kiosk_string), service.getServiceInfo().getName(), diff --git a/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java b/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java index a8482e0eb..a3f218074 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java +++ b/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java @@ -2,12 +2,12 @@ package org.schabi.newpipe.settings; import android.content.Context; import android.os.Bundle; -import android.support.v4.app.Fragment; -import android.support.v7.app.ActionBar; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.preference.Preference; -import android.support.v7.preference.PreferenceFragmentCompat; -import android.support.v7.widget.Toolbar; +import androidx.fragment.app.Fragment; +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AppCompatActivity; +import androidx.preference.Preference; +import androidx.preference.PreferenceFragmentCompat; +import androidx.appcompat.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; diff --git a/app/src/main/java/org/schabi/newpipe/settings/UpdateSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/UpdateSettingsFragment.java index 76d887dd1..9a4d59549 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/UpdateSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/UpdateSettingsFragment.java @@ -1,10 +1,9 @@ package org.schabi.newpipe.settings; import android.os.Bundle; -import android.support.annotation.Nullable; -import android.support.v7.preference.Preference; +import androidx.annotation.Nullable; +import androidx.preference.Preference; -import org.schabi.newpipe.CheckForNewAppVersionTask; import org.schabi.newpipe.R; public class UpdateSettingsFragment extends BasePreferenceFragment { diff --git a/app/src/main/java/org/schabi/newpipe/settings/tabs/AddTabDialog.java b/app/src/main/java/org/schabi/newpipe/settings/tabs/AddTabDialog.java index 695f81ff5..b93ec91d0 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/tabs/AddTabDialog.java +++ b/app/src/main/java/org/schabi/newpipe/settings/tabs/AddTabDialog.java @@ -3,9 +3,9 @@ package org.schabi.newpipe.settings.tabs; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; -import android.support.annotation.DrawableRes; -import android.support.annotation.NonNull; -import android.support.v7.widget.AppCompatImageView; +import androidx.annotation.DrawableRes; +import androidx.annotation.NonNull; +import androidx.appcompat.widget.AppCompatImageView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; diff --git a/app/src/main/java/org/schabi/newpipe/settings/tabs/ChooseTabsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/tabs/ChooseTabsFragment.java index b86f13d14..4297fb13e 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/tabs/ChooseTabsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/tabs/ChooseTabsFragment.java @@ -4,18 +4,18 @@ import android.annotation.SuppressLint; import android.app.Dialog; import android.content.Context; import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.design.widget.FloatingActionButton; -import android.support.v4.app.Fragment; -import android.support.v7.app.ActionBar; -import android.support.v7.app.AlertDialog; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.content.res.AppCompatResources; -import android.support.v7.widget.AppCompatImageView; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.support.v7.widget.helper.ItemTouchHelper; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import com.google.android.material.floatingactionbutton.FloatingActionButton; +import androidx.fragment.app.Fragment; +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.content.res.AppCompatResources; +import androidx.appcompat.widget.AppCompatImageView; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; +import androidx.recyclerview.widget.ItemTouchHelper; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -229,6 +229,12 @@ public class ChooseTabsFragment extends Fragment { returnList.add(new ChooseTabListItem(tab.getTabId(), getString(R.string.channel_page_summary), tab.getTabIconRes(context))); break; + case DEFAULT_KIOSK: + if (!tabList.contains(tab)) { + returnList.add(new ChooseTabListItem(tab.getTabId(), getString(R.string.default_kiosk_page_sumatry), + ThemeHelper.resolveResourceIdFromAttr(context, R.attr.ic_hot))); + } + break; default: if (!tabList.contains(tab)) { returnList.add(new ChooseTabListItem(context, tab)); @@ -310,6 +316,9 @@ public class ChooseTabsFragment extends Fragment { case CHANNEL: tabName = NewPipe.getNameOfService(((Tab.ChannelTab) tab).getChannelServiceId()) + "/" + tabName; break; + case DEFAULT_KIOSK: + tabName = requireContext().getString(R.string.default_kiosk_page_sumatry); + break; } diff --git a/app/src/main/java/org/schabi/newpipe/settings/tabs/Tab.java b/app/src/main/java/org/schabi/newpipe/settings/tabs/Tab.java index d7c249a3e..64ba3683b 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/tabs/Tab.java +++ b/app/src/main/java/org/schabi/newpipe/settings/tabs/Tab.java @@ -1,15 +1,18 @@ package org.schabi.newpipe.settings.tabs; import android.content.Context; -import android.support.annotation.DrawableRes; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.app.Fragment; +import androidx.annotation.DrawableRes; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; import com.grack.nanojson.JsonObject; import com.grack.nanojson.JsonSink; +import org.jsoup.helper.StringUtil; +import org.schabi.newpipe.App; import org.schabi.newpipe.R; +import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.fragments.BlankFragment; import org.schabi.newpipe.fragments.list.channel.ChannelFragment; @@ -19,6 +22,7 @@ import org.schabi.newpipe.local.feed.FeedFragment; import org.schabi.newpipe.local.history.StatisticsPlaylistFragment; import org.schabi.newpipe.local.subscription.SubscriptionFragment; import org.schabi.newpipe.util.KioskTranslator; +import org.schabi.newpipe.util.ServiceHelper; import org.schabi.newpipe.util.ThemeHelper; public abstract class Tab { @@ -111,6 +115,12 @@ public abstract class Tab { return new KioskTab(jsonObject); case CHANNEL: return new ChannelTab(jsonObject); + case DEFAULT_KIOSK: + DefaultKioskTab tab = new DefaultKioskTab(); + if(!StringUtil.isBlank(tab.getKioskId())){ + return tab; + } + return null; } } @@ -128,7 +138,8 @@ public abstract class Tab { BOOKMARKS(new BookmarksTab()), HISTORY(new HistoryTab()), KIOSK(new KioskTab()), - CHANNEL(new ChannelTab()); + CHANNEL(new ChannelTab()), + DEFAULT_KIOSK(new DefaultKioskTab()); private Tab tab; @@ -413,4 +424,55 @@ public abstract class Tab { return channelName; } } + + public static class DefaultKioskTab extends Tab { + public static final int ID = 7; + + private int kioskServiceId; + private String kioskId; + + protected DefaultKioskTab() { + initKiosk(); + } + + public void initKiosk() { + this.kioskServiceId = ServiceHelper.getSelectedServiceId(App.getApp()); + try { + this.kioskId = NewPipe.getService(this.kioskServiceId).getKioskList().getDefaultKioskId(); + } catch (ExtractionException e) { + this.kioskId = ""; + } + } + + @Override + public int getTabId() { + return ID; + } + + @Override + public String getTabName(Context context) { + return KioskTranslator.getTranslatedKioskName(kioskId, context); + } + + @DrawableRes + @Override + public int getTabIconRes(Context context) { + final int kioskIcon = KioskTranslator.getKioskIcons(kioskId, context); + + if (kioskIcon <= 0) { + throw new IllegalStateException("Kiosk ID is not valid: \"" + kioskId + "\""); + } + + return kioskIcon; + } + + @Override + public KioskFragment getFragment() throws ExtractionException { + return KioskFragment.getInstance(kioskServiceId, kioskId); + } + + public String getKioskId() { + return kioskId; + } + } } diff --git a/app/src/main/java/org/schabi/newpipe/settings/tabs/TabsJsonHelper.java b/app/src/main/java/org/schabi/newpipe/settings/tabs/TabsJsonHelper.java index 332e244c8..9553e47e1 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/tabs/TabsJsonHelper.java +++ b/app/src/main/java/org/schabi/newpipe/settings/tabs/TabsJsonHelper.java @@ -1,6 +1,6 @@ package org.schabi.newpipe.settings.tabs; -import android.support.annotation.Nullable; +import androidx.annotation.Nullable; import com.grack.nanojson.JsonArray; import com.grack.nanojson.JsonObject; @@ -9,27 +9,18 @@ import com.grack.nanojson.JsonParserException; import com.grack.nanojson.JsonStringWriter; import com.grack.nanojson.JsonWriter; -import org.schabi.newpipe.settings.tabs.Tab.Type; +import org.jsoup.helper.StringUtil; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.List; -import static org.schabi.newpipe.extractor.ServiceList.YouTube; - /** * Class to get a JSON representation of a list of tabs, and the other way around. */ public class TabsJsonHelper { private static final String JSON_TABS_ARRAY_KEY = "tabs"; - protected static final List FALLBACK_INITIAL_TABS_LIST = Collections.unmodifiableList(Arrays.asList( - new Tab.KioskTab(YouTube.getServiceId(), "Trending"), - Type.SUBSCRIPTIONS.getTab(), - Type.BOOKMARKS.getTab() - )); - public static class InvalidJsonException extends Exception { private InvalidJsonException() { super(); @@ -48,7 +39,7 @@ public class TabsJsonHelper { * Try to reads the passed JSON and returns the list of tabs if no error were encountered. *

* If the JSON is null or empty, or the list of tabs that it represents is empty, the - * {@link #FALLBACK_INITIAL_TABS_LIST fallback list} will be returned. + * {@link #getDefaultTabs fallback list} will be returned. *

* Tabs with invalid ids (i.e. not in the {@link Tab.Type} enum) will be ignored. * @@ -58,7 +49,7 @@ public class TabsJsonHelper { */ public static List getTabsFromJson(@Nullable String tabsJson) throws InvalidJsonException { if (tabsJson == null || tabsJson.isEmpty()) { - return FALLBACK_INITIAL_TABS_LIST; + return getDefaultTabs(); } final List returnTabs = new ArrayList<>(); @@ -86,12 +77,22 @@ public class TabsJsonHelper { } if (returnTabs.isEmpty()) { - return FALLBACK_INITIAL_TABS_LIST; + return getDefaultTabs(); } return returnTabs; } + public static List getDefaultTabs(){ + List tabs = new ArrayList<>(); + Tab.DefaultKioskTab tab = new Tab.DefaultKioskTab(); + if(!StringUtil.isBlank(tab.getKioskId())){ + tabs.add(tab); + } + tabs.add(Tab.Type.SUBSCRIPTIONS.getTab()); + tabs.add(Tab.Type.BOOKMARKS.getTab()); + return Collections.unmodifiableList(tabs); + } /** * Get a JSON representation from a list of tabs. * diff --git a/app/src/main/java/org/schabi/newpipe/settings/tabs/TabsManager.java b/app/src/main/java/org/schabi/newpipe/settings/tabs/TabsManager.java index a7d8dffa4..1c99775e5 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/tabs/TabsManager.java +++ b/app/src/main/java/org/schabi/newpipe/settings/tabs/TabsManager.java @@ -44,7 +44,7 @@ public class TabsManager { } public List getDefaultTabs() { - return TabsJsonHelper.FALLBACK_INITIAL_TABS_LIST; + return TabsJsonHelper.getDefaultTabs(); } /*////////////////////////////////////////////////////////////////////////// diff --git a/app/src/main/java/org/schabi/newpipe/streams/DataReader.java b/app/src/main/java/org/schabi/newpipe/streams/DataReader.java index d0e946eb7..0e62810c5 100644 --- a/app/src/main/java/org/schabi/newpipe/streams/DataReader.java +++ b/app/src/main/java/org/schabi/newpipe/streams/DataReader.java @@ -1,9 +1,10 @@ package org.schabi.newpipe.streams; +import org.schabi.newpipe.streams.io.SharpStream; + import java.io.EOFException; import java.io.IOException; - -import org.schabi.newpipe.streams.io.SharpStream; +import java.io.InputStream; /** * @author kapodamy @@ -15,89 +16,239 @@ public class DataReader { public final static int INTEGER_SIZE = 4; public final static int FLOAT_SIZE = 4; - private long pos; - public final SharpStream stream; - private final boolean rewind; + private final static int BUFFER_SIZE = 128 * 1024;// 128 KiB + + private long position = 0; + private final SharpStream stream; + + private InputStream view; + private int viewSize; public DataReader(SharpStream stream) { - this.rewind = stream.canRewind(); this.stream = stream; - this.pos = 0L; + this.readOffset = this.readBuffer.length; } public long position() { - return pos; + return position; } - public final int readInt() throws IOException { + public int read() throws IOException { + if (fillBuffer()) { + return -1; + } + + position++; + readCount--; + + return readBuffer[readOffset++] & 0xFF; + } + + public long skipBytes(long amount) throws IOException { + if (readCount < 0) { + return 0; + } else if (readCount == 0) { + amount = stream.skip(amount); + } else { + if (readCount > amount) { + readCount -= (int) amount; + readOffset += (int) amount; + } else { + amount = readCount + stream.skip(amount - readCount); + readCount = 0; + readOffset = readBuffer.length; + } + } + + position += amount; + return amount; + } + + public int readInt() throws IOException { primitiveRead(INTEGER_SIZE); return primitive[0] << 24 | primitive[1] << 16 | primitive[2] << 8 | primitive[3]; } - public final int read() throws IOException { - int value = stream.read(); - if (value == -1) { - throw new EOFException(); - } - - pos++; - return value; + public short readShort() throws IOException { + primitiveRead(SHORT_SIZE); + return (short) (primitive[0] << 8 | primitive[1]); } - public final long skipBytes(long amount) throws IOException { - amount = stream.skip(amount); - pos += amount; - return amount; - } - - public final long readLong() throws IOException { + public long readLong() throws IOException { primitiveRead(LONG_SIZE); long high = primitive[0] << 24 | primitive[1] << 16 | primitive[2] << 8 | primitive[3]; long low = primitive[4] << 24 | primitive[5] << 16 | primitive[6] << 8 | primitive[7]; return high << 32 | low; } - public final short readShort() throws IOException { - primitiveRead(SHORT_SIZE); - return (short) (primitive[0] << 8 | primitive[1]); - } - - public final int read(byte[] buffer) throws IOException { + public int read(byte[] buffer) throws IOException { return read(buffer, 0, buffer.length); } - public final int read(byte[] buffer, int offset, int count) throws IOException { - int res = stream.read(buffer, offset, count); - pos += res; + public int read(byte[] buffer, int offset, int count) throws IOException { + if (readCount < 0) { + return -1; + } + int total = 0; - return res; + if (count >= readBuffer.length) { + if (readCount > 0) { + System.arraycopy(readBuffer, readOffset, buffer, offset, readCount); + readOffset += readCount; + + offset += readCount; + count -= readCount; + + total = readCount; + readCount = 0; + } + total += Math.max(stream.read(buffer, offset, count), 0); + } else { + while (count > 0 && !fillBuffer()) { + int read = Math.min(readCount, count); + System.arraycopy(readBuffer, readOffset, buffer, offset, read); + + readOffset += read; + readCount -= read; + + offset += read; + count -= read; + + total += read; + } + } + + position += total; + return total; } - public final boolean available() { - return stream.available() > 0; + public boolean available() { + return readCount > 0 || stream.available() > 0; } public void rewind() throws IOException { stream.rewind(); - pos = 0; + + if ((position - viewSize) > 0) { + viewSize = 0;// drop view + } else { + viewSize += position; + } + + position = 0; + readOffset = readBuffer.length; } public boolean canRewind() { - return rewind; + return stream.canRewind(); } - private short[] primitive = new short[LONG_SIZE]; + /** + * Wraps this instance of {@code DataReader} into {@code InputStream} + * object. Note: Any read in the {@code DataReader} will not modify + * (decrease) the view size + * + * @param size the size of the view + * @return the view + */ + public InputStream getView(int size) { + if (view == null) { + view = new InputStream() { + @Override + public int read() throws IOException { + if (viewSize < 1) { + return -1; + } + int res = DataReader.this.read(); + if (res > 0) { + viewSize--; + } + return res; + } + + @Override + public int read(byte[] buffer) throws IOException { + return read(buffer, 0, buffer.length); + } + + @Override + public int read(byte[] buffer, int offset, int count) throws IOException { + if (viewSize < 1) { + return -1; + } + + int res = DataReader.this.read(buffer, offset, Math.min(viewSize, count)); + viewSize -= res; + + return res; + } + + @Override + public long skip(long amount) throws IOException { + if (viewSize < 1) { + return 0; + } + int res = (int) DataReader.this.skipBytes(Math.min(amount, viewSize)); + viewSize -= res; + + return res; + } + + @Override + public int available() { + return viewSize; + } + + @Override + public void close() { + viewSize = 0; + } + + @Override + public boolean markSupported() { + return false; + } + + }; + } + viewSize = size; + + return view; + } + + private final short[] primitive = new short[LONG_SIZE]; private void primitiveRead(int amount) throws IOException { byte[] buffer = new byte[amount]; - int read = stream.read(buffer, 0, amount); - pos += read; + int read = read(buffer, 0, amount); + if (read != amount) { - throw new EOFException("Truncated data, missing " + String.valueOf(amount - read) + " bytes"); + throw new EOFException("Truncated stream, missing " + String.valueOf(amount - read) + " bytes"); } - for (int i = 0; i < buffer.length; i++) { - primitive[i] = (short) (buffer[i] & 0xFF);// the "byte" datatype is signed and is very annoying + for (int i = 0; i < amount; i++) { + primitive[i] = (short) (buffer[i] & 0xFF);// the "byte" data type in java is signed and is very annoying } } + + private final byte[] readBuffer = new byte[BUFFER_SIZE]; + private int readOffset; + private int readCount; + + private boolean fillBuffer() throws IOException { + if (readCount < 0) { + return true; + } + if (readOffset >= readBuffer.length) { + readCount = stream.read(readBuffer); + if (readCount < 1) { + readCount = -1; + return true; + } + readOffset = 0; + } + + return readCount < 1; + } + } diff --git a/app/src/main/java/org/schabi/newpipe/streams/Mp4DashReader.java b/app/src/main/java/org/schabi/newpipe/streams/Mp4DashReader.java index 271929d47..c52ebf3aa 100644 --- a/app/src/main/java/org/schabi/newpipe/streams/Mp4DashReader.java +++ b/app/src/main/java/org/schabi/newpipe/streams/Mp4DashReader.java @@ -1,17 +1,15 @@ package org.schabi.newpipe.streams; +import org.schabi.newpipe.streams.io.SharpStream; + import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; - import java.nio.ByteBuffer; - import java.util.ArrayList; import java.util.NoSuchElementException; -import org.schabi.newpipe.streams.io.SharpStream; - /** * @author kapodamy */ @@ -35,14 +33,29 @@ public class Mp4DashReader { private static final int ATOM_TREX = 0x74726578; private static final int ATOM_TKHD = 0x746B6864; private static final int ATOM_MFRA = 0x6D667261; - private static final int ATOM_TFRA = 0x74667261; private static final int ATOM_MDHD = 0x6D646864; + private static final int ATOM_EDTS = 0x65647473; + private static final int ATOM_ELST = 0x656C7374; + private static final int ATOM_HDLR = 0x68646C72; + private static final int ATOM_MINF = 0x6D696E66; + private static final int ATOM_DINF = 0x64696E66; + private static final int ATOM_STBL = 0x7374626C; + private static final int ATOM_STSD = 0x73747364; + private static final int ATOM_VMHD = 0x766D6864; + private static final int ATOM_SMHD = 0x736D6864; + private static final int BRAND_DASH = 0x64617368; + private static final int BRAND_ISO5 = 0x69736F35; + + private static final int HANDLER_VIDE = 0x76696465; + private static final int HANDLER_SOUN = 0x736F756E; + private static final int HANDLER_SUBT = 0x73756274; // private final DataReader stream; private Mp4Track[] tracks = null; + private int[] brands = null; private Box box; private Moof moof; @@ -50,9 +63,10 @@ public class Mp4DashReader { private boolean chunkZero = false; private int selectedTrack = -1; + private Box backupBox = null; public enum TrackKind { - Audio, Video, Other + Audio, Video, Subtitles, Other } public Mp4DashReader(SharpStream source) { @@ -65,8 +79,15 @@ public class Mp4DashReader { } box = readBox(ATOM_FTYP); - if (parse_ftyp() != BRAND_DASH) { - throw new NoSuchElementException("Main Brand is not dash"); + brands = parse_ftyp(box); + switch (brands[0]) { + case BRAND_DASH: + case BRAND_ISO5:// ¿why not? + break; + default: + throw new NoSuchElementException( + "Not a MPEG-4 DASH container, major brand is not 'dash' or 'iso5' is " + boxName(brands[0]) + ); } Moov moov = null; @@ -84,8 +105,6 @@ public class Mp4DashReader { break; case ATOM_MFRA: break; - case ATOM_MDAT: - throw new IOException("Expected moof, found mdat"); } } @@ -107,15 +126,26 @@ public class Mp4DashReader { } } - if (moov.trak[i].tkhd.bHeight == 0 && moov.trak[i].tkhd.bWidth == 0) { - tracks[i].kind = moov.trak[i].tkhd.bVolume == 0 ? TrackKind.Other : TrackKind.Audio; - } else { - tracks[i].kind = TrackKind.Video; + switch (moov.trak[i].mdia.hdlr.subType) { + case HANDLER_VIDE: + tracks[i].kind = TrackKind.Video; + break; + case HANDLER_SOUN: + tracks[i].kind = TrackKind.Audio; + break; + case HANDLER_SUBT: + tracks[i].kind = TrackKind.Subtitles; + break; + default: + tracks[i].kind = TrackKind.Other; + break; } } + + backupBox = box; } - public Mp4Track selectTrack(int index) { + Mp4Track selectTrack(int index) { selectedTrack = index; return tracks[index]; } @@ -126,7 +156,7 @@ public class Mp4DashReader { * @return list with a basic info * @throws IOException if the source stream is not seekeable */ - public int getFragmentsCount() throws IOException { + int getFragmentsCount() throws IOException { if (selectedTrack < 0) { throw new IllegalStateException("track no selected"); } @@ -136,7 +166,6 @@ public class Mp4DashReader { Box tmp; int count = 0; - long orig_offset = stream.position(); if (box.type == ATOM_MOOF) { tmp = box; @@ -162,17 +191,36 @@ public class Mp4DashReader { ensure(tmp); } while (stream.available() && (tmp = readBox()) != null); - stream.rewind(); - stream.skipBytes((int) orig_offset); + rewind(); return count; } + public int[] getBrands() { + if (brands == null) throw new IllegalStateException("Not parsed"); + return brands; + } + + public void rewind() throws IOException { + if (!stream.canRewind()) { + throw new IOException("The provided stream doesn't allow seek"); + } + if (box == null) { + return; + } + + box = backupBox; + chunkZero = false; + + stream.rewind(); + stream.skipBytes(backupBox.offset + (DataReader.INTEGER_SIZE * 2)); + } + public Mp4Track[] getAvailableTracks() { return tracks; } - public Mp4TrackChunk getNextChunk() throws IOException { + public Mp4DashChunk getNextChunk(boolean infoOnly) throws IOException { Mp4Track track = tracks[selectedTrack]; while (stream.available()) { @@ -208,7 +256,7 @@ public class Mp4DashReader { if (hasFlag(moof.traf.tfhd.bFlags, 0x10)) { moof.traf.trun.chunkSize = moof.traf.tfhd.defaultSampleSize * moof.traf.trun.entryCount; } else { - moof.traf.trun.chunkSize = box.size - 8; + moof.traf.trun.chunkSize = (int) (box.size - 8); } } if (!hasFlag(moof.traf.trun.bFlags, 0x900) && moof.traf.trun.chunkDuration == 0) { @@ -228,9 +276,12 @@ public class Mp4DashReader { continue;// find another chunk } - Mp4TrackChunk chunk = new Mp4TrackChunk(); + Mp4DashChunk chunk = new Mp4DashChunk(); chunk.moof = moof; - chunk.data = new TrackDataChunk(stream, moof.traf.trun.chunkSize); + if (!infoOnly) { + chunk.data = stream.getView(moof.traf.trun.chunkSize); + } + moof = null; stream.skipBytes(chunk.moof.traf.trun.dataOffset); @@ -269,6 +320,10 @@ public class Mp4DashReader { b.size = stream.readInt(); b.type = stream.readInt(); + if (b.size == 1) { + b.size = stream.readLong(); + } + return b; } @@ -280,6 +335,25 @@ public class Mp4DashReader { return b; } + private byte[] readFullBox(Box ref) throws IOException { + // full box reading is limited to 2 GiB, and should be enough + int size = (int) ref.size; + + ByteBuffer buffer = ByteBuffer.allocate(size); + buffer.putInt(size); + buffer.putInt(ref.type); + + int read = size - 8; + + if (stream.read(buffer.array(), 8, read) != read) { + throw new EOFException( + String.format("EOF reached in box: type=%s offset=%s size=%s", boxName(ref.type), ref.offset, ref.size) + ); + } + + return buffer.array(); + } + private void ensure(Box ref) throws IOException { long skip = ref.offset + ref.size - stream.position(); @@ -310,6 +384,14 @@ public class Mp4DashReader { return null; } + private Box untilAnyBox(Box ref) throws IOException { + if (stream.position() >= (ref.offset + ref.size)) { + return null; + } + + return readBox(); + } + // // @@ -329,7 +411,7 @@ public class Mp4DashReader { return obj; } } - + return obj; } @@ -397,14 +479,14 @@ public class Mp4DashReader { private long parse_tfdt() throws IOException { int version = stream.read(); - stream.skipBytes(3);// flags + stream.skipBytes(3);// flags return version == 0 ? readUint() : stream.readLong(); } private Trun parse_trun() throws IOException { Trun obj = new Trun(); obj.bFlags = stream.readInt(); - obj.entryCount = stream.readInt();// unsigned int + obj.entryCount = stream.readInt();// unsigned int obj.entries_rowSize = 0; if (hasFlag(obj.bFlags, 0x0100)) { @@ -448,11 +530,18 @@ public class Mp4DashReader { return obj; } - private int parse_ftyp() throws IOException { - int brand = stream.readInt(); + private int[] parse_ftyp(Box ref) throws IOException { + int i = 0; + int[] list = new int[(int) ((ref.offset + ref.size - stream.position() - 4) / 4)]; + + list[i++] = stream.readInt();// major brand + stream.skipBytes(4);// minor version - return brand; + for (; i < list.length; i++) + list[i] = stream.readInt();// compatible brands + + return list; } private Mvhd parse_mvhd() throws IOException { @@ -521,32 +610,66 @@ public class Mp4DashReader { trak.tkhd = parse_tkhd(); ensure(b); - b = untilBox(ref, ATOM_MDIA); - trak.mdia = new byte[b.size]; + while ((b = untilBox(ref, ATOM_MDIA, ATOM_EDTS)) != null) { + switch (b.type) { + case ATOM_MDIA: + trak.mdia = parse_mdia(b); + break; + case ATOM_EDTS: + trak.edst_elst = parse_edts(b); + break; + } - ByteBuffer buffer = ByteBuffer.wrap(trak.mdia); - buffer.putInt(b.size); - buffer.putInt(ATOM_MDIA); - stream.read(trak.mdia, 8, b.size - 8); - - trak.mdia_mdhd_timeScale = parse_mdia(buffer); + ensure(b); + } return trak; } - private int parse_mdia(ByteBuffer data) { - while (data.hasRemaining()) { - int end = data.position() + data.getInt(); - if (data.getInt() == ATOM_MDHD) { - byte version = data.get(); - data.position(data.position() + 3 + ((version == 0 ? 4 : 8) * 2)); - return data.getInt(); - } + private Mdia parse_mdia(Box ref) throws IOException { + Mdia obj = new Mdia(); - data.position(end); + Box b; + while ((b = untilBox(ref, ATOM_MDHD, ATOM_HDLR, ATOM_MINF)) != null) { + switch (b.type) { + case ATOM_MDHD: + obj.mdhd = readFullBox(b); + + // read time scale + ByteBuffer buffer = ByteBuffer.wrap(obj.mdhd); + byte version = buffer.get(8); + buffer.position(12 + ((version == 0 ? 4 : 8) * 2)); + obj.mdhd_timeScale = buffer.getInt(); + break; + case ATOM_HDLR: + obj.hdlr = parse_hdlr(b); + break; + case ATOM_MINF: + obj.minf = parse_minf(b); + break; + } + ensure(b); } - return 0;// this NEVER should happen + return obj; + } + + private Hdlr parse_hdlr(Box ref) throws IOException { + // version + // flags + stream.skipBytes(4); + + Hdlr obj = new Hdlr(); + obj.bReserved = new byte[12]; + + obj.type = stream.readInt(); + obj.subType = stream.readInt(); + stream.read(obj.bReserved); + + // component name (is a ansi/ascii string) + stream.skipBytes((ref.offset + ref.size) - stream.position()); + + return obj; } private Moov parse_moov(Box ref) throws IOException { @@ -570,7 +693,7 @@ public class Mp4DashReader { ensure(b); } - moov.trak = tmp.toArray(new Trak[tmp.size()]); + moov.trak = tmp.toArray(new Trak[0]); return moov; } @@ -584,7 +707,7 @@ public class Mp4DashReader { ensure(b); } - return tmp.toArray(new Trex[tmp.size()]); + return tmp.toArray(new Trex[0]); } private Trex parse_trex() throws IOException { @@ -602,74 +725,74 @@ public class Mp4DashReader { return obj; } - private Tfra parse_tfra() throws IOException { - int version = stream.read(); - - stream.skipBytes(3);// flags - - Tfra tfra = new Tfra(); - tfra.trackId = stream.readInt(); - - stream.skipBytes(3);// reserved - int bFlags = stream.read(); - int size_tts = ((bFlags >> 4) & 3) + ((bFlags >> 2) & 3) + (bFlags & 3); - - tfra.entries_time = new int[stream.readInt()]; - - for (int i = 0; i < tfra.entries_time.length; i++) { - tfra.entries_time[i] = version == 0 ? stream.readInt() : (int) stream.readLong(); - stream.skipBytes(size_tts + (version == 0 ? 4 : 8)); + private Elst parse_edts(Box ref) throws IOException { + Box b = untilBox(ref, ATOM_ELST); + if (b == null) { + return null; } - return tfra; - } - - private Sidx parse_sidx() throws IOException { - int version = stream.read(); + Elst obj = new Elst(); + boolean v1 = stream.read() == 1; stream.skipBytes(3);// flags - Sidx obj = new Sidx(); - obj.referenceId = stream.readInt(); - obj.timescale = stream.readInt(); + int entryCount = stream.readInt(); + if (entryCount < 1) { + obj.bMediaRate = 0x00010000;// default media rate (1.0) + return obj; + } - // earliest presentation entries_time - // first offset - // reserved - stream.skipBytes((2 * (version == 0 ? 4 : 8)) + 2); + if (v1) { + stream.skipBytes(DataReader.LONG_SIZE);// segment duration + obj.MediaTime = stream.readLong(); + // ignore all remain entries + stream.skipBytes((entryCount - 1) * (DataReader.LONG_SIZE * 2)); + } else { + stream.skipBytes(DataReader.INTEGER_SIZE);// segment duration + obj.MediaTime = stream.readInt(); + } - obj.entries_subsegmentDuration = new int[stream.readShort()]; + obj.bMediaRate = stream.readInt(); - for (int i = 0; i < obj.entries_subsegmentDuration.length; i++) { - // reference type - // referenced size - stream.skipBytes(4); - obj.entries_subsegmentDuration[i] = stream.readInt();// unsigned int + return obj; + } - // starts with SAP - // SAP type - // SAP delta entries_time - stream.skipBytes(4); + private Minf parse_minf(Box ref) throws IOException { + Minf obj = new Minf(); + + Box b; + while ((b = untilAnyBox(ref)) != null) { + + switch (b.type) { + case ATOM_DINF: + obj.dinf = readFullBox(b); + break; + case ATOM_STBL: + obj.stbl_stsd = parse_stbl(b); + break; + case ATOM_VMHD: + case ATOM_SMHD: + obj.$mhd = readFullBox(b); + break; + + } + ensure(b); } return obj; } - private Tfra[] parse_mfra(Box ref, int trackCount) throws IOException { - ArrayList tmp = new ArrayList<>(trackCount); - long limit = ref.offset + ref.size; + /** + * this only read the "stsd" box inside + */ + private byte[] parse_stbl(Box ref) throws IOException { + Box b = untilBox(ref, ATOM_STSD); - while (stream.position() < limit) { - box = readBox(); - - if (box.type == ATOM_TFRA) { - tmp.add(parse_tfra()); - } - - ensure(box); + if (b == null) { + return new byte[0];// this never should happens (missing codec startup data) } - return tmp.toArray(new Tfra[tmp.size()]); + return readFullBox(b); } // @@ -679,14 +802,7 @@ public class Mp4DashReader { int type; long offset; - int size; - } - - class Sidx { - - int timescale; - int referenceId; - int[] entries_subsegmentDuration; + long size; } public class Moof { @@ -711,12 +827,16 @@ public class Mp4DashReader { int defaultSampleFlags; } - public class TrunEntry { + class TrunEntry { + + int sampleDuration; + int sampleSize; + int sampleFlags; + int sampleCompositionTimeOffset; + + boolean hasCompositionTimeOffset; + boolean isKeyframe; - public int sampleDuration; - public int sampleSize; - public int sampleFlags; - public int sampleCompositionTimeOffset; } public class Trun { @@ -749,6 +869,31 @@ public class Mp4DashReader { entry.sampleCompositionTimeOffset = buffer.getInt(); } + entry.hasCompositionTimeOffset = hasFlag(bFlags, 0x0800); + entry.isKeyframe = !hasFlag(entry.sampleFlags, 0x10000); + + return entry; + } + + public TrunEntry getAbsoluteEntry(int i, Tfhd header) { + TrunEntry entry = getEntry(i); + + if (!hasFlag(bFlags, 0x0100) && hasFlag(header.bFlags, 0x20)) { + entry.sampleFlags = header.defaultSampleFlags; + } + + if (!hasFlag(bFlags, 0x0200) && hasFlag(header.bFlags, 0x10)) { + entry.sampleSize = header.defaultSampleSize; + } + + if (!hasFlag(bFlags, 0x0100) && hasFlag(header.bFlags, 0x08)) { + entry.sampleDuration = header.defaultSampleDuration; + } + + if (i == 0 && hasFlag(bFlags, 0x0004)) { + entry.sampleFlags = bFirstSampleFlags; + } + return entry; } } @@ -768,9 +913,9 @@ public class Mp4DashReader { public class Trak { public Tkhd tkhd; - public int mdia_mdhd_timeScale; + public Elst edst_elst; + public Mdia mdia; - byte[] mdia; } class Mvhd { @@ -786,12 +931,6 @@ public class Mp4DashReader { Trex[] mvex_trex; } - class Tfra { - - int trackId; - int[] entries_time; - } - public class Trex { private int trackId; @@ -801,6 +940,34 @@ public class Mp4DashReader { int defaultSampleFlags; } + public class Elst { + + public long MediaTime; + public int bMediaRate; + } + + public class Mdia { + + public int mdhd_timeScale; + public byte[] mdhd; + public Hdlr hdlr; + public Minf minf; + } + + public class Hdlr { + + public int type; + public int subType; + public byte[] bReserved; + } + + public class Minf { + + public byte[] dinf; + public byte[] stbl_stsd; + public byte[] $mhd; + } + public class Mp4Track { public TrackKind kind; @@ -808,10 +975,43 @@ public class Mp4DashReader { public Trex trex; } - public class Mp4TrackChunk { + public class Mp4DashChunk { public InputStream data; public Moof moof; + private int i = 0; + + public TrunEntry getNextSampleInfo() { + if (i >= moof.traf.trun.entryCount) { + return null; + } + return moof.traf.trun.getAbsoluteEntry(i++, moof.traf.tfhd); + } + + public Mp4DashSample getNextSample() throws IOException { + if (data == null) { + throw new IllegalStateException("This chunk has info only"); + } + if (i >= moof.traf.trun.entryCount) { + return null; + } + + Mp4DashSample sample = new Mp4DashSample(); + sample.info = moof.traf.trun.getAbsoluteEntry(i++, moof.traf.tfhd); + sample.data = new byte[sample.info.sampleSize]; + + if (data.read(sample.data) != sample.info.sampleSize) { + throw new EOFException("EOF reached while reading a sample"); + } + + return sample; + } + } + + public class Mp4DashSample { + + public TrunEntry info; + public byte[] data; } // } diff --git a/app/src/main/java/org/schabi/newpipe/streams/Mp4DashWriter.java b/app/src/main/java/org/schabi/newpipe/streams/Mp4DashWriter.java deleted file mode 100644 index babb2e24c..000000000 --- a/app/src/main/java/org/schabi/newpipe/streams/Mp4DashWriter.java +++ /dev/null @@ -1,623 +0,0 @@ -package org.schabi.newpipe.streams; - -import org.schabi.newpipe.streams.io.SharpStream; - -import org.schabi.newpipe.streams.Mp4DashReader.Mp4Track; -import org.schabi.newpipe.streams.Mp4DashReader.Mp4TrackChunk; -import org.schabi.newpipe.streams.Mp4DashReader.Trak; -import org.schabi.newpipe.streams.Mp4DashReader.Trex; - - -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.List; - -import static org.schabi.newpipe.streams.Mp4DashReader.hasFlag; - -/** - * - * @author kapodamy - */ -public class Mp4DashWriter { - - private final static byte DIMENSIONAL_FIVE = 5; - private final static byte DIMENSIONAL_TWO = 2; - private final static short DEFAULT_TIMESCALE = 1000; - private final static int BUFFER_SIZE = 8 * 1024; - private final static byte DEFAULT_TREX_SIZE = 32; - private final static byte[] TFRA_TTS_DEFAULT = new byte[]{0x01, 0x01, 0x01}; - private final static int EPOCH_OFFSET = 2082844800; - - private Mp4Track[] infoTracks; - private SharpStream[] sourceTracks; - - private Mp4DashReader[] readers; - private final long time; - - private boolean done = false; - private boolean parsed = false; - - private long written = 0; - private ArrayList> chunkTimes; - private ArrayList moofOffsets; - private ArrayList fragSizes; - - public Mp4DashWriter(SharpStream... source) { - sourceTracks = source; - readers = new Mp4DashReader[sourceTracks.length]; - infoTracks = new Mp4Track[sourceTracks.length]; - time = (System.currentTimeMillis() / 1000L) + EPOCH_OFFSET; - } - - public Mp4Track[] getTracksFromSource(int sourceIndex) throws IllegalStateException { - if (!parsed) { - throw new IllegalStateException("All sources must be parsed first"); - } - - return readers[sourceIndex].getAvailableTracks(); - } - - public void parseSources() throws IOException, IllegalStateException { - if (done) { - throw new IllegalStateException("already done"); - } - if (parsed) { - throw new IllegalStateException("already parsed"); - } - - try { - for (int i = 0; i < readers.length; i++) { - readers[i] = new Mp4DashReader(sourceTracks[i]); - readers[i].parse(); - } - - } finally { - parsed = true; - } - } - - public void selectTracks(int... trackIndex) throws IOException { - if (done) { - throw new IOException("already done"); - } - if (chunkTimes != null) { - throw new IOException("tracks already selected"); - } - - try { - chunkTimes = new ArrayList<>(readers.length); - moofOffsets = new ArrayList<>(32); - fragSizes = new ArrayList<>(32); - - for (int i = 0; i < readers.length; i++) { - infoTracks[i] = readers[i].selectTrack(trackIndex[i]); - - chunkTimes.add(new ArrayList(32)); - } - - } finally { - parsed = true; - } - } - - public long getBytesWritten() { - return written; - } - - public void build(SharpStream out) throws IOException, RuntimeException { - if (done) { - throw new RuntimeException("already done"); - } - if (!out.canWrite()) { - throw new IOException("the provided output is not writable"); - } - - long sidxOffsets = -1; - int maxFrags = 0; - - for (SharpStream stream : sourceTracks) { - if (!stream.canRewind()) { - sidxOffsets = -2;// sidx not available - } - } - - try { - dump(make_ftyp(), out); - dump(make_moov(), out); - - if (sidxOffsets == -1 && out.canRewind()) { - // - int reserved = 0; - for (Mp4DashReader reader : readers) { - int count = reader.getFragmentsCount(); - if (count > maxFrags) { - maxFrags = count; - } - reserved += 12 + calcSidxBodySize(count); - } - if (maxFrags > 0xFFFF) { - sidxOffsets = -3;// TODO: to many fragments, needs a multi-sidx implementation - } else { - sidxOffsets = written; - dump(make_free(reserved), out); - } - // - } - ArrayList chunks = new ArrayList<>(readers.length); - chunks.add(null); - - int read; - byte[] buffer = new byte[BUFFER_SIZE]; - int sequenceNumber = 1; - - while (true) { - chunks.clear(); - - for (int i = 0; i < readers.length; i++) { - Mp4TrackChunk chunk = readers[i].getNextChunk(); - if (chunk == null || chunk.moof.traf.trun.chunkSize < 1) { - continue; - } - chunk.moof.traf.tfhd.trackId = i + 1; - chunks.add(chunk); - - if (sequenceNumber == 1) { - if (chunk.moof.traf.trun.entryCount > 0 && hasFlag(chunk.moof.traf.trun.bFlags, 0x0800)) { - chunkTimes.get(i).add(chunk.moof.traf.trun.getEntry(0).sampleCompositionTimeOffset); - } else { - chunkTimes.get(i).add(0); - } - } - - chunkTimes.get(i).add(chunk.moof.traf.trun.chunkDuration); - } - - if (chunks.size() < 1) { - break; - } - - long offset = written; - moofOffsets.add(offset); - - dump(make_moof(sequenceNumber++, chunks, offset), out); - dump(make_mdat(chunks), out); - - for (Mp4TrackChunk chunk : chunks) { - while ((read = chunk.data.read(buffer)) > 0) { - out.write(buffer, 0, read); - written += read; - } - } - - fragSizes.add((int) (written - offset)); - } - - dump(make_mfra(), out); - - if (sidxOffsets > 0 && moofOffsets.size() == maxFrags) { - long len = written; - - out.rewind(); - out.skip(sidxOffsets); - - written = sidxOffsets; - sidxOffsets = moofOffsets.get(0); - - for (int i = 0; i < readers.length; i++) { - dump(make_sidx(i, sidxOffsets - written), out); - } - - written = len; - } - } finally { - done = true; - } - } - - public boolean isDone() { - return done; - } - - public boolean isParsed() { - return parsed; - } - - public void close() { - done = true; - parsed = true; - - for (SharpStream src : sourceTracks) { - src.dispose(); - } - - sourceTracks = null; - readers = null; - infoTracks = null; - moofOffsets = null; - chunkTimes = null; - } - - // - private void dump(byte[][] buffer, SharpStream stream) throws IOException { - for (byte[] buff : buffer) { - stream.write(buff); - written += buff.length; - } - } - - private byte[][] lengthFor(byte[][] buffer) { - int length = 0; - for (byte[] buff : buffer) { - length += buff.length; - } - - ByteBuffer.wrap(buffer[0]).putInt(length); - - return buffer; - } - - private int calcSidxBodySize(int entryCount) { - return 4 + 4 + 8 + 8 + 4 + (entryCount * 12); - } - // - - // - private byte[][] make_moof(int sequence, ArrayList chunks, long referenceOffset) { - int pos = 2; - TrunExtra[] extra = new TrunExtra[chunks.size()]; - - byte[][] buffer = new byte[pos + (extra.length * DIMENSIONAL_FIVE)][]; - buffer[0] = new byte[]{ - 0x00, 0x00, 0x00, 0x00, 0x6D, 0x6F, 0x6F, 0x66,// info header - 0x00, 0x00, 0x00, 0x10, 0x6D, 0x66, 0x68, 0x64, 0x00, 0x00, 0x00, 0x00//mfhd - }; - buffer[1] = new byte[4]; - ByteBuffer.wrap(buffer[1]).putInt(sequence); - - for (int i = 0; i < extra.length; i++) { - extra[i] = new TrunExtra(); - for (byte[] buff : make_traf(chunks.get(i), extra[i], referenceOffset)) { - buffer[pos++] = buff; - } - } - - lengthFor(buffer); - - int offset = 8 + ByteBuffer.wrap(buffer[0]).getInt(); - - for (int i = 0; i < extra.length; i++) { - extra[i].byteBuffer.putInt(offset); - offset += chunks.get(i).moof.traf.trun.chunkSize; - } - - return buffer; - } - - private byte[][] make_traf(Mp4TrackChunk chunk, TrunExtra extra, long moofOffset) { - byte[][] buffer = new byte[DIMENSIONAL_FIVE][]; - buffer[0] = new byte[]{ - 0x00, 0x00, 0x00, 0x00, 0x74, 0x72, 0x61, 0x66, - 0x00, 0x00, 0x00, 0x00, 0x74, 0x66, 0x68, 0x64 - }; - - int flags = (chunk.moof.traf.tfhd.bFlags & 0x38) | 0x01; - byte tfhdBodySize = 8 + 8; - if (hasFlag(flags, 0x08)) { - tfhdBodySize += 4; - } - if (hasFlag(flags, 0x10)) { - tfhdBodySize += 4; - } - if (hasFlag(flags, 0x20)) { - tfhdBodySize += 4; - } - buffer[1] = new byte[tfhdBodySize]; - ByteBuffer set = ByteBuffer.wrap(buffer[1]); - set.position(4); - set.putInt(chunk.moof.traf.tfhd.trackId); - set.putLong(moofOffset); - if (hasFlag(flags, 0x08)) { - set.putInt(chunk.moof.traf.tfhd.defaultSampleDuration); - } - if (hasFlag(flags, 0x10)) { - set.putInt(chunk.moof.traf.tfhd.defaultSampleSize); - } - if (hasFlag(flags, 0x20)) { - set.putInt(chunk.moof.traf.tfhd.defaultSampleFlags); - } - set.putInt(0, flags); - ByteBuffer.wrap(buffer[0]).putInt(8, 8 + tfhdBodySize); - - buffer[2] = new byte[]{ - 0x00, 0x00, 0x00, 0x14, - 0x74, 0x66, 0x64, 0x74, - 0x01, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00 - }; - - ByteBuffer.wrap(buffer[2]).putLong(12, chunk.moof.traf.tfdt); - - buffer[3] = new byte[]{ - 0x00, 0x00, 0x00, 0x00, 0x74, 0x72, 0x75, 0x6E, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00 - }; - - buffer[4] = chunk.moof.traf.trun.bEntries; - - lengthFor(buffer); - - set = ByteBuffer.wrap(buffer[3]); - set.putInt(buffer[3].length + buffer[4].length); - set.position(8); - set.putInt((chunk.moof.traf.trun.bFlags | 0x01) & 0x0F01); - set.putInt(chunk.moof.traf.trun.entryCount); - extra.byteBuffer = set; - - return buffer; - } - - private byte[][] make_mdat(ArrayList chunks) { - byte[][] buffer = new byte[][]{ - { - 0x00, 0x00, 0x00, 0x00, 0x6D, 0x64, 0x61, 0x74 - } - }; - - int length = 0; - - for (Mp4TrackChunk chunk : chunks) { - length += chunk.moof.traf.trun.chunkSize; - } - - ByteBuffer.wrap(buffer[0]).putInt(length + 8); - - return buffer; - } - - private byte[][] make_ftyp() { - return new byte[][]{ - { - 0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70, 0x64, 0x61, 0x73, 0x68, 0x00, 0x00, 0x00, 0x00, - 0x6D, 0x70, 0x34, 0x31, 0x69, 0x73, 0x6F, 0x6D, 0x69, 0x73, 0x6F, 0x36, 0x69, 0x73, 0x6F, 0x32 - } - }; - } - - private byte[][] make_mvhd() { - byte[][] buffer = new byte[DIMENSIONAL_FIVE][]; - - buffer[0] = new byte[]{ - 0x00, 0x00, 0x00, 0x78, 0x6D, 0x76, 0x68, 0x64, 0x01, 0x00, 0x00, 0x00 - }; - buffer[1] = new byte[28]; - buffer[2] = new byte[]{ - 0x00, 0x01, 0x00, 0x00, 0x01, 0x00,// default volume and rate - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,// reserved values - // default matrix - 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x40, 0x00, 0x00, 0x00 - }; - buffer[3] = new byte[24];// predefined - buffer[4] = ByteBuffer.allocate(4).putInt(infoTracks.length + 1).array(); - - long longestTrack = 0; - - for (Mp4Track track : infoTracks) { - long tmp = (long) ((track.trak.tkhd.duration / (double) track.trak.mdia_mdhd_timeScale) * DEFAULT_TIMESCALE); - if (tmp > longestTrack) { - longestTrack = tmp; - } - } - - ByteBuffer.wrap(buffer[1]) - .putLong(time) - .putLong(time) - .putInt(DEFAULT_TIMESCALE) - .putLong(longestTrack); - - return buffer; - } - - private byte[][] make_trak(int trackId, Trak trak) throws RuntimeException { - if (trak.tkhd.matrix.length != 36) { - throw new RuntimeException("bad track matrix length (expected 36)"); - } - - byte[][] buffer = new byte[DIMENSIONAL_FIVE][]; - - buffer[0] = new byte[]{ - 0x00, 0x00, 0x00, 0x00, 0x74, 0x72, 0x61, 0x6B,// trak header - 0x00, 0x00, 0x00, 0x68, 0x74, 0x6B, 0x68, 0x64, 0x01, 0x00, 0x00, 0x03 // tkhd header - }; - buffer[1] = new byte[48]; - buffer[2] = trak.tkhd.matrix; - buffer[3] = new byte[8]; - buffer[4] = trak.mdia; - - ByteBuffer set = ByteBuffer.wrap(buffer[1]); - set.putLong(time); - set.putLong(time); - set.putInt(trackId); - set.position(24); - set.putLong(trak.tkhd.duration); - set.position(40); - set.putShort(trak.tkhd.bLayer); - set.putShort(trak.tkhd.bAlternateGroup); - set.putShort(trak.tkhd.bVolume); - - ByteBuffer.wrap(buffer[3]) - .putInt(trak.tkhd.bWidth) - .putInt(trak.tkhd.bHeight); - - return lengthFor(buffer); - } - - private byte[][] make_moov() throws RuntimeException { - int pos = 1; - byte[][] buffer = new byte[2 + (DIMENSIONAL_TWO * infoTracks.length) + (DIMENSIONAL_FIVE * infoTracks.length) + DIMENSIONAL_FIVE + 1][]; - - buffer[0] = new byte[]{ - 0x00, 0x00, 0x00, 0x00, 0x6D, 0x6F, 0x6F, 0x76 - }; - - for (byte[] buff : make_mvhd()) { - buffer[pos++] = buff; - } - - for (int i = 0; i < infoTracks.length; i++) { - for (byte[] buff : make_trak(i + 1, infoTracks[i].trak)) { - buffer[pos++] = buff; - } - } - - buffer[pos] = new byte[]{ - 0x00, 0x00, 0x00, 0x00, 0x6D, 0x76, 0x65, 0x78 - }; - - ByteBuffer.wrap(buffer[pos++]).putInt((infoTracks.length * DEFAULT_TREX_SIZE) + 8); - - for (int i = 0; i < infoTracks.length; i++) { - for (byte[] buff : make_trex(i + 1, infoTracks[i].trex)) { - buffer[pos++] = buff; - } - } - - // default udta - buffer[pos] = new byte[]{ - 0x00, 0x00, 0x00, 0x5C, 0x75, 0x64, 0x74, 0x61, 0x00, 0x00, 0x00, 0x54, 0x6D, 0x65, 0x74, 0x61, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x68, 0x64, 0x6C, 0x72, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x6D, 0x64, 0x69, 0x72, 0x61, 0x70, 0x70, 0x6C, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x27, 0x69, 0x6C, 0x73, 0x74, 0x00, 0x00, 0x00, - 0x1F, (byte) 0xA9, 0x63, 0x6D, 0x74, 0x00, 0x00, 0x00, 0x17, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, - 0x01, 0x00, 0x00, 0x00, 0x00, - 0x4E, 0x65, 0x77, 0x50, 0x69, 0x70, 0x65// "NewPipe" binary string - }; - - return lengthFor(buffer); - } - - private byte[][] make_trex(int trackId, Trex trex) { - byte[][] buffer = new byte[][]{ - { - 0x00, 0x00, 0x00, 0x20, 0x74, 0x72, 0x65, 0x78, 0x00, 0x00, 0x00, 0x00 - }, - new byte[20] - }; - - ByteBuffer.wrap(buffer[1]) - .putInt(trackId) - .putInt(trex.defaultSampleDescriptionIndex) - .putInt(trex.defaultSampleDuration) - .putInt(trex.defaultSampleSize) - .putInt(trex.defaultSampleFlags); - - return buffer; - } - - private byte[][] make_tfra(int trackId, List times, List moofOffsets) { - int entryCount = times.size() - 1; - byte[][] buffer = new byte[DIMENSIONAL_TWO][]; - buffer[0] = new byte[]{ - 0x00, 0x00, 0x00, 0x00, 0x74, 0x66, 0x72, 0x61, 0x01, 0x00, 0x00, 0x00 - }; - buffer[1] = new byte[12 + ((16 + TFRA_TTS_DEFAULT.length) * entryCount)]; - - ByteBuffer set = ByteBuffer.wrap(buffer[1]); - set.putInt(trackId); - set.position(8); - set.putInt(entryCount); - - long decodeTime = 0; - - for (int i = 0; i < entryCount; i++) { - decodeTime += times.get(i); - set.putLong(decodeTime); - set.putLong(moofOffsets.get(i)); - set.put(TFRA_TTS_DEFAULT);// default values: traf number/trun number/sample number - } - - return lengthFor(buffer); - } - - private byte[][] make_mfra() { - byte[][] buffer = new byte[2 + (DIMENSIONAL_TWO * infoTracks.length)][]; - buffer[0] = new byte[]{ - 0x00, 0x00, 0x00, 0x00, 0x6D, 0x66, 0x72, 0x61 - }; - int pos = 1; - - for (int i = 0; i < infoTracks.length; i++) { - for (byte[] buff : make_tfra(i + 1, chunkTimes.get(i), moofOffsets)) { - buffer[pos++] = buff; - } - } - - buffer[pos] = new byte[]{// mfro - 0x00, 0x00, 0x00, 0x10, 0x6D, 0x66, 0x72, 0x6F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 - }; - - lengthFor(buffer); - - ByteBuffer set = ByteBuffer.wrap(buffer[pos]); - set.position(12); - set.put(buffer[0], 0, 4); - - return buffer; - - } - - private byte[][] make_sidx(int internalTrackId, long firstOffset) { - List times = chunkTimes.get(internalTrackId); - int count = times.size() - 1;// the first item is ignored (composition time) - - if (count > 65535) { - throw new OutOfMemoryError("to many fragments. sidx limit is 65535, found " + String.valueOf(count)); - } - - byte[][] buffer = new byte[][]{ - new byte[]{ - 0x00, 0x00, 0x00, 0x00, 0x73, 0x69, 0x64, 0x78, 0x01, 0x00, 0x00, 0x00 - }, - new byte[calcSidxBodySize(count)] - }; - - lengthFor(buffer); - - ByteBuffer set = ByteBuffer.wrap(buffer[1]); - set.putInt(internalTrackId + 1); - set.putInt(infoTracks[internalTrackId].trak.mdia_mdhd_timeScale); - set.putLong(0); - set.putLong(firstOffset - ByteBuffer.wrap(buffer[0]).getInt()); - set.putInt(0xFFFF & count);// unsigned - - int i = 0; - while (i < count) { - set.putInt(fragSizes.get(i) & 0x7fffffff);// default reference type is 0 - set.putInt(times.get(i + 1)); - set.putInt(0x90000000);// default SAP settings - i++; - } - - return buffer; - } - - private byte[][] make_free(int totalSize) { - return lengthFor(new byte[][]{ - new byte[]{0x00, 0x00, 0x00, 0x00, 0x66, 0x72, 0x65, 0x65}, - new byte[totalSize - 8]// this is waste of RAM - }); - - } - -// - - class TrunExtra { - - ByteBuffer byteBuffer; - } -} diff --git a/app/src/main/java/org/schabi/newpipe/streams/Mp4FromDashWriter.java b/app/src/main/java/org/schabi/newpipe/streams/Mp4FromDashWriter.java new file mode 100644 index 000000000..03aab447c --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/streams/Mp4FromDashWriter.java @@ -0,0 +1,810 @@ +package org.schabi.newpipe.streams; + +import org.schabi.newpipe.streams.Mp4DashReader.Hdlr; +import org.schabi.newpipe.streams.Mp4DashReader.Mdia; +import org.schabi.newpipe.streams.Mp4DashReader.Mp4DashChunk; +import org.schabi.newpipe.streams.Mp4DashReader.Mp4DashSample; +import org.schabi.newpipe.streams.Mp4DashReader.Mp4Track; +import org.schabi.newpipe.streams.Mp4DashReader.TrunEntry; +import org.schabi.newpipe.streams.io.SharpStream; + +import java.io.IOException; +import java.nio.ByteBuffer; + +/** + * @author kapodamy + */ +public class Mp4FromDashWriter { + + private final static int EPOCH_OFFSET = 2082844800; + private final static short DEFAULT_TIMESCALE = 1000; + private final static byte SAMPLES_PER_CHUNK_INIT = 2; + private final static byte SAMPLES_PER_CHUNK = 6;// ffmpeg uses 2, basic uses 1 (with 60fps uses 21 or 22). NewPipe will use 6 + private final static long THRESHOLD_FOR_CO64 = 0xFFFEFFFFL;// near 3.999 GiB + private final static int THRESHOLD_MOOV_LENGTH = (256 * 1024) + (2048 * 1024); // 2.2 MiB enough for: 1080p 60fps 00h35m00s + + private final long time; + + private ByteBuffer auxBuffer; + private SharpStream outStream; + + private long lastWriteOffset = -1; + private long writeOffset; + + private boolean moovSimulation = true; + + private boolean done = false; + private boolean parsed = false; + + private Mp4Track[] tracks; + private SharpStream[] sourceTracks; + + private Mp4DashReader[] readers; + private Mp4DashChunk[] readersChunks; + + private int overrideMainBrand = 0x00; + + public Mp4FromDashWriter(SharpStream... sources) throws IOException { + for (SharpStream src : sources) { + if (!src.canRewind() && !src.canRead()) { + throw new IOException("All sources must be readable and allow rewind"); + } + } + + sourceTracks = sources; + readers = new Mp4DashReader[sourceTracks.length]; + readersChunks = new Mp4DashChunk[readers.length]; + time = (System.currentTimeMillis() / 1000L) + EPOCH_OFFSET; + } + + public Mp4Track[] getTracksFromSource(int sourceIndex) throws IllegalStateException { + if (!parsed) { + throw new IllegalStateException("All sources must be parsed first"); + } + + return readers[sourceIndex].getAvailableTracks(); + } + + public void parseSources() throws IOException, IllegalStateException { + if (done) { + throw new IllegalStateException("already done"); + } + if (parsed) { + throw new IllegalStateException("already parsed"); + } + + try { + for (int i = 0; i < readers.length; i++) { + readers[i] = new Mp4DashReader(sourceTracks[i]); + readers[i].parse(); + } + + } finally { + parsed = true; + } + } + + public void selectTracks(int... trackIndex) throws IOException { + if (done) { + throw new IOException("already done"); + } + if (tracks != null) { + throw new IOException("tracks already selected"); + } + + try { + tracks = new Mp4Track[readers.length]; + for (int i = 0; i < readers.length; i++) { + tracks[i] = readers[i].selectTrack(trackIndex[i]); + } + } finally { + parsed = true; + } + } + + public void setMainBrand(int brandId) { + overrideMainBrand = brandId; + } + + public boolean isDone() { + return done; + } + + public boolean isParsed() { + return parsed; + } + + public void close() throws IOException { + done = true; + parsed = true; + + for (SharpStream src : sourceTracks) { + src.close(); + } + + tracks = null; + sourceTracks = null; + + readers = null; + readersChunks = null; + + auxBuffer = null; + outStream = null; + } + + public void build(SharpStream output) throws IOException { + if (done) { + throw new RuntimeException("already done"); + } + if (!output.canWrite()) { + throw new IOException("the provided output is not writable"); + } + + // + // WARNING: the muxer requires at least 8 samples of every track + // not allowed for very short tracks (less than 0.5 seconds) + // + outStream = output; + int read = 8;// mdat box header size + long totalSampleSize = 0; + int[] sampleExtra = new int[readers.length]; + int[] defaultMediaTime = new int[readers.length]; + int[] defaultSampleDuration = new int[readers.length]; + int[] sampleCount = new int[readers.length]; + + TablesInfo[] tablesInfo = new TablesInfo[tracks.length]; + for (int i = 0; i < tablesInfo.length; i++) { + tablesInfo[i] = new TablesInfo(); + } + + // + for (int i = 0; i < readers.length; i++) { + int samplesSize = 0; + int sampleSizeChanges = 0; + int compositionOffsetLast = -1; + + Mp4DashChunk chunk; + while ((chunk = readers[i].getNextChunk(true)) != null) { + + if (defaultMediaTime[i] < 1 && chunk.moof.traf.tfhd.defaultSampleDuration > 0) { + defaultMediaTime[i] = chunk.moof.traf.tfhd.defaultSampleDuration; + } + + read += chunk.moof.traf.trun.chunkSize; + sampleExtra[i] += chunk.moof.traf.trun.chunkDuration;// calculate track duration + + TrunEntry info; + while ((info = chunk.getNextSampleInfo()) != null) { + if (info.isKeyframe) { + tablesInfo[i].stss++; + } + + if (info.sampleDuration > defaultSampleDuration[i]) { + defaultSampleDuration[i] = info.sampleDuration; + } + + tablesInfo[i].stsz++; + if (samplesSize != info.sampleSize) { + samplesSize = info.sampleSize; + sampleSizeChanges++; + } + + if (info.hasCompositionTimeOffset) { + if (info.sampleCompositionTimeOffset != compositionOffsetLast) { + tablesInfo[i].ctts++; + compositionOffsetLast = info.sampleCompositionTimeOffset; + } + } + + totalSampleSize += info.sampleSize; + } + } + + if (defaultMediaTime[i] < 1) { + defaultMediaTime[i] = defaultSampleDuration[i]; + } + + readers[i].rewind(); + + int tmp = tablesInfo[i].stsz - SAMPLES_PER_CHUNK_INIT; + tablesInfo[i].stco = (tmp / SAMPLES_PER_CHUNK) + 1;// +1 for samples in first chunk + + tmp = tmp % SAMPLES_PER_CHUNK; + if (tmp == 0) { + tablesInfo[i].stsc = 2;// first chunk (init) and succesive chunks + tablesInfo[i].stsc_bEntries = new int[]{ + 1, SAMPLES_PER_CHUNK_INIT, 1, + 2, SAMPLES_PER_CHUNK, 1 + }; + } else { + tablesInfo[i].stsc = 3;// first chunk (init) and succesive chunks and remain chunk + tablesInfo[i].stsc_bEntries = new int[]{ + 1, SAMPLES_PER_CHUNK_INIT, 1, + 2, SAMPLES_PER_CHUNK, 1, + tablesInfo[i].stco + 1, tmp, 1 + }; + tablesInfo[i].stco++; + } + + sampleCount[i] = tablesInfo[i].stsz; + + if (sampleSizeChanges == 1) { + tablesInfo[i].stsz = 0; + tablesInfo[i].stsz_default = samplesSize; + } else { + tablesInfo[i].stsz_default = 0; + } + + if (tablesInfo[i].stss == tablesInfo[i].stsz) { + tablesInfo[i].stss = -1;// for audio tracks (all samples are keyframes) + } + + // ensure track duration + if (tracks[i].trak.tkhd.duration < 1) { + tracks[i].trak.tkhd.duration = sampleExtra[i];// this never should happen + } + } + // + + boolean is64 = read > THRESHOLD_FOR_CO64; + + // calculate the moov size; + int auxSize = make_moov(defaultMediaTime, tablesInfo, is64); + + if (auxSize < THRESHOLD_MOOV_LENGTH) { + auxBuffer = ByteBuffer.allocate(auxSize);// cache moov in the memory + } + + moovSimulation = false; + writeOffset = 0; + + final int ftyp_size = make_ftyp(); + + // reserve moov space in the output stream + /*if (outStream.canSetLength()) { + long length = writeOffset + auxSize; + outStream.setLength(length); + outSeek(length); + } else {*/ + if (auxSize > 0) { + int length = auxSize; + byte[] buffer = new byte[8 * 1024];// 8 KiB + while (length > 0) { + int count = Math.min(length, buffer.length); + outWrite(buffer, 0, count); + length -= count; + } + } + + if (auxBuffer == null) { + outSeek(ftyp_size); + } + + // tablesInfo contais row counts + // and after returning from make_moov() will contain table offsets + make_moov(defaultMediaTime, tablesInfo, is64); + + // write tables: stts stsc + // reset for ctts table: sampleCount sampleExtra + for (int i = 0; i < readers.length; i++) { + writeEntryArray(tablesInfo[i].stts, 2, sampleCount[i], defaultSampleDuration[i]); + writeEntryArray(tablesInfo[i].stsc, tablesInfo[i].stsc_bEntries.length, tablesInfo[i].stsc_bEntries); + tablesInfo[i].stsc_bEntries = null; + if (tablesInfo[i].ctts > 0) { + sampleCount[i] = 1;// index is not base zero + sampleExtra[i] = -1; + } + } + + if (auxBuffer == null) { + outRestore(); + } + + outWrite(make_mdat(totalSampleSize, is64)); + + int[] sampleIndex = new int[readers.length]; + int[] sizes = new int[SAMPLES_PER_CHUNK]; + int[] sync = new int[SAMPLES_PER_CHUNK]; + + int written = readers.length; + while (written > 0) { + written = 0; + + for (int i = 0; i < readers.length; i++) { + if (sampleIndex[i] < 0) { + continue;// track is done + } + + long chunkOffset = writeOffset; + int syncCount = 0; + int limit = sampleIndex[i] == 0 ? SAMPLES_PER_CHUNK_INIT : SAMPLES_PER_CHUNK; + + int j = 0; + for (; j < limit; j++) { + Mp4DashSample sample = getNextSample(i); + + if (sample == null) { + if (tablesInfo[i].ctts > 0 && sampleExtra[i] >= 0) { + writeEntryArray(tablesInfo[i].ctts, 1, sampleCount[i], sampleExtra[i]);// flush last entries + } + sampleIndex[i] = -1; + break; + } + + sampleIndex[i]++; + + if (tablesInfo[i].ctts > 0) { + if (sample.info.sampleCompositionTimeOffset == sampleExtra[i]) { + sampleCount[i]++; + } else { + if (sampleExtra[i] >= 0) { + tablesInfo[i].ctts = writeEntryArray(tablesInfo[i].ctts, 2, sampleCount[i], sampleExtra[i]); + outRestore(); + } + sampleCount[i] = 1; + sampleExtra[i] = sample.info.sampleCompositionTimeOffset; + } + } + + if (tablesInfo[i].stss > 0 && sample.info.isKeyframe) { + sync[syncCount++] = sampleIndex[i]; + } + + if (tablesInfo[i].stsz > 0) { + sizes[j] = sample.data.length; + } + + outWrite(sample.data, 0, sample.data.length); + } + + if (j > 0) { + written++; + + if (tablesInfo[i].stsz > 0) { + tablesInfo[i].stsz = writeEntryArray(tablesInfo[i].stsz, j, sizes); + } + + if (syncCount > 0) { + tablesInfo[i].stss = writeEntryArray(tablesInfo[i].stss, syncCount, sync); + } + + if (is64) { + tablesInfo[i].stco = writeEntry64(tablesInfo[i].stco, chunkOffset); + } else { + tablesInfo[i].stco = writeEntryArray(tablesInfo[i].stco, 1, (int) chunkOffset); + } + + outRestore(); + } + } + } + + if (auxBuffer != null) { + // dump moov + outSeek(ftyp_size); + outStream.write(auxBuffer.array(), 0, auxBuffer.capacity()); + auxBuffer = null; + } + } + + private Mp4DashSample getNextSample(int track) throws IOException { + if (readersChunks[track] == null) { + readersChunks[track] = readers[track].getNextChunk(false); + if (readersChunks[track] == null) { + return null;// EOF reached + } + } + + Mp4DashSample sample = readersChunks[track].getNextSample(); + if (sample == null) { + readersChunks[track] = null; + return getNextSample(track); + } else { + return sample; + } + } + + // + private int writeEntry64(int offset, long value) throws IOException { + outBackup(); + + auxSeek(offset); + auxWrite(ByteBuffer.allocate(8).putLong(value).array()); + + return offset + 8; + } + + private int writeEntryArray(int offset, int count, int... values) throws IOException { + outBackup(); + + auxSeek(offset); + + int size = count * 4; + ByteBuffer buffer = ByteBuffer.allocate(size); + + for (int i = 0; i < count; i++) { + buffer.putInt(values[i]); + } + + auxWrite(buffer.array()); + + return offset + size; + } + + private void outBackup() { + if (auxBuffer == null && lastWriteOffset < 0) { + lastWriteOffset = writeOffset; + } + } + + /** + * Restore to the previous position before the first call to writeEntry64() + * or writeEntryArray() methods. + */ + private void outRestore() throws IOException { + if (lastWriteOffset > 0) { + outSeek(lastWriteOffset); + lastWriteOffset = -1; + } + } + // + + // + private void outWrite(byte[] buffer) throws IOException { + outWrite(buffer, 0, buffer.length); + } + + private void outWrite(byte[] buffer, int offset, int count) throws IOException { + writeOffset += count; + outStream.write(buffer, offset, count); + } + + private void outSeek(long offset) throws IOException { + if (outStream.canSeek()) { + outStream.seek(offset); + writeOffset = offset; + } else if (outStream.canRewind()) { + outStream.rewind(); + writeOffset = 0; + outSkip(offset); + } else { + throw new IOException("cannot seek or rewind the output stream"); + } + } + + private void outSkip(long amount) throws IOException { + outStream.skip(amount); + writeOffset += amount; + } + + private int lengthFor(int offset) throws IOException { + int size = auxOffset() - offset; + + if (moovSimulation) { + return size; + } + + auxSeek(offset); + auxWrite(size); + auxSkip(size - 4); + + return size; + } + + private int make(int type, int extra, int columns, int rows) throws IOException { + final byte base = 16; + int size = columns * rows * 4; + int total = size + base; + int offset = auxOffset(); + + if (extra >= 0) { + total += 4; + } + + auxWrite(ByteBuffer.allocate(12) + .putInt(total) + .putInt(type) + .putInt(0x00)// default version & flags + .array() + ); + + if (extra >= 0) { + //size += 4;// commented for auxiliar buffer !!! + offset += 4; + auxWrite(extra); + } + + auxWrite(rows); + auxSkip(size); + + return offset + base; + } + + private void auxWrite(int value) throws IOException { + auxWrite(ByteBuffer.allocate(4) + .putInt(value) + .array() + ); + } + + private void auxWrite(byte[] buffer) throws IOException { + if (moovSimulation) { + writeOffset += buffer.length; + } else if (auxBuffer == null) { + outWrite(buffer, 0, buffer.length); + } else { + auxBuffer.put(buffer); + } + } + + private void auxSeek(int offset) throws IOException { + if (moovSimulation) { + writeOffset = offset; + } else if (auxBuffer == null) { + outSeek(offset); + } else { + auxBuffer.position(offset); + } + } + + private void auxSkip(int amount) throws IOException { + if (moovSimulation) { + writeOffset += amount; + } else if (auxBuffer == null) { + outSkip(amount); + } else { + auxBuffer.position(auxBuffer.position() + amount); + } + } + + private int auxOffset() { + return auxBuffer == null ? (int) writeOffset : auxBuffer.position(); + } + // + + // + private int make_ftyp() throws IOException { + byte[] buffer = new byte[]{ + 0x00, 0x00, 0x00, 0x1C, 0x66, 0x74, 0x79, 0x70,// ftyp + 0x6D, 0x70, 0x34, 0x32,// mayor brand (mp42) + 0x00, 0x00, 0x02, 0x00,// default minor version (512) + 0x6D, 0x70, 0x34, 0x31, 0x69, 0x73, 0x6F, 0x6D, 0x69, 0x73, 0x6F, 0x32// compatible brands: mp41 isom iso2 + }; + + if (overrideMainBrand != 0) + ByteBuffer.wrap(buffer).putInt(8, overrideMainBrand); + + outWrite(buffer); + + return buffer.length; + } + + private byte[] make_mdat(long refSize, boolean is64) { + if (is64) { + refSize += 16; + } else { + refSize += 8; + } + + ByteBuffer buffer = ByteBuffer.allocate(is64 ? 16 : 8) + .putInt(is64 ? 0x01 : (int) refSize) + .putInt(0x6D646174);// mdat + + if (is64) { + buffer.putLong(refSize); + } + + return buffer.array(); + } + + private void make_mvhd(long longestTrack) throws IOException { + auxWrite(new byte[]{ + 0x00, 0x00, 0x00, 0x78, 0x6D, 0x76, 0x68, 0x64, 0x01, 0x00, 0x00, 0x00 + }); + auxWrite(ByteBuffer.allocate(28) + .putLong(time) + .putLong(time) + .putInt(DEFAULT_TIMESCALE) + .putLong(longestTrack) + .array() + ); + + auxWrite(new byte[]{ + 0x00, 0x01, 0x00, 0x00, 0x01, 0x00,// default volume and rate + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,// reserved values + // default matrix + 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x40, 0x00, 0x00, 0x00 + }); + auxWrite(new byte[24]);// predefined + auxWrite(ByteBuffer.allocate(4) + .putInt(tracks.length + 1) + .array() + ); + } + + private int make_moov(int[] defaultMediaTime, TablesInfo[] tablesInfo, boolean is64) throws RuntimeException, IOException { + int start = auxOffset(); + + auxWrite(new byte[]{ + 0x00, 0x00, 0x00, 0x00, 0x6D, 0x6F, 0x6F, 0x76 + }); + + long longestTrack = 0; + long[] durations = new long[tracks.length]; + + for (int i = 0; i < durations.length; i++) { + durations[i] = (long) Math.ceil( + ((double) tracks[i].trak.tkhd.duration / tracks[i].trak.mdia.mdhd_timeScale) * DEFAULT_TIMESCALE + ); + + if (durations[i] > longestTrack) { + longestTrack = durations[i]; + } + } + + make_mvhd(longestTrack); + + for (int i = 0; i < tracks.length; i++) { + if (tracks[i].trak.tkhd.matrix.length != 36) { + throw new RuntimeException("bad track matrix length (expected 36) in track n°" + i); + } + make_trak(i, durations[i], defaultMediaTime[i], tablesInfo[i], is64); + } + + // udta/meta/ilst/©too + auxWrite(new byte[]{ + 0x00, 0x00, 0x00, 0x5C, 0x75, 0x64, 0x74, 0x61, 0x00, 0x00, 0x00, 0x54, 0x6D, 0x65, 0x74, 0x61, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x68, 0x64, 0x6C, 0x72, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x6D, 0x64, 0x69, 0x72, 0x61, 0x70, 0x70, 0x6C, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x27, 0x69, 0x6C, 0x73, 0x74, 0x00, 0x00, 0x00, + 0x1F, (byte) 0xA9, 0x74, 0x6F, 0x6F, 0x00, 0x00, 0x00, 0x17, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, + 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x4E, 0x65, 0x77, 0x50, 0x69, 0x70, 0x65// "NewPipe" binary string + }); + + return lengthFor(start); + } + + private void make_trak(int index, long duration, int defaultMediaTime, TablesInfo tables, boolean is64) throws IOException { + int start = auxOffset(); + + auxWrite(new byte[]{ + 0x00, 0x00, 0x00, 0x00, 0x74, 0x72, 0x61, 0x6B,// trak header + 0x00, 0x00, 0x00, 0x68, 0x74, 0x6B, 0x68, 0x64, 0x01, 0x00, 0x00, 0x03 // tkhd header + }); + + ByteBuffer buffer = ByteBuffer.allocate(48); + buffer.putLong(time); + buffer.putLong(time); + buffer.putInt(index + 1); + buffer.position(24); + buffer.putLong(duration); + buffer.position(40); + buffer.putShort(tracks[index].trak.tkhd.bLayer); + buffer.putShort(tracks[index].trak.tkhd.bAlternateGroup); + buffer.putShort(tracks[index].trak.tkhd.bVolume); + auxWrite(buffer.array()); + + auxWrite(tracks[index].trak.tkhd.matrix); + auxWrite(ByteBuffer.allocate(8) + .putInt(tracks[index].trak.tkhd.bWidth) + .putInt(tracks[index].trak.tkhd.bHeight) + .array() + ); + + auxWrite(new byte[]{ + 0x00, 0x00, 0x00, 0x24, 0x65, 0x64, 0x74, 0x73,// edts header + 0x00, 0x00, 0x00, 0x1C, 0x65, 0x6C, 0x73, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01// elst header + }); + + int bMediaRate; + int mediaTime; + + if (tracks[index].trak.edst_elst == null) { + // is a audio track ¿is edst/elst opcional for audio tracks? + mediaTime = 0x00;// ffmpeg set this value as zero, instead of defaultMediaTime + bMediaRate = 0x00010000; + } else { + mediaTime = (int) tracks[index].trak.edst_elst.MediaTime; + bMediaRate = tracks[index].trak.edst_elst.bMediaRate; + } + + auxWrite(ByteBuffer + .allocate(12) + .putInt((int) duration) + .putInt(mediaTime) + .putInt(bMediaRate) + .array() + ); + + make_mdia(tracks[index].trak.mdia, tables, is64); + + lengthFor(start); + } + + private void make_mdia(Mdia mdia, TablesInfo tablesInfo, boolean is64) throws IOException { + + int start_mdia = auxOffset(); + auxWrite(new byte[]{0x00, 0x00, 0x00, 0x00, 0x6D, 0x64, 0x69, 0x61});// mdia + auxWrite(mdia.mdhd); + auxWrite(make_hdlr(mdia.hdlr)); + + int start_minf = auxOffset(); + auxWrite(new byte[]{0x00, 0x00, 0x00, 0x00, 0x6D, 0x69, 0x6E, 0x66});// minf + auxWrite(mdia.minf.$mhd); + auxWrite(mdia.minf.dinf); + + int start_stbl = auxOffset(); + auxWrite(new byte[]{0x00, 0x00, 0x00, 0x00, 0x73, 0x74, 0x62, 0x6C});// stbl + auxWrite(mdia.minf.stbl_stsd); + + // + // In audio tracks the following tables is not required: ssts ctts + // And stsz can be empty if has a default sample size + // + if (moovSimulation) { + make(0x73747473, -1, 2, 1); + if (tablesInfo.stss > 0) { + make(0x73747373, -1, 1, tablesInfo.stss); + } + if (tablesInfo.ctts > 0) { + make(0x63747473, -1, 2, tablesInfo.ctts); + } + make(0x73747363, -1, 3, tablesInfo.stsc); + make(0x7374737A, tablesInfo.stsz_default, 1, tablesInfo.stsz); + make(is64 ? 0x636F3634 : 0x7374636F, -1, is64 ? 2 : 1, tablesInfo.stco); + } else { + tablesInfo.stts = make(0x73747473, -1, 2, 1); + if (tablesInfo.stss > 0) { + tablesInfo.stss = make(0x73747373, -1, 1, tablesInfo.stss); + } + if (tablesInfo.ctts > 0) { + tablesInfo.ctts = make(0x63747473, -1, 2, tablesInfo.ctts); + } + tablesInfo.stsc = make(0x73747363, -1, 3, tablesInfo.stsc); + tablesInfo.stsz = make(0x7374737A, tablesInfo.stsz_default, 1, tablesInfo.stsz); + tablesInfo.stco = make(is64 ? 0x636F3634 : 0x7374636F, -1, is64 ? 2 : 1, tablesInfo.stco); + } + + lengthFor(start_stbl); + lengthFor(start_minf); + lengthFor(start_mdia); + } + + private byte[] make_hdlr(Hdlr hdlr) { + ByteBuffer buffer = ByteBuffer.wrap(new byte[]{ + 0x00, 0x00, 0x00, 0x77, 0x68, 0x64, 0x6C, 0x72,// hdlr + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + // binary string "ISO Media file created in NewPipe (A libre lightweight streaming frontend for Android)." + 0x49, 0x53, 0x4F, 0x20, 0x4D, 0x65, 0x64, 0x69, 0x61, 0x20, 0x66, 0x69, 0x6C, 0x65, 0x20, 0x63, + 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x20, 0x69, 0x6E, 0x20, 0x4E, 0x65, 0x77, 0x50, 0x69, 0x70, + 0x65, 0x20, 0x28, 0x41, 0x20, 0x6C, 0x69, 0x62, 0x72, 0x65, 0x20, 0x6C, 0x69, 0x67, 0x68, 0x74, + 0x77, 0x65, 0x69, 0x67, 0x68, 0x74, 0x20, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6D, 0x69, 0x6E, 0x67, + 0x20, 0x66, 0x72, 0x6F, 0x6E, 0x74, 0x65, 0x6E, 0x64, 0x20, 0x66, 0x6F, 0x72, 0x20, 0x41, 0x6E, + 0x64, 0x72, 0x6F, 0x69, 0x64, 0x29, 0x2E + }); + + buffer.position(12); + buffer.putInt(hdlr.type); + buffer.putInt(hdlr.subType); + buffer.put(hdlr.bReserved);// always is a zero array + + return buffer.array(); + } + // + + class TablesInfo { + + public int stts; + public int stsc; + public int[] stsc_bEntries; + public int ctts; + public int stsz; + public int stsz_default; + public int stss; + public int stco; + } +} diff --git a/app/src/main/java/org/schabi/newpipe/streams/SubtitleConverter.java b/app/src/main/java/org/schabi/newpipe/streams/SubtitleConverter.java index 26aaf49a5..c41db4373 100644 --- a/app/src/main/java/org/schabi/newpipe/streams/SubtitleConverter.java +++ b/app/src/main/java/org/schabi/newpipe/streams/SubtitleConverter.java @@ -1,5 +1,6 @@ package org.schabi.newpipe.streams; +import org.schabi.newpipe.streams.io.SharpStream; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -12,8 +13,6 @@ import java.nio.charset.Charset; import java.text.ParseException; import java.util.Locale; -import org.schabi.newpipe.streams.io.SharpStream; - import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -27,11 +26,11 @@ public class SubtitleConverter { public void dumpTTML(SharpStream in, final SharpStream out, final boolean ignoreEmptyFrames, final boolean detectYoutubeDuplicateLines ) throws IOException, ParseException, SAXException, ParserConfigurationException, XPathExpressionException { - + final FrameWriter callback = new FrameWriter() { int frameIndex = 0; final Charset charset = Charset.forName("utf-8"); - + @Override public void yield(SubtitleFrame frame) throws IOException { if (ignoreEmptyFrames && frame.isEmptyText()) { @@ -48,13 +47,13 @@ public class SubtitleConverter { out.write(NEW_LINE.getBytes(charset)); } }; - + read_xml_based(in, callback, detectYoutubeDuplicateLines, "tt", "xmlns", "http://www.w3.org/ns/ttml", new String[]{"timedtext", "head", "wp"}, new String[]{"body", "div", "p"}, "begin", "end", true - ); + ); } private void read_xml_based(SharpStream source, FrameWriter callback, boolean detectYoutubeDuplicateLines, @@ -70,7 +69,7 @@ public class SubtitleConverter { * Language parsing is not supported */ - byte[] buffer = new byte[source.available()]; + byte[] buffer = new byte[(int) source.available()]; source.read(buffer); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); @@ -206,7 +205,7 @@ public class SubtitleConverter { } } - private static NodeList selectNodes(Document xml, String[] path, String namespaceUri) throws XPathExpressionException { + private static NodeList selectNodes(Document xml, String[] path, String namespaceUri) { Element ref = xml.getDocumentElement(); for (int i = 0; i < path.length - 1; i++) { diff --git a/app/src/main/java/org/schabi/newpipe/streams/TrackDataChunk.java b/app/src/main/java/org/schabi/newpipe/streams/TrackDataChunk.java deleted file mode 100644 index 86eb5ff4f..000000000 --- a/app/src/main/java/org/schabi/newpipe/streams/TrackDataChunk.java +++ /dev/null @@ -1,65 +0,0 @@ -package org.schabi.newpipe.streams; - -import java.io.InputStream; -import java.io.IOException; - -public class TrackDataChunk extends InputStream { - - private final DataReader base; - private int size; - - public TrackDataChunk(DataReader base, int size) { - this.base = base; - this.size = size; - } - - @Override - public int read() throws IOException { - if (size < 1) { - return -1; - } - - int res = base.read(); - - if (res >= 0) { - size--; - } - - return res; - } - - @Override - public int read(byte[] buffer) throws IOException { - return read(buffer, 0, buffer.length); - } - - @Override - public int read(byte[] buffer, int offset, int count) throws IOException { - count = Math.min(size, count); - int read = base.read(buffer, offset, count); - size -= count; - return read; - } - - @Override - public long skip(long amount) throws IOException { - long res = base.skipBytes(Math.min(amount, size)); - size -= res; - return res; - } - - @Override - public int available() { - return size; - } - - @Override - public void close() { - size = 0; - } - - @Override - public boolean markSupported() { - return false; - } -} diff --git a/app/src/main/java/org/schabi/newpipe/streams/WebMReader.java b/app/src/main/java/org/schabi/newpipe/streams/WebMReader.java index f61ef14c5..0c635ebe3 100644 --- a/app/src/main/java/org/schabi/newpipe/streams/WebMReader.java +++ b/app/src/main/java/org/schabi/newpipe/streams/WebMReader.java @@ -1,12 +1,13 @@ package org.schabi.newpipe.streams; +import org.schabi.newpipe.streams.io.SharpStream; + import java.io.EOFException; import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.NoSuchElementException; -import java.util.Objects; - -import org.schabi.newpipe.streams.io.SharpStream; /** * @@ -121,7 +122,7 @@ public class WebMReader { } private String readString(Element parent) throws IOException { - return new String(readBlob(parent), "utf-8"); + return new String(readBlob(parent), StandardCharsets.UTF_8);// or use "utf-8" } private byte[] readBlob(Element parent) throws IOException { @@ -193,6 +194,7 @@ public class WebMReader { return elem; } } + ensure(elem); } @@ -306,7 +308,7 @@ public class WebMReader { entry.trackNumber = readNumber(elem); break; case ID_TrackType: - entry.trackType = (int)readNumber(elem); + entry.trackType = (int) readNumber(elem); break; case ID_CodecID: entry.codecId = readString(elem); @@ -445,7 +447,7 @@ public class WebMReader { public class SimpleBlock { - public TrackDataChunk data; + public InputStream data; SimpleBlock(Element ref) { this.ref = ref; @@ -492,7 +494,7 @@ public class WebMReader { currentSimpleBlock = readSimpleBlock(elem); if (currentSimpleBlock.trackNumber == tracks[selectedTrack].trackNumber) { - currentSimpleBlock.data = new TrackDataChunk(stream, (int) currentSimpleBlock.dataSize); + currentSimpleBlock.data = stream.getView((int) currentSimpleBlock.dataSize); return currentSimpleBlock; } diff --git a/app/src/main/java/org/schabi/newpipe/streams/WebMWriter.java b/app/src/main/java/org/schabi/newpipe/streams/WebMWriter.java index ea038c607..e5881fd0b 100644 --- a/app/src/main/java/org/schabi/newpipe/streams/WebMWriter.java +++ b/app/src/main/java/org/schabi/newpipe/streams/WebMWriter.java @@ -1,20 +1,20 @@ package org.schabi.newpipe.streams; +import androidx.annotation.NonNull; + import org.schabi.newpipe.streams.WebMReader.Cluster; import org.schabi.newpipe.streams.WebMReader.Segment; import org.schabi.newpipe.streams.WebMReader.SimpleBlock; import org.schabi.newpipe.streams.WebMReader.WebMTrack; +import org.schabi.newpipe.streams.io.SharpStream; import java.io.IOException; import java.io.InputStream; -import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; -import org.schabi.newpipe.streams.io.SharpStream; - /** - * * @author kapodamy */ public class WebMWriter { @@ -94,10 +94,6 @@ public class WebMWriter { } } - public long getBytesWritten() { - return written; - } - public boolean isDone() { return done; } @@ -111,7 +107,7 @@ public class WebMWriter { parsed = true; for (SharpStream src : sourceTracks) { - src.dispose(); + src.close(); } sourceTracks = null; @@ -138,42 +134,42 @@ public class WebMWriter { /* segment */ listBuffer.add(new byte[]{ - 0x18, 0x53, (byte) 0x80, 0x67, 0x01, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00// segment content size + 0x18, 0x53, (byte) 0x80, 0x67, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00// segment content size }); long baseSegmentOffset = written + listBuffer.get(0).length; /* seek head */ listBuffer.add(new byte[]{ - 0x11, 0x4d, (byte) 0x9b, 0x74, (byte) 0xbe, - 0x4d, (byte) 0xbb, (byte) 0x8b, - 0x53, (byte) 0xab, (byte) 0x84, 0x15, 0x49, (byte) 0xa9, 0x66, 0x53, - (byte) 0xac, (byte) 0x81, /*info offset*/ 0x43, - 0x4d, (byte) 0xbb, (byte) 0x8b, 0x53, (byte) 0xab, - (byte) 0x84, 0x16, 0x54, (byte) 0xae, 0x6b, 0x53, (byte) 0xac, (byte) 0x81, - /*tracks offset*/ 0x6a, - 0x4d, (byte) 0xbb, (byte) 0x8e, 0x53, (byte) 0xab, (byte) 0x84, 0x1f, - 0x43, (byte) 0xb6, 0x75, 0x53, (byte) 0xac, (byte) 0x84, /*cluster offset [2]*/ 0x00, 0x00, 0x00, 0x00, - 0x4d, (byte) 0xbb, (byte) 0x8e, 0x53, (byte) 0xab, (byte) 0x84, 0x1c, 0x53, - (byte) 0xbb, 0x6b, 0x53, (byte) 0xac, (byte) 0x84, /*cues offset [7]*/ 0x00, 0x00, 0x00, 0x00 + 0x11, 0x4d, (byte) 0x9b, 0x74, (byte) 0xbe, + 0x4d, (byte) 0xbb, (byte) 0x8b, + 0x53, (byte) 0xab, (byte) 0x84, 0x15, 0x49, (byte) 0xa9, 0x66, 0x53, + (byte) 0xac, (byte) 0x81, /*info offset*/ 0x43, + 0x4d, (byte) 0xbb, (byte) 0x8b, 0x53, (byte) 0xab, + (byte) 0x84, 0x16, 0x54, (byte) 0xae, 0x6b, 0x53, (byte) 0xac, (byte) 0x81, + /*tracks offset*/ 0x6a, + 0x4d, (byte) 0xbb, (byte) 0x8e, 0x53, (byte) 0xab, (byte) 0x84, 0x1f, + 0x43, (byte) 0xb6, 0x75, 0x53, (byte) 0xac, (byte) 0x84, /*cluster offset [2]*/ 0x00, 0x00, 0x00, 0x00, + 0x4d, (byte) 0xbb, (byte) 0x8e, 0x53, (byte) 0xab, (byte) 0x84, 0x1c, 0x53, + (byte) 0xbb, 0x6b, 0x53, (byte) 0xac, (byte) 0x84, /*cues offset [7]*/ 0x00, 0x00, 0x00, 0x00 }); /* info */ listBuffer.add(new byte[]{ - 0x15, 0x49, (byte) 0xa9, 0x66, (byte) 0xa2, 0x2a, (byte) 0xd7, (byte) 0xb1 + 0x15, 0x49, (byte) 0xa9, 0x66, (byte) 0xa2, 0x2a, (byte) 0xd7, (byte) 0xb1 }); listBuffer.add(encode(DEFAULT_TIMECODE_SCALE, true));// this value MUST NOT exceed 4 bytes listBuffer.add(new byte[]{0x44, (byte) 0x89, (byte) 0x84, - 0x00, 0x00, 0x00, 0x00,// info.duration - - /* MuxingApp */ - 0x4d, (byte) 0x80, (byte) 0x87, 0x4E, - 0x65, 0x77, 0x50, 0x69, 0x70, 0x65, // "NewPipe" binary string - - /* WritingApp */ - 0x57, 0x41, (byte) 0x87, 0x4E, - 0x65, 0x77, 0x50, 0x69, 0x70, 0x65// "NewPipe" binary string + 0x00, 0x00, 0x00, 0x00,// info.duration + + /* MuxingApp */ + 0x4d, (byte) 0x80, (byte) 0x87, 0x4E, + 0x65, 0x77, 0x50, 0x69, 0x70, 0x65, // "NewPipe" binary string + + /* WritingApp */ + 0x57, 0x41, (byte) 0x87, 0x4E, + 0x65, 0x77, 0x50, 0x69, 0x70, 0x65// "NewPipe" binary string }); /* tracks */ @@ -200,7 +196,6 @@ public class WebMWriter { long nextCueTime = infoTracks[cuesForTrackId].trackType == 1 ? -1 : 0; ArrayList keyFrames = new ArrayList<>(32); - //ArrayList chunks = new ArrayList<>(readers.length); ArrayList clusterOffsets = new ArrayList<>(32); ArrayList clusterSizes = new ArrayList<>(32); @@ -283,24 +278,21 @@ public class WebMWriter { long segmentSize = written - offsetSegmentSizeSet - 7; - // final step write offsets and sizes - out.rewind(); - written = 0; - - skipTo(out, offsetSegmentSizeSet); + /* ---- final step write offsets and sizes ---- */ + seekTo(out, offsetSegmentSizeSet); writeLong(out, segmentSize); if (predefinedDurations[durationFromTrackId] > -1) { duration += predefinedDurations[durationFromTrackId];// this value is full-filled in makeTrackEntry() method } - skipTo(out, offsetInfoDurationSet); + seekTo(out, offsetInfoDurationSet); writeFloat(out, duration); firstClusterOffset -= baseSegmentOffset; - skipTo(out, offsetClusterSet); + seekTo(out, offsetClusterSet); writeInt(out, firstClusterOffset); - skipTo(out, cueReservedOffset); + seekTo(out, cueReservedOffset); /* Cue */ dump(new byte[]{0x1c, 0x53, (byte) 0xbb, 0x6b, 0x20, 0x00, 0x00}, out); @@ -321,20 +313,16 @@ public class WebMWriter { voidBuffer.putShort((short) (firstClusterOffset - written - 4)); dump(voidBuffer.array(), out); - out.rewind(); - written = 0; - - skipTo(out, offsetCuesSet); + seekTo(out, offsetCuesSet); writeInt(out, (int) (cueReservedOffset - baseSegmentOffset)); - skipTo(out, cueReservedOffset + 5); + seekTo(out, cueReservedOffset + 5); writeShort(out, cueSize); for (int i = 0; i < clusterSizes.size(); i++) { - skipTo(out, clusterOffsets.get(i)); - byte[] size = ByteBuffer.allocate(4).putInt(clusterSizes.get(i) | 0x200000).array(); - out.write(size, 1, 3); - written += 3; + seekTo(out, clusterOffsets.get(i)); + byte[] buffer = ByteBuffer.allocate(4).putInt(clusterSizes.get(i) | 0x10000000).array(); + dump(buffer, out); } } @@ -365,20 +353,29 @@ public class WebMWriter { bloq.dataSize = (int) res.dataSize; bloq.trackNumber = internalTrackId; bloq.flags = res.flags; - bloq.absoluteTimecode = convertTimecode(res.relativeTimeCode, readersSegment[internalTrackId].info.timecodeScale, DEFAULT_TIMECODE_SCALE); + bloq.absoluteTimecode = convertTimecode(res.relativeTimeCode, readersSegment[internalTrackId].info.timecodeScale); bloq.absoluteTimecode += readersCluter[internalTrackId].timecode; return bloq; } - private short convertTimecode(int time, long oldTimeScale, int newTimeScale) { - return (short) (time * (newTimeScale / oldTimeScale)); + private short convertTimecode(int time, long oldTimeScale) { + return (short) (time * (DEFAULT_TIMECODE_SCALE / oldTimeScale)); } - private void skipTo(SharpStream stream, long absoluteOffset) throws IOException { - absoluteOffset -= written; - written += absoluteOffset; - stream.skip(absoluteOffset); + private void seekTo(SharpStream stream, long offset) throws IOException { + if (stream.canSeek()) { + stream.seek(offset); + } else { + if (offset > written) { + stream.skip(offset - written); + } else { + stream.rewind(); + stream.skip(offset); + } + } + + written = offset; } private void writeLong(SharpStream stream, long number) throws IOException { @@ -453,7 +450,7 @@ public class WebMWriter { /* cluster */ dump(new byte[]{0x1f, 0x43, (byte) 0xb6, 0x75}, stream); clusterOffsets.add(written);// warning: max cluster size is 256 MiB - dump(new byte[]{0x20, 0x00, 0x00}, stream); + dump(new byte[]{0x10, 0x00, 0x00, 0x00}, stream); startOffset = written;// size for the this cluster @@ -468,12 +465,12 @@ public class WebMWriter { private void makeEBML(SharpStream stream) throws IOException { // deafult values dump(new byte[]{ - 0x1A, 0x45, (byte) 0xDF, (byte) 0xA3, 0x01, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x1F, 0x42, (byte) 0x86, (byte) 0x81, 0x01, - 0x42, (byte) 0xF7, (byte) 0x81, 0x01, 0x42, (byte) 0xF2, (byte) 0x81, 0x04, - 0x42, (byte) 0xF3, (byte) 0x81, 0x08, 0x42, (byte) 0x82, (byte) 0x84, 0x77, - 0x65, 0x62, 0x6D, 0x42, (byte) 0x87, (byte) 0x81, 0x02, - 0x42, (byte) 0x85, (byte) 0x81, 0x02 + 0x1A, 0x45, (byte) 0xDF, (byte) 0xA3, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x1F, 0x42, (byte) 0x86, (byte) 0x81, 0x01, + 0x42, (byte) 0xF7, (byte) 0x81, 0x01, 0x42, (byte) 0xF2, (byte) 0x81, 0x04, + 0x42, (byte) 0xF3, (byte) 0x81, 0x08, 0x42, (byte) 0x82, (byte) 0x84, 0x77, + 0x65, 0x62, 0x6D, 0x42, (byte) 0x87, (byte) 0x81, 0x02, + 0x42, (byte) 0x85, (byte) 0x81, 0x02 }, stream); } @@ -618,9 +615,10 @@ public class WebMWriter { int offset = withLength ? 1 : 0; byte[] buffer = new byte[offset + length]; - long marker = (long) Math.floor((length - 1) / 8); + long marker = (long) Math.floor((length - 1f) / 8f); - for (int i = length - 1, mul = 1; i >= 0; i--, mul *= 0x100) { + float mul = 1; + for (int i = length - 1; i >= 0; i--, mul *= 0x100) { long b = (long) Math.floor(number / mul); if (!withLength && i == marker) { b = b | (0x80 >> (length - 1)); @@ -637,11 +635,7 @@ public class WebMWriter { private ArrayList encode(String value) { byte[] str; - try { - str = value.getBytes("utf-8"); - } catch (UnsupportedEncodingException err) { - str = value.getBytes(); - } + str = value.getBytes(StandardCharsets.UTF_8);// or use "utf-8" ArrayList buffer = new ArrayList<>(2); buffer.add(encode(str.length, false)); @@ -720,9 +714,10 @@ public class WebMWriter { return (flags & 0x80) == 0x80; } + @NonNull @Override public String toString() { - return String.format("trackNumber=%s isKeyFrame=%S absoluteTimecode=%s", trackNumber, (flags & 0x80) == 0x80, absoluteTimecode); + return String.format("trackNumber=%s isKeyFrame=%S absoluteTimecode=%s", trackNumber, isKeyframe(), absoluteTimecode); } } } diff --git a/app/src/main/java/org/schabi/newpipe/streams/io/SharpStream.java b/app/src/main/java/org/schabi/newpipe/streams/io/SharpStream.java index 48bea06f6..5950ba3dd 100644 --- a/app/src/main/java/org/schabi/newpipe/streams/io/SharpStream.java +++ b/app/src/main/java/org/schabi/newpipe/streams/io/SharpStream.java @@ -1,11 +1,12 @@ package org.schabi.newpipe.streams.io; +import java.io.Closeable; import java.io.IOException; /** - * based c# + * based on c# */ -public abstract class SharpStream { +public abstract class SharpStream implements Closeable { public abstract int read() throws IOException; @@ -15,16 +16,14 @@ public abstract class SharpStream { public abstract long skip(long amount) throws IOException; - - public abstract int available(); + public abstract long available(); public abstract void rewind() throws IOException; + public abstract boolean isClosed(); - public abstract void dispose(); - - public abstract boolean isDisposed(); - + @Override + public abstract void close(); public abstract boolean canRewind(); @@ -32,6 +31,13 @@ public abstract class SharpStream { public abstract boolean canWrite(); + public boolean canSetLength() { + return false; + } + + public boolean canSeek() { + return false; + } public abstract void write(byte value) throws IOException; @@ -39,9 +45,19 @@ public abstract class SharpStream { public abstract void write(byte[] buffer, int offset, int count) throws IOException; - public abstract void flush() throws IOException; + public void flush() throws IOException { + // STUB + } public void setLength(long length) throws IOException { throw new IOException("Not implemented"); } + + public void seek(long offset) throws IOException { + throw new IOException("Not implemented"); + } + + public long length() throws IOException { + throw new UnsupportedOperationException("Unsupported operation"); + } } diff --git a/app/src/main/java/org/schabi/newpipe/util/AnimationUtils.java b/app/src/main/java/org/schabi/newpipe/util/AnimationUtils.java index 6a398a8a2..e47e14483 100644 --- a/app/src/main/java/org/schabi/newpipe/util/AnimationUtils.java +++ b/app/src/main/java/org/schabi/newpipe/util/AnimationUtils.java @@ -24,10 +24,10 @@ import android.animation.AnimatorListenerAdapter; import android.animation.ArgbEvaluator; import android.animation.ValueAnimator; import android.content.res.ColorStateList; -import android.support.annotation.ColorInt; -import android.support.annotation.FloatRange; -import android.support.v4.view.ViewCompat; -import android.support.v4.view.animation.FastOutSlowInInterpolator; +import androidx.annotation.ColorInt; +import androidx.annotation.FloatRange; +import androidx.core.view.ViewCompat; +import androidx.interpolator.view.animation.FastOutSlowInInterpolator; import android.util.Log; import android.view.View; import android.widget.TextView; diff --git a/app/src/main/java/org/schabi/newpipe/util/CommentTextOnTouchListener.java b/app/src/main/java/org/schabi/newpipe/util/CommentTextOnTouchListener.java index 570b5f8b2..ac79fee23 100644 --- a/app/src/main/java/org/schabi/newpipe/util/CommentTextOnTouchListener.java +++ b/app/src/main/java/org/schabi/newpipe/util/CommentTextOnTouchListener.java @@ -31,7 +31,7 @@ public class CommentTextOnTouchListener implements View.OnTouchListener { public static final CommentTextOnTouchListener INSTANCE = new CommentTextOnTouchListener(); - private static final Pattern timestampPattern = Pattern.compile(".*&t=(\\d+)"); + private static final Pattern timestampPattern = Pattern.compile("(.*)#timestamp=(\\d+)"); @Override public boolean onTouch(View v, MotionEvent event) { @@ -86,6 +86,12 @@ public class CommentTextOnTouchListener implements View.OnTouchListener { private boolean handleUrl(Context context, URLSpan urlSpan) { String url = urlSpan.getURL(); + int seconds = -1; + Matcher matcher = timestampPattern.matcher(url); + if(matcher.matches()){ + url = matcher.group(1); + seconds = Integer.parseInt(matcher.group(2)); + } StreamingService service; StreamingService.LinkType linkType; try { @@ -97,9 +103,7 @@ public class CommentTextOnTouchListener implements View.OnTouchListener { if(linkType == StreamingService.LinkType.NONE){ return false; } - Matcher matcher = timestampPattern.matcher(url); - if(linkType == StreamingService.LinkType.STREAM && matcher.matches()){ - int seconds = Integer.parseInt(matcher.group(1)); + if(linkType == StreamingService.LinkType.STREAM && seconds != -1){ return playOnPopup(context, url, service, seconds); }else{ NavigationHelper.openRouterActivity(context, url); @@ -119,9 +123,8 @@ public class CommentTextOnTouchListener implements View.OnTouchListener { single.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(info -> { - PlayQueue playQueue = new SinglePlayQueue((StreamInfo) info); - ((StreamInfo) info).setStartPosition(seconds); - NavigationHelper.enqueueOnPopupPlayer(context, playQueue, true); + PlayQueue playQueue = new SinglePlayQueue((StreamInfo) info, seconds*1000); + NavigationHelper.playOnPopupPlayer(context, playQueue, false); }); return true; } diff --git a/app/src/main/java/org/schabi/newpipe/util/ExtractorHelper.java b/app/src/main/java/org/schabi/newpipe/util/ExtractorHelper.java index 0f1c39473..c4471942e 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ExtractorHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ExtractorHelper.java @@ -228,6 +228,10 @@ public final class ExtractorHelper { }); } + public static boolean isCached(final int serviceId, final String url, InfoItem.InfoType infoType) { + return null != loadFromCache(serviceId, url, infoType).blockingGet(); + } + /** * A simple and general error handler that show a Toast for known exceptions, and for others, opens the report error activity with the (optional) error message. */ @@ -243,8 +247,6 @@ public final class ExtractorHelper { context.startActivity(intent); } else if (exception instanceof IOException) { Toast.makeText(context, R.string.network_error, Toast.LENGTH_LONG).show(); - } else if (exception instanceof YoutubeStreamExtractor.GemaException) { - Toast.makeText(context, R.string.blocked_by_gema, Toast.LENGTH_LONG).show(); } else if (exception instanceof ContentNotAvailableException) { Toast.makeText(context, R.string.content_not_available, Toast.LENGTH_LONG).show(); } else { diff --git a/app/src/main/java/org/schabi/newpipe/util/FallbackViewHolder.java b/app/src/main/java/org/schabi/newpipe/util/FallbackViewHolder.java index f0cdfdf99..bfe0ae5c5 100644 --- a/app/src/main/java/org/schabi/newpipe/util/FallbackViewHolder.java +++ b/app/src/main/java/org/schabi/newpipe/util/FallbackViewHolder.java @@ -1,6 +1,6 @@ package org.schabi.newpipe.util; -import android.support.v7.widget.RecyclerView; +import androidx.recyclerview.widget.RecyclerView; import android.view.View; public class FallbackViewHolder extends RecyclerView.ViewHolder { diff --git a/app/src/main/java/org/schabi/newpipe/util/FilePickerActivityHelper.java b/app/src/main/java/org/schabi/newpipe/util/FilePickerActivityHelper.java index 20554ce59..420322c27 100644 --- a/app/src/main/java/org/schabi/newpipe/util/FilePickerActivityHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/FilePickerActivityHelper.java @@ -2,13 +2,14 @@ package org.schabi.newpipe.util; import android.content.Context; import android.content.Intent; +import android.net.Uri; import android.os.Bundle; import android.os.Environment; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.content.Loader; -import android.support.v7.util.SortedList; -import android.support.v7.widget.RecyclerView; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.loader.content.Loader; +import androidx.recyclerview.widget.SortedList; +import androidx.recyclerview.widget.RecyclerView; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; @@ -29,7 +30,7 @@ public class FilePickerActivityHelper extends com.nononsenseapps.filepicker.File @Override public void onCreate(Bundle savedInstanceState) { - if(ThemeHelper.isLightThemeSelected(this)) { + if (ThemeHelper.isLightThemeSelected(this)) { this.setTheme(R.style.FilePickerThemeLight); } else { this.setTheme(R.style.FilePickerThemeDark); @@ -73,6 +74,11 @@ public class FilePickerActivityHelper extends com.nononsenseapps.filepicker.File .putExtra(FilePickerActivityHelper.EXTRA_MODE, FilePickerActivityHelper.MODE_NEW_FILE); } + public static boolean isOwnFileUri(@NonNull Context context, @NonNull Uri uri) { + if (uri.getAuthority() == null) return false; + return uri.getAuthority().startsWith(context.getPackageName()); + } + /*////////////////////////////////////////////////////////////////////////// // Internal //////////////////////////////////////////////////////////////////////////*/ @@ -109,6 +115,12 @@ public class FilePickerActivityHelper extends com.nononsenseapps.filepicker.File super.onClickOk(view); } + @Override + protected boolean isItemVisible(@NonNull File file) { + if (file.isDirectory() && file.isHidden()) return true; + return super.isItemVisible(file); + } + public File getBackTop() { if (getArguments() == null) return Environment.getExternalStorageDirectory(); diff --git a/app/src/main/java/org/schabi/newpipe/util/FilenameUtils.java b/app/src/main/java/org/schabi/newpipe/util/FilenameUtils.java index b874a9eca..37d94cd16 100644 --- a/app/src/main/java/org/schabi/newpipe/util/FilenameUtils.java +++ b/app/src/main/java/org/schabi/newpipe/util/FilenameUtils.java @@ -10,6 +10,9 @@ import java.util.regex.Pattern; public class FilenameUtils { + private static final String CHARSET_MOST_SPECIAL = "[\\n\\r|?*<\":\\\\>/']+"; + private static final String CHARSET_ONLY_LETTERS_AND_DIGITS = "[^\\w\\d]+"; + /** * #143 #44 #42 #22: make sure that the filename does not contain illegal chars. * @param context the context to retrieve strings and preferences from @@ -18,11 +21,28 @@ public class FilenameUtils { */ public static String createFilename(Context context, String title) { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); - final String key = context.getString(R.string.settings_file_charset_key); - final String value = sharedPreferences.getString(key, context.getString(R.string.default_file_charset_value)); - Pattern pattern = Pattern.compile(value); + + final String charset_ld = context.getString(R.string.charset_letters_and_digits_value); + final String charset_ms = context.getString(R.string.charset_most_special_value); + final String defaultCharset = context.getString(R.string.default_file_charset_value); final String replacementChar = sharedPreferences.getString(context.getString(R.string.settings_file_replacement_character_key), "_"); + String selectedCharset = sharedPreferences.getString(context.getString(R.string.settings_file_charset_key), null); + + final String charset; + + if (selectedCharset == null || selectedCharset.isEmpty()) selectedCharset = defaultCharset; + + if (selectedCharset.equals(charset_ld)) { + charset = CHARSET_ONLY_LETTERS_AND_DIGITS; + } else if (selectedCharset.equals(charset_ms)) { + charset = CHARSET_MOST_SPECIAL; + } else { + charset = selectedCharset;// ¿is the user using a custom charset? + } + + Pattern pattern = Pattern.compile(charset); + return createFilename(title, pattern, replacementChar); } diff --git a/app/src/main/java/org/schabi/newpipe/util/InfoCache.java b/app/src/main/java/org/schabi/newpipe/util/InfoCache.java index 23b134281..afb7604c5 100644 --- a/app/src/main/java/org/schabi/newpipe/util/InfoCache.java +++ b/app/src/main/java/org/schabi/newpipe/util/InfoCache.java @@ -19,9 +19,9 @@ package org.schabi.newpipe.util; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.util.LruCache; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.collection.LruCache; import android.util.Log; import org.schabi.newpipe.MainActivity; diff --git a/app/src/main/java/org/schabi/newpipe/util/LayoutManagerSmoothScroller.java b/app/src/main/java/org/schabi/newpipe/util/LayoutManagerSmoothScroller.java index 9eca2d610..df7549c47 100644 --- a/app/src/main/java/org/schabi/newpipe/util/LayoutManagerSmoothScroller.java +++ b/app/src/main/java/org/schabi/newpipe/util/LayoutManagerSmoothScroller.java @@ -2,9 +2,9 @@ package org.schabi.newpipe.util; import android.content.Context; import android.graphics.PointF; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.LinearSmoothScroller; -import android.support.v7.widget.RecyclerView; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.LinearSmoothScroller; +import androidx.recyclerview.widget.RecyclerView; public class LayoutManagerSmoothScroller extends LinearLayoutManager { diff --git a/app/src/main/java/org/schabi/newpipe/util/ListHelper.java b/app/src/main/java/org/schabi/newpipe/util/ListHelper.java index 8fc423837..eb950b1ed 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ListHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ListHelper.java @@ -4,7 +4,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.net.ConnectivityManager; import android.preference.PreferenceManager; -import android.support.annotation.StringRes; +import androidx.annotation.StringRes; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.MediaFormat; @@ -430,24 +430,26 @@ public final class ListHelper { */ private static String getResolutionLimit(Context context) { String resolutionLimit = null; - if (!isWifiActive(context)) { + if (isMeteredNetwork(context)) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); String defValue = context.getString(R.string.limit_data_usage_none_key); String value = preferences.getString( context.getString(R.string.limit_mobile_data_usage_key), defValue); - resolutionLimit = value.equals(defValue) ? null : value; + resolutionLimit = defValue.equals(value) ? null : value; } return resolutionLimit; } /** - * Are we connected to wifi? + * The current network is metered (like mobile data)? * @param context App context - * @return {@code true} if connected to wifi + * @return {@code true} if connected to a metered network */ - private static boolean isWifiActive(Context context) + private static boolean isMeteredNetwork(Context context) { ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); - return manager != null && manager.getActiveNetworkInfo() != null && manager.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI; + if (manager == null || manager.getActiveNetworkInfo() == null) return false; + + return manager.isActiveNetworkMetered(); } } diff --git a/app/src/main/java/org/schabi/newpipe/util/Localization.java b/app/src/main/java/org/schabi/newpipe/util/Localization.java index eed1a8ae2..08c9c6d98 100644 --- a/app/src/main/java/org/schabi/newpipe/util/Localization.java +++ b/app/src/main/java/org/schabi/newpipe/util/Localization.java @@ -4,9 +4,9 @@ import android.content.Context; import android.content.SharedPreferences; import android.content.res.Resources; import android.preference.PreferenceManager; -import android.support.annotation.NonNull; -import android.support.annotation.PluralsRes; -import android.support.annotation.StringRes; +import androidx.annotation.NonNull; +import androidx.annotation.PluralsRes; +import androidx.annotation.StringRes; import android.text.TextUtils; import org.schabi.newpipe.R; diff --git a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java index 98ae3a88a..e2b03c8e8 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java @@ -8,12 +8,12 @@ import android.content.Intent; import android.net.Uri; import android.os.Build; import android.preference.PreferenceManager; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentTransaction; -import android.support.v7.app.AlertDialog; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentTransaction; +import androidx.appcompat.app.AlertDialog; import android.util.Log; import android.widget.Toast; @@ -69,12 +69,14 @@ public class NavigationHelper { public static Intent getPlayerIntent(@NonNull final Context context, @NonNull final Class targetClazz, @NonNull final PlayQueue playQueue, - @Nullable final String quality) { + @Nullable final String quality, + final boolean resumePlayback) { Intent intent = new Intent(context, targetClazz); final String cacheKey = SerializedCache.getInstance().put(playQueue, PlayQueue.class); if (cacheKey != null) intent.putExtra(VideoPlayer.PLAY_QUEUE_KEY, cacheKey); if (quality != null) intent.putExtra(VideoPlayer.PLAYBACK_QUALITY, quality); + intent.putExtra(VideoPlayer.RESUME_PLAYBACK, resumePlayback); return intent; } @@ -82,16 +84,18 @@ public class NavigationHelper { @NonNull public static Intent getPlayerIntent(@NonNull final Context context, @NonNull final Class targetClazz, - @NonNull final PlayQueue playQueue) { - return getPlayerIntent(context, targetClazz, playQueue, null); + @NonNull final PlayQueue playQueue, + final boolean resumePlayback) { + return getPlayerIntent(context, targetClazz, playQueue, null, resumePlayback); } @NonNull public static Intent getPlayerEnqueueIntent(@NonNull final Context context, @NonNull final Class targetClazz, @NonNull final PlayQueue playQueue, - final boolean selectOnAppend) { - return getPlayerIntent(context, targetClazz, playQueue) + final boolean selectOnAppend, + final boolean resumePlayback) { + return getPlayerIntent(context, targetClazz, playQueue, resumePlayback) .putExtra(BasePlayer.APPEND_ONLY, true) .putExtra(BasePlayer.SELECT_ON_APPEND, selectOnAppend); } @@ -104,40 +108,41 @@ public class NavigationHelper { final float playbackSpeed, final float playbackPitch, final boolean playbackSkipSilence, - @Nullable final String playbackQuality) { - return getPlayerIntent(context, targetClazz, playQueue, playbackQuality) + @Nullable final String playbackQuality, + final boolean resumePlayback) { + return getPlayerIntent(context, targetClazz, playQueue, playbackQuality, resumePlayback) .putExtra(BasePlayer.REPEAT_MODE, repeatMode) .putExtra(BasePlayer.PLAYBACK_SPEED, playbackSpeed) .putExtra(BasePlayer.PLAYBACK_PITCH, playbackPitch) .putExtra(BasePlayer.PLAYBACK_SKIP_SILENCE, playbackSkipSilence); } - public static void playOnMainPlayer(final Context context, final PlayQueue queue) { - final Intent playerIntent = getPlayerIntent(context, MainVideoPlayer.class, queue); + public static void playOnMainPlayer(final Context context, final PlayQueue queue, final boolean resumePlayback) { + final Intent playerIntent = getPlayerIntent(context, MainVideoPlayer.class, queue, resumePlayback); playerIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(playerIntent); } - public static void playOnPopupPlayer(final Context context, final PlayQueue queue) { + public static void playOnPopupPlayer(final Context context, final PlayQueue queue, final boolean resumePlayback) { if (!PermissionHelper.isPopupEnabled(context)) { PermissionHelper.showPopupEnablementToast(context); return; } Toast.makeText(context, R.string.popup_playing_toast, Toast.LENGTH_SHORT).show(); - startService(context, getPlayerIntent(context, PopupVideoPlayer.class, queue)); + startService(context, getPlayerIntent(context, PopupVideoPlayer.class, queue, resumePlayback)); } - public static void playOnBackgroundPlayer(final Context context, final PlayQueue queue) { + public static void playOnBackgroundPlayer(final Context context, final PlayQueue queue, final boolean resumePlayback) { Toast.makeText(context, R.string.background_player_playing_toast, Toast.LENGTH_SHORT).show(); - startService(context, getPlayerIntent(context, BackgroundPlayer.class, queue)); + startService(context, getPlayerIntent(context, BackgroundPlayer.class, queue, resumePlayback)); } - public static void enqueueOnPopupPlayer(final Context context, final PlayQueue queue) { - enqueueOnPopupPlayer(context, queue, false); + public static void enqueueOnPopupPlayer(final Context context, final PlayQueue queue, final boolean resumePlayback) { + enqueueOnPopupPlayer(context, queue, false, resumePlayback); } - public static void enqueueOnPopupPlayer(final Context context, final PlayQueue queue, boolean selectOnAppend) { + public static void enqueueOnPopupPlayer(final Context context, final PlayQueue queue, boolean selectOnAppend, final boolean resumePlayback) { if (!PermissionHelper.isPopupEnabled(context)) { PermissionHelper.showPopupEnablementToast(context); return; @@ -145,17 +150,17 @@ public class NavigationHelper { Toast.makeText(context, R.string.popup_playing_append, Toast.LENGTH_SHORT).show(); startService(context, - getPlayerEnqueueIntent(context, PopupVideoPlayer.class, queue, selectOnAppend)); + getPlayerEnqueueIntent(context, PopupVideoPlayer.class, queue, selectOnAppend, resumePlayback)); } - public static void enqueueOnBackgroundPlayer(final Context context, final PlayQueue queue) { - enqueueOnBackgroundPlayer(context, queue, false); + public static void enqueueOnBackgroundPlayer(final Context context, final PlayQueue queue, final boolean resumePlayback) { + enqueueOnBackgroundPlayer(context, queue, false, resumePlayback); } - public static void enqueueOnBackgroundPlayer(final Context context, final PlayQueue queue, boolean selectOnAppend) { + public static void enqueueOnBackgroundPlayer(final Context context, final PlayQueue queue, boolean selectOnAppend, final boolean resumePlayback) { Toast.makeText(context, R.string.background_player_append, Toast.LENGTH_SHORT).show(); startService(context, - getPlayerEnqueueIntent(context, BackgroundPlayer.class, queue, selectOnAppend)); + getPlayerEnqueueIntent(context, BackgroundPlayer.class, queue, selectOnAppend, resumePlayback)); } public static void startService(@NonNull final Context context, @NonNull final Intent intent) { diff --git a/app/src/main/java/org/schabi/newpipe/util/OnClickGesture.java b/app/src/main/java/org/schabi/newpipe/util/OnClickGesture.java index 01416b279..18f4f67f4 100644 --- a/app/src/main/java/org/schabi/newpipe/util/OnClickGesture.java +++ b/app/src/main/java/org/schabi/newpipe/util/OnClickGesture.java @@ -1,6 +1,6 @@ package org.schabi.newpipe.util; -import android.support.v7.widget.RecyclerView; +import androidx.recyclerview.widget.RecyclerView; public abstract class OnClickGesture { diff --git a/app/src/main/java/org/schabi/newpipe/util/PermissionHelper.java b/app/src/main/java/org/schabi/newpipe/util/PermissionHelper.java index 7574a9304..f32bb6587 100644 --- a/app/src/main/java/org/schabi/newpipe/util/PermissionHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/PermissionHelper.java @@ -8,9 +8,9 @@ import android.content.pm.PackageManager; import android.net.Uri; import android.os.Build; import android.provider.Settings; -import android.support.annotation.RequiresApi; -import android.support.v4.app.ActivityCompat; -import android.support.v4.content.ContextCompat; +import androidx.annotation.RequiresApi; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; import android.view.Gravity; import android.widget.TextView; import android.widget.Toast; @@ -21,10 +21,9 @@ public class PermissionHelper { public static final int DOWNLOAD_DIALOG_REQUEST_CODE = 778; public static final int DOWNLOADS_REQUEST_CODE = 777; - public static boolean checkStoragePermissions(Activity activity, int requestCode) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - if(!checkReadStoragePermissions(activity, requestCode)) return false; + if (!checkReadStoragePermissions(activity, requestCode)) return false; } return checkWriteStoragePermissions(activity, requestCode); } @@ -92,7 +91,7 @@ public class PermissionHelper { i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); return false; - }else return true; + } else return true; } public static boolean isPopupEnabled(Context context) { diff --git a/app/src/main/java/org/schabi/newpipe/util/SecondaryStreamHelper.java b/app/src/main/java/org/schabi/newpipe/util/SecondaryStreamHelper.java index b3522aea0..d2ebcd9f8 100644 --- a/app/src/main/java/org/schabi/newpipe/util/SecondaryStreamHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/SecondaryStreamHelper.java @@ -1,6 +1,6 @@ package org.schabi.newpipe.util; -import android.support.annotation.NonNull; +import androidx.annotation.NonNull; import org.schabi.newpipe.extractor.MediaFormat; import org.schabi.newpipe.extractor.stream.AudioStream; @@ -38,7 +38,7 @@ public class SecondaryStreamHelper { public static AudioStream getAudioStreamFor(@NonNull List audioStreams, @NonNull VideoStream videoStream) { switch (videoStream.getFormat()) { case WEBM: - case MPEG_4: + case MPEG_4:// ¿is mpeg-4 DASH? break; default: return null; diff --git a/app/src/main/java/org/schabi/newpipe/util/SerializedCache.java b/app/src/main/java/org/schabi/newpipe/util/SerializedCache.java index 02871aff5..7680daf48 100644 --- a/app/src/main/java/org/schabi/newpipe/util/SerializedCache.java +++ b/app/src/main/java/org/schabi/newpipe/util/SerializedCache.java @@ -1,8 +1,8 @@ package org.schabi.newpipe.util; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.util.LruCache; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.collection.LruCache; import android.util.Log; import org.schabi.newpipe.MainActivity; diff --git a/app/src/main/java/org/schabi/newpipe/util/ServiceHelper.java b/app/src/main/java/org/schabi/newpipe/util/ServiceHelper.java index 6a4f65bcb..429331dfd 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ServiceHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ServiceHelper.java @@ -3,8 +3,8 @@ package org.schabi.newpipe.util; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; -import android.support.annotation.DrawableRes; -import android.support.annotation.StringRes; +import androidx.annotation.DrawableRes; +import androidx.annotation.StringRes; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.NewPipe; diff --git a/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java b/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java new file mode 100644 index 000000000..c5c78a726 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java @@ -0,0 +1,22 @@ +package org.schabi.newpipe.util; + +import android.content.Context; +import android.content.Intent; +import android.net.Uri; + +import org.schabi.newpipe.R; + +public class ShareUtils { + public static void openUrlInBrowser(Context context, String url) { + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); + context.startActivity(Intent.createChooser(intent, context.getString(R.string.share_dialog_title))); + } + + public static void shareUrl(Context context, String subject, String url) { + Intent intent = new Intent(Intent.ACTION_SEND); + intent.setType("text/plain"); + intent.putExtra(Intent.EXTRA_SUBJECT, subject); + intent.putExtra(Intent.EXTRA_TEXT, url); + context.startActivity(Intent.createChooser(intent, context.getString(R.string.share_dialog_title))); + } +} diff --git a/app/src/main/java/org/schabi/newpipe/util/SparseArrayUtils.java b/app/src/main/java/org/schabi/newpipe/util/SparseArrayUtils.java new file mode 100644 index 000000000..d17c9aa42 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/util/SparseArrayUtils.java @@ -0,0 +1,30 @@ +package org.schabi.newpipe.util; + +import android.util.SparseArray; + +public abstract class SparseArrayUtils { + + public static void shiftItemsDown(SparseArray sparseArray, int lower, int upper) { + for (int i = lower + 1; i <= upper; i++) { + final T o = sparseArray.get(i); + sparseArray.put(i - 1, o); + sparseArray.remove(i); + } + } + + public static void shiftItemsUp(SparseArray sparseArray, int lower, int upper) { + for (int i = upper - 1; i >= lower; i--) { + final T o = sparseArray.get(i); + sparseArray.put(i + 1, o); + sparseArray.remove(i); + } + } + + public static int[] getKeys(SparseArray sparseArray) { + final int[] result = new int[sparseArray.size()]; + for (int i = 0; i < result.length; i++) { + result[i] = sparseArray.keyAt(i); + } + return result; + } +} diff --git a/app/src/main/java/org/schabi/newpipe/util/StateSaver.java b/app/src/main/java/org/schabi/newpipe/util/StateSaver.java index 3115862e0..fffa9e99f 100644 --- a/app/src/main/java/org/schabi/newpipe/util/StateSaver.java +++ b/app/src/main/java/org/schabi/newpipe/util/StateSaver.java @@ -24,8 +24,8 @@ import android.content.Context; import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import android.text.TextUtils; import android.util.Log; diff --git a/app/src/main/java/org/schabi/newpipe/util/StreamDialogEntry.java b/app/src/main/java/org/schabi/newpipe/util/StreamDialogEntry.java new file mode 100644 index 000000000..b3ec4d14e --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/util/StreamDialogEntry.java @@ -0,0 +1,109 @@ +package org.schabi.newpipe.util; + +import android.content.Context; +import androidx.fragment.app.Fragment; + +import org.schabi.newpipe.R; +import org.schabi.newpipe.extractor.stream.StreamInfoItem; +import org.schabi.newpipe.local.dialog.PlaylistAppendDialog; +import org.schabi.newpipe.player.playqueue.SinglePlayQueue; + +import java.util.Collections; + +public enum StreamDialogEntry { + ////////////////////////////////////// + // enum values with DEFAULT actions // + ////////////////////////////////////// + + enqueue_on_background(R.string.enqueue_on_background, (fragment, item) -> + NavigationHelper.enqueueOnBackgroundPlayer(fragment.getContext(), new SinglePlayQueue(item), false)), + + enqueue_on_popup(R.string.enqueue_on_popup, (fragment, item) -> + NavigationHelper.enqueueOnPopupPlayer(fragment.getContext(), new SinglePlayQueue(item), false)), + + start_here_on_background(R.string.start_here_on_background, (fragment, item) -> + NavigationHelper.playOnBackgroundPlayer(fragment.getContext(), new SinglePlayQueue(item), true)), + + start_here_on_popup(R.string.start_here_on_popup, (fragment, item) -> + NavigationHelper.playOnPopupPlayer(fragment.getContext(), new SinglePlayQueue(item), true)), + + set_as_playlist_thumbnail(R.string.set_as_playlist_thumbnail, (fragment, item) -> {}), // has to be set manually + + delete(R.string.delete, (fragment, item) -> {}), // has to be set manually + + append_playlist(R.string.append_playlist, (fragment, item) -> { + if (fragment.getFragmentManager() != null) { + PlaylistAppendDialog.fromStreamInfoItems(Collections.singletonList(item)) + .show(fragment.getFragmentManager(), "StreamDialogEntry@append_playlist"); + }}), + + share(R.string.share, (fragment, item) -> + ShareUtils.shareUrl(fragment.getContext(), item.getName(), item.getUrl())); + + + /////////////// + // variables // + /////////////// + + public interface StreamDialogEntryAction { + void onClick(Fragment fragment, final StreamInfoItem infoItem); + } + + private final int resource; + private final StreamDialogEntryAction defaultAction; + private StreamDialogEntryAction customAction; + + private static StreamDialogEntry[] enabledEntries; + + + /////////////////////////////////////////////////////// + // non-static methods to initialize and edit entries // + /////////////////////////////////////////////////////// + + StreamDialogEntry(final int resource, StreamDialogEntryAction defaultAction) { + this.resource = resource; + this.defaultAction = defaultAction; + this.customAction = null; + } + + /** + * Can be used after {@link #setEnabledEntries(StreamDialogEntry...)} has been called + */ + public void setCustomAction(StreamDialogEntryAction action) { + this.customAction = action; + } + + + //////////////////////////////////////////////// + // static methods that act on enabled entries // + //////////////////////////////////////////////// + + /** + * To be called before using {@link #setCustomAction(StreamDialogEntryAction)} + */ + public static void setEnabledEntries(StreamDialogEntry... entries) { + // cleanup from last time StreamDialogEntry was used + for (StreamDialogEntry streamDialogEntry : values()) { + streamDialogEntry.customAction = null; + } + + enabledEntries = entries; + } + + public static String[] getCommands(Context context) { + String[] commands = new String[enabledEntries.length]; + for (int i = 0; i != enabledEntries.length; ++i) { + commands[i] = context.getResources().getString(enabledEntries[i].resource); + } + + return commands; + } + + public static void clickOn(int which, Fragment fragment, StreamInfoItem infoItem) { + if (enabledEntries[which].customAction == null) { + enabledEntries[which].defaultAction.onClick(fragment, infoItem); + } else { + enabledEntries[which].customAction.onClick(fragment, infoItem); + } + } +} diff --git a/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java b/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java index 13996eaf2..f476cf66b 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java @@ -22,9 +22,9 @@ package org.schabi.newpipe.util; import android.content.Context; import android.content.res.TypedArray; import android.preference.PreferenceManager; -import android.support.annotation.AttrRes; -import android.support.annotation.StyleRes; -import android.support.v4.content.ContextCompat; +import androidx.annotation.AttrRes; +import androidx.annotation.StyleRes; +import androidx.core.content.ContextCompat; import android.util.TypedValue; import android.view.ContextThemeWrapper; diff --git a/app/src/main/java/org/schabi/newpipe/views/AnimatedProgressBar.java b/app/src/main/java/org/schabi/newpipe/views/AnimatedProgressBar.java new file mode 100644 index 000000000..03ab40db5 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/views/AnimatedProgressBar.java @@ -0,0 +1,64 @@ +package org.schabi.newpipe.views; + +import android.content.Context; +import androidx.annotation.Nullable; +import android.util.AttributeSet; +import android.view.animation.AccelerateDecelerateInterpolator; +import android.view.animation.Animation; +import android.view.animation.Transformation; +import android.widget.ProgressBar; + +public final class AnimatedProgressBar extends ProgressBar { + + @Nullable + private ProgressBarAnimation animation = null; + + public AnimatedProgressBar(Context context) { + super(context); + } + + public AnimatedProgressBar(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public AnimatedProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + public synchronized void setProgressAnimated(int progress) { + cancelAnimation(); + animation = new ProgressBarAnimation(this, getProgress(), progress); + startAnimation(animation); + } + + private void cancelAnimation() { + if (animation != null) { + animation.cancel(); + animation = null; + } + clearAnimation(); + } + + private static class ProgressBarAnimation extends Animation { + + private final AnimatedProgressBar progressBar; + private final float from; + private final float to; + + ProgressBarAnimation(AnimatedProgressBar progressBar, float from, float to) { + super(); + this.progressBar = progressBar; + this.from = from; + this.to = to; + setDuration(500); + setInterpolator(new AccelerateDecelerateInterpolator()); + } + + @Override + protected void applyTransformation(float interpolatedTime, Transformation t) { + super.applyTransformation(interpolatedTime, t); + float value = from + (to - from) * interpolatedTime; + progressBar.setProgress((int) value); + } + } +} diff --git a/app/src/main/java/org/schabi/newpipe/views/CollapsibleView.java b/app/src/main/java/org/schabi/newpipe/views/CollapsibleView.java index 88d525625..38ca58cea 100644 --- a/app/src/main/java/org/schabi/newpipe/views/CollapsibleView.java +++ b/app/src/main/java/org/schabi/newpipe/views/CollapsibleView.java @@ -23,9 +23,9 @@ import android.animation.ValueAnimator; import android.content.Context; import android.os.Build; import android.os.Parcelable; -import android.support.annotation.IntDef; -import android.support.annotation.Nullable; -import android.support.annotation.RequiresApi; +import androidx.annotation.IntDef; +import androidx.annotation.Nullable; +import androidx.annotation.RequiresApi; import android.util.AttributeSet; import android.util.Log; import android.widget.LinearLayout; diff --git a/app/src/main/java/us/shandian/giga/get/DownloadInitializer.java b/app/src/main/java/us/shandian/giga/get/DownloadInitializer.java index b864cf4fb..247faeb6d 100644 --- a/app/src/main/java/us/shandian/giga/get/DownloadInitializer.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadInitializer.java @@ -1,185 +1,191 @@ -package us.shandian.giga.get; - -import android.support.annotation.NonNull; -import android.util.Log; - -import java.io.File; -import java.io.IOException; -import java.io.InterruptedIOException; -import java.io.RandomAccessFile; -import java.net.HttpURLConnection; -import java.nio.channels.ClosedByInterruptException; - -import us.shandian.giga.util.Utility; - -import static org.schabi.newpipe.BuildConfig.DEBUG; - -public class DownloadInitializer extends Thread { - private final static String TAG = "DownloadInitializer"; - final static int mId = 0; - - private DownloadMission mMission; - private HttpURLConnection mConn; - - DownloadInitializer(@NonNull DownloadMission mission) { - mMission = mission; - mConn = null; - } - - @Override - public void run() { - if (mMission.current > 0) mMission.resetState(); - - int retryCount = 0; - while (true) { - try { - mMission.currentThreadCount = mMission.threadCount; - - mConn = mMission.openConnection(mId, -1, -1); - mMission.establishConnection(mId, mConn); - - if (!mMission.running || Thread.interrupted()) return; - - mMission.length = Utility.getContentLength(mConn); - - - if (mMission.length == 0) { - mMission.notifyError(DownloadMission.ERROR_HTTP_NO_CONTENT, null); - return; - } - - // check for dynamic generated content - if (mMission.length == -1 && mConn.getResponseCode() == 200) { - mMission.blocks = 0; - mMission.length = 0; - mMission.fallback = true; - mMission.unknownLength = true; - mMission.currentThreadCount = 1; - - if (DEBUG) { - Log.d(TAG, "falling back (unknown length)"); - } - } else { - // Open again - mConn = mMission.openConnection(mId, mMission.length - 10, mMission.length); - mMission.establishConnection(mId, mConn); - - if (!mMission.running || Thread.interrupted()) return; - - synchronized (mMission.blockState) { - if (mConn.getResponseCode() == 206) { - if (mMission.currentThreadCount > 1) { - mMission.blocks = mMission.length / DownloadMission.BLOCK_SIZE; - - if (mMission.currentThreadCount > mMission.blocks) { - mMission.currentThreadCount = (int) mMission.blocks; - } - if (mMission.currentThreadCount <= 0) { - mMission.currentThreadCount = 1; - } - if (mMission.blocks * DownloadMission.BLOCK_SIZE < mMission.length) { - mMission.blocks++; - } - } else { - // if one thread is solicited don't calculate blocks, is useless - mMission.blocks = 1; - mMission.fallback = true; - mMission.unknownLength = false; - } - - if (DEBUG) { - Log.d(TAG, "http response code = " + mConn.getResponseCode()); - } - } else { - // Fallback to single thread - mMission.blocks = 0; - mMission.fallback = true; - mMission.unknownLength = false; - mMission.currentThreadCount = 1; - - if (DEBUG) { - Log.d(TAG, "falling back due http response code = " + mConn.getResponseCode()); - } - } - - for (long i = 0; i < mMission.currentThreadCount; i++) { - mMission.threadBlockPositions.add(i); - mMission.threadBytePositions.add(0L); - } - } - - if (!mMission.running || Thread.interrupted()) return; - } - - File file; - if (mMission.current == 0) { - file = new File(mMission.location); - if (!Utility.mkdir(file, true)) { - mMission.notifyError(DownloadMission.ERROR_PATH_CREATION, null); - return; - } - - file = new File(file, mMission.name); - - // if the name is used by another process, delete it - if (file.exists() && !file.isFile() && !file.delete()) { - mMission.notifyError(DownloadMission.ERROR_FILE_CREATION, null); - return; - } - - if (!file.exists() && !file.createNewFile()) { - mMission.notifyError(DownloadMission.ERROR_FILE_CREATION, null); - return; - } - } else { - file = new File(mMission.location, mMission.name); - } - - RandomAccessFile af = new RandomAccessFile(file, "rw"); - af.setLength(mMission.offsets[mMission.current] + mMission.length); - af.seek(mMission.offsets[mMission.current]); - af.close(); - - if (!mMission.running || Thread.interrupted()) return; - - mMission.running = false; - break; - } catch (InterruptedIOException | ClosedByInterruptException e) { - return; - } catch (Exception e) { - if (!mMission.running) return; - - if (e instanceof IOException && e.getMessage().contains("Permission denied")) { - mMission.notifyError(DownloadMission.ERROR_PERMISSION_DENIED, e); - return; - } - - if (retryCount++ > mMission.maxRetry) { - Log.e(TAG, "initializer failed", e); - mMission.notifyError(e); - return; - } - - Log.e(TAG, "initializer failed, retrying", e); - } - } - - // hide marquee in the progress bar - mMission.done++; - - mMission.start(); - } - - @Override - public void interrupt() { - super.interrupt(); - - if (mConn != null) { - try { - mConn.disconnect(); - } catch (Exception e) { - // nothing to do - } - } - } -} +package us.shandian.giga.get; + +import androidx.annotation.NonNull; +import android.util.Log; + +import org.schabi.newpipe.streams.io.SharpStream; + +import java.io.IOException; +import java.io.InterruptedIOException; +import java.net.HttpURLConnection; +import java.nio.channels.ClosedByInterruptException; + +import us.shandian.giga.util.Utility; + +import static org.schabi.newpipe.BuildConfig.DEBUG; + +public class DownloadInitializer extends Thread { + private final static String TAG = "DownloadInitializer"; + final static int mId = 0; + private final static int RESERVE_SPACE_DEFAULT = 5 * 1024 * 1024;// 5 MiB + private final static int RESERVE_SPACE_MAXIMUM = 150 * 1024 * 1024;// 150 MiB + + private DownloadMission mMission; + private HttpURLConnection mConn; + + DownloadInitializer(@NonNull DownloadMission mission) { + mMission = mission; + mConn = null; + } + + private static void safeClose(HttpURLConnection con) { + try { + con.getInputStream().close(); + } catch (Exception e) { + // nothing to do + } + } + + @Override + public void run() { + if (mMission.current > 0) mMission.resetState(false, true, DownloadMission.ERROR_NOTHING); + + int retryCount = 0; + int httpCode = 204; + + while (true) { + try { + if (mMission.blocks == null && mMission.current == 0) { + // calculate the whole size of the mission + long finalLength = 0; + long lowestSize = Long.MAX_VALUE; + + for (int i = 0; i < mMission.urls.length && mMission.running; i++) { + mConn = mMission.openConnection(mMission.urls[i], mId, -1, -1); + mMission.establishConnection(mId, mConn); + safeClose(mConn); + + if (Thread.interrupted()) return; + long length = Utility.getContentLength(mConn); + + if (i == 0) { + httpCode = mConn.getResponseCode(); + mMission.length = length; + } + + if (length > 0) finalLength += length; + if (length < lowestSize) lowestSize = length; + } + + mMission.nearLength = finalLength; + + // reserve space at the start of the file + if (mMission.psAlgorithm != null && mMission.psAlgorithm.reserveSpace) { + if (lowestSize < 1) { + // the length is unknown use the default size + mMission.offsets[0] = RESERVE_SPACE_DEFAULT; + } else { + // use the smallest resource size to download, otherwise, use the maximum + mMission.offsets[0] = lowestSize < RESERVE_SPACE_MAXIMUM ? lowestSize : RESERVE_SPACE_MAXIMUM; + } + } + } else { + // ask for the current resource length + mConn = mMission.openConnection(mId, -1, -1); + mMission.establishConnection(mId, mConn); + safeClose(mConn); + + if (!mMission.running || Thread.interrupted()) return; + + httpCode = mConn.getResponseCode(); + mMission.length = Utility.getContentLength(mConn); + } + + if (mMission.length == 0 || httpCode == 204) { + mMission.notifyError(DownloadMission.ERROR_HTTP_NO_CONTENT, null); + return; + } + + // check for dynamic generated content + if (mMission.length == -1 && mConn.getResponseCode() == 200) { + mMission.blocks = new int[0]; + mMission.length = 0; + mMission.unknownLength = true; + + if (DEBUG) { + Log.d(TAG, "falling back (unknown length)"); + } + } else { + // Open again + mConn = mMission.openConnection(mId, mMission.length - 10, mMission.length); + mMission.establishConnection(mId, mConn); + safeClose(mConn); + + if (!mMission.running || Thread.interrupted()) return; + + synchronized (mMission.LOCK) { + if (mConn.getResponseCode() == 206) { + + if (mMission.threadCount > 1) { + int count = (int) (mMission.length / DownloadMission.BLOCK_SIZE); + if ((count * DownloadMission.BLOCK_SIZE) < mMission.length) count++; + + mMission.blocks = new int[count]; + } else { + // if one thread is required don't calculate blocks, is useless + mMission.blocks = new int[0]; + mMission.unknownLength = false; + } + + if (DEBUG) { + Log.d(TAG, "http response code = " + mConn.getResponseCode()); + } + } else { + // Fallback to single thread + mMission.blocks = new int[0]; + mMission.unknownLength = false; + + if (DEBUG) { + Log.d(TAG, "falling back due http response code = " + mConn.getResponseCode()); + } + } + } + + if (!mMission.running || Thread.interrupted()) return; + } + + SharpStream fs = mMission.storage.getStream(); + fs.setLength(mMission.offsets[mMission.current] + mMission.length); + fs.seek(mMission.offsets[mMission.current]); + fs.close(); + + if (!mMission.running || Thread.interrupted()) return; + + mMission.running = false; + break; + } catch (InterruptedIOException | ClosedByInterruptException e) { + return; + } catch (Exception e) { + if (!mMission.running) return; + + if (e instanceof IOException && e.getMessage().contains("Permission denied")) { + mMission.notifyError(DownloadMission.ERROR_PERMISSION_DENIED, e); + return; + } + + if (retryCount++ > mMission.maxRetry) { + Log.e(TAG, "initializer failed", e); + mMission.notifyError(e); + return; + } + + Log.e(TAG, "initializer failed, retrying", e); + } + } + + mMission.start(); + } + + @Override + public void interrupt() { + super.interrupt(); + + if (mConn != null) { + try { + mConn.disconnect(); + } catch (Exception e) { + // nothing to do + } + } + } +} diff --git a/app/src/main/java/us/shandian/giga/get/DownloadMission.java b/app/src/main/java/us/shandian/giga/get/DownloadMission.java index 243a8585a..815ad9b65 100644 --- a/app/src/main/java/us/shandian/giga/get/DownloadMission.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadMission.java @@ -1,22 +1,25 @@ package us.shandian.giga.get; import android.os.Handler; -import android.os.Message; import android.util.Log; +import androidx.annotation.Nullable; + +import org.schabi.newpipe.Downloader; + import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.Serializable; import java.net.ConnectException; import java.net.HttpURLConnection; +import java.net.SocketTimeoutException; import java.net.URL; import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; import javax.net.ssl.SSLException; +import us.shandian.giga.io.StoredFileHelper; import us.shandian.giga.postprocessing.Postprocessing; import us.shandian.giga.service.DownloadManagerService; import us.shandian.giga.util.Utility; @@ -24,10 +27,13 @@ import us.shandian.giga.util.Utility; import static org.schabi.newpipe.BuildConfig.DEBUG; public class DownloadMission extends Mission { - private static final long serialVersionUID = 3L;// last bump: 8 november 2018 + private static final long serialVersionUID = 5L;// last bump: 30 june 2019 static final int BUFFER_SIZE = 64 * 1024; - final static int BLOCK_SIZE = 512 * 1024; + static final int BLOCK_SIZE = 512 * 1024; + + @SuppressWarnings("SpellCheckingInspection") + private static final String INSUFFICIENT_STORAGE = "ENOSPC"; private static final String TAG = "DownloadMission"; @@ -40,6 +46,11 @@ public class DownloadMission extends Mission { public static final int ERROR_UNKNOWN_HOST = 1005; public static final int ERROR_CONNECT_HOST = 1006; public static final int ERROR_POSTPROCESSING = 1007; + public static final int ERROR_POSTPROCESSING_STOPPED = 1008; + public static final int ERROR_POSTPROCESSING_HOLD = 1009; + public static final int ERROR_INSUFFICIENT_STORAGE = 1010; + public static final int ERROR_PROGRESS_LOST = 1011; + public static final int ERROR_TIMEOUT = 1012; public static final int ERROR_HTTP_NO_CONTENT = 204; public static final int ERROR_HTTP_UNSUPPORTED_RANGE = 206; @@ -48,11 +59,6 @@ public class DownloadMission extends Mission { */ public String[] urls; - /** - * Number of blocks the size of {@link DownloadMission#BLOCK_SIZE} - */ - long blocks = -1; - /** * Number of bytes downloaded */ @@ -68,171 +74,128 @@ public class DownloadMission extends Mission { */ public long[] offsets; - /** - * The post-processing algorithm arguments - */ - public String[] postprocessingArgs; - - /** - * The post-processing algorithm name - */ - public String postprocessingName; - /** * Indicates if the post-processing state: * 0: ready * 1: running * 2: completed + * 3: hold */ - public int postprocessingState; + public volatile int psState; /** - * Indicate if the post-processing algorithm works on the same file + * the post-processing algorithm instance */ - public boolean postprocessingThis; + public Postprocessing psAlgorithm; /** - * The current resource to download {@code urls[current]} + * The current resource to download, {@code urls[current]} and {@code offsets[current]} */ public int current; /** * Metadata where the mission state is saved */ - public File metadata; + public transient File metadata; /** * maximum attempts */ - public int maxRetry; + public transient int maxRetry; /** * Approximated final length, this represent the sum of all resources sizes */ public long nearLength; + /** + * Download blocks, the size is multiple of {@link DownloadMission#BLOCK_SIZE}. + * Every entry (block) in this array holds an offset, used to resume the download. + * An block offset can be -1 if the block was downloaded successfully. + */ + int[] blocks; + + /** + * Download/File resume offset in fallback mode (if applicable) {@link DownloadRunnableFallback} + */ + long fallbackResumeOffset; + + /** + * Maximum of download threads running, chosen by the user + */ public int threadCount = 3; - boolean fallback; - private int finishCount; + + private transient int finishCount; public transient boolean running; - public transient boolean enqueued = true; + public boolean enqueued; public int errCode = ERROR_NOTHING; + public Exception errObject = null; - public transient Exception errObject = null; public transient boolean recovered; public transient Handler mHandler; private transient boolean mWritingToFile; + private transient boolean[] blockAcquired; - @SuppressWarnings("UseSparseArrays")// LongSparseArray is not serializable - final HashMap blockState = new HashMap<>(); - final List threadBlockPositions = new ArrayList<>(); - final List threadBytePositions = new ArrayList<>(); + final Object LOCK = new Lock(); private transient boolean deleted; - int currentThreadCount; - private transient Thread[] threads = new Thread[0]; + + public transient volatile Thread[] threads = new Thread[0]; private transient Thread init = null; - - protected DownloadMission() { - - } - - public DownloadMission(String url, String name, String location, char kind) { - this(new String[]{url}, name, location, kind, null, null); - } - - public DownloadMission(String[] urls, String name, String location, char kind, String postprocessingName, String[] postprocessingArgs) { - if (name == null) throw new NullPointerException("name is null"); - if (name.isEmpty()) throw new IllegalArgumentException("name is empty"); + public DownloadMission(String[] urls, StoredFileHelper storage, char kind, Postprocessing psInstance) { if (urls == null) throw new NullPointerException("urls is null"); if (urls.length < 1) throw new IllegalArgumentException("urls is empty"); - if (location == null) throw new NullPointerException("location is null"); - if (location.isEmpty()) throw new IllegalArgumentException("location is empty"); this.urls = urls; - this.name = name; - this.location = location; this.kind = kind; this.offsets = new long[urls.length]; + this.enqueued = true; + this.maxRetry = 3; + this.storage = storage; + this.psAlgorithm = psInstance; - if (postprocessingName != null) { - Postprocessing algorithm = Postprocessing.getAlgorithm(postprocessingName, null); - this.postprocessingThis = algorithm.worksOnSameFile; - this.offsets[0] = algorithm.recommendedReserve; - this.postprocessingName = postprocessingName; - this.postprocessingArgs = postprocessingArgs; - } else { - if (DEBUG && urls.length > 1) { - Log.w(TAG, "mission created with multiple urls ¿missing post-processing algorithm?"); + if (DEBUG && psInstance == null && urls.length > 1) { + Log.w(TAG, "mission created with multiple urls ¿missing post-processing algorithm?"); + } + } + + /** + * Acquire a block + * + * @return the block or {@code null} if no more blocks left + */ + @Nullable + Block acquireBlock() { + synchronized (LOCK) { + for (int i = 0; i < blockAcquired.length; i++) { + if (!blockAcquired[i] && blocks[i] >= 0) { + Block block = new Block(); + block.position = i; + block.done = blocks[i]; + + blockAcquired[i] = true; + return block; + } } } + + return null; } - private void checkBlock(long block) { - if (block < 0 || block >= blocks) { - throw new IllegalArgumentException("illegal block identifier"); + /** + * Release an block + * + * @param position the index of the block + * @param done amount of bytes downloaded + */ + void releaseBlock(int position, int done) { + synchronized (LOCK) { + blockAcquired[position] = false; + blocks[position] = done; } } - /** - * Check if a block is reserved - * - * @param block the block identifier - * @return true if the block is reserved and false if otherwise - */ - boolean isBlockPreserved(long block) { - checkBlock(block); - return blockState.containsKey(block) ? blockState.get(block) : false; - } - - void preserveBlock(long block) { - checkBlock(block); - synchronized (blockState) { - blockState.put(block, true); - } - } - - /** - * Set the block of the file - * - * @param threadId the identifier of the thread - * @param position the block of the thread - */ - void setBlockPosition(int threadId, long position) { - threadBlockPositions.set(threadId, position); - } - - /** - * Get the block of a file - * - * @param threadId the identifier of the thread - * @return the block for the thread - */ - long getBlockPosition(int threadId) { - return threadBlockPositions.get(threadId); - } - - /** - * Save the position of the desired thread - * - * @param threadId the identifier of the thread - * @param position the relative position in bytes or zero - */ - void setThreadBytePosition(int threadId, long position) { - threadBytePositions.set(threadId, position); - } - - /** - * Get position inside of the thread, where thread will be resumed - * - * @param threadId the identifier of the thread - * @return the relative position in bytes or zero - */ - long getThreadBytePosition(int threadId) { - return threadBytePositions.get(threadId); - } - /** * Open connection * @@ -243,9 +206,18 @@ public class DownloadMission extends Mission { * @throws IOException if an I/O exception occurs. */ HttpURLConnection openConnection(int threadId, long rangeStart, long rangeEnd) throws IOException { - URL url = new URL(urls[current]); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + return openConnection(urls[current], threadId, rangeStart, rangeEnd); + } + + HttpURLConnection openConnection(String url, int threadId, long rangeStart, long rangeEnd) throws IOException { + HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setInstanceFollowRedirects(true); + conn.setRequestProperty("User-Agent", Downloader.USER_AGENT); + conn.setRequestProperty("Accept", "*/*"); + + // BUG workaround: switching between networks can freeze the download forever + conn.setConnectTimeout(30000); + conn.setReadTimeout(10000); if (rangeStart >= 0) { String req = "bytes=" + rangeStart + "-"; @@ -292,11 +264,7 @@ public class DownloadMission extends Mission { private void notify(int what) { - Message m = new Message(); - m.what = what; - m.obj = this; - - mHandler.sendMessage(m); + mHandler.obtainMessage(what, this).sendToTarget(); } synchronized void notifyProgress(long deltaLen) { @@ -337,20 +305,48 @@ public class DownloadMission extends Mission { notifyError(ERROR_CONNECT_HOST, null); } else if (err instanceof UnknownHostException) { notifyError(ERROR_UNKNOWN_HOST, null); + } else if (err instanceof SocketTimeoutException) { + notifyError(ERROR_TIMEOUT, null); } else { notifyError(ERROR_UNKNOWN_EXCEPTION, err); } } - synchronized void notifyError(int code, Exception err) { + public synchronized void notifyError(int code, Exception err) { Log.e(TAG, "notifyError() code = " + code, err); + if (err instanceof IOException) { + if (!storage.canWrite() || err.getMessage().contains("Permission denied")) { + code = ERROR_PERMISSION_DENIED; + err = null; + } else if (err.getMessage().contains(INSUFFICIENT_STORAGE)) { + code = ERROR_INSUFFICIENT_STORAGE; + err = null; + } + } errCode = code; errObject = err; - pause(); + switch (code) { + case ERROR_SSL_EXCEPTION: + case ERROR_UNKNOWN_HOST: + case ERROR_CONNECT_HOST: + case ERROR_TIMEOUT: + // do not change the queue flag for network errors, can be + // recovered silently without the user interaction + break; + default: + // also checks for server errors + if (code < 500 || code > 599) enqueued = false; + } notify(DownloadManagerService.MESSAGE_ERROR); + + if (running) { + running = false; + recovered = true; + if (threads != null) selfPause(); + } } synchronized void notifyFinished() { @@ -358,11 +354,11 @@ public class DownloadMission extends Mission { finishCount++; - if (finishCount == currentThreadCount) { + if (blocks.length < 1 || threads == null || finishCount == threads.length) { if (errCode != ERROR_NOTHING) return; if (DEBUG) { - Log.d(TAG, "onFinish" + (current + 1) + "/" + urls.length); + Log.d(TAG, "onFinish: " + (current + 1) + "/" + urls.length); } if ((current + 1) < urls.length) { @@ -378,6 +374,7 @@ public class DownloadMission extends Mission { if (!doPostprocessing()) return; + enqueued = false; running = false; deleteThisFromFile(); @@ -386,29 +383,28 @@ public class DownloadMission extends Mission { } private void notifyPostProcessing(int state) { - if (DEBUG) { - String action; - switch (state) { - case 1: - action = "Running"; - break; - case 2: - action = "Completed"; - break; - default: - action = "Failed"; - } - - Log.d(TAG, action + " postprocessing on " + location + File.separator + name); + String action; + switch (state) { + case 1: + action = "Running"; + break; + case 2: + action = "Completed"; + break; + default: + action = "Failed"; } - synchronized (blockState) { + Log.d(TAG, action + " postprocessing on " + storage.getName()); + + synchronized (LOCK) { // don't return without fully write the current state - postprocessingState = state; + psState = state; Utility.writeToFile(metadata, DownloadMission.this); } } + /** * Start downloading with multiple threads. */ @@ -417,55 +413,61 @@ public class DownloadMission extends Mission { // ensure that the previous state is completely paused. joinForThread(init); - if (threads != null) + if (threads != null) { for (Thread thread : threads) joinForThread(thread); + threads = null; + } - enqueued = false; running = true; errCode = ERROR_NOTHING; - if (current >= urls.length && postprocessingName != null) { - runAsync(1, () -> { - if (doPostprocessing()) { - running = false; - deleteThisFromFile(); - - notify(DownloadManagerService.MESSAGE_FINISHED); - } - }); - + if (hasInvalidStorage()) { + notifyError(ERROR_FILE_CREATION, null); return; } - if (blocks < 0) { + if (current >= urls.length) { + runAsync(1, this::notifyFinished); + return; + } + + if (blocks == null) { initializer(); return; } init = null; + finishCount = 0; + blockAcquired = new boolean[blocks.length]; - if (threads == null || threads.length < 1) { - threads = new Thread[currentThreadCount]; - } - - if (fallback) { + if (blocks.length < 1) { if (unknownLength) { done = 0; length = 0; } - threads[0] = runAsync(1, new DownloadRunnableFallback(this)); + threads = new Thread[]{runAsync(1, new DownloadRunnableFallback(this))}; } else { - for (int i = 0; i < currentThreadCount; i++) { + int remainingBlocks = 0; + for (int block : blocks) if (block >= 0) remainingBlocks++; + + if (remainingBlocks < 1) { + runAsync(1, this::notifyFinished); + return; + } + + threads = new Thread[Math.min(threadCount, remainingBlocks)]; + + for (int i = 0; i < threads.length; i++) { threads[i] = runAsync(i + 1, new DownloadRunnable(this, i)); } } } /** - * Pause the mission, does not affect the blocks that are being downloaded. + * Pause the mission */ - public synchronized void pause() { + public void pause() { if (!running) return; if (isPsRunning()) { @@ -477,71 +479,84 @@ public class DownloadMission extends Mission { running = false; recovered = true; - enqueued = false; - if (init != null && Thread.currentThread() != init && init.isAlive()) { + if (init != null && init.isAlive()) { + // NOTE: if start() method is running ¡will no have effect! init.interrupt(); - synchronized (blockState) { - resetState(); + synchronized (LOCK) { + resetState(false, true, ERROR_NOTHING); } return; } - if (DEBUG && blocks == 0) { + if (DEBUG && unknownLength) { Log.w(TAG, "pausing a download that can not be resumed (range requests not allowed by the server)."); } - if (threads == null || Thread.currentThread().isInterrupted()) { + // check if the calling thread (alias UI thread) is interrupted + if (Thread.currentThread().isInterrupted()) { writeThisToFile(); return; } // wait for all threads are suspended before save the state - runAsync(-1, () -> { - try { - for (Thread thread : threads) { - if (thread.isAlive()) { - thread.interrupt(); - thread.join(5000); - } + if (threads != null) runAsync(-1, this::selfPause); + } + + private void selfPause() { + try { + for (Thread thread : threads) { + if (thread.isAlive()) { + thread.interrupt(); + thread.join(5000); } - } catch (Exception e) { - // nothing to do - } finally { - writeThisToFile(); } - }); + } catch (Exception e) { + // nothing to do + } finally { + writeThisToFile(); + } } /** - * Removes the file and the meta file + * Removes the downloaded file and the meta file */ @Override public boolean delete() { deleted = true; + if (psAlgorithm != null) psAlgorithm.cleanupTemporalDir(); + boolean res = deleteThisFromFile(); - if (!super.delete()) res = false; + + if (!super.delete()) return false; return res; } - void resetState() { - done = 0; - blocks = -1; - errCode = ERROR_NOTHING; - fallback = false; - unknownLength = false; - finishCount = 0; - threadBlockPositions.clear(); - threadBytePositions.clear(); - blockState.clear(); - threads = new Thread[0]; - Utility.writeToFile(metadata, DownloadMission.this); + /** + * Resets the mission state + * + * @param rollback {@code true} true to forget all progress, otherwise, {@code false} + * @param persistChanges {@code true} to commit changes to the metadata file, otherwise, {@code false} + */ + public void resetState(boolean rollback, boolean persistChanges, int errorCode) { + done = 0; + errCode = errorCode; + errObject = null; + unknownLength = false; + threads = null; + fallbackResumeOffset = 0; + blocks = null; + blockAcquired = null; + + if (rollback) current = 0; + + if (persistChanges) + Utility.writeToFile(metadata, DownloadMission.this); } private void initializer() { init = runAsync(DownloadInitializer.mId, new DownloadInitializer(this)); - } /** @@ -549,7 +564,7 @@ public class DownloadMission extends Mission { * if no thread is already running. */ private void writeThisToFile() { - synchronized (blockState) { + synchronized (LOCK) { if (deleted) return; Utility.writeToFile(metadata, DownloadMission.this); } @@ -562,7 +577,7 @@ public class DownloadMission extends Mission { * @return true, otherwise, false */ public boolean isFinished() { - return current >= urls.length && (postprocessingName == null || postprocessingState == 2); + return current >= urls.length && (psAlgorithm == null || psState == 2); } /** @@ -571,7 +586,13 @@ public class DownloadMission extends Mission { * @return {@code true} if this mission is unrecoverable */ public boolean isPsFailed() { - return postprocessingName != null && errCode == DownloadMission.ERROR_POSTPROCESSING && postprocessingThis; + switch (errCode) { + case ERROR_POSTPROCESSING: + case ERROR_POSTPROCESSING_STOPPED: + return psAlgorithm.worksOnSameFile; + } + + return false; } /** @@ -580,12 +601,26 @@ public class DownloadMission extends Mission { * @return true, otherwise, false */ public boolean isPsRunning() { - return postprocessingName != null && postprocessingState == 1; + return psAlgorithm != null && (psState == 1 || psState == 3); } + /** + * Indicated if the mission is ready + * + * @return true, otherwise, false + */ + public boolean isInitialized() { + return blocks != null; // DownloadMissionInitializer was executed + } + + /** + * Gets the approximated final length of the file + * + * @return the length in bytes + */ public long getLength() { long calculated; - if (postprocessingState == 1) { + if (psState == 1 || psState == 3) { calculated = length; } else { calculated = offsets[current < offsets.length ? current : (offsets.length - 1)] + length; @@ -596,30 +631,67 @@ public class DownloadMission extends Mission { return calculated > nearLength ? calculated : nearLength; } + /** + * set this mission state on the queue + * + * @param queue true to add to the queue, otherwise, false + */ + public void setEnqueued(boolean queue) { + enqueued = queue; + runAsync(-2, this::writeThisToFile); + } + + /** + * Attempts to continue a blocked post-processing + * + * @param recover {@code true} to retry, otherwise, {@code false} to cancel + */ + public void psContinue(boolean recover) { + psState = 1; + errCode = recover ? ERROR_NOTHING : ERROR_POSTPROCESSING; + threads[0].interrupt(); + } + + /** + * Indicates whatever the backed storage is invalid + * + * @return {@code true}, if storage is invalid and cannot be used + */ + public boolean hasInvalidStorage() { + return errCode == ERROR_PROGRESS_LOST || storage == null || storage.isInvalid() || !storage.existsAsFile(); + } + + /** + * Indicates whatever is possible to start the mission + * + * @return {@code true} is this mission its "healthy", otherwise, {@code false} + */ + public boolean isCorrupt() { + return (isPsFailed() || errCode == ERROR_POSTPROCESSING_HOLD) || isFinished(); + } + private boolean doPostprocessing() { - if (postprocessingName == null || postprocessingState == 2) return true; + if (psAlgorithm == null || psState == 2) return true; + + errObject = null; notifyPostProcessing(1); notifyProgress(0); - Thread.currentThread().setName("[" + TAG + "] post-processing = " + postprocessingName + " filename = " + name); + if (DEBUG) + Thread.currentThread().setName("[" + TAG + "] ps = " + + psAlgorithm.getClass().getSimpleName() + + " filename = " + storage.getName() + ); + + threads = new Thread[]{Thread.currentThread()}; Exception exception = null; try { - Postprocessing - .getAlgorithm(postprocessingName, this) - .run(); + psAlgorithm.run(this); } catch (Exception err) { - StringBuilder args = new StringBuilder(" "); - if (postprocessingArgs != null) { - for (String arg : postprocessingArgs) { - args.append(", "); - args.append(arg); - } - args.delete(0, 1); - } - Log.e(TAG, String.format("Post-processing failed. algorithm = %s args = [%s]", postprocessingName, args), err); + Log.e(TAG, "Post-processing failed. " + psAlgorithm.toString(), err); if (errCode == ERROR_NOTHING) errCode = ERROR_POSTPROCESSING; @@ -639,7 +711,7 @@ public class DownloadMission extends Mission { } private boolean deleteThisFromFile() { - synchronized (blockState) { + synchronized (LOCK) { return metadata.delete(); } } @@ -669,7 +741,7 @@ public class DownloadMission extends Mission { // >=1: any download thread if (DEBUG) { - who.setName(String.format("%s[%s] %s", TAG, id, name)); + who.setName(String.format("%s[%s] %s", TAG, id, storage.getName())); } who.start(); @@ -701,7 +773,7 @@ public class DownloadMission extends Mission { static class HttpError extends Exception { - int statusCode; + final int statusCode; HttpError(int statusCode) { this.statusCode = statusCode; @@ -709,7 +781,16 @@ public class DownloadMission extends Mission { @Override public String getMessage() { - return "HTTP " + String.valueOf(statusCode); + return "HTTP " + statusCode; } } + + static class Block { + int position; + int done; + } + + private static class Lock implements Serializable { + // java.lang.Object cannot be used because is not serializable + } } diff --git a/app/src/main/java/us/shandian/giga/get/DownloadRunnable.java b/app/src/main/java/us/shandian/giga/get/DownloadRunnable.java index 244fbd47a..f5b9b06d4 100644 --- a/app/src/main/java/us/shandian/giga/get/DownloadRunnable.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadRunnable.java @@ -2,14 +2,18 @@ package us.shandian.giga.get; import android.util.Log; -import java.io.FileNotFoundException; +import org.schabi.newpipe.streams.io.SharpStream; + +import java.io.IOException; import java.io.InputStream; -import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.nio.channels.ClosedByInterruptException; +import us.shandian.giga.get.DownloadMission.Block; + import static org.schabi.newpipe.BuildConfig.DEBUG; + /** * Runnable to download blocks of a file until the file is completely downloaded, * an error occurs or the process is stopped. @@ -26,118 +30,107 @@ public class DownloadRunnable extends Thread { if (mission == null) throw new NullPointerException("mission is null"); mMission = mission; mId = id; - mConn = null; + } + + private void releaseBlock(Block block, long remain) { + // set the block offset to -1 if it is completed + mMission.releaseBlock(block.position, remain < 0 ? -1 : block.done); } @Override public void run() { - boolean retry = mMission.recovered; - long blockPosition = mMission.getBlockPosition(mId); + boolean retry = false; + Block block = null; + int retryCount = 0; if (DEBUG) { - Log.d(TAG, mId + ":default pos " + blockPosition); Log.d(TAG, mId + ":recovered: " + mMission.recovered); } - RandomAccessFile f; - InputStream is = null; + SharpStream f; try { - f = new RandomAccessFile(mMission.getDownloadedFile(), "rw"); - } catch (FileNotFoundException e) { + f = mMission.storage.getStream(); + } catch (IOException e) { mMission.notifyError(e);// this never should happen return; } - while (mMission.running && mMission.errCode == DownloadMission.ERROR_NOTHING && blockPosition < mMission.blocks) { - - if (DEBUG && retry) { - Log.d(TAG, mId + ":retry is true. Resuming at " + blockPosition); + while (mMission.running && mMission.errCode == DownloadMission.ERROR_NOTHING) { + if (!retry) { + block = mMission.acquireBlock(); } - // Wait for an unblocked position - while (!retry && blockPosition < mMission.blocks && mMission.isBlockPreserved(blockPosition)) { - - if (DEBUG) { - Log.d(TAG, mId + ":position " + blockPosition + " preserved, passing"); - } - - blockPosition++; - } - - retry = false; - - if (blockPosition >= mMission.blocks) { + if (block == null) { + if (DEBUG) Log.d(TAG, mId + ":no more blocks left, exiting"); break; } if (DEBUG) { - Log.d(TAG, mId + ":preserving position " + blockPosition); + if (retry) + Log.d(TAG, mId + ":retry block at position=" + block.position + " from the start"); + else + Log.d(TAG, mId + ":acquired block at position=" + block.position + " done=" + block.done); } - mMission.preserveBlock(blockPosition); - mMission.setBlockPosition(mId, blockPosition); - - long start = blockPosition * DownloadMission.BLOCK_SIZE; + long start = block.position * DownloadMission.BLOCK_SIZE; long end = start + DownloadMission.BLOCK_SIZE - 1; - long offset = mMission.getThreadBytePosition(mId); - start += offset; + start += block.done; if (end >= mMission.length) { end = mMission.length - 1; } - long total = 0; - try { mConn = mMission.openConnection(mId, start, end); mMission.establishConnection(mId, mConn); // check if the download can be resumed - if (mConn.getResponseCode() == 416 && offset > 0) { - retryCount--; + if (mConn.getResponseCode() == 416) { + if (block.done > 0) { + // try again from the start (of the block) + block.done = 0; + retry = true; + mConn.disconnect(); + continue; + } + throw new DownloadMission.HttpError(416); } + retry = false; + // The server may be ignoring the range request if (mConn.getResponseCode() != 206) { - mMission.notifyError(new DownloadMission.HttpError(mConn.getResponseCode())); - if (DEBUG) { Log.e(TAG, mId + ":Unsupported " + mConn.getResponseCode()); } - + mMission.notifyError(new DownloadMission.HttpError(mConn.getResponseCode())); break; } f.seek(mMission.offsets[mMission.current] + start); - is = mConn.getInputStream(); + try (InputStream is = mConn.getInputStream()) { + byte[] buf = new byte[DownloadMission.BUFFER_SIZE]; + int len; - byte[] buf = new byte[DownloadMission.BUFFER_SIZE]; - int len; - - while (start < end && mMission.running && (len = is.read(buf, 0, buf.length)) != -1) { - f.write(buf, 0, len); - start += len; - total += len; - mMission.notifyProgress(len); + // use always start <= end + // fixes a deadlock in DownloadRunnable because youtube is sending one byte alone after downloading 26MiB exactly + while (start <= end && mMission.running && (len = is.read(buf, 0, buf.length)) != -1) { + f.write(buf, 0, len); + start += len; + block.done += len; + mMission.notifyProgress(len); + } } if (DEBUG && mMission.running) { - Log.d(TAG, mId + ":position " + blockPosition + " finished, " + total + " bytes downloaded"); + Log.d(TAG, mId + ":position " + block.position + " stopped " + start + "/" + end); } - - if (mMission.running) - mMission.setThreadBytePosition(mId, 0L);// clear byte position for next block - else - mMission.setThreadBytePosition(mId, total);// download paused, save progress for this block - } catch (Exception e) { - mMission.setThreadBytePosition(mId, total); - if (!mMission.running || e instanceof ClosedByInterruptException) break; if (retryCount++ >= mMission.maxRetry) { @@ -145,20 +138,12 @@ public class DownloadRunnable extends Thread { break; } - if (DEBUG) { - Log.d(TAG, mId + ":position " + blockPosition + " retrying due exception", e); - } - retry = true; + } finally { + if (!retry) releaseBlock(block, end - start); } } - try { - if (is != null) is.close(); - } catch (Exception err) { - // nothing to do - } - try { f.close(); } catch (Exception err) { diff --git a/app/src/main/java/us/shandian/giga/get/DownloadRunnableFallback.java b/app/src/main/java/us/shandian/giga/get/DownloadRunnableFallback.java index 4bcaeaf85..7fb1f0c77 100644 --- a/app/src/main/java/us/shandian/giga/get/DownloadRunnableFallback.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadRunnableFallback.java @@ -1,16 +1,15 @@ package us.shandian.giga.get; -import android.annotation.SuppressLint; -import android.support.annotation.NonNull; +import androidx.annotation.NonNull; import android.util.Log; +import org.schabi.newpipe.streams.io.SharpStream; + import java.io.IOException; import java.io.InputStream; -import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.nio.channels.ClosedByInterruptException; - import us.shandian.giga.util.Utility; import static org.schabi.newpipe.BuildConfig.DEBUG; @@ -19,21 +18,17 @@ import static org.schabi.newpipe.BuildConfig.DEBUG; * Single-threaded fallback mode */ public class DownloadRunnableFallback extends Thread { - private static final String TAG = "DownloadRunnableFallback"; + private static final String TAG = "DownloadRunnableFallbac"; private final DownloadMission mMission; - private final int mId = 1; private int mRetryCount = 0; private InputStream mIs; - private RandomAccessFile mF; + private SharpStream mF; private HttpURLConnection mConn; DownloadRunnableFallback(@NonNull DownloadMission mission) { mMission = mission; - mIs = null; - mF = null; - mConn = null; } private void dispose() { @@ -43,30 +38,34 @@ public class DownloadRunnableFallback extends Thread { // nothing to do } - try { - if (mF != null) mF.close(); - } catch (IOException e) { - // ¿ejected media storage? ¿file deleted? ¿storage ran out of space? + if (mF != null) mF.close(); + } + + private long loadPosition() { + synchronized (mMission.LOCK) { + return mMission.fallbackResumeOffset; + } + } + + private void savePosition(long position) { + synchronized (mMission.LOCK) { + mMission.fallbackResumeOffset = position; } } @Override - @SuppressLint("LongLogTag") public void run() { boolean done; + long start = loadPosition(); - long start = 0; - - if (!mMission.unknownLength) { - start = mMission.getThreadBytePosition(0); - if (DEBUG && start > 0) { - Log.i(TAG, "Resuming a single-thread download at " + start); - } + if (DEBUG && !mMission.unknownLength && start > 0) { + Log.i(TAG, "Resuming a single-thread download at " + start); } try { long rangeStart = (mMission.unknownLength || start < 1) ? -1 : start; + int mId = 1; mConn = mMission.openConnection(mId, rangeStart, -1); mMission.establishConnection(mId, mConn); @@ -81,7 +80,7 @@ public class DownloadRunnableFallback extends Thread { if (!mMission.unknownLength) mMission.unknownLength = Utility.getContentLength(mConn) == -1; - mF = new RandomAccessFile(mMission.getDownloadedFile(), "rw"); + mF = mMission.storage.getStream(); mF.seek(mMission.offsets[mMission.current] + start); mIs = mConn.getInputStream(); @@ -95,13 +94,12 @@ public class DownloadRunnableFallback extends Thread { mMission.notifyProgress(len); } - // if thread goes interrupted check if the last part mIs written. This avoid re-download the whole file + // if thread goes interrupted check if the last part is written. This avoid re-download the whole file done = len == -1; } catch (Exception e) { dispose(); - // save position - mMission.setThreadBytePosition(0, start); + savePosition(start); if (!mMission.running || e instanceof ClosedByInterruptException) return; @@ -110,6 +108,10 @@ public class DownloadRunnableFallback extends Thread { return; } + if (DEBUG) { + Log.e(TAG, "got exception, retrying...", e); + } + run();// try again return; } @@ -119,7 +121,7 @@ public class DownloadRunnableFallback extends Thread { if (done) { mMission.notifyFinished(); } else { - mMission.setThreadBytePosition(0, start); + savePosition(start); } } diff --git a/app/src/main/java/us/shandian/giga/get/FinishedMission.java b/app/src/main/java/us/shandian/giga/get/FinishedMission.java index b7d6908a5..b468f3c76 100644 --- a/app/src/main/java/us/shandian/giga/get/FinishedMission.java +++ b/app/src/main/java/us/shandian/giga/get/FinishedMission.java @@ -1,16 +1,18 @@ package us.shandian.giga.get; +import androidx.annotation.NonNull; + public class FinishedMission extends Mission { public FinishedMission() { } - public FinishedMission(DownloadMission mission) { + public FinishedMission(@NonNull DownloadMission mission) { source = mission.source; length = mission.length;// ¿or mission.done? timestamp = mission.timestamp; - name = mission.name; - location = mission.location; kind = mission.kind; + storage = mission.storage; + } } diff --git a/app/src/main/java/us/shandian/giga/get/Mission.java b/app/src/main/java/us/shandian/giga/get/Mission.java index ec2ddaa26..8e814a2af 100644 --- a/app/src/main/java/us/shandian/giga/get/Mission.java +++ b/app/src/main/java/us/shandian/giga/get/Mission.java @@ -1,12 +1,14 @@ package us.shandian.giga.get; -import java.io.File; +import androidx.annotation.NonNull; + import java.io.Serializable; -import java.text.SimpleDateFormat; import java.util.Calendar; +import us.shandian.giga.io.StoredFileHelper; + public abstract class Mission implements Serializable { - private static final long serialVersionUID = 0L;// last bump: 5 october 2018 + private static final long serialVersionUID = 1L;// last bump: 27 march 2019 /** * Source url of the resource @@ -23,33 +25,24 @@ public abstract class Mission implements Serializable { */ public long timestamp; - /** - * The filename - */ - public String name; - - /** - * The directory to store the download - */ - public String location; - /** * pre-defined content type */ public char kind; /** - * get the target file on the storage - * - * @return File object + * The downloaded file */ - public File getDownloadedFile() { - return new File(location, name); - } + public StoredFileHelper storage; + /** + * Delete the downloaded file + * + * @return {@code true] if and only if the file is successfully deleted, otherwise, {@code false} + */ public boolean delete() { - deleted = true; - return getDownloadedFile().delete(); + if (storage != null) return storage.delete(); + return true; } /** @@ -57,10 +50,11 @@ public abstract class Mission implements Serializable { */ public transient boolean deleted = false; + @NonNull @Override public String toString() { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(timestamp); - return "[" + calendar.getTime().toString() + "] " + location + File.separator + name; + return "[" + calendar.getTime().toString() + "] " + (storage.isInvalid() ? storage.getName() : storage.getUri()); } } diff --git a/app/src/main/java/us/shandian/giga/get/sqlite/DownloadDataSource.java b/app/src/main/java/us/shandian/giga/get/sqlite/DownloadDataSource.java deleted file mode 100644 index 4b4d5d733..000000000 --- a/app/src/main/java/us/shandian/giga/get/sqlite/DownloadDataSource.java +++ /dev/null @@ -1,73 +0,0 @@ -package us.shandian.giga.get.sqlite; - -import android.content.ContentValues; -import android.content.Context; -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; -import android.util.Log; - -import java.util.ArrayList; -import java.util.List; - -import us.shandian.giga.get.DownloadMission; -import us.shandian.giga.get.FinishedMission; -import us.shandian.giga.get.Mission; - -import static us.shandian.giga.get.sqlite.DownloadMissionHelper.KEY_LOCATION; -import static us.shandian.giga.get.sqlite.DownloadMissionHelper.KEY_NAME; -import static us.shandian.giga.get.sqlite.DownloadMissionHelper.MISSIONS_TABLE_NAME; - -public class DownloadDataSource { - - private static final String TAG = "DownloadDataSource"; - private final DownloadMissionHelper downloadMissionHelper; - - public DownloadDataSource(Context context) { - downloadMissionHelper = new DownloadMissionHelper(context); - } - - public ArrayList loadFinishedMissions() { - SQLiteDatabase database = downloadMissionHelper.getReadableDatabase(); - Cursor cursor = database.query(MISSIONS_TABLE_NAME, null, null, - null, null, null, DownloadMissionHelper.KEY_TIMESTAMP); - - int count = cursor.getCount(); - if (count == 0) return new ArrayList<>(1); - - ArrayList result = new ArrayList<>(count); - while (cursor.moveToNext()) { - result.add(DownloadMissionHelper.getMissionFromCursor(cursor)); - } - - return result; - } - - public void addMission(DownloadMission downloadMission) { - if (downloadMission == null) throw new NullPointerException("downloadMission is null"); - SQLiteDatabase database = downloadMissionHelper.getWritableDatabase(); - ContentValues values = DownloadMissionHelper.getValuesOfMission(downloadMission); - database.insert(MISSIONS_TABLE_NAME, null, values); - } - - public void deleteMission(Mission downloadMission) { - if (downloadMission == null) throw new NullPointerException("downloadMission is null"); - SQLiteDatabase database = downloadMissionHelper.getWritableDatabase(); - database.delete(MISSIONS_TABLE_NAME, - KEY_LOCATION + " = ? AND " + - KEY_NAME + " = ?", - new String[]{downloadMission.location, downloadMission.name}); - } - - public void updateMission(DownloadMission downloadMission) { - if (downloadMission == null) throw new NullPointerException("downloadMission is null"); - SQLiteDatabase database = downloadMissionHelper.getWritableDatabase(); - ContentValues values = DownloadMissionHelper.getValuesOfMission(downloadMission); - String whereClause = KEY_LOCATION + " = ? AND " + - KEY_NAME + " = ?"; - int rowsAffected = database.update(MISSIONS_TABLE_NAME, values, - whereClause, new String[]{downloadMission.location, downloadMission.name}); - if (rowsAffected != 1) { - Log.e(TAG, "Expected 1 row to be affected by update but got " + rowsAffected); - } - } -} diff --git a/app/src/main/java/us/shandian/giga/get/sqlite/DownloadMissionHelper.java b/app/src/main/java/us/shandian/giga/get/sqlite/DownloadMissionHelper.java deleted file mode 100644 index 6dadc98c8..000000000 --- a/app/src/main/java/us/shandian/giga/get/sqlite/DownloadMissionHelper.java +++ /dev/null @@ -1,112 +0,0 @@ -package us.shandian.giga.get.sqlite; - -import android.content.ContentValues; -import android.content.Context; -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteOpenHelper; - -import us.shandian.giga.get.DownloadMission; -import us.shandian.giga.get.FinishedMission; - -/** - * SQLiteHelper to store finished {@link us.shandian.giga.get.DownloadMission}'s - */ -public class DownloadMissionHelper extends SQLiteOpenHelper { - private final String TAG = "DownloadMissionHelper"; - - // TODO: use NewPipeSQLiteHelper ('s constants) when playlist branch is merged (?) - private static final String DATABASE_NAME = "downloads.db"; - - private static final int DATABASE_VERSION = 3; - - /** - * The table name of download missions - */ - static final String MISSIONS_TABLE_NAME = "download_missions"; - - /** - * The key to the directory location of the mission - */ - static final String KEY_LOCATION = "location"; - /** - * The key to the urls of a mission - */ - static final String KEY_SOURCE_URL = "url"; - /** - * The key to the name of a mission - */ - static final String KEY_NAME = "name"; - - /** - * The key to the done. - */ - static final String KEY_DONE = "bytes_downloaded"; - - static final String KEY_TIMESTAMP = "timestamp"; - - static final String KEY_KIND = "kind"; - - /** - * The statement to create the table - */ - private static final String MISSIONS_CREATE_TABLE = - "CREATE TABLE " + MISSIONS_TABLE_NAME + " (" + - KEY_LOCATION + " TEXT NOT NULL, " + - KEY_NAME + " TEXT NOT NULL, " + - KEY_SOURCE_URL + " TEXT NOT NULL, " + - KEY_DONE + " INTEGER NOT NULL, " + - KEY_TIMESTAMP + " INTEGER NOT NULL, " + - KEY_KIND + " TEXT NOT NULL, " + - " UNIQUE(" + KEY_LOCATION + ", " + KEY_NAME + "));"; - - public DownloadMissionHelper(Context context) { - super(context, DATABASE_NAME, null, DATABASE_VERSION); - } - - @Override - public void onCreate(SQLiteDatabase db) { - db.execSQL(MISSIONS_CREATE_TABLE); - } - - @Override - public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - if (oldVersion == 2) { - db.execSQL("ALTER TABLE " + MISSIONS_TABLE_NAME + " ADD COLUMN " + KEY_KIND + " TEXT;"); - } - } - - /** - * Returns all values of the download mission as ContentValues. - * - * @param downloadMission the download mission - * @return the content values - */ - public static ContentValues getValuesOfMission(DownloadMission downloadMission) { - ContentValues values = new ContentValues(); - values.put(KEY_SOURCE_URL, downloadMission.source); - values.put(KEY_LOCATION, downloadMission.location); - values.put(KEY_NAME, downloadMission.name); - values.put(KEY_DONE, downloadMission.done); - values.put(KEY_TIMESTAMP, downloadMission.timestamp); - values.put(KEY_KIND, String.valueOf(downloadMission.kind)); - return values; - } - - public static FinishedMission getMissionFromCursor(Cursor cursor) { - if (cursor == null) throw new NullPointerException("cursor is null"); - - String kind = cursor.getString(cursor.getColumnIndex(KEY_KIND)); - if (kind == null || kind.isEmpty()) kind = "?"; - - FinishedMission mission = new FinishedMission(); - mission.name = cursor.getString(cursor.getColumnIndexOrThrow(KEY_NAME)); - mission.location = cursor.getString(cursor.getColumnIndexOrThrow(KEY_LOCATION)); - mission.source = cursor.getString(cursor.getColumnIndexOrThrow(KEY_SOURCE_URL));; - mission.length = cursor.getLong(cursor.getColumnIndexOrThrow(KEY_DONE)); - mission.timestamp = cursor.getLong(cursor.getColumnIndexOrThrow(KEY_TIMESTAMP)); - mission.kind = kind.charAt(0); - - return mission; - } -} diff --git a/app/src/main/java/us/shandian/giga/get/sqlite/FinishedMissionStore.java b/app/src/main/java/us/shandian/giga/get/sqlite/FinishedMissionStore.java new file mode 100644 index 000000000..1fa987c88 --- /dev/null +++ b/app/src/main/java/us/shandian/giga/get/sqlite/FinishedMissionStore.java @@ -0,0 +1,237 @@ +package us.shandian.giga.get.sqlite; + +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.net.Uri; +import androidx.annotation.NonNull; +import android.util.Log; + +import java.io.File; +import java.util.ArrayList; + +import us.shandian.giga.get.DownloadMission; +import us.shandian.giga.get.FinishedMission; +import us.shandian.giga.get.Mission; +import us.shandian.giga.io.StoredFileHelper; + +/** + * SQLite helper to store finished {@link us.shandian.giga.get.FinishedMission}'s + */ +public class FinishedMissionStore extends SQLiteOpenHelper { + + // TODO: use NewPipeSQLiteHelper ('s constants) when playlist branch is merged (?) + private static final String DATABASE_NAME = "downloads.db"; + + private static final int DATABASE_VERSION = 4; + + /** + * The table name of download missions (old) + */ + private static final String MISSIONS_TABLE_NAME_v2 = "download_missions"; + + /** + * The table name of download missions + */ + private static final String FINISHED_TABLE_NAME = "finished_missions"; + + /** + * The key to the urls of a mission + */ + private static final String KEY_SOURCE = "url"; + + + /** + * The key to the done. + */ + private static final String KEY_DONE = "bytes_downloaded"; + + private static final String KEY_TIMESTAMP = "timestamp"; + + private static final String KEY_KIND = "kind"; + + private static final String KEY_PATH = "path"; + + /** + * The statement to create the table + */ + private static final String MISSIONS_CREATE_TABLE = + "CREATE TABLE " + FINISHED_TABLE_NAME + " (" + + KEY_PATH + " TEXT NOT NULL, " + + KEY_SOURCE + " TEXT NOT NULL, " + + KEY_DONE + " INTEGER NOT NULL, " + + KEY_TIMESTAMP + " INTEGER NOT NULL, " + + KEY_KIND + " TEXT NOT NULL, " + + " UNIQUE(" + KEY_TIMESTAMP + ", " + KEY_PATH + "));"; + + + private Context context; + + public FinishedMissionStore(Context context) { + super(context, DATABASE_NAME, null, DATABASE_VERSION); + this.context = context; + } + + @Override + public void onCreate(SQLiteDatabase db) { + db.execSQL(MISSIONS_CREATE_TABLE); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + if (oldVersion == 2) { + db.execSQL("ALTER TABLE " + MISSIONS_TABLE_NAME_v2 + " ADD COLUMN " + KEY_KIND + " TEXT;"); + oldVersion++; + } + + if (oldVersion == 3) { + final String KEY_LOCATION = "location"; + final String KEY_NAME = "name"; + + db.execSQL(MISSIONS_CREATE_TABLE); + + Cursor cursor = db.query(MISSIONS_TABLE_NAME_v2, null, null, + null, null, null, KEY_TIMESTAMP); + + int count = cursor.getCount(); + if (count > 0) { + db.beginTransaction(); + while (cursor.moveToNext()) { + ContentValues values = new ContentValues(); + values.put(KEY_SOURCE, cursor.getString(cursor.getColumnIndex(KEY_SOURCE))); + values.put(KEY_DONE, cursor.getString(cursor.getColumnIndex(KEY_DONE))); + values.put(KEY_TIMESTAMP, cursor.getLong(cursor.getColumnIndex(KEY_TIMESTAMP))); + values.put(KEY_KIND, cursor.getString(cursor.getColumnIndex(KEY_KIND))); + values.put(KEY_PATH, Uri.fromFile( + new File( + cursor.getString(cursor.getColumnIndex(KEY_LOCATION)), + cursor.getString(cursor.getColumnIndex(KEY_NAME)) + ) + ).toString()); + + db.insert(FINISHED_TABLE_NAME, null, values); + } + db.setTransactionSuccessful(); + db.endTransaction(); + } + + cursor.close(); + db.execSQL("DROP TABLE " + MISSIONS_TABLE_NAME_v2); + } + } + + /** + * Returns all values of the download mission as ContentValues. + * + * @param downloadMission the download mission + * @return the content values + */ + private ContentValues getValuesOfMission(@NonNull Mission downloadMission) { + ContentValues values = new ContentValues(); + values.put(KEY_SOURCE, downloadMission.source); + values.put(KEY_PATH, downloadMission.storage.getUri().toString()); + values.put(KEY_DONE, downloadMission.length); + values.put(KEY_TIMESTAMP, downloadMission.timestamp); + values.put(KEY_KIND, String.valueOf(downloadMission.kind)); + return values; + } + + private FinishedMission getMissionFromCursor(Cursor cursor) { + if (cursor == null) throw new NullPointerException("cursor is null"); + + String kind = cursor.getString(cursor.getColumnIndex(KEY_KIND)); + if (kind == null || kind.isEmpty()) kind = "?"; + + String path = cursor.getString(cursor.getColumnIndexOrThrow(KEY_PATH)); + + FinishedMission mission = new FinishedMission(); + + mission.source = cursor.getString(cursor.getColumnIndexOrThrow(KEY_SOURCE)); + mission.length = cursor.getLong(cursor.getColumnIndexOrThrow(KEY_DONE)); + mission.timestamp = cursor.getLong(cursor.getColumnIndexOrThrow(KEY_TIMESTAMP)); + mission.kind = kind.charAt(0); + + try { + mission.storage = new StoredFileHelper(context,null, Uri.parse(path), ""); + } catch (Exception e) { + Log.e("FinishedMissionStore", "failed to load the storage path of: " + path, e); + mission.storage = new StoredFileHelper(null, path, "", ""); + } + + return mission; + } + + + ////////////////////////////////// + // Data source methods + /////////////////////////////////// + + public ArrayList loadFinishedMissions() { + SQLiteDatabase database = getReadableDatabase(); + Cursor cursor = database.query(FINISHED_TABLE_NAME, null, null, + null, null, null, KEY_TIMESTAMP + " DESC"); + + int count = cursor.getCount(); + if (count == 0) return new ArrayList<>(1); + + ArrayList result = new ArrayList<>(count); + while (cursor.moveToNext()) { + result.add(getMissionFromCursor(cursor)); + } + + return result; + } + + public void addFinishedMission(DownloadMission downloadMission) { + if (downloadMission == null) throw new NullPointerException("downloadMission is null"); + SQLiteDatabase database = getWritableDatabase(); + ContentValues values = getValuesOfMission(downloadMission); + database.insert(FINISHED_TABLE_NAME, null, values); + } + + public void deleteMission(Mission mission) { + if (mission == null) throw new NullPointerException("mission is null"); + String ts = String.valueOf(mission.timestamp); + + SQLiteDatabase database = getWritableDatabase(); + + if (mission instanceof FinishedMission) { + if (mission.storage.isInvalid()) { + database.delete(FINISHED_TABLE_NAME, KEY_TIMESTAMP + " = ?", new String[]{ts}); + } else { + database.delete(FINISHED_TABLE_NAME, KEY_TIMESTAMP + " = ? AND " + KEY_PATH + " = ?", new String[]{ + ts, mission.storage.getUri().toString() + }); + } + } else { + throw new UnsupportedOperationException("DownloadMission"); + } + } + + public void updateMission(Mission mission) { + if (mission == null) throw new NullPointerException("mission is null"); + SQLiteDatabase database = getWritableDatabase(); + ContentValues values = getValuesOfMission(mission); + String ts = String.valueOf(mission.timestamp); + + int rowsAffected; + + if (mission instanceof FinishedMission) { + if (mission.storage.isInvalid()) { + rowsAffected = database.update(FINISHED_TABLE_NAME, values, KEY_TIMESTAMP + " = ?", new String[]{ts}); + } else { + rowsAffected = database.update(FINISHED_TABLE_NAME, values, KEY_PATH + " = ?", new String[]{ + mission.storage.getUri().toString() + }); + } + } else { + throw new UnsupportedOperationException("DownloadMission"); + } + + if (rowsAffected != 1) { + Log.e("FinishedMissionStore", "Expected 1 row to be affected by update but got " + rowsAffected); + } + } +} diff --git a/app/src/main/java/us/shandian/giga/postprocessing/io/ChunkFileInputStream.java b/app/src/main/java/us/shandian/giga/io/ChunkFileInputStream.java similarity index 78% rename from app/src/main/java/us/shandian/giga/postprocessing/io/ChunkFileInputStream.java rename to app/src/main/java/us/shandian/giga/io/ChunkFileInputStream.java index cd62c5d22..16a90fcee 100644 --- a/app/src/main/java/us/shandian/giga/postprocessing/io/ChunkFileInputStream.java +++ b/app/src/main/java/us/shandian/giga/io/ChunkFileInputStream.java @@ -1,153 +1,148 @@ -package us.shandian.giga.postprocessing.io; - -import org.schabi.newpipe.streams.io.SharpStream; - -import java.io.File; -import java.io.IOException; -import java.io.RandomAccessFile; - -public class ChunkFileInputStream extends SharpStream { - - private RandomAccessFile source; - private final long offset; - private final long length; - private long position; - - public ChunkFileInputStream(File file, long start, long end, String mode) throws IOException { - source = new RandomAccessFile(file, mode); - offset = start; - length = end - start; - position = 0; - - if (length < 1) { - source.close(); - throw new IOException("The chunk is empty or invalid"); - } - if (source.length() < end) { - try { - throw new IOException(String.format("invalid file length. expected = %s found = %s", end, source.length())); - } finally { - source.close(); - } - } - - source.seek(offset); - } - - /** - * Get absolute position on file - * - * @return the position - */ - public long getFilePointer() { - return offset + position; - } - - @Override - public int read() throws IOException { - if ((position + 1) > length) { - return 0; - } - - int res = source.read(); - if (res >= 0) { - position++; - } - - return res; - } - - @Override - public int read(byte b[]) throws IOException { - return read(b, 0, b.length); - } - - @Override - public int read(byte b[], int off, int len) throws IOException { - if ((position + len) > length) { - len = (int) (length - position); - } - if (len == 0) { - return 0; - } - - int res = source.read(b, off, len); - position += res; - - return res; - } - - @Override - public long skip(long pos) throws IOException { - pos = Math.min(pos + position, length); - - if (pos == 0) { - return 0; - } - - source.seek(offset + pos); - - long oldPos = position; - position = pos; - - return pos - oldPos; - } - - @Override - public int available() { - return (int) (length - position); - } - - @SuppressWarnings("EmptyCatchBlock") - @Override - public void dispose() { - try { - source.close(); - } catch (IOException err) { - } finally { - source = null; - } - } - - @Override - public boolean isDisposed() { - return source == null; - } - - @Override - public void rewind() throws IOException { - position = 0; - source.seek(offset); - } - - @Override - public boolean canRewind() { - return true; - } - - @Override - public boolean canRead() { - return true; - } - - @Override - public boolean canWrite() { - return false; - } - - @Override - public void write(byte value) { - } - - @Override - public void write(byte[] buffer) { - } - - @Override - public void write(byte[] buffer, int offset, int count) { - } - - @Override - public void flush() { - } -} +package us.shandian.giga.io; + +import org.schabi.newpipe.streams.io.SharpStream; + +import java.io.IOException; + +public class ChunkFileInputStream extends SharpStream { + + private SharpStream source; + private final long offset; + private final long length; + private long position; + + public ChunkFileInputStream(SharpStream target, long start) throws IOException { + this(target, start, target.length()); + } + + public ChunkFileInputStream(SharpStream target, long start, long end) throws IOException { + source = target; + offset = start; + length = end - start; + position = 0; + + if (length < 1) { + source.close(); + throw new IOException("The chunk is empty or invalid"); + } + if (source.length() < end) { + try { + throw new IOException(String.format("invalid file length. expected = %s found = %s", end, source.length())); + } finally { + source.close(); + } + } + + source.seek(offset); + } + + /** + * Get absolute position on file + * + * @return the position + */ + public long getFilePointer() { + return offset + position; + } + + @Override + public int read() throws IOException { + if ((position + 1) > length) { + return 0; + } + + int res = source.read(); + if (res >= 0) { + position++; + } + + return res; + } + + @Override + public int read(byte b[]) throws IOException { + return read(b, 0, b.length); + } + + @Override + public int read(byte b[], int off, int len) throws IOException { + if ((position + len) > length) { + len = (int) (length - position); + } + if (len == 0) { + return 0; + } + + int res = source.read(b, off, len); + position += res; + + return res; + } + + @Override + public long skip(long pos) throws IOException { + pos = Math.min(pos + position, length); + + if (pos == 0) { + return 0; + } + + source.seek(offset + pos); + + long oldPos = position; + position = pos; + + return pos - oldPos; + } + + @Override + public long available() { + return (int) (length - position); + } + + @SuppressWarnings("EmptyCatchBlock") + @Override + public void close() { + source.close(); + source = null; + } + + @Override + public boolean isClosed() { + return source == null; + } + + @Override + public void rewind() throws IOException { + position = 0; + source.seek(offset); + } + + @Override + public boolean canRewind() { + return true; + } + + @Override + public boolean canRead() { + return true; + } + + @Override + public boolean canWrite() { + return false; + } + + @Override + public void write(byte value) { + } + + @Override + public void write(byte[] buffer) { + } + + @Override + public void write(byte[] buffer, int offset, int count) { + } + +} diff --git a/app/src/main/java/us/shandian/giga/io/CircularFileWriter.java b/app/src/main/java/us/shandian/giga/io/CircularFileWriter.java new file mode 100644 index 000000000..e2afb9202 --- /dev/null +++ b/app/src/main/java/us/shandian/giga/io/CircularFileWriter.java @@ -0,0 +1,497 @@ +package us.shandian.giga.io; + +import androidx.annotation.NonNull; + +import org.schabi.newpipe.streams.io.SharpStream; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; + +public class CircularFileWriter extends SharpStream { + + private final static int QUEUE_BUFFER_SIZE = 8 * 1024;// 8 KiB + private final static int COPY_BUFFER_SIZE = 128 * 1024; // 128 KiB + private final static int NOTIFY_BYTES_INTERVAL = 64 * 1024;// 64 KiB + private final static int THRESHOLD_AUX_LENGTH = 15 * 1024 * 1024;// 15 MiB + + private OffsetChecker callback; + + public ProgressReport onProgress; + public WriteErrorHandle onWriteError; + + private long reportPosition; + private long maxLengthKnown = -1; + + private BufferedFile out; + private BufferedFile aux; + + public CircularFileWriter(SharpStream target, File temp, OffsetChecker checker) throws IOException { + if (checker == null) { + throw new NullPointerException("checker is null"); + } + + if (!temp.exists()) { + if (!temp.createNewFile()) { + throw new IOException("Cannot create a temporal file"); + } + } + + aux = new BufferedFile(temp); + out = new BufferedFile(target); + + callback = checker; + + reportPosition = NOTIFY_BYTES_INTERVAL; + } + + private void flushAuxiliar(long amount) throws IOException { + if (aux.length < 1) { + return; + } + + out.flush(); + aux.flush(); + + boolean underflow = aux.offset < aux.length || out.offset < out.length; + byte[] buffer = new byte[COPY_BUFFER_SIZE]; + + aux.target.seek(0); + out.target.seek(out.length); + + long length = amount; + while (length > 0) { + int read = (int) Math.min(length, Integer.MAX_VALUE); + read = aux.target.read(buffer, 0, Math.min(read, buffer.length)); + + if (read < 1) { + amount -= length; + break; + } + + out.writeProof(buffer, read); + length -= read; + } + + if (underflow) { + if (out.offset >= out.length) { + // calculate the aux underflow pointer + if (aux.offset < amount) { + out.offset += aux.offset; + aux.offset = 0; + out.target.seek(out.offset); + } else { + aux.offset -= amount; + out.offset = out.length + amount; + } + } else { + aux.offset = 0; + } + } else { + out.offset += amount; + aux.offset -= amount; + } + + out.length += amount; + + if (out.length > maxLengthKnown) { + maxLengthKnown = out.length; + } + + if (amount < aux.length) { + // move the excess data to the beginning of the file + long readOffset = amount; + long writeOffset = 0; + + aux.length -= amount; + length = aux.length; + while (length > 0) { + int read = (int) Math.min(length, Integer.MAX_VALUE); + read = aux.target.read(buffer, 0, Math.min(read, buffer.length)); + + aux.target.seek(writeOffset); + aux.writeProof(buffer, read); + + writeOffset += read; + readOffset += read; + length -= read; + + aux.target.seek(readOffset); + } + + aux.target.setLength(aux.length); + return; + } + + if (aux.length > THRESHOLD_AUX_LENGTH) { + aux.target.setLength(THRESHOLD_AUX_LENGTH);// or setLength(0); + } + + aux.reset(); + } + + /** + * Flush any buffer and close the output file. Use this method if the + * operation is successful + * + * @return the final length of the file + * @throws IOException if an I/O error occurs + */ + public long finalizeFile() throws IOException { + flushAuxiliar(aux.length); + + out.flush(); + + // change file length (if required) + long length = Math.max(maxLengthKnown, out.length); + if (length != out.target.length()) { + out.target.setLength(length); + } + + close(); + + return length; + } + + /** + * Close the file without flushing any buffer + */ + @Override + public void close() { + if (out != null) { + out.close(); + out = null; + } + if (aux != null) { + aux.close(); + aux = null; + } + } + + @Override + public void write(byte b) throws IOException { + write(new byte[]{b}, 0, 1); + } + + @Override + public void write(byte b[]) throws IOException { + write(b, 0, b.length); + } + + @Override + public void write(byte b[], int off, int len) throws IOException { + if (len == 0) { + return; + } + + long available; + long offsetOut = out.getOffset(); + long offsetAux = aux.getOffset(); + long end = callback.check(); + + if (end == -1) { + available = Integer.MAX_VALUE; + } else if (end < offsetOut) { + throw new IOException("The reported offset is invalid: " + end + "<" + offsetOut); + } else { + available = end - offsetOut; + } + + boolean usingAux = aux.length > 0 && offsetOut >= out.length; + boolean underflow = offsetAux < aux.length || offsetOut < out.length; + + if (usingAux) { + // before continue calculate the final length of aux + long length = offsetAux + len; + if (underflow) { + if (aux.length > length) { + length = aux.length;// the length is not changed + } + } else { + length = aux.length + len; + } + + aux.write(b, off, len); + + if (length >= THRESHOLD_AUX_LENGTH && length <= available) { + flushAuxiliar(available); + } + } else { + if (underflow) { + available = out.length - offsetOut; + } + + int length = Math.min(len, (int) available); + out.write(b, off, length); + + len -= length; + off += length; + + if (len > 0) { + aux.write(b, off, len); + } + } + + if (onProgress != null) { + long absoluteOffset = out.getOffset() + aux.getOffset(); + if (absoluteOffset > reportPosition) { + reportPosition = absoluteOffset + NOTIFY_BYTES_INTERVAL; + onProgress.report(absoluteOffset); + } + } + } + + @Override + public void flush() throws IOException { + aux.flush(); + out.flush(); + + long total = out.length + aux.length; + if (total > maxLengthKnown) { + maxLengthKnown = total;// save the current file length in case the method {@code rewind()} is called + } + } + + @Override + public long skip(long amount) throws IOException { + seek(out.getOffset() + aux.getOffset() + amount); + return amount; + } + + @Override + public void rewind() throws IOException { + if (onProgress != null) { + onProgress.report(-out.length - aux.length);// rollback the whole progress + } + + seek(0); + + reportPosition = NOTIFY_BYTES_INTERVAL; + } + + @Override + public void seek(long offset) throws IOException { + long total = out.length + aux.length; + + if (offset == total) { + // do not ignore the seek offset if a underflow exists + long relativeOffset = out.getOffset() + aux.getOffset(); + if (relativeOffset == total) { + return; + } + } + + // flush everything, avoid any underflow + flush(); + + if (offset < 0 || offset > total) { + throw new IOException("desired offset is outside of range=0-" + total + " offset=" + offset); + } + + if (offset > out.length) { + out.seek(out.length); + aux.seek(offset - out.length); + } else { + out.seek(offset); + aux.seek(0); + } + } + + @Override + public boolean isClosed() { + return out == null; + } + + @Override + public boolean canRewind() { + return true; + } + + @Override + public boolean canWrite() { + return true; + } + + @Override + public boolean canSeek() { + return true; + } + + // + @Override + public boolean canRead() { + return false; + } + + @Override + public int read() { + throw new UnsupportedOperationException("write-only"); + } + + @Override + public int read(byte[] buffer + ) { + throw new UnsupportedOperationException("write-only"); + } + + @Override + public int read(byte[] buffer, int offset, int count + ) { + throw new UnsupportedOperationException("write-only"); + } + + @Override + public long available() { + throw new UnsupportedOperationException("write-only"); + } + // + + public interface OffsetChecker { + + /** + * Checks the amount of available space ahead + * + * @return absolute offset in the file where no more data SHOULD NOT be + * written. If the value is -1 the whole file will be used + */ + long check(); + } + + public interface ProgressReport { + + /** + * Report the size of the new file + * + * @param progress the new size + */ + void report(long progress); + } + + public interface WriteErrorHandle { + + /** + * Attempts to handle a I/O exception + * + * @param err the cause + * @return {@code true} to retry and continue, otherwise, {@code false} + * and throw the exception + */ + boolean handle(Exception err); + } + + class BufferedFile { + + protected final SharpStream target; + + private long offset; + protected long length; + + private byte[] queue = new byte[QUEUE_BUFFER_SIZE]; + private int queueSize; + + BufferedFile(File file) throws FileNotFoundException { + this.target = new FileStream(file); + } + + BufferedFile(SharpStream target) { + this.target = target; + } + + protected long getOffset() { + return offset + queueSize;// absolute offset in the file + } + + protected void close() { + queue = null; + target.close(); + } + + protected void write(byte b[], int off, int len) throws IOException { + while (len > 0) { + // if the queue is full, the method available() will flush the queue + int read = Math.min(available(), len); + + // enqueue incoming buffer + System.arraycopy(b, off, queue, queueSize, read); + queueSize += read; + + len -= read; + off += read; + } + + long total = offset + queueSize; + if (total > length) { + length = total;// save length + } + } + + void flush() throws IOException { + writeProof(queue, queueSize); + offset += queueSize; + queueSize = 0; + } + + protected void rewind() throws IOException { + offset = 0; + target.seek(0); + } + + protected int available() throws IOException { + if (queueSize >= queue.length) { + flush(); + return queue.length; + } + + return queue.length - queueSize; + } + + void reset() throws IOException { + offset = 0; + length = 0; + target.seek(0); + } + + protected void seek(long absoluteOffset) throws IOException { + if (absoluteOffset == offset) { + return;// nothing to do + } + offset = absoluteOffset; + target.seek(absoluteOffset); + } + + void writeProof(byte[] buffer, int length) throws IOException { + if (onWriteError == null) { + target.write(buffer, 0, length); + return; + } + + while (true) { + try { + target.write(buffer, 0, length); + return; + } catch (Exception e) { + if (!onWriteError.handle(e)) { + throw e;// give up + } + } + } + } + + @NonNull + @Override + public String toString() { + String absLength; + + try { + absLength = Long.toString(target.length()); + } catch (IOException e) { + absLength = "[" + e.getLocalizedMessage() + "]"; + } + + return String.format( + "offset=%s length=%s queue=%s absLength=%s", + offset, length, queueSize, absLength + ); + } + } +} diff --git a/app/src/main/java/us/shandian/giga/postprocessing/io/FileStream.java b/app/src/main/java/us/shandian/giga/io/FileStream.java similarity index 57% rename from app/src/main/java/us/shandian/giga/postprocessing/io/FileStream.java rename to app/src/main/java/us/shandian/giga/io/FileStream.java index c1b675eef..2cc4d5db8 100644 --- a/app/src/main/java/us/shandian/giga/postprocessing/io/FileStream.java +++ b/app/src/main/java/us/shandian/giga/io/FileStream.java @@ -1,126 +1,131 @@ -package us.shandian.giga.postprocessing.io; - -import org.schabi.newpipe.streams.io.SharpStream; - -import java.io.IOException; -import java.io.RandomAccessFile; -import java.nio.channels.FileChannel; - -/** - * @author kapodamy - */ -public class FileStream extends SharpStream { - - public enum Mode { - Read, - ReadWrite - } - - public RandomAccessFile source; - private final Mode mode; - - public FileStream(String path, Mode mode) throws IOException { - String flags; - - if (mode == Mode.Read) { - flags = "r"; - } else { - flags = "rw"; - } - - this.mode = mode; - source = new RandomAccessFile(path, flags); - } - - @Override - public int read() throws IOException { - return source.read(); - } - - @Override - public int read(byte b[]) throws IOException { - return read(b, 0, b.length); - } - - @Override - public int read(byte b[], int off, int len) throws IOException { - return source.read(b, off, len); - } - - @Override - public long skip(long pos) throws IOException { - FileChannel fc = source.getChannel(); - fc.position(fc.position() + pos); - return pos; - } - - @Override - public int available() { - try { - return (int) (source.length() - source.getFilePointer()); - } catch (IOException ex) { - return 0; - } - } - - @SuppressWarnings("EmptyCatchBlock") - @Override - public void dispose() { - try { - source.close(); - } catch (IOException err) { - - } finally { - source = null; - } - } - - @Override - public boolean isDisposed() { - return source == null; - } - - @Override - public void rewind() throws IOException { - source.getChannel().position(0); - } - - @Override - public boolean canRewind() { - return true; - } - - @Override - public boolean canRead() { - return mode == Mode.Read || mode == Mode.ReadWrite; - } - - @Override - public boolean canWrite() { - return mode == Mode.ReadWrite; - } - - @Override - public void write(byte value) throws IOException { - source.write(value); - } - - @Override - public void write(byte[] buffer) throws IOException { - source.write(buffer); - } - - @Override - public void write(byte[] buffer, int offset, int count) throws IOException { - source.write(buffer, offset, count); - } - - @Override - public void flush() { - } - - @Override - public void setLength(long length) throws IOException { - source.setLength(length); - } -} +package us.shandian.giga.io; + +import androidx.annotation.NonNull; + +import org.schabi.newpipe.streams.io.SharpStream; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; + +/** + * @author kapodamy + */ +public class FileStream extends SharpStream { + + public RandomAccessFile source; + + public FileStream(@NonNull File target) throws FileNotFoundException { + this.source = new RandomAccessFile(target, "rw"); + } + + public FileStream(@NonNull String path) throws FileNotFoundException { + this.source = new RandomAccessFile(path, "rw"); + } + + @Override + public int read() throws IOException { + return source.read(); + } + + @Override + public int read(byte b[]) throws IOException { + return source.read(b); + } + + @Override + public int read(byte b[], int off, int len) throws IOException { + return source.read(b, off, len); + } + + @Override + public long skip(long pos) throws IOException { + return source.skipBytes((int) pos); + } + + @Override + public long available() { + try { + return source.length() - source.getFilePointer(); + } catch (IOException e) { + return 0; + } + } + + @Override + public void close() { + if (source == null) return; + try { + source.close(); + } catch (IOException err) { + // nothing to do + } + source = null; + } + + @Override + public boolean isClosed() { + return source == null; + } + + @Override + public void rewind() throws IOException { + source.seek(0); + } + + @Override + public boolean canRewind() { + return true; + } + + @Override + public boolean canRead() { + return true; + } + + @Override + public boolean canWrite() { + return true; + } + + @Override + public boolean canSeek() { + return true; + } + + @Override + public boolean canSetLength() { + return true; + } + + @Override + public void write(byte value) throws IOException { + source.write(value); + } + + @Override + public void write(byte[] buffer) throws IOException { + source.write(buffer); + } + + @Override + public void write(byte[] buffer, int offset, int count) throws IOException { + source.write(buffer, offset, count); + } + + @Override + public void setLength(long length) throws IOException { + source.setLength(length); + } + + @Override + public void seek(long offset) throws IOException { + source.seek(offset); + } + + @Override + public long length() throws IOException { + return source.length(); + } +} diff --git a/app/src/main/java/us/shandian/giga/io/FileStreamSAF.java b/app/src/main/java/us/shandian/giga/io/FileStreamSAF.java new file mode 100644 index 000000000..fc716b4f9 --- /dev/null +++ b/app/src/main/java/us/shandian/giga/io/FileStreamSAF.java @@ -0,0 +1,145 @@ +package us.shandian.giga.io; + +import android.content.ContentResolver; +import android.net.Uri; +import android.os.ParcelFileDescriptor; +import androidx.annotation.NonNull; +import android.util.Log; + +import org.schabi.newpipe.streams.io.SharpStream; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.channels.FileChannel; + +public class FileStreamSAF extends SharpStream { + + private final FileInputStream in; + private final FileOutputStream out; + private final FileChannel channel; + private final ParcelFileDescriptor file; + + private boolean disposed; + + public FileStreamSAF(@NonNull ContentResolver contentResolver, Uri fileUri) throws IOException { + // Notes: + // the file must exists first + // ¡read-write mode must allow seek! + // It is not guaranteed to work with files in the cloud (virtual files), tested in local storage devices + + file = contentResolver.openFileDescriptor(fileUri, "rw"); + + if (file == null) { + throw new IOException("Cannot get the ParcelFileDescriptor for " + fileUri.toString()); + } + + in = new FileInputStream(file.getFileDescriptor()); + out = new FileOutputStream(file.getFileDescriptor()); + channel = out.getChannel();// or use in.getChannel() + } + + @Override + public int read() throws IOException { + return in.read(); + } + + @Override + public int read(byte[] buffer) throws IOException { + return in.read(buffer); + } + + @Override + public int read(byte[] buffer, int offset, int count) throws IOException { + return in.read(buffer, offset, count); + } + + @Override + public long skip(long amount) throws IOException { + return in.skip(amount);// ¿or use channel.position(channel.position() + amount)? + } + + @Override + public long available() { + try { + return in.available(); + } catch (IOException e) { + return 0;// ¡but not -1! + } + } + + @Override + public void rewind() throws IOException { + seek(0); + } + + @Override + public void close() { + try { + disposed = true; + + file.close(); + in.close(); + out.close(); + channel.close(); + } catch (IOException e) { + Log.e("FileStreamSAF", "close() error", e); + } + } + + @Override + public boolean isClosed() { + return disposed; + } + + @Override + public boolean canRewind() { + return true; + } + + @Override + public boolean canRead() { + return true; + } + + @Override + public boolean canWrite() { + return true; + } + + public boolean canSetLength() { + return true; + } + + public boolean canSeek() { + return true; + } + + @Override + public void write(byte value) throws IOException { + out.write(value); + } + + @Override + public void write(byte[] buffer) throws IOException { + out.write(buffer); + } + + @Override + public void write(byte[] buffer, int offset, int count) throws IOException { + out.write(buffer, offset, count); + } + + public void setLength(long length) throws IOException { + channel.truncate(length); + } + + public void seek(long offset) throws IOException { + channel.position(offset); + } + + @Override + public long length() throws IOException { + return channel.size(); + } +} diff --git a/app/src/main/java/us/shandian/giga/postprocessing/io/SharpInputStream.java b/app/src/main/java/us/shandian/giga/io/SharpInputStream.java similarity index 85% rename from app/src/main/java/us/shandian/giga/postprocessing/io/SharpInputStream.java rename to app/src/main/java/us/shandian/giga/io/SharpInputStream.java index 52e0775da..0d6320b53 100644 --- a/app/src/main/java/us/shandian/giga/postprocessing/io/SharpInputStream.java +++ b/app/src/main/java/us/shandian/giga/io/SharpInputStream.java @@ -1,59 +1,61 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package us.shandian.giga.postprocessing.io; - -import android.support.annotation.NonNull; - -import org.schabi.newpipe.streams.io.SharpStream; - -import java.io.IOException; -import java.io.InputStream; - -/** - * Wrapper for the classic {@link java.io.InputStream} - * @author kapodamy - */ -public class SharpInputStream extends InputStream { - - private final SharpStream base; - - public SharpInputStream(SharpStream base) throws IOException { - if (!base.canRead()) { - throw new IOException("The provided stream is not readable"); - } - this.base = base; - } - - @Override - public int read() throws IOException { - return base.read(); - } - - @Override - public int read(@NonNull byte[] bytes) throws IOException { - return base.read(bytes); - } - - @Override - public int read(@NonNull byte[] bytes, int i, int i1) throws IOException { - return base.read(bytes, i, i1); - } - - @Override - public long skip(long l) throws IOException { - return base.skip(l); - } - - @Override - public int available() { - return base.available(); - } - - @Override - public void close() { - base.dispose(); - } -} +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package us.shandian.giga.io; + +import androidx.annotation.NonNull; + +import org.schabi.newpipe.streams.io.SharpStream; + +import java.io.IOException; +import java.io.InputStream; + +/** + * Wrapper for the classic {@link java.io.InputStream} + * + * @author kapodamy + */ +public class SharpInputStream extends InputStream { + + private final SharpStream base; + + public SharpInputStream(SharpStream base) throws IOException { + if (!base.canRead()) { + throw new IOException("The provided stream is not readable"); + } + this.base = base; + } + + @Override + public int read() throws IOException { + return base.read(); + } + + @Override + public int read(@NonNull byte[] bytes) throws IOException { + return base.read(bytes); + } + + @Override + public int read(@NonNull byte[] bytes, int i, int i1) throws IOException { + return base.read(bytes, i, i1); + } + + @Override + public long skip(long l) throws IOException { + return base.skip(l); + } + + @Override + public int available() { + long res = base.available(); + return res > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) res; + } + + @Override + public void close() { + base.close(); + } +} diff --git a/app/src/main/java/us/shandian/giga/io/StoredDirectoryHelper.java b/app/src/main/java/us/shandian/giga/io/StoredDirectoryHelper.java new file mode 100644 index 000000000..c1a63bb6a --- /dev/null +++ b/app/src/main/java/us/shandian/giga/io/StoredDirectoryHelper.java @@ -0,0 +1,289 @@ +package us.shandian.giga.io; + +import android.annotation.TargetApi; +import android.content.ContentResolver; +import android.content.Context; +import android.content.Intent; +import android.database.Cursor; +import android.net.Uri; +import android.os.Build; +import android.provider.DocumentsContract; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.documentfile.provider.DocumentFile; + +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.util.ArrayList; +import java.util.Collections; + +import static android.provider.DocumentsContract.Document.COLUMN_DISPLAY_NAME; +import static android.provider.DocumentsContract.Root.COLUMN_DOCUMENT_ID; + + +public class StoredDirectoryHelper { + public final static int PERMISSION_FLAGS = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION; + + private File ioTree; + private DocumentFile docTree; + + private Context context; + + private String tag; + + public StoredDirectoryHelper(@NonNull Context context, @NonNull Uri path, String tag) throws IOException { + this.tag = tag; + + if (ContentResolver.SCHEME_FILE.equalsIgnoreCase(path.getScheme())) { + this.ioTree = new File(URI.create(path.toString())); + return; + } + + this.context = context; + + try { + this.context.getContentResolver().takePersistableUriPermission(path, PERMISSION_FLAGS); + } catch (Exception e) { + throw new IOException(e); + } + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) + throw new IOException("Storage Access Framework with Directory API is not available"); + + this.docTree = DocumentFile.fromTreeUri(context, path); + + if (this.docTree == null) + throw new IOException("Failed to create the tree from Uri"); + } + + @TargetApi(Build.VERSION_CODES.KITKAT) + public StoredDirectoryHelper(@NonNull URI location, String tag) { + ioTree = new File(location); + this.tag = tag; + } + + public StoredFileHelper createFile(String filename, String mime) { + return createFile(filename, mime, false); + } + + public StoredFileHelper createUniqueFile(String name, String mime) { + ArrayList matches = new ArrayList<>(); + String[] filename = splitFilename(name); + String lcFilename = filename[0].toLowerCase(); + + if (docTree == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + for (File file : ioTree.listFiles()) + addIfStartWith(matches, lcFilename, file.getName()); + } else { + // warning: SAF file listing is very slow + Uri docTreeChildren = DocumentsContract.buildChildDocumentsUriUsingTree( + docTree.getUri(), DocumentsContract.getDocumentId(docTree.getUri()) + ); + + String[] projection = new String[]{COLUMN_DISPLAY_NAME}; + String selection = "(LOWER(" + COLUMN_DISPLAY_NAME + ") LIKE ?%"; + ContentResolver cr = context.getContentResolver(); + + try (Cursor cursor = cr.query(docTreeChildren, projection, selection, new String[]{lcFilename}, null)) { + if (cursor != null) { + while (cursor.moveToNext()) + addIfStartWith(matches, lcFilename, cursor.getString(0)); + } + } + } + + if (matches.size() < 1) { + return createFile(name, mime, true); + } else { + // check if the filename is in use + String lcName = name.toLowerCase(); + for (String testName : matches) { + if (testName.equals(lcName)) { + lcName = null; + break; + } + } + + // check if not in use + if (lcName != null) return createFile(name, mime, true); + } + + Collections.sort(matches, String::compareTo); + + for (int i = 1; i < 1000; i++) { + if (Collections.binarySearch(matches, makeFileName(lcFilename, i, filename[1])) < 0) + return createFile(makeFileName(filename[0], i, filename[1]), mime, true); + } + + return createFile(String.valueOf(System.currentTimeMillis()).concat(filename[1]), mime, false); + } + + private StoredFileHelper createFile(String filename, String mime, boolean safe) { + StoredFileHelper storage; + + try { + if (docTree == null) + storage = new StoredFileHelper(ioTree, filename, mime); + else + storage = new StoredFileHelper(context, docTree, filename, mime, safe); + } catch (IOException e) { + return null; + } + + storage.tag = tag; + + return storage; + } + + public Uri getUri() { + return docTree == null ? Uri.fromFile(ioTree) : docTree.getUri(); + } + + public boolean exists() { + return docTree == null ? ioTree.exists() : docTree.exists(); + } + + /** + * Indicates whatever if is possible access using the {@code java.io} API + * + * @return {@code true} for Java I/O API, otherwise, {@code false} for Storage Access Framework + */ + public boolean isDirect() { + return docTree == null; + } + + /** + * Only using Java I/O. Creates the directory named by this abstract pathname, including any + * necessary but nonexistent parent directories. Note that if this + * operation fails it may have succeeded in creating some of the necessary + * parent directories. + * + * @return true if and only if the directory was created, + * along with all necessary parent directories or already exists; false + * otherwise + */ + public boolean mkdirs() { + if (docTree == null) { + return ioTree.exists() || ioTree.mkdirs(); + } + + if (docTree.exists()) return true; + + try { + DocumentFile parent; + String child = docTree.getName(); + + while (true) { + parent = docTree.getParentFile(); + if (parent == null || child == null) break; + if (parent.exists()) return true; + + parent.createDirectory(child); + + child = parent.getName();// for the next iteration + } + } catch (Exception e) { + // no more parent directories or unsupported by the storage provider + } + + return false; + } + + public String getTag() { + return tag; + } + + public Uri findFile(String filename) { + if (docTree == null) { + File res = new File(ioTree, filename); + return res.exists() ? Uri.fromFile(res) : null; + } + + DocumentFile res = findFileSAFHelper(context, docTree, filename); + return res == null ? null : res.getUri(); + } + + public boolean canWrite() { + return docTree == null ? ioTree.canWrite() : docTree.canWrite(); + } + + @NonNull + @Override + public String toString() { + return docTree == null ? Uri.fromFile(ioTree).toString() : docTree.getUri().toString(); + } + + + //////////////////// + // Utils + /////////////////// + + private static void addIfStartWith(ArrayList list, @NonNull String base, String str) { + if (str == null || str.isEmpty()) return; + str = str.toLowerCase(); + if (str.startsWith(base)) list.add(str); + } + + private static String[] splitFilename(@NonNull String filename) { + int dotIndex = filename.lastIndexOf('.'); + + if (dotIndex < 0 || (dotIndex == filename.length() - 1)) + return new String[]{filename, ""}; + + return new String[]{filename.substring(0, dotIndex), filename.substring(dotIndex)}; + } + + private static String makeFileName(String name, int idx, String ext) { + return name.concat(" (").concat(String.valueOf(idx)).concat(")").concat(ext); + } + + /** + * Fast (but not enough) file/directory finder under the storage access framework + * + * @param context The context + * @param tree Directory where search + * @param filename Target filename + * @return A {@link DocumentFile} contain the reference, otherwise, null + */ + static DocumentFile findFileSAFHelper(@Nullable Context context, DocumentFile tree, String filename) { + if (context == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + return tree.findFile(filename);// warning: this is very slow + } + + if (!tree.canRead()) return null;// missing read permission + + final int name = 0; + final int documentId = 1; + + // LOWER() SQL function is not supported + String selection = COLUMN_DISPLAY_NAME + " = ?"; + //String selection = COLUMN_DISPLAY_NAME + " LIKE ?%"; + + Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree( + tree.getUri(), DocumentsContract.getDocumentId(tree.getUri()) + ); + String[] projection = {COLUMN_DISPLAY_NAME, COLUMN_DOCUMENT_ID}; + ContentResolver contentResolver = context.getContentResolver(); + + filename = filename.toLowerCase(); + + try (Cursor cursor = contentResolver.query(childrenUri, projection, selection, new String[]{filename}, null)) { + if (cursor == null) return null; + + while (cursor.moveToNext()) { + if (cursor.isNull(name) || !cursor.getString(name).toLowerCase().startsWith(filename)) + continue; + + return DocumentFile.fromSingleUri( + context, DocumentsContract.buildDocumentUriUsingTree( + tree.getUri(), cursor.getString(documentId) + ) + ); + } + } + + return null; + } + +} diff --git a/app/src/main/java/us/shandian/giga/io/StoredFileHelper.java b/app/src/main/java/us/shandian/giga/io/StoredFileHelper.java new file mode 100644 index 000000000..463c3ce5f --- /dev/null +++ b/app/src/main/java/us/shandian/giga/io/StoredFileHelper.java @@ -0,0 +1,382 @@ +package us.shandian.giga.io; + +import android.annotation.TargetApi; +import android.content.ContentResolver; +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.os.Build; +import android.provider.DocumentsContract; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import androidx.documentfile.provider.DocumentFile; + +import org.schabi.newpipe.streams.io.SharpStream; + +import java.io.File; +import java.io.IOException; +import java.io.Serializable; +import java.net.URI; + +public class StoredFileHelper implements Serializable { + private static final long serialVersionUID = 0L; + public static final String DEFAULT_MIME = "application/octet-stream"; + + private transient DocumentFile docFile; + private transient DocumentFile docTree; + private transient File ioFile; + private transient Context context; + + protected String source; + private String sourceTree; + + protected String tag; + + private String srcName; + private String srcType; + + public StoredFileHelper(@Nullable Uri parent, String filename, String mime, String tag) { + this.source = null;// this instance will be "invalid" see invalidate()/isInvalid() methods + + this.srcName = filename; + this.srcType = mime == null ? DEFAULT_MIME : mime; + if (parent != null) this.sourceTree = parent.toString(); + + this.tag = tag; + } + + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + StoredFileHelper(@Nullable Context context, DocumentFile tree, String filename, String mime, boolean safe) throws IOException { + this.docTree = tree; + this.context = context; + + DocumentFile res; + + if (safe) { + // no conflicts (the filename is not in use) + res = this.docTree.createFile(mime, filename); + if (res == null) throw new IOException("Cannot create the file"); + } else { + res = createSAF(context, mime, filename); + } + + this.docFile = res; + + this.source = docFile.getUri().toString(); + this.sourceTree = docTree.getUri().toString(); + + this.srcName = this.docFile.getName(); + this.srcType = this.docFile.getType(); + } + + StoredFileHelper(File location, String filename, String mime) throws IOException { + this.ioFile = new File(location, filename); + + if (this.ioFile.exists()) { + if (!this.ioFile.isFile() && !this.ioFile.delete()) + throw new IOException("The filename is already in use by non-file entity and cannot overwrite it"); + } else { + if (!this.ioFile.createNewFile()) + throw new IOException("Cannot create the file"); + } + + this.source = Uri.fromFile(this.ioFile).toString(); + this.sourceTree = Uri.fromFile(location).toString(); + + this.srcName = ioFile.getName(); + this.srcType = mime; + } + + @TargetApi(Build.VERSION_CODES.KITKAT) + public StoredFileHelper(Context context, @Nullable Uri parent, @NonNull Uri path, String tag) throws IOException { + this.tag = tag; + this.source = path.toString(); + + if (path.getScheme() == null || path.getScheme().equalsIgnoreCase(ContentResolver.SCHEME_FILE)) { + this.ioFile = new File(URI.create(this.source)); + } else { + DocumentFile file = DocumentFile.fromSingleUri(context, path); + + if (file == null) throw new RuntimeException("SAF not available"); + + this.context = context; + + if (file.getName() == null) { + this.source = null; + return; + } else { + this.docFile = file; + takePermissionSAF(); + } + } + + if (parent != null) { + if (!ContentResolver.SCHEME_FILE.equals(parent.getScheme())) + this.docTree = DocumentFile.fromTreeUri(context, parent); + + this.sourceTree = parent.toString(); + } + + this.srcName = getName(); + this.srcType = getType(); + } + + + public static StoredFileHelper deserialize(@NonNull StoredFileHelper storage, Context context) throws IOException { + Uri treeUri = storage.sourceTree == null ? null : Uri.parse(storage.sourceTree); + + if (storage.isInvalid()) + return new StoredFileHelper(treeUri, storage.srcName, storage.srcType, storage.tag); + + StoredFileHelper instance = new StoredFileHelper(context, treeUri, Uri.parse(storage.source), storage.tag); + + // under SAF, if the target document is deleted, conserve the filename and mime + if (instance.srcName == null) instance.srcName = storage.srcName; + if (instance.srcType == null) instance.srcType = storage.srcType; + + return instance; + } + + public static void requestSafWithFileCreation(@NonNull Fragment who, int requestCode, String filename, String mime) { + // SAF notes: + // ACTION_OPEN_DOCUMENT Do not let you create the file, useful for overwrite files + // ACTION_CREATE_DOCUMENT No overwrite support, useless the file provider resolve the conflict + + Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT) + .addCategory(Intent.CATEGORY_OPENABLE) + .setType(mime) + .putExtra(Intent.EXTRA_TITLE, filename) + .addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION | StoredDirectoryHelper.PERMISSION_FLAGS) + .putExtra("android.content.extra.SHOW_ADVANCED", true);// hack, show all storage disks + + who.startActivityForResult(intent, requestCode); + } + + + public SharpStream getStream() throws IOException { + invalid(); + + if (docFile == null) + return new FileStream(ioFile); + else + return new FileStreamSAF(context.getContentResolver(), docFile.getUri()); + } + + /** + * Indicates whatever if is possible access using the {@code java.io} API + * + * @return {@code true} for Java I/O API, otherwise, {@code false} for Storage Access Framework + */ + public boolean isDirect() { + invalid(); + + return docFile == null; + } + + public boolean isInvalid() { + return source == null; + } + + public Uri getUri() { + invalid(); + + return docFile == null ? Uri.fromFile(ioFile) : docFile.getUri(); + } + + public Uri getParentUri() { + invalid(); + + return sourceTree == null ? null : Uri.parse(sourceTree); + } + + public void truncate() throws IOException { + invalid(); + + try (SharpStream fs = getStream()) { + fs.setLength(0); + } + } + + public boolean delete() { + if (source == null) return true; + if (docFile == null) return ioFile.delete(); + + + boolean res = docFile.delete(); + + try { + int flags = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION; + context.getContentResolver().releasePersistableUriPermission(docFile.getUri(), flags); + } catch (Exception ex) { + // nothing to do + } + + return res; + } + + public long length() { + invalid(); + + return docFile == null ? ioFile.length() : docFile.length(); + } + + public boolean canWrite() { + if (source == null) return false; + return docFile == null ? ioFile.canWrite() : docFile.canWrite(); + } + + public String getName() { + if (source == null) + return srcName; + else if (docFile == null) + return ioFile.getName(); + + String name = docFile.getName(); + return name == null ? srcName : name; + } + + public String getType() { + if (source == null || docFile == null) + return srcType; + + String type = docFile.getType(); + return type == null ? srcType : type; + } + + public String getTag() { + return tag; + } + + public boolean existsAsFile() { + if (source == null) return false; + + // WARNING: DocumentFile.exists() and DocumentFile.isFile() methods are slow + boolean exists = docFile == null ? ioFile.exists() : docFile.exists(); + boolean isFile = docFile == null ? ioFile.isFile() : docFile.isFile();// ¿docFile.isVirtual() means is no-physical? + + return exists && isFile; + } + + public boolean create() { + invalid(); + boolean result; + + if (docFile == null) { + try { + result = ioFile.createNewFile(); + } catch (IOException e) { + return false; + } + } else if (docTree == null) { + result = false; + } else { + if (!docTree.canRead() || !docTree.canWrite()) return false; + try { + docFile = createSAF(context, srcType, srcName); + if (docFile == null || docFile.getName() == null) return false; + result = true; + } catch (IOException e) { + return false; + } + } + + if (result) { + source = (docFile == null ? Uri.fromFile(ioFile) : docFile.getUri()).toString(); + srcName = getName(); + srcType = getType(); + } + + return result; + } + + public void invalidate() { + if (source == null) return; + + srcName = getName(); + srcType = getType(); + + source = null; + + docTree = null; + docFile = null; + ioFile = null; + context = null; + } + + public boolean equals(StoredFileHelper storage) { + if (this == storage) return true; + + // note: do not compare tags, files can have the same parent folder + //if (stringMismatch(this.tag, storage.tag)) return false; + + if (stringMismatch(getLowerCase(this.sourceTree), getLowerCase(this.sourceTree))) + return false; + + if (this.isInvalid() || storage.isInvalid()) { + return this.srcName.equalsIgnoreCase(storage.srcName) && this.srcType.equalsIgnoreCase(storage.srcType); + } + + if (this.isDirect() != storage.isDirect()) return false; + + if (this.isDirect()) + return this.ioFile.getPath().equalsIgnoreCase(storage.ioFile.getPath()); + + return DocumentsContract.getDocumentId( + this.docFile.getUri() + ).equalsIgnoreCase(DocumentsContract.getDocumentId( + storage.docFile.getUri() + )); + } + + @NonNull + @Override + public String toString() { + if (source == null) + return "[Invalid state] name=" + srcName + " type=" + srcType + " tag=" + tag; + else + return "sourceFile=" + source + " treeSource=" + (sourceTree == null ? "" : sourceTree) + " tag=" + tag; + } + + + private void invalid() { + if (source == null) + throw new IllegalStateException("In invalid state"); + } + + private void takePermissionSAF() throws IOException { + try { + context.getContentResolver().takePersistableUriPermission(docFile.getUri(), StoredDirectoryHelper.PERMISSION_FLAGS); + } catch (Exception e) { + if (docFile.getName() == null) throw new IOException(e); + } + } + + private DocumentFile createSAF(@Nullable Context context, String mime, String filename) throws IOException { + DocumentFile res = StoredDirectoryHelper.findFileSAFHelper(context, docTree, filename); + + if (res != null && res.exists() && res.isDirectory()) { + if (!res.delete()) + throw new IOException("Directory with the same name found but cannot delete"); + res = null; + } + + if (res == null) { + res = this.docTree.createFile(srcType == null ? DEFAULT_MIME : mime, filename); + if (res == null) throw new IOException("Cannot create the file"); + } + + return res; + } + + private String getLowerCase(String str) { + return str == null ? null : str.toLowerCase(); + } + + private boolean stringMismatch(String str1, String str2) { + if (str1 == null && str2 == null) return false; + if ((str1 == null) != (str2 == null)) return true; + + return !str1.equals(str2); + } +} diff --git a/app/src/main/java/us/shandian/giga/postprocessing/M4aNoDash.java b/app/src/main/java/us/shandian/giga/postprocessing/M4aNoDash.java new file mode 100644 index 000000000..aa5170908 --- /dev/null +++ b/app/src/main/java/us/shandian/giga/postprocessing/M4aNoDash.java @@ -0,0 +1,41 @@ +package us.shandian.giga.postprocessing; + +import org.schabi.newpipe.streams.Mp4DashReader; +import org.schabi.newpipe.streams.Mp4FromDashWriter; +import org.schabi.newpipe.streams.io.SharpStream; + +import java.io.IOException; + +class M4aNoDash extends Postprocessing { + + M4aNoDash() { + super(false, true, ALGORITHM_M4A_NO_DASH); + } + + @Override + boolean test(SharpStream... sources) throws IOException { + // check if the mp4 file is DASH (youtube) + + Mp4DashReader reader = new Mp4DashReader(sources[0]); + reader.parse(); + + switch (reader.getBrands()[0]) { + case 0x64617368:// DASH + case 0x69736F35:// ISO5 + return true; + default: + return false; + } + } + + @Override + int process(SharpStream out, SharpStream... sources) throws IOException { + Mp4FromDashWriter muxer = new Mp4FromDashWriter(sources[0]); + muxer.setMainBrand(0x4D344120);// binary string "M4A " + muxer.parseSources(); + muxer.selectTracks(0); + muxer.build(out); + + return OK_RESULT; + } +} diff --git a/app/src/main/java/us/shandian/giga/postprocessing/Mp4DashMuxer.java b/app/src/main/java/us/shandian/giga/postprocessing/Mp4FromDashMuxer.java similarity index 53% rename from app/src/main/java/us/shandian/giga/postprocessing/Mp4DashMuxer.java rename to app/src/main/java/us/shandian/giga/postprocessing/Mp4FromDashMuxer.java index 45c06dd4b..74cb43116 100644 --- a/app/src/main/java/us/shandian/giga/postprocessing/Mp4DashMuxer.java +++ b/app/src/main/java/us/shandian/giga/postprocessing/Mp4FromDashMuxer.java @@ -1,29 +1,27 @@ -package us.shandian.giga.postprocessing; - -import org.schabi.newpipe.streams.Mp4DashWriter; -import org.schabi.newpipe.streams.io.SharpStream; - -import java.io.IOException; - -import us.shandian.giga.get.DownloadMission; - -/** - * @author kapodamy - */ -class Mp4DashMuxer extends Postprocessing { - - Mp4DashMuxer(DownloadMission mission) { - super(mission, 15360 * 1024/* 15 MiB */, true); - } - - @Override - int process(SharpStream out, SharpStream... sources) throws IOException { - Mp4DashWriter muxer = new Mp4DashWriter(sources); - muxer.parseSources(); - muxer.selectTracks(0, 0); - muxer.build(out); - - return OK_RESULT; - } - -} +package us.shandian.giga.postprocessing; + +import org.schabi.newpipe.streams.Mp4FromDashWriter; +import org.schabi.newpipe.streams.io.SharpStream; + +import java.io.IOException; + +/** + * @author kapodamy + */ +class Mp4FromDashMuxer extends Postprocessing { + + Mp4FromDashMuxer() { + super(true, true, ALGORITHM_MP4_FROM_DASH_MUXER); + } + + @Override + int process(SharpStream out, SharpStream... sources) throws IOException { + Mp4FromDashWriter muxer = new Mp4FromDashWriter(sources); + muxer.parseSources(); + muxer.selectTracks(0, 0); + muxer.build(out); + + return OK_RESULT; + } + +} diff --git a/app/src/main/java/us/shandian/giga/postprocessing/Mp4Muxer.java b/app/src/main/java/us/shandian/giga/postprocessing/Mp4Muxer.java deleted file mode 100644 index bf932d5c1..000000000 --- a/app/src/main/java/us/shandian/giga/postprocessing/Mp4Muxer.java +++ /dev/null @@ -1,136 +0,0 @@ -package us.shandian.giga.postprocessing; - -import android.media.MediaCodec.BufferInfo; -import android.media.MediaExtractor; -import android.media.MediaMuxer; -import android.media.MediaMuxer.OutputFormat; -import android.util.Log; - -import static org.schabi.newpipe.BuildConfig.DEBUG; - -import org.schabi.newpipe.streams.io.SharpStream; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.nio.ByteBuffer; - -import us.shandian.giga.get.DownloadMission; - - -class Mp4Muxer extends Postprocessing { - private static final String TAG = "Mp4Muxer"; - private static final int NOTIFY_BYTES_INTERVAL = 128 * 1024;// 128 KiB - - Mp4Muxer(DownloadMission mission) { - super(mission, 0, false); - } - - @Override - int process(SharpStream out, SharpStream... sources) throws IOException { - File dlFile = mission.getDownloadedFile(); - File tmpFile = new File(mission.location, mission.name.concat(".tmp")); - - if (tmpFile.exists()) - if (!tmpFile.delete()) return DownloadMission.ERROR_FILE_CREATION; - - if (!tmpFile.createNewFile()) return DownloadMission.ERROR_FILE_CREATION; - - FileInputStream source = null; - MediaMuxer muxer = null; - - //noinspection TryFinallyCanBeTryWithResources - try { - source = new FileInputStream(dlFile); - MediaExtractor tracks[] = { - getMediaExtractor(source, mission.offsets[0], mission.offsets[1] - mission.offsets[0]), - getMediaExtractor(source, mission.offsets[1], mission.length - mission.offsets[1]) - }; - - muxer = new MediaMuxer(tmpFile.getAbsolutePath(), OutputFormat.MUXER_OUTPUT_MPEG_4); - - int tracksIndex[] = { - muxer.addTrack(tracks[0].getTrackFormat(0)), - muxer.addTrack(tracks[1].getTrackFormat(0)) - }; - - ByteBuffer buffer = ByteBuffer.allocate(512 * 1024);// 512 KiB - BufferInfo info = new BufferInfo(); - - long written = 0; - long nextReport = NOTIFY_BYTES_INTERVAL; - - muxer.start(); - - while (true) { - int done = 0; - - for (int i = 0; i < tracks.length; i++) { - if (tracksIndex[i] < 0) continue; - - info.set(0, - tracks[i].readSampleData(buffer, 0), - tracks[i].getSampleTime(), - tracks[i].getSampleFlags() - ); - - if (info.size >= 0) { - muxer.writeSampleData(tracksIndex[i], buffer, info); - written += info.size; - done++; - } - if (!tracks[i].advance()) { - // EOF reached - tracks[i].release(); - tracksIndex[i] = -1; - } - - if (written > nextReport) { - nextReport = written + NOTIFY_BYTES_INTERVAL; - super.progressReport(written); - } - } - - if (done < 1) break; - } - - // this part should not fail - if (!dlFile.delete()) return DownloadMission.ERROR_FILE_CREATION; - if (!tmpFile.renameTo(dlFile)) return DownloadMission.ERROR_FILE_CREATION; - - return OK_RESULT; - } finally { - try { - if (muxer != null) { - muxer.stop(); - muxer.release(); - } - } catch (Exception err) { - if (DEBUG) - Log.e(TAG, "muxer stop/release failed", err); - } - - if (source != null) { - try { - source.close(); - } catch (IOException e) { - // nothing to do - } - } - - // if the operation fails, delete the temporal file - if (tmpFile.exists()) { - //noinspection ResultOfMethodCallIgnored - tmpFile.delete(); - } - } - } - - private MediaExtractor getMediaExtractor(FileInputStream source, long offset, long length) throws IOException { - MediaExtractor extractor = new MediaExtractor(); - extractor.setDataSource(source.getFD(), offset, length); - extractor.selectTrack(0); - - return extractor; - } -} diff --git a/app/src/main/java/us/shandian/giga/postprocessing/Postprocessing.java b/app/src/main/java/us/shandian/giga/postprocessing/Postprocessing.java index 635140bd3..22cc325d5 100644 --- a/app/src/main/java/us/shandian/giga/postprocessing/Postprocessing.java +++ b/app/src/main/java/us/shandian/giga/postprocessing/Postprocessing.java @@ -1,164 +1,256 @@ -package us.shandian.giga.postprocessing; - -import android.os.Message; - -import org.schabi.newpipe.streams.io.SharpStream; - -import java.io.File; -import java.io.IOException; - -import us.shandian.giga.get.DownloadMission; -import us.shandian.giga.postprocessing.io.ChunkFileInputStream; -import us.shandian.giga.postprocessing.io.CircularFile; -import us.shandian.giga.service.DownloadManagerService; - -public abstract class Postprocessing { - - static final byte OK_RESULT = DownloadMission.ERROR_NOTHING; - - public static final String ALGORITHM_TTML_CONVERTER = "ttml"; - public static final String ALGORITHM_MP4_DASH_MUXER = "mp4D"; - public static final String ALGORITHM_MP4_MUXER = "mp4"; - public static final String ALGORITHM_WEBM_MUXER = "webm"; - - public static Postprocessing getAlgorithm(String algorithmName, DownloadMission mission) { - if (null == algorithmName) { - throw new NullPointerException("algorithmName"); - } else switch (algorithmName) { - case ALGORITHM_TTML_CONVERTER: - return new TtmlConverter(mission); - case ALGORITHM_MP4_DASH_MUXER: - return new Mp4DashMuxer(mission); - case ALGORITHM_MP4_MUXER: - return new Mp4Muxer(mission); - case ALGORITHM_WEBM_MUXER: - return new WebMMuxer(mission); - /*case "example-algorithm": - return new ExampleAlgorithm(mission);*/ - default: - throw new RuntimeException("Unimplemented post-processing algorithm: " + algorithmName); - } - } - - /** - * Get a boolean value that indicate if the given algorithm work on the same - * file - */ - public boolean worksOnSameFile; - - /** - * Get the recommended space to reserve for the given algorithm. The amount - * is in bytes - */ - public int recommendedReserve; - - /** - * the download to post-process - */ - protected DownloadMission mission; - - Postprocessing(DownloadMission mission, int recommendedReserve, boolean worksOnSameFile) { - this.mission = mission; - this.recommendedReserve = recommendedReserve; - this.worksOnSameFile = worksOnSameFile; - } - - public void run() throws IOException { - File file = mission.getDownloadedFile(); - CircularFile out = null; - int result; - long finalLength = -1; - - mission.done = 0; - mission.length = file.length(); - - if (worksOnSameFile) { - ChunkFileInputStream[] sources = new ChunkFileInputStream[mission.urls.length]; - try { - int i = 0; - for (; i < sources.length - 1; i++) { - sources[i] = new ChunkFileInputStream(file, mission.offsets[i], mission.offsets[i + 1], "rw"); - } - sources[i] = new ChunkFileInputStream(file, mission.offsets[i], mission.getDownloadedFile().length(), "rw"); - - int[] idx = {0}; - CircularFile.OffsetChecker checker = () -> { - while (idx[0] < sources.length) { - /* - * WARNING: never use rewind() in any chunk after any writing (especially on first chunks) - * or the CircularFile can lead to unexpected results - */ - if (sources[idx[0]].isDisposed() || sources[idx[0]].available() < 1) { - idx[0]++; - continue;// the selected source is not used anymore - } - - return sources[idx[0]].getFilePointer() - 1; - } - - return -1; - }; - out = new CircularFile(file, 0, this::progressReport, checker); - - result = process(out, sources); - - if (result == OK_RESULT) - finalLength = out.finalizeFile(); - } finally { - for (SharpStream source : sources) { - if (source != null && !source.isDisposed()) { - source.dispose(); - } - } - if (out != null) { - out.dispose(); - } - } - } else { - result = process(null); - } - - if (result == OK_RESULT) { - if (finalLength < 0) finalLength = file.length(); - mission.done = finalLength; - mission.length = finalLength; - } else { - mission.errCode = DownloadMission.ERROR_UNKNOWN_EXCEPTION; - mission.errObject = new RuntimeException("post-processing algorithm returned " + result); - } - - if (result != OK_RESULT && worksOnSameFile) { - //noinspection ResultOfMethodCallIgnored - file.delete(); - } - } - - /** - * Abstract method to execute the pos-processing algorithm - * - * @param out output stream - * @param sources files to be processed - * @return a error code, 0 means the operation was successful - * @throws IOException if an I/O error occurs. - */ - abstract int process(SharpStream out, SharpStream... sources) throws IOException; - - String getArgumentAt(int index, String defaultValue) { - if (mission.postprocessingArgs == null || index >= mission.postprocessingArgs.length) { - return defaultValue; - } - - return mission.postprocessingArgs[index]; - } - - void progressReport(long done) { - mission.done = done; - if (mission.length < mission.done) mission.length = mission.done; - - Message m = new Message(); - m.what = DownloadManagerService.MESSAGE_PROGRESS; - m.obj = mission; - - mission.mHandler.sendMessage(m); - } -} +package us.shandian.giga.postprocessing; + +import android.os.Message; +import androidx.annotation.NonNull; +import android.util.Log; + +import org.schabi.newpipe.streams.io.SharpStream; + +import java.io.File; +import java.io.IOException; +import java.io.Serializable; + +import us.shandian.giga.get.DownloadMission; +import us.shandian.giga.io.ChunkFileInputStream; +import us.shandian.giga.io.CircularFileWriter; +import us.shandian.giga.io.CircularFileWriter.OffsetChecker; +import us.shandian.giga.service.DownloadManagerService; + +import static us.shandian.giga.get.DownloadMission.ERROR_NOTHING; +import static us.shandian.giga.get.DownloadMission.ERROR_POSTPROCESSING_HOLD; +import static us.shandian.giga.get.DownloadMission.ERROR_UNKNOWN_EXCEPTION; + +public abstract class Postprocessing implements Serializable { + + static transient final byte OK_RESULT = ERROR_NOTHING; + + public transient static final String ALGORITHM_TTML_CONVERTER = "ttml"; + public transient static final String ALGORITHM_WEBM_MUXER = "webm"; + public transient static final String ALGORITHM_MP4_FROM_DASH_MUXER = "mp4D-mp4"; + public transient static final String ALGORITHM_M4A_NO_DASH = "mp4D-m4a"; + + public static Postprocessing getAlgorithm(@NonNull String algorithmName, String[] args) { + Postprocessing instance; + + switch (algorithmName) { + case ALGORITHM_TTML_CONVERTER: + instance = new TtmlConverter(); + break; + case ALGORITHM_WEBM_MUXER: + instance = new WebMMuxer(); + break; + case ALGORITHM_MP4_FROM_DASH_MUXER: + instance = new Mp4FromDashMuxer(); + break; + case ALGORITHM_M4A_NO_DASH: + instance = new M4aNoDash(); + break; + /*case "example-algorithm": + instance = new ExampleAlgorithm();*/ + default: + throw new UnsupportedOperationException("Unimplemented post-processing algorithm: " + algorithmName); + } + + instance.args = args; + return instance; + } + + /** + * Get a boolean value that indicate if the given algorithm work on the same + * file + */ + public final boolean worksOnSameFile; + + /** + * Indicates whether the selected algorithm needs space reserved at the beginning of the file + */ + public final boolean reserveSpace; + + /** + * Gets the given algorithm short name + */ + private final String name; + + + private String[] args; + + protected transient DownloadMission mission; + + private File tempFile; + + Postprocessing(boolean reserveSpace, boolean worksOnSameFile, String algorithmName) { + this.reserveSpace = reserveSpace; + this.worksOnSameFile = worksOnSameFile; + this.name = algorithmName;// for debugging only + } + + public void setTemporalDir(@NonNull File directory) { + long rnd = (int) (Math.random() * 100000f); + tempFile = new File(directory, rnd + "_" + System.nanoTime() + ".tmp"); + } + + public void cleanupTemporalDir() { + if (tempFile != null && tempFile.exists()) { + //noinspection ResultOfMethodCallIgnored + tempFile.delete(); + } + } + + + public void run(DownloadMission target) throws IOException { + this.mission = target; + + CircularFileWriter out = null; + int result; + long finalLength = -1; + + mission.done = 0; + mission.length = mission.storage.length(); + + if (worksOnSameFile) { + ChunkFileInputStream[] sources = new ChunkFileInputStream[mission.urls.length]; + try { + int i = 0; + for (; i < sources.length - 1; i++) { + sources[i] = new ChunkFileInputStream(mission.storage.getStream(), mission.offsets[i], mission.offsets[i + 1]); + } + sources[i] = new ChunkFileInputStream(mission.storage.getStream(), mission.offsets[i]); + + if (test(sources)) { + for (SharpStream source : sources) source.rewind(); + + OffsetChecker checker = () -> { + for (ChunkFileInputStream source : sources) { + /* + * WARNING: never use rewind() in any chunk after any writing (especially on first chunks) + * or the CircularFileWriter can lead to unexpected results + */ + if (source.isClosed() || source.available() < 1) { + continue;// the selected source is not used anymore + } + + return source.getFilePointer() - 1; + } + + return -1; + }; + + out = new CircularFileWriter(mission.storage.getStream(), tempFile, checker); + out.onProgress = this::progressReport; + + out.onWriteError = (err) -> { + mission.psState = 3; + mission.notifyError(ERROR_POSTPROCESSING_HOLD, err); + + try { + synchronized (this) { + while (mission.psState == 3) + wait(); + } + } catch (InterruptedException e) { + // nothing to do + Log.e(this.getClass().getSimpleName(), "got InterruptedException"); + } + + return mission.errCode == ERROR_NOTHING; + }; + + result = process(out, sources); + + if (result == OK_RESULT) + finalLength = out.finalizeFile(); + } else { + result = OK_RESULT; + } + } finally { + for (SharpStream source : sources) { + if (source != null && !source.isClosed()) { + source.close(); + } + } + if (out != null) { + out.close(); + } + if (tempFile != null) { + //noinspection ResultOfMethodCallIgnored + tempFile.delete(); + tempFile = null; + } + } + } else { + result = test() ? process(null) : OK_RESULT; + } + + if (result == OK_RESULT) { + if (finalLength != -1) { + mission.done = finalLength; + mission.length = finalLength; + } + } else { + mission.errCode = ERROR_UNKNOWN_EXCEPTION; + mission.errObject = new RuntimeException("post-processing algorithm returned " + result); + } + + if (result != OK_RESULT && worksOnSameFile) mission.storage.delete(); + + this.mission = null; + } + + /** + * Test if the post-processing algorithm can be skipped + * + * @param sources files to be processed + * @return {@code true} if the post-processing is required, otherwise, {@code false} + * @throws IOException if an I/O error occurs. + */ + boolean test(SharpStream... sources) throws IOException { + return true; + } + + /** + * Abstract method to execute the post-processing algorithm + * + * @param out output stream + * @param sources files to be processed + * @return a error code, 0 means the operation was successful + * @throws IOException if an I/O error occurs. + */ + abstract int process(SharpStream out, SharpStream... sources) throws IOException; + + String getArgumentAt(int index, String defaultValue) { + if (args == null || index >= args.length) { + return defaultValue; + } + + return args[index]; + } + + private void progressReport(long done) { + mission.done = done; + if (mission.length < mission.done) mission.length = mission.done; + + Message m = new Message(); + m.what = DownloadManagerService.MESSAGE_PROGRESS; + m.obj = mission; + + mission.mHandler.sendMessage(m); + } + + @NonNull + @Override + public String toString() { + StringBuilder str = new StringBuilder(); + + str.append("name=").append(name).append('['); + + if (args != null) { + for (String arg : args) { + str.append(", "); + str.append(arg); + } + str.delete(0, 1); + } + + return str.append(']').toString(); + } +} diff --git a/app/src/main/java/us/shandian/giga/postprocessing/TtmlConverter.java b/app/src/main/java/us/shandian/giga/postprocessing/TtmlConverter.java index 390061840..5a5b687f7 100644 --- a/app/src/main/java/us/shandian/giga/postprocessing/TtmlConverter.java +++ b/app/src/main/java/us/shandian/giga/postprocessing/TtmlConverter.java @@ -1,75 +1,72 @@ -package us.shandian.giga.postprocessing; - -import android.util.Log; - -import org.schabi.newpipe.streams.io.SharpStream; -import org.schabi.newpipe.streams.SubtitleConverter; -import org.xml.sax.SAXException; - -import java.io.IOException; -import java.text.ParseException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPathExpressionException; - -import us.shandian.giga.get.DownloadMission; -import us.shandian.giga.postprocessing.io.SharpInputStream; - -/** - * @author kapodamy - */ -class TtmlConverter extends Postprocessing { - private static final String TAG = "TtmlConverter"; - - TtmlConverter(DownloadMission mission) { - // due how XmlPullParser works, the xml is fully loaded on the ram - super(mission, 0, true); - } - - @Override - int process(SharpStream out, SharpStream... sources) throws IOException { - // check if the subtitle is already in srt and copy, this should never happen - String format = getArgumentAt(0, null); - - if (format == null || format.equals("ttml")) { - SubtitleConverter ttmlDumper = new SubtitleConverter(); - - try { - ttmlDumper.dumpTTML( - sources[0], - out, - getArgumentAt(1, "true").equals("true"), - getArgumentAt(2, "true").equals("true") - ); - } catch (Exception err) { - Log.e(TAG, "subtitle parse failed", err); - - if (err instanceof IOException) { - return 1; - } else if (err instanceof ParseException) { - return 2; - } else if (err instanceof SAXException) { - return 3; - } else if (err instanceof ParserConfigurationException) { - return 4; - } else if (err instanceof XPathExpressionException) { - return 7; - } - - return 8; - } - - return OK_RESULT; - } else if (format.equals("srt")) { - byte[] buffer = new byte[8 * 1024]; - int read; - while ((read = sources[0].read(buffer)) > 0) { - out.write(buffer, 0, read); - } - return OK_RESULT; - } - - throw new UnsupportedOperationException("Can't convert this subtitle, unimplemented format: " + format); - } - -} +package us.shandian.giga.postprocessing; + +import android.util.Log; + +import org.schabi.newpipe.streams.SubtitleConverter; +import org.schabi.newpipe.streams.io.SharpStream; +import org.xml.sax.SAXException; + +import java.io.IOException; +import java.text.ParseException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPathExpressionException; + +/** + * @author kapodamy + */ +class TtmlConverter extends Postprocessing { + private static final String TAG = "TtmlConverter"; + + TtmlConverter() { + // due how XmlPullParser works, the xml is fully loaded on the ram + super(false, true, ALGORITHM_TTML_CONVERTER); + } + + @Override + int process(SharpStream out, SharpStream... sources) throws IOException { + // check if the subtitle is already in srt and copy, this should never happen + String format = getArgumentAt(0, null); + + if (format == null || format.equals("ttml")) { + SubtitleConverter ttmlDumper = new SubtitleConverter(); + + try { + ttmlDumper.dumpTTML( + sources[0], + out, + getArgumentAt(1, "true").equals("true"), + getArgumentAt(2, "true").equals("true") + ); + } catch (Exception err) { + Log.e(TAG, "subtitle parse failed", err); + + if (err instanceof IOException) { + return 1; + } else if (err instanceof ParseException) { + return 2; + } else if (err instanceof SAXException) { + return 3; + } else if (err instanceof ParserConfigurationException) { + return 4; + } else if (err instanceof XPathExpressionException) { + return 7; + } + + return 8; + } + + return OK_RESULT; + } else if (format.equals("srt")) { + byte[] buffer = new byte[8 * 1024]; + int read; + while ((read = sources[0].read(buffer)) > 0) { + out.write(buffer, 0, read); + } + return OK_RESULT; + } + + throw new UnsupportedOperationException("Can't convert this subtitle, unimplemented format: " + format); + } + +} diff --git a/app/src/main/java/us/shandian/giga/postprocessing/WebMMuxer.java b/app/src/main/java/us/shandian/giga/postprocessing/WebMMuxer.java index 2ffb0f08d..ea1676482 100644 --- a/app/src/main/java/us/shandian/giga/postprocessing/WebMMuxer.java +++ b/app/src/main/java/us/shandian/giga/postprocessing/WebMMuxer.java @@ -1,42 +1,44 @@ -package us.shandian.giga.postprocessing; - -import org.schabi.newpipe.streams.WebMReader.TrackKind; -import org.schabi.newpipe.streams.WebMReader.WebMTrack; -import org.schabi.newpipe.streams.WebMWriter; -import org.schabi.newpipe.streams.io.SharpStream; - -import java.io.IOException; - -import us.shandian.giga.get.DownloadMission; - -/** - * @author kapodamy - */ -class WebMMuxer extends Postprocessing { - - WebMMuxer(DownloadMission mission) { - super(mission, 2048 * 1024/* 2 MiB */, true); - } - - @Override - int process(SharpStream out, SharpStream... sources) throws IOException { - WebMWriter muxer = new WebMWriter(sources); - muxer.parseSources(); - - // youtube uses a webm with a fake video track that acts as a "cover image" - WebMTrack[] tracks = muxer.getTracksFromSource(1); - int audioTrackIndex = 0; - for (int i = 0; i < tracks.length; i++) { - if (tracks[i].kind == TrackKind.Audio) { - audioTrackIndex = i; - break; - } - } - - muxer.selectTracks(0, audioTrackIndex); - muxer.build(out); - - return OK_RESULT; - } - -} +package us.shandian.giga.postprocessing; + +import org.schabi.newpipe.streams.WebMReader.TrackKind; +import org.schabi.newpipe.streams.WebMReader.WebMTrack; +import org.schabi.newpipe.streams.WebMWriter; +import org.schabi.newpipe.streams.io.SharpStream; + +import java.io.IOException; + +/** + * @author kapodamy + */ +class WebMMuxer extends Postprocessing { + + WebMMuxer() { + super(true, true, ALGORITHM_WEBM_MUXER); + } + + @Override + int process(SharpStream out, SharpStream... sources) throws IOException { + WebMWriter muxer = new WebMWriter(sources); + muxer.parseSources(); + + // youtube uses a webm with a fake video track that acts as a "cover image" + int[] indexes = new int[sources.length]; + + for (int i = 0; i < sources.length; i++) { + WebMTrack[] tracks = muxer.getTracksFromSource(i); + for (int j = 0; j < tracks.length; j++) { + if (tracks[j].kind == TrackKind.Audio) { + indexes[i] = j; + i = sources.length; + break; + } + } + } + + muxer.selectTracks(indexes); + muxer.build(out); + + return OK_RESULT; + } + +} diff --git a/app/src/main/java/us/shandian/giga/postprocessing/io/CircularFile.java b/app/src/main/java/us/shandian/giga/postprocessing/io/CircularFile.java deleted file mode 100644 index d2fc82d33..000000000 --- a/app/src/main/java/us/shandian/giga/postprocessing/io/CircularFile.java +++ /dev/null @@ -1,375 +0,0 @@ -package us.shandian.giga.postprocessing.io; - -import org.schabi.newpipe.streams.io.SharpStream; - -import java.io.File; -import java.io.IOException; -import java.io.RandomAccessFile; -import java.util.ArrayList; - -public class CircularFile extends SharpStream { - - private final static int AUX_BUFFER_SIZE = 1024 * 1024;// 1 MiB - private final static int AUX_BUFFER_SIZE2 = 512 * 1024;// 512 KiB - private final static int NOTIFY_BYTES_INTERVAL = 64 * 1024;// 64 KiB - private final static int QUEUE_BUFFER_SIZE = 8 * 1024;// 8 KiB - private final static boolean IMMEDIATE_AUX_BUFFER_FLUSH = false; - - private RandomAccessFile out; - private long position; - private long maxLengthKnown = -1; - - private ArrayList auxiliaryBuffers; - private OffsetChecker callback; - private ManagedBuffer queue; - private long startOffset; - private ProgressReport onProgress; - private long reportPosition; - - public CircularFile(File file, long offset, ProgressReport progressReport, OffsetChecker checker) throws IOException { - if (checker == null) { - throw new NullPointerException("checker is null"); - } - - try { - queue = new ManagedBuffer(QUEUE_BUFFER_SIZE); - out = new RandomAccessFile(file, "rw"); - out.seek(offset); - position = offset; - } catch (IOException err) { - try { - if (out != null) { - out.close(); - } - } catch (IOException e) { - // nothing to do - } - throw err; - } - - auxiliaryBuffers = new ArrayList<>(15); - callback = checker; - startOffset = offset; - reportPosition = offset; - onProgress = progressReport; - - } - - /** - * Close the file without flushing any buffer - */ - @Override - public void dispose() { - try { - auxiliaryBuffers = null; - if (out != null) { - out.close(); - out = null; - } - } catch (IOException err) { - // nothing to do - } - } - - /** - * Flush any buffer and close the output file. Use this method if the - * operation is successful - * - * @return the final length of the file - * @throws IOException if an I/O error occurs - */ - public long finalizeFile() throws IOException { - flushEverything(); - - if (maxLengthKnown > -1) { - position = maxLengthKnown; - } - if (position < out.length()) { - out.setLength(position); - } - - dispose(); - - return position; - } - - @Override - public void write(byte b) throws IOException { - write(new byte[]{b}, 0, 1); - } - - @Override - public void write(byte b[]) throws IOException { - write(b, 0, b.length); - } - - @Override - public void write(byte b[], int off, int len) throws IOException { - if (len == 0) { - return; - } - - long end = callback.check(); - long available; - - if (end == -1) { - available = Long.MAX_VALUE; - } else { - if (end < startOffset) { - throw new IOException("The reported offset is invalid. reported offset is " + String.valueOf(end)); - } - available = end - position; - } - - // Check if possible flush one or more auxiliary buffer - if (auxiliaryBuffers.size() > 0) { - ManagedBuffer aux = auxiliaryBuffers.get(0); - - // check if there is enough space to flush it completely - while (available >= (aux.size + queue.size)) { - available -= aux.size; - writeQueue(aux.buffer, 0, aux.size); - aux.dereference(); - auxiliaryBuffers.remove(0); - - if (auxiliaryBuffers.size() < 1) { - aux = null; - break; - } - aux = auxiliaryBuffers.get(0); - } - - if (IMMEDIATE_AUX_BUFFER_FLUSH) { - // try partial flush to avoid allocate another auxiliary buffer - if (aux != null && aux.available() < len && available > queue.size) { - int size = Math.min(aux.size, (int) available - queue.size); - - writeQueue(aux.buffer, 0, size); - aux.dereference(size); - - available -= size; - } - } - } - - if (auxiliaryBuffers.size() < 1 && available > (len + queue.size)) { - writeQueue(b, off, len); - } else { - int i = auxiliaryBuffers.size() - 1; - while (len > 0) { - if (i < 0) { - // allocate a new auxiliary buffer - auxiliaryBuffers.add(new ManagedBuffer(AUX_BUFFER_SIZE)); - i++; - } - - ManagedBuffer aux = auxiliaryBuffers.get(i); - available = aux.available(); - - if (available < 1) { - // secondary auxiliary buffer - available = len; - aux = new ManagedBuffer(Math.max(len, AUX_BUFFER_SIZE2)); - auxiliaryBuffers.add(aux); - i++; - } else { - available = Math.min(len, available); - } - - aux.write(b, off, (int) available); - - len -= available; - if (len > 0) off += available; - } - } - } - - private void writeOutside(byte buffer[], int offset, int length) throws IOException { - out.write(buffer, offset, length); - position += length; - - if (onProgress != null && position > reportPosition) { - reportPosition = position + NOTIFY_BYTES_INTERVAL; - onProgress.report(position); - } - } - - private void writeQueue(byte[] buffer, int offset, int length) throws IOException { - while (length > 0) { - if (queue.available() < length) { - flushQueue(); - - if (length >= queue.buffer.length) { - writeOutside(buffer, offset, length); - return; - } - } - - int size = Math.min(queue.available(), length); - queue.write(buffer, offset, size); - - offset += size; - length -= size; - } - - if (queue.size >= queue.buffer.length) { - flushQueue(); - } - } - - private void flushQueue() throws IOException { - writeOutside(queue.buffer, 0, queue.size); - queue.size = 0; - } - - private void flushEverything() throws IOException { - flushQueue(); - - if (auxiliaryBuffers.size() > 0) { - for (ManagedBuffer aux : auxiliaryBuffers) { - writeOutside(aux.buffer, 0, aux.size); - aux.dereference(); - } - auxiliaryBuffers.clear(); - } - } - - /** - * Flush any buffer directly to the file. Warning: use this method ONLY if - * all read dependencies are disposed - * - * @throws IOException if the dependencies are not disposed - */ - @Override - public void flush() throws IOException { - if (callback.check() != -1) { - throw new IOException("All read dependencies of this file must be disposed first"); - } - flushEverything(); - - // Save the current file length in case the method {@code rewind()} is called - if (position > maxLengthKnown) { - maxLengthKnown = position; - } - } - - @Override - public void rewind() throws IOException { - flush(); - out.seek(startOffset); - - if (onProgress != null) { - onProgress.report(-position); - } - - position = startOffset; - reportPosition = startOffset; - - } - - @Override - public long skip(long amount) throws IOException { - flush(); - position += amount; - - out.seek(position); - - return amount; - } - - @Override - public boolean isDisposed() { - return out == null; - } - - @Override - public boolean canRewind() { - return true; - } - - @Override - public boolean canWrite() { - return true; - } - - // - @Override - public boolean canRead() { - return false; - } - - @Override - public int read() { - throw new UnsupportedOperationException("write-only"); - } - - @Override - public int read(byte[] buffer) { - throw new UnsupportedOperationException("write-only"); - } - - @Override - public int read(byte[] buffer, int offset, int count) { - throw new UnsupportedOperationException("write-only"); - } - - @Override - public int available() { - throw new UnsupportedOperationException("write-only"); - } -// - - public interface OffsetChecker { - - /** - * Checks the amount of available space ahead - * - * @return absolute offset in the file where no more data SHOULD NOT be - * written. If the value is -1 the whole file will be used - */ - long check(); - } - - public interface ProgressReport { - - void report(long progress); - } - - class ManagedBuffer { - - byte[] buffer; - int size; - - ManagedBuffer(int length) { - buffer = new byte[length]; - } - - void dereference() { - buffer = null; - size = 0; - } - - void dereference(int amount) { - if (amount > size) { - throw new IndexOutOfBoundsException("Invalid dereference amount (" + amount + ">=" + size + ")"); - } - size -= amount; - System.arraycopy(buffer, amount, buffer, 0, size); - } - - protected int available() { - return buffer.length - size; - } - - private void write(byte[] b, int off, int len) { - System.arraycopy(b, off, buffer, size, len); - size += len; - } - - @Override - public String toString() { - return "holding: " + String.valueOf(size) + " length: " + String.valueOf(buffer.length) + " available: " + String.valueOf(available()); - } - - } -} diff --git a/app/src/main/java/us/shandian/giga/service/DownloadManager.java b/app/src/main/java/us/shandian/giga/service/DownloadManager.java index 883c26850..3d34411b9 100644 --- a/app/src/main/java/us/shandian/giga/service/DownloadManager.java +++ b/app/src/main/java/us/shandian/giga/service/DownloadManager.java @@ -2,9 +2,9 @@ package us.shandian.giga.service; import android.content.Context; import android.os.Handler; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v7.util.DiffUtil; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.recyclerview.widget.DiffUtil; import android.util.Log; import android.widget.Toast; @@ -13,14 +13,15 @@ import org.schabi.newpipe.R; import java.io.File; import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import us.shandian.giga.get.DownloadMission; import us.shandian.giga.get.FinishedMission; import us.shandian.giga.get.Mission; -import us.shandian.giga.get.sqlite.DownloadDataSource; +import us.shandian.giga.get.sqlite.FinishedMissionStore; +import us.shandian.giga.io.StoredDirectoryHelper; +import us.shandian.giga.io.StoredFileHelper; import us.shandian.giga.util.Utility; import static org.schabi.newpipe.BuildConfig.DEBUG; @@ -28,13 +29,16 @@ import static org.schabi.newpipe.BuildConfig.DEBUG; public class DownloadManager { private static final String TAG = DownloadManager.class.getSimpleName(); - enum NetworkState {Unavailable, WifiOperating, MobileOperating, OtherOperating} + enum NetworkState {Unavailable, Operating, MeteredOperating} public final static int SPECIAL_NOTHING = 0; public final static int SPECIAL_PENDING = 1; public final static int SPECIAL_FINISHED = 2; - private final DownloadDataSource mDownloadDataSource; + public static final String TAG_AUDIO = "audio"; + public static final String TAG_VIDEO = "video"; + + private final FinishedMissionStore mFinishedMissionStore; private final ArrayList mMissionsPending = new ArrayList<>(); private final ArrayList mMissionsFinished; @@ -45,7 +49,12 @@ public class DownloadManager { private NetworkState mLastNetworkStatus = NetworkState.Unavailable; int mPrefMaxRetry; - boolean mPrefCrossNetwork; + boolean mPrefMeteredDownloads; + boolean mPrefQueueLimit; + private boolean mSelfMissionsControl; + + StoredDirectoryHelper mMainStorageAudio; + StoredDirectoryHelper mMainStorageVideo; /** * Create a new instance @@ -53,13 +62,15 @@ public class DownloadManager { * @param context Context for the data source for finished downloads * @param handler Thread required for Messaging */ - DownloadManager(@NonNull Context context, Handler handler) { + DownloadManager(@NonNull Context context, Handler handler, StoredDirectoryHelper storageVideo, StoredDirectoryHelper storageAudio) { if (DEBUG) { Log.d(TAG, "new DownloadManager instance. 0x" + Integer.toHexString(this.hashCode())); } - mDownloadDataSource = new DownloadDataSource(context); + mFinishedMissionStore = new FinishedMissionStore(context); mHandler = handler; + mMainStorageAudio = storageAudio; + mMainStorageVideo = storageVideo; mMissionsFinished = loadFinishedMissions(); mPendingMissionsDir = getPendingDir(context); @@ -67,7 +78,7 @@ public class DownloadManager { throw new RuntimeException("failed to create pending_downloads in data directory"); } - loadPendingMissions(); + loadPendingMissions(context); } private static File getPendingDir(@NonNull Context context) { @@ -88,29 +99,24 @@ public class DownloadManager { * Loads finished missions from the data source */ private ArrayList loadFinishedMissions() { - ArrayList finishedMissions = mDownloadDataSource.loadFinishedMissions(); + ArrayList finishedMissions = mFinishedMissionStore.loadFinishedMissions(); - // missions always is stored by creation order, simply reverse the list - ArrayList result = new ArrayList<>(finishedMissions.size()); + // check if the files exists, otherwise, forget the download for (int i = finishedMissions.size() - 1; i >= 0; i--) { FinishedMission mission = finishedMissions.get(i); - File file = mission.getDownloadedFile(); - if (!file.isFile()) { - if (DEBUG) { - Log.d(TAG, "downloaded file removed: " + file.getAbsolutePath()); - } - mDownloadDataSource.deleteMission(mission); - continue; + if (!mission.storage.existsAsFile()) { + if (DEBUG) Log.d(TAG, "downloaded file removed: " + mission.storage.getName()); + + mFinishedMissionStore.deleteMission(mission); + finishedMissions.remove(i); } - - result.add(mission); } - return result; + return finishedMissions; } - private void loadPendingMissions() { + private void loadPendingMissions(Context ctx) { File[] subs = mPendingMissionsDir.listFiles(); if (subs == null) { @@ -125,109 +131,76 @@ public class DownloadManager { } for (File sub : subs) { - if (sub.isFile()) { - DownloadMission mis = Utility.readFromFile(sub); + if (!sub.isFile()) continue; - if (mis == null) { - //noinspection ResultOfMethodCallIgnored - sub.delete(); - } else { - if (mis.isFinished()) { - //noinspection ResultOfMethodCallIgnored - sub.delete(); - continue; - } - - File dl = mis.getDownloadedFile(); - boolean exists = dl.exists(); - - if (mis.isPsRunning()) { - if (mis.postprocessingThis) { - // Incomplete post-processing results in a corrupted download file - // because the selected algorithm works on the same file to save space. - if (exists && dl.isFile() && !dl.delete()) - Log.w(TAG, "Unable to delete incomplete download file: " + sub.getPath()); - - exists = true; - } - - mis.postprocessingState = 0; - mis.errCode = DownloadMission.ERROR_POSTPROCESSING; - mis.errObject = new RuntimeException("stopped unexpectedly"); - } else if (exists && !dl.isFile()) { - // probably a folder, this should never happens - if (!sub.delete()) { - Log.w(TAG, "Unable to delete serialized file: " + sub.getPath()); - } - continue; - } - - if (!exists) { - // downloaded file deleted, reset mission state - DownloadMission m = new DownloadMission(mis.urls, mis.name, mis.location, mis.kind, mis.postprocessingName, mis.postprocessingArgs); - m.timestamp = mis.timestamp; - m.threadCount = mis.threadCount; - m.source = mis.source; - m.maxRetry = mis.maxRetry; - m.nearLength = mis.nearLength; - mis = m; - } - - mis.running = false; - mis.recovered = exists; - mis.metadata = sub; - mis.mHandler = mHandler; - - mMissionsPending.add(mis); - } + DownloadMission mis = Utility.readFromFile(sub); + if (mis == null || mis.isFinished()) { + //noinspection ResultOfMethodCallIgnored + sub.delete(); + continue; } + + boolean exists; + try { + mis.storage = StoredFileHelper.deserialize(mis.storage, ctx); + exists = !mis.storage.isInvalid() && mis.storage.existsAsFile(); + } catch (Exception ex) { + Log.e(TAG, "Failed to load the file source of " + mis.storage.toString(), ex); + mis.storage.invalidate(); + exists = false; + } + + if (mis.isPsRunning()) { + if (mis.psAlgorithm.worksOnSameFile) { + // Incomplete post-processing results in a corrupted download file + // because the selected algorithm works on the same file to save space. + // the file will be deleted if the storage API + // is Java IO (avoid showing the "Save as..." dialog) + if (exists && mis.storage.isDirect() && !mis.storage.delete()) + Log.w(TAG, "Unable to delete incomplete download file: " + sub.getPath()); + + exists = true; + } + + mis.psState = 0; + mis.errCode = DownloadMission.ERROR_POSTPROCESSING_STOPPED; + } else if (!exists) { + tryRecover(mis); + + // the progress is lost, reset mission state + if (mis.isInitialized()) + mis.resetState(true, true, DownloadMission.ERROR_PROGRESS_LOST); + } + + if (mis.psAlgorithm != null) { + mis.psAlgorithm.cleanupTemporalDir(); + mis.psAlgorithm.setTemporalDir(pickAvailableTemporalDir(ctx)); + } + + mis.recovered = exists; + mis.metadata = sub; + mis.maxRetry = mPrefMaxRetry; + mis.mHandler = mHandler; + + mMissionsPending.add(mis); } - if (mMissionsPending.size() > 1) { + if (mMissionsPending.size() > 1) Collections.sort(mMissionsPending, (mission1, mission2) -> Long.compare(mission1.timestamp, mission2.timestamp)); - } } /** * Start a new download mission * - * @param urls the list of urls to download - * @param location the location - * @param name the name of the file to create - * @param kind type of file (a: audio v: video s: subtitle ?: file-extension defined) - * @param threads the number of threads maximal used to download chunks of the file. - * @param psName the name of the required post-processing algorithm, or {@code null} to ignore. - * @param source source url of the resource - * @param psArgs the arguments for the post-processing algorithm. + * @param mission the new download mission to add and run (if possible) */ - void startMission(String[] urls, String location, String name, char kind, int threads, - String source, String psName, String[] psArgs, long nearLength) { + void startMission(DownloadMission mission) { synchronized (this) { - // check for existing pending download - DownloadMission pendingMission = getPendingMission(location, name); - if (pendingMission != null) { - // generate unique filename (?) - try { - name = generateUniqueName(location, name); - } catch (Exception e) { - Log.e(TAG, "Unable to generate unique name", e); - name = System.currentTimeMillis() + name; - Log.i(TAG, "Using " + name); - } - } else { - // check for existing finished download - int index = getFinishedMissionIndex(location, name); - if (index >= 0) mDownloadDataSource.deleteMission(mMissionsFinished.remove(index)); - } - - DownloadMission mission = new DownloadMission(urls, name, location, kind, psName, psArgs); mission.timestamp = System.currentTimeMillis(); - mission.threadCount = threads; - mission.source = source; mission.mHandler = mHandler; mission.maxRetry = mPrefMaxRetry; - mission.nearLength = nearLength; + // create metadata file while (true) { mission.metadata = new File(mPendingMissionsDir, String.valueOf(mission.timestamp)); if (!mission.metadata.isFile() && !mission.metadata.exists()) { @@ -242,14 +215,25 @@ public class DownloadManager { mission.timestamp = System.currentTimeMillis(); } + mSelfMissionsControl = true; mMissionsPending.add(mission); - // Before starting, save the state in case the internet connection is not available + // Before continue, save the metadata in case the internet connection is not available Utility.writeToFile(mission.metadata, mission); - if (canDownloadInCurrentNetwork() && (getRunningMissionsCount() < 1)) { + if (mission.storage == null) { + // noting to do here + mission.errCode = DownloadMission.ERROR_FILE_CREATION; + if (mission.errObject != null) + mission.errObject = new IOException("DownloadMission.storage == NULL"); + return; + } + + boolean start = !mPrefQueueLimit || getRunningMissionsCount() < 1; + + if (canDownloadInCurrentNetwork() && start) { + mHandler.sendEmptyMessage(DownloadManagerService.MESSAGE_PROGRESS); mission.start(); - mHandler.sendEmptyMessage(DownloadManagerService.MESSAGE_RUNNING); } } } @@ -257,13 +241,14 @@ public class DownloadManager { public void resumeMission(DownloadMission mission) { if (!mission.running) { + mHandler.sendEmptyMessage(DownloadManagerService.MESSAGE_PROGRESS); mission.start(); - mHandler.sendEmptyMessage(DownloadManagerService.MESSAGE_RUNNING); } } public void pauseMission(DownloadMission mission) { if (mission.running) { + mission.setEnqueued(false); mission.pause(); mHandler.sendEmptyMessage(DownloadManagerService.MESSAGE_PAUSED); } @@ -275,7 +260,7 @@ public class DownloadManager { mMissionsPending.remove(mission); } else if (mission instanceof FinishedMission) { mMissionsFinished.remove(mission); - mDownloadDataSource.deleteMission(mission); + mFinishedMissionStore.deleteMission(mission); } mHandler.sendEmptyMessage(DownloadManagerService.MESSAGE_DELETED); @@ -283,18 +268,54 @@ public class DownloadManager { } } + public void forgetMission(StoredFileHelper storage) { + synchronized (this) { + Mission mission = getAnyMission(storage); + if (mission == null) return; + + if (mission instanceof DownloadMission) { + mMissionsPending.remove(mission); + } else if (mission instanceof FinishedMission) { + mMissionsFinished.remove(mission); + mFinishedMissionStore.deleteMission(mission); + } + + mHandler.sendEmptyMessage(DownloadManagerService.MESSAGE_DELETED); + mission.storage = null; + mission.delete(); + } + } + + public void tryRecover(DownloadMission mission) { + StoredDirectoryHelper mainStorage = getMainStorage(mission.storage.getTag()); + + if (!mission.storage.isInvalid() && mission.storage.create()) return; + + // using javaIO cannot recreate the file + // using SAF in older devices (no tree available) + // + // force the user to pick again the save path + mission.storage.invalidate(); + + if (mainStorage == null) return; + + // if the user has changed the save path before this download, the original save path will be lost + StoredFileHelper newStorage = mainStorage.createFile(mission.storage.getName(), mission.storage.getType()); + + if (newStorage != null) mission.storage = newStorage; + } + /** - * Get a pending mission by its location and name + * Get a pending mission by its path * - * @param location the location - * @param name the name + * @param storage where the file possible is stored * @return the mission or null if no such mission exists */ @Nullable - private DownloadMission getPendingMission(String location, String name) { + private DownloadMission getPendingMission(StoredFileHelper storage) { for (DownloadMission mission : mMissionsPending) { - if (location.equalsIgnoreCase(mission.location) && name.equalsIgnoreCase(mission.name)) { + if (mission.storage.equals(storage)) { return mission; } } @@ -302,16 +323,14 @@ public class DownloadManager { } /** - * Get a finished mission by its location and name + * Get a finished mission by its path * - * @param location the location - * @param name the name + * @param storage where the file possible is stored * @return the mission index or -1 if no such mission exists */ - private int getFinishedMissionIndex(String location, String name) { + private int getFinishedMissionIndex(StoredFileHelper storage) { for (int i = 0; i < mMissionsFinished.size(); i++) { - FinishedMission mission = mMissionsFinished.get(i); - if (location.equalsIgnoreCase(mission.location) && name.equalsIgnoreCase(mission.name)) { + if (mMissionsFinished.get(i).storage.equals(storage)) { return i; } } @@ -319,12 +338,12 @@ public class DownloadManager { return -1; } - public Mission getAnyMission(String location, String name) { + private Mission getAnyMission(StoredFileHelper storage) { synchronized (this) { - Mission mission = getPendingMission(location, name); + Mission mission = getPendingMission(storage); if (mission != null) return mission; - int idx = getFinishedMissionIndex(location, name); + int idx = getFinishedMissionIndex(storage); if (idx >= 0) return mMissionsFinished.get(idx); } @@ -335,7 +354,7 @@ public class DownloadManager { int count = 0; synchronized (this) { for (DownloadMission mission : mMissionsPending) { - if (mission.running && !mission.isFinished() && !mission.isPsFailed()) + if (mission.running && !mission.isPsFailed() && !mission.isFinished()) count++; } } @@ -343,62 +362,36 @@ public class DownloadManager { return count; } - void pauseAllMissions() { + public void pauseAllMissions(boolean force) { + boolean flag = false; + synchronized (this) { - for (DownloadMission mission : mMissionsPending) mission.pause(); - } - } + for (DownloadMission mission : mMissionsPending) { + if (!mission.running || mission.isPsRunning() || mission.isFinished()) continue; + if (force) mission.threads = null;// avoid waiting for threads - /** - * Splits the filename into name and extension - *

- * Dots are ignored if they appear: not at all, at the beginning of the file, - * at the end of the file - * - * @param name the name to split - * @return a string array with a length of 2 containing the name and the extension - */ - private static String[] splitName(String name) { - int dotIndex = name.lastIndexOf('.'); - if (dotIndex <= 0 || (dotIndex == name.length() - 1)) { - return new String[]{name, ""}; - } else { - return new String[]{name.substring(0, dotIndex), name.substring(dotIndex + 1)}; - } - } - - /** - * Generates a unique file name. - *

- * e.g. "myName (1).txt" if the name "myName.txt" exists. - * - * @param location the location (to check for existing files) - * @param name the name of the file - * @return the unique file name - * @throws IllegalArgumentException if the location is not a directory - * @throws SecurityException if the location is not readable - */ - private static String generateUniqueName(String location, String name) { - if (location == null) throw new NullPointerException("location is null"); - if (name == null) throw new NullPointerException("name is null"); - File destination = new File(location); - if (!destination.isDirectory()) { - throw new IllegalArgumentException("location is not a directory: " + location); - } - final String[] nameParts = splitName(name); - String[] existingName = destination.list((dir, name1) -> name1.startsWith(nameParts[0])); - Arrays.sort(existingName); - String newName; - int downloadIndex = 0; - do { - newName = nameParts[0] + " (" + downloadIndex + ")." + nameParts[1]; - ++downloadIndex; - if (downloadIndex == 1000) { // Probably an error on our side - throw new RuntimeException("Too many existing files"); + mission.pause(); + flag = true; } - } while (Arrays.binarySearch(existingName, newName) >= 0); - return newName; + } + + if (flag) mHandler.sendEmptyMessage(DownloadManagerService.MESSAGE_PAUSED); + } + + public void startAllMissions() { + boolean flag = false; + + synchronized (this) { + for (DownloadMission mission : mMissionsPending) { + if (mission.running || mission.isCorrupt()) continue; + + flag = true; + mission.start(); + } + } + + if (flag) mHandler.sendEmptyMessage(DownloadManagerService.MESSAGE_PROGRESS); } /** @@ -410,36 +403,43 @@ public class DownloadManager { synchronized (this) { mMissionsPending.remove(mission); mMissionsFinished.add(0, new FinishedMission(mission)); - mDownloadDataSource.addMission(mission); + mFinishedMissionStore.addFinishedMission(mission); } } /** - * runs another mission in queue if possible + * runs one or multiple missions in from queue if possible * - * @return true if exits pending missions running or a mission was started, otherwise, false + * @return true if one or multiple missions are running, otherwise, false */ - boolean runAnotherMission() { + boolean runMissions() { synchronized (this) { if (mMissionsPending.size() < 1) return false; - - int i = getRunningMissionsCount(); - if (i > 0) return true; - if (!canDownloadInCurrentNetwork()) return false; - for (DownloadMission mission : mMissionsPending) { - if (!mission.running && mission.errCode == DownloadMission.ERROR_NOTHING && mission.enqueued) { - resumeMission(mission); - return true; - } + if (mPrefQueueLimit) { + for (DownloadMission mission : mMissionsPending) + if (!mission.isFinished() && mission.running) return true; } - return false; + boolean flag = false; + for (DownloadMission mission : mMissionsPending) { + if (mission.running || !mission.enqueued || mission.isFinished()) + continue; + + resumeMission(mission); + if (mission.errCode != DownloadMission.ERROR_NOTHING) continue; + + if (mPrefQueueLimit) return true; + flag = true; + } + + return flag; } } public MissionIterator getIterator() { + mSelfMissionsControl = true; return new MissionIterator(); } @@ -449,7 +449,7 @@ public class DownloadManager { public void forgetFinishedDownloads() { synchronized (this) { for (FinishedMission mission : mMissionsFinished) { - mDownloadDataSource.deleteMission(mission); + mFinishedMissionStore.deleteMission(mission); } mMissionsFinished.clear(); } @@ -457,31 +457,43 @@ public class DownloadManager { private boolean canDownloadInCurrentNetwork() { if (mLastNetworkStatus == NetworkState.Unavailable) return false; - return !(mPrefCrossNetwork && mLastNetworkStatus == NetworkState.MobileOperating); + return !(mPrefMeteredDownloads && mLastNetworkStatus == NetworkState.MeteredOperating); } - void handleConnectivityChange(NetworkState currentStatus) { + void handleConnectivityState(NetworkState currentStatus, boolean updateOnly) { if (currentStatus == mLastNetworkStatus) return; mLastNetworkStatus = currentStatus; + if (currentStatus == NetworkState.Unavailable) return; - if (currentStatus == NetworkState.Unavailable) { - return; - } else if (currentStatus != NetworkState.MobileOperating || !mPrefCrossNetwork) { - return; + if (!mSelfMissionsControl || updateOnly) { + return;// don't touch anything without the user interaction } - boolean flag = false; + boolean isMetered = mPrefMeteredDownloads && mLastNetworkStatus == NetworkState.MeteredOperating; + + int running = 0; + int paused = 0; synchronized (this) { for (DownloadMission mission : mMissionsPending) { - if (mission.running && !mission.isFinished() && !mission.isPsRunning()) { - flag = true; + if (mission.isCorrupt() || mission.isPsRunning()) continue; + + if (mission.running && isMetered) { + paused++; mission.pause(); + } else if (!mission.running && !isMetered && mission.enqueued) { + running++; + mission.start(); + if (mPrefQueueLimit) break; } } } - if (flag) mHandler.sendEmptyMessage(DownloadManagerService.MESSAGE_PAUSED); + if (running > 0) { + mHandler.sendEmptyMessage(DownloadManagerService.MESSAGE_PROGRESS); + return; + } + if (paused > 0) mHandler.sendEmptyMessage(DownloadManagerService.MESSAGE_PAUSED); } void updateMaximumAttempts() { @@ -506,21 +518,46 @@ public class DownloadManager { ), Toast.LENGTH_LONG).show(); } - void checkForRunningMission(String location, String name, DownloadManagerService.DMChecker check) { - boolean listed; - boolean finished = false; - + public MissionState checkForExistingMission(StoredFileHelper storage) { synchronized (this) { - DownloadMission mission = getPendingMission(location, name); - if (mission != null) { - listed = true; + DownloadMission pending = getPendingMission(storage); + + if (pending == null) { + if (getFinishedMissionIndex(storage) >= 0) return MissionState.Finished; } else { - listed = getFinishedMissionIndex(location, name) >= 0; - finished = listed; + if (pending.isFinished()) { + return MissionState.Finished;// this never should happen (race-condition) + } else { + return pending.running ? MissionState.PendingRunning : MissionState.Pending; + } } } - check.callback(listed, finished); + return MissionState.None; + } + + private static boolean isDirectoryAvailable(File directory) { + return directory != null && directory.canWrite() && directory.exists(); + } + + static File pickAvailableTemporalDir(@NonNull Context ctx) { + if (isDirectoryAvailable(ctx.getExternalFilesDir(null))) + return ctx.getExternalFilesDir(null); + else if (isDirectoryAvailable(ctx.getFilesDir())) + return ctx.getFilesDir(); + + // this never should happen + return ctx.getDir("tmp", Context.MODE_PRIVATE); + } + + @Nullable + private StoredDirectoryHelper getMainStorage(@NonNull String tag) { + if (tag.equals(TAG_AUDIO)) return mMainStorageAudio; + if (tag.equals(TAG_VIDEO)) return mMainStorageVideo; + + Log.w(TAG, "Unknown download category, not [audio video]: " + tag); + + return null;// this never should happen } public class MissionIterator extends DiffUtil.Callback { @@ -592,39 +629,6 @@ public class DownloadManager { return SPECIAL_NOTHING; } - public MissionItem getItemUnsafe(int position) { - synchronized (DownloadManager.this) { - int count = mMissionsPending.size(); - int count2 = mMissionsFinished.size(); - - if (count > 0) { - position--; - if (position == -1) - return new MissionItem(SPECIAL_PENDING); - else if (position < count) - return new MissionItem(SPECIAL_NOTHING, mMissionsPending.get(position)); - else if (position == count && count2 > 0) - return new MissionItem(SPECIAL_FINISHED); - else - position -= count; - } else { - if (count2 > 0 && position == 0) { - return new MissionItem(SPECIAL_FINISHED); - } - } - - position--; - - if (count2 < 1) { - throw new RuntimeException( - String.format("Out of range. pending_count=%s finished_count=%s position=%s", count, count2, position) - ); - } - - return new MissionItem(SPECIAL_NOTHING, mMissionsFinished.get(position)); - } - } - public void start() { current = getSpecialItems(); @@ -647,6 +651,32 @@ public class DownloadManager { return hasFinished; } + /** + * Check if exists missions running and paused. Corrupted and hidden missions are not counted + * + * @return two-dimensional array contains the current missions state. + * 1° entry: true if has at least one mission running + * 2° entry: true if has at least one mission paused + */ + public boolean[] hasValidPendingMissions() { + boolean running = false; + boolean paused = false; + + synchronized (DownloadManager.this) { + for (DownloadMission mission : mMissionsPending) { + if (hidden.contains(mission) || mission.isCorrupt()) + continue; + + if (mission.running) + running = true; + else + paused = true; + } + } + + return new boolean[]{running, paused}; + } + @Override public int getOldListSize() { @@ -665,7 +695,14 @@ public class DownloadManager { @Override public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) { - return areItemsTheSame(oldItemPosition, newItemPosition); + Object x = snapshot.get(oldItemPosition); + Object y = current.get(newItemPosition); + + if (x instanceof Mission && y instanceof Mission) { + return ((Mission) x).storage.equals(((Mission) y).storage); + } + + return false; } } diff --git a/app/src/main/java/us/shandian/giga/service/DownloadManagerService.java b/app/src/main/java/us/shandian/giga/service/DownloadManagerService.java index a57fe1734..461787b62 100755 --- a/app/src/main/java/us/shandian/giga/service/DownloadManagerService.java +++ b/app/src/main/java/us/shandian/giga/service/DownloadManagerService.java @@ -1,32 +1,34 @@ package us.shandian.giga.service; -import android.Manifest; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.BroadcastReceiver; -import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.content.ServiceConnection; import android.content.SharedPreferences; +import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.ConnectivityManager; +import android.net.Network; import android.net.NetworkInfo; +import android.net.NetworkRequest; import android.net.Uri; import android.os.Binder; import android.os.Build; import android.os.Handler; +import android.os.Handler.Callback; import android.os.IBinder; -import android.os.Looper; import android.os.Message; import android.preference.PreferenceManager; -import android.support.v4.app.NotificationCompat; -import android.support.v4.app.NotificationCompat.Builder; -import android.support.v4.content.PermissionChecker; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.annotation.StringRes; +import androidx.core.app.NotificationCompat; +import androidx.core.app.NotificationCompat.Builder; import android.util.Log; import android.util.SparseArray; import android.widget.Toast; @@ -36,9 +38,13 @@ import org.schabi.newpipe.download.DownloadActivity; import org.schabi.newpipe.player.helper.LockManager; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import us.shandian.giga.get.DownloadMission; +import us.shandian.giga.io.StoredDirectoryHelper; +import us.shandian.giga.io.StoredFileHelper; +import us.shandian.giga.postprocessing.Postprocessing; import us.shandian.giga.service.DownloadManager.NetworkState; import static org.schabi.newpipe.BuildConfig.APPLICATION_ID; @@ -48,7 +54,6 @@ public class DownloadManagerService extends Service { private static final String TAG = "DownloadManagerService"; - public static final int MESSAGE_RUNNING = 0; public static final int MESSAGE_PAUSED = 1; public static final int MESSAGE_FINISHED = 2; public static final int MESSAGE_PROGRESS = 3; @@ -59,36 +64,39 @@ public class DownloadManagerService extends Service { private static final int DOWNLOADS_NOTIFICATION_ID = 1001; private static final String EXTRA_URLS = "DownloadManagerService.extra.urls"; - private static final String EXTRA_NAME = "DownloadManagerService.extra.name"; - private static final String EXTRA_LOCATION = "DownloadManagerService.extra.location"; private static final String EXTRA_KIND = "DownloadManagerService.extra.kind"; private static final String EXTRA_THREADS = "DownloadManagerService.extra.threads"; private static final String EXTRA_POSTPROCESSING_NAME = "DownloadManagerService.extra.postprocessingName"; private static final String EXTRA_POSTPROCESSING_ARGS = "DownloadManagerService.extra.postprocessingArgs"; private static final String EXTRA_SOURCE = "DownloadManagerService.extra.source"; private static final String EXTRA_NEAR_LENGTH = "DownloadManagerService.extra.nearLength"; + private static final String EXTRA_PATH = "DownloadManagerService.extra.storagePath"; + private static final String EXTRA_PARENT_PATH = "DownloadManagerService.extra.storageParentPath"; + private static final String EXTRA_STORAGE_TAG = "DownloadManagerService.extra.storageTag"; private static final String ACTION_RESET_DOWNLOAD_FINISHED = APPLICATION_ID + ".reset_download_finished"; private static final String ACTION_OPEN_DOWNLOADS_FINISHED = APPLICATION_ID + ".open_downloads_finished"; - private DMBinder mBinder; + private DownloadManagerBinder mBinder; private DownloadManager mManager; private Notification mNotification; private Handler mHandler; private boolean mForeground = false; - private NotificationManager notificationManager = null; + private NotificationManager mNotificationManager = null; private boolean mDownloadNotificationEnable = true; private int downloadDoneCount = 0; private Builder downloadDoneNotification = null; private StringBuilder downloadDoneList = null; - private final ArrayList mEchoObservers = new ArrayList<>(1); + private final ArrayList mEchoObservers = new ArrayList<>(1); - private BroadcastReceiver mNetworkStateListener; + private ConnectivityManager mConnectivityManager; + private BroadcastReceiver mNetworkStateListener = null; + private ConnectivityManager.NetworkCallback mNetworkStateListenerL = null; private SharedPreferences mPrefs = null; - private final SharedPreferences.OnSharedPreferenceChangeListener mPrefChangeListener = this::handlePreferenceChange; + private final OnSharedPreferenceChangeListener mPrefChangeListener = this::handlePreferenceChange; private boolean mLockAcquired = false; private LockManager mLock = null; @@ -106,10 +114,10 @@ public class DownloadManagerService extends Service { /** * notify media scanner on downloaded media file ... * - * @param file the downloaded file + * @param file the downloaded file uri */ - private void notifyMediaScanner(File file) { - sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file))); + private void notifyMediaScanner(Uri file) { + sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, file)); } @Override @@ -120,15 +128,12 @@ public class DownloadManagerService extends Service { Log.d(TAG, "onCreate"); } - mBinder = new DMBinder(); - mHandler = new Handler(Looper.myLooper()) { - @Override - public void handleMessage(Message msg) { - DownloadManagerService.this.handleMessage(msg); - } - }; + mBinder = new DownloadManagerBinder(); + mHandler = new Handler(this::handleMessage); - mManager = new DownloadManager(this, mHandler); + mPrefs = PreferenceManager.getDefaultSharedPreferences(this); + + mManager = new DownloadManager(this, mHandler, loadMainVideoStorage(), loadMainAudioStorage()); Intent openDownloadListIntent = new Intent(this, DownloadActivity.class) .setAction(Intent.ACTION_MAIN); @@ -147,54 +152,55 @@ public class DownloadManagerService extends Service { .setContentText(getString(R.string.msg_running_detail)); mNotification = builder.build(); - notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - mNetworkStateListener = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) { - handleConnectivityChange(null); - return; + mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); + + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + mNetworkStateListenerL = new ConnectivityManager.NetworkCallback() { + @Override + public void onAvailable(Network network) { + handleConnectivityState(false); } - handleConnectivityChange(intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO)); - } - }; - registerReceiver(mNetworkStateListener, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); - mPrefs = PreferenceManager.getDefaultSharedPreferences(this); + @Override + public void onLost(Network network) { + handleConnectivityState(false); + } + }; + mConnectivityManager.registerNetworkCallback(new NetworkRequest.Builder().build(), mNetworkStateListenerL); + } else { + mNetworkStateListener = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + handleConnectivityState(false); + } + }; + registerReceiver(mNetworkStateListener, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); + } + mPrefs.registerOnSharedPreferenceChangeListener(mPrefChangeListener); handlePreferenceChange(mPrefs, getString(R.string.downloads_cross_network)); handlePreferenceChange(mPrefs, getString(R.string.downloads_maximum_retry)); + handlePreferenceChange(mPrefs, getString(R.string.downloads_queue_limit)); mLock = new LockManager(this); } @Override - public int onStartCommand(Intent intent, int flags, int startId) { + public int onStartCommand(final Intent intent, int flags, int startId) { if (DEBUG) { - if (intent == null) { - Log.d(TAG, "Restarting"); - return START_NOT_STICKY; - } - Log.d(TAG, "Starting"); + Log.d(TAG, intent == null ? "Restarting" : "Starting"); } + + if (intent == null) return START_NOT_STICKY; + Log.i(TAG, "Got intent: " + intent); String action = intent.getAction(); if (action != null) { if (action.equals(Intent.ACTION_RUN)) { - String[] urls = intent.getStringArrayExtra(EXTRA_URLS); - String name = intent.getStringExtra(EXTRA_NAME); - String location = intent.getStringExtra(EXTRA_LOCATION); - int threads = intent.getIntExtra(EXTRA_THREADS, 1); - char kind = intent.getCharExtra(EXTRA_KIND, '?'); - String psName = intent.getStringExtra(EXTRA_POSTPROCESSING_NAME); - String[] psArgs = intent.getStringArrayExtra(EXTRA_POSTPROCESSING_ARGS); - String source = intent.getStringExtra(EXTRA_SOURCE); - long nearLength = intent.getLongExtra(EXTRA_NEAR_LENGTH, 0); - - mHandler.post(() -> mManager.startMission(urls, location, name, kind, threads, source, psName, psArgs, nearLength)); - + mHandler.post(() -> startMission(intent)); } else if (downloadDoneNotification != null) { if (action.equals(ACTION_RESET_DOWNLOAD_FINISHED) || action.equals(ACTION_OPEN_DOWNLOADS_FINISHED)) { downloadDoneCount = 0; @@ -221,25 +227,30 @@ public class DownloadManagerService extends Service { stopForeground(true); - if (notificationManager != null && downloadDoneNotification != null) { + if (mNotificationManager != null && downloadDoneNotification != null) { downloadDoneNotification.setDeleteIntent(null);// prevent NewPipe running when is killed, cleared from recent, etc - notificationManager.notify(DOWNLOADS_NOTIFICATION_ID, downloadDoneNotification.build()); + mNotificationManager.notify(DOWNLOADS_NOTIFICATION_ID, downloadDoneNotification.build()); } - mManager.pauseAllMissions(); - manageLock(false); - unregisterReceiver(mNetworkStateListener); + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) + mConnectivityManager.unregisterNetworkCallback(mNetworkStateListenerL); + else + unregisterReceiver(mNetworkStateListener); + mPrefs.unregisterOnSharedPreferenceChangeListener(mPrefChangeListener); if (icDownloadDone != null) icDownloadDone.recycle(); if (icDownloadFailed != null) icDownloadFailed.recycle(); if (icLauncher != null) icLauncher.recycle(); + + mManager.pauseAllMissions(true); } @Override public IBinder onBind(Intent intent) { + /* int permissionCheck; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { permissionCheck = PermissionChecker.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE); @@ -252,27 +263,29 @@ public class DownloadManagerService extends Service { if (permissionCheck == PermissionChecker.PERMISSION_DENIED) { Toast.makeText(this, "Permission denied (write)", Toast.LENGTH_SHORT).show(); } + */ return mBinder; } - public void handleMessage(Message msg) { + private boolean handleMessage(@NonNull Message msg) { DownloadMission mission = (DownloadMission) msg.obj; switch (msg.what) { case MESSAGE_FINISHED: - notifyMediaScanner(mission.getDownloadedFile()); - notifyFinishedDownload(mission.name); + notifyMediaScanner(mission.storage.getUri()); + notifyFinishedDownload(mission.storage.getName()); mManager.setFinished(mission); - updateForegroundState(mManager.runAnotherMission()); + handleConnectivityState(false); + updateForegroundState(mManager.runMissions()); break; - case MESSAGE_RUNNING: case MESSAGE_PROGRESS: updateForegroundState(true); break; case MESSAGE_ERROR: notifyFailedDownload(mission); - updateForegroundState(mManager.runAnotherMission()); + handleConnectivityState(false); + updateForegroundState(mManager.runMissions()); break; case MESSAGE_PAUSED: updateForegroundState(mManager.getRunningMissionsCount() > 0); @@ -283,56 +296,54 @@ public class DownloadManagerService extends Service { mFailedDownloads.delete(mFailedDownloads.indexOfValue(mission)); synchronized (mEchoObservers) { - for (Handler handler : mEchoObservers) { - Message echo = new Message(); - echo.what = msg.what; - echo.obj = msg.obj; - - handler.sendMessage(echo); + for (Callback observer : mEchoObservers) { + observer.handleMessage(msg); } } + + return true; } - private void handleConnectivityChange(NetworkInfo info) { + private void handleConnectivityState(boolean updateOnly) { + NetworkInfo info = mConnectivityManager.getActiveNetworkInfo(); NetworkState status; if (info == null) { status = NetworkState.Unavailable; - Log.i(TAG, "actual connectivity status is unavailable"); - } else if (!info.isAvailable() || !info.isConnected()) { - status = NetworkState.Unavailable; - Log.i(TAG, "actual connectivity status is not available and not connected"); + Log.i(TAG, "Active network [connectivity is unavailable]"); } else { - int type = info.getType(); - if (type == ConnectivityManager.TYPE_MOBILE || type == ConnectivityManager.TYPE_MOBILE_DUN) { - status = NetworkState.MobileOperating; - } else if (type == ConnectivityManager.TYPE_WIFI) { - status = NetworkState.WifiOperating; - } else if (type == ConnectivityManager.TYPE_WIMAX || - type == ConnectivityManager.TYPE_ETHERNET || - type == ConnectivityManager.TYPE_BLUETOOTH) { - status = NetworkState.OtherOperating; - } else { + boolean connected = info.isConnected(); + boolean metered = mConnectivityManager.isActiveNetworkMetered(); + + if (connected) + status = metered ? NetworkState.MeteredOperating : NetworkState.Operating; + else status = NetworkState.Unavailable; - } - Log.i(TAG, "actual connectivity status is " + status.name()); + + Log.i(TAG, "Active network [connected=" + connected + " metered=" + metered + "] " + info.toString()); } if (mManager == null) return;// avoid race-conditions while the service is starting - mManager.handleConnectivityChange(status); + mManager.handleConnectivityState(status, updateOnly); } - private void handlePreferenceChange(SharedPreferences prefs, String key) { + private void handlePreferenceChange(SharedPreferences prefs, @NonNull String key) { if (key.equals(getString(R.string.downloads_maximum_retry))) { try { String value = prefs.getString(key, getString(R.string.downloads_maximum_retry_default)); - mManager.mPrefMaxRetry = Integer.parseInt(value); + mManager.mPrefMaxRetry = value == null ? 0 : Integer.parseInt(value); } catch (Exception e) { mManager.mPrefMaxRetry = 0; } mManager.updateMaximumAttempts(); } else if (key.equals(getString(R.string.downloads_cross_network))) { - mManager.mPrefCrossNetwork = prefs.getBoolean(key, false); + mManager.mPrefMeteredDownloads = prefs.getBoolean(key, false); + } else if (key.equals(getString(R.string.downloads_queue_limit))) { + mManager.mPrefQueueLimit = prefs.getBoolean(key, true); + } else if (key.equals(getString(R.string.download_path_video_key))) { + mManager.mMainStorageVideo = loadMainVideoStorage(); + } else if (key.equals(getString(R.string.download_path_audio_key))) { + mManager.mMainStorageAudio = loadMainAudioStorage(); } } @@ -350,46 +361,78 @@ public class DownloadManagerService extends Service { mForeground = state; } - public static void startMission(Context context, String urls[], String location, String name, char kind, + /** + * Start a new download mission + * + * @param context the activity context + * @param urls the list of urls to download + * @param storage where the file is saved + * @param kind type of file (a: audio v: video s: subtitle ?: file-extension defined) + * @param threads the number of threads maximal used to download chunks of the file. + * @param psName the name of the required post-processing algorithm, or {@code null} to ignore. + * @param source source url of the resource + * @param psArgs the arguments for the post-processing algorithm. + * @param nearLength the approximated final length of the file + */ + public static void startMission(Context context, String[] urls, StoredFileHelper storage, char kind, int threads, String source, String psName, String[] psArgs, long nearLength) { Intent intent = new Intent(context, DownloadManagerService.class); intent.setAction(Intent.ACTION_RUN); intent.putExtra(EXTRA_URLS, urls); - intent.putExtra(EXTRA_NAME, name); - intent.putExtra(EXTRA_LOCATION, location); intent.putExtra(EXTRA_KIND, kind); intent.putExtra(EXTRA_THREADS, threads); intent.putExtra(EXTRA_SOURCE, source); intent.putExtra(EXTRA_POSTPROCESSING_NAME, psName); intent.putExtra(EXTRA_POSTPROCESSING_ARGS, psArgs); intent.putExtra(EXTRA_NEAR_LENGTH, nearLength); + + intent.putExtra(EXTRA_PARENT_PATH, storage.getParentUri()); + intent.putExtra(EXTRA_PATH, storage.getUri()); + intent.putExtra(EXTRA_STORAGE_TAG, storage.getTag()); + context.startService(intent); } - public static void checkForRunningMission(Context context, String location, String name, DMChecker check) { - Intent intent = new Intent(); - intent.setClass(context, DownloadManagerService.class); - context.bindService(intent, new ServiceConnection() { - @Override - public void onServiceConnected(ComponentName cname, IBinder service) { - try { - ((DMBinder) service).getDownloadManager().checkForRunningMission(location, name, check); - } catch (Exception err) { - Log.w(TAG, "checkForRunningMission() callback is defective", err); - } + private void startMission(Intent intent) { + String[] urls = intent.getStringArrayExtra(EXTRA_URLS); + Uri path = intent.getParcelableExtra(EXTRA_PATH); + Uri parentPath = intent.getParcelableExtra(EXTRA_PARENT_PATH); + int threads = intent.getIntExtra(EXTRA_THREADS, 1); + char kind = intent.getCharExtra(EXTRA_KIND, '?'); + String psName = intent.getStringExtra(EXTRA_POSTPROCESSING_NAME); + String[] psArgs = intent.getStringArrayExtra(EXTRA_POSTPROCESSING_ARGS); + String source = intent.getStringExtra(EXTRA_SOURCE); + long nearLength = intent.getLongExtra(EXTRA_NEAR_LENGTH, 0); + String tag = intent.getStringExtra(EXTRA_STORAGE_TAG); - // TODO: find a efficient way to unbind the service. This destroy the service due idle, but is started again when the user start a download. - context.unbindService(this); - } + StoredFileHelper storage; + try { + storage = new StoredFileHelper(this, parentPath, path, tag); + } catch (IOException e) { + throw new RuntimeException(e);// this never should happen + } - @Override - public void onServiceDisconnected(ComponentName name) { - } - }, Context.BIND_AUTO_CREATE); + Postprocessing ps; + if (psName == null) + ps = null; + else + ps = Postprocessing.getAlgorithm(psName, psArgs); + + final DownloadMission mission = new DownloadMission(urls, storage, kind, ps); + mission.threadCount = threads; + mission.source = source; + mission.nearLength = nearLength; + + if (ps != null) + ps.setTemporalDir(DownloadManager.pickAvailableTemporalDir(this)); + + handleConnectivityState(true);// first check the actual network status + + mManager.startMission(mission); } public void notifyFinishedDownload(String name) { - if (!mDownloadNotificationEnable || notificationManager == null) { + if (!mDownloadNotificationEnable || mNotificationManager == null) { return; } @@ -428,7 +471,7 @@ public class DownloadManagerService extends Service { downloadDoneNotification.setContentText(downloadDoneList); } - notificationManager.notify(DOWNLOADS_NOTIFICATION_ID, downloadDoneNotification.build()); + mNotificationManager.notify(DOWNLOADS_NOTIFICATION_ID, downloadDoneNotification.build()); downloadDoneCount++; } @@ -450,15 +493,15 @@ public class DownloadManagerService extends Service { if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { downloadFailedNotification.setContentTitle(getString(R.string.app_name)); downloadFailedNotification.setStyle(new NotificationCompat.BigTextStyle() - .bigText(getString(R.string.download_failed).concat(": ").concat(mission.name))); + .bigText(getString(R.string.download_failed).concat(": ").concat(mission.storage.getName()))); } else { downloadFailedNotification.setContentTitle(getString(R.string.download_failed)); - downloadFailedNotification.setContentText(mission.name); + downloadFailedNotification.setContentText(mission.storage.getName()); downloadFailedNotification.setStyle(new NotificationCompat.BigTextStyle() - .bigText(mission.name)); + .bigText(mission.storage.getName())); } - notificationManager.notify(id, downloadFailedNotification.build()); + mNotificationManager.notify(id, downloadFailedNotification.build()); } private PendingIntent makePendingIntent(String action) { @@ -466,7 +509,7 @@ public class DownloadManagerService extends Service { return PendingIntent.getService(this, intent.hashCode(), intent, PendingIntent.FLAG_UPDATE_CURRENT); } - private void manageObservers(Handler handler, boolean add) { + private void manageObservers(Callback handler, boolean add) { synchronized (mEchoObservers) { if (add) { mEchoObservers.add(handler); @@ -487,30 +530,84 @@ public class DownloadManagerService extends Service { mLockAcquired = acquire; } - // Wrapper of DownloadManager - public class DMBinder extends Binder { + private StoredDirectoryHelper loadMainVideoStorage() { + return loadMainStorage(R.string.download_path_video_key, DownloadManager.TAG_VIDEO); + } + + private StoredDirectoryHelper loadMainAudioStorage() { + return loadMainStorage(R.string.download_path_audio_key, DownloadManager.TAG_AUDIO); + } + + private StoredDirectoryHelper loadMainStorage(@StringRes int prefKey, String tag) { + String path = mPrefs.getString(getString(prefKey), null); + + if (path == null || path.isEmpty()) return null; + + if (path.charAt(0) == File.separatorChar) { + Log.i(TAG, "Old save path style present: " + path); + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) + path = Uri.fromFile(new File(path)).toString(); + else + path = ""; + + mPrefs.edit().putString(getString(prefKey), "").apply(); + } + + try { + return new StoredDirectoryHelper(this, Uri.parse(path), tag); + } catch (Exception e) { + Log.e(TAG, "Failed to load the storage of " + tag + " from " + path, e); + Toast.makeText(this, R.string.no_available_dir, Toast.LENGTH_LONG).show(); + } + + return null; + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + // Wrappers for DownloadManager + //////////////////////////////////////////////////////////////////////////////////////////////// + + public class DownloadManagerBinder extends Binder { public DownloadManager getDownloadManager() { return mManager; } - public void addMissionEventListener(Handler handler) { + @Nullable + public StoredDirectoryHelper getMainStorageVideo() { + return mManager.mMainStorageVideo; + } + + @Nullable + public StoredDirectoryHelper getMainStorageAudio() { + return mManager.mMainStorageAudio; + } + + public boolean askForSavePath() { + return DownloadManagerService.this.mPrefs.getBoolean( + DownloadManagerService.this.getString(R.string.downloads_storage_ask), + false + ); + } + + public void addMissionEventListener(Callback handler) { manageObservers(handler, true); } - public void removeMissionEventListener(Handler handler) { + public void removeMissionEventListener(Callback handler) { manageObservers(handler, false); } public void clearDownloadNotifications() { - if (notificationManager == null) return; + if (mNotificationManager == null) return; if (downloadDoneNotification != null) { - notificationManager.cancel(DOWNLOADS_NOTIFICATION_ID); + mNotificationManager.cancel(DOWNLOADS_NOTIFICATION_ID); downloadDoneList.setLength(0); downloadDoneCount = 0; } if (downloadFailedNotification != null) { for (; downloadFailedNotificationID > DOWNLOADS_NOTIFICATION_ID; downloadFailedNotificationID--) { - notificationManager.cancel(downloadFailedNotificationID); + mNotificationManager.cancel(downloadFailedNotificationID); } mFailedDownloads.clear(); downloadFailedNotificationID++; @@ -523,8 +620,4 @@ public class DownloadManagerService extends Service { } - public interface DMChecker { - void callback(boolean listed, boolean finished); - } - } diff --git a/app/src/main/java/us/shandian/giga/service/MissionState.java b/app/src/main/java/us/shandian/giga/service/MissionState.java new file mode 100644 index 000000000..2d7802ff5 --- /dev/null +++ b/app/src/main/java/us/shandian/giga/service/MissionState.java @@ -0,0 +1,5 @@ +package us.shandian.giga.service; + +public enum MissionState { + None, Pending, PendingRunning, Finished +} diff --git a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java index 4a35aa166..6d1169031 100644 --- a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java +++ b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java @@ -8,21 +8,21 @@ import android.content.Intent; import android.net.Uri; import android.os.AsyncTask; import android.os.Build; -import android.os.Bundle; import android.os.Handler; -import android.os.Looper; import android.os.Message; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.content.FileProvider; -import android.support.v4.view.ViewCompat; -import android.support.v7.app.AlertDialog; -import android.support.v7.util.DiffUtil; -import android.support.v7.widget.RecyclerView; -import android.support.v7.widget.RecyclerView.ViewHolder; -import android.support.v7.widget.RecyclerView.Adapter; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.annotation.StringRes; +import androidx.core.content.FileProvider; +import androidx.core.view.ViewCompat; +import androidx.appcompat.app.AlertDialog; +import androidx.recyclerview.widget.DiffUtil; +import androidx.recyclerview.widget.RecyclerView; +import androidx.recyclerview.widget.RecyclerView.Adapter; +import androidx.recyclerview.widget.RecyclerView.ViewHolder; import android.util.Log; import android.util.SparseArray; +import android.view.HapticFeedbackConstants; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; @@ -36,14 +36,20 @@ import android.widget.Toast; import org.schabi.newpipe.BuildConfig; import org.schabi.newpipe.R; +import org.schabi.newpipe.report.ErrorActivity; +import org.schabi.newpipe.report.UserAction; import org.schabi.newpipe.util.NavigationHelper; import java.io.File; import java.lang.ref.WeakReference; +import java.net.URI; import java.util.ArrayList; +import java.util.Collections; import us.shandian.giga.get.DownloadMission; import us.shandian.giga.get.FinishedMission; +import us.shandian.giga.get.Mission; +import us.shandian.giga.io.StoredFileHelper; import us.shandian.giga.service.DownloadManager; import us.shandian.giga.service.DownloadManagerService; import us.shandian.giga.ui.common.Deleter; @@ -57,18 +63,24 @@ import static us.shandian.giga.get.DownloadMission.ERROR_CONNECT_HOST; import static us.shandian.giga.get.DownloadMission.ERROR_FILE_CREATION; import static us.shandian.giga.get.DownloadMission.ERROR_HTTP_NO_CONTENT; import static us.shandian.giga.get.DownloadMission.ERROR_HTTP_UNSUPPORTED_RANGE; +import static us.shandian.giga.get.DownloadMission.ERROR_INSUFFICIENT_STORAGE; import static us.shandian.giga.get.DownloadMission.ERROR_NOTHING; import static us.shandian.giga.get.DownloadMission.ERROR_PATH_CREATION; import static us.shandian.giga.get.DownloadMission.ERROR_PERMISSION_DENIED; import static us.shandian.giga.get.DownloadMission.ERROR_POSTPROCESSING; +import static us.shandian.giga.get.DownloadMission.ERROR_POSTPROCESSING_HOLD; +import static us.shandian.giga.get.DownloadMission.ERROR_POSTPROCESSING_STOPPED; +import static us.shandian.giga.get.DownloadMission.ERROR_PROGRESS_LOST; import static us.shandian.giga.get.DownloadMission.ERROR_SSL_EXCEPTION; +import static us.shandian.giga.get.DownloadMission.ERROR_TIMEOUT; import static us.shandian.giga.get.DownloadMission.ERROR_UNKNOWN_EXCEPTION; import static us.shandian.giga.get.DownloadMission.ERROR_UNKNOWN_HOST; -public class MissionAdapter extends Adapter { +public class MissionAdapter extends Adapter implements Handler.Callback { private static final SparseArray ALGORITHMS = new SparseArray<>(); private static final String TAG = "MissionAdapter"; private static final String UNDEFINED_PROGRESS = "--.-%"; + private static final String DEFAULT_MIME_TYPE = "*/*"; static { @@ -85,9 +97,12 @@ public class MissionAdapter extends Adapter { private ArrayList mPendingDownloadsItems = new ArrayList<>(); private Handler mHandler; private MenuItem mClear; + private MenuItem mStartButton; + private MenuItem mPauseButton; private View mEmptyMessage; + private RecoverHelper mRecover; - public MissionAdapter(Context context, DownloadManager downloadManager, MenuItem clearButton, View emptyMessage) { + public MissionAdapter(Context context, @NonNull DownloadManager downloadManager, View emptyMessage) { mContext = context; mDownloadManager = downloadManager; mDeleter = null; @@ -95,20 +110,8 @@ public class MissionAdapter extends Adapter { mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mLayout = R.layout.mission_item; - mHandler = new Handler(Looper.myLooper()) { - @Override - public void handleMessage(Message msg) { - switch (msg.what) { - case DownloadManagerService.MESSAGE_PROGRESS: - case DownloadManagerService.MESSAGE_ERROR: - case DownloadManagerService.MESSAGE_FINISHED: - onServiceMessage(msg); - break; - } - } - }; + mHandler = new Handler(context.getMainLooper()); - mClear = clearButton; mEmptyMessage = emptyMessage; mIterator = downloadManager.getIterator(); @@ -137,7 +140,10 @@ public class MissionAdapter extends Adapter { if (h.item.mission instanceof DownloadMission) { mPendingDownloadsItems.remove(h); - if (mPendingDownloadsItems.size() < 1) setAutoRefresh(false); + if (mPendingDownloadsItems.size() < 1) { + setAutoRefresh(false); + checkMasterButtonsVisibility(); + } } h.popupMenu.dismiss(); @@ -170,10 +176,10 @@ public class MissionAdapter extends Adapter { ViewHolderItem h = (ViewHolderItem) view; h.item = item; - Utility.FileType type = Utility.getFileType(item.mission.kind, item.mission.name); + Utility.FileType type = Utility.getFileType(item.mission.kind, item.mission.storage.getName()); h.icon.setImageResource(Utility.getIconForFileType(type)); - h.name.setText(item.mission.name); + h.name.setText(item.mission.storage.getName()); h.progress.setColors(Utility.getBackgroundForFileType(mContext, type), Utility.getForegroundForFileType(mContext, type)); @@ -225,8 +231,10 @@ public class MissionAdapter extends Adapter { long deltaDone = mission.done - h.lastDone; boolean hasError = mission.errCode != ERROR_NOTHING; - // on error hide marquee or show if condition (mission.done < 1 || mission.unknownLength) is true - h.progress.setMarquee(!hasError && (mission.done < 1 || mission.unknownLength)); + // hide on error + // show if current resource length is not fetched + // show if length is unknown + h.progress.setMarquee(!hasError && (!mission.isInitialized() || mission.unknownLength)); float progress; if (mission.unknownLength) { @@ -252,7 +260,7 @@ public class MissionAdapter extends Adapter { long length = mission.getLength(); int state; - if (mission.isPsFailed()) { + if (mission.isPsFailed() || mission.errCode == ERROR_POSTPROCESSING_HOLD) { state = 0; } else if (!mission.running) { state = mission.enqueued ? 1 : 2; @@ -305,145 +313,224 @@ public class MissionAdapter extends Adapter { } } - private boolean viewWithFileProvider(@NonNull File file) { - if (!file.exists()) return true; + private void viewWithFileProvider(Mission mission) { + if (checkInvalidFile(mission)) return; - String ext = Utility.getFileExt(file.getName()); - if (ext == null) return false; + String mimeType = resolveMimeType(mission); - String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext.substring(1)); - Log.v(TAG, "Mime: " + mimeType + " package: " + BuildConfig.APPLICATION_ID + ".provider"); + if (BuildConfig.DEBUG) + Log.v(TAG, "Mime: " + mimeType + " package: " + BuildConfig.APPLICATION_ID + ".provider"); - Uri uri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".provider", file); + Uri uri; + + if (mission.storage.isDirect()) { + uri = FileProvider.getUriForFile( + mContext, + BuildConfig.APPLICATION_ID + ".provider", + new File(URI.create(mission.storage.getUri().toString())) + ); + } else { + uri = mission.storage.getUri(); + } Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(uri, mimeType); intent.addFlags(FLAG_GRANT_READ_URI_PERMISSION); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { intent.addFlags(FLAG_GRANT_PREFIX_URI_PERMISSION); } - if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) { + if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) { intent.addFlags(FLAG_ACTIVITY_NEW_TASK); } + //mContext.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION); - Log.v(TAG, "Starting intent: " + intent); + if (intent.resolveActivity(mContext.getPackageManager()) != null) { mContext.startActivity(intent); } else { - Toast noPlayerToast = Toast.makeText(mContext, R.string.toast_no_player, Toast.LENGTH_LONG); - noPlayerToast.show(); + Toast.makeText(mContext, R.string.toast_no_player, Toast.LENGTH_LONG).show(); + } + } + + private void shareFile(Mission mission) { + if (checkInvalidFile(mission)) return; + + Intent intent = new Intent(Intent.ACTION_SEND); + intent.setType(resolveMimeType(mission)); + intent.putExtra(Intent.EXTRA_STREAM, mission.storage.getUri()); + + mContext.startActivity(Intent.createChooser(intent, null)); + } + + private static String resolveMimeType(@NonNull Mission mission) { + String mimeType; + + if (!mission.storage.isInvalid()) { + mimeType = mission.storage.getType(); + if (mimeType != null && mimeType.length() > 0 && !mimeType.equals(StoredFileHelper.DEFAULT_MIME)) + return mimeType; } + String ext = Utility.getFileExt(mission.storage.getName()); + if (ext == null) return DEFAULT_MIME_TYPE; + + mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext.substring(1)); + + return mimeType == null ? DEFAULT_MIME_TYPE : mimeType; + } + + private boolean checkInvalidFile(@NonNull Mission mission) { + if (mission.storage.existsAsFile()) return false; + + Toast.makeText(mContext, R.string.missing_file, Toast.LENGTH_SHORT).show(); return true; } - public Handler getMessenger() { - return mHandler; - } + @Override + public boolean handleMessage(@NonNull Message msg) { + if (mStartButton != null && mPauseButton != null) { + checkMasterButtonsVisibility(); + } - private void onServiceMessage(@NonNull Message msg) { switch (msg.what) { case DownloadManagerService.MESSAGE_PROGRESS: - setAutoRefresh(true); - return; case DownloadManagerService.MESSAGE_ERROR: case DownloadManagerService.MESSAGE_FINISHED: break; default: - return; + return false; } - for (int i = 0; i < mPendingDownloadsItems.size(); i++) { - ViewHolderItem h = mPendingDownloadsItems.get(i); + if (msg.what == DownloadManagerService.MESSAGE_PROGRESS) { + setAutoRefresh(true); + return true; + } + + for (ViewHolderItem h : mPendingDownloadsItems) { if (h.item.mission != msg.obj) continue; if (msg.what == DownloadManagerService.MESSAGE_FINISHED) { // DownloadManager should mark the download as finished applyChanges(); - return; + return true; } updateProgress(h); - return; + return true; } + + return false; } private void showError(@NonNull DownloadMission mission) { - StringBuilder str = new StringBuilder(); - str.append(mContext.getString(R.string.label_code)); - str.append(": "); - str.append(mission.errCode); - str.append('\n'); + @StringRes int msg = R.string.general_error; + String msgEx = null; switch (mission.errCode) { case 416: - str.append(mContext.getString(R.string.error_http_requested_range_not_satisfiable)); + msg = R.string.error_http_requested_range_not_satisfiable; break; case 404: - str.append(mContext.getString(R.string.error_http_not_found)); + msg = R.string.error_http_not_found; break; case ERROR_NOTHING: - str.append("¿?"); - break; + return;// this never should happen case ERROR_FILE_CREATION: - str.append(mContext.getString(R.string.error_file_creation)); + msg = R.string.error_file_creation; break; case ERROR_HTTP_NO_CONTENT: - str.append(mContext.getString(R.string.error_http_no_content)); + msg = R.string.error_http_no_content; break; case ERROR_HTTP_UNSUPPORTED_RANGE: - str.append(mContext.getString(R.string.error_http_unsupported_range)); + msg = R.string.error_http_unsupported_range; break; case ERROR_PATH_CREATION: - str.append(mContext.getString(R.string.error_path_creation)); + msg = R.string.error_path_creation; break; case ERROR_PERMISSION_DENIED: - str.append(mContext.getString(R.string.permission_denied)); + msg = R.string.permission_denied; break; case ERROR_SSL_EXCEPTION: - str.append(mContext.getString(R.string.error_ssl_exception)); + msg = R.string.error_ssl_exception; break; case ERROR_UNKNOWN_HOST: - str.append(mContext.getString(R.string.error_unknown_host)); + msg = R.string.error_unknown_host; break; case ERROR_CONNECT_HOST: - str.append(mContext.getString(R.string.error_connect_host)); + msg = R.string.error_connect_host; + break; + case ERROR_POSTPROCESSING_STOPPED: + msg = R.string.error_postprocessing_stopped; break; case ERROR_POSTPROCESSING: - str.append(mContext.getString(R.string.error_postprocessing_failed)); + case ERROR_POSTPROCESSING_HOLD: + showError(mission.errObject, UserAction.DOWNLOAD_POSTPROCESSING, R.string.error_postprocessing_failed); + return; + case ERROR_INSUFFICIENT_STORAGE: + msg = R.string.error_insufficient_storage; + break; case ERROR_UNKNOWN_EXCEPTION: + showError(mission.errObject, UserAction.DOWNLOAD_FAILED, R.string.general_error); + return; + case ERROR_PROGRESS_LOST: + msg = R.string.error_progress_lost; + break; + case ERROR_TIMEOUT: + msg = R.string.error_timeout; break; default: if (mission.errCode >= 100 && mission.errCode < 600) { - str = new StringBuilder(8); - str.append("HTTP "); - str.append(mission.errCode); + msgEx = "HTTP " + mission.errCode; } else if (mission.errObject == null) { - str.append("(not_decelerated_error_code)"); + msgEx = "(not_decelerated_error_code)"; + } else { + showError(mission.errObject, UserAction.DOWNLOAD_FAILED, msg); + return; } break; } - if (mission.errObject != null) { - str.append("\n\n"); - str.append(mission.errObject.toString()); + AlertDialog.Builder builder = new AlertDialog.Builder(mContext); + + if (msgEx != null) + builder.setMessage(msgEx); + else + builder.setMessage(msg); + + // add report button for non-HTTP errors (range 100-599) + if (mission.errObject != null && (mission.errCode < 100 || mission.errCode >= 600)) { + @StringRes final int mMsg = msg; + builder.setPositiveButton(R.string.error_report_title, (dialog, which) -> + showError(mission.errObject, UserAction.DOWNLOAD_FAILED, mMsg) + ); } - AlertDialog.Builder builder = new AlertDialog.Builder(mContext); - builder.setTitle(mission.name) - .setMessage(str) - .setNegativeButton(android.R.string.ok, (dialog, which) -> dialog.cancel()) + builder.setNegativeButton(android.R.string.ok, (dialog, which) -> dialog.cancel()) + .setTitle(mission.storage.getName()) .create() .show(); } + private void showError(Exception exception, UserAction action, @StringRes int reason) { + ErrorActivity.reportError( + mContext, + Collections.singletonList(exception), + null, + null, + ErrorActivity.ErrorInfo.make(action, "-", "-", reason) + ); + } + public void clearFinishedDownloads() { mDownloadManager.forgetFinishedDownloads(); applyChanges(); } private boolean handlePopupItem(@NonNull ViewHolderItem h, @NonNull MenuItem option) { + if (h.item == null) return true; + int id = option.getItemId(); DownloadMission mission = h.item.mission instanceof DownloadMission ? (DownloadMission) h.item.mission : null; @@ -466,16 +553,32 @@ public class MissionAdapter extends Adapter { showError(mission); return true; case R.id.queue: - h.queue.setChecked(!h.queue.isChecked()); - mission.enqueued = h.queue.isChecked(); + boolean flag = !h.queue.isChecked(); + h.queue.setChecked(flag); + mission.setEnqueued(flag); updateProgress(h); return true; + case R.id.retry: + if (mission.isPsRunning()) { + mission.psContinue(true); + } else { + mDownloadManager.tryRecover(mission); + if (mission.storage.isInvalid()) + mRecover.tryRecover(mission); + else + recoverMission(mission); + } + return true; + case R.id.cancel: + mission.psContinue(false); + return false; } } switch (id) { - case R.id.open: - return viewWithFileProvider(h.item.mission.getDownloadedFile()); + case R.id.menu_item_share: + shareFile(h.item.mission); + return true; case R.id.delete: if (mDeleter == null) { mDownloadManager.deleteMission(h.item.mission); @@ -486,7 +589,7 @@ public class MissionAdapter extends Adapter { return true; case R.id.md5: case R.id.sha1: - new ChecksumTask(mContext).execute(h.item.mission.getDownloadedFile().getAbsolutePath(), ALGORITHMS.get(id)); + new ChecksumTask(mContext).execute(h.item.mission.storage, ALGORITHMS.get(id)); return true; case R.id.source: /*Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(h.item.mission.source)); @@ -529,29 +632,78 @@ public class MissionAdapter extends Adapter { } public void setClearButton(MenuItem clearButton) { - if (mClear == null) clearButton.setVisible(mIterator.hasFinishedMissions()); + if (mClear == null) + clearButton.setVisible(mIterator.hasFinishedMissions()); + mClear = clearButton; } + public void setMasterButtons(MenuItem startButton, MenuItem pauseButton) { + boolean init = mStartButton == null || mPauseButton == null; + + mStartButton = startButton; + mPauseButton = pauseButton; + + if (init) checkMasterButtonsVisibility(); + } + private void checkEmptyMessageVisibility() { int flag = mIterator.getOldListSize() > 0 ? View.GONE : View.VISIBLE; if (mEmptyMessage.getVisibility() != flag) mEmptyMessage.setVisibility(flag); } - - public void deleterDispose(Bundle bundle) { - if (mDeleter != null) mDeleter.dispose(bundle); + public void checkMasterButtonsVisibility() { + boolean[] state = mIterator.hasValidPendingMissions(); + setButtonVisible(mPauseButton, state[0]); + setButtonVisible(mStartButton, state[1]); } - public void deleterLoad(Bundle bundle, View view) { + private static void setButtonVisible(MenuItem button, boolean visible) { + if (button.isVisible() != visible) + button.setVisible(visible); + } + + public void ensurePausedMissions() { + for (ViewHolderItem h : mPendingDownloadsItems) { + if (((DownloadMission) h.item.mission).running) continue; + updateProgress(h); + h.lastTimeStamp = -1; + h.lastDone = -1; + } + } + + + public void deleterDispose(boolean commitChanges) { + if (mDeleter != null) mDeleter.dispose(commitChanges); + } + + public void deleterLoad(View view) { if (mDeleter == null) - mDeleter = new Deleter(bundle, view, mContext, this, mDownloadManager, mIterator, mHandler); + mDeleter = new Deleter(view, mContext, this, mDownloadManager, mIterator, mHandler); } public void deleterResume() { if (mDeleter != null) mDeleter.resume(); } + public void recoverMission(DownloadMission mission) { + for (ViewHolderItem h : mPendingDownloadsItems) { + if (mission != h.item.mission) continue; + + mission.errObject = null; + mission.resetState(true, false, DownloadMission.ERROR_NOTHING); + + h.status.setText(UNDEFINED_PROGRESS); + h.state = -1; + h.size.setText(Utility.formatBytes(mission.getLength())); + h.progress.setMarquee(true); + + mDownloadManager.resumeMission(mission); + return; + } + + } + private boolean mUpdaterRunning = false; private final Runnable rUpdater = this::updater; @@ -593,6 +745,10 @@ public class MissionAdapter extends Adapter { return Float.isNaN(value) || Float.isInfinite(value); } + public void setRecover(@NonNull RecoverHelper callback) { + mRecover = callback; + } + class ViewHolderItem extends RecyclerView.ViewHolder { DownloadManager.MissionItem item; @@ -604,6 +760,8 @@ public class MissionAdapter extends Adapter { ProgressDrawable progress; PopupMenu popupMenu; + MenuItem retry; + MenuItem cancel; MenuItem start; MenuItem pause; MenuItem open; @@ -636,22 +794,34 @@ public class MissionAdapter extends Adapter { button.setOnClickListener(v -> showPopupMenu()); Menu menu = popupMenu.getMenu(); + retry = menu.findItem(R.id.retry); + cancel = menu.findItem(R.id.cancel); start = menu.findItem(R.id.start); pause = menu.findItem(R.id.pause); - open = menu.findItem(R.id.open); + open = menu.findItem(R.id.menu_item_share); queue = menu.findItem(R.id.queue); showError = menu.findItem(R.id.error_message_view); delete = menu.findItem(R.id.delete); source = menu.findItem(R.id.source); checksum = menu.findItem(R.id.checksum); - itemView.setOnClickListener((v) -> { + itemView.setHapticFeedbackEnabled(true); + + itemView.setOnClickListener(v -> { if (item.mission instanceof FinishedMission) - viewWithFileProvider(item.mission.getDownloadedFile()); + viewWithFileProvider(item.mission); + }); + + itemView.setOnLongClickListener(v -> { + v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); + showPopupMenu(); + return true; }); } private void showPopupMenu() { + retry.setVisible(false); + cancel.setVisible(false); start.setVisible(false); pause.setVisible(false); open.setVisible(false); @@ -664,7 +834,20 @@ public class MissionAdapter extends Adapter { DownloadMission mission = item.mission instanceof DownloadMission ? (DownloadMission) item.mission : null; if (mission != null) { - if (!mission.isPsRunning()) { + if (mission.hasInvalidStorage()) { + retry.setVisible(true); + delete.setVisible(true); + showError.setVisible(true); + } else if (mission.isPsRunning()) { + switch (mission.errCode) { + case ERROR_INSUFFICIENT_STORAGE: + case ERROR_POSTPROCESSING_HOLD: + retry.setVisible(true); + cancel.setVisible(true); + showError.setVisible(true); + break; + } + } else { if (mission.running) { pause.setVisible(true); } else { @@ -713,7 +896,7 @@ public class MissionAdapter extends Adapter { } - static class ChecksumTask extends AsyncTask { + static class ChecksumTask extends AsyncTask { ProgressDialog progressDialog; WeakReference weakReference; @@ -736,8 +919,8 @@ public class MissionAdapter extends Adapter { } @Override - protected String doInBackground(String... params) { - return Utility.checksum(params[0], params[1]); + protected String doInBackground(Object... params) { + return Utility.checksum((StoredFileHelper) params[0], (String) params[1]); } @Override @@ -764,4 +947,8 @@ public class MissionAdapter extends Adapter { } } + public interface RecoverHelper { + void tryRecover(DownloadMission mission); + } + } diff --git a/app/src/main/java/us/shandian/giga/ui/common/Deleter.java b/app/src/main/java/us/shandian/giga/ui/common/Deleter.java index 6407ab019..81b4e33e8 100644 --- a/app/src/main/java/us/shandian/giga/ui/common/Deleter.java +++ b/app/src/main/java/us/shandian/giga/ui/common/Deleter.java @@ -3,10 +3,8 @@ package us.shandian.giga.ui.common; import android.content.Context; import android.content.Intent; import android.graphics.Color; -import android.net.Uri; -import android.os.Bundle; import android.os.Handler; -import android.support.design.widget.Snackbar; +import com.google.android.material.snackbar.Snackbar; import android.view.View; import org.schabi.newpipe.R; @@ -23,8 +21,6 @@ public class Deleter { private static final int TIMEOUT = 5000;// ms private static final int DELAY = 350;// ms private static final int DELAY_RESUME = 400;// ms - private static final String BUNDLE_NAMES = "us.shandian.giga.ui.common.deleter.names"; - private static final String BUNDLE_LOCATIONS = "us.shandian.giga.ui.common.deleter.locations"; private Snackbar snackbar; private ArrayList items; @@ -41,7 +37,7 @@ public class Deleter { private final Runnable rNext; private final Runnable rCommit; - public Deleter(Bundle b, View v, Context c, MissionAdapter a, DownloadManager d, MissionIterator i, Handler h) { + public Deleter(View v, Context c, MissionAdapter a, DownloadManager d, MissionIterator i, Handler h) { mView = v; mContext = c; mAdapter = a; @@ -55,27 +51,6 @@ public class Deleter { rCommit = this::commit; items = new ArrayList<>(2); - - if (b != null) { - String[] names = b.getStringArray(BUNDLE_NAMES); - String[] locations = b.getStringArray(BUNDLE_LOCATIONS); - - if (names == null || locations == null) return; - if (names.length < 1 || locations.length < 1) return; - if (names.length != locations.length) return; - - items.ensureCapacity(names.length); - - for (int j = 0; j < locations.length; j++) { - Mission mission = mDownloadManager.getAnyMission(locations[j], names[j]); - if (mission == null) continue; - - items.add(mission); - mIterator.hide(mission); - } - - if (items.size() > 0) resume(); - } } public void append(Mission item) { @@ -104,7 +79,7 @@ public class Deleter { private void next() { if (items.size() < 1) return; - String msg = mContext.getString(R.string.file_deleted).concat(":\n").concat(items.get(0).name); + String msg = mContext.getString(R.string.file_deleted).concat(":\n").concat(items.get(0).storage.getName()); snackbar = Snackbar.make(mView, msg, Snackbar.LENGTH_INDEFINITE); snackbar.setAction(R.string.undo, s -> forget()); @@ -125,7 +100,7 @@ public class Deleter { mDownloadManager.deleteMission(mission); if (mission instanceof FinishedMission) { - mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(mission.getDownloadedFile()))); + mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mission.storage.getUri())); } break; } @@ -151,27 +126,14 @@ public class Deleter { mHandler.postDelayed(rShow, DELAY_RESUME); } - public void dispose(Bundle bundle) { + public void dispose(boolean commitChanges) { if (items.size() < 1) return; pause(); - if (bundle == null) { - for (Mission mission : items) mDownloadManager.deleteMission(mission); - items = null; - return; - } + if (!commitChanges) return; - String[] names = new String[items.size()]; - String[] locations = new String[items.size()]; - - for (int i = 0; i < items.size(); i++) { - Mission mission = items.get(i); - names[i] = mission.name; - locations[i] = mission.location; - } - - bundle.putStringArray(BUNDLE_NAMES, names); - bundle.putStringArray(BUNDLE_LOCATIONS, locations); + for (Mission mission : items) mDownloadManager.deleteMission(mission); + items = null; } } diff --git a/app/src/main/java/us/shandian/giga/ui/common/ProgressDrawable.java b/app/src/main/java/us/shandian/giga/ui/common/ProgressDrawable.java index 33eba22eb..a0ff24aaa 100644 --- a/app/src/main/java/us/shandian/giga/ui/common/ProgressDrawable.java +++ b/app/src/main/java/us/shandian/giga/ui/common/ProgressDrawable.java @@ -9,8 +9,8 @@ import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.os.Handler; import android.os.Looper; -import android.support.annotation.ColorInt; -import android.support.annotation.NonNull; +import androidx.annotation.ColorInt; +import androidx.annotation.NonNull; public class ProgressDrawable extends Drawable { private static final int MARQUEE_INTERVAL = 150; diff --git a/app/src/main/java/us/shandian/giga/ui/common/ToolbarActivity.java b/app/src/main/java/us/shandian/giga/ui/common/ToolbarActivity.java index 6a04db5cd..2ba091573 100644 --- a/app/src/main/java/us/shandian/giga/ui/common/ToolbarActivity.java +++ b/app/src/main/java/us/shandian/giga/ui/common/ToolbarActivity.java @@ -1,8 +1,8 @@ package us.shandian.giga.ui.common; import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; import org.schabi.newpipe.R; diff --git a/app/src/main/java/us/shandian/giga/ui/fragment/MissionsFragment.java b/app/src/main/java/us/shandian/giga/ui/fragment/MissionsFragment.java index c4fd3b5fd..26da47b1f 100644 --- a/app/src/main/java/us/shandian/giga/ui/fragment/MissionsFragment.java +++ b/app/src/main/java/us/shandian/giga/ui/fragment/MissionsFragment.java @@ -1,40 +1,57 @@ package us.shandian.giga.ui.fragment; import android.app.Activity; -import android.app.Fragment; +import android.app.AlertDialog; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.content.SharedPreferences; +import android.net.Uri; import android.os.Bundle; +import android.os.Environment; import android.os.IBinder; import android.preference.PreferenceManager; -import android.support.v7.widget.GridLayoutManager; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; +import androidx.recyclerview.widget.GridLayoutManager; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import android.widget.Toast; + +import com.nononsenseapps.filepicker.Utils; import org.schabi.newpipe.R; +import org.schabi.newpipe.settings.NewPipeSettings; +import org.schabi.newpipe.util.FilePickerActivityHelper; import org.schabi.newpipe.util.ThemeHelper; +import java.io.File; +import java.io.IOException; + +import us.shandian.giga.get.DownloadMission; +import us.shandian.giga.io.StoredFileHelper; import us.shandian.giga.service.DownloadManager; import us.shandian.giga.service.DownloadManagerService; -import us.shandian.giga.service.DownloadManagerService.DMBinder; +import us.shandian.giga.service.DownloadManagerService.DownloadManagerBinder; import us.shandian.giga.ui.adapter.MissionAdapter; public class MissionsFragment extends Fragment { private static final int SPAN_SIZE = 2; + private static final int REQUEST_DOWNLOAD_SAVE_AS = 0x1230; private SharedPreferences mPrefs; private boolean mLinear; private MenuItem mSwitch; private MenuItem mClear = null; + private MenuItem mStart = null; + private MenuItem mPause = null; private RecyclerView mList; private View mEmpty; @@ -43,23 +60,26 @@ public class MissionsFragment extends Fragment { private LinearLayoutManager mLinearManager; private Context mContext; - private DMBinder mBinder; - private Bundle mBundle; + private DownloadManagerBinder mBinder; private boolean mForceUpdate; + private DownloadMission unsafeMissionTarget = null; + private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder binder) { - mBinder = (DownloadManagerService.DMBinder) binder; + mBinder = (DownloadManagerBinder) binder; mBinder.clearDownloadNotifications(); - mAdapter = new MissionAdapter(mContext, mBinder.getDownloadManager(), mClear, mEmpty); - mAdapter.deleterLoad(mBundle, getView()); + mAdapter = new MissionAdapter(mContext, mBinder.getDownloadManager(), mEmpty); + mAdapter.deleterLoad(getView()); - mBundle = null; + mAdapter.setRecover(MissionsFragment.this::recoverMission); - mBinder.addMissionEventListener(mAdapter.getMessenger()); + setAdapterButtons(); + + mBinder.addMissionEventListener(mAdapter); mBinder.enableNotifications(false); updateList(); @@ -74,15 +94,12 @@ public class MissionsFragment extends Fragment { }; @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.missions, container, false); mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); mLinear = mPrefs.getBoolean("linear", false); - //mContext = getActivity().getApplicationContext(); - mBundle = savedInstanceState; - // Bind the service mContext.bindService(new Intent(mContext, DownloadManagerService.class), mConnection, Context.BIND_AUTO_CREATE); @@ -128,11 +145,12 @@ public class MissionsFragment extends Fragment { * deprecated in API level 23, * but must remain to allow compatibility with api<23 */ + @SuppressWarnings("deprecation") @Override public void onAttach(Activity activity) { super.onAttach(activity); - mContext = activity.getApplicationContext(); + mContext = activity; } @@ -141,10 +159,10 @@ public class MissionsFragment extends Fragment { super.onDestroy(); if (mBinder == null || mAdapter == null) return; - mBinder.removeMissionEventListener(mAdapter.getMessenger()); + mBinder.removeMissionEventListener(mAdapter); mBinder.enableNotifications(true); mContext.unbindService(mConnection); - mAdapter.deleterDispose(null); + mAdapter.deleterDispose(true); mBinder = null; mAdapter = null; @@ -154,7 +172,11 @@ public class MissionsFragment extends Fragment { public void onPrepareOptionsMenu(Menu menu) { mSwitch = menu.findItem(R.id.switch_mode); mClear = menu.findItem(R.id.clear_list); - if (mAdapter != null) mAdapter.setClearButton(mClear); + mStart = menu.findItem(R.id.start_downloads); + mPause = menu.findItem(R.id.pause_downloads); + + if (mAdapter != null) setAdapterButtons(); + super.onPrepareOptionsMenu(menu); } @@ -166,8 +188,21 @@ public class MissionsFragment extends Fragment { updateList(); return true; case R.id.clear_list: - mAdapter.clearFinishedDownloads(); + AlertDialog.Builder prompt = new AlertDialog.Builder(mContext); + prompt.setTitle(R.string.clear_finished_download); + prompt.setMessage(R.string.confirm_prompt); + prompt.setPositiveButton(android.R.string.ok, (dialog, which) -> mAdapter.clearFinishedDownloads()); + prompt.setNegativeButton(R.string.cancel, null); + prompt.create().show(); return true; + case R.id.start_downloads: + item.setVisible(false); + mBinder.getDownloadManager().startAllMissions(); + return true; + case R.id.pause_downloads: + item.setVisible(false); + mBinder.getDownloadManager().pauseAllMissions(false); + mAdapter.ensurePausedMissions();// update items view default: return super.onOptionsItemSelected(item); } @@ -193,9 +228,9 @@ public class MissionsFragment extends Fragment { int icon; if (mLinear) - icon = isLight ? R.drawable.ic_list_black_24dp : R.drawable.ic_list_white_24dp; - else icon = isLight ? R.drawable.ic_grid_black_24dp : R.drawable.ic_grid_white_24dp; + else + icon = isLight ? R.drawable.ic_list_black_24dp : R.drawable.ic_list_white_24dp; mSwitch.setIcon(icon); mSwitch.setTitle(mLinear ? R.string.grid : R.string.list); @@ -203,14 +238,47 @@ public class MissionsFragment extends Fragment { } } + private void setAdapterButtons() { + if (mClear == null || mStart == null || mPause == null) return; + + mAdapter.setClearButton(mClear); + mAdapter.setMasterButtons(mStart, mPause); + } + + private void recoverMission(@NonNull DownloadMission mission) { + unsafeMissionTarget = mission; + + if (NewPipeSettings.useStorageAccessFramework(mContext)) { + StoredFileHelper.requestSafWithFileCreation( + MissionsFragment.this, + REQUEST_DOWNLOAD_SAVE_AS, + mission.storage.getName(), + mission.storage.getType() + ); + + } else { + File initialSavePath; + if (DownloadManager.TAG_VIDEO.equals(mission.storage.getType())) + initialSavePath = NewPipeSettings.getDir(Environment.DIRECTORY_MOVIES); + else + initialSavePath = NewPipeSettings.getDir(Environment.DIRECTORY_MUSIC); + + initialSavePath = new File(initialSavePath, mission.storage.getName()); + startActivityForResult( + FilePickerActivityHelper.chooseFileToSave(mContext, initialSavePath.getAbsolutePath()), + REQUEST_DOWNLOAD_SAVE_AS + ); + } + } + @Override - public void onSaveInstanceState(Bundle outState) { + public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); if (mAdapter != null) { - mAdapter.deleterDispose(outState); + mAdapter.deleterDispose(false); mForceUpdate = true; - mBinder.removeMissionEventListener(mAdapter.getMessenger()); + mBinder.removeMissionEventListener(mAdapter); } } @@ -226,7 +294,8 @@ public class MissionsFragment extends Fragment { mAdapter.forceUpdate(); } - mBinder.addMissionEventListener(mAdapter.getMessenger()); + mBinder.addMissionEventListener(mAdapter); + mAdapter.checkMasterButtonsVisibility(); } if (mBinder != null) mBinder.enableNotifications(false); } @@ -237,4 +306,28 @@ public class MissionsFragment extends Fragment { if (mAdapter != null) mAdapter.onPaused(); if (mBinder != null) mBinder.enableNotifications(true); } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + + if (requestCode != REQUEST_DOWNLOAD_SAVE_AS || resultCode != Activity.RESULT_OK) return; + + if (unsafeMissionTarget == null || data.getData() == null) { + return; + } + + try { + Uri fileUri = data.getData(); + if (fileUri.getAuthority() != null && FilePickerActivityHelper.isOwnFileUri(mContext, fileUri)) { + fileUri = Uri.fromFile(Utils.getFileForUri(fileUri)); + } + + String tag = unsafeMissionTarget.storage.getTag(); + unsafeMissionTarget.storage = new StoredFileHelper(mContext, null, fileUri, tag); + mAdapter.recoverMission(unsafeMissionTarget); + } catch (IOException e) { + Toast.makeText(mContext, R.string.general_error, Toast.LENGTH_LONG).show(); + } + } } diff --git a/app/src/main/java/us/shandian/giga/util/Utility.java b/app/src/main/java/us/shandian/giga/util/Utility.java index e5149cf9b..21fdd72ad 100644 --- a/app/src/main/java/us/shandian/giga/util/Utility.java +++ b/app/src/main/java/us/shandian/giga/util/Utility.java @@ -4,19 +4,20 @@ import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; import android.os.Build; -import android.support.annotation.ColorInt; -import android.support.annotation.DrawableRes; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.content.ContextCompat; +import androidx.annotation.ColorInt; +import androidx.annotation.DrawableRes; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; +import android.util.Log; import android.widget.Toast; import org.schabi.newpipe.R; +import org.schabi.newpipe.streams.io.SharpStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; @@ -25,7 +26,8 @@ import java.io.Serializable; import java.net.HttpURLConnection; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; -import java.util.Locale; + +import us.shandian.giga.io.StoredFileHelper; public class Utility { @@ -80,6 +82,7 @@ public class Utility { objectInputStream = new ObjectInputStream(new FileInputStream(file)); object = (T) objectInputStream.readObject(); } catch (Exception e) { + Log.e("Utility", "Failed to deserialize the object", e); object = null; } @@ -206,7 +209,7 @@ public class Utility { Toast.makeText(context, R.string.msg_copied, Toast.LENGTH_SHORT).show(); } - public static String checksum(String path, String algorithm) { + public static String checksum(StoredFileHelper source, String algorithm) { MessageDigest md; try { @@ -215,11 +218,11 @@ public class Utility { throw new RuntimeException(e); } - FileInputStream i; + SharpStream i; try { - i = new FileInputStream(path); - } catch (FileNotFoundException e) { + i = source.getStream(); + } catch (Exception e) { throw new RuntimeException(e); } @@ -247,15 +250,15 @@ public class Utility { } @SuppressWarnings("ResultOfMethodCallIgnored") - public static boolean mkdir(File path, boolean allDirs) { - if (path.exists()) return true; + public static boolean mkdir(File p, boolean allDirs) { + if (p.exists()) return true; if (allDirs) - path.mkdirs(); + p.mkdirs(); else - path.mkdir(); + p.mkdir(); - return path.exists(); + return p.exists(); } public static long getContentLength(HttpURLConnection connection) { @@ -264,8 +267,7 @@ public class Utility { } try { - long length = Long.parseLong(connection.getHeaderField("Content-Length")); - if (length >= 0) return length; + return Long.parseLong(connection.getHeaderField("Content-Length")); } catch (Exception err) { // nothing to do } diff --git a/app/src/main/res/drawable-hdpi/ic_pause_black_24dp.png b/app/src/main/res/drawable-hdpi/ic_pause_black_24dp.png new file mode 100644 index 000000000..3770b9124 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_pause_black_24dp.png differ diff --git a/app/src/main/res/drawable-hdpi/ic_pause_white_24dp.png b/app/src/main/res/drawable-hdpi/ic_pause_white_24dp.png new file mode 100644 index 000000000..3e3de2dce Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_pause_white_24dp.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_pause_black_24dp.png b/app/src/main/res/drawable-mdpi/ic_pause_black_24dp.png new file mode 100644 index 000000000..6e81d3ad4 Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_pause_black_24dp.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_pause_white_24dp.png b/app/src/main/res/drawable-mdpi/ic_pause_white_24dp.png new file mode 100644 index 000000000..f5236e8aa Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_pause_white_24dp.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_pause_black_24dp.png b/app/src/main/res/drawable-xhdpi/ic_pause_black_24dp.png new file mode 100644 index 000000000..e6de3973a Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_pause_black_24dp.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_pause_white_24dp.png b/app/src/main/res/drawable-xhdpi/ic_pause_white_24dp.png new file mode 100644 index 000000000..b94b2ae40 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_pause_white_24dp.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_pause_black_24dp.png b/app/src/main/res/drawable-xxhdpi/ic_pause_black_24dp.png new file mode 100644 index 000000000..2691adeb3 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_pause_black_24dp.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_pause_white_24dp.png b/app/src/main/res/drawable-xxhdpi/ic_pause_white_24dp.png new file mode 100644 index 000000000..15cb0b51c Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_pause_white_24dp.png differ diff --git a/app/src/main/res/drawable-xxxhdpi/ic_pause_black_24dp.png b/app/src/main/res/drawable-xxxhdpi/ic_pause_black_24dp.png new file mode 100644 index 000000000..3fbcd0326 Binary files /dev/null and b/app/src/main/res/drawable-xxxhdpi/ic_pause_black_24dp.png differ diff --git a/app/src/main/res/drawable-xxxhdpi/ic_pause_white_24dp.png b/app/src/main/res/drawable-xxxhdpi/ic_pause_white_24dp.png new file mode 100644 index 000000000..12a49bc12 Binary files /dev/null and b/app/src/main/res/drawable-xxxhdpi/ic_pause_white_24dp.png differ diff --git a/app/src/main/res/drawable/progress_soundcloud_horizontal_dark.xml b/app/src/main/res/drawable/progress_soundcloud_horizontal_dark.xml new file mode 100644 index 000000000..1ec9f67b6 --- /dev/null +++ b/app/src/main/res/drawable/progress_soundcloud_horizontal_dark.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/progress_soundcloud_horizontal_light.xml b/app/src/main/res/drawable/progress_soundcloud_horizontal_light.xml new file mode 100644 index 000000000..c326c5c04 --- /dev/null +++ b/app/src/main/res/drawable/progress_soundcloud_horizontal_light.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/progress_youtube_horizontal_dark.xml b/app/src/main/res/drawable/progress_youtube_horizontal_dark.xml new file mode 100644 index 000000000..404410f98 --- /dev/null +++ b/app/src/main/res/drawable/progress_youtube_horizontal_dark.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/progress_youtube_horizontal_light.xml b/app/src/main/res/drawable/progress_youtube_horizontal_light.xml new file mode 100644 index 000000000..120a6e5fb --- /dev/null +++ b/app/src/main/res/drawable/progress_youtube_horizontal_light.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout-land/activity_player_queue_control.xml b/app/src/main/res/layout-land/activity_player_queue_control.xml index 72f673ffc..7467a79cf 100644 --- a/app/src/main/res/layout-land/activity_player_queue_control.xml +++ b/app/src/main/res/layout-land/activity_player_queue_control.xml @@ -8,14 +8,14 @@ android:fitsSystemWindows="true" tools:context="org.schabi.newpipe.player.BackgroundPlayerActivity"> - - - + - - - + + - +