upgrade settings to AppCompatActivity (remove tablet view) / streamline preference access

This commit is contained in:
David Luhmer 2019-04-07 12:46:23 -03:00
parent 8af4210fc8
commit 26c90c228c
11 changed files with 437 additions and 605 deletions

View file

@ -36,14 +36,6 @@ import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.core.content.ContextCompat;
import androidx.core.view.GravityCompat;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
@ -74,6 +66,16 @@ import java.lang.reflect.Field;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.inject.Named;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.core.view.GravityCompat;
import androidx.customview.widget.ViewDragHelper;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.DialogFragment;
@ -81,6 +83,7 @@ import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.preference.PreferenceManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
@ -158,21 +161,25 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements
private static final String LIST_ADAPTER_TOTAL_COUNT = "LIST_ADAPTER_TOTAL_COUNT";
private static final String LIST_ADAPTER_PAGE_COUNT = "LIST_ADAPTER_PAGE_COUNT";
@Inject @Named("sharedPreferencesFileName") String sharedPreferencesFileName;
@Override
protected void onCreate(Bundle savedInstanceState) {
protected void onCreate(Bundle savedInstanceState) {
((NewsReaderApplication) getApplication()).getAppComponent().injectActivity(this);
SharedPreferences defaultValueSp = getSharedPreferences(PreferenceManager.KEY_HAS_SET_DEFAULT_VALUES, Context.MODE_PRIVATE);
if(!defaultValueSp.getBoolean(PreferenceManager.KEY_HAS_SET_DEFAULT_VALUES, false)) {
PreferenceManager.setDefaultValues(this, R.xml.pref_data_sync, true);
PreferenceManager.setDefaultValues(this, R.xml.pref_display, true);
PreferenceManager.setDefaultValues(this, R.xml.pref_general, true);
PreferenceManager.setDefaultValues(this, R.xml.pref_notification, true);
PreferenceManager.setDefaultValues(this, sharedPreferencesFileName, Context.MODE_PRIVATE, R.xml.pref_data_sync, true);
PreferenceManager.setDefaultValues(this, sharedPreferencesFileName, Context.MODE_PRIVATE, R.xml.pref_display, true);
PreferenceManager.setDefaultValues(this, sharedPreferencesFileName, Context.MODE_PRIVATE, R.xml.pref_general, true);
PreferenceManager.setDefaultValues(this, sharedPreferencesFileName, Context.MODE_PRIVATE, R.xml.pref_notification, true);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newsreader);
setContentView(R.layout.activity_newsreader);
ButterKnife.bind(this);
ButterKnife.bind(this);
if (toolbar != null) {
setSupportActionBar(toolbar);
@ -259,6 +266,9 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements
if (tabletSize) {
showTapLogoToSyncShowcaseView();
}
Intent intent = new Intent(this, SettingsActivity.class);
startActivityForResult(intent, RESULT_SETTINGS);
}
/* (non-Javadoc)

View file

@ -273,6 +273,8 @@ public class NewsReaderListFragment extends Fragment implements OnCreateContextM
.show();
}
private static final String USER_INFO_STRING = "USER_INFO";
public void startAsyncTaskGetUserInfo() {
mApi.getAPI().user()
.subscribeOn(Schedulers.newThread())
@ -288,7 +290,9 @@ public class NewsReaderListFragment extends Fragment implements OnCreateContextM
Log.d(TAG, "onNext() called with: userInfo = [" + userInfo + "]");
try {
mPrefs.edit().putString("USER_INFO", NewsReaderListFragment.toString(userInfo)).apply();
String userInfoAsString = NewsReaderListFragment.toString(userInfo);
//Log.v(TAG, userInfoAsString);
mPrefs.edit().putString(USER_INFO_STRING, userInfoAsString).apply();
} catch (IOException e) {
e.printStackTrace();
}
@ -299,7 +303,7 @@ public class NewsReaderListFragment extends Fragment implements OnCreateContextM
Log.e(TAG, "onError() called with:", e);
if("Method Not Allowed".equals(e.getMessage())) { //Remove if old version is used
mPrefs.edit().remove("USER_INFO").apply();
mPrefs.edit().remove(USER_INFO_STRING).apply();
}
bindUserInfoToUI();
@ -313,11 +317,7 @@ public class NewsReaderListFragment extends Fragment implements OnCreateContextM
}
protected void bindUserInfoToUI() {
bindUserInfoToUI(false);
}
public void bindUserInfoToUI(boolean testMode) {
public void bindUserInfoToUI() {
if(getActivity() == null) { // e.g. Activity is closed
return;
}
@ -334,11 +334,7 @@ public class NewsReaderListFragment extends Fragment implements OnCreateContextM
userTextView.setText(mUsername);
urlTextView.setText(mOc_root_path);
if(testMode) { //Hide real url in test mode
urlTextView.setText("example.com/ownCloud");
}
String uInfo = mPrefs.getString("USER_INFO", null);
String uInfo = mPrefs.getString(USER_INFO_STRING, null);
if(uInfo == null)
return;

View file

@ -11,13 +11,9 @@ import android.os.Bundle;
import android.os.Handler;
import android.os.RemoteException;
import android.os.ResultReceiver;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import android.support.v4.media.MediaBrowserCompat;
import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.MediaSessionCompat;
import androidx.core.view.GravityCompat;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.util.TypedValue;
import android.view.SurfaceView;
@ -39,6 +35,10 @@ import java.lang.reflect.Proxy;
import javax.inject.Inject;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import butterknife.BindView;
import butterknife.ButterKnife;
import de.luhmer.owncloudnewsreader.ListView.SubscriptionExpandableListAdapter;
@ -104,11 +104,11 @@ public class PodcastFragmentActivity extends AppCompatActivity implements IPlayP
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
((NewsReaderApplication) getApplication()).getAppComponent().injectActivity(this);
ThemeChooser.getInstance(this).chooseTheme(this);
super.onCreate(savedInstanceState);
ThemeChooser.getInstance(this).afterOnCreate(this);
((NewsReaderApplication) getApplication()).getAppComponent().injectActivity(this);
if (mApi.getAPI() instanceof Proxy) { // Single Sign On
VersionCheckHelper.verifyMinVersion(this, MIN_NEXTCLOUD_FILES_APP_VERSION_CODE);

View file

@ -21,59 +21,16 @@
package de.luhmer.owncloudnewsreader;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DialogFragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.provider.ContactsContract;
import android.provider.Settings;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.appcompat.widget.AppCompatCheckedTextView;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatRadioButton;
import androidx.appcompat.widget.AppCompatSpinner;
import androidx.appcompat.widget.Toolbar;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.google.android.material.appbar.AppBarLayout;
import javax.inject.Inject;
import org.apache.velocity.util.ArrayListWrapper;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import androidx.preference.CheckBoxPreference;
import androidx.preference.DialogPreference;
import androidx.preference.EditTextPreference;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceFragment;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import androidx.preference.TwoStatePreference;
import de.luhmer.owncloudnewsreader.database.DatabaseConnectionOrm;
import de.luhmer.owncloudnewsreader.helper.AppCompatPreferenceActivity;
import de.luhmer.owncloudnewsreader.helper.ImageHandler;
import de.luhmer.owncloudnewsreader.helper.NewsFileUtils;
import de.luhmer.owncloudnewsreader.helper.PostDelayHandler;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import de.luhmer.owncloudnewsreader.helper.ThemeChooser;
/**
@ -133,20 +90,19 @@ public class SettingsActivity extends AppCompatActivity {
public static final String CB_VERSION = "cb_version";
private static EditTextPreference clearCachePref;
private static String version = "<loading>";
@Inject SharedPreferences mPrefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
((NewsReaderApplication) getApplication()).getAppComponent().injectActivity(this);
ThemeChooser.getInstance(this).chooseTheme(this);
super.onCreate(savedInstanceState);
ThemeChooser.getInstance(this).afterOnCreate(this);
setContentView(R.layout.activity_settings);
version = VersionInfoDialogFragment.getVersionString(this);
setupActionBar();
}
@ -156,7 +112,7 @@ public class SettingsActivity extends AppCompatActivity {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.container, new PhonePreferenceFragment())
.replace(R.id.container, new SettingsFragment())
.commit();
}
@ -170,73 +126,11 @@ public class SettingsActivity extends AppCompatActivity {
}
/**
* This fragment shows all preferences for phones.
*/
public static class PhonePreferenceFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
// In the simplified UI, fragments are not used at all and we instead
// use the older PreferenceActivity APIs.
// This is to initialize the settings panel to allow adding a first-section header (below)
// without running into stupid exceptions (otherwise getPreferenceScreen() will return null)
addPreferencesFromResource(R.xml.pref_empty);
// Add 'general' preferences.
PreferenceCategory header = new PreferenceCategory(getContext());
header.setTitle(R.string.pref_header_general);
getPreferenceScreen().addPreference(header);
addPreferencesFromResource(R.xml.pref_general);
//header = new PreferenceCategory(getContext());
//header.setTitle(R.string.pref_header_display);
//getPreferenceScreen().addPreference(header);
addPreferencesFromResource(R.xml.pref_display);
//header = new PreferenceCategory(getContext());
//header.setTitle(R.string.pref_header_data_sync);
//getPreferenceScreen().addPreference(header);
addPreferencesFromResource(R.xml.pref_data_sync);
//header = new PreferenceCategory(getContext());
//header.setTitle(R.string.pref_header_notifications);
//getPreferenceScreen().addPreference(header);
addPreferencesFromResource(R.xml.pref_notification);
//header = new PreferenceCategory(getContext());
//header.setTitle(R.string.pref_header_about);
//getPreferenceScreen().addPreference(header);
addPreferencesFromResource(R.xml.pref_about);
//header = new PreferenceCategory(this);
//header.setTitle(R.string.pref_header_podcast);
//getPreferenceScreen().addPreference(header);
//addPreferencesFromResource(R.xml.pref_podcast);
bindGeneralPreferences(this);
bindDisplayPreferences(this);
bindDataSyncPreferences(this);
bindNotificationPreferences(this);
bindAboutPreferences(this, getActivity());
//bindPodcastPreferences(this);
}
}
/* (non-Javadoc)
* @see com.actionbarsherlock.app.SherlockPreferenceActivity#onOptionsItemSelected(com.actionbarsherlock.view.MenuItem)
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
//NavUtils.navigateUpTo(this, new Intent(this,
// NewsReaderListActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
@ -248,407 +142,16 @@ public class SettingsActivity extends AppCompatActivity {
Intent intent = getIntent();
intent.putExtra(
SettingsActivity.SP_FEED_LIST_LAYOUT,
PreferenceManager.getDefaultSharedPreferences(this).getString(SettingsActivity.SP_FEED_LIST_LAYOUT, "0")
mPrefs.getString(SettingsActivity.SP_FEED_LIST_LAYOUT, "0")
);
setResult(RESULT_OK,intent);
}
///** {@inheritDoc} */
//@Override
public boolean onIsMultiPane() {
return this.getResources().getBoolean(R.bool.isTablet);
}
/*
@Override
public void onBuildHeaders(List<PreferenceActivity.Header> target) {
super.onBuildHeaders(target);
if (onIsMultiPane()) {
loadHeadersFromResource(R.xml.pref_headers, target);
// Fix settings page header ("breadcrumb") text color for dark mode
// Thank you Stackoverflow: https://stackoverflow.com/a/27078485
final View breadcrumb = findViewById(android.R.id.title);
if (breadcrumb == null) {
// Single pane layout
return;
}
try {
final Field titleColor = breadcrumb.getClass().getDeclaredField("mTextColor");
titleColor.setAccessible(true);
titleColor.setInt(breadcrumb, ContextCompat.getColor(this, R.color.primaryTextColor));
} catch (final Exception e) {
Log.e(TAG, "onBuildHeaders failed", e);
}
}
}
@Override
protected boolean isValidFragment(String fragmentName) {
return true;
}
*/
/**
* A preference value change listener that updates the preference's summary
* to reflect its new value.
*/
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = (preference, value) -> {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);
// only enable black-bg setting if light or auto theme is selected
if(SP_APP_THEME.equals(preference.getKey())) {
if (value.equals("1")) // value "1" means Light theme
preference.getPreferenceManager().findPreference(CB_OLED_MODE).setEnabled(false);
else
preference.getPreferenceManager().findPreference(CB_OLED_MODE).setEnabled(true);
}
} else {
String key = preference.getKey();
// For all other preferences, set the summary to the value's
// simple string representation.
if(key.equals(EDT_PASSWORD_STRING))
preference.setSummary(null);
else
preference.setSummary(stringValue);
}
return true;
};
private static Preference.OnPreferenceChangeListener sBindPreferenceBooleanToValueListener = (preference, newValue) -> {
if(preference instanceof CheckBoxPreference) { //For legacy Android support
CheckBoxPreference cbPreference = ((CheckBoxPreference) preference);
cbPreference.setChecked((Boolean) newValue);
} else {
TwoStatePreference twoStatePreference = ((TwoStatePreference) preference);
twoStatePreference.setChecked((Boolean) newValue);
}
return true;
};
/**
* Binds a preference's summary to its value. More specifically, when the
* preference's value is changed, its summary (line of text below the
* preference title) is updated to reflect the value. The summary is also
* immediately updated upon calling this method. The exact display format is
* dependent on the type of preference.
*
* @see #sBindPreferenceSummaryToValueListener
*/
private static void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(
preference,
PreferenceManager.getDefaultSharedPreferences(
preference.getContext()).getString(preference.getKey(),
""));
}
private static void bindPreferenceBooleanToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreferenceBooleanToValueListener);
// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceBooleanToValueListener.onPreferenceChange(
preference,
PreferenceManager.getDefaultSharedPreferences(
preference.getContext()).getBoolean(preference.getKey(), false));
}
// TODO DO WE NEED THE CODE BELOW?!!
/*
@Nullable
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
// Allow super to try and create a view first
final View result = super.onCreateView(name, context, attrs);
if (result != null) {
return result;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
// If we're running pre-L, we need to 'inject' our tint aware Views in place of the
// standard framework versions
switch (name) {
case "EditText":
return new AppCompatEditText(this, attrs);
case "Spinner":
return new AppCompatSpinner(this, attrs);
case "CheckBox":
return new AppCompatCheckBox(this, attrs);
case "RadioButton":
return new AppCompatRadioButton(this, attrs);
case "CheckedTextView":
return new AppCompatCheckedTextView(this, attrs);
default:
Log.v(TAG, "Error. Didn't find view of type: " + name);
}
}
return null;
}
*/
/**
* This fragment shows general preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
public static class EmptyPreferenceFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.pref_empty, rootKey);
}
}
/**
* This fragment shows general preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
public static class GeneralPreferenceFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.pref_general, rootKey);
bindGeneralPreferences(this);
}
}
/**
* This fragment shows podcast preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
/*
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class PodcastPreferenceFragment extends PreferenceFragmentCompar {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.pref_podcast);
bindPodcastPreferences(this);
}
}
*/
/**
* This fragment shows notification preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
public static class NotificationPreferenceFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.pref_notification, rootKey);
bindNotificationPreferences(this);
}
}
/**
* This fragment shows data and sync preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
public static class DataSyncPreferenceFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.pref_data_sync, rootKey);
bindDataSyncPreferences(this);
}
}
/**
* This fragment shows data and sync preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
public static class DisplayPreferenceFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.pref_display, rootKey);
bindDisplayPreferences(this);
}
}
/**
* This fragment shows about preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
public static class AboutPreferenceFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.pref_about, rootKey);
bindAboutPreferences(this, getActivity());
}
}
@SuppressWarnings("deprecation")
private static void bindDisplayPreferences(DialogPreference.TargetFragment prefFrag)
{
bindPreferenceSummaryToValue(prefFrag.findPreference(SP_APP_THEME));
bindPreferenceBooleanToValue(prefFrag.findPreference(CB_OLED_MODE));
bindPreferenceSummaryToValue(prefFrag.findPreference(SP_FEED_LIST_LAYOUT));
bindPreferenceSummaryToValue(prefFrag.findPreference(SP_FONT_SIZE));
bindPreferenceSummaryToValue(prefFrag.findPreference(SP_DISPLAY_BROWSER));
}
@SuppressWarnings("deprecation")
private static void bindGeneralPreferences(DialogPreference.TargetFragment prefFrag)
{
/*
bindPreferenceSummaryToValue(prefFrag.findPreference(EDT_USERNAME_STRING));
bindPreferenceSummaryToValue(prefFrag.findPreference(EDT_PASSWORD_STRING));
bindPreferenceSummaryToValue(prefFrag.findPreference(EDT_OWNCLOUDROOTPATH_STRING));
*/
//bindPreferenceBooleanToValue(prefFrag.findPreference(CB_ALLOWALLSSLCERTIFICATES_STRING));
bindPreferenceBooleanToValue(prefFrag.findPreference(CB_SYNCONSTARTUP_STRING));
bindPreferenceBooleanToValue(prefFrag.findPreference(CB_SHOWONLYUNREAD_STRING));
bindPreferenceBooleanToValue(prefFrag.findPreference(CB_NAVIGATE_WITH_VOLUME_BUTTONS_STRING));
bindPreferenceBooleanToValue(prefFrag.findPreference(CB_MARK_AS_READ_WHILE_SCROLLING_STRING));
bindPreferenceBooleanToValue(prefFrag.findPreference(CB_SKIP_DETAILVIEW_AND_OPEN_BROWSER_DIRECTLY_STRING));
bindPreferenceSummaryToValue(prefFrag.findPreference(SP_SORT_ORDER));
bindPreferenceSummaryToValue(prefFrag.findPreference(SP_SEARCH_IN));
}
@SuppressWarnings("deprecation")
private static void bindDataSyncPreferences(final DialogPreference.TargetFragment prefFrag)
{
String[] authorities = { "de.luhmer.owncloudnewsreader" };
Intent intentSyncSettings = new Intent(Settings.ACTION_SYNC_SETTINGS);
intentSyncSettings.putExtra(Settings.EXTRA_AUTHORITIES, authorities);
prefFrag.findPreference(PREF_SYNC_SETTINGS).setIntent(intentSyncSettings);
//bindPreferenceSummaryToValue(prefFrag.findPreference(SP_MAX_ITEMS_SYNC));
clearCachePref = prefFrag.findPreference(EDT_CLEAR_CACHE);
bindPreferenceSummaryToValue(prefFrag.findPreference(LV_CACHE_IMAGES_OFFLINE_STRING));
bindPreferenceSummaryToValue(prefFrag.findPreference(SP_MAX_CACHE_SIZE));
clearCachePref.setOnPreferenceClickListener(preference -> {
//((EditTextPreference) preference).getDialog().dismiss(); // TODO we need this line again!!!!!!!!!!!!
//mPrefs. // TODO INJECT MPREFS HERE !!!!!! FIX LINE BELOW!!
//PreferenceManager.getDefaultSharedPreferences(prefFrag.getActivity()).edit().remove("USER_INFO").apply();
checkForUnsycedChangesInDatabaseAndResetDatabase(_mActivity);
return false;
});
}
private static void bindNotificationPreferences(DialogPreference.TargetFragment prefFrag)
{
bindPreferenceBooleanToValue(prefFrag.findPreference(CB_SHOW_NOTIFICATION_NEW_ARTICLES_STRING));
}
private static void bindAboutPreferences(final DialogPreference.TargetFragment prefFrag, Activity activity) {
prefFrag.findPreference(CB_VERSION).setSummary(version);
Preference changelogPreference = prefFrag.findPreference(CB_VERSION);
changelogPreference.setOnPreferenceClickListener(preference -> {
DialogFragment dialog = new VersionInfoDialogFragment();
dialog.show(activity.getFragmentManager(), "VersionChangelogDialogFragment");
return true;
});
}
private static void bindPodcastPreferences(DialogPreference.TargetFragment prefFrag)
{
//bindPreferenceBooleanToValue(prefFrag.findPreference(CB_ENABLE_PODCASTS_STRING));
}
public static void checkForUnsycedChangesInDatabaseAndResetDatabase(final Context context) {
DatabaseConnectionOrm dbConn = new DatabaseConnectionOrm(context);
boolean resetDatabase = true;
if(dbConn.areThereAnyUnsavedChangesInDatabase())
resetDatabase = false;
if(resetDatabase) {
new ResetDatabaseAsyncTask(context).execute();
} else {
new AlertDialog.Builder(context)
.setTitle(context.getString(R.string.warning))
.setMessage(context.getString(R.string.reset_cache_unsaved_changes))
.setPositiveButton(context.getString(android.R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
PostDelayHandler pDelayHandler = new PostDelayHandler(context);
pDelayHandler.stopRunningPostDelayHandler();
new ResetDatabaseAsyncTask(context).execute();
}
})
.setNegativeButton(context.getString(android.R.string.no), null)
.create()
.show();
}
}
public static class ResetDatabaseAsyncTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog pd;
private Context context;
public ResetDatabaseAsyncTask(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
pd = new ProgressDialog(context);
pd.setIndeterminate(true);
pd.setCancelable(false);
pd.setTitle(context.getString(R.string.dialog_clearing_cache));
pd.setMessage(context.getString(R.string.dialog_clearing_cache_please_wait));
pd.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
DatabaseConnectionOrm dbConn = new DatabaseConnectionOrm(context);
dbConn.resetDatabase();
ImageHandler.clearCache();
NewsFileUtils.clearWebArchiveCache(context);
NewsFileUtils.clearPodcastCache(context);
return null;
}
@Override
protected void onPostExecute(Void result) {
if(context instanceof Activity) {
Intent intent = ((Activity) context).getIntent();
intent.putExtra(SettingsActivity.CACHE_CLEARED, true);
((Activity) context).setResult(RESULT_OK,intent);
}
pd.dismiss();
Toast.makeText(context, context.getString(R.string.cache_is_cleared), Toast.LENGTH_SHORT).show();
super.onPostExecute(result);
}
}
}

