Merge pull request #2486 from kapodamy/32k-issue-fix

Fix slow download speed
This commit is contained in:
Tobias Groza 2019-08-02 22:23:34 +02:00 committed by GitHub
commit 21a90bb7ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 52 additions and 98 deletions

View File

@ -60,7 +60,6 @@ import org.schabi.newpipe.report.ErrorActivity;
import org.schabi.newpipe.util.Constants;
import org.schabi.newpipe.util.KioskTranslator;
import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.PermissionHelper;
import org.schabi.newpipe.util.ServiceHelper;
import org.schabi.newpipe.util.StateSaver;
import org.schabi.newpipe.util.ThemeHelper;
@ -422,17 +421,6 @@ public class MainActivity extends AppCompatActivity {
return;
}
}
switch (requestCode) {
case PermissionHelper.DOWNLOADS_REQUEST_CODE:
NavigationHelper.openDownloads(this);
break;
case PermissionHelper.DOWNLOAD_DIALOG_REQUEST_CODE:
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_holder);
if (fragment instanceof VideoDetailFragment) {
((VideoDetailFragment) fragment).openDownloadDialog();
}
break;
}
}
/**

View File

@ -382,10 +382,8 @@ public class RouterActivity extends AppCompatActivity {
}
if (selectedChoiceKey.equals(getString(R.string.download_key))) {
if (PermissionHelper.checkStoragePermissions(this, PermissionHelper.DOWNLOAD_DIALOG_REQUEST_CODE)) {
selectionIsDownload = true;
openDownloadDialog();
}
selectionIsDownload = true;
openDownloadDialog();
return;
}
@ -453,9 +451,6 @@ public class RouterActivity extends AppCompatActivity {
return;
}
}
if (requestCode == PermissionHelper.DOWNLOAD_DIALOG_REQUEST_CODE) {
openDownloadDialog();
}
}
private static class AdapterChoiceItem {

View File

@ -47,7 +47,6 @@ import org.schabi.newpipe.report.ErrorActivity;
import org.schabi.newpipe.report.UserAction;
import org.schabi.newpipe.util.FilenameUtils;
import org.schabi.newpipe.util.ListHelper;
import org.schabi.newpipe.util.PermissionHelper;
import org.schabi.newpipe.util.SecondaryStreamHelper;
import org.schabi.newpipe.util.StreamItemAdapter;
import org.schabi.newpipe.util.StreamItemAdapter.StreamSizeWrapper;
@ -173,10 +172,6 @@ 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;
}
context = getContext();
@ -217,32 +212,6 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck
okButton.setEnabled(true);
context.unbindService(this);
// check of download paths are defined
if (!askForSavePath) {
String msg = "";
if (mainStorageVideo == null) msg += getString(R.string.download_path_title);
if (mainStorageAudio == null)
msg += getString(R.string.download_path_audio_title);
if (!msg.isEmpty()) {
String title;
if (mainStorageVideo == null && mainStorageAudio == null) {
title = getString(R.string.general_error);
msg = getString(R.string.no_available_dir) + ":\n" + msg;
} else {
title = msg;
msg = getString(R.string.no_available_dir);
}
new AlertDialog.Builder(context)
.setPositiveButton(android.R.string.ok, null)
.setTitle(title)
.setMessage(msg)
.create()
.show();
}
}
}
@Override
@ -602,6 +571,9 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck
// * save path not defined (via download settings)
// * the user as checked the "ask where to download" option
if (!askForSavePath)
Toast.makeText(context, getString(R.string.no_available_dir), Toast.LENGTH_LONG).show();
StoredFileHelper.requestSafWithFileCreation(this, REQUEST_DOWNLOAD_PATH_SAF, filename, mime);
return;
}
@ -773,7 +745,6 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck
// more download logic: select muxer, subtitle converter, etc.
switch (radioStreamsGroup.getCheckedRadioButtonId()) {
case R.id.audio_button:
threads = 1;// use unique thread for subtitles due small file size
kind = 'a';
selectedStream = audioStreamsAdapter.getItem(selectedAudioIndex);
@ -808,6 +779,7 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck
}
break;
case R.id.subtitle_button:
threads = 1;// use unique thread for subtitles due small file size
kind = 's';
selectedStream = subtitleStreamsAdapter.getItem(selectedSubtitleIndex);

View File

@ -385,10 +385,7 @@ public class VideoDetailFragment
}
break;
case R.id.detail_controls_download:
if (PermissionHelper.checkStoragePermissions(activity,
PermissionHelper.DOWNLOAD_DIALOG_REQUEST_CODE)) {
this.openDownloadDialog();
}
this.openDownloadDialog();
break;
case R.id.detail_uploader_root_layout:
if (TextUtils.isEmpty(currentInfo.getUploaderUrl())) {

View File

@ -12,7 +12,6 @@ import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.preference.Preference;
import android.util.Log;
import android.widget.Toast;
import com.nononsenseapps.filepicker.Utils;
@ -64,7 +63,6 @@ public class DownloadSettingsFragment extends BasePreferenceFragment {
}
if (hasInvalidPath(DOWNLOAD_PATH_VIDEO_PREFERENCE) || hasInvalidPath(DOWNLOAD_PATH_AUDIO_PREFERENCE)) {
Toast.makeText(ctx, R.string.download_pick_path, Toast.LENGTH_SHORT).show();
updatePreferencesSummary();
}

View File

@ -22,6 +22,7 @@ package org.schabi.newpipe.settings;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
@ -66,8 +67,10 @@ public class NewPipeSettings {
PreferenceManager.setDefaultValues(context, R.xml.video_audio_settings, true);
PreferenceManager.setDefaultValues(context, R.xml.debug_settings, true);
getVideoDownloadFolder(context);
getAudioDownloadFolder(context);
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
getVideoDownloadFolder(context);
getAudioDownloadFolder(context);
}
}
private static void getVideoDownloadFolder(Context context) {

View File

@ -446,9 +446,6 @@ public class NavigationHelper {
}
public static boolean openDownloads(Activity activity) {
if (!PermissionHelper.checkStoragePermissions(activity, PermissionHelper.DOWNLOADS_REQUEST_CODE)) {
return false;
}
Intent intent = new Intent(activity, DownloadActivity.class);
activity.startActivity(intent);
return true;

View File

@ -18,9 +18,6 @@ import android.widget.Toast;
import org.schabi.newpipe.R;
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) {

View File

@ -28,11 +28,21 @@ public class DownloadInitializer extends Thread {
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) {
@ -43,11 +53,16 @@ public class DownloadInitializer extends Thread {
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) mMission.length = length;
if (i == 0) {
httpCode = mConn.getResponseCode();
mMission.length = length;
}
if (length > 0) finalLength += length;
if (length < lowestSize) lowestSize = length;
}
@ -68,13 +83,15 @@ public class DownloadInitializer extends Thread {
// 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 || mConn.getResponseCode() == 204) {
if (mMission.length == 0 || httpCode == 204) {
mMission.notifyError(DownloadMission.ERROR_HTTP_NO_CONTENT, null);
return;
}
@ -92,6 +109,7 @@ public class DownloadInitializer extends Thread {
// Open again
mConn = mMission.openConnection(mId, mMission.length - 10, mMission.length);
mMission.establishConnection(mId, mConn);
safeClose(mConn);
if (!mMission.running || Thread.interrupted()) return;

View File

@ -49,7 +49,6 @@ public class DownloadRunnable extends Thread {
}
SharpStream f;
InputStream is = null;
try {
f = mMission.storage.getStream();
@ -114,16 +113,16 @@ public class DownloadRunnable extends Thread {
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;
block.done += len;
mMission.notifyProgress(len);
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) {
@ -143,12 +142,6 @@ public class DownloadRunnable extends Thread {
}
}
try {
if (is != null) is.close();
} catch (Exception err) {
// nothing to do
}
try {
f.close();
} catch (Exception err) {

View File

@ -94,7 +94,7 @@ 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();

View File

@ -1,7 +1,5 @@
package us.shandian.giga.service;
import android.Manifest;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
@ -30,7 +28,6 @@ import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.support.v4.content.PermissionChecker;
import android.util.Log;
import android.util.SparseArray;
import android.widget.Toast;
@ -257,18 +254,20 @@ public class DownloadManagerService extends Service {
@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);
// if (permissionCheck == PermissionChecker.PERMISSION_DENIED) {
// Toast.makeText(this, "Permission denied (read)", Toast.LENGTH_SHORT).show();
// }
// }
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
permissionCheck = PermissionChecker.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
if (permissionCheck == PermissionChecker.PERMISSION_DENIED) {
Toast.makeText(this, "Permission denied (read)", Toast.LENGTH_SHORT).show();
}
}
permissionCheck = PermissionChecker.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck == PermissionChecker.PERMISSION_DENIED) {
Toast.makeText(this, "Permission denied (write)", Toast.LENGTH_SHORT).show();
}
*/
return mBinder;
}

