diff options
author | Arun Raghavan <arun.raghavan@collabora.co.uk> | 2012-04-23 01:31:10 +0530 |
---|---|---|
committer | Arun Raghavan <arun.raghavan@collabora.co.uk> | 2012-04-23 01:31:10 +0530 |
commit | f23201277c23afe72b173ec4e7c0d7f6590b1c61 (patch) | |
tree | c86f518e67994ed31f67f28eb459e34c929b957d | |
parent | b0a1b71e8913ddfbde6a905d69db23ee81b668be (diff) |
Keep list of recent servers for easy access
-rw-r--r-- | res/layout/main.xml | 12 | ||||
-rw-r--r-- | src/org/pulseaudio/outputswitcher/OutputSwitcher.java | 71 |
2 files changed, 78 insertions, 5 deletions
diff --git a/res/layout/main.xml b/res/layout/main.xml index 62940e2..dd4eec2 100644 --- a/res/layout/main.xml +++ b/res/layout/main.xml @@ -28,4 +28,16 @@ android:paddingTop="8dp" android:paddingBottom="8dp" /> +<TextView + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:text="Recent servers" + android:paddingTop="16dp" + /> +<ListView android:id="@android:id/list" + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:paddingTop="8dp" + android:paddingBottom="8dp" + /> </LinearLayout> diff --git a/src/org/pulseaudio/outputswitcher/OutputSwitcher.java b/src/org/pulseaudio/outputswitcher/OutputSwitcher.java index 74d0018..7ad5274 100644 --- a/src/org/pulseaudio/outputswitcher/OutputSwitcher.java +++ b/src/org/pulseaudio/outputswitcher/OutputSwitcher.java @@ -15,15 +15,23 @@ */ package org.pulseaudio.outputswitcher; -import android.app.Activity; +import java.util.Arrays; +import java.util.ArrayList; +import android.app.ListActivity; import android.os.Bundle; +import android.view.View; import android.widget.*; import android.net.wifi.WifiManager; import android.net.wifi.WifiManager.WifiLock; +import android.content.SharedPreferences; +import android.util.Log; -public class OutputSwitcher extends Activity implements CompoundButton.OnCheckedChangeListener +public class OutputSwitcher extends ListActivity implements CompoundButton.OnCheckedChangeListener, AdapterView.OnItemClickListener { + private static final String TAG = "PAOutputSwitcher"; + private WifiLock wifiLock; + private ArrayList<String> remotes; @Override public void onCheckedChanged(CompoundButton sw, boolean isChecked) @@ -37,10 +45,14 @@ public class OutputSwitcher extends Activity implements CompoundButton.OnChecked wifiLock.release(); } else { EditText text_server = (EditText) findViewById(R.id.text_server); + String server = text_server.getText().toString(); - if (switchToNetwork(text_server.getText().toString())) - updateStatus("Switched to remote playback on '" + text_server.getText().toString() + "'"); - else + if (switchToNetwork(server)) { + updateStatus("Switched to remote playback on '" + server + "'"); + addRecentRemote(server); + + ((ArrayAdapter<String>) getListAdapter()).notifyDataSetChanged(); + } else updateStatus("Failed to switch to remote playback on '" + text_server.getText().toString() + "'"); wifiLock.acquire(); @@ -48,6 +60,13 @@ public class OutputSwitcher extends Activity implements CompoundButton.OnChecked } @Override + public void onItemClick(AdapterView<?> parent, View view, int position, long id) + { + EditText text_server = (EditText) findViewById(R.id.text_server); + text_server.setText(((TextView) view).getText().toString()); + } + + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -56,6 +75,10 @@ public class OutputSwitcher extends Activity implements CompoundButton.OnChecked Switch sw = (Switch) findViewById(R.id.switch_remote); sw.setOnCheckedChangeListener(this); + remotes = new ArrayList(Arrays.asList(getRecentRemotes().split(","))); + setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, remotes)); + getListView().setOnItemClickListener(this); + WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE); wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "PA_WifiLock"); wifiLock.setReferenceCounted(false); @@ -73,4 +96,42 @@ public class OutputSwitcher extends Activity implements CompoundButton.OnChecked static { System.loadLibrary("output-switcher"); } + + /* Util functions for recent remotes */ + private String getRecentRemotes() + { + return getPreferences(MODE_PRIVATE).getString("recentRemotes", ""); + } + + private void saveRecentRemotes(String remotes) + { + if (remotes.length() == 0) + return; + + SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit(); + + editor.putString("recentRemotes", remotes); + editor.commit(); + } + + private void addRecentRemote(String newRemote) + { + String curRemotes = getRecentRemotes(); + String prefString = newRemote; + + String[] remotesList = curRemotes.split(","); + + for (int i = 0; i < remotesList.length; i++) { + if (remotesList[i].equals(newRemote)) { + /* Dupe, get out of here */ + return; + } + + prefString = prefString + "," + remotesList[i]; + } + + saveRecentRemotes(prefString); + /* Add to list view as well */ + remotes.add(0, newRemote); + } } |