View file

@ -0,0 +1,362 @@
package de.luhmer.owncloudnewsreader;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DialogFragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;
import javax.inject.Inject;
import androidx.preference.CheckBoxPreference;
import androidx.preference.DialogPreference;
import androidx.preference.EditTextPreference;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.TwoStatePreference;
import de.luhmer.owncloudnewsreader.database.DatabaseConnectionOrm;
import de.luhmer.owncloudnewsreader.helper.ImageHandler;
import de.luhmer.owncloudnewsreader.helper.NewsFileUtils;
import de.luhmer.owncloudnewsreader.helper.PostDelayHandler;
import static android.app.Activity.RESULT_OK;
import static de.luhmer.owncloudnewsreader.SettingsActivity.CB_MARK_AS_READ_WHILE_SCROLLING_STRING;
import static de.luhmer.owncloudnewsreader.SettingsActivity.CB_NAVIGATE_WITH_VOLUME_BUTTONS_STRING;
import static de.luhmer.owncloudnewsreader.SettingsActivity.CB_OLED_MODE;
import static de.luhmer.owncloudnewsreader.SettingsActivity.CB_SHOWONLYUNREAD_STRING;
import static de.luhmer.owncloudnewsreader.SettingsActivity.CB_SHOW_NOTIFICATION_NEW_ARTICLES_STRING;
import static de.luhmer.owncloudnewsreader.SettingsActivity.CB_SKIP_DETAILVIEW_AND_OPEN_BROWSER_DIRECTLY_STRING;
import static de.luhmer.owncloudnewsreader.SettingsActivity.CB_SYNCONSTARTUP_STRING;
import static de.luhmer.owncloudnewsreader.SettingsActivity.CB_VERSION;
import static de.luhmer.owncloudnewsreader.SettingsActivity.EDT_CLEAR_CACHE;
import static de.luhmer.owncloudnewsreader.SettingsActivity.EDT_PASSWORD_STRING;
import static de.luhmer.owncloudnewsreader.SettingsActivity.LV_CACHE_IMAGES_OFFLINE_STRING;
import static de.luhmer.owncloudnewsreader.SettingsActivity.PREF_SYNC_SETTINGS;
import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_APP_THEME;
import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_DISPLAY_BROWSER;
import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_FEED_LIST_LAYOUT;
import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_FONT_SIZE;
import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_MAX_CACHE_SIZE;
import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_SEARCH_IN;
import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_SORT_ORDER;
public class SettingsFragment extends PreferenceFragmentCompat {
@Inject SharedPreferences mPrefs;
private static String version = "<loading>";
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
((NewsReaderApplication) getActivity().getApplication()).getAppComponent().injectFragment(this);
version = VersionInfoDialogFragment.getVersionString(getActivity());
addPreferencesFromResource(R.xml.pref_general);
bindGeneralPreferences(this);
addPreferencesFromResource(R.xml.pref_display);
bindDisplayPreferences(this);
addPreferencesFromResource(R.xml.pref_data_sync);
bindDataSyncPreferences(this);
addPreferencesFromResource(R.xml.pref_notification);
bindNotificationPreferences(this);
addPreferencesFromResource(R.xml.pref_about);
bindAboutPreferences(this);
//addPreferencesFromResource(R.xml.pref_podcast);
//bindPodcastPreferences(this);
/*
// Fix settings page header ("breadcrumb") text color for dark mode
// Thank you Stackoverflow: https://stackoverflow.com/a/27078485
final View breadcrumb = findViewById(android.R.id.title);
if (breadcrumb == null) {
// Single pane layout
return;
}
try {
final Field titleColor = breadcrumb.getClass().getDeclaredField("mTextColor");
titleColor.setAccessible(true);
titleColor.setInt(breadcrumb, ContextCompat.getColor(this, R.color.primaryTextColor));
} catch (final Exception e) {
Log.e(TAG, "onBuildHeaders failed", e);
}
*/
}
/**
* A preference value change listener that updates the preference's summary
* to reflect its new value.
*/
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = (preference, value) -> {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);
// only enable black-bg setting if light or auto theme is selected
if(SP_APP_THEME.equals(preference.getKey())) {
if (value.equals("1")) // value "1" means Light theme
preference.getPreferenceManager().findPreference(CB_OLED_MODE).setEnabled(false);
else
preference.getPreferenceManager().findPreference(CB_OLED_MODE).setEnabled(true);
}
} else {
String key = preference.getKey();
// For all other preferences, set the summary to the value's
// simple string representation.
if(key.equals(EDT_PASSWORD_STRING))
preference.setSummary(null);
else
preference.setSummary(stringValue);
}
return true;
};
private static Preference.OnPreferenceChangeListener sBindPreferenceBooleanToValueListener = (preference, newValue) -> {
if(preference instanceof CheckBoxPreference) { //For legacy Android support
CheckBoxPreference cbPreference = ((CheckBoxPreference) preference);
cbPreference.setChecked((Boolean) newValue);
} else {
TwoStatePreference twoStatePreference = ((TwoStatePreference) preference);
twoStatePreference.setChecked((Boolean) newValue);
}
return true;
};
/**
* Binds a preference's summary to its value. More specifically, when the
* preference's value is changed, its summary (line of text below the
* preference title) is updated to reflect the value. The summary is also
* immediately updated upon calling this method. The exact display format is
* dependent on the type of preference.
*
* @see #sBindPreferenceSummaryToValueListener
*/
private void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(
preference,
mPrefs.getString(preference.getKey(),
""));
}
private void bindPreferenceBooleanToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreferenceBooleanToValueListener);
// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceBooleanToValueListener.onPreferenceChange(
preference,
mPrefs.getBoolean(preference.getKey(), false));
}
// TODO DO WE NEED THE CODE BELOW?!!
/*
@Nullable
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
// Allow super to try and create a view first
final View result = super.onCreateView(name, context, attrs);
if (result != null) {
return result;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
// If we're running pre-L, we need to 'inject' our tint aware Views in place of the
// standard framework versions
switch (name) {
case "EditText":
return new AppCompatEditText(this, attrs);
case "Spinner":
return new AppCompatSpinner(this, attrs);
case "CheckBox":
return new AppCompatCheckBox(this, attrs);
case "RadioButton":
return new AppCompatRadioButton(this, attrs);
case "CheckedTextView":
return new AppCompatCheckedTextView(this, attrs);
default:
Log.v(TAG, "Error. Didn't find view of type: " + name);
}
}
return null;
}
*/
private void bindDisplayPreferences(PreferenceFragmentCompat prefFrag)
{
bindPreferenceSummaryToValue(prefFrag.findPreference(SP_APP_THEME));
bindPreferenceBooleanToValue(prefFrag.findPreference(CB_OLED_MODE));
bindPreferenceSummaryToValue(prefFrag.findPreference(SP_FEED_LIST_LAYOUT));
bindPreferenceSummaryToValue(prefFrag.findPreference(SP_FONT_SIZE));
bindPreferenceSummaryToValue(prefFrag.findPreference(SP_DISPLAY_BROWSER));
}
private void bindGeneralPreferences(DialogPreference.TargetFragment prefFrag)
{
/*
bindPreferenceSummaryToValue(prefFrag.findPreference(EDT_USERNAME_STRING));
bindPreferenceSummaryToValue(prefFrag.findPreference(EDT_PASSWORD_STRING));
bindPreferenceSummaryToValue(prefFrag.findPreference(EDT_OWNCLOUDROOTPATH_STRING));
*/
//bindPreferenceBooleanToValue(prefFrag.findPreference(CB_ALLOWALLSSLCERTIFICATES_STRING));
bindPreferenceBooleanToValue(prefFrag.findPreference(CB_SYNCONSTARTUP_STRING));
bindPreferenceBooleanToValue(prefFrag.findPreference(CB_SHOWONLYUNREAD_STRING));
bindPreferenceBooleanToValue(prefFrag.findPreference(CB_NAVIGATE_WITH_VOLUME_BUTTONS_STRING));
bindPreferenceBooleanToValue(prefFrag.findPreference(CB_MARK_AS_READ_WHILE_SCROLLING_STRING));
bindPreferenceBooleanToValue(prefFrag.findPreference(CB_SKIP_DETAILVIEW_AND_OPEN_BROWSER_DIRECTLY_STRING));
bindPreferenceSummaryToValue(prefFrag.findPreference(SP_SORT_ORDER));
bindPreferenceSummaryToValue(prefFrag.findPreference(SP_SEARCH_IN));
}
private void bindDataSyncPreferences(final PreferenceFragmentCompat prefFrag)
{
String[] authorities = { "de.luhmer.owncloudnewsreader" };
Intent intentSyncSettings = new Intent(Settings.ACTION_SYNC_SETTINGS);
intentSyncSettings.putExtra(Settings.EXTRA_AUTHORITIES, authorities);
prefFrag.findPreference(PREF_SYNC_SETTINGS).setIntent(intentSyncSettings);
//bindPreferenceSummaryToValue(prefFrag.findPreference(SP_MAX_ITEMS_SYNC));
Preference clearCachePref = prefFrag.findPreference(EDT_CLEAR_CACHE);
bindPreferenceSummaryToValue(prefFrag.findPreference(LV_CACHE_IMAGES_OFFLINE_STRING));
bindPreferenceSummaryToValue(prefFrag.findPreference(SP_MAX_CACHE_SIZE));
clearCachePref.setOnPreferenceClickListener(preference -> {
mPrefs.edit().remove("USER_INFO").apply();
checkForUnsycedChangesInDatabaseAndResetDatabase(prefFrag.getActivity());
return true;
});
}
private void bindNotificationPreferences(PreferenceFragmentCompat prefFrag)
{
bindPreferenceBooleanToValue(prefFrag.findPreference(CB_SHOW_NOTIFICATION_NEW_ARTICLES_STRING));
}
private void bindAboutPreferences(final PreferenceFragmentCompat prefFrag) {
prefFrag.findPreference(CB_VERSION).setSummary(version);
Preference changelogPreference = prefFrag.findPreference(CB_VERSION);
changelogPreference.setOnPreferenceClickListener(preference -> {
DialogFragment dialog = new VersionInfoDialogFragment();
dialog.show(prefFrag.getActivity().getFragmentManager(), "VersionChangelogDialogFragment");
return true;
});
}
private void bindPodcastPreferences(PreferenceFragmentCompat prefFrag)
{
//bindPreferenceBooleanToValue(prefFrag.findPreference(CB_ENABLE_PODCASTS_STRING));
}
public void checkForUnsycedChangesInDatabaseAndResetDatabase(final Context context) {
DatabaseConnectionOrm dbConn = new DatabaseConnectionOrm(context);
boolean resetDatabase = true;
if(dbConn.areThereAnyUnsavedChangesInDatabase()) {
resetDatabase = false;
}
if(resetDatabase) {
new ResetDatabaseAsyncTask(context).execute();
} else {
new AlertDialog.Builder(context)
.setTitle(context.getString(R.string.warning))
.setMessage(context.getString(R.string.reset_cache_unsaved_changes))
.setPositiveButton(context.getString(android.R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
PostDelayHandler pDelayHandler = new PostDelayHandler(context);
pDelayHandler.stopRunningPostDelayHandler();
new ResetDatabaseAsyncTask(context).execute();
}
})
.setNegativeButton(context.getString(android.R.string.no), null)
.create()
.show();
}
}
public static class ResetDatabaseAsyncTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog pd;
private Context context;
public ResetDatabaseAsyncTask(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
pd = new ProgressDialog(context);
pd.setIndeterminate(true);
pd.setCancelable(false);
pd.setTitle(context.getString(R.string.dialog_clearing_cache));
pd.setMessage(context.getString(R.string.dialog_clearing_cache_please_wait));
pd.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
//Thread.sleep(1000);
DatabaseConnectionOrm dbConn = new DatabaseConnectionOrm(context);
dbConn.resetDatabase();
ImageHandler.clearCache();
NewsFileUtils.clearWebArchiveCache(context);
NewsFileUtils.clearPodcastCache(context);
return null;
}
@Override
protected void onPostExecute(Void result) {
if(context instanceof Activity) {
Intent intent = ((Activity) context).getIntent();
intent.putExtra(SettingsActivity.CACHE_CLEARED, true);
((Activity) context).setResult(RESULT_OK, intent);
}
pd.dismiss();
Toast.makeText(context, context.getString(R.string.cache_is_cleared), Toast.LENGTH_SHORT).show();
super.onPostExecute(result);
}
}
}

