various activity lifecycle-related fixes

This commit is contained in:
Andrew Dolgov 2011-09-09 13:23:56 +04:00
parent 38028b1e7e
commit 7393ab7631
7 changed files with 212 additions and 97 deletions

View File

@ -10,4 +10,9 @@
<ProgressBar android:layout_height="wrap_content"
android:layout_width="wrap_content" android:visibility="invisible"
android:layout_gravity="center" android:id="@+id/loading_progress"/>
<TextView android:id="@+id/no_unread_feeds"
android:visibility="invisible"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/no_unread_feeds"></TextView>
</FrameLayout>

View File

@ -5,9 +5,9 @@
android:title="@string/preferences"
android:showAsAction="ifRoom|withText"/>
<!-- <item android:id="@+id/logout"
android:title="@string/logout"
<item android:id="@+id/sync_status"
android:title="@string/offline"
android:icon="@android:drawable/presence_offline"
android:showAsAction="ifRoom|withText"/> -->
android:showAsAction="ifRoom|withText"/>
</menu>

View File

@ -24,4 +24,7 @@
<string name="login_wrong_password">Login failed: username or password incorrect.</string>
<string name="login_success">Logged in.</string>
<string name="no_unread_feeds">No unread feeds.</string>
<string name="offline">Offline</string>
<string name="online">Online</string>
<string name="synchronizing">Synchronizing...</string>
</resources>

View File

@ -47,10 +47,11 @@ public class DatabaseHelper extends SQLiteOpenHelper {
"content TEXT" +
");");
db.execSQL("CREATE VIEW feeds_unread AS SELECT feeds._id AS _id, " +
db.execSQL("CREATE VIEW feeds_unread AS SELECT feeds."+BaseColumns._ID+" AS "+BaseColumns._ID+", " +
"feeds.title AS title, " +
"SUM(articles.unread) AS unread FROM feeds " +
"LEFT JOIN articles ON (articles.feed_id = feeds._id) GROUP BY feeds._id, feeds.title;");
"LEFT JOIN articles ON (articles.feed_id = feeds."+BaseColumns._ID+") " +
"GROUP BY feeds."+BaseColumns._ID+", feeds.title;");
}
@Override

View File

@ -35,13 +35,12 @@ import com.google.gson.reflect.TypeToken;
public class FeedsFragment extends Fragment implements OnItemClickListener {
private final String TAG = this.getClass().getSimpleName();
// protected ArrayList<Feed> m_feeds = new ArrayList<Feed>();
protected FeedsListAdapter m_adapter;
protected SharedPreferences m_prefs;
protected int m_activeFeedId;
protected long m_lastUpdate;
protected Gson m_gson = new Gson();
protected Cursor m_cursor;
protected SQLiteDatabase m_db;
private Timer m_timer;
private TimerTask m_updateTask = new TimerTask() {
@ -62,13 +61,14 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
if (savedInstanceState != null) {
m_activeFeedId = savedInstanceState.getInt("activeFeedId");
m_lastUpdate = savedInstanceState.getLong("lastUpdate");
}
View view = inflater.inflate(R.layout.feeds_fragment, container, false);
DatabaseHelper helper = new DatabaseHelper(getActivity());
m_cursor = helper.getReadableDatabase().query("feeds_unread", null, null, null, null, null, "unread DESC");
m_db = helper.getReadableDatabase();
m_cursor = m_db.query("feeds_unread", null, "unread > 0", null, null, null, "unread DESC");
m_adapter = new FeedsListAdapter(getActivity(), R.layout.feeds_row, m_cursor,
new String[] { "title", "unread" }, new int[] { R.id.title, R.id.unread_counter }, 0);
@ -78,11 +78,15 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
if (list != null) {
list.setAdapter(m_adapter);
list.setOnItemClickListener(this);
list.setEmptyView(view.findViewById(R.id.no_unread_feeds));
list.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
}
//updateSelf();
View pb = view.findViewById(R.id.loading_progress);
if (pb != null && m_cursor.getCount() == 0)
pb.setVisibility(View.VISIBLE);
m_timer = new Timer("UpdateFeeds");
m_timer.schedule(m_updateTask, 1000L, 60*1000L);
@ -93,6 +97,9 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
public void onDestroy() {
super.onDestroy();
m_cursor.close();
m_db.close();
m_timer.cancel();
m_timer = null;
}
@ -114,7 +121,11 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
protected void onPostExecute(JsonElement result) {
if (result != null && getAuthStatus() == STATUS_OK) {
try {
((MainActivity)getActivity()).setSessionId(getSessionId());
try {
((MainActivity)getActivity()).setSessionId(getSessionId());
} catch (NullPointerException e) {
//
}
JsonArray feeds_object = (JsonArray) result.getAsJsonArray();
@ -124,11 +135,6 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
DatabaseHelper dh = new DatabaseHelper(getActivity());
SQLiteDatabase db = dh.getWritableDatabase();
/* db.execSQL("DELETE FROM FEEDS"); */
/* SQLiteStatement stmFind = db.compileStatement("SELECT "+BaseColumns._ID+" FROM feeds WHERE "+
BaseColumns._ID+" = ?"); */
SQLiteStatement stmtUpdate = db.compileStatement("UPDATE feeds SET " +
"title = ?, feed_url = ?, has_icon = ?, cat_id = ?, last_updated = ? WHERE " +
BaseColumns._ID + " = ?");
@ -167,9 +173,12 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
db.close();
m_cursor.requery();
m_adapter.notifyDataSetChanged();
View pb = getView().findViewById(R.id.loading_progress);
if (pb != null) pb.setVisibility(View.INVISIBLE);
updateListView();
} catch (Exception e) {
e.printStackTrace();
}
@ -183,7 +192,7 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
put("sid", ((MainActivity)getActivity()).getSessionId());
put("op", "getFeeds");
put("cat_id", "-3");
put("unread_only", "true");
put("unread_only", "0");
}
});
@ -194,7 +203,6 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
super.onSaveInstanceState(out);
out.putInt("activeFeedId", m_activeFeedId);
out.putLong("lastUpdate", m_lastUpdate);
}
class FeedsListAdapter extends SimpleCursorAdapter {
@ -243,6 +251,11 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
m_adapter.notifyDataSetChanged();
}
public synchronized void updateListView() {
m_cursor.requery();
m_adapter.notifyDataSetChanged();
}
}

