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_enabled">Watching pinned threads</string>
<string name="watch_info_text">
To view the thread watcher options, turn the thread watcher on.
</string>
<string name="watch_info_text">To watch pins for new posts, 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-array name="watch_background_timeouts">
<item>1 minute</item>

@ -1,12 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<ListPreference
android:title="@string/watch_background_timeout_description"
android:summary="Foo"
android:key="preference_watch_background_timeout"
<CheckBoxPreference
android:key="preference_watch_background_enabled"
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:dialogTitle="bar"
android:dialogTitle="@string/watch_background_timeout_description"
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() {
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() {
String number = ChanApplication.getPreferences().getString("preference_watch_background_timeout", "0");
return Integer.parseInt(number) * 1000L;
}
}

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

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

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

@ -10,6 +10,9 @@ import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
@ -108,19 +111,33 @@ public class WatchSettingsActivity extends Activity implements OnCheckedChangeLi
super.onCreate(savedInstanceState);
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