Added settings for background watching.

captchafix
Florens Douwes 11 years ago
parent 0a28b34789
commit 1487369ca7
  1. 8
      Chan/res/values/strings.xml
  2. 22
      Chan/res/xml/preference_watch.xml
  3. 11
      Chan/src/org/floens/chan/core/ChanPreferences.java
  4. 4
      Chan/src/org/floens/chan/core/watch/PinWatcher.java
  5. 4
      Chan/src/org/floens/chan/core/watch/WatchNotifier.java
  6. 50
      Chan/src/org/floens/chan/service/WatchService.java
  7. 39
      Chan/src/org/floens/chan/ui/activity/WatchSettingsActivity.java

@ -104,9 +104,11 @@
<string name="watch_summary_disabled">Off</string> <string name="watch_summary_disabled">Off</string>
<string name="watch_summary_enabled">Watching pinned threads</string> <string name="watch_summary_enabled">Watching pinned threads</string>
<string name="watch_info_text"> <string name="watch_info_text">To watch pins for new posts, turn the thread watcher on.</string>
To view the thread watcher options, turn the thread watcher on.
</string> <string name="watch_enable_background">Enable in the background</string>
<string name="watch_enable_background_on">Watching pins when Chan is in the background</string>
<string name="watch_enable_background_off">Not watching when Chan is in the background</string>
<string name="watch_background_timeout_description">Time between reloads in background</string> <string name="watch_background_timeout_description">Time between reloads in background</string>
<string-array name="watch_background_timeouts"> <string-array name="watch_background_timeouts">
<item>1 minute</item> <item>1 minute</item>

@ -1,12 +1,20 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<ListPreference
android:title="@string/watch_background_timeout_description" <CheckBoxPreference
android:summary="Foo" android:key="preference_watch_background_enabled"
android:key="preference_watch_background_timeout" android:summaryOff="@string/watch_enable_background_off"
android:summaryOn="@string/watch_enable_background_on"
android:title="@string/watch_enable_background" />
<ListPreference
android:dependency="preference_watch_background_enabled"
android:defaultValue="60" android:defaultValue="60"
android:dialogTitle="bar" android:dialogTitle="@string/watch_background_timeout_description"
android:entries="@array/watch_background_timeouts" android:entries="@array/watch_background_timeouts"
android:entryValues="@array/watch_background_timeouts_int" /> android:entryValues="@array/watch_background_timeouts_int"
android:key="preference_watch_background_timeout"
android:summary="Foo"
android:title="@string/watch_background_timeout_description" />
</PreferenceScreen> </PreferenceScreen>

