Save Credentials

Now you can save credentials in settings so you don't have to enter them every time
This commit is contained in:
Przemek Grondek 2014-07-28 14:32:07 +02:00
parent 5d822098fe
commit 83b5ab3407
5 changed files with 101 additions and 6 deletions

View file

@ -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){

View file

@ -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);

View file

@ -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;
}
}

View file

@ -27,5 +27,8 @@
<string name="error_no_databases">No available databases</string>
<string name="action_previous">Previous</string>
<string name="action_next">Next</string>
<string name="login_settings">Login credentials</string>
<string name="save_credentials">Save credentials</string>
<string name="connector_url">Connector URL</string>
</resources>

View file

@ -9,5 +9,29 @@
android:summary="@string/entries_summary"
/>
</PreferenceCategory>
<PreferenceCategory
android:title="@string/login_settings">
<CheckBoxPreference
android:key="save_credentials_enabled"
android:title="@string/save_credentials"/>
<EditTextPreference
android:key="url"
android:title="@string/connector_url"
android:inputType="textUri"
android:dependency="save_credentials_enabled"/>
<EditTextPreference
android:key="login"
android:title="@string/username"
android:dependency="save_credentials_enabled"/>
<EditTextPreference
android:key="password"
android:title="@string/password"
android:password="true"
android:dependency="save_credentials_enabled"/>
</PreferenceCategory>
</PreferenceScreen>