implement API level checking

This commit is contained in:
Andrew Dolgov 2011-11-29 08:25:13 +03:00
parent 9e96e4434f
commit cebcc4928e
5 changed files with 71 additions and 21 deletions

View File

@ -47,7 +47,7 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
private boolean m_transportDebugging = false;
protected int m_httpStatusCode = 0;
protected int m_apiStatusCode = 0;
private Context m_context;
protected Context m_context;
private SharedPreferences m_prefs;
protected ApiError m_lastError;

View File

@ -35,7 +35,7 @@ public class FeedCategoriesFragment extends Fragment implements OnItemClickListe
private SharedPreferences m_prefs;
private FeedCategoryListAdapter m_adapter;
private FeedCategoryList m_cats = new FeedCategoryList();
private int m_selectedCatId;
private int m_selectedCatId = -100;
private OnCatSelectedListener m_catSelectedListener;
public interface OnCatSelectedListener {
@ -172,6 +172,15 @@ public class FeedCategoriesFragment extends Fragment implements OnItemClickListe
m_cats.clear();
int apiLevel = ((MainActivity)getActivity()).getApiLevel();
// virtual cats implemented in getCategories since api level 1
if (apiLevel == 0) {
m_cats.add(new FeedCategory(-1, "Special", 0));
m_cats.add(new FeedCategory(-2, "Labels", 0));
m_cats.add(new FeedCategory(0, "Uncategorized", 0));
}
for (FeedCategory c : cats)
m_cats.add(c);

View File

@ -12,6 +12,12 @@ public class FeedCategory implements Parcelable {
readFromParcel(in);
}
public FeedCategory(int id, String title, int unread) {
this.id = id;
this.title = title;
this.unread = unread;
}
@Override
public int describeContents() {
return 0;

View File

@ -160,14 +160,14 @@ public class FeedsFragment extends Fragment implements OnItemClickListener, OnSh
@SuppressWarnings({ "unchecked", "serial" })
public void refresh(boolean background) {
FeedsRequest req = new FeedsRequest(getActivity().getApplicationContext());
FeedCategory cat = ((MainActivity)getActivity()).getActiveCategory();
final int catId = (cat != null) ? cat.id : -4;
final String sessionId = ((MainActivity)getActivity()).getSessionId();
final boolean unreadOnly = ((MainActivity)getActivity()).getUnreadOnly();
FeedCategory cat = ((MainActivity)getActivity()).getActiveCategory();
final int catId = (cat != null) ? cat.id : -4;
FeedsRequest req = new FeedsRequest(getActivity().getApplicationContext(), catId);
if (sessionId != null) {
@ -257,9 +257,11 @@ public class FeedsFragment extends Fragment implements OnItemClickListener, OnSh
}
private class FeedsRequest extends ApiRequest {
private int m_catId;
public FeedsRequest(Context context) {
public FeedsRequest(Context context, int catId) {
super(context);
m_catId = catId;
}
protected void onPostExecute(JsonElement result) {
@ -274,7 +276,7 @@ public class FeedsFragment extends Fragment implements OnItemClickListener, OnSh
m_feeds.clear();
for (Feed f : feeds)
if (f.id > -10) // skip labels for now
if (f.id > -10 || m_catId != -4) // skip labels for flat feedlist for now
m_feeds.add(f);
sortFeeds();

View File

@ -46,6 +46,7 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
private boolean m_canLoadMore = true;
private boolean m_compatMode = false;
private boolean m_enableCats = false;
private int m_apiLevel = 0;
public void updateHeadlines() {
HeadlinesFragment frag = (HeadlinesFragment)getSupportFragmentManager().findFragmentById(R.id.headlines_fragment);
@ -54,6 +55,10 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
}
}
public int getApiLevel() {
return m_apiLevel;
}
@SuppressWarnings({ "unchecked", "serial" })
public void saveArticleUnread(final Article article) {
ApiRequest req = new ApiRequest(getApplicationContext());
@ -203,6 +208,7 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
m_unreadArticlesOnly = savedInstanceState.getBoolean("unreadArticlesOnly");
m_canLoadMore = savedInstanceState.getBoolean("canLoadMore");
m_activeCategory = savedInstanceState.getParcelable("activeCategory");
m_apiLevel = savedInstanceState.getInt("apiLevel");
}
m_enableCats = m_prefs.getBoolean("enable_cats", false);
@ -302,6 +308,7 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
out.putBoolean("unreadArticlesOnly", m_unreadArticlesOnly);
out.putBoolean("canLoadMore", m_canLoadMore);
out.putParcelable("activeCategory", m_activeCategory);
out.putInt("apiLevel", m_apiLevel);
}
@Override
@ -428,7 +435,10 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
startActivityForResult(intent, 0);
return true;
case R.id.update_feeds:
refreshFeeds();
if (!m_enableCats || m_activeCategory != null )
refreshFeeds();
else
refreshCategories();
return true;
case R.id.logout:
logout();
@ -644,19 +654,42 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
Log.d(TAG, "Authenticated!");
ApiRequest req = new ApiRequest(m_context) {
protected void onPostExecute(JsonElement result) {
if (result != null) {
m_apiLevel = result.getAsJsonObject().get("level").getAsInt();
} else {
m_apiLevel = 0;
}
Log.d(TAG, "Received API level: " + m_apiLevel);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (m_enableCats) {
FeedCategoriesFragment frag = new FeedCategoriesFragment();
ft.replace(R.id.cats_fragment, frag);
} else {
FeedsFragment frag = new FeedsFragment();
ft.replace(R.id.feeds_fragment, frag);
}
ft.commit();
}
};
HashMap<String,String> map = new HashMap<String,String>() {
{
put("sid", m_sessionId);
put("op", "getApiLevel");
}
};
req.execute(map);
setLoadingStatus(R.string.loading_message, true);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (m_enableCats) {
FeedCategoriesFragment frag = new FeedCategoriesFragment();
ft.replace(R.id.cats_fragment, frag);
} else {
FeedsFragment frag = new FeedsFragment();
ft.replace(R.id.feeds_fragment, frag);
}
ft.commit();
loginSuccess();
return;