Added new download dialog
This commit is contained in:
parent
c622923edd
commit
3beafa2a74
|
@ -579,8 +579,7 @@ public class VideoItemDetailFragment extends Fragment {
|
||||||
}
|
}
|
||||||
|
|
||||||
args.putString(DownloadDialog.TITLE, info.title);
|
args.putString(DownloadDialog.TITLE, info.title);
|
||||||
DownloadDialog downloadDialog = new DownloadDialog();
|
DownloadDialog downloadDialog = DownloadDialog.newInstance(args);
|
||||||
downloadDialog.setArguments(args);
|
|
||||||
downloadDialog.show(activity.getSupportFragmentManager(), "downloadDialog");
|
downloadDialog.show(activity.getSupportFragmentManager(), "downloadDialog");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Toast.makeText(VideoItemDetailFragment.this.getActivity(),
|
Toast.makeText(VideoItemDetailFragment.this.getActivity(),
|
||||||
|
|
|
@ -1,19 +1,28 @@
|
||||||
package org.schabi.newpipe.download;
|
package org.schabi.newpipe.download;
|
||||||
|
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.app.Dialog;
|
import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.ServiceConnection;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.annotation.NonNull;
|
import android.os.IBinder;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
import android.support.v4.app.ActivityCompat;
|
import android.support.v4.app.ActivityCompat;
|
||||||
import android.support.v4.app.DialogFragment;
|
import android.support.v4.app.DialogFragment;
|
||||||
import android.support.v4.content.ContextCompat;
|
import android.support.v4.content.ContextCompat;
|
||||||
import android.support.v7.app.AlertDialog;
|
import android.support.v7.widget.Toolbar;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.MenuItem;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.CheckBox;
|
||||||
|
import android.widget.EditText;
|
||||||
|
import android.widget.SeekBar;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
import org.schabi.newpipe.App;
|
import org.schabi.newpipe.App;
|
||||||
import org.schabi.newpipe.NewPipeSettings;
|
import org.schabi.newpipe.NewPipeSettings;
|
||||||
|
@ -23,6 +32,10 @@ import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import us.shandian.giga.get.DownloadManager;
|
||||||
|
import us.shandian.giga.service.DownloadManagerService;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Christian Schabesberger on 21.09.15.
|
* Created by Christian Schabesberger on 21.09.15.
|
||||||
*
|
*
|
||||||
|
@ -51,83 +64,126 @@ public class DownloadDialog extends DialogFragment {
|
||||||
public static final String FILE_SUFFIX_VIDEO = "file_suffix_video";
|
public static final String FILE_SUFFIX_VIDEO = "file_suffix_video";
|
||||||
public static final String AUDIO_URL = "audio_url";
|
public static final String AUDIO_URL = "audio_url";
|
||||||
public static final String VIDEO_URL = "video_url";
|
public static final String VIDEO_URL = "video_url";
|
||||||
private Bundle arguments;
|
|
||||||
|
|
||||||
@NonNull
|
private DownloadManager mManager;
|
||||||
|
private DownloadManagerService.DMBinder mBinder;
|
||||||
|
|
||||||
|
private ServiceConnection mConnection = new ServiceConnection() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
public void onServiceConnected(ComponentName p1, IBinder binder) {
|
||||||
arguments = getArguments();
|
mBinder = (DownloadManagerService.DMBinder) binder;
|
||||||
super.onCreateDialog(savedInstanceState);
|
mManager = mBinder.getDownloadManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onServiceDisconnected(ComponentName p1) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
public DownloadDialog() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DownloadDialog newInstance(Bundle args)
|
||||||
|
{
|
||||||
|
DownloadDialog dialog = new DownloadDialog();
|
||||||
|
dialog.setArguments(args);
|
||||||
|
dialog.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
|
||||||
|
return dialog;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||||
|
Bundle savedInstanceState) {
|
||||||
|
|
||||||
if(ContextCompat.checkSelfPermission(this.getContext(),Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED)
|
if(ContextCompat.checkSelfPermission(this.getContext(),Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED)
|
||||||
ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},0);
|
ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},0);
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
|
||||||
builder.setTitle(R.string.download_dialog_title);
|
|
||||||
|
|
||||||
// If no audio stream available
|
Intent i = new Intent();
|
||||||
if(arguments.getString(AUDIO_URL) == null) {
|
i.setClass(getContext(), DownloadManagerService.class);
|
||||||
builder.setItems(R.array.download_options_no_audio, new DialogInterface.OnClickListener() {
|
getContext().startService(i);
|
||||||
@Override
|
getContext().bindService(i, mConnection, Context.BIND_AUTO_CREATE);
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
|
||||||
Context context = getActivity();
|
|
||||||
String title = arguments.getString(TITLE);
|
return inflater.inflate(R.layout.dialog_url, container);
|
||||||
switch (which) {
|
|
||||||
case 0: // Video
|
|
||||||
download(arguments.getString(VIDEO_URL),
|
|
||||||
title,
|
|
||||||
arguments.getString(FILE_SUFFIX_VIDEO),
|
|
||||||
NewPipeSettings.getVideoDownloadFolder(context),context);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
Log.d(TAG, "lolz");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
|
||||||
|
super.onViewCreated(view, savedInstanceState);
|
||||||
|
|
||||||
|
Bundle arguments = getArguments();
|
||||||
|
final Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
|
||||||
|
final EditText name = (EditText) view.findViewById(R.id.file_name);
|
||||||
|
final TextView tCount = (TextView) view.findViewById(R.id.threads_count);
|
||||||
|
final SeekBar threads = (SeekBar) view.findViewById(R.id.threads);
|
||||||
|
|
||||||
|
toolbar.setTitle(R.string.download_dialog_title);
|
||||||
|
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
|
||||||
|
toolbar.inflateMenu(R.menu.dialog_url);
|
||||||
|
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
getDialog().dismiss();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// If no video stream available
|
|
||||||
} else if(arguments.getString(VIDEO_URL) == null) {
|
threads.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
|
||||||
builder.setItems(R.array.download_options_no_video, new DialogInterface.OnClickListener() {
|
|
||||||
@Override
|
@Override
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {
|
||||||
Context context = getActivity();
|
tCount.setText(String.valueOf(progress + 1));
|
||||||
String title = arguments.getString(TITLE);
|
|
||||||
switch (which) {
|
|
||||||
case 0: // Audio
|
|
||||||
download(arguments.getString(AUDIO_URL),
|
|
||||||
title,
|
|
||||||
arguments.getString(FILE_SUFFIX_AUDIO),
|
|
||||||
NewPipeSettings.getAudioDownloadFolder(context),context);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
Log.d(TAG, "lolz");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStartTrackingTouch(SeekBar p1) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStopTrackingTouch(SeekBar p1) {
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
//if both streams ar available
|
|
||||||
|
checkDownloadOptions();
|
||||||
|
|
||||||
|
//int def = mPrefs.getInt("threads", 4);
|
||||||
|
int def = 3;
|
||||||
|
threads.setProgress(def - 1);
|
||||||
|
tCount.setText(String.valueOf(def));
|
||||||
|
|
||||||
|
name.setText(createFileName(arguments.getString(TITLE)));
|
||||||
|
|
||||||
|
|
||||||
|
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
|
||||||
|
@Override
|
||||||
|
public boolean onMenuItemClick(MenuItem item) {
|
||||||
|
if (item.getItemId() == R.id.okay) {
|
||||||
|
download();
|
||||||
|
return true;
|
||||||
} else {
|
} else {
|
||||||
builder.setItems(R.array.download_options, new DialogInterface.OnClickListener() {
|
return false;
|
||||||
@Override
|
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
|
||||||
Context context = getActivity();
|
|
||||||
String title = arguments.getString(TITLE);
|
|
||||||
switch (which) {
|
|
||||||
case 0: // Video
|
|
||||||
download(arguments.getString(VIDEO_URL),
|
|
||||||
title,
|
|
||||||
arguments.getString(FILE_SUFFIX_VIDEO),
|
|
||||||
NewPipeSettings.getVideoDownloadFolder(context), context);
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
download(arguments.getString(AUDIO_URL),
|
|
||||||
title,
|
|
||||||
arguments.getString(FILE_SUFFIX_AUDIO),
|
|
||||||
NewPipeSettings.getAudioDownloadFolder(context), context);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
Log.d(TAG, "lolz");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void checkDownloadOptions(){
|
||||||
|
View view = getView();
|
||||||
|
Bundle arguments = getArguments();
|
||||||
|
CheckBox audio = (CheckBox) view.findViewById(R.id.audio);
|
||||||
|
CheckBox video = (CheckBox) view.findViewById(R.id.video);
|
||||||
|
|
||||||
|
if(arguments.getString(AUDIO_URL) == null) {
|
||||||
|
audio.setVisibility(View.GONE);
|
||||||
|
} else if(arguments.getString(VIDEO_URL) == null) {
|
||||||
|
video.setVisibility(View.GONE);
|
||||||
}
|
}
|
||||||
return builder.create();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -148,6 +204,40 @@ public class DownloadDialog extends DialogFragment {
|
||||||
return nameToTest;
|
return nameToTest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//download audio, video or both?
|
||||||
|
private void download()
|
||||||
|
{
|
||||||
|
View view = getView();
|
||||||
|
Bundle arguments = getArguments();
|
||||||
|
final EditText name = (EditText) view.findViewById(R.id.file_name);
|
||||||
|
final SeekBar threads = (SeekBar) view.findViewById(R.id.threads);
|
||||||
|
CheckBox audio = (CheckBox) view.findViewById(R.id.audio);
|
||||||
|
CheckBox video = (CheckBox) view.findViewById(R.id.video);
|
||||||
|
|
||||||
|
String fName = name.getText().toString().trim();
|
||||||
|
|
||||||
|
while (mBinder == null);
|
||||||
|
|
||||||
|
if(audio.isChecked()){
|
||||||
|
int res = mManager.startMission(
|
||||||
|
arguments.getString(AUDIO_URL),
|
||||||
|
fName + arguments.getString(FILE_SUFFIX_AUDIO),
|
||||||
|
threads.getProgress() + 1);
|
||||||
|
mBinder.onMissionAdded(mManager.getMission(res));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(video.isChecked()){
|
||||||
|
int res = mManager.startMission(
|
||||||
|
arguments.getString(VIDEO_URL),
|
||||||
|
fName + arguments.getString(FILE_SUFFIX_VIDEO),
|
||||||
|
threads.getProgress() + 1);
|
||||||
|
mBinder.onMissionAdded(mManager.getMission(res));
|
||||||
|
}
|
||||||
|
getDialog().dismiss();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private void download(String url, String title,
|
private void download(String url, String title,
|
||||||
String fileSuffix, File downloadDir, Context context) {
|
String fileSuffix, File downloadDir, Context context) {
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="match_parent"
|
||||||
android:orientation="vertical">
|
android:orientation="vertical">
|
||||||
|
|
||||||
<android.support.v7.widget.Toolbar
|
<android.support.v7.widget.Toolbar
|
||||||
|
@ -56,24 +56,17 @@
|
||||||
android:layout_marginLeft="24dp"
|
android:layout_marginLeft="24dp"
|
||||||
android:layout_marginRight="24dp"
|
android:layout_marginRight="24dp"
|
||||||
android:layout_marginBottom="24dp"
|
android:layout_marginBottom="24dp"
|
||||||
android:gravity="center_vertical"
|
android:gravity="left"
|
||||||
android:orientation="vertical">
|
android:orientation="horizontal">
|
||||||
<RadioGroup
|
<CheckBox android:id="@+id/video"
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:orientation="horizontal"
|
|
||||||
android:checkedButton="@+id/video">
|
|
||||||
<RadioButton android:id="@+id/video"
|
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="@string/video"
|
android:text="@string/video"
|
||||||
android:onClick="onRadioButtonClicked"/>
|
android:checked="true"/>
|
||||||
<RadioButton android:id="@+id/audio"
|
<CheckBox android:id="@+id/audio"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="@string/audio"
|
android:text="@string/audio" />
|
||||||
android:onClick="onRadioButtonClicked"/>
|
|
||||||
</RadioGroup>
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
|
|
Loading…
Reference in New Issue