Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/src/main/java/org/schabi/newpipe/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
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;
Expand Down Expand Up @@ -421,6 +422,17 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
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;
}
}

/**
Expand Down
36 changes: 23 additions & 13 deletions app/src/main/java/org/schabi/newpipe/RouterActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,13 @@
*/
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;
Expand Down Expand Up @@ -257,7 +260,7 @@ private void showDialog(final List<AdapterChoiceItem> choices) {
.setNegativeButton(R.string.just_once, dialogButtonsClickListener)
.setPositiveButton(R.string.always, dialogButtonsClickListener)
.setOnDismissListener((dialog) -> {
if(!selectionIsDownload) finish();
if (!selectionIsDownload) finish();
})
.create();

Expand Down Expand Up @@ -358,13 +361,13 @@ private void setDialogButtonsState(AlertDialog dialog, boolean state) {
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) {
Expand All @@ -382,8 +385,10 @@ private void handleChoice(final String selectedChoiceKey) {
}

if (selectedChoiceKey.equals(getString(R.string.download_key))) {
selectionIsDownload = true;
openDownloadDialog();
if (PermissionHelper.checkStoragePermissions(this, PermissionHelper.DOWNLOAD_DIALOG_REQUEST_CODE)) {
selectionIsDownload = true;
openDownloadDialog();
}
return;
}

Expand All @@ -395,7 +400,7 @@ private void handleChoice(final String selectedChoiceKey) {
.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);
}
Expand Down Expand Up @@ -445,17 +450,21 @@ private void openDownloadDialog() {

@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;
}
}
if (requestCode == PermissionHelper.DOWNLOAD_DIALOG_REQUEST_CODE) {
openDownloadDialog();
}
}

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;
Expand Down Expand Up @@ -553,7 +562,8 @@ public Consumer<Info> getResultHandler(Choice choice) {

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;
Expand Down
48 changes: 43 additions & 5 deletions app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand All @@ -33,6 +34,8 @@
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;
Expand All @@ -45,13 +48,17 @@
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;
import org.schabi.newpipe.util.SecondaryStreamHelper;
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;
Expand All @@ -72,7 +79,7 @@
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_PATH_SAF = 0x1230;
private static final int REQUEST_DOWNLOAD_SAVE_AS = 0x1230;

@State
protected StreamInfo currentInfo;
Expand Down Expand Up @@ -173,6 +180,11 @@ public void onCreate(@Nullable Bundle 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();

setStyle(STYLE_NO_TITLE, ThemeHelper.getDialogTheme(context));
Expand Down Expand Up @@ -311,12 +323,18 @@ public void onSaveInstanceState(@NonNull Bundle outState) {
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == REQUEST_DOWNLOAD_PATH_SAF && resultCode == Activity.RESULT_OK) {
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);
Expand Down Expand Up @@ -569,12 +587,27 @@ private void prepareSelectedDownload() {
// This part is called if with SAF preferred:
// * older android version running
// * save path not defined (via download settings)
// * the user as checked the "ask where to download" option
// * the user 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);
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;
}

Expand Down Expand Up @@ -624,6 +657,11 @@ private void checkSelectedDownload(StoredDirectoryHelper mainStorage, Uri target
// 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) {
Expand Down Expand Up @@ -728,7 +766,7 @@ private void continueSelectedDownload(@NonNull StoredFileHelper storage) {
try {
if (storage.length() > 0) storage.truncate();
} catch (IOException e) {
Log.e(TAG, "failed to overwrite the file: " + storage.getUri().toString(), e);
Log.e(TAG, "failed to truncate the file: " + storage.getUri().toString(), e);
showFailedDialog(R.string.overwrite_failed);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,10 @@ public void onClick(View v) {
}
break;
case R.id.detail_controls_download:
this.openDownloadDialog();
if (PermissionHelper.checkStoragePermissions(activity,
PermissionHelper.DOWNLOAD_DIALOG_REQUEST_CODE)) {
this.openDownloadDialog();
}
break;
case R.id.detail_uploader_root_layout:
if (TextUtils.isEmpty(currentInfo.getUploaderUrl())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public class DownloadSettingsFragment extends BasePreferenceFragment {
private String DOWNLOAD_PATH_VIDEO_PREFERENCE;
private String DOWNLOAD_PATH_AUDIO_PREFERENCE;

private String DOWNLOAD_STORAGE_ASK;

private Preference prefPathVideo;
private Preference prefPathAudio;
private Preference prefStorageAsk;
Expand All @@ -49,14 +47,14 @@ public void onCreate(@Nullable Bundle savedInstanceState) {

DOWNLOAD_PATH_VIDEO_PREFERENCE = getString(R.string.download_path_video_key);
DOWNLOAD_PATH_AUDIO_PREFERENCE = getString(R.string.download_path_audio_key);
DOWNLOAD_STORAGE_ASK = getString(R.string.downloads_storage_ask);
final String downloadStorageAsk = getString(R.string.downloads_storage_ask);

prefPathVideo = findPreference(DOWNLOAD_PATH_VIDEO_PREFERENCE);
prefPathAudio = findPreference(DOWNLOAD_PATH_AUDIO_PREFERENCE);
prefStorageAsk = findPreference(DOWNLOAD_STORAGE_ASK);
prefStorageAsk = findPreference(downloadStorageAsk);

updatePreferencesSummary();
updatePathPickers(!defaultPreferences.getBoolean(DOWNLOAD_STORAGE_ASK, false));
updatePathPickers(!defaultPreferences.getBoolean(downloadStorageAsk, false));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
prefStorageAsk.setSummary(R.string.downloads_storage_ask_summary);
Expand Down Expand Up @@ -180,7 +178,7 @@ public boolean onPreferenceTreeClick(Preference preference) {
}

Intent i;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
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);
Expand Down Expand Up @@ -221,16 +219,17 @@ else if (requestCode == REQUEST_DOWNLOAD_AUDIO_PATH)
return;
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// steps:
// 1. revoke permissions on the old save path
// 2. acquire permissions on the new save path
// 3. save the new path, if step(2) was successful
final Context ctx = getContext();
if (ctx == null) throw new NullPointerException("getContext()");

forgetSAFTree(ctx, defaultPreferences.getString(key, ""));
// 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);

Expand All @@ -245,7 +244,7 @@ else if (requestCode == REQUEST_DOWNLOAD_AUDIO_PATH)
return;
}
} else {
File target = Utils.getFileForUri(data.getData());
File target = Utils.getFileForUri(uri);
if (!target.canWrite()) {
showMessageDialog(R.string.download_to_sdcard_error_title, R.string.download_to_sdcard_error_message);
return;
Expand Down
17 changes: 11 additions & 6 deletions app/src/main/java/org/schabi/newpipe/settings/NewPipeSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

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;
Expand Down Expand Up @@ -67,10 +66,8 @@ public static void initSettings(Context context) {
PreferenceManager.setDefaultValues(context, R.xml.video_audio_settings, true);
PreferenceManager.setDefaultValues(context, R.xml.debug_settings, true);

if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
getVideoDownloadFolder(context);
getAudioDownloadFolder(context);
}
getVideoDownloadFolder(context);
getAudioDownloadFolder(context);
}

private static void getVideoDownloadFolder(Context context) {
Expand All @@ -93,11 +90,19 @@ private static void getDir(Context context, int keyID, String defaultDirectoryNa
}

@NonNull
private static File getDir(String defaultDirectoryName) {
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);
}

}
Loading