Skip to content

Commit

Permalink
add folder selector; use folder to download and move finally to the c…
Browse files Browse the repository at this point in the history
…orrect place; fix back on bottomSheet
  • Loading branch information
tastelessjolt committed Oct 9, 2018
1 parent bcb1a1e commit 74513d1
Show file tree
Hide file tree
Showing 13 changed files with 402 additions and 30 deletions.
6 changes: 0 additions & 6 deletions .settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
offline.mode=false
override.workspace.settings=true
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ dependencies {

implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'

implementation 'com.github.codekidX:storage-chooser:2.0.4.2'
}
repositories {
mavenCentral()
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS" />

<application
android:allowBackup="true"
Expand Down Expand Up @@ -66,6 +67,12 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".Activities.MainActivity"/>
</activity>
<receiver android:name=".Utils.DownloadReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
</intent-filter>
</receiver>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ protected void onPause() {
editor.apply();
}

@Override
public void onBackPressed() {
if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_HIDDEN) {
super.onBackPressed();
}
else {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
Expand All @@ -228,7 +238,6 @@ public boolean onCreateOptionsMenu(Menu menu) {
}

public void openSettingPage() {
Toast.makeText(getApplicationContext(), "Settings page coming soon!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void onClick(View view) {
assert clipboard != null;
ClipData clip = ClipData.newRawUri("DownloadURL", Uri.parse(finalurl));
clipboard.setPrimaryClip(clip);
Toast.makeText(context, String.format("Your video \"%s\" is now downloading. Check the notification area.", formats.get(getLayoutPosition()).title), Toast.LENGTH_SHORT).show();
// Toast.makeText(context, String.format("Your video \"%s\" is now downloading. Check the notification area.", formats.get(getLayoutPosition()).title), Toast.LENGTH_SHORT).show();
// Toast.makeText(context, String.format("(%s) Quality link copied to Clipboard", ((TextView) view.findViewById(R.id.format_quality)).getText().toString()), Toast.LENGTH_SHORT).show();

formats.get(getLayoutPosition()).download(context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
package me.harshithgoka.youtubedl.Fragments;


import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.codekidlabs.storagechooser.StorageChooser;
import com.codekidlabs.storagechooser.utils.DiskUtil;

import java.util.Map;

import androidx.core.content.res.ResourcesCompat;
import androidx.preference.EditTextPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import me.harshithgoka.youtubedl.Activities.MainActivity;
import me.harshithgoka.youtubedl.Activities.SettingsActivity;
import me.harshithgoka.youtubedl.R;
import me.harshithgoka.youtubedl.Utils.StringPreference;

/**
* A simple {@link Fragment} subclass.
*/
public class SettingsFragment extends PreferenceFragmentCompat {
public class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceClickListener {


private SharedPreferences sharedPreferences;
private StringPreference downloadFolderPref;

public SettingsFragment() {
// Required empty public constructor
Expand All @@ -26,4 +44,84 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}

@Override
public void onResume() {
super.onResume();

sharedPreferences = getPreferenceManager().getSharedPreferences();

// we want to watch the preference values' changes
sharedPreferences.registerOnSharedPreferenceChangeListener(this);

Preference.OnPreferenceClickListener notImplemented = new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Toast.makeText(getContext(), "Auto download coming soon...", Toast.LENGTH_SHORT).show();
return true;
}
};

downloadFolderPref = (StringPreference) findPreference("download_folder");
findPreference("default_format").setOnPreferenceClickListener(notImplemented);
findPreference("autoDownload").setOnPreferenceClickListener(notImplemented);

// iterate through the preference entries and update their summary if they are an instance of EditTextPreference
updateSummary(downloadFolderPref);

downloadFolderPref.setOnPreferenceClickListener(this);
}

private void updateSummary(StringPreference preferenceEntry) {
preferenceEntry.setSummary(preferenceEntry.getText());
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Map<String, ?> preferencesMap = sharedPreferences.getAll();

// get the preference that has been changed
Object changedPreference = preferencesMap.get(key);
// and if it's an instance of EditTextPreference class, update its summary
if (preferencesMap.get(key) instanceof EditTextPreference) {
if (changedPreference != null) {
updateSummary((StringPreference) changedPreference);
}
}
}

@Override
public boolean onPreferenceClick(Preference preference) {
if (preference.getKey().equals(downloadFolderPref.getKey())) {
StorageChooser.Theme theme = new StorageChooser.Theme(getContext());

int[] myScheme = theme.getDefaultScheme();
myScheme[StorageChooser.Theme.OVERVIEW_HEADER_INDEX] = ResourcesCompat.getColor(getResources(), R.color.colorAccent, getContext().getTheme());
myScheme[StorageChooser.Theme.SEC_FOLDER_TINT_INDEX] = ResourcesCompat.getColor(getResources(), R.color.colorPrimary, getContext().getTheme());
myScheme[StorageChooser.Theme.SEC_ADDRESS_BAR_BG] = ResourcesCompat.getColor(getResources(), R.color.colorAccent, getContext().getTheme());
theme.setScheme(myScheme);

StorageChooser chooser = new StorageChooser.Builder()
.allowCustomPath(true)
.setType(StorageChooser.DIRECTORY_CHOOSER)
.withActivity(getActivity())
.withFragmentManager(getActivity().getFragmentManager())
.withMemoryBar(true)
.setTheme(theme)
.build();

chooser.show();

// get path that the user has chosen
chooser.setOnSelectListener(new StorageChooser.OnSelectListener() {
@Override
public void onSelect(String path) {
Log.d("SELECTED_PATH", path);
downloadFolderPref.setText(path);
downloadFolderPref.setSummary(path);
}
});
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package me.harshithgoka.youtubedl.Utils;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.net.URI;
import java.nio.file.Files;
import java.util.HashSet;
import java.util.Set;

public class DownloadReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
long receivedID = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, -1L);
DownloadManager mgr = (DownloadManager)
context.getSystemService(Context.DOWNLOAD_SERVICE);

DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(receivedID);
Cursor cur = mgr.query(query);
int status_index = cur.getColumnIndex(DownloadManager.COLUMN_STATUS);
int id_index = cur.getColumnIndex(DownloadManager.COLUMN_ID);
int uri_index = cur.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
int dest_index = cur.getColumnIndex(DownloadManager.COLUMN_DESCRIPTION);

SharedPreferences sharedPreferences = context.getSharedPreferences("download_history", Context.MODE_PRIVATE);
Set<String> inProgressDownloads = sharedPreferences.getStringSet("in_progress", new HashSet<String>());



if(cur.moveToFirst()) {
if(cur.getInt(status_index) == DownloadManager.STATUS_SUCCESSFUL){
long id = cur.getLong(id_index);
if(inProgressDownloads.remove("" + id)) {
URI uri = URI.create(cur.getString(uri_index));
File file = new File(uri);
Log.d("DownloadReceiver", file.getAbsolutePath());
File destFile = new File(cur.getString(dest_index));
if(file.renameTo(destFile)){
Log.d("DownloadReceiver", "Move to final dest successful");
Toast.makeText(context, String.format("YoutubeDL download complete to folder \"%s\"", destFile.getParentFile().getAbsolutePath()), Toast.LENGTH_SHORT).show();
}
}
}
}
cur.close();

SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putStringSet("in_progress", inProgressDownloads);
editor.apply();
}
}

Loading

0 comments on commit 74513d1

Please sign in to comment.