View file

@ -1,14 +1,14 @@
package de.luhmer.owncloudnewsreader.di;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import androidx.annotation.VisibleForTesting;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import javax.inject.Named;
import javax.inject.Singleton;
import dagger.Module;
@ -34,10 +34,18 @@ public class ApiModule {
// Dagger will only look for methods annotated with @Provides
@Provides
@Singleton
@VisibleForTesting
// Application reference must come from AppModule.class
public SharedPreferences providesSharedPreferences() {
return PreferenceManager.getDefaultSharedPreferences(mApplication);
SharedPreferences providesSharedPreferences() {
//return PreferenceManager.getDefaultSharedPreferences(mApplication);
return mApplication.getSharedPreferences(providesSharedPreferencesFileName(), Context.MODE_PRIVATE);
}
// Dagger will only look for methods annotated with @Provides
@Provides
@Named("sharedPreferencesFileName")
public String providesSharedPreferencesFileName() {
//return PreferenceManager.getDefaultSharedPreferencesName(mApplication);
return mApplication.getPackageName() + "_preferences";
}
/*

View file

@ -5,9 +5,12 @@ import javax.inject.Singleton;
import dagger.Component;
import de.luhmer.owncloudnewsreader.LoginDialogFragment;
import de.luhmer.owncloudnewsreader.NewFeedActivity;
import de.luhmer.owncloudnewsreader.NewsReaderListActivity;
import de.luhmer.owncloudnewsreader.NewsReaderListDialogFragment;
import de.luhmer.owncloudnewsreader.NewsReaderListFragment;
import de.luhmer.owncloudnewsreader.PodcastFragmentActivity;
import de.luhmer.owncloudnewsreader.SettingsActivity;
import de.luhmer.owncloudnewsreader.SettingsFragment;
import de.luhmer.owncloudnewsreader.authentication.OwnCloudSyncAdapter;
import de.luhmer.owncloudnewsreader.services.SyncItemStateService;
@ -19,12 +22,15 @@ import de.luhmer.owncloudnewsreader.services.SyncItemStateService;
@Component(modules = { ApiModule.class })
public interface AppComponent {
void injectActivity(NewsReaderListActivity activity);
void injectActivity(PodcastFragmentActivity activity);
void injectActivity(NewFeedActivity activity);
void injectActivity(SettingsActivity activity);
void injectFragment(NewsReaderListDialogFragment fragment);
void injectFragment(NewsReaderListFragment fragment);
void injectFragment(LoginDialogFragment fragment);
void injectFragment(SettingsFragment settingsFragment);
void injectService(SyncItemStateService service);
void injectService(OwnCloudSyncAdapter ownCloudSyncAdapter);

View file

@ -51,9 +51,11 @@ public class ImageHandler {
return links;
}
public static void clearCache()
{
ImageLoader.getInstance().clearDiskCache();
ImageLoader.getInstance().clearMemoryCache();
}
public static void clearCache()
{
if(ImageLoader.getInstance().isInited()) {
ImageLoader.getInstance().clearDiskCache();
ImageLoader.getInstance().clearMemoryCache();
}
}
}

View file

@ -43,10 +43,8 @@
android:title="@string/pref_title_data_sync_max_items" />
-->
<EditTextPreference
<Preference
android:key="edt_clearCache"
android:maxLines="1"
android:singleLine="true"
android:title="@string/pref_title_clearCache"
app:iconSpaceReserved="false"/>

View file

@ -1,3 +0,0 @@
<androidx.preference.PreferenceScreen>
<!-- This layout definition is EMPTY ON PURPOSE, it's needed to initialise a settings screen to add a first-section header to -->
</androidx.preference.PreferenceScreen>

View file

@ -1,50 +0,0 @@
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- These settings headers are only used on tablets. -->
<Preference
android:fragment="de.luhmer.owncloudnewsreader.SettingsActivity$EmptyPreferenceFragment"
android:title=""
app:iconSpaceReserved="false"/>
<Preference
android:fragment="de.luhmer.owncloudnewsreader.SettingsActivity$GeneralPreferenceFragment"
android:title="@string/pref_header_general"
app:iconSpaceReserved="false"/>
<Preference
android:fragment="de.luhmer.owncloudnewsreader.SettingsActivity$DisplayPreferenceFragment"
android:title="@string/pref_header_display"
app:iconSpaceReserved="false"/>
<Preference
android:fragment="de.luhmer.owncloudnewsreader.SettingsActivity$NotificationPreferenceFragment"
android:title="@string/pref_header_notifications"
app:iconSpaceReserved="false"/>
<Preference
android:fragment="de.luhmer.owncloudnewsreader.SettingsActivity$DataSyncPreferenceFragment"
android:title="@string/pref_header_data_sync"
app:iconSpaceReserved="false"/>
<Preference
android:fragment="de.luhmer.owncloudnewsreader.SettingsActivity$AboutPreferenceFragment"
android:title="@string/pref_header_about"
app:iconSpaceReserved="false"/>
<!--
<Preference
android:fragment="de.luhmer.owncloudnewsreader.SettingsActivity$PodcastPreferenceFragment"
android:title="@string/pref_header_podcast"
app:iconSpaceReserved="false"/>
-->
<!--
<Preference
android:fragment="de.luhmer.owncloudnewsreader.SettingsActivity$DataSyncPreferenceFragment"
android:title="@string/pref_header_data_sync"
app:iconSpaceReserved="false"/>
-->
</androidx.preference.PreferenceScreen>