View File

@ -103,7 +103,7 @@
<string name="msg_running_detail">Toque para ver detalles</string>
<string name="msg_wait">Por favor espere…</string>
<string name="msg_copied">Copiado al portapapeles</string>
<string name="no_available_dir">Por favor, seleccione un directorio de descarga disponible</string>
<string name="no_available_dir">Por favor, defina un directorio de descarga mas tarde en ajustes</string>
<string name="could_not_load_image">No se pudo cargar la imagen</string>
<string name="app_ui_crash">La interfaz de la app dejó de funcionar</string>
<string name="info_labels">Lo sucedido:\\nPetición:\\nIdioma del contenido:\\nServicio:\\nHora GMT:\\nPaquete:\\nVersión:\\nVersión del SO:</string>
@ -458,7 +458,6 @@ abrir en modo popup</string>
<string name="error_progress_lost">Se perdió el progreso porque el archivo fue eliminado</string>
<string name="error_timeout">Tiempo de espera excedido</string>
<string name="download_pick_path">Seleccione los directorios de descarga</string>
<string name="downloads_storage_ask_title">Preguntar dónde descargar</string>
<string name="downloads_storage_ask_summary">Se preguntará dónde guardar cada descarga</string>
<string name="downloads_storage_ask_summary_kitkat">Se preguntará dónde guardar cada descarga.\nHabilita esta opción si quieres descargar en la tarjeta SD externa</string>

View File

@ -294,7 +294,7 @@
<string name="msg_running_detail">Tap for details</string>
<string name="msg_wait">Please wait…</string>
<string name="msg_copied">Copied to clipboard</string>
<string name="no_available_dir">Please select an available download folder</string>
<string name="no_available_dir">Please define an download folder later in settings</string>
<string name="msg_popup_permission">This permission is needed to\nopen in popup mode</string>
<string name="one_item_deleted">1 item deleted.</string>
<!-- Checksum types -->
@ -553,8 +553,6 @@
<string name="start_downloads">Start downloads</string>
<string name="pause_downloads">Pause downloads</string>
<string name="download_pick_path">Select the downloads save path</string>
<string name="downloads_storage_ask_title">Ask where to download</string>
<string name="downloads_storage_ask_summary">You will be asked where to save each download</string>
<string name="downloads_storage_ask_summary_kitkat">You will be asked where to save each download.\nEnable this option if you want download to the external SD Card</string>