switch between articles using volume buttons

This commit is contained in:
Andrew Dolgov 2011-11-30 18:22:33 +03:00
parent 4cf1ca7c57
commit 6a8878f980
2 changed files with 76 additions and 2 deletions

View File

@ -478,7 +478,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
ListView list = (ListView)getView().findViewById(R.id.headlines);
if (list != null) {
int position = m_adapter.getPosition(m_articleOps.getSelectedArticle());
int position = m_adapter.getPosition(getArticleById(id));
list.setSelection(position);
}
}
@ -501,6 +501,14 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
return m_adapter.getItem(position);
}
public Article getArticleById(int id) {
for (Article a : m_articles) {
if (a.id == id)
return a;
}
return null;
}
public ArticleList getUnreadArticles() {
ArticleList tmp = new ArticleList();
for (Article a : m_articles) {
@ -521,4 +529,9 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
// no-op
}
public int getActiveArticleId() {
return m_activeArticleId;
}
}

View File

@ -1272,9 +1272,70 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
return null;
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
HeadlinesFragment hf = (HeadlinesFragment)getSupportFragmentManager().findFragmentById(R.id.headlines_fragment);
if (hf != null && m_activeFeed != null) {
Article base = hf.getArticleById(hf.getActiveArticleId());
Article next = base != null ? getRelativeArticle(base, RelativeArticle.AFTER) : hf.getArticleAtPosition(0);
if (next != null) {
hf.setActiveArticleId(next.id);
boolean combinedMode = m_prefs.getBoolean("combined_mode", false);
if (combinedMode || m_selectedArticle == null) {
next.unread = false;
saveArticleUnread(next);
} else {
openArticle(next, 0);
}
}
}
}
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_UP) {
HeadlinesFragment hf = (HeadlinesFragment)getSupportFragmentManager().findFragmentById(R.id.headlines_fragment);
if (hf != null && m_activeFeed != null) {
Article base = hf.getArticleById(hf.getActiveArticleId());
Article prev = base != null ? getRelativeArticle(base, RelativeArticle.BEFORE) : hf.getArticleAtPosition(0);
if (prev != null) {
hf.setActiveArticleId(prev.id);
boolean combinedMode = m_prefs.getBoolean("combined_mode", false);
if (combinedMode || m_selectedArticle == null) {
prev.unread = false;
saveArticleUnread(prev);
} else {
openArticle(prev, 0);
}
}
}
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
@Override
public void onCatSelected(FeedCategory cat) {
Log.d(TAG, "onCatSelected");
viewCategory(cat, m_prefs.getBoolean("browse_cats_like_feeds", false));
boolean browse = m_prefs.getBoolean("browse_cats_like_feeds", false);
viewCategory(cat, browse && cat.id >= 0);
}
}