@ -29,7 +29,7 @@ public class ChanPreferences {
} }
public static boolean getWatchEnabled() { public static boolean getWatchEnabled() {
return ChanApplication.getPreferences().getBoolean("preference_watch_enabled", true); return ChanApplication.getPreferences().getBoolean("preference_watch_enabled", false);
} }
/** /**
@ -46,8 +46,17 @@ public class ChanPreferences {
} }
} }
public static boolean getWatchBackgroundEnabled() {
return ChanApplication.getPreferences().getBoolean("preference_watch_background_enabled", true);
}
public static long getWatchBackgroundTimeout() { public static long getWatchBackgroundTimeout() {
String number = ChanApplication.getPreferences().getString("preference_watch_background_timeout", "0"); String number = ChanApplication.getPreferences().getString("preference_watch_background_timeout", "0");
return Integer.parseInt(number) * 1000L; return Integer.parseInt(number) * 1000L;
} }
} }

@ -56,7 +56,7 @@ public class PinWatcher implements Loader.LoaderListener {
pin.watchLastCount = 0; pin.watchLastCount = 0;
pin.watchNewCount = 0; pin.watchNewCount = 0;
WatchService.callOnPinsChanged(); WatchService.onPinWatcherResult();
} }
@Override @Override
@ -73,6 +73,6 @@ public class PinWatcher implements Loader.LoaderListener {
pin.watchNewCount = count; pin.watchNewCount = count;
WatchService.callOnPinsChanged(); WatchService.onPinWatcherResult();
} }
} }

@ -28,6 +28,10 @@ public class WatchNotifier {
nm = (NotificationManager) pinnedService.getSystemService(Context.NOTIFICATION_SERVICE); nm = (NotificationManager) pinnedService.getSystemService(Context.NOTIFICATION_SERVICE);
} }
public void destroy() {
nm.cancel(NOTIFICATION_ID);
}
public void update() { public void update() {
if (!WatchService.getActivityInForeground()) { if (!WatchService.getActivityInForeground()) {
prepareNotification(); prepareNotification();

@ -17,6 +17,8 @@ import android.os.Looper;
import android.widget.Toast; import android.widget.Toast;
public class WatchService extends Service { public class WatchService extends Service {
private static final String TAG = "WatchService";
private static final long FOREGROUND_INTERVAL = 10000L; private static final long FOREGROUND_INTERVAL = 10000L;
private static WatchService instance; private static WatchService instance;
@ -76,6 +78,8 @@ public class WatchService extends Service {
for (Pin pin : pins) { for (Pin pin : pins) {
pin.destroyWatcher(); pin.destroyWatcher();
} }
instance.watchNotifier.destroy();
} }
} }
@ -83,11 +87,14 @@ public class WatchService extends Service {
return instance != null; return instance != null;
} }
public static void callOnPinsChanged() { public static void onPinWatcherResult() {
new Handler(Looper.getMainLooper()).post(new Runnable() { new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override @Override
public void run() { public void run() {
ChanApplication.getPinnedManager().onPinsChanged(); ChanApplication.getPinnedManager().onPinsChanged();
if (instance != null) {
instance.watchNotifier.update();
}
} }
}); });
} }
@ -100,6 +107,8 @@ public class WatchService extends Service {
watchNotifier = new WatchNotifier(this); watchNotifier = new WatchNotifier(this);
Logger.i(TAG, "WatchService created");
startThread(); startThread();
} }
@ -107,6 +116,7 @@ public class WatchService extends Service {
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
instance = null; instance = null;
running = false; running = false;
@ -114,6 +124,8 @@ public class WatchService extends Service {
loadThread.interrupt(); loadThread.interrupt();
Toast.makeText(getApplicationContext(), "Pinned thread interrupted", Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), "Pinned thread interrupted", Toast.LENGTH_SHORT).show();
} }
Logger.i(TAG, "WatchService destroyed");
} }
private void startThread() { private void startThread() {
@ -124,7 +136,7 @@ public class WatchService extends Service {
@Override @Override
public void run() { public void run() {
while (running) { while (running) {
Logger.test("Loadthread iteration"); Logger.d(TAG, "Loadthread loop");
update(); update();
@ -132,12 +144,21 @@ public class WatchService extends Service {
return; return;
long timeout = activityInForeground ? FOREGROUND_INTERVAL : getBackgroundTimeout(); long timeout = activityInForeground ? FOREGROUND_INTERVAL : getBackgroundTimeout();
if (timeout < 0L) {
try { Logger.d(TAG, "Waiting for interrupt...");
Thread.sleep(timeout); try {
} catch (InterruptedException e) { Object o = new Object();
if (!running) { synchronized (o) {
return; o.wait();
}
} catch (InterruptedException e) {
Logger.d(TAG, "Interrupted!");
}
} else {
try {
Thread.sleep(timeout);
} catch (InterruptedException e) {
Logger.d(TAG, "Interrupted!");
} }
} }
} }
@ -160,9 +181,16 @@ public class WatchService extends Service {
watchNotifier.onForegroundChanged(); watchNotifier.onForegroundChanged();
} }
/**
* Returns the sleep time the user specified for background iteration
* @return the sleep time in ms, or -1 if background reloading is disabled
*/
private long getBackgroundTimeout() { private long getBackgroundTimeout() {
Logger.test("::: " + ChanPreferences.getWatchBackgroundTimeout()); if (ChanPreferences.getWatchBackgroundEnabled()) {
return ChanPreferences.getWatchBackgroundTimeout(); return ChanPreferences.getWatchBackgroundTimeout();
} else {
return -1;
}
} }
private void update() { private void update() {
@ -170,8 +198,6 @@ public class WatchService extends Service {
for (Pin pin : pins) { for (Pin pin : pins) {
pin.updateWatch(); pin.updateWatch();
} }
watchNotifier.update();
} }
@Override @Override

@ -10,6 +10,9 @@ import android.app.FragmentTransaction;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
import android.view.Gravity; import android.view.Gravity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -108,19 +111,33 @@ public class WatchSettingsActivity extends Activity implements OnCheckedChangeLi
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference_watch); addPreferencesFromResource(R.xml.preference_watch);
}
}
}
// final Preference backgroundEnabled =
// findPreference("preference_watch_background_enabled");
final ListPreference backgroundTimeout = (ListPreference) findPreference("preference_watch_background_timeout");
String currentValue = backgroundTimeout.getValue();
if (currentValue == null) {
backgroundTimeout.setValue((String) backgroundTimeout.getEntryValues()[0]);
currentValue = backgroundTimeout.getValue();
}
updateListSummary(backgroundTimeout, currentValue.toString());
// Timeout is reset when board activity is started
backgroundTimeout.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
updateListSummary(backgroundTimeout, newValue.toString());
return true;
}
});
}
private void updateListSummary(ListPreference backgroundTimeout, String value) {
int index = backgroundTimeout.findIndexOfValue(value);
backgroundTimeout.setSummary(backgroundTimeout.getEntries()[index]);
}
}
}

Loading…
Cancel
Save