reimplement most menu handlers

This commit is contained in:
Andrew Dolgov 2012-09-16 20:46:54 +04:00
parent c52ea94c44
commit 3cbae613a5
5 changed files with 309 additions and 16 deletions

View File

@ -23,10 +23,12 @@ import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.util.TypedValue;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
@ -37,6 +39,7 @@ import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;
public class ArticleFragment extends Fragment {
@SuppressWarnings("unused")
@ -60,6 +63,28 @@ public class ArticleFragment extends Fragment {
private View.OnTouchListener m_gestureListener;
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
switch (item.getItemId()) {
case R.id.article_link_share:
if (true) {
((OnlineActivity) getActivity()).shareArticle(m_article);
}
return true;
case R.id.article_link_copy:
if (true) {
((OnlineActivity) getActivity()).copyToClipboard(m_article.link);
}
return true;
default:
Log.d(TAG, "onContextItemSelected, unhandled id=" + item.getItemId());
return super.onContextItemSelected(item);
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {

View File

@ -121,8 +121,17 @@ public class ArticlePager extends Fragment {
super.onAttach(activity);
m_onlineServices = (HeadlinesEventListener)activity;
((OnlineActivity)getActivity()).initMenu();
}
@Override
public void onResume() {
super.onResume();
((OnlineActivity)getActivity()).initMenu();
}
public Article getSelectedArticle() {
return m_article;
}
}

View File

@ -19,6 +19,7 @@ import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ShareActionProvider;
public class FeedsActivity extends OnlineActivity implements HeadlinesEventListener, ArticleEventListener {
private final String TAG = this.getClass().getSimpleName();
@ -71,18 +72,18 @@ public class FeedsActivity extends OnlineActivity implements HeadlinesEventListe
if (m_menu != null && m_sessionId != null) {
Fragment ff = getSupportFragmentManager().findFragmentByTag(FRAG_FEEDS);
Fragment cf = getSupportFragmentManager().findFragmentByTag(FRAG_CATS);
Fragment af = getSupportFragmentManager().findFragmentByTag(FRAG_ARTICLE);
ArticlePager af = (ArticlePager) getSupportFragmentManager().findFragmentByTag(FRAG_ARTICLE);
HeadlinesFragment hf = (HeadlinesFragment)getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
Log.d(TAG, "ff/cf/af/hf " + ff + " " + cf + " " + af + " " + hf);
m_menu.setGroupVisible(R.id.menu_group_feeds, ff != null || cf != null);
m_menu.setGroupVisible(R.id.menu_group_feeds, (ff != null && ff.isVisible()) || (cf != null && cf.isVisible()));
m_menu.setGroupVisible(R.id.menu_group_article, af != null);
m_menu.setGroupVisible(R.id.menu_group_article, af != null && af.isVisible());
m_menu.setGroupVisible(R.id.menu_group_headlines, hf != null && hf.getSelectedArticles().size() == 0);
m_menu.setGroupVisible(R.id.menu_group_headlines_selection, hf != null && hf.getSelectedArticles().size() != 0);
m_menu.setGroupVisible(R.id.menu_group_headlines, hf != null && hf.isVisible() && hf.getSelectedArticles().size() == 0);
m_menu.setGroupVisible(R.id.menu_group_headlines_selection, hf != null && hf.isVisible() && hf.getSelectedArticles().size() != 0);
MenuItem item = m_menu.findItem(R.id.show_feeds);
@ -92,6 +93,18 @@ public class FeedsActivity extends OnlineActivity implements HeadlinesEventListe
item.setTitle(R.string.menu_unread_feeds);
}
if (android.os.Build.VERSION.SDK_INT >= 14) {
ShareActionProvider shareProvider = (ShareActionProvider) m_menu.findItem(R.id.share_article).getActionProvider();
if (af != null && af.getSelectedArticle() != null) {
Log.d(TAG, "setting up share provider");
shareProvider.setShareIntent(getShareIntent(af.getSelectedArticle()));
if (!isSmallScreen()) {
m_menu.findItem(R.id.share_article).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
}
}
}
}
@ -230,6 +243,7 @@ public class FeedsActivity extends OnlineActivity implements HeadlinesEventListe
startActivityForResult(intent, 0);
}
} else {
initMenu();
/* HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
if (hf != null) hf.setActiveArticle(article); */
}
@ -240,4 +254,9 @@ public class FeedsActivity extends OnlineActivity implements HeadlinesEventListe
onArticleSelected(article, true);
}
@SuppressWarnings("unchecked")
public void catchupFeed(final Feed feed) {
super.catchupFeed(feed);
refresh();
}
}