View File

@ -19,15 +19,6 @@ public class HeadlinesFragment extends Fragment {
View view = inflater.inflate(R.layout.headlines_fragment, container, false);
/* m_adapter = new FeedsListAdapter(getActivity(), R.id.feeds_row, m_feeds);
ListView list = (ListView) view.findViewById(R.id.feeds);
if (list != null) {
list.setAdapter(m_adapter);
list.setOnItemClickListener(this);
} */
return view;
}

View File

@ -28,12 +28,24 @@ import com.google.gson.reflect.TypeToken;
public class MainActivity extends Activity {
private final String TAG = this.getClass().getSimpleName();
private final static int UPDATE_INITIAL = 1;
private final static int UPDATE_SEQUENTIAL = 2;
private final static int UPDATE_OFFLINE = 3;
private final static int INITIAL_OFFSET_MAX = 100;
private SharedPreferences m_prefs;
private String m_themeName = "";
private boolean m_feedsOpened = false;
protected String m_sessionId;
protected int m_offset = 0;
protected int m_limit = 100;
protected int m_limit = 25;
protected int m_maxId = 0;
protected int m_updateMode = UPDATE_INITIAL;
protected enum SyncStatus { SYNC_INITIAL, SYNC_ONLINE, SYNC_OFFLINE };
protected MenuItem m_syncStatus;
protected String getSessionId() {
return m_sessionId;
@ -48,7 +60,9 @@ public class MainActivity extends Activity {
}
private Timer m_timer;
private TimerTask m_updateTask = new TimerTask() {
private UpdateTask m_updateTask;
private class UpdateTask extends TimerTask {
@Override
public void run() {
@ -67,13 +81,6 @@ public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// allow database to upgrade before we do anything else
DatabaseHelper dh = new DatabaseHelper(getApplicationContext());
SQLiteDatabase db = dh.getWritableDatabase();
db.execSQL("DELETE FROM feeds;");
db.execSQL("DELETE FROM articles;");
db.close();
m_prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if (m_prefs.getString("theme", "THEME_DARK").equals("THEME_DARK")) {
@ -89,8 +96,24 @@ public class MainActivity extends Activity {
if (savedInstanceState != null) {
m_feedsOpened = savedInstanceState.getBoolean("feedsOpened");
m_sessionId = savedInstanceState.getString("sessionId");
m_offset = savedInstanceState.getInt("offset");
m_limit = savedInstanceState.getInt("limit");
m_updateMode = savedInstanceState.getInt("updateMode");
m_maxId = savedInstanceState.getInt("maxId");
}
// allow database to upgrade before we do anything else
DatabaseHelper dh = new DatabaseHelper(getApplicationContext());
SQLiteDatabase db = dh.getWritableDatabase();
if (m_updateMode == UPDATE_INITIAL) {
db.execSQL("DELETE FROM feeds;");
db.execSQL("DELETE FROM articles;");
}
db.close();
setContentView(R.layout.main);
if (!m_feedsOpened) {
@ -106,8 +129,7 @@ public class MainActivity extends Activity {
m_feedsOpened = true;
}
m_timer = new Timer("UpdateArticles");
m_timer.schedule(m_updateTask, 1000L, 5*1000L);
scheduleNextUpdate();
}
@Override
@ -116,6 +138,10 @@ public class MainActivity extends Activity {
out.putBoolean("feedsOpened", m_feedsOpened);
out.putString("sessionId", m_sessionId);
out.putInt("offset", m_offset);
out.putInt("limit", m_limit);
out.putInt("updateMode", m_updateMode);
out.putInt("maxId", m_maxId);
}
@Override
@ -141,9 +167,40 @@ public class MainActivity extends Activity {
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
m_syncStatus = menu.findItem(R.id.sync_status);
switch (m_updateMode) {
case UPDATE_INITIAL:
setSyncStatus(SyncStatus.SYNC_INITIAL);
break;
case UPDATE_SEQUENTIAL:
setSyncStatus(SyncStatus.SYNC_ONLINE);
break;
default:
setSyncStatus(SyncStatus.SYNC_OFFLINE);
}
return true;
}
public void setSyncStatus(SyncStatus status) {
switch (status) {
case SYNC_INITIAL:
m_syncStatus.setTitle(R.string.synchronizing);
m_syncStatus.setIcon(android.R.drawable.presence_online);
break;
case SYNC_ONLINE:
m_syncStatus.setTitle(R.string.online);
m_syncStatus.setIcon(android.R.drawable.presence_online);
break;
case SYNC_OFFLINE:
m_syncStatus.setTitle(R.string.offline);
m_syncStatus.setIcon(android.R.drawable.presence_offline);
break;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
@ -166,76 +223,101 @@ public class MainActivity extends Activity {
if (result != null && getAuthStatus() == STATUS_OK) {
try {
setSessionId(getSessionId());
JsonArray feeds_object = (JsonArray) result.getAsJsonArray();
Type listType = new TypeToken<List<Article>>() {}.getType();
List<Article> articles = m_gson.fromJson(feeds_object, listType);
DatabaseHelper dh = new DatabaseHelper(getApplicationContext());
SQLiteDatabase db = dh.getWritableDatabase();
/* db.execSQL("DELETE FROM articles"); */
SQLiteStatement stmtInsert = db.compileStatement("INSERT INTO articles " +
"("+BaseColumns._ID+", unread, marked, published, updated, is_updated, title, link, feed_id, tags, content) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
SQLiteStatement stmtUpdate = db.compileStatement("UPDATE articles SET " +
"unread = ?, marked = ?, published = ?, updated = ?, is_updated = ?, title = ?, link = ?, feed_id = ?, " +
"tags = ?, content = ? WHERE " + BaseColumns._ID + " = ?");
int articlesFound = 0;
for (Article article : articles) {
//Log.d(TAG, "Processing article #" + article.id);
try {
JsonArray feeds_object = (JsonArray) result.getAsJsonArray();
++articlesFound;
Type listType = new TypeToken<List<Article>>() {}.getType();
List<Article> articles = m_gson.fromJson(feeds_object, listType);
DatabaseHelper dh = new DatabaseHelper(getApplicationContext());
SQLiteDatabase db = dh.getWritableDatabase();
/* db.execSQL("DELETE FROM articles"); */
Cursor c = db.query("articles", new String[] { BaseColumns._ID } , BaseColumns._ID + "=?",
new String[] { String.valueOf(article.id) }, null, null, null);
if (c.getCount() != 0) {
//Log.d(TAG, "article found");
SQLiteStatement stmtInsert = db.compileStatement("INSERT INTO articles " +
"("+BaseColumns._ID+", unread, marked, published, updated, is_updated, title, link, feed_id, tags, content) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
SQLiteStatement stmtUpdate = db.compileStatement("UPDATE articles SET " +
"unread = ?, marked = ?, published = ?, updated = ?, is_updated = ?, title = ?, link = ?, feed_id = ?, " +
"tags = ?, content = ? WHERE " + BaseColumns._ID + " = ?");
for (Article article : articles) {
//Log.d(TAG, "Processing article #" + article.id);
} else {
//Log.d(TAG, "article not found");
stmtInsert.bindLong(1, article.id);
stmtInsert.bindLong(2, article.unread ? 1 : 0);
stmtInsert.bindLong(3, article.marked ? 1 : 0);
stmtInsert.bindLong(4, article.published ? 1 : 0);
stmtInsert.bindLong(5, article.updated);
stmtInsert.bindLong(6, article.is_updated ? 1 : 0);
stmtInsert.bindString(7, article.title);
stmtInsert.bindString(8, article.link);
stmtInsert.bindLong(9, article.feed_id);
stmtInsert.bindString(10, ""); // comma-separated tags
stmtInsert.bindString(11, article.content);
stmtInsert.execute();
m_maxId = Math.max(m_maxId, article.id);
++articlesFound;
Cursor c = db.query("articles", new String[] { BaseColumns._ID } , BaseColumns._ID + "=?",
new String[] { String.valueOf(article.id) }, null, null, null);
if (c.getCount() != 0) {
stmtUpdate.bindLong(1, article.unread ? 1 : 0);
stmtUpdate.bindLong(2, article.marked ? 1 : 0);
stmtUpdate.bindLong(3, article.published ? 1 : 0);
stmtUpdate.bindLong(4, article.updated);
stmtUpdate.bindLong(5, article.is_updated ? 1 : 0);
stmtUpdate.bindString(6, article.title);
stmtUpdate.bindString(7, article.link);
stmtUpdate.bindLong(8, article.feed_id);
stmtUpdate.bindString(9, ""); // comma-separated tags
stmtUpdate.bindString(10, article.content);
stmtUpdate.bindLong(11, article.id);
stmtUpdate.execute();
} else {
//Log.d(TAG, "article not found");
stmtInsert.bindLong(1, article.id);
stmtInsert.bindLong(2, article.unread ? 1 : 0);
stmtInsert.bindLong(3, article.marked ? 1 : 0);
stmtInsert.bindLong(4, article.published ? 1 : 0);
stmtInsert.bindLong(5, article.updated);
stmtInsert.bindLong(6, article.is_updated ? 1 : 0);
stmtInsert.bindString(7, article.title);
stmtInsert.bindString(8, article.link);
stmtInsert.bindLong(9, article.feed_id);
stmtInsert.bindString(10, ""); // comma-separated tags
stmtInsert.bindString(11, article.content);
stmtInsert.execute();
}
c.close();
}
c.close();
}
db.close();
FeedsFragment ff = (FeedsFragment) getFragmentManager().findFragmentByTag("FEEDLIST");
if (ff != null) {
ff.m_cursor.requery();
ff.m_adapter.notifyDataSetChanged();
db.close();
FeedsFragment ff = (FeedsFragment) getFragmentManager().findFragmentByTag("FEEDLIST");
if (ff != null) ff.updateListView();
} catch (Exception e) {
e.printStackTrace();
}
Log.d(TAG, articlesFound + " articles processed");
if (articlesFound == m_limit && m_offset <= 300) {
if (m_updateMode == UPDATE_INITIAL && articlesFound == m_limit && m_offset < INITIAL_OFFSET_MAX) {
m_offset += m_limit;
} else {
m_offset = 0;
m_timer.cancel();
if (m_updateMode == UPDATE_INITIAL) {
Log.i(TAG, "Switching to sequential mode...");
setSyncStatus(SyncStatus.SYNC_ONLINE);
m_updateMode = UPDATE_SEQUENTIAL;
}
}
scheduleNextUpdate();
} catch (Exception e) {
e.printStackTrace();
@ -245,8 +327,6 @@ public class MainActivity extends Activity {
}
};
Log.d(TAG, "Requesting articles [offset=" + m_offset + "]");
task.execute(new HashMap<String,String>() {
{
put("sid", m_sessionId);
@ -256,8 +336,30 @@ public class MainActivity extends Activity {
put("limit", String.valueOf(m_limit));
put("skip", String.valueOf(m_offset));
put("view_mode", "unread");
if (m_updateMode != UPDATE_INITIAL) {
put("since_id", String.valueOf(m_maxId));
}
}
});
}
protected void scheduleNextUpdate() {
if (m_updateTask != null) m_updateTask.cancel();
m_updateTask = new UpdateTask();
if (m_updateMode == UPDATE_INITIAL) {
Log.i(TAG, "Scheduling initial update...");
m_timer = new Timer("DownloadInitial");
m_timer.schedule(m_updateTask, 1000L);
} else {
Log.i(TAG, "Scheduling sequential update...");
m_timer = new Timer("DownloadSequential");
m_timer.schedule(m_updateTask, 60*1000L);
}
}
}