Pause Periodic List Refresh when View is not Visible

Closes #553
This commit is contained in:
Torsten Grote
2016-07-29 16:09:39 -03:00
committed by akwizgran
parent 2577b2ab2a
commit 15d139afd4
6 changed files with 30 additions and 16 deletions

View File

@@ -153,7 +153,6 @@ public class ContactListFragment extends BaseFragment implements EventListener {
list.setLayoutManager(new LinearLayoutManager(getContext())); list.setLayoutManager(new LinearLayoutManager(getContext()));
list.setAdapter(adapter); list.setAdapter(adapter);
list.setEmptyText(getString(R.string.no_contacts)); list.setEmptyText(getString(R.string.no_contacts));
list.periodicallyUpdateContent();
return contentView; return contentView;
} }
@@ -183,6 +182,7 @@ public class ContactListFragment extends BaseFragment implements EventListener {
super.onResume(); super.onResume();
eventBus.addListener(this); eventBus.addListener(this);
loadContacts(); loadContacts();
list.startPeriodicUpdate();
} }
@Override @Override
@@ -190,6 +190,7 @@ public class ContactListFragment extends BaseFragment implements EventListener {
super.onPause(); super.onPause();
adapter.clear(); adapter.clear();
eventBus.removeListener(this); eventBus.removeListener(this);
list.stopPeriodicUpdate();
} }
private void loadContacts() { private void loadContacts() {

View File

@@ -170,7 +170,6 @@ public class ConversationActivity extends BriarActivity
list.setLayoutManager(new LinearLayoutManager(this)); list.setLayoutManager(new LinearLayoutManager(this));
list.setAdapter(adapter); list.setAdapter(adapter);
list.setEmptyText(getString(R.string.no_private_messages)); list.setEmptyText(getString(R.string.no_private_messages));
list.periodicallyUpdateContent();
content = (EditText) findViewById(R.id.input_text); content = (EditText) findViewById(R.id.input_text);
sendButton = findViewById(R.id.btn_send); sendButton = findViewById(R.id.btn_send);
@@ -205,6 +204,7 @@ public class ConversationActivity extends BriarActivity
notificationManager.blockNotification(groupId); notificationManager.blockNotification(groupId);
notificationManager.clearPrivateMessageNotification(groupId); notificationManager.clearPrivateMessageNotification(groupId);
loadData(); loadData();
list.startPeriodicUpdate();
} }
@Override @Override
@@ -212,6 +212,7 @@ public class ConversationActivity extends BriarActivity
super.onPause(); super.onPause();
eventBus.removeListener(this); eventBus.removeListener(this);
notificationManager.unblockNotification(groupId); notificationManager.unblockNotification(groupId);
list.stopPeriodicUpdate();
if (isFinishing()) markMessagesRead(); if (isFinishing()) markMessagesRead();
} }

View File

@@ -119,7 +119,7 @@ public class ForumActivity extends BriarActivity implements
forumAdapter = new ForumAdapter( forumAdapter = new ForumAdapter(
forumController.getForumEntries()); forumController.getForumEntries());
recyclerView.setAdapter(forumAdapter); recyclerView.setAdapter(forumAdapter);
recyclerView.periodicallyUpdateContent(); recyclerView.startPeriodicUpdate();
if (state != null) { if (state != null) {
byte[] replyId = byte[] replyId =
state.getByteArray(KEY_REPLY_ID); state.getByteArray(KEY_REPLY_ID);
@@ -250,12 +250,18 @@ public class ForumActivity extends BriarActivity implements
super.onResume(); super.onResume();
notificationManager.blockNotification(groupId); notificationManager.blockNotification(groupId);
notificationManager.clearForumPostNotification(groupId); notificationManager.clearForumPostNotification(groupId);
if (recyclerView.getRecyclerView().getAdapter() != null) {
recyclerView.startPeriodicUpdate();
}
} }
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
notificationManager.unblockNotification(groupId); notificationManager.unblockNotification(groupId);
if (recyclerView.getRecyclerView().getAdapter() != null) {
recyclerView.stopPeriodicUpdate();
}
} }
public void sendMessage(View view) { public void sendMessage(View view) {

View File

@@ -84,7 +84,6 @@ public class ForumListFragment extends BaseEventFragment implements
list.setLayoutManager(new LinearLayoutManager(getActivity())); list.setLayoutManager(new LinearLayoutManager(getActivity()));
list.setAdapter(adapter); list.setAdapter(adapter);
list.setEmptyText(getString(R.string.no_forums)); list.setEmptyText(getString(R.string.no_forums));
list.periodicallyUpdateContent();
snackbar = Snackbar.make(list, "", LENGTH_INDEFINITE); snackbar = Snackbar.make(list, "", LENGTH_INDEFINITE);
snackbar.getView().setBackgroundResource(R.color.briar_primary); snackbar.getView().setBackgroundResource(R.color.briar_primary);
@@ -111,6 +110,7 @@ public class ForumListFragment extends BaseEventFragment implements
loadForumHeaders(); loadForumHeaders();
loadAvailableForums(); loadAvailableForums();
list.startPeriodicUpdate();
} }
@Override @Override
@@ -118,6 +118,7 @@ public class ForumListFragment extends BaseEventFragment implements
super.onPause(); super.onPause();
adapter.clear(); adapter.clear();
list.stopPeriodicUpdate();
} }
@Override @Override

