Skip to content
This repository has been archived by the owner on May 20, 2020. It is now read-only.

Commit

Permalink
Profiles in settings and landscape layout for navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
claha committed Jan 13, 2015
1 parent 17c4a73 commit c7a89bd
Show file tree
Hide file tree
Showing 10 changed files with 416 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down Expand Up @@ -168,5 +168,9 @@ private void selectFragment(int position) {

abstract protected int getAppName();

protected int getStartPage() {
return 0;
}

}

209 changes: 208 additions & 1 deletion app/src/main/java/com/claha/showtimeremote/SettingsFragment.java
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();
}
}
}
16 changes: 8 additions & 8 deletions app/src/main/java/com/claha/showtimeremote/ShowtimeRemote.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -44,13 +44,8 @@ protected int getDrawerResourceID() {

@Override
protected List<String> getDrawerItems() {
List<String> 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;
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/res/layout-land/fragment_navigation.xml
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>
67 changes: 6 additions & 61 deletions app/src/main/res/layout/fragment_navigation.xml
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>
23 changes: 23 additions & 0 deletions app/src/main/res/layout/layout_navigation_ctrl.xml
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>
Loading

0 comments on commit c7a89bd

Please sign in to comment.