more offline menu-relate stuff

This commit is contained in:
Andrew Dolgov 2011-12-06 12:26:19 +03:00
parent 1145eae894
commit d8da03554f
3 changed files with 222 additions and 36 deletions

View File

@ -35,6 +35,7 @@ import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import com.google.ads.c;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
@ -58,8 +59,6 @@ public class OfflineActivity extends FragmentActivity {
private SQLiteDatabase m_readableDb; private SQLiteDatabase m_readableDb;
private SQLiteDatabase m_writableDb; private SQLiteDatabase m_writableDb;
/** Called when the activity is first created. */
public boolean isSmallScreen() { public boolean isSmallScreen() {
return m_smallScreenMode; return m_smallScreenMode;
} }
@ -219,7 +218,7 @@ public class OfflineActivity extends FragmentActivity {
public void setUnreadOnly(boolean unread) { public void setUnreadOnly(boolean unread) {
m_unreadOnly = unread; m_unreadOnly = unread;
refreshFeeds(); refreshViews();
/*if (!m_enableCats || m_activeCategory != null ) /*if (!m_enableCats || m_activeCategory != null )
refreshFeeds(); refreshFeeds();
@ -295,6 +294,7 @@ public class OfflineActivity extends FragmentActivity {
findViewById(R.id.feeds_fragment).setVisibility(View.VISIBLE); findViewById(R.id.feeds_fragment).setVisibility(View.VISIBLE);
//} //}
m_activeFeedId = 0; m_activeFeedId = 0;
refreshViews();
initMainMenu(); initMainMenu();
} else { } else {
@ -326,20 +326,30 @@ public class OfflineActivity extends FragmentActivity {
Cursor article = getArticleById(articleId); Cursor article = getArticleById(articleId);
if (article.isFirst()) { if (article != null) {
shareArticle(article);
article.close();
}
}
public void shareArticle(Cursor article) {
if (article != null) {
String title = article.getString(article.getColumnIndex("title"));
String link = article.getString(article.getColumnIndex("link"));
Intent intent = new Intent(Intent.ACTION_SEND); Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain"); intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, article.getString(article.getColumnIndex("title"))); intent.putExtra(Intent.EXTRA_SUBJECT, title);
intent.putExtra(Intent.EXTRA_TEXT, article.getString(article.getColumnIndex("link"))); intent.putExtra(Intent.EXTRA_TEXT, link);
startActivity(Intent.createChooser(intent, getString(R.id.share_article))); startActivity(Intent.createChooser(intent, getString(R.id.share_article)));
} }
article.close();
} }
public void updateHeadlines() { public void refreshHeadlines() {
OfflineHeadlinesFragment ohf = (OfflineHeadlinesFragment)getSupportFragmentManager().findFragmentById(R.id.headlines_fragment); OfflineHeadlinesFragment ohf = (OfflineHeadlinesFragment)getSupportFragmentManager().findFragmentById(R.id.headlines_fragment);
if (ohf != null) { if (ohf != null) {
@ -393,7 +403,7 @@ public class OfflineActivity extends FragmentActivity {
break; break;
} }
updateHeadlines(); refreshViews();
initMainMenu(); initMainMenu();
dialog.cancel(); dialog.cancel();
@ -410,7 +420,7 @@ public class OfflineActivity extends FragmentActivity {
stmt.bindLong(1, m_activeFeedId); stmt.bindLong(1, m_activeFeedId);
stmt.execute(); stmt.execute();
stmt.close(); stmt.close();
updateHeadlines(); refreshViews();
} }
return true; return true;
case R.id.share_article: case R.id.share_article:
@ -422,13 +432,38 @@ public class OfflineActivity extends FragmentActivity {
stmt.bindLong(1, m_selectedArticleId); stmt.bindLong(1, m_selectedArticleId);
stmt.execute(); stmt.execute();
stmt.close(); stmt.close();
updateHeadlines(); refreshViews();
} }
return true; return true;
case R.id.selection_select_none: case R.id.selection_select_none:
deselectAllArticles();
return true;
case R.id.selection_toggle_unread: case R.id.selection_toggle_unread:
if (getSelectedArticleCount() > 0 && m_activeFeedId != 0) {
SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE articles SET unread = NOT unread WHERE selected = 1 AND feed_id = ?");
stmt.bindLong(1, m_activeFeedId);
stmt.execute();
stmt.close();
refreshViews();
}
return true;
case R.id.selection_toggle_marked: case R.id.selection_toggle_marked:
if (getSelectedArticleCount() > 0 && m_activeFeedId != 0) {
SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE articles SET marked = NOT marked WHERE selected = 1 AND feed_id = ?");
stmt.bindLong(1, m_activeFeedId);
stmt.execute();
stmt.close();
refreshViews();
}
return true;
case R.id.selection_toggle_published: case R.id.selection_toggle_published:
if (getSelectedArticleCount() > 0 && m_activeFeedId != 0) {
SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE articles SET published = NOT published WHERE selected = 1 AND feed_id = ?");
stmt.bindLong(1, m_activeFeedId);
stmt.execute();
stmt.close();
refreshViews();
}
return true; return true;
case R.id.toggle_published: case R.id.toggle_published:
if (m_selectedArticleId != 0) { if (m_selectedArticleId != 0) {
@ -436,12 +471,18 @@ public class OfflineActivity extends FragmentActivity {
stmt.bindLong(1, m_selectedArticleId); stmt.bindLong(1, m_selectedArticleId);
stmt.execute(); stmt.execute();
stmt.close(); stmt.close();
updateHeadlines(); refreshViews();
} }
return true; return true;
case R.id.catchup_above: case R.id.catchup_above:
if (m_selectedArticleId != 0) { if (m_selectedArticleId != 0 && m_activeFeedId != 0) {
// SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE articles SET unread = 0 WHERE updated >= " +
"(SELECT updated FROM articles WHERE " + BaseColumns._ID + " = ?) AND feed_id = ?");
stmt.bindLong(1, m_selectedArticleId);
stmt.bindLong(2, m_activeFeedId);
stmt.execute();
stmt.close();
refreshViews();
} }
return true; return true;
case R.id.set_unread: case R.id.set_unread:
@ -450,7 +491,7 @@ public class OfflineActivity extends FragmentActivity {
stmt.bindLong(1, m_selectedArticleId); stmt.bindLong(1, m_selectedArticleId);
stmt.execute(); stmt.execute();
stmt.close(); stmt.close();
updateHeadlines(); refreshViews();
} }
return true; return true;
case R.id.show_feeds: case R.id.show_feeds:
@ -493,17 +534,17 @@ public class OfflineActivity extends FragmentActivity {
m_selectedArticleId = 0; m_selectedArticleId = 0;
initMainMenu(); initMainMenu();
refreshFeeds(); refreshViews();
} }
public int getSelectedArticles() { public int getSelectedArticleCount() {
Cursor c = getReadableDb().query("articles", new String[] { "COUNT(*)" }, "selected = 1", null, null, null, null); Cursor c = getReadableDb().query("articles", new String[] { "COUNT(*)" }, "selected = 1", null, null, null, null);
c.moveToFirst(); c.moveToFirst();
int unread = c.getInt(0); int selected = c.getInt(0);
c.close(); c.close();
return unread; return selected;
} }
public void initMainMenu() { public void initMainMenu() {
@ -531,7 +572,7 @@ public class OfflineActivity extends FragmentActivity {
OfflineHeadlinesFragment hf = (OfflineHeadlinesFragment)getSupportFragmentManager().findFragmentById(R.id.headlines_fragment); OfflineHeadlinesFragment hf = (OfflineHeadlinesFragment)getSupportFragmentManager().findFragmentById(R.id.headlines_fragment);
if (hf != null) { if (hf != null) {
if (getSelectedArticles() != 0) { if (getSelectedArticleCount() != 0) {
m_menu.setGroupVisible(R.id.menu_group_headlines, false); m_menu.setGroupVisible(R.id.menu_group_headlines, false);
m_menu.setGroupVisible(R.id.menu_group_headlines_selection, true); m_menu.setGroupVisible(R.id.menu_group_headlines_selection, true);
} else { } else {
@ -576,11 +617,118 @@ public class OfflineActivity extends FragmentActivity {
} }
public void refreshViews() {
refreshFeeds();
refreshHeadlines();
}
@Override @Override
public boolean onContextItemSelected(MenuItem item) { public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
OfflineHeadlinesFragment hf = (OfflineHeadlinesFragment)getSupportFragmentManager().findFragmentById(R.id.headlines_fragment);
OfflineFeedsFragment ff = (OfflineFeedsFragment)getSupportFragmentManager().findFragmentById(R.id.feeds_fragment);
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.browse_articles:
// TODO cat stuff
return true;
case R.id.browse_feeds:
// TODO cat stuff
return true;
case R.id.catchup_category:
// TODO cat stuff
return true;
case R.id.catchup_feed:
if (ff != null) {
int feedId = ff.getFeedIdAtPosition(info.position);
if (feedId != 0) {
SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE articles SET unread = 0 WHERE feed_id = ?");
stmt.bindLong(1, feedId);
stmt.execute();
stmt.close();
refreshViews();
}
}
return true;
case R.id.selection_toggle_unread:
if (getSelectedArticleCount() > 0 && m_activeFeedId != 0) {
SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE articles SET unread = NOT unread WHERE selected = 1 AND feed_id = ?");
stmt.bindLong(1, m_activeFeedId);
stmt.execute();
stmt.close();
refreshViews();
} else {
int articleId = hf.getArticleIdAtPosition(info.position);
if (articleId != 0) {
SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE articles SET unread = NOT unread WHERE " +
BaseColumns._ID + " = ?");
stmt.bindLong(1, articleId);
stmt.execute();
stmt.close();
refreshViews();
}
}
return true;
case R.id.selection_toggle_marked:
if (getSelectedArticleCount() > 0 && m_activeFeedId != 0) {
SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE articles SET marked = NOT marked WHERE selected = 1 AND feed_id = ?");
stmt.bindLong(1, m_activeFeedId);
stmt.execute();
stmt.close();
refreshViews();
} else {
int articleId = hf.getArticleIdAtPosition(info.position);
if (articleId != 0) {
SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE articles SET marked = NOT marked WHERE " +
BaseColumns._ID + " = ?");
stmt.bindLong(1, articleId);
stmt.execute();
stmt.close();
refreshViews();
}
}
return true;
case R.id.selection_toggle_published:
if (getSelectedArticleCount() > 0 && m_activeFeedId != 0) {
SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE articles SET published = NOT published WHERE selected = 1 AND feed_id = ?");
stmt.bindLong(1, m_activeFeedId);
stmt.execute();
stmt.close();
refreshViews();
} else {
int articleId = hf.getArticleIdAtPosition(info.position);
if (articleId != 0) {
SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE articles SET published = NOT published WHERE " +
BaseColumns._ID + " = ?");
stmt.bindLong(1, articleId);
stmt.execute();
stmt.close();
refreshViews();
}
}
return true;
case R.id.share_article:
Cursor article = hf.getArticleAtPosition(info.position);
if (article != null) {
shareArticle(article);
}
return true;
case R.id.catchup_above:
int articleId = hf.getArticleIdAtPosition(info.position);
if (articleId != 0 && m_activeFeedId != 0) {
SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE articles SET unread = 0 WHERE updated >= " +
"(SELECT updated FROM articles WHERE " + BaseColumns._ID + " = ?) AND feed_id = ?");
stmt.bindLong(1, articleId);
stmt.bindLong(2, m_activeFeedId);
stmt.execute();
stmt.close();
refreshViews();
}
return true;
default: default:
Log.d(TAG, "onContextItemSelected, unhandled id=" + item.getItemId()); Log.d(TAG, "onContextItemSelected, unhandled id=" + item.getItemId());
return super.onContextItemSelected(item); return super.onContextItemSelected(item);

View File

@ -61,12 +61,14 @@ public class OfflineFeedsFragment extends Fragment implements OnItemClickListene
} }
public void refresh() { public void refresh() {
if (m_cursor != null) m_cursor.close(); if (m_cursor != null && !m_cursor.isClosed()) m_cursor.close();
m_cursor = createCursor(); m_cursor = createCursor();
m_adapter.changeCursor(m_cursor); if (m_cursor != null) {
m_adapter.notifyDataSetChanged(); m_adapter.changeCursor(m_cursor);
m_adapter.notifyDataSetChanged();
}
} }
@Override @Override
@ -101,7 +103,7 @@ public class OfflineFeedsFragment extends Fragment implements OnItemClickListene
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
m_cursor.close(); if (m_cursor != null && !m_cursor.isClosed()) m_cursor.close();
} }
@Override @Override
@ -261,4 +263,16 @@ public class OfflineFeedsFragment extends Fragment implements OnItemClickListene
} }
public int getFeedIdAtPosition(int position) {
Cursor c = (Cursor)m_adapter.getItem(position);
if (c != null) {
int feedId = c.getInt(0);
c.close();
return feedId;
}
return 0;
}
} }

View File

@ -66,7 +66,17 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
m_cursor.close(); if (m_cursor != null && !m_cursor.isClosed()) m_cursor.close();
}
public int getSelectedArticleCount() {
Cursor c = ((OfflineActivity)getActivity()).getReadableDb().query("articles",
new String[] { "COUNT(*)" }, "selected = 1", null, null, null, null);
c.moveToFirst();
int selected = c.getInt(0);
c.close();
return selected;
} }
@Override @Override
@ -75,27 +85,30 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
getActivity().getMenuInflater().inflate(R.menu.headlines_menu, menu); getActivity().getMenuInflater().inflate(R.menu.headlines_menu, menu);
//if (m_selectedArticles.size() > 0) { if (getSelectedArticleCount() > 0) {
// menu.setHeaderTitle(R.string.headline_context_multiple); menu.setHeaderTitle(R.string.headline_context_multiple);
// menu.setGroupVisible(R.id.menu_group_single_article, false); menu.setGroupVisible(R.id.menu_group_single_article, false);
//} else { } else {
AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo; AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
Cursor c = getArticleAtPosition(info.position); Cursor c = getArticleAtPosition(info.position);
menu.setHeaderTitle(c.getString(c.getColumnIndex("title"))); menu.setHeaderTitle(c.getString(c.getColumnIndex("title")));
//c.close();
menu.setGroupVisible(R.id.menu_group_single_article, true); menu.setGroupVisible(R.id.menu_group_single_article, true);
//} }
super.onCreateContextMenu(menu, v, menuInfo); super.onCreateContextMenu(menu, v, menuInfo);
} }
public void refresh() { public void refresh() {
if (m_cursor != null) m_cursor.close(); if (m_cursor != null && !m_cursor.isClosed()) m_cursor.close();
m_cursor = createCursor(); m_cursor = createCursor();
m_adapter.changeCursor(m_cursor); if (m_cursor != null) {
m_adapter.notifyDataSetChanged(); m_adapter.changeCursor(m_cursor);
m_adapter.notifyDataSetChanged();
}
} }
@Override @Override
@ -393,7 +406,18 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
public Cursor getArticleAtPosition(int position) { public Cursor getArticleAtPosition(int position) {
return (Cursor) m_adapter.getItem(position); return (Cursor) m_adapter.getItem(position);
} }
public int getArticleIdAtPosition(int position) {
Cursor c = getArticleAtPosition(position);
if (c != null) {
int id = c.getInt(0);
return id;
}
return 0;
}
public int getActiveArticleId() { public int getActiveArticleId() {
return m_activeArticleId; return m_activeArticleId;
} }