From 181c83090d4bd383405ade52d6927e0994061340 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 26 May 2016 11:59:12 -0300 Subject: [PATCH 01/10] Starting to add a filepicker to the directory preferences --- app/src/main/AndroidManifest.xml | 7 +++++ .../org/schabi/newpipe/SettingsActivity.java | 29 +++++++++++++++++-- app/src/main/res/values/styles.xml | 20 +++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f6e311248..26788cd1b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -137,5 +137,12 @@ + + + diff --git a/app/src/main/java/org/schabi/newpipe/SettingsActivity.java b/app/src/main/java/org/schabi/newpipe/SettingsActivity.java index 316923c25..f93664ee0 100644 --- a/app/src/main/java/org/schabi/newpipe/SettingsActivity.java +++ b/app/src/main/java/org/schabi/newpipe/SettingsActivity.java @@ -1,16 +1,20 @@ package org.schabi.newpipe; import android.app.Activity; +import android.content.ClipData; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.media.AudioManager; +import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.preference.CheckBoxPreference; import android.preference.EditTextPreference; import android.preference.ListPreference; +import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; @@ -23,7 +27,13 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import com.nononsenseapps.filepicker.AbstractFilePickerFragment; +import com.nononsenseapps.filepicker.FilePickerActivity; + +import java.util.ArrayList; + import info.guardianproject.netcipher.proxy.OrbotHelper; +import us.shandian.giga.util.Utility; /** * Created by Christian Schabesberger on 31.08.15. @@ -81,7 +91,7 @@ public class SettingsActivity extends PreferenceActivity { private ListPreference defaultResolutionPreference; private ListPreference defaultAudioFormatPreference; private ListPreference searchLanguagePreference; - private EditTextPreference downloadPathPreference; + private Preference downloadPathPreference; private EditTextPreference downloadPathAudioPreference; private CheckBoxPreference useTorCheckBox; private SharedPreferences defaultPreferences; @@ -111,8 +121,7 @@ public class SettingsActivity extends PreferenceActivity { (ListPreference) findPreference(DEFAULT_AUDIO_FORMAT_PREFERENCE); searchLanguagePreference = (ListPreference) findPreference(SEARCH_LANGUAGE_PREFERENCE); - downloadPathPreference = - (EditTextPreference) findPreference(DOWNLOAD_PATH_PREFERENCE); + downloadPathPreference = (Preference) findPreference(DOWNLOAD_PATH_PREFERENCE); downloadPathAudioPreference = (EditTextPreference) findPreference(DOWNLOAD_PATH_AUDIO_PREFERENCE); useTorCheckBox = (CheckBoxPreference) findPreference(USE_TOR_KEY); @@ -141,6 +150,20 @@ public class SettingsActivity extends PreferenceActivity { }; defaultPreferences.registerOnSharedPreferenceChangeListener(prefListener); + downloadPathPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener(){ + @Override + public boolean onPreferenceClick(Preference preference) { + Intent i = new Intent(getActivity(), FilePickerActivity.class); + i.setAction(Intent.ACTION_GET_CONTENT); + i.putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false); + i.putExtra(FilePickerActivity.EXTRA_ALLOW_CREATE_DIR, true); + i.putExtra(FilePickerActivity.EXTRA_MODE, AbstractFilePickerFragment.MODE_DIR); + startActivityForResult(i, 233); + + return true; + } + }); + updateSummary(); } diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index e7549b7f7..1c8dca48f 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -49,4 +49,24 @@ + + + + + From 1b7cece306137f9452cb4fe8609693b160793cbe Mon Sep 17 00:00:00 2001 From: David Date: Tue, 14 Jun 2016 06:13:06 -0300 Subject: [PATCH 02/10] Trying to make last filepicker version work, maybe I'm having some memory problems --- .../org/schabi/newpipe/SettingsActivity.java | 45 +++++++++++++++++-- app/src/main/res/xml/settings.xml | 2 +- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/SettingsActivity.java b/app/src/main/java/org/schabi/newpipe/SettingsActivity.java index f93664ee0..58caea7fe 100644 --- a/app/src/main/java/org/schabi/newpipe/SettingsActivity.java +++ b/app/src/main/java/org/schabi/newpipe/SettingsActivity.java @@ -121,7 +121,7 @@ public class SettingsActivity extends PreferenceActivity { (ListPreference) findPreference(DEFAULT_AUDIO_FORMAT_PREFERENCE); searchLanguagePreference = (ListPreference) findPreference(SEARCH_LANGUAGE_PREFERENCE); - downloadPathPreference = (Preference) findPreference(DOWNLOAD_PATH_PREFERENCE); + downloadPathPreference = findPreference(DOWNLOAD_PATH_PREFERENCE); downloadPathAudioPreference = (EditTextPreference) findPreference(DOWNLOAD_PATH_AUDIO_PREFERENCE); useTorCheckBox = (CheckBoxPreference) findPreference(USE_TOR_KEY); @@ -154,16 +154,17 @@ public class SettingsActivity extends PreferenceActivity { @Override public boolean onPreferenceClick(Preference preference) { Intent i = new Intent(getActivity(), FilePickerActivity.class); - i.setAction(Intent.ACTION_GET_CONTENT); + i.putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false); i.putExtra(FilePickerActivity.EXTRA_ALLOW_CREATE_DIR, true); - i.putExtra(FilePickerActivity.EXTRA_MODE, AbstractFilePickerFragment.MODE_DIR); + i.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_DIR); startActivityForResult(i, 233); return true; } }); + updateSummary(); } @@ -189,11 +190,47 @@ public class SettingsActivity extends PreferenceActivity { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); + + if (requestCode == 233 && resultCode == Activity.RESULT_OK) { + if (data.getBooleanExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false)) { + // For JellyBean and above + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + ClipData clip = data.getClipData(); + + if (clip != null) { + for (int i = 0; i < clip.getItemCount(); i++) { + Uri uri = clip.getItemAt(i).getUri(); + // Do something with the URI + } + } + // For Ice Cream Sandwich + } else { + ArrayList paths = data.getStringArrayListExtra + (FilePickerActivity.EXTRA_PATHS); + + if (paths != null) { + for (String path: paths) { + Uri uri = Uri.parse(path); + // Do something with the URI + } + } + } + + } else { + Uri uri = data.getData(); + // Do something with the URI + } + + + + } + // try to start tor regardless of resultCode since clicking back after // installing the app does not necessarily return RESULT_OK App.configureTor(requestCode == REQUEST_INSTALL_ORBOT && OrbotHelper.requestStartTor(this)); + + super.onActivityResult(requestCode, resultCode, data); } @Override diff --git a/app/src/main/res/xml/settings.xml b/app/src/main/res/xml/settings.xml index f40ce43bc..ba46d353f 100644 --- a/app/src/main/res/xml/settings.xml +++ b/app/src/main/res/xml/settings.xml @@ -92,7 +92,7 @@ android:title="@string/settings_category_other_title" android:textAllCaps="true"> - Date: Wed, 15 Jun 2016 07:19:26 -0300 Subject: [PATCH 03/10] The file picker is working and I'm reordering the code to make it easier to follow --- .../org/schabi/newpipe/SettingsActivity.java | 132 ++-------------- .../org/schabi/newpipe/SettingsFragment.java | 142 ++++++++++++++++++ 2 files changed, 151 insertions(+), 123 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/SettingsFragment.java diff --git a/app/src/main/java/org/schabi/newpipe/SettingsActivity.java b/app/src/main/java/org/schabi/newpipe/SettingsActivity.java index 58caea7fe..7b58946b1 100644 --- a/app/src/main/java/org/schabi/newpipe/SettingsActivity.java +++ b/app/src/main/java/org/schabi/newpipe/SettingsActivity.java @@ -18,6 +18,7 @@ import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; +import android.preference.PreferenceScreen; import android.support.annotation.LayoutRes; import android.support.annotation.NonNull; import android.support.v7.app.ActionBar; @@ -57,7 +58,7 @@ import us.shandian.giga.util.Utility; public class SettingsActivity extends PreferenceActivity { - private static final int REQUEST_INSTALL_ORBOT = 0x1234; + public static final int REQUEST_INSTALL_ORBOT = 0x1234; private AppCompatDelegate mDelegate = null; @Override @@ -77,117 +78,6 @@ public class SettingsActivity extends PreferenceActivity { } - public static class SettingsFragment extends PreferenceFragment{ - SharedPreferences.OnSharedPreferenceChangeListener prefListener; - - // get keys - String DEFAULT_RESOLUTION_PREFERENCE; - String DEFAULT_AUDIO_FORMAT_PREFERENCE; - String SEARCH_LANGUAGE_PREFERENCE; - String DOWNLOAD_PATH_PREFERENCE; - String DOWNLOAD_PATH_AUDIO_PREFERENCE; - String USE_TOR_KEY; - - private ListPreference defaultResolutionPreference; - private ListPreference defaultAudioFormatPreference; - private ListPreference searchLanguagePreference; - private Preference downloadPathPreference; - private EditTextPreference downloadPathAudioPreference; - private CheckBoxPreference useTorCheckBox; - private SharedPreferences defaultPreferences; - - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - addPreferencesFromResource(R.xml.settings); - - final Activity activity = getActivity(); - - defaultPreferences = PreferenceManager.getDefaultSharedPreferences(activity); - - // get keys - DEFAULT_RESOLUTION_PREFERENCE =getString(R.string.default_resolution_key); - DEFAULT_AUDIO_FORMAT_PREFERENCE =getString(R.string.default_audio_format_key); - SEARCH_LANGUAGE_PREFERENCE =getString(R.string.search_language_key); - DOWNLOAD_PATH_PREFERENCE = getString(R.string.download_path_key); - DOWNLOAD_PATH_AUDIO_PREFERENCE = getString(R.string.download_path_audio_key); - USE_TOR_KEY = getString(R.string.use_tor_key); - - // get pref objects - defaultResolutionPreference = - (ListPreference) findPreference(DEFAULT_RESOLUTION_PREFERENCE); - defaultAudioFormatPreference = - (ListPreference) findPreference(DEFAULT_AUDIO_FORMAT_PREFERENCE); - searchLanguagePreference = - (ListPreference) findPreference(SEARCH_LANGUAGE_PREFERENCE); - downloadPathPreference = findPreference(DOWNLOAD_PATH_PREFERENCE); - downloadPathAudioPreference = - (EditTextPreference) findPreference(DOWNLOAD_PATH_AUDIO_PREFERENCE); - useTorCheckBox = (CheckBoxPreference) findPreference(USE_TOR_KEY); - - prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() { - @Override - public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, - String key) { - Activity a = getActivity(); - if(a != null) { - updateSummary(); - - if (defaultPreferences.getBoolean(USE_TOR_KEY, false)) { - if (OrbotHelper.isOrbotInstalled(a)) { - App.configureTor(true); - OrbotHelper.requestStartTor(a); - } else { - Intent intent = OrbotHelper.getOrbotInstallIntent(a); - a.startActivityForResult(intent, REQUEST_INSTALL_ORBOT); - } - } else { - App.configureTor(false); - } - } - } - }; - defaultPreferences.registerOnSharedPreferenceChangeListener(prefListener); - - downloadPathPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener(){ - @Override - public boolean onPreferenceClick(Preference preference) { - Intent i = new Intent(getActivity(), FilePickerActivity.class); - - i.putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false); - i.putExtra(FilePickerActivity.EXTRA_ALLOW_CREATE_DIR, true); - i.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_DIR); - startActivityForResult(i, 233); - - return true; - } - }); - - - updateSummary(); - } - - // This is used to show the status of some preference in the description - private void updateSummary() { - defaultResolutionPreference.setSummary( - defaultPreferences.getString(DEFAULT_RESOLUTION_PREFERENCE, - getString(R.string.default_resolution_value))); - defaultAudioFormatPreference.setSummary( - defaultPreferences.getString(DEFAULT_AUDIO_FORMAT_PREFERENCE, - getString(R.string.default_audio_format_value))); - searchLanguagePreference.setSummary( - defaultPreferences.getString(SEARCH_LANGUAGE_PREFERENCE, - getString(R.string.default_language_value))); - downloadPathPreference.setSummary( - defaultPreferences.getString(DOWNLOAD_PATH_PREFERENCE, - getString(R.string.download_path_summary))); - downloadPathAudioPreference.setSummary( - defaultPreferences.getString(DOWNLOAD_PATH_AUDIO_PREFERENCE, - getString(R.string.download_path_audio_summary))); - } - } - @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { @@ -200,7 +90,6 @@ public class SettingsActivity extends PreferenceActivity { if (clip != null) { for (int i = 0; i < clip.getItemCount(); i++) { Uri uri = clip.getItemAt(i).getUri(); - // Do something with the URI } } // For Ice Cream Sandwich @@ -211,25 +100,22 @@ public class SettingsActivity extends PreferenceActivity { if (paths != null) { for (String path: paths) { Uri uri = Uri.parse(path); - // Do something with the URI } } } } else { Uri uri = data.getData(); - // Do something with the URI } - - + } + else if(requestCode == REQUEST_INSTALL_ORBOT) + { + // try to start tor regardless of resultCode since clicking back after + // installing the app does not necessarily return RESULT_OK + App.configureTor(requestCode == REQUEST_INSTALL_ORBOT + && OrbotHelper.requestStartTor(this)); } - - // try to start tor regardless of resultCode since clicking back after - // installing the app does not necessarily return RESULT_OK - App.configureTor(requestCode == REQUEST_INSTALL_ORBOT - && OrbotHelper.requestStartTor(this)); - super.onActivityResult(requestCode, resultCode, data); } diff --git a/app/src/main/java/org/schabi/newpipe/SettingsFragment.java b/app/src/main/java/org/schabi/newpipe/SettingsFragment.java new file mode 100644 index 000000000..061a6671d --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/SettingsFragment.java @@ -0,0 +1,142 @@ +package org.schabi.newpipe; + +import android.app.Activity; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.preference.CheckBoxPreference; +import android.preference.EditTextPreference; +import android.preference.ListPreference; +import android.preference.Preference; +import android.preference.PreferenceFragment; +import android.preference.PreferenceManager; +import android.preference.PreferenceScreen; + +import com.nononsenseapps.filepicker.FilePickerActivity; + +import info.guardianproject.netcipher.proxy.OrbotHelper; + +/** + * Created by david on 15/06/16. + */ +public class SettingsFragment extends PreferenceFragment + implements SharedPreferences.OnSharedPreferenceChangeListener +{ + SharedPreferences.OnSharedPreferenceChangeListener prefListener; + + // get keys + String DEFAULT_RESOLUTION_PREFERENCE; + String DEFAULT_AUDIO_FORMAT_PREFERENCE; + String SEARCH_LANGUAGE_PREFERENCE; + String DOWNLOAD_PATH_PREFERENCE; + String DOWNLOAD_PATH_AUDIO_PREFERENCE; + String USE_TOR_KEY; + + private ListPreference defaultResolutionPreference; + private ListPreference defaultAudioFormatPreference; + private ListPreference searchLanguagePreference; + private Preference downloadPathPreference; + private EditTextPreference downloadPathAudioPreference; + private CheckBoxPreference useTorCheckBox; + private SharedPreferences defaultPreferences; + + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.settings); + + final Activity activity = getActivity(); + + defaultPreferences = PreferenceManager.getDefaultSharedPreferences(activity); + + // get keys + DEFAULT_RESOLUTION_PREFERENCE = getString(R.string.default_resolution_key); + DEFAULT_AUDIO_FORMAT_PREFERENCE = getString(R.string.default_audio_format_key); + SEARCH_LANGUAGE_PREFERENCE = getString(R.string.search_language_key); + DOWNLOAD_PATH_PREFERENCE = getString(R.string.download_path_key); + DOWNLOAD_PATH_AUDIO_PREFERENCE = getString(R.string.download_path_audio_key); + USE_TOR_KEY = getString(R.string.use_tor_key); + + // get pref objects + defaultResolutionPreference = + (ListPreference) findPreference(DEFAULT_RESOLUTION_PREFERENCE); + defaultAudioFormatPreference = + (ListPreference) findPreference(DEFAULT_AUDIO_FORMAT_PREFERENCE); + searchLanguagePreference = + (ListPreference) findPreference(SEARCH_LANGUAGE_PREFERENCE); + downloadPathPreference = findPreference(DOWNLOAD_PATH_PREFERENCE); + downloadPathAudioPreference = + (EditTextPreference) findPreference(DOWNLOAD_PATH_AUDIO_PREFERENCE); + useTorCheckBox = (CheckBoxPreference) findPreference(USE_TOR_KEY); + + prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() { + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, + String key) { + Activity a = getActivity(); + + if(a != null) { + updateSummary(); + + if (defaultPreferences.getBoolean(USE_TOR_KEY, false)) { + if (OrbotHelper.isOrbotInstalled(a)) { + App.configureTor(true); + OrbotHelper.requestStartTor(a); + } else { + Intent intent = OrbotHelper.getOrbotInstallIntent(a); + a.startActivityForResult(intent, SettingsActivity.REQUEST_INSTALL_ORBOT); + } + } else { + App.configureTor(false); + } + } + } + }; + defaultPreferences.registerOnSharedPreferenceChangeListener(prefListener); + + + updateSummary(); + } + + @Override + public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { + if(preference.getKey() == downloadPathPreference.getKey()) + { + Activity activity = getActivity(); + Intent i = new Intent(activity, FilePickerActivity.class); + + i.putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false); + i.putExtra(FilePickerActivity.EXTRA_ALLOW_CREATE_DIR, true); + i.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_DIR); + + activity.startActivityForResult(i, 233); + } + + return super.onPreferenceTreeClick(preferenceScreen, preference); + } + + // This is used to show the status of some preference in the description + private void updateSummary() { + defaultResolutionPreference.setSummary( + defaultPreferences.getString(DEFAULT_RESOLUTION_PREFERENCE, + getString(R.string.default_resolution_value))); + defaultAudioFormatPreference.setSummary( + defaultPreferences.getString(DEFAULT_AUDIO_FORMAT_PREFERENCE, + getString(R.string.default_audio_format_value))); + searchLanguagePreference.setSummary( + defaultPreferences.getString(SEARCH_LANGUAGE_PREFERENCE, + getString(R.string.default_language_value))); + downloadPathPreference.setSummary( + defaultPreferences.getString(DOWNLOAD_PATH_PREFERENCE, + getString(R.string.download_path_summary))); + downloadPathAudioPreference.setSummary( + defaultPreferences.getString(DOWNLOAD_PATH_AUDIO_PREFERENCE, + getString(R.string.download_path_audio_summary))); + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + + } +} From d145a3e967b01533be811714b40581b317258c45 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 15 Jun 2016 12:52:31 -0300 Subject: [PATCH 04/10] Done with the file picker for the download settings --- .../org/schabi/newpipe/SettingsActivity.java | 31 +++++++------ .../org/schabi/newpipe/SettingsFragment.java | 44 +++++++++++++++---- app/src/main/res/xml/settings.xml | 2 +- 3 files changed, 53 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/SettingsActivity.java b/app/src/main/java/org/schabi/newpipe/SettingsActivity.java index 7b58946b1..17e3b16bd 100644 --- a/app/src/main/java/org/schabi/newpipe/SettingsActivity.java +++ b/app/src/main/java/org/schabi/newpipe/SettingsActivity.java @@ -6,19 +6,11 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; -import android.media.AudioManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; -import android.os.Environment; -import android.preference.CheckBoxPreference; -import android.preference.EditTextPreference; -import android.preference.ListPreference; -import android.preference.Preference; import android.preference.PreferenceActivity; -import android.preference.PreferenceFragment; import android.preference.PreferenceManager; -import android.preference.PreferenceScreen; import android.support.annotation.LayoutRes; import android.support.annotation.NonNull; import android.support.v7.app.ActionBar; @@ -75,13 +67,16 @@ public class SettingsActivity extends PreferenceActivity { getFragmentManager().beginTransaction() .replace(android.R.id.content, new SettingsFragment()) .commit(); - } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == 233 && resultCode == Activity.RESULT_OK) { + if ((requestCode == R.string.download_path_audio_key + || requestCode == R.string.download_path_key) + && resultCode == Activity.RESULT_OK) { + + Uri uri = null; if (data.getBooleanExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false)) { // For JellyBean and above if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { @@ -89,7 +84,7 @@ public class SettingsActivity extends PreferenceActivity { if (clip != null) { for (int i = 0; i < clip.getItemCount(); i++) { - Uri uri = clip.getItemAt(i).getUri(); + uri = clip.getItemAt(i).getUri(); } } // For Ice Cream Sandwich @@ -99,14 +94,22 @@ public class SettingsActivity extends PreferenceActivity { if (paths != null) { for (String path: paths) { - Uri uri = Uri.parse(path); + uri = Uri.parse(path); } } } - } else { - Uri uri = data.getData(); + uri = data.getData(); } + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + + //requestCode is equal to R.string.download_path_key or + //R.string.download_path_audio_key + String key = getString(requestCode); + String path = data.getData().toString().substring(7); + prefs.edit() + .putString(key, path) + .commit(); } else if(requestCode == REQUEST_INSTALL_ORBOT) { diff --git a/app/src/main/java/org/schabi/newpipe/SettingsFragment.java b/app/src/main/java/org/schabi/newpipe/SettingsFragment.java index 061a6671d..4f2bc4613 100644 --- a/app/src/main/java/org/schabi/newpipe/SettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/SettingsFragment.java @@ -36,7 +36,7 @@ public class SettingsFragment extends PreferenceFragment private ListPreference defaultAudioFormatPreference; private ListPreference searchLanguagePreference; private Preference downloadPathPreference; - private EditTextPreference downloadPathAudioPreference; + private Preference downloadPathAudioPreference; private CheckBoxPreference useTorCheckBox; private SharedPreferences defaultPreferences; @@ -66,8 +66,7 @@ public class SettingsFragment extends PreferenceFragment searchLanguagePreference = (ListPreference) findPreference(SEARCH_LANGUAGE_PREFERENCE); downloadPathPreference = findPreference(DOWNLOAD_PATH_PREFERENCE); - downloadPathAudioPreference = - (EditTextPreference) findPreference(DOWNLOAD_PATH_AUDIO_PREFERENCE); + downloadPathAudioPreference = findPreference(DOWNLOAD_PATH_AUDIO_PREFERENCE); useTorCheckBox = (CheckBoxPreference) findPreference(USE_TOR_KEY); prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() { @@ -75,8 +74,12 @@ public class SettingsFragment extends PreferenceFragment public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { Activity a = getActivity(); - - if(a != null) { + if(a == null) + { + return; + } + if (key == USE_TOR_KEY) + { updateSummary(); if (defaultPreferences.getBoolean(USE_TOR_KEY, false)) { @@ -91,17 +94,34 @@ public class SettingsFragment extends PreferenceFragment App.configureTor(false); } } + else if (key == DOWNLOAD_PATH_PREFERENCE) + { + String downloadPath = sharedPreferences + .getString(DOWNLOAD_PATH_PREFERENCE, + getString(R.string.download_path_summary)); + downloadPathPreference + .setSummary(downloadPath); + } + else if (key == DOWNLOAD_PATH_AUDIO_PREFERENCE) + { + String downloadPath = sharedPreferences + .getString(DOWNLOAD_PATH_AUDIO_PREFERENCE, + getString(R.string.download_path_audio_summary)); + downloadPathAudioPreference + .setSummary(downloadPath); + } } }; defaultPreferences.registerOnSharedPreferenceChangeListener(prefListener); - updateSummary(); } @Override public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { - if(preference.getKey() == downloadPathPreference.getKey()) + + if(preference.getKey() == downloadPathPreference.getKey() || + preference.getKey() == downloadPathAudioPreference.getKey()) { Activity activity = getActivity(); Intent i = new Intent(activity, FilePickerActivity.class); @@ -109,8 +129,14 @@ public class SettingsFragment extends PreferenceFragment i.putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false); i.putExtra(FilePickerActivity.EXTRA_ALLOW_CREATE_DIR, true); i.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_DIR); - - activity.startActivityForResult(i, 233); + if(preference.getKey() == downloadPathPreference.getKey()) + { + activity.startActivityForResult(i, R.string.download_path_key); + } + else if (preference.getKey() == downloadPathAudioPreference.getKey()) + { + activity.startActivityForResult(i, R.string.download_path_audio_key); + } } return super.onPreferenceTreeClick(preferenceScreen, preference); diff --git a/app/src/main/res/xml/settings.xml b/app/src/main/res/xml/settings.xml index ba46d353f..f3c53ec36 100644 --- a/app/src/main/res/xml/settings.xml +++ b/app/src/main/res/xml/settings.xml @@ -98,7 +98,7 @@ android:summary="@string/download_path_summary" android:dialogTitle="@string/download_path_dialog_title" /> - Date: Thu, 16 Jun 2016 07:58:09 -0300 Subject: [PATCH 05/10] A little cleanup --- app/src/main/java/org/schabi/newpipe/SettingsActivity.java | 2 -- app/src/main/java/org/schabi/newpipe/SettingsFragment.java | 1 - 2 files changed, 3 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/SettingsActivity.java b/app/src/main/java/org/schabi/newpipe/SettingsActivity.java index 17e3b16bd..e9c4a4870 100644 --- a/app/src/main/java/org/schabi/newpipe/SettingsActivity.java +++ b/app/src/main/java/org/schabi/newpipe/SettingsActivity.java @@ -20,13 +20,11 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import com.nononsenseapps.filepicker.AbstractFilePickerFragment; import com.nononsenseapps.filepicker.FilePickerActivity; import java.util.ArrayList; import info.guardianproject.netcipher.proxy.OrbotHelper; -import us.shandian.giga.util.Utility; /** * Created by Christian Schabesberger on 31.08.15. diff --git a/app/src/main/java/org/schabi/newpipe/SettingsFragment.java b/app/src/main/java/org/schabi/newpipe/SettingsFragment.java index 4f2bc4613..916d9d6b1 100644 --- a/app/src/main/java/org/schabi/newpipe/SettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/SettingsFragment.java @@ -5,7 +5,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.CheckBoxPreference; -import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceFragment; From 45be3fb0e8653b2c20de3e1230bbcc662e5356a7 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 26 May 2016 11:59:12 -0300 Subject: [PATCH 06/10] Starting to add a filepicker to the directory preferences --- app/src/main/AndroidManifest.xml | 7 +++++ .../org/schabi/newpipe/SettingsActivity.java | 29 +++++++++++++++++-- app/src/main/res/values/styles.xml | 20 +++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f6e311248..26788cd1b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -137,5 +137,12 @@ + + + diff --git a/app/src/main/java/org/schabi/newpipe/SettingsActivity.java b/app/src/main/java/org/schabi/newpipe/SettingsActivity.java index 6436574de..3714d7361 100644 --- a/app/src/main/java/org/schabi/newpipe/SettingsActivity.java +++ b/app/src/main/java/org/schabi/newpipe/SettingsActivity.java @@ -1,16 +1,20 @@ package org.schabi.newpipe; import android.app.Activity; +import android.content.ClipData; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.media.AudioManager; +import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.preference.CheckBoxPreference; import android.preference.EditTextPreference; import android.preference.ListPreference; +import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; @@ -23,7 +27,13 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import com.nononsenseapps.filepicker.AbstractFilePickerFragment; +import com.nononsenseapps.filepicker.FilePickerActivity; + +import java.util.ArrayList; + import info.guardianproject.netcipher.proxy.OrbotHelper; +import us.shandian.giga.util.Utility; /** * Created by Christian Schabesberger on 31.08.15. @@ -81,7 +91,7 @@ public class SettingsActivity extends PreferenceActivity { private ListPreference defaultResolutionPreference; private ListPreference defaultAudioFormatPreference; private ListPreference searchLanguagePreference; - private EditTextPreference downloadPathPreference; + private Preference downloadPathPreference; private EditTextPreference downloadPathAudioPreference; private CheckBoxPreference useTorCheckBox; private SharedPreferences defaultPreferences; @@ -111,8 +121,7 @@ public class SettingsActivity extends PreferenceActivity { (ListPreference) findPreference(DEFAULT_AUDIO_FORMAT_PREFERENCE); searchLanguagePreference = (ListPreference) findPreference(SEARCH_LANGUAGE_PREFERENCE); - downloadPathPreference = - (EditTextPreference) findPreference(DOWNLOAD_PATH_PREFERENCE); + downloadPathPreference = (Preference) findPreference(DOWNLOAD_PATH_PREFERENCE); downloadPathAudioPreference = (EditTextPreference) findPreference(DOWNLOAD_PATH_AUDIO_PREFERENCE); useTorCheckBox = (CheckBoxPreference) findPreference(USE_TOR_KEY); @@ -141,6 +150,20 @@ public class SettingsActivity extends PreferenceActivity { }; defaultPreferences.registerOnSharedPreferenceChangeListener(prefListener); + downloadPathPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener(){ + @Override + public boolean onPreferenceClick(Preference preference) { + Intent i = new Intent(getActivity(), FilePickerActivity.class); + i.setAction(Intent.ACTION_GET_CONTENT); + i.putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false); + i.putExtra(FilePickerActivity.EXTRA_ALLOW_CREATE_DIR, true); + i.putExtra(FilePickerActivity.EXTRA_MODE, AbstractFilePickerFragment.MODE_DIR); + startActivityForResult(i, 233); + + return true; + } + }); + updateSummary(); } diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index e7549b7f7..1c8dca48f 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -49,4 +49,24 @@ + + + + + From 36babcaf3633d922551bacabea48f37e446f2eb7 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 14 Jun 2016 06:13:06 -0300 Subject: [PATCH 07/10] Trying to make last filepicker version work, maybe I'm having some memory problems --- .../org/schabi/newpipe/SettingsActivity.java | 45 +++++++++++++++++-- app/src/main/res/xml/settings.xml | 2 +- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/SettingsActivity.java b/app/src/main/java/org/schabi/newpipe/SettingsActivity.java index 3714d7361..61a74ed19 100644 --- a/app/src/main/java/org/schabi/newpipe/SettingsActivity.java +++ b/app/src/main/java/org/schabi/newpipe/SettingsActivity.java @@ -121,7 +121,7 @@ public class SettingsActivity extends PreferenceActivity { (ListPreference) findPreference(DEFAULT_AUDIO_FORMAT_PREFERENCE); searchLanguagePreference = (ListPreference) findPreference(SEARCH_LANGUAGE_PREFERENCE); - downloadPathPreference = (Preference) findPreference(DOWNLOAD_PATH_PREFERENCE); + downloadPathPreference = findPreference(DOWNLOAD_PATH_PREFERENCE); downloadPathAudioPreference = (EditTextPreference) findPreference(DOWNLOAD_PATH_AUDIO_PREFERENCE); useTorCheckBox = (CheckBoxPreference) findPreference(USE_TOR_KEY); @@ -154,16 +154,17 @@ public class SettingsActivity extends PreferenceActivity { @Override public boolean onPreferenceClick(Preference preference) { Intent i = new Intent(getActivity(), FilePickerActivity.class); - i.setAction(Intent.ACTION_GET_CONTENT); + i.putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false); i.putExtra(FilePickerActivity.EXTRA_ALLOW_CREATE_DIR, true); - i.putExtra(FilePickerActivity.EXTRA_MODE, AbstractFilePickerFragment.MODE_DIR); + i.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_DIR); startActivityForResult(i, 233); return true; } }); + updateSummary(); } @@ -189,11 +190,47 @@ public class SettingsActivity extends PreferenceActivity { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); + + if (requestCode == 233 && resultCode == Activity.RESULT_OK) { + if (data.getBooleanExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false)) { + // For JellyBean and above + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + ClipData clip = data.getClipData(); + + if (clip != null) { + for (int i = 0; i < clip.getItemCount(); i++) { + Uri uri = clip.getItemAt(i).getUri(); + // Do something with the URI + } + } + // For Ice Cream Sandwich + } else { + ArrayList paths = data.getStringArrayListExtra + (FilePickerActivity.EXTRA_PATHS); + + if (paths != null) { + for (String path: paths) { + Uri uri = Uri.parse(path); + // Do something with the URI + } + } + } + + } else { + Uri uri = data.getData(); + // Do something with the URI + } + + + + } + // try to start tor regardless of resultCode since clicking back after // installing the app does not necessarily return RESULT_OK App.configureTor(requestCode == REQUEST_INSTALL_ORBOT && OrbotHelper.requestStartTor(this)); + + super.onActivityResult(requestCode, resultCode, data); } @Override diff --git a/app/src/main/res/xml/settings.xml b/app/src/main/res/xml/settings.xml index f40ce43bc..ba46d353f 100644 --- a/app/src/main/res/xml/settings.xml +++ b/app/src/main/res/xml/settings.xml @@ -92,7 +92,7 @@ android:title="@string/settings_category_other_title" android:textAllCaps="true"> - Date: Wed, 15 Jun 2016 07:19:26 -0300 Subject: [PATCH 08/10] The file picker is working and I'm reordering the code to make it easier to follow --- .../org/schabi/newpipe/SettingsActivity.java | 144 ++---------------- .../org/schabi/newpipe/SettingsFragment.java | 142 +++++++++++++++++ 2 files changed, 151 insertions(+), 135 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/SettingsFragment.java diff --git a/app/src/main/java/org/schabi/newpipe/SettingsActivity.java b/app/src/main/java/org/schabi/newpipe/SettingsActivity.java index 61a74ed19..5c5ec56c4 100644 --- a/app/src/main/java/org/schabi/newpipe/SettingsActivity.java +++ b/app/src/main/java/org/schabi/newpipe/SettingsActivity.java @@ -4,20 +4,11 @@ import android.app.Activity; import android.content.ClipData; import android.content.Context; import android.content.Intent; -import android.content.SharedPreferences; import android.content.res.Configuration; -import android.media.AudioManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; -import android.os.Environment; -import android.preference.CheckBoxPreference; -import android.preference.EditTextPreference; -import android.preference.ListPreference; -import android.preference.Preference; import android.preference.PreferenceActivity; -import android.preference.PreferenceFragment; -import android.preference.PreferenceManager; import android.support.annotation.LayoutRes; import android.support.annotation.NonNull; import android.support.v7.app.ActionBar; @@ -27,13 +18,11 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import com.nononsenseapps.filepicker.AbstractFilePickerFragment; import com.nononsenseapps.filepicker.FilePickerActivity; import java.util.ArrayList; import info.guardianproject.netcipher.proxy.OrbotHelper; -import us.shandian.giga.util.Utility; /** * Created by Christian Schabesberger on 31.08.15. @@ -57,8 +46,8 @@ import us.shandian.giga.util.Utility; public class SettingsActivity extends PreferenceActivity { - private static final int REQUEST_INSTALL_ORBOT = 0x1234; - private AppCompatDelegate mDelegate; + public static final int REQUEST_INSTALL_ORBOT = 0x1234; + private AppCompatDelegate mDelegate = null; @Override protected void onCreate(Bundle savedInstanceBundle) { @@ -77,117 +66,6 @@ public class SettingsActivity extends PreferenceActivity { } - public static class SettingsFragment extends PreferenceFragment{ - SharedPreferences.OnSharedPreferenceChangeListener prefListener; - - // get keys - String DEFAULT_RESOLUTION_PREFERENCE; - String DEFAULT_AUDIO_FORMAT_PREFERENCE; - String SEARCH_LANGUAGE_PREFERENCE; - String DOWNLOAD_PATH_PREFERENCE; - String DOWNLOAD_PATH_AUDIO_PREFERENCE; - String USE_TOR_KEY; - - private ListPreference defaultResolutionPreference; - private ListPreference defaultAudioFormatPreference; - private ListPreference searchLanguagePreference; - private Preference downloadPathPreference; - private EditTextPreference downloadPathAudioPreference; - private CheckBoxPreference useTorCheckBox; - private SharedPreferences defaultPreferences; - - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - addPreferencesFromResource(R.xml.settings); - - final Activity activity = getActivity(); - - defaultPreferences = PreferenceManager.getDefaultSharedPreferences(activity); - - // get keys - DEFAULT_RESOLUTION_PREFERENCE =getString(R.string.default_resolution_key); - DEFAULT_AUDIO_FORMAT_PREFERENCE =getString(R.string.default_audio_format_key); - SEARCH_LANGUAGE_PREFERENCE =getString(R.string.search_language_key); - DOWNLOAD_PATH_PREFERENCE = getString(R.string.download_path_key); - DOWNLOAD_PATH_AUDIO_PREFERENCE = getString(R.string.download_path_audio_key); - USE_TOR_KEY = getString(R.string.use_tor_key); - - // get pref objects - defaultResolutionPreference = - (ListPreference) findPreference(DEFAULT_RESOLUTION_PREFERENCE); - defaultAudioFormatPreference = - (ListPreference) findPreference(DEFAULT_AUDIO_FORMAT_PREFERENCE); - searchLanguagePreference = - (ListPreference) findPreference(SEARCH_LANGUAGE_PREFERENCE); - downloadPathPreference = findPreference(DOWNLOAD_PATH_PREFERENCE); - downloadPathAudioPreference = - (EditTextPreference) findPreference(DOWNLOAD_PATH_AUDIO_PREFERENCE); - useTorCheckBox = (CheckBoxPreference) findPreference(USE_TOR_KEY); - - prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() { - @Override - public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, - String key) { - Activity a = getActivity(); - if(a != null) { - updateSummary(); - - if (defaultPreferences.getBoolean(USE_TOR_KEY, false)) { - if (OrbotHelper.isOrbotInstalled(a)) { - App.configureTor(true); - OrbotHelper.requestStartTor(a); - } else { - Intent intent = OrbotHelper.getOrbotInstallIntent(a); - a.startActivityForResult(intent, REQUEST_INSTALL_ORBOT); - } - } else { - App.configureTor(false); - } - } - } - }; - defaultPreferences.registerOnSharedPreferenceChangeListener(prefListener); - - downloadPathPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener(){ - @Override - public boolean onPreferenceClick(Preference preference) { - Intent i = new Intent(getActivity(), FilePickerActivity.class); - - i.putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false); - i.putExtra(FilePickerActivity.EXTRA_ALLOW_CREATE_DIR, true); - i.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_DIR); - startActivityForResult(i, 233); - - return true; - } - }); - - - updateSummary(); - } - - // This is used to show the status of some preference in the description - private void updateSummary() { - defaultResolutionPreference.setSummary( - defaultPreferences.getString(DEFAULT_RESOLUTION_PREFERENCE, - getString(R.string.default_resolution_value))); - defaultAudioFormatPreference.setSummary( - defaultPreferences.getString(DEFAULT_AUDIO_FORMAT_PREFERENCE, - getString(R.string.default_audio_format_value))); - searchLanguagePreference.setSummary( - defaultPreferences.getString(SEARCH_LANGUAGE_PREFERENCE, - getString(R.string.default_language_value))); - downloadPathPreference.setSummary( - defaultPreferences.getString(DOWNLOAD_PATH_PREFERENCE, - getString(R.string.download_path_summary))); - downloadPathAudioPreference.setSummary( - defaultPreferences.getString(DOWNLOAD_PATH_AUDIO_PREFERENCE, - getString(R.string.download_path_audio_summary))); - } - } - @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { @@ -200,7 +78,6 @@ public class SettingsActivity extends PreferenceActivity { if (clip != null) { for (int i = 0; i < clip.getItemCount(); i++) { Uri uri = clip.getItemAt(i).getUri(); - // Do something with the URI } } // For Ice Cream Sandwich @@ -211,25 +88,22 @@ public class SettingsActivity extends PreferenceActivity { if (paths != null) { for (String path: paths) { Uri uri = Uri.parse(path); - // Do something with the URI } } } } else { Uri uri = data.getData(); - // Do something with the URI } - - + } + else if(requestCode == REQUEST_INSTALL_ORBOT) + { + // try to start tor regardless of resultCode since clicking back after + // installing the app does not necessarily return RESULT_OK + App.configureTor(requestCode == REQUEST_INSTALL_ORBOT + && OrbotHelper.requestStartTor(this)); } - - // try to start tor regardless of resultCode since clicking back after - // installing the app does not necessarily return RESULT_OK - App.configureTor(requestCode == REQUEST_INSTALL_ORBOT - && OrbotHelper.requestStartTor(this)); - super.onActivityResult(requestCode, resultCode, data); } diff --git a/app/src/main/java/org/schabi/newpipe/SettingsFragment.java b/app/src/main/java/org/schabi/newpipe/SettingsFragment.java new file mode 100644 index 000000000..061a6671d --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/SettingsFragment.java @@ -0,0 +1,142 @@ +package org.schabi.newpipe; + +import android.app.Activity; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.preference.CheckBoxPreference; +import android.preference.EditTextPreference; +import android.preference.ListPreference; +import android.preference.Preference; +import android.preference.PreferenceFragment; +import android.preference.PreferenceManager; +import android.preference.PreferenceScreen; + +import com.nononsenseapps.filepicker.FilePickerActivity; + +import info.guardianproject.netcipher.proxy.OrbotHelper; + +/** + * Created by david on 15/06/16. + */ +public class SettingsFragment extends PreferenceFragment + implements SharedPreferences.OnSharedPreferenceChangeListener +{ + SharedPreferences.OnSharedPreferenceChangeListener prefListener; + + // get keys + String DEFAULT_RESOLUTION_PREFERENCE; + String DEFAULT_AUDIO_FORMAT_PREFERENCE; + String SEARCH_LANGUAGE_PREFERENCE; + String DOWNLOAD_PATH_PREFERENCE; + String DOWNLOAD_PATH_AUDIO_PREFERENCE; + String USE_TOR_KEY; + + private ListPreference defaultResolutionPreference; + private ListPreference defaultAudioFormatPreference; + private ListPreference searchLanguagePreference; + private Preference downloadPathPreference; + private EditTextPreference downloadPathAudioPreference; + private CheckBoxPreference useTorCheckBox; + private SharedPreferences defaultPreferences; + + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.settings); + + final Activity activity = getActivity(); + + defaultPreferences = PreferenceManager.getDefaultSharedPreferences(activity); + + // get keys + DEFAULT_RESOLUTION_PREFERENCE = getString(R.string.default_resolution_key); + DEFAULT_AUDIO_FORMAT_PREFERENCE = getString(R.string.default_audio_format_key); + SEARCH_LANGUAGE_PREFERENCE = getString(R.string.search_language_key); + DOWNLOAD_PATH_PREFERENCE = getString(R.string.download_path_key); + DOWNLOAD_PATH_AUDIO_PREFERENCE = getString(R.string.download_path_audio_key); + USE_TOR_KEY = getString(R.string.use_tor_key); + + // get pref objects + defaultResolutionPreference = + (ListPreference) findPreference(DEFAULT_RESOLUTION_PREFERENCE); + defaultAudioFormatPreference = + (ListPreference) findPreference(DEFAULT_AUDIO_FORMAT_PREFERENCE); + searchLanguagePreference = + (ListPreference) findPreference(SEARCH_LANGUAGE_PREFERENCE); + downloadPathPreference = findPreference(DOWNLOAD_PATH_PREFERENCE); + downloadPathAudioPreference = + (EditTextPreference) findPreference(DOWNLOAD_PATH_AUDIO_PREFERENCE); + useTorCheckBox = (CheckBoxPreference) findPreference(USE_TOR_KEY); + + prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() { + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, + String key) { + Activity a = getActivity(); + + if(a != null) { + updateSummary(); + + if (defaultPreferences.getBoolean(USE_TOR_KEY, false)) { + if (OrbotHelper.isOrbotInstalled(a)) { + App.configureTor(true); + OrbotHelper.requestStartTor(a); + } else { + Intent intent = OrbotHelper.getOrbotInstallIntent(a); + a.startActivityForResult(intent, SettingsActivity.REQUEST_INSTALL_ORBOT); + } + } else { + App.configureTor(false); + } + } + } + }; + defaultPreferences.registerOnSharedPreferenceChangeListener(prefListener); + + + updateSummary(); + } + + @Override + public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { + if(preference.getKey() == downloadPathPreference.getKey()) + { + Activity activity = getActivity(); + Intent i = new Intent(activity, FilePickerActivity.class); + + i.putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false); + i.putExtra(FilePickerActivity.EXTRA_ALLOW_CREATE_DIR, true); + i.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_DIR); + + activity.startActivityForResult(i, 233); + } + + return super.onPreferenceTreeClick(preferenceScreen, preference); + } + + // This is used to show the status of some preference in the description + private void updateSummary() { + defaultResolutionPreference.setSummary( + defaultPreferences.getString(DEFAULT_RESOLUTION_PREFERENCE, + getString(R.string.default_resolution_value))); + defaultAudioFormatPreference.setSummary( + defaultPreferences.getString(DEFAULT_AUDIO_FORMAT_PREFERENCE, + getString(R.string.default_audio_format_value))); + searchLanguagePreference.setSummary( + defaultPreferences.getString(SEARCH_LANGUAGE_PREFERENCE, + getString(R.string.default_language_value))); + downloadPathPreference.setSummary( + defaultPreferences.getString(DOWNLOAD_PATH_PREFERENCE, + getString(R.string.download_path_summary))); + downloadPathAudioPreference.setSummary( + defaultPreferences.getString(DOWNLOAD_PATH_AUDIO_PREFERENCE, + getString(R.string.download_path_audio_summary))); + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + + } +} From b3fdd2b0cb7d66947ad51077aa7eef303c79f592 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 15 Jun 2016 12:52:31 -0300 Subject: [PATCH 09/10] Done with the file picker for the download settings --- .../org/schabi/newpipe/SettingsActivity.java | 25 ++++++++--- .../org/schabi/newpipe/SettingsFragment.java | 44 +++++++++++++++---- app/src/main/res/xml/settings.xml | 2 +- 3 files changed, 55 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/SettingsActivity.java b/app/src/main/java/org/schabi/newpipe/SettingsActivity.java index 5c5ec56c4..e9c4a4870 100644 --- a/app/src/main/java/org/schabi/newpipe/SettingsActivity.java +++ b/app/src/main/java/org/schabi/newpipe/SettingsActivity.java @@ -4,11 +4,13 @@ import android.app.Activity; import android.content.ClipData; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.content.res.Configuration; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.preference.PreferenceActivity; +import android.preference.PreferenceManager; import android.support.annotation.LayoutRes; import android.support.annotation.NonNull; import android.support.v7.app.ActionBar; @@ -63,13 +65,16 @@ public class SettingsActivity extends PreferenceActivity { getFragmentManager().beginTransaction() .replace(android.R.id.content, new SettingsFragment()) .commit(); - } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == 233 && resultCode == Activity.RESULT_OK) { + if ((requestCode == R.string.download_path_audio_key + || requestCode == R.string.download_path_key) + && resultCode == Activity.RESULT_OK) { + + Uri uri = null; if (data.getBooleanExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false)) { // For JellyBean and above if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { @@ -77,7 +82,7 @@ public class SettingsActivity extends PreferenceActivity { if (clip != null) { for (int i = 0; i < clip.getItemCount(); i++) { - Uri uri = clip.getItemAt(i).getUri(); + uri = clip.getItemAt(i).getUri(); } } // For Ice Cream Sandwich @@ -87,14 +92,22 @@ public class SettingsActivity extends PreferenceActivity { if (paths != null) { for (String path: paths) { - Uri uri = Uri.parse(path); + uri = Uri.parse(path); } } } - } else { - Uri uri = data.getData(); + uri = data.getData(); } + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + + //requestCode is equal to R.string.download_path_key or + //R.string.download_path_audio_key + String key = getString(requestCode); + String path = data.getData().toString().substring(7); + prefs.edit() + .putString(key, path) + .commit(); } else if(requestCode == REQUEST_INSTALL_ORBOT) { diff --git a/app/src/main/java/org/schabi/newpipe/SettingsFragment.java b/app/src/main/java/org/schabi/newpipe/SettingsFragment.java index 061a6671d..4f2bc4613 100644 --- a/app/src/main/java/org/schabi/newpipe/SettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/SettingsFragment.java @@ -36,7 +36,7 @@ public class SettingsFragment extends PreferenceFragment private ListPreference defaultAudioFormatPreference; private ListPreference searchLanguagePreference; private Preference downloadPathPreference; - private EditTextPreference downloadPathAudioPreference; + private Preference downloadPathAudioPreference; private CheckBoxPreference useTorCheckBox; private SharedPreferences defaultPreferences; @@ -66,8 +66,7 @@ public class SettingsFragment extends PreferenceFragment searchLanguagePreference = (ListPreference) findPreference(SEARCH_LANGUAGE_PREFERENCE); downloadPathPreference = findPreference(DOWNLOAD_PATH_PREFERENCE); - downloadPathAudioPreference = - (EditTextPreference) findPreference(DOWNLOAD_PATH_AUDIO_PREFERENCE); + downloadPathAudioPreference = findPreference(DOWNLOAD_PATH_AUDIO_PREFERENCE); useTorCheckBox = (CheckBoxPreference) findPreference(USE_TOR_KEY); prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() { @@ -75,8 +74,12 @@ public class SettingsFragment extends PreferenceFragment public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { Activity a = getActivity(); - - if(a != null) { + if(a == null) + { + return; + } + if (key == USE_TOR_KEY) + { updateSummary(); if (defaultPreferences.getBoolean(USE_TOR_KEY, false)) { @@ -91,17 +94,34 @@ public class SettingsFragment extends PreferenceFragment App.configureTor(false); } } + else if (key == DOWNLOAD_PATH_PREFERENCE) + { + String downloadPath = sharedPreferences + .getString(DOWNLOAD_PATH_PREFERENCE, + getString(R.string.download_path_summary)); + downloadPathPreference + .setSummary(downloadPath); + } + else if (key == DOWNLOAD_PATH_AUDIO_PREFERENCE) + { + String downloadPath = sharedPreferences + .getString(DOWNLOAD_PATH_AUDIO_PREFERENCE, + getString(R.string.download_path_audio_summary)); + downloadPathAudioPreference + .setSummary(downloadPath); + } } }; defaultPreferences.registerOnSharedPreferenceChangeListener(prefListener); - updateSummary(); } @Override public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { - if(preference.getKey() == downloadPathPreference.getKey()) + + if(preference.getKey() == downloadPathPreference.getKey() || + preference.getKey() == downloadPathAudioPreference.getKey()) { Activity activity = getActivity(); Intent i = new Intent(activity, FilePickerActivity.class); @@ -109,8 +129,14 @@ public class SettingsFragment extends PreferenceFragment i.putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false); i.putExtra(FilePickerActivity.EXTRA_ALLOW_CREATE_DIR, true); i.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_DIR); - - activity.startActivityForResult(i, 233); + if(preference.getKey() == downloadPathPreference.getKey()) + { + activity.startActivityForResult(i, R.string.download_path_key); + } + else if (preference.getKey() == downloadPathAudioPreference.getKey()) + { + activity.startActivityForResult(i, R.string.download_path_audio_key); + } } return super.onPreferenceTreeClick(preferenceScreen, preference); diff --git a/app/src/main/res/xml/settings.xml b/app/src/main/res/xml/settings.xml index ba46d353f..f3c53ec36 100644 --- a/app/src/main/res/xml/settings.xml +++ b/app/src/main/res/xml/settings.xml @@ -98,7 +98,7 @@ android:summary="@string/download_path_summary" android:dialogTitle="@string/download_path_dialog_title" /> - Date: Thu, 16 Jun 2016 07:58:09 -0300 Subject: [PATCH 10/10] A little cleanup --- app/src/main/java/org/schabi/newpipe/SettingsFragment.java | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/org/schabi/newpipe/SettingsFragment.java b/app/src/main/java/org/schabi/newpipe/SettingsFragment.java index 4f2bc4613..916d9d6b1 100644 --- a/app/src/main/java/org/schabi/newpipe/SettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/SettingsFragment.java @@ -5,7 +5,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.CheckBoxPreference; -import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceFragment;