switch mainactivity typecasting to an interface

This commit is contained in:
Andrew Dolgov 2011-12-06 15:52:33 +03:00
parent 5178ba7287
commit a86697dcdf
9 changed files with 99 additions and 88 deletions

View File

@ -3,7 +3,7 @@ package org.fox.ttrss;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.fox.ttrss.ArticleOps.RelativeArticle;
import org.fox.ttrss.OnlineServices.RelativeArticle;
import android.app.Activity;
import android.content.SharedPreferences;
@ -32,7 +32,7 @@ public class ArticleFragment extends Fragment implements OnClickListener {
private SharedPreferences m_prefs;
private Article m_article;
private ArticleOps m_articleOps;
private OnlineServices m_onlineServices;
private Article m_nextArticle;
private Article m_prevArticle;
private GestureDetector m_gestureDetector;
@ -58,13 +58,12 @@ public class ArticleFragment extends Fragment implements OnClickListener {
};
// TODO change to interface?
MainActivity activity = (MainActivity)getActivity();
Activity activity = (Activity)getActivity();
if (activity != null) {
int orientation = activity.getWindowManager().getDefaultDisplay().getOrientation();
if (!activity.isSmallScreen()) {
if (!m_onlineServices.isSmallScreen()) {
if (orientation % 2 == 0) {
view.findViewById(R.id.splitter_horizontal).setVisibility(View.GONE);
} else {
@ -129,7 +128,7 @@ public class ArticleFragment extends Fragment implements OnClickListener {
web.loadDataWithBaseURL(null, content, "text/html", "utf-8", null);
if (activity.isSmallScreen())
if (m_onlineServices.isSmallScreen())
web.setOnTouchListener(m_gestureListener);
}
@ -161,7 +160,7 @@ public class ArticleFragment extends Fragment implements OnClickListener {
AdView av = (AdView)view.findViewById(R.id.ad);
if (av != null) {
if (!((MainActivity)getActivity()).getLicensed()) {
if (!m_onlineServices.getLicensed()) {
AdRequest request = new AdRequest();
request.addTestDevice(AdRequest.TEST_EMULATOR);
av.loadAd(request);
@ -212,19 +211,19 @@ public class ArticleFragment extends Fragment implements OnClickListener {
super.onAttach(activity);
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
m_articleOps = (ArticleOps)activity;
m_article = m_articleOps.getSelectedArticle();
m_onlineServices = (OnlineServices)activity;
m_article = m_onlineServices.getSelectedArticle();
m_prevArticle = m_articleOps.getRelativeArticle(m_article, RelativeArticle.BEFORE);
m_nextArticle = m_articleOps.getRelativeArticle(m_article, RelativeArticle.AFTER);
m_prevArticle = m_onlineServices.getRelativeArticle(m_article, RelativeArticle.BEFORE);
m_nextArticle = m_onlineServices.getRelativeArticle(m_article, RelativeArticle.AFTER);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.next_article) {
m_articleOps.openArticle(m_nextArticle, 0);
m_onlineServices.openArticle(m_nextArticle, 0);
} else if (v.getId() == R.id.prev_article) {
m_articleOps.openArticle(m_prevArticle, R.anim.slide_right);
m_onlineServices.openArticle(m_prevArticle, R.anim.slide_right);
}
}
@ -245,13 +244,13 @@ public class ArticleFragment extends Fragment implements OnClickListener {
//Log.d(TAG, "Right swipe");
if (m_prevArticle != null)
m_articleOps.openArticle(m_prevArticle, R.anim.slide_right);
m_onlineServices.openArticle(m_prevArticle, R.anim.slide_right);
} else {
//Log.d(TAG, "Left swipe");
if (m_nextArticle != null)
m_articleOps.openArticle(m_nextArticle, 0);
m_onlineServices.openArticle(m_nextArticle, 0);
}
return true;

View File

@ -1,14 +0,0 @@
package org.fox.ttrss;
public interface ArticleOps {
public enum RelativeArticle { BEFORE, AFTER };
public Article getSelectedArticle();
public void saveArticleUnread(final Article article);
public void saveArticleMarked(final Article article);
public void saveArticlePublished(final Article article);
public void updateHeadlines();
public void openArticle(Article article, int compatAnimation);
public Article getRelativeArticle(Article article, RelativeArticle ra);
}

View File

@ -39,11 +39,7 @@ public class FeedCategoriesFragment extends Fragment implements OnItemClickListe
private FeedCategoryListAdapter m_adapter;
private FeedCategoryList m_cats = new FeedCategoryList();
private int m_selectedCatId = -100;
private OnCatSelectedListener m_catSelectedListener;
public interface OnCatSelectedListener {
public void onCatSelected(FeedCategory cat);
}
private OnlineServices m_onlineServices;
class CatUnreadComparator implements Comparator<FeedCategory> {
@Override
@ -115,7 +111,7 @@ public class FeedCategoriesFragment extends Fragment implements OnItemClickListe
public void onAttach(Activity activity) {
super.onAttach(activity);
m_catSelectedListener = (OnCatSelectedListener)activity;
m_onlineServices = (OnlineServices)activity;
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
m_prefs.registerOnSharedPreferenceChangeListener(this);
@ -150,8 +146,8 @@ public class FeedCategoriesFragment extends Fragment implements OnItemClickListe
public void refresh(boolean background) {
CatsRequest req = new CatsRequest(getActivity().getApplicationContext());
final String sessionId = ((MainActivity)getActivity()).getSessionId();
final boolean unreadOnly = ((MainActivity)getActivity()).getUnreadOnly();
final String sessionId = m_onlineServices.getSessionId();
final boolean unreadOnly = m_onlineServices.getUnreadOnly();
if (sessionId != null) {
@ -196,7 +192,7 @@ public class FeedCategoriesFragment extends Fragment implements OnItemClickListe
m_cats.clear();
int apiLevel = ((MainActivity)getActivity()).getApiLevel();
int apiLevel = m_onlineServices.getApiLevel();
// virtual cats implemented in getCategories since api level 1
if (apiLevel == 0) {
@ -224,8 +220,7 @@ public class FeedCategoriesFragment extends Fragment implements OnItemClickListe
}
if (m_lastError == ApiError.LOGIN_FAILED) {
MainActivity activity = (MainActivity)getActivity();
activity.login();
m_onlineServices.login();
} else {
setLoadingStatus(getErrorMessage(), false);
}
@ -332,7 +327,7 @@ public class FeedCategoriesFragment extends Fragment implements OnItemClickListe
if (list != null) {
FeedCategory cat = (FeedCategory)list.getItemAtPosition(position);
m_catSelectedListener.onCatSelected(cat);
m_onlineServices.onCatSelected(cat);
m_selectedCatId = cat.id;
m_adapter.notifyDataSetChanged();
}

View File

@ -61,16 +61,12 @@ public class FeedsFragment extends Fragment implements OnItemClickListener, OnSh
private SharedPreferences m_prefs;
private FeedListAdapter m_adapter;
private FeedList m_feeds = new FeedList();
private OnFeedSelectedListener m_feedSelectedListener;
private OnlineServices m_onlineServices;
private int m_selectedFeedId;
private static final String ICON_PATH = "/org.fox.ttrss/icons/";
private boolean m_enableFeedIcons;
private boolean m_feedIconsChecked = false;
public interface OnFeedSelectedListener {
public void onFeedSelected(Feed feed);
}
class FeedUnreadComparator implements Comparator<Feed> {
@Override
@ -153,9 +149,9 @@ public class FeedsFragment extends Fragment implements OnItemClickListener, OnSh
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
m_prefs.registerOnSharedPreferenceChangeListener(this);
m_feedSelectedListener = (OnFeedSelectedListener) activity;
m_onlineServices = (OnlineServices)activity;
Feed activeFeed = ((MainActivity)activity).getActiveFeed();
Feed activeFeed = m_onlineServices.getActiveFeed();
if (activeFeed != null)
m_selectedFeedId = activeFeed.id;
@ -176,7 +172,7 @@ public class FeedsFragment extends Fragment implements OnItemClickListener, OnSh
if (list != null) {
Feed feed = (Feed)list.getItemAtPosition(position);
m_feedSelectedListener.onFeedSelected(feed);
m_onlineServices.onFeedSelected(feed);
m_selectedFeedId = feed.id;
m_adapter.notifyDataSetChanged();
}
@ -184,12 +180,12 @@ public class FeedsFragment extends Fragment implements OnItemClickListener, OnSh
@SuppressWarnings({ "unchecked", "serial" })
public void refresh(boolean background) {
FeedCategory cat = ((MainActivity)getActivity()).getActiveCategory();
FeedCategory cat = m_onlineServices.getActiveCategory();
final int catId = (cat != null) ? cat.id : -4;
final String sessionId = ((MainActivity)getActivity()).getSessionId();
final boolean unreadOnly = ((MainActivity)getActivity()).getUnreadOnly();
final String sessionId = m_onlineServices.getSessionId();
final boolean unreadOnly = m_onlineServices.getUnreadOnly();
FeedsRequest req = new FeedsRequest(getActivity().getApplicationContext(), catId);
@ -270,7 +266,7 @@ public class FeedsFragment extends Fragment implements OnItemClickListener, OnSh
}
};
final String sessionId = ((MainActivity)getActivity()).getSessionId();
final String sessionId = m_onlineServices.getSessionId();
HashMap<String,String> map = new HashMap<String,String>() {
{
@ -323,8 +319,7 @@ public class FeedsFragment extends Fragment implements OnItemClickListener, OnSh
}
if (m_lastError == ApiError.LOGIN_FAILED) {
MainActivity activity = (MainActivity)getActivity();
activity.login();
m_onlineServices.login();
} else {
setLoadingStatus(getErrorMessage(), false);
}

View File

@ -65,7 +65,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
private ArticleList m_articles = new ArticleList();
private ArticleList m_selectedArticles = new ArticleList();
private ArticleOps m_articleOps;
private OnlineServices m_onlineServices;
private ImageGetter m_dummyGetter = new ImageGetter() {
@ -135,9 +135,10 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
m_feed = ((MainActivity)activity).getActiveFeed();
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
m_articleOps = (ArticleOps) activity;
m_onlineServices = (OnlineServices) activity;
m_feed = m_onlineServices.getActiveFeed();
m_combinedMode = m_prefs.getBoolean("combined_mode", false);
}
@ -152,9 +153,9 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
if (article.id >= 0) {
if (m_combinedMode) {
article.unread = false;
m_articleOps.saveArticleUnread(article);
m_onlineServices.saveArticleUnread(article);
} else {
m_articleOps.openArticle(article, 0);
m_onlineServices.openArticle(article, 0);
}
m_activeArticleId = article.id;
@ -169,8 +170,8 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
HeadlinesRequest req = new HeadlinesRequest(getActivity().getApplicationContext());
final String sessionId = ((MainActivity)getActivity()).getSessionId();
final boolean showUnread = ((MainActivity)getActivity()).getUnreadArticlesOnly();
final String sessionId = m_onlineServices.getSessionId();
final boolean showUnread = m_onlineServices.getUnreadArticlesOnly();
final boolean isCat = m_feed.is_cat;
int skip = 0;
@ -287,8 +288,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
}
if (m_lastError == ApiError.LOGIN_FAILED) {
MainActivity activity = (MainActivity)getActivity();
activity.login();
m_onlineServices.login();
} else {
setLoadingStatus(getErrorMessage(), false);
}
@ -386,7 +386,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
article.marked = !article.marked;
m_adapter.notifyDataSetChanged();
m_articleOps.saveArticleMarked(article);
m_onlineServices.saveArticleMarked(article);
}
});
}
@ -403,7 +403,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
article.published = !article.published;
m_adapter.notifyDataSetChanged();
m_articleOps.saveArticlePublished(article);
m_onlineServices.saveArticlePublished(article);
}
});
}
@ -464,7 +464,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
m_selectedArticles.remove(article);
}
((MainActivity)getActivity()).initMainMenu();
m_onlineServices.initMainMenu();
Log.d(TAG, "num selected: " + m_selectedArticles.size());
}

View File

@ -40,7 +40,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
public class MainActivity extends FragmentActivity implements FeedsFragment.OnFeedSelectedListener, ArticleOps, FeedCategoriesFragment.OnCatSelectedListener {
public class MainActivity extends FragmentActivity implements OnlineServices {
private final String TAG = this.getClass().getSimpleName();
private final int OFFLINE_SYNC_SEQ = 60;
@ -75,15 +75,17 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
}
}
@Override
public boolean getLicensed() {
return m_isLicensed == 1;
}
@Override
public int getApiLevel() {
return m_apiLevel;
}
public boolean hasPendingOfflineData() {
private boolean hasPendingOfflineData() {
Cursor c = getReadableDb().query("articles",
new String[] { "COUNT(*)" }, "modified = 1", null, null, null, null);
if (c.moveToFirst()) {
@ -100,7 +102,7 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
getWritableDb().execSQL("UPDATE articles SET modified = 0");
}
public boolean hasOfflineData() {
private boolean hasOfflineData() {
Cursor c = getReadableDb().query("articles",
new String[] { "COUNT(*)" }, null, null, null, null, null);
if (c.moveToFirst()) {
@ -201,7 +203,7 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
}
@SuppressWarnings("unchecked")
public void toggleArticlesMarked(final ArticleList articles) {
private void toggleArticlesMarked(final ArticleList articles) {
ApiRequest req = new ApiRequest(getApplicationContext());
@SuppressWarnings("serial")
@ -219,7 +221,7 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
}
@SuppressWarnings("unchecked")
public void toggleArticlesUnread(final ArticleList articles) {
private void toggleArticlesUnread(final ArticleList articles) {
ApiRequest req = new ApiRequest(getApplicationContext());
@SuppressWarnings("serial")
@ -237,7 +239,7 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
}
@SuppressWarnings("unchecked")
public void toggleArticlesPublished(final ArticleList articles) {
private void toggleArticlesPublished(final ArticleList articles) {
ApiRequest req = new ApiRequest(getApplicationContext());
@SuppressWarnings("serial")
@ -295,7 +297,7 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
}
}
public void setUnreadOnly(boolean unread) {
private void setUnreadOnly(boolean unread) {
m_unreadOnly = unread;
if (!m_enableCats || m_activeCategory != null )
@ -304,11 +306,12 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
refreshCategories();
}
@Override
public boolean getUnreadOnly() {
return m_unreadOnly;
}
public void setUnreadArticlesOnly(boolean unread) {
private void setUnreadArticlesOnly(boolean unread) {
m_unreadArticlesOnly = unread;
HeadlinesFragment frag = (HeadlinesFragment)getSupportFragmentManager().findFragmentById(R.id.headlines_fragment);
@ -316,16 +319,17 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
if (frag != null) frag.refresh(false);
}
@Override
public boolean getUnreadArticlesOnly() {
return m_unreadArticlesOnly;
}
@Override
public String getSessionId() {
return m_sessionId;
}
/** Called when the activity is first created. */
@Override
public boolean isSmallScreen() {
return m_smallScreenMode;
}
@ -441,7 +445,7 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
}
public void initDatabase() {
private void initDatabase() {
DatabaseHelper dh = new DatabaseHelper(getApplicationContext());
m_writableDb = dh.getWritableDatabase();
m_readableDb = dh.getReadableDatabase();
@ -456,7 +460,7 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
}
@SuppressWarnings("unchecked")
public void offlineGetArticles() {
private void offlineGetArticles() {
Log.d(TAG, "offline: downloading articles... offset=" + m_articleOffset);
OfflineArticlesRequest req = new OfflineArticlesRequest(this);
@ -477,7 +481,7 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
}
@SuppressWarnings("unchecked")
public void switchOffline() {
private void switchOffline() {
AlertDialog.Builder builder = new AlertDialog.Builder(this).
setMessage(R.string.dialog_offline_switch_prompt).
@ -567,7 +571,7 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
}
public void switchOfflineSuccess() {
private void switchOfflineSuccess() {
logout();
//setLoadingStatus(R.string.blank, false);
@ -581,7 +585,7 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
}
public void setLoadingStatus(int status, boolean showProgress) {
private void setLoadingStatus(int status, boolean showProgress) {
TextView tv = (TextView)findViewById(R.id.loading_message);
if (tv != null) {
@ -656,7 +660,7 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
return true;
}
public void setMenuLabel(int id, int labelId) {
private void setMenuLabel(int id, int labelId) {
MenuItem mi = m_menu.findItem(id);
if (mi != null) {
@ -935,7 +939,7 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
}
}
public void shareArticle(Article article) {
private void shareArticle(Article article) {
if (article != null) {
Intent intent = new Intent(Intent.ACTION_SEND);
@ -976,6 +980,7 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
}
@Override
public void initMainMenu() {
if (m_menu != null) {
if (m_sessionId != null) {
@ -1460,15 +1465,17 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
}
@Override
public Feed getActiveFeed() {
return m_activeFeed;
}
@Override
public FeedCategory getActiveCategory() {
return m_activeCategory;
}
public void logout() {
private void logout() {
if (m_refreshTask != null) {
m_refreshTask.cancel();
m_refreshTask = null;
@ -1495,6 +1502,7 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
initMainMenu();
}
@Override
@SuppressWarnings({ "unchecked", "serial" })
public void login() {

View File

@ -1,6 +1,6 @@
package org.fox.ttrss;
import org.fox.ttrss.ArticleOps.RelativeArticle;
import org.fox.ttrss.OnlineServices.RelativeArticle;
import android.app.AlertDialog;
import android.app.Dialog;
@ -781,7 +781,7 @@ public class OfflineActivity extends FragmentActivity {
getWritableDb().execSQL("UPDATE articles SET selected = 0 ");
}
public int getRelativeArticleId(int baseId, int feedId, ArticleOps.RelativeArticle mode) {
public int getRelativeArticleId(int baseId, int feedId, OnlineServices.RelativeArticle mode) {
Cursor c;

View File

@ -3,7 +3,7 @@ package org.fox.ttrss;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.fox.ttrss.ArticleOps.RelativeArticle;
import org.fox.ttrss.OnlineServices.RelativeArticle;
import android.app.Activity;
import android.content.SharedPreferences;

View File

@ -0,0 +1,28 @@
package org.fox.ttrss;
public interface OnlineServices {
public enum RelativeArticle { BEFORE, AFTER };
public Article getSelectedArticle();
public void saveArticleUnread(final Article article);
public void saveArticleMarked(final Article article);
public void saveArticlePublished(final Article article);
public void updateHeadlines();
public void openArticle(Article article, int compatAnimation);
public Article getRelativeArticle(Article article, RelativeArticle ra);
public void onCatSelected(FeedCategory cat);
public void onFeedSelected(Feed feed);
public void initMainMenu();
public void login();
public Feed getActiveFeed();
public FeedCategory getActiveCategory();
public String getSessionId();
public boolean getUnreadArticlesOnly();
public boolean isSmallScreen();
public boolean getUnreadOnly();
public boolean getLicensed();
public int getApiLevel();
}