View File

@@ -30,6 +30,8 @@ import static android.text.format.DateUtils.WEEK_IN_MILLIS;
public class AndroidUtils { public class AndroidUtils {
static final long MIN_RESOLUTION = MINUTE_IN_MILLIS;
// Fake Bluetooth address returned by BluetoothAdapter on API 23 and later // Fake Bluetooth address returned by BluetoothAdapter on API 23 and later
private static final String FAKE_BLUETOOTH_ADDRESS = "02:00:00:00:00:00"; private static final String FAKE_BLUETOOTH_ADDRESS = "02:00:00:00:00:00";
@@ -97,22 +99,20 @@ public class AndroidUtils {
} }
public static String formatDate(Context ctx, long time) { public static String formatDate(Context ctx, long time) {
// update BriarRecyclerView#DEFAULT_REFRESH_INTERVAL along with this
long minResolution = MINUTE_IN_MILLIS;
int flags = FORMAT_ABBREV_RELATIVE | int flags = FORMAT_ABBREV_RELATIVE |
FORMAT_SHOW_DATE | FORMAT_ABBREV_TIME | FORMAT_ABBREV_MONTH; FORMAT_SHOW_DATE | FORMAT_ABBREV_TIME | FORMAT_ABBREV_MONTH;
long diff = System.currentTimeMillis() - time; long diff = System.currentTimeMillis() - time;
if (diff < minResolution) return ctx.getString(R.string.now); if (diff < MIN_RESOLUTION) return ctx.getString(R.string.now);
if (diff >= DAY_IN_MILLIS && diff < WEEK_IN_MILLIS) { if (diff >= DAY_IN_MILLIS && diff < WEEK_IN_MILLIS) {
// also show time when older than a day, but newer than a week // also show time when older than a day, but newer than a week
return DateUtils.getRelativeDateTimeString(ctx, time, minResolution, return DateUtils.getRelativeDateTimeString(ctx, time,
WEEK_IN_MILLIS, flags).toString(); MIN_RESOLUTION, WEEK_IN_MILLIS, flags).toString();
} }
// otherwise just show "...ago" or date string // otherwise just show "...ago" or date string
return DateUtils return DateUtils
.getRelativeTimeSpanString(time, System.currentTimeMillis(), .getRelativeTimeSpanString(time, System.currentTimeMillis(),
minResolution, flags).toString(); MIN_RESOLUTION, flags).toString();
} }
} }

View File

@@ -17,6 +17,7 @@ import org.briarproject.R;
import java.util.logging.Logger; import java.util.logging.Logger;
import static android.text.format.DateUtils.MINUTE_IN_MILLIS; import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
import static org.briarproject.android.util.AndroidUtils.MIN_RESOLUTION;
public class BriarRecyclerView extends FrameLayout { public class BriarRecyclerView extends FrameLayout {
@@ -28,7 +29,7 @@ public class BriarRecyclerView extends FrameLayout {
private boolean isScrollingToEnd = false; private boolean isScrollingToEnd = false;
private final Logger LOG = Logger.getLogger(getClass().getName()); private final Logger LOG = Logger.getLogger(getClass().getName());
private final long DEFAULT_REFRESH_INTERVAL = MINUTE_IN_MILLIS; private final long DEFAULT_REFRESH_INTERVAL = MIN_RESOLUTION;
public BriarRecyclerView(Context context) { public BriarRecyclerView(Context context) {
this(context, null, 0); this(context, null, 0);
@@ -52,10 +53,7 @@ public class BriarRecyclerView extends FrameLayout {
@Override @Override
protected void onDetachedFromWindow() { protected void onDetachedFromWindow() {
super.onDetachedFromWindow(); super.onDetachedFromWindow();
if (refresher != null) { stopPeriodicUpdate();
LOG.info("Removing Handler Callback");
removeCallbacks(refresher);
}
} }
private void initViews() { private void initViews() {
@@ -172,7 +170,7 @@ public class BriarRecyclerView extends FrameLayout {
return this.recyclerView; return this.recyclerView;
} }
public void periodicallyUpdateContent() { public void startPeriodicUpdate() {
if (recyclerView == null || recyclerView.getAdapter() == null) { if (recyclerView == null || recyclerView.getAdapter() == null) {
throw new IllegalStateException("Need to call setAdapter() first!"); throw new IllegalStateException("Need to call setAdapter() first!");
} }
@@ -188,4 +186,11 @@ public class BriarRecyclerView extends FrameLayout {
postDelayed(refresher, DEFAULT_REFRESH_INTERVAL); postDelayed(refresher, DEFAULT_REFRESH_INTERVAL);
} }
public void stopPeriodicUpdate() {
if (refresher != null) {
LOG.info("Removing Handler Callback");
removeCallbacks(refresher);
}
}
} }