diff --git a/app/src/main/java/com/claha/showtimeremote/NavigationDrawerActivity.java b/app/src/main/java/com/claha/showtimeremote/NavigationDrawerActivity.java index 485f5ae..ade2c81 100644 --- a/app/src/main/java/com/claha/showtimeremote/NavigationDrawerActivity.java +++ b/app/src/main/java/com/claha/showtimeremote/NavigationDrawerActivity.java @@ -83,7 +83,7 @@ public void onDrawerSlide(View drawerView, float slideOffset) { // if (savedInstanceState == null) { - selectFragment(0); + selectFragment(getStartPage()); } else { title = savedInstanceState.getCharSequence("title"); if (!savedInstanceState.getBoolean("isDrawerOpen")) { @@ -168,5 +168,9 @@ private void selectFragment(int position) { abstract protected int getAppName(); + protected int getStartPage() { + return 0; + } + } diff --git a/app/src/main/java/com/claha/showtimeremote/SettingsFragment.java b/app/src/main/java/com/claha/showtimeremote/SettingsFragment.java index f8933c7..3359621 100644 --- a/app/src/main/java/com/claha/showtimeremote/SettingsFragment.java +++ b/app/src/main/java/com/claha/showtimeremote/SettingsFragment.java @@ -1,14 +1,221 @@ package com.claha.showtimeremote; +import android.content.SharedPreferences; import android.os.Bundle; +import android.preference.EditTextPreference; +import android.preference.ListPreference; +import android.preference.Preference; +import android.preference.PreferenceCategory; import android.preference.PreferenceFragment; +import android.preference.PreferenceManager; +import android.preference.PreferenceScreen; +import android.util.Log; -public class SettingsFragment extends PreferenceFragment { +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class SettingsFragment extends PreferenceFragment implements Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener { + + private final String TAG = "SettingsFragment"; + + private final static int PROFILES = 0; + private final static int PROFILES_CHOOSE = 0; + private final static int PROFILES_ADD = 1; + private final static int PROFILES_DELETE = 2; + + private final static int NETWORK = 1; + private final static int NETWORK_IP_ADDRESS = 0; + private final static int NETWORK_PORT = 1; + + + private List profiles; + + private EditTextPreference profilesAdd; + private ListPreference profilesChoose; + private ListPreference profilesDelete; + + private EditTextPreference networkIPAddress; + private EditTextPreference networkPort; + + private SharedPreferences sharedPreferences; + + public SettingsFragment() { + profiles = new ArrayList<>(); + } @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.fragment_settings); + + // Shared preferences + sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); + + // Root + PreferenceScreen root = getPreferenceScreen(); + + // Profiles + PreferenceCategory profiles = (PreferenceCategory) root.getPreference(PROFILES); + + profilesChoose = (ListPreference) profiles.getPreference(PROFILES_CHOOSE); + profilesDelete = (ListPreference) profiles.getPreference(PROFILES_DELETE); + profilesAdd = (EditTextPreference) profiles.getPreference(PROFILES_ADD); + + profilesAdd.setOnPreferenceChangeListener(this); + profilesDelete.setOnPreferenceChangeListener(this); + profilesChoose.setOnPreferenceChangeListener(this); + + profilesAdd.setOnPreferenceClickListener(this); + + loadProfiles(); + updateProfiles(); + + // Network + PreferenceCategory network = (PreferenceCategory) root.getPreference(NETWORK); + + networkIPAddress = (EditTextPreference) network.getPreference(NETWORK_IP_ADDRESS); + networkPort = (EditTextPreference) network.getPreference(NETWORK_PORT); } + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + Log.d(TAG, "onPreferenceChange"); + + // Profile add + if (preference == profilesAdd) { + Log.d(TAG, "profiles_add"); + + String name = profilesAdd.getEditText().getText().toString(); + profiles.add(new Profile(name, networkIPAddress.getText(), networkPort.getText())); + + saveProfiles(); + updateProfiles(); + + profilesChoose.setValueIndex(profiles.size() - 1); + + return true; + + // Profile delete + } else if (preference == profilesDelete) { + String name = (String) newValue; + int index = profiles.indexOf(new Profile(name, "", "")); + profiles.remove(index); + + saveProfiles(); + updateProfiles(); + + return true; + + // Profile choose + } else if (preference == profilesChoose) { + String name = ((String) newValue).split("_")[0]; + int index = profiles.indexOf(new Profile(name, "", "")); + + updateNetwork(profiles.get(index)); + + return true; + } + + return false; + } + + private void updateProfiles() { + int N = profiles.size(); + + if (N > 0) { + CharSequence[] entriesAndEntryValues = new CharSequence[N]; + for (int i = 0; i < N; i++) { + entriesAndEntryValues[i] = "" + profiles.get(i).name; + } + + profilesChoose.setEntries(entriesAndEntryValues); + profilesDelete.setEntries(entriesAndEntryValues); + profilesChoose.setEntryValues(entriesAndEntryValues); + profilesDelete.setEntryValues(entriesAndEntryValues); + profilesChoose.setEnabled(true); + profilesDelete.setEnabled(true); + profilesDelete.setValue(null); + } else { + profilesChoose.setEnabled(false); + profilesDelete.setEnabled(false); + } + } + + private void updateNetwork(Profile profile) { + networkIPAddress.setText(profile.ipAddress); + networkPort.setText(profile.port); + } + + @Override + public boolean onPreferenceClick(Preference preference) { + if (preference == profilesAdd) { + profilesAdd.setText(""); + profilesAdd.getEditText().setText(""); + return true; + } + + return false; + } + + private void loadProfiles() { + Log.d(TAG, "saveProfiles"); + Set profilesToLoadTemp = sharedPreferences.getStringSet("profiles", new HashSet()); + List profilesToLoad = new ArrayList<>(profilesToLoadTemp); + Collections.sort(profilesToLoad); + Log.d(TAG, "number of profiles: " + profilesToLoad.size()); + profiles = new ArrayList<>(); + for (String profile : profilesToLoad) { + profiles.add(new Profile(profile)); + } + } + + private void saveProfiles() { + Log.d(TAG, "saveProfiles"); + Set profilesToSave = new HashSet<>(); + for (Profile profile : profiles) { + profilesToSave.add(profile.toString()); + } + Log.d(TAG, "number of profiles: " + profilesToSave.size()); + sharedPreferences.edit().putStringSet("profiles", profilesToSave).apply(); + } + + private static class Profile { + final String name; + final String ipAddress; + final String port; + + public Profile(String name, String ipAddress, String port) { + this.name = name; + this.ipAddress = ipAddress; + this.port = port; + } + + public Profile(String profile) { + String[] info = profile.split("_"); + name = info[0]; + ipAddress = info[1]; + port = info[2]; + } + + @Override + public String toString() { + return name + "_" + ipAddress + "_" + port; + } + + @Override + public boolean equals(Object o) { + if (!(o instanceof Profile)) { + return false; + } + return ((Profile) o).name.equals(name); + } + + @Override + public int hashCode() { + return name.hashCode(); + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/claha/showtimeremote/ShowtimeRemote.java b/app/src/main/java/com/claha/showtimeremote/ShowtimeRemote.java index bddabd1..9032595 100644 --- a/app/src/main/java/com/claha/showtimeremote/ShowtimeRemote.java +++ b/app/src/main/java/com/claha/showtimeremote/ShowtimeRemote.java @@ -9,7 +9,7 @@ import android.view.Menu; import android.view.MenuItem; -import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class ShowtimeRemote extends NavigationDrawerActivity { @@ -44,13 +44,8 @@ protected int getDrawerResourceID() { @Override protected List getDrawerItems() { - List drawerItems = new ArrayList<>(); - drawerItems.add("Home"); - drawerItems.add("Navigation"); - drawerItems.add("Media"); - drawerItems.add("Settings"); - drawerItems.add("About"); - return drawerItems; + String[] drawerItems = getResources().getStringArray(R.array.settings_misc_start_page_entries); + return Arrays.asList(drawerItems); } private boolean showOptionsMenu; @@ -98,6 +93,11 @@ protected int getAppName() { return R.string.app_name; } + @Override + protected int getStartPage() { + return Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(this).getString("misc_start_page", "0")); + } + @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); diff --git a/app/src/main/res/layout-land/fragment_navigation.xml b/app/src/main/res/layout-land/fragment_navigation.xml new file mode 100644 index 0000000..4c3acd8 --- /dev/null +++ b/app/src/main/res/layout-land/fragment_navigation.xml @@ -0,0 +1,17 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_navigation.xml b/app/src/main/res/layout/fragment_navigation.xml index 7e56f6d..131bdaa 100644 --- a/app/src/main/res/layout/fragment_navigation.xml +++ b/app/src/main/res/layout/fragment_navigation.xml @@ -1,68 +1,13 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + \ No newline at end of file diff --git a/app/src/main/res/layout/layout_navigation_ctrl.xml b/app/src/main/res/layout/layout_navigation_ctrl.xml new file mode 100644 index 0000000..afbbbc4 --- /dev/null +++ b/app/src/main/res/layout/layout_navigation_ctrl.xml @@ -0,0 +1,23 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/layout_navigation_pad.xml b/app/src/main/res/layout/layout_navigation_pad.xml new file mode 100644 index 0000000..d672a19 --- /dev/null +++ b/app/src/main/res/layout/layout_navigation_pad.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml new file mode 100644 index 0000000..62bfc7a --- /dev/null +++ b/app/src/main/res/values/arrays.xml @@ -0,0 +1,20 @@ + + + + + Home + Navigation + Media + Settings + About + + + + 0 + 1 + 2 + 3 + 4 + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1e1666b..3235bca 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -5,6 +5,19 @@ v0.0.3 + profiles_choose + Choose + Choose which profile to use + + profiles_add + Add + Add a new profile with current settings + Profile Name + + profiles_delete + Delete + Delete an existing profile + ipAddress IP Address IP address of Showtime @@ -15,6 +28,10 @@ Port of Showtime 42000 + misc_start_page + Choose startup page + Start Page + Search diff --git a/app/src/main/res/xml/fragment_settings.xml b/app/src/main/res/xml/fragment_settings.xml index c13769b..2a31ec2 100644 --- a/app/src/main/res/xml/fragment_settings.xml +++ b/app/src/main/res/xml/fragment_settings.xml @@ -1,23 +1,71 @@ - - - + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file