diff --git a/app/src/main/java/info/nerull7/mysqlbrowser/EntriesFragment.java b/app/src/main/java/info/nerull7/mysqlbrowser/EntriesFragment.java index f7811fe..c0d32c1 100644 --- a/app/src/main/java/info/nerull7/mysqlbrowser/EntriesFragment.java +++ b/app/src/main/java/info/nerull7/mysqlbrowser/EntriesFragment.java @@ -6,6 +6,7 @@ import android.content.Context; import android.graphics.Canvas; import android.graphics.Typeface; import android.os.Bundle; +import android.preference.PreferenceManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -61,7 +62,7 @@ public class EntriesFragment extends Fragment implements AsyncDatabaseConnector. tableName = getArguments().getString(Static.TABLE_NAME_ARG); page = getArguments().getInt(Static.PAGE_ARG); - entriesLimit = getActivity().getSharedPreferences(SettingsFragment.PREFERENCE_FILE, Context.MODE_PRIVATE).getInt(SettingsFragment.ENTRIES_PAGE_LIMIT, SettingsFragment.ENTRIES_PAGE_LIMIT_DEF); + entriesLimit = PreferenceManager.getDefaultSharedPreferences(getActivity()).getInt(SettingsFragment.ENTRIES_PAGE_LIMIT, SettingsFragment.ENTRIES_PAGE_LIMIT_DEF); } private void initViewItems(View rootView){ diff --git a/app/src/main/java/info/nerull7/mysqlbrowser/LoginFragment.java b/app/src/main/java/info/nerull7/mysqlbrowser/LoginFragment.java index efb54b4..c885ce4 100644 --- a/app/src/main/java/info/nerull7/mysqlbrowser/LoginFragment.java +++ b/app/src/main/java/info/nerull7/mysqlbrowser/LoginFragment.java @@ -2,9 +2,13 @@ package info.nerull7.mysqlbrowser; import android.app.AlertDialog; import android.app.Fragment; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.content.SharedPreferences; import android.os.Bundle; +import android.preference.Preference; +import android.preference.PreferenceManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -36,10 +40,21 @@ public class LoginFragment extends Fragment implements View.OnClickListener, Asy loginTextbox = (EditText) rootView.findViewById(R.id.editLogin); passwordTextbox = (EditText) rootView.findViewById(R.id.editPassowrd); progressBar = (ProgressBar) rootView.findViewById(R.id.loginProgressBar); + processCredentials(); return rootView; } + private void processCredentials() { + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); + + if(sharedPreferences.getBoolean(SettingsFragment.SAVE_SERVER_CREDENTIALS, false)){ + urlTextbox.setText(sharedPreferences.getString(SettingsFragment.URL_CREDENTIALS, null)); + loginTextbox.setText(sharedPreferences.getString(SettingsFragment.LOGIN_CREDENTIALS, null)); + passwordTextbox.setText(sharedPreferences.getString(SettingsFragment.PASSWORD_CREDENTIALS, null)); + } + } + @Override public void onClick(View view) { progressBar.setVisibility(View.VISIBLE); diff --git a/app/src/main/java/info/nerull7/mysqlbrowser/SettingsFragment.java b/app/src/main/java/info/nerull7/mysqlbrowser/SettingsFragment.java index 2ae1484..4379169 100644 --- a/app/src/main/java/info/nerull7/mysqlbrowser/SettingsFragment.java +++ b/app/src/main/java/info/nerull7/mysqlbrowser/SettingsFragment.java @@ -1,9 +1,9 @@ package info.nerull7.mysqlbrowser; -import android.app.Activity; -import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; +import android.preference.CheckBoxPreference; +import android.preference.EditTextPreference; import android.preference.Preference; import android.preference.PreferenceFragment; import android.preference.PreferenceScreen; @@ -11,9 +11,12 @@ import android.preference.PreferenceScreen; /** * Created by nerull7 on 18.07.14. */ -public class SettingsFragment extends PreferenceFragment implements NumberPickerDialog.OnNumberSetListener { - public static final String PREFERENCE_FILE = "preferences"; +public class SettingsFragment extends PreferenceFragment implements NumberPickerDialog.OnNumberSetListener, Preference.OnPreferenceClickListener { public static final String ENTRIES_PAGE_LIMIT = "entries_limit"; + public static final String SAVE_SERVER_CREDENTIALS = "save_credentials_enabled"; + public static final String URL_CREDENTIALS = "url"; + public static final String LOGIN_CREDENTIALS = "login"; + public static final String PASSWORD_CREDENTIALS = "password"; public static final int ENTRIES_PAGE_LIMIT_DEF = 20; public static final int ENTRIES_MIN_PAGE = 20; @@ -21,19 +24,57 @@ public class SettingsFragment extends PreferenceFragment implements NumberPicker private SharedPreferences preferences; private Preference mEntriesLimit; + private CheckBoxPreference saveCredentials; + private EditTextPreference connectorUrlCredentials; + private EditTextPreference loginCredentials; + private EditTextPreference passwordCredentials; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); - preferences = getActivity().getSharedPreferences(PREFERENCE_FILE, Context.MODE_PRIVATE); + preferences = getPreferenceManager().getDefaultSharedPreferences(getActivity()); + loadPrefs(); } private void loadPrefs(){ addPreferencesFromResource(R.xml.settings); + // Getting fields mEntriesLimit = findPreference(ENTRIES_PAGE_LIMIT); + saveCredentials = (CheckBoxPreference) findPreference(SAVE_SERVER_CREDENTIALS); + connectorUrlCredentials = (EditTextPreference) findPreference(URL_CREDENTIALS); + loginCredentials = (EditTextPreference) findPreference(LOGIN_CREDENTIALS); + passwordCredentials = (EditTextPreference) findPreference(PASSWORD_CREDENTIALS); // TODO: Some encryption + + // Settings fields setEntriesPageLimitSummary(); + + // Settings Listener + saveCredentials.setOnPreferenceClickListener(this); + } + + private void setSaveServerCredentials(boolean isEnabled){ + SharedPreferences.Editor editor = preferences.edit(); + + if(!isEnabled){ // Cleaning Credentials + editor.remove(URL_CREDENTIALS); + editor.remove(LOGIN_CREDENTIALS); + editor.remove(PASSWORD_CREDENTIALS); + } + editor.putBoolean(SAVE_SERVER_CREDENTIALS, isEnabled); + editor.apply(); + editor.commit(); + + if(!isEnabled) + reloadLoginPrefsView(); + } + + private void reloadLoginPrefsView(){ // Update values + saveCredentials.setChecked(preferences.getBoolean(SAVE_SERVER_CREDENTIALS, false)); + connectorUrlCredentials.setText(preferences.getString(URL_CREDENTIALS, null)); + loginCredentials.setText(preferences.getString(LOGIN_CREDENTIALS, null)); + passwordCredentials.setText(preferences.getString(PASSWORD_CREDENTIALS, null));; } private int getEntriesPageLimit(){ @@ -60,4 +101,15 @@ public class SettingsFragment extends PreferenceFragment implements NumberPicker editor.apply(); setEntriesPageLimitSummary(); } + + @Override + public boolean onPreferenceClick(Preference preference) { + if(preference==saveCredentials){ + setSaveServerCredentials(saveCredentials.isChecked()); + } + else + return false; + + return true; + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 329bfdb..ff22c47 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -27,5 +27,8 @@ No available databases Previous Next + Login credentials + Save credentials + Connector URL diff --git a/app/src/main/res/xml/settings.xml b/app/src/main/res/xml/settings.xml index d25f802..3cd5391 100644 --- a/app/src/main/res/xml/settings.xml +++ b/app/src/main/res/xml/settings.xml @@ -9,5 +9,29 @@ android:summary="@string/entries_summary" /> + + + + + + + + + + \ No newline at end of file