This repository has been archived by the owner on May 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Profiles in settings and landscape layout for navigation
- Loading branch information
Showing
10 changed files
with
416 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
209 changes: 208 additions & 1 deletion
209
app/src/main/java/com/claha/showtimeremote/SettingsFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Profile> 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<String> profilesToLoadTemp = sharedPreferences.getStringSet("profiles", new HashSet<String>()); | ||
List<String> 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<String> 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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
style="@style/horizontal_center_layout" | ||
android:layout_height="match_parent" | ||
android:baselineAligned="false"> | ||
|
||
<include layout="@layout/layout_navigation_pad" | ||
android:layout_height="match_parent" | ||
android:layout_width="match_parent" | ||
android:layout_weight="1"/> | ||
|
||
<include layout="@layout/layout_navigation_ctrl" | ||
android:layout_height="match_parent" | ||
android:layout_width="match_parent" | ||
android:layout_weight="1"/> | ||
|
||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:showtime="http://schemas.android.com/apk/res-auto" | ||
style="@style/vertical_center_layout"> | ||
|
||
<LinearLayout style="@style/horizontal_center_layout"> | ||
<include layout="@layout/layout_navigation_pad" | ||
android:layout_height="wrap_content" | ||
android:layout_width="match_parent"/> | ||
|
||
<com.claha.showtimeremote.ShowtimeButton | ||
style="@style/showtime_button" | ||
android:src="@drawable/ic_action_up" | ||
showtime:action="Up" | ||
showtime:onPress="true" /> | ||
|
||
</LinearLayout> | ||
|
||
<LinearLayout style="@style/horizontal_center_layout"> | ||
|
||
<com.claha.showtimeremote.ShowtimeButton | ||
style="@style/showtime_button" | ||
android:src="@drawable/ic_action_left" | ||
showtime:action="Left" | ||
showtime:onPress="true" /> | ||
|
||
<com.claha.showtimeremote.ShowtimeButton | ||
style="@style/showtime_button" | ||
android:src="@drawable/ic_action_activate" | ||
showtime:action="Activate" /> | ||
|
||
<com.claha.showtimeremote.ShowtimeButton | ||
style="@style/showtime_button" | ||
android:src="@drawable/ic_action_right" | ||
showtime:action="Right" | ||
showtime:onPress="true" /> | ||
|
||
</LinearLayout> | ||
|
||
<LinearLayout style="@style/horizontal_center_layout"> | ||
|
||
<com.claha.showtimeremote.ShowtimeButton | ||
style="@style/showtime_button" | ||
android:src="@drawable/ic_action_down" | ||
showtime:action="Down" | ||
showtime:onPress="true" /> | ||
|
||
</LinearLayout> | ||
|
||
<LinearLayout style="@style/horizontal_center_layout"> | ||
|
||
<com.claha.showtimeremote.ShowtimeButton | ||
style="@style/showtime_button" | ||
android:src="@drawable/ic_action_back" | ||
showtime:action="Back" | ||
showtime:onPress="true" /> | ||
|
||
<com.claha.showtimeremote.ShowtimeButton | ||
style="@style/showtime_button" | ||
android:src="@drawable/ic_action_home" | ||
showtime:action="Home" /> | ||
|
||
<com.claha.showtimeremote.ShowtimeButton | ||
style="@style/showtime_button" | ||
android:src="@drawable/ic_action_menu" | ||
showtime:action="Menu" | ||
showtime:actionLong="ItemMenu" /> | ||
|
||
</LinearLayout> | ||
<include layout="@layout/layout_navigation_ctrl" | ||
android:layout_height="wrap_content" | ||
android:layout_width="match_parent"/> | ||
|
||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:showtime="http://schemas.android.com/apk/res-auto" | ||
style="@style/horizontal_center_layout"> | ||
|
||
<com.claha.showtimeremote.ShowtimeButton | ||
style="@style/showtime_button" | ||
android:src="@drawable/ic_action_back" | ||
showtime:action="Back" | ||
showtime:onPress="true" /> | ||
|
||
<com.claha.showtimeremote.ShowtimeButton | ||
style="@style/showtime_button" | ||
android:src="@drawable/ic_action_home" | ||
showtime:action="Home" /> | ||
|
||
<com.claha.showtimeremote.ShowtimeButton | ||
style="@style/showtime_button" | ||
android:src="@drawable/ic_action_menu" | ||
showtime:action="Menu" | ||
showtime:actionLong="ItemMenu" /> | ||
|
||
</LinearLayout> |
Oops, something went wrong.