Notify forward changed

This commit is contained in:
M66B 2016-02-13 12:42:26 +01:00
parent f83cc0e82d
commit 37940d6fc9
2 changed files with 59 additions and 0 deletions

View File

@ -50,6 +50,19 @@ public class ActivityForwarding extends AppCompatActivity {
private AdapterForwarding adapter;
private AlertDialog dialog = null;
private DatabaseHelper.ForwardChangedListener listener = new DatabaseHelper.ForwardChangedListener() {
@Override
public void onChanged() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (adapter != null)
adapter.changeCursor(DatabaseHelper.getInstance(ActivityForwarding.this).getForwarding());
}
});
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
Util.setTheme(this);
@ -99,6 +112,20 @@ public class ActivityForwarding extends AppCompatActivity {
});
}
@Override
protected void onResume() {
super.onResume();
DatabaseHelper.getInstance(this).addForwardChangedListener(listener);
if (adapter != null)
adapter.changeCursor(DatabaseHelper.getInstance(ActivityForwarding.this).getForwarding());
}
@Override
protected void onPause() {
super.onPause();
DatabaseHelper.getInstance(this).removeForwardChangedListener(listener);
}
@Override
protected void onDestroy() {
running = false;

View File

@ -46,6 +46,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
private static boolean once = true;
private static List<LogChangedListener> logChangedListeners = new ArrayList<>();
private static List<AccessChangedListener> accessChangedListeners = new ArrayList<>();
private static List<ForwardChangedListener> forwardChangedListeners = new ArrayList<>();
private Context context;
private static HandlerThread hthread = null;
@ -53,6 +54,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
private final static int MSG_LOG = 1;
private final static int MSG_ACCESS = 2;
private final static int MSG_FORWARD = 3;
private ReentrantReadWriteLock mLock = new ReentrantReadWriteLock(true);
@ -654,6 +656,8 @@ public class DatabaseHelper extends SQLiteOpenHelper {
} finally {
mLock.writeLock().unlock();
}
notifyForwardChanged();
}
public void deleteForward(int protocol, int dport) {
@ -665,6 +669,8 @@ public class DatabaseHelper extends SQLiteOpenHelper {
} finally {
mLock.writeLock().unlock();
}
notifyForwardChanged();
}
public Cursor getForwarding() {
@ -696,6 +702,14 @@ public class DatabaseHelper extends SQLiteOpenHelper {
accessChangedListeners.remove(listener);
}
public void addForwardChangedListener(ForwardChangedListener listener) {
forwardChangedListeners.add(listener);
}
public void removeForwardChangedListener(ForwardChangedListener listener) {
forwardChangedListeners.remove(listener);
}
private void notifyLogChanged() {
Message msg = handler.obtainMessage();
msg.what = MSG_LOG;
@ -708,6 +722,12 @@ public class DatabaseHelper extends SQLiteOpenHelper {
handler.sendMessage(msg);
}
private void notifyForwardChanged() {
Message msg = handler.obtainMessage();
msg.what = MSG_FORWARD;
handler.sendMessage(msg);
}
private static void handleChangedNotification(Message msg) {
// Batch notifications
try {
@ -733,6 +753,14 @@ public class DatabaseHelper extends SQLiteOpenHelper {
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
} else if (msg.what == MSG_FORWARD) {
for (ForwardChangedListener listener : forwardChangedListeners)
try {
listener.onChanged();
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
}
}
@ -743,4 +771,8 @@ public class DatabaseHelper extends SQLiteOpenHelper {
public interface AccessChangedListener {
void onChanged();
}
public interface ForwardChangedListener {
void onChanged();
}
}