2015-09-21 21:12:48 +02:00
|
|
|
package org.schabi.newpipe;
|
|
|
|
|
2016-01-07 13:28:17 +01:00
|
|
|
import android.Manifest;
|
|
|
|
import android.app.Activity;
|
2015-09-21 21:12:48 +02:00
|
|
|
import android.app.Dialog;
|
|
|
|
import android.app.DownloadManager;
|
2016-01-07 14:22:55 +01:00
|
|
|
import android.app.Notification;
|
2015-09-21 21:12:48 +02:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.SharedPreferences;
|
2016-01-07 13:28:17 +01:00
|
|
|
import android.content.pm.PackageManager;
|
2015-09-21 21:12:48 +02:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
2015-12-23 17:52:01 +01:00
|
|
|
import android.os.Environment;
|
2015-09-21 21:12:48 +02:00
|
|
|
import android.preference.PreferenceManager;
|
2015-11-29 13:06:27 +01:00
|
|
|
import android.support.annotation.NonNull;
|
2016-01-07 13:28:17 +01:00
|
|
|
import android.support.v4.app.ActivityCompat;
|
2015-09-21 21:12:48 +02:00
|
|
|
import android.support.v4.app.DialogFragment;
|
2016-01-07 13:28:17 +01:00
|
|
|
import android.support.v4.content.ContextCompat;
|
2015-09-21 21:12:48 +02:00
|
|
|
import android.support.v7.app.AlertDialog;
|
|
|
|
import android.util.Log;
|
2016-01-07 14:22:55 +01:00
|
|
|
import android.widget.Toast;
|
2015-09-21 21:12:48 +02:00
|
|
|
|
|
|
|
import java.io.File;
|
2016-01-07 14:22:55 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2015-09-21 21:12:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Christian Schabesberger on 21.09.15.
|
|
|
|
*
|
|
|
|
* Copyright (C) Christian Schabesberger 2015 <chris.schabesberger@mailbox.org>
|
|
|
|
* DownloadDialog.java is part of NewPipe.
|
|
|
|
*
|
|
|
|
* NewPipe is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* NewPipe is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class DownloadDialog extends DialogFragment {
|
|
|
|
private static final String TAG = DialogFragment.class.getName();
|
|
|
|
|
|
|
|
public static final String TITLE = "name";
|
|
|
|
public static final String FILE_SUFFIX_AUDIO = "file_suffix_audio";
|
|
|
|
public static final String FILE_SUFFIX_VIDEO = "file_suffix_video";
|
|
|
|
public static final String AUDIO_URL = "audio_url";
|
|
|
|
public static final String VIDEO_URL = "video_url";
|
2015-11-29 13:06:27 +01:00
|
|
|
private Bundle arguments;
|
2015-09-21 21:12:48 +02:00
|
|
|
|
2015-11-29 13:06:27 +01:00
|
|
|
@NonNull
|
2015-09-21 21:12:48 +02:00
|
|
|
@Override
|
|
|
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
|
|
|
arguments = getArguments();
|
|
|
|
super.onCreateDialog(savedInstanceState);
|
2016-01-07 13:28:17 +01:00
|
|
|
if(ContextCompat.checkSelfPermission(this.getContext(),Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED)
|
|
|
|
ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},0);
|
2015-09-21 21:12:48 +02:00
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
2016-01-05 22:56:40 +03:00
|
|
|
builder.setTitle(R.string.download_dialog_title)
|
|
|
|
.setItems(R.array.download_options, new DialogInterface.OnClickListener() {
|
2015-09-21 21:12:48 +02:00
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
Context context = getActivity();
|
2016-01-01 22:09:36 +01:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2015-09-21 21:12:48 +02:00
|
|
|
String suffix = "";
|
|
|
|
String title = arguments.getString(TITLE);
|
|
|
|
String url = "";
|
2016-01-07 14:22:55 +01:00
|
|
|
File downloadDir = NewPipeSettings.getDownloadFolder();
|
2015-09-21 21:12:48 +02:00
|
|
|
switch(which) {
|
|
|
|
case 0: // Video
|
|
|
|
suffix = arguments.getString(FILE_SUFFIX_VIDEO);
|
|
|
|
url = arguments.getString(VIDEO_URL);
|
2016-01-07 14:22:55 +01:00
|
|
|
downloadDir = NewPipeSettings.getVideoDownloadFolder(context);
|
2015-09-21 21:12:48 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
suffix = arguments.getString(FILE_SUFFIX_AUDIO);
|
|
|
|
url = arguments.getString(AUDIO_URL);
|
2016-01-07 14:22:55 +01:00
|
|
|
downloadDir = NewPipeSettings.getAudioDownloadFolder(context);
|
2015-09-21 21:12:48 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Log.d(TAG, "lolz");
|
|
|
|
}
|
2016-01-07 14:22:55 +01:00
|
|
|
if(!downloadDir.exists()) {
|
2016-01-05 21:11:15 +01:00
|
|
|
//attempt to create directory
|
2016-01-07 14:22:55 +01:00
|
|
|
boolean mkdir = downloadDir.mkdirs();
|
|
|
|
if(!mkdir && !downloadDir.isDirectory()) {
|
|
|
|
String message = context.getString(R.string.err_dir_create,downloadDir.toString());
|
|
|
|
Log.e(TAG, message);
|
|
|
|
Toast.makeText(context,message , Toast.LENGTH_LONG).show();
|
|
|
|
|
|
|
|
return;
|
2015-12-23 17:52:01 +01:00
|
|
|
}
|
2016-01-07 14:22:55 +01:00
|
|
|
String message = context.getString(R.string.info_dir_created,downloadDir.toString());
|
|
|
|
Log.e(TAG, message);
|
|
|
|
Toast.makeText(context,message , Toast.LENGTH_LONG).show();
|
2015-12-23 17:52:01 +01:00
|
|
|
}
|
2016-01-05 21:11:15 +01:00
|
|
|
|
2016-01-07 14:22:55 +01:00
|
|
|
File saveFilePath = new File(downloadDir,createFileName(title) + suffix);
|
|
|
|
|
|
|
|
long id = 0;
|
2016-01-02 22:47:21 +01:00
|
|
|
if (App.isUsingTor()) {
|
|
|
|
// if using Tor, do not use DownloadManager because the proxy cannot be set
|
2016-01-07 14:22:55 +01:00
|
|
|
Downloader.downloadFile(getContext(), url, saveFilePath, title);
|
2016-01-02 22:47:21 +01:00
|
|
|
} else {
|
|
|
|
DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
|
|
|
DownloadManager.Request request = new DownloadManager.Request(
|
|
|
|
Uri.parse(url));
|
2016-01-07 14:22:55 +01:00
|
|
|
request.setDestinationUri(Uri.fromFile(saveFilePath));
|
2016-01-02 22:47:21 +01:00
|
|
|
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
|
2016-01-07 14:22:55 +01:00
|
|
|
|
|
|
|
request.setTitle(title);
|
|
|
|
request.setDescription("'" + url +
|
|
|
|
"' => '" + saveFilePath + "'");
|
|
|
|
request.allowScanningByMediaScanner();
|
|
|
|
|
2016-01-02 22:47:21 +01:00
|
|
|
try {
|
2016-01-07 14:22:55 +01:00
|
|
|
id = dm.enqueue(request);
|
2016-01-02 22:47:21 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2015-09-21 21:12:48 +02:00
|
|
|
}
|
2016-01-07 14:22:55 +01:00
|
|
|
|
|
|
|
Log.i(TAG,"Started downloading '" + url +
|
|
|
|
"' => '" + saveFilePath + "' #" + id);
|
2015-09-21 21:12:48 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return builder.create();
|
|
|
|
}
|
|
|
|
|
2016-01-07 14:22:55 +01:00
|
|
|
/**
|
|
|
|
* #143 #44 #42 #22: make shure that the filename does not contain illegal chars.
|
|
|
|
* This should fix some of the "cannot download" problems.
|
|
|
|
* */
|
|
|
|
private String createFileName(String fName) {
|
|
|
|
// from http://eng-przemelek.blogspot.de/2009/07/how-to-create-valid-file-name.html
|
|
|
|
|
|
|
|
List<String> forbiddenCharsPatterns = new ArrayList<String> ();
|
|
|
|
forbiddenCharsPatterns.add("[:]+"); // Mac OS, but it looks that also Windows XP
|
|
|
|
forbiddenCharsPatterns.add("[\\*\"/\\\\\\[\\]\\:\\;\\|\\=\\,]+"); // Windows
|
|
|
|
forbiddenCharsPatterns.add("[^\\w\\d\\.]+"); // last chance... only latin letters and digits
|
|
|
|
String nameToTest = fName;
|
|
|
|
for (String pattern : forbiddenCharsPatterns) {
|
|
|
|
nameToTest = nameToTest.replaceAll(pattern, "_");
|
|
|
|
}
|
|
|
|
return nameToTest;
|
|
|
|
}
|
2015-09-21 21:12:48 +02:00
|
|
|
}
|