diff options
author | Scott Jackson <daneren2005@gmail.com> | 2013-05-23 21:19:39 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2013-05-23 21:19:39 -0700 |
commit | 4963016fd6022db53766b439e5bc5d33b6bbca49 (patch) | |
tree | 412569e265187d91773a1353b2f8b1eb6dc01792 /subsonic-android/src/github | |
parent | 9d0c9e0f8c836b5a054fde95f71184bad0833fef (diff) | |
download | dsub-4963016fd6022db53766b439e5bc5d33b6bbca49.tar.gz dsub-4963016fd6022db53766b439e5bc5d33b6bbca49.tar.bz2 dsub-4963016fd6022db53766b439e5bc5d33b6bbca49.zip |
Added settings for chat auto refresh
Diffstat (limited to 'subsonic-android/src/github')
3 files changed, 55 insertions, 4 deletions
diff --git a/subsonic-android/src/github/daneren2005/dsub/activity/SettingsActivity.java b/subsonic-android/src/github/daneren2005/dsub/activity/SettingsActivity.java index 773f4def..4fdf8431 100644 --- a/subsonic-android/src/github/daneren2005/dsub/activity/SettingsActivity.java +++ b/subsonic-android/src/github/daneren2005/dsub/activity/SettingsActivity.java @@ -68,6 +68,7 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer private EditTextPreference bufferLength; private Preference addServerPreference; private PreferenceCategory serversCategory; + private EditTextPreference chatRefreshRate; private int serverCount = 3; private SharedPreferences settings; @@ -93,6 +94,7 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer bufferLength = (EditTextPreference) findPreference(Constants.PREFERENCES_KEY_BUFFER_LENGTH); addServerPreference = (Preference) findPreference(Constants.PREFERENCES_KEY_SERVER_ADD); serversCategory = (PreferenceCategory) findPreference(Constants.PREFERENCES_KEY_SERVER_KEY); + chatRefreshRate = (EditTextPreference) findPreference(Constants.PREFERENCES_KEY_CHAT_REFRESH); settings = Util.getPreferences(this); serverCount = settings.getInt(Constants.PREFERENCES_KEY_SERVER_COUNT, 3); @@ -188,6 +190,7 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer randomSize.setSummary(randomSize.getText()); tempLoss.setSummary(tempLoss.getEntry()); bufferLength.setSummary(bufferLength.getText() + " seconds"); + chatRefreshRate.setSummary(chatRefreshRate.getText()); for (ServerSettings ss : serverSettings.values()) { ss.update(); } diff --git a/subsonic-android/src/github/daneren2005/dsub/fragments/ChatFragment.java b/subsonic-android/src/github/daneren2005/dsub/fragments/ChatFragment.java index 35470721..44114681 100644 --- a/subsonic-android/src/github/daneren2005/dsub/fragments/ChatFragment.java +++ b/subsonic-android/src/github/daneren2005/dsub/fragments/ChatFragment.java @@ -1,12 +1,15 @@ package github.daneren2005.dsub.fragments;
import android.content.Context;
+import android.content.SharedPreferences;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.os.Bundle;
+import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
+import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
@@ -27,16 +30,22 @@ import github.daneren2005.dsub.util.TabBackgroundTask; import github.daneren2005.dsub.util.Util;
import github.daneren2005.dsub.view.ChatAdapter;
import com.actionbarsherlock.view.Menu;
+import github.daneren2005.dsub.util.Constants;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
/**
* @author Joshua Bahnsen
*/
public class ChatFragment extends SubsonicFragment {
+ private static final String TAG = ChatFragment.class.getSimpleName();
private ListView chatListView;
private EditText messageEditText;
private ImageButton sendButton;
private Long lastChatMessageTime = (long) 0;
private ArrayList<ChatMessage> messageList = new ArrayList<ChatMessage>();
+ private ScheduledExecutorService executorService;
@Override
public void onCreate(Bundle bundle) {
@@ -93,6 +102,39 @@ public class ChatFragment extends SubsonicFragment { }
@Override
+ public void onResume() {
+ super.onResume();
+
+ final Handler handler = new Handler();
+ Runnable runnable = new Runnable() {
+ @Override
+ public void run() {
+ handler.post(new Runnable() {
+ @Override
+ public void run() {
+ if(primaryFragment) {
+ load(false);
+ } else {
+ invalidated = true;
+ }
+ }
+ });
+ }
+ };
+
+ SharedPreferences prefs = Util.getPreferences(context);
+ long refreshRate = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_CHAT_REFRESH, "30"));
+ executorService = Executors.newSingleThreadScheduledExecutor();
+ executorService.scheduleWithFixedDelay(runnable, refreshRate * 1000L, refreshRate * 1000L, TimeUnit.MILLISECONDS);
+ }
+
+ @Override
+ public void onPause() {
+ super.onPause();
+ executorService.shutdown();
+ }
+
+ @Override
public void onCreateOptionsMenu(Menu menu, com.actionbarsherlock.view.MenuInflater menuInflater) {
menuInflater.inflate(R.menu.empty, menu);
}
@@ -108,21 +150,26 @@ public class ChatFragment extends SubsonicFragment { @Override
protected void refresh(boolean refresh) {
- load();
+ load(refresh);
}
- private synchronized void load() {
+ private synchronized void load(final boolean refresh) {
+ Log.i(TAG, "Loading: " + refresh);
setTitle(R.string.button_bar_chat);
BackgroundTask<List<ChatMessage>> task = new TabBackgroundTask<List<ChatMessage>>(this) {
@Override
protected List<ChatMessage> doInBackground() throws Throwable {
MusicService musicService = MusicServiceFactory.getMusicService(context);
- return musicService.getChatMessages(lastChatMessageTime, context, this);
+ return musicService.getChatMessages(refresh ? 0L : lastChatMessageTime, context, this);
}
@Override
protected void done(List<ChatMessage> result) {
if (result != null && !result.isEmpty()) {
+ if(refresh) {
+ messageList.clear();
+ }
+
// Reset lastChatMessageTime if we have a newer message
for (ChatMessage message : result) {
if (message.getTime() > lastChatMessageTime) {
@@ -161,7 +208,7 @@ public class ChatFragment extends SubsonicFragment { @Override
protected void done(Void result) {
- load();
+ load(false);
}
};
diff --git a/subsonic-android/src/github/daneren2005/dsub/util/Constants.java b/subsonic-android/src/github/daneren2005/dsub/util/Constants.java index b6cb3d89..e82b8111 100644 --- a/subsonic-android/src/github/daneren2005/dsub/util/Constants.java +++ b/subsonic-android/src/github/daneren2005/dsub/util/Constants.java @@ -101,6 +101,7 @@ public final class Constants { public static final String PREFERENCES_KEY_PERSISTENT_NOTIFICATION = "persistentNotification"; public static final String PREFERENCES_KEY_GAPLESS_PLAYBACK = "gaplessPlayback"; public static final String PREFERENCES_KEY_SHUFFLE_MODE = "shuffleMode"; + public static final String PREFERENCES_KEY_CHAT_REFRESH = "chatRefreshRate"; public static final String MAIN_BACK_STACK = "backStackIds"; public static final String MAIN_BACK_STACK_SIZE = "backStackIdsSize"; |