View File

@ -115,6 +115,34 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
.getMenuInfo();
switch (item.getItemId()) {
case R.id.set_labels:
if (true) {
Article article = getArticleAtPosition(info.position);
if (article != null) {
m_activity.editArticleLabels(article);
}
}
return true;
case R.id.article_set_note:
if (true) {
Article article = getArticleAtPosition(info.position);
if (article != null) {
m_activity.editArticleNote(article);
}
}
return true;
case R.id.article_link_copy:
if (true) {
Article article = getArticleAtPosition(info.position);
if (article != null) {
m_activity.copyToClipboard(article.link);
}
}
return true;
case R.id.selection_toggle_marked:
if (true) {
ArticleList selected = getSelectedArticles();
@ -735,13 +763,9 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
/* public void notifyUpdated() {
public void notifyUpdated() {
m_adapter.notifyDataSetChanged();
Article article = m_onlineServices.getSelectedArticle();
setActiveArticle(article);
} */
}
public ArticleList getAllArticles() {
return m_articles;

View File

@ -1,13 +1,18 @@
package org.fox.ttrss;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import org.fox.ttrss.types.Article;
import org.fox.ttrss.types.ArticleList;
import org.fox.ttrss.types.Feed;
import org.fox.ttrss.types.Label;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import android.app.AlertDialog;
import android.app.Dialog;
@ -16,6 +21,7 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentTransaction;
@ -139,7 +145,8 @@ public class OnlineActivity extends CommonActivity {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
final HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
final ArticlePager ap = (ArticlePager)getSupportFragmentManager().findFragmentByTag(FRAG_ARTICLE);
switch (item.getItemId()) {
case R.id.logout:
logout();
@ -150,6 +157,11 @@ public class OnlineActivity extends CommonActivity {
case R.id.go_offline:
// FIXME go offline
return true;
case R.id.article_set_note:
if (ap != null && ap.getSelectedArticle() != null) {
editArticleNote(ap.getSelectedArticle());
}
return true;
case R.id.preferences:
Intent intent = new Intent(OnlineActivity.this,
PreferencesActivity.class);
@ -254,12 +266,218 @@ public class OnlineActivity extends CommonActivity {
dialog.show();
}
return true;
case R.id.share_article:
if (android.os.Build.VERSION.SDK_INT < 14) {
if (ap != null) {
shareArticle(ap.getSelectedArticle());
}
}
return true;
case R.id.toggle_marked:
if (ap != null & ap.getSelectedArticle() != null) {
Article a = ap.getSelectedArticle();
a.marked = !a.marked;
saveArticleMarked(a);
if (hf != null) hf.notifyUpdated();
}
return true;
case R.id.selection_select_none:
if (hf != null) {
ArticleList selected = hf.getSelectedArticles();
if (selected.size() > 0) {
selected.clear();
initMenu();
hf.notifyUpdated();
}
}
return true;
case R.id.selection_toggle_unread:
if (hf != null) {
ArticleList selected = hf.getSelectedArticles();
if (selected.size() > 0) {
for (Article a : selected)
a.unread = !a.unread;
toggleArticlesUnread(selected);
hf.notifyUpdated();
}
}
return true;
case R.id.selection_toggle_marked:
if (hf != null) {
ArticleList selected = hf.getSelectedArticles();
if (selected.size() > 0) {
for (Article a : selected)
a.marked = !a.marked;
toggleArticlesMarked(selected);
hf.notifyUpdated();
}
}
return true;
case R.id.selection_toggle_published:
if (hf != null) {
ArticleList selected = hf.getSelectedArticles();
if (selected.size() > 0) {
for (Article a : selected)
a.published = !a.published;
toggleArticlesPublished(selected);
hf.notifyUpdated();
}
}
return true;
case R.id.toggle_published:
if (ap != null && ap.getSelectedArticle() != null) {
Article a = ap.getSelectedArticle();
a.published = !a.published;
saveArticlePublished(a);
if (hf != null) hf.notifyUpdated();
}
return true;
case R.id.catchup_above:
if (hf != null) {
if (ap != null && ap.getSelectedArticle() != null) {
Article article = ap.getSelectedArticle();
ArticleList articles = hf.getAllArticles();
ArticleList tmp = new ArticleList();
for (Article a : articles) {
a.unread = false;
tmp.add(a);
if (article.id == a.id)
break;
}
if (tmp.size() > 0) {
toggleArticlesUnread(tmp);
hf.notifyUpdated();
}
}
}
return true;
case R.id.set_unread:
if (ap != null && ap.getSelectedArticle() != null) {
Article a = ap.getSelectedArticle();
a.unread = true;
saveArticleUnread(a);
if (hf != null) hf.notifyUpdated();
}
return true;
case R.id.set_labels:
if (ap != null && ap.getSelectedArticle() != null) {
editArticleLabels(ap.getSelectedArticle());
}
return true;
default:
Log.d(TAG, "onOptionsItemSelected, unhandled id=" + item.getItemId());
return super.onOptionsItemSelected(item);
}
}
public void editArticleNote(final Article article) {
String note = "";
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(article.title);
final EditText topicEdit = new EditText(this);
topicEdit.setText(note);
builder.setView(topicEdit);
builder.setPositiveButton(R.string.article_set_note, new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
saveArticleNote(article, topicEdit.getText().toString().trim());
article.published = true;
saveArticlePublished(article);
HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
if (hf != null) hf.notifyUpdated();
}
});
builder.setNegativeButton(R.string.dialog_cancel, new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
public void editArticleLabels(Article article) {
final int articleId = article.id;
ApiRequest req = new ApiRequest(getApplicationContext()) {
@Override
protected void onPostExecute(JsonElement result) {
if (result != null) {
Type listType = new TypeToken<List<Label>>() {}.getType();
final List<Label> labels = new Gson().fromJson(result, listType);
CharSequence[] items = new CharSequence[labels.size()];
final int[] itemIds = new int[labels.size()];
boolean[] checkedItems = new boolean[labels.size()];
for (int i = 0; i < labels.size(); i++) {
items[i] = labels.get(i).caption;
itemIds[i] = labels.get(i).id;
checkedItems[i] = labels.get(i).checked;
}
Dialog dialog = new Dialog(OnlineActivity.this);
AlertDialog.Builder builder = new AlertDialog.Builder(OnlineActivity.this)
.setTitle(R.string.article_set_labels)
.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, final boolean isChecked) {
final int labelId = itemIds[which];
@SuppressWarnings("serial")
HashMap<String, String> map = new HashMap<String, String>() {
{
put("sid", m_sessionId);
put("op", "setArticleLabel");
put("label_id", String.valueOf(labelId));
put("article_ids", String.valueOf(articleId));
if (isChecked) put("assign", "true");
}
};
ApiRequest req = new ApiRequest(m_context);
req.execute(map);
}
}).setPositiveButton(R.string.dialog_close, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog = builder.create();
dialog.show();
}
}
};
@SuppressWarnings("serial")
HashMap<String, String> map = new HashMap<String, String>() {
{
put("sid", m_sessionId);
put("op", "getLabels");
put("article_id", String.valueOf(articleId));
}
};
req.execute(map);
}
protected void logout() {
m_sessionId = null;
@ -428,7 +646,7 @@ public class OnlineActivity extends CommonActivity {
}
}
private Intent getShareIntent(Article article) {
protected Intent getShareIntent(Article article) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
@ -608,8 +826,6 @@ public class OnlineActivity extends CommonActivity {
Log.d(TAG, "Received API level: " + m_apiLevel);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
loginSuccess();
return;
}