android-ibc-forum

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 42bea308efacd18464796e50141db5a8e9430a1f
parent 90e10b84362864695fdb01f104833a01a465717b
Author: Jan Dankert <devnull@localhost>
Date:   Sat, 11 Feb 2012 00:01:22 +0100

Speicherverbrauch reduzieren: Keine harten Referenzen in IBCApplication, sondern stattdessen SoftReferences benutzen.

Diffstat:
src/de/mtbnews/android/ForumActivity.java | 11+++++++----
src/de/mtbnews/android/ForumOverviewActivity.java | 57++++++++++++++++++++++++---------------------------------
src/de/mtbnews/android/IBCActivity.java | 68+++++++++++++++++++++++---------------------------------------------
src/de/mtbnews/android/IBCApplication.java | 68+++++++++++++++++++++++++++++++++++++++++++++++++++++---------------
src/de/mtbnews/android/NewsDetailActivity.java | 99++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
src/de/mtbnews/android/SearchActivity.java | 2+-
src/de/mtbnews/android/SubscriptionForenActivity.java | 4++--
src/de/mtbnews/android/SubscriptionTopicsActivity.java | 2+-
src/de/mtbnews/android/TopicActivity.java | 2+-
9 files changed, 185 insertions(+), 128 deletions(-)

diff --git a/src/de/mtbnews/android/ForumActivity.java b/src/de/mtbnews/android/ForumActivity.java @@ -12,6 +12,7 @@ import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.text.TextUtils; +import android.util.Log; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuInflater; @@ -29,6 +30,7 @@ import de.mtbnews.android.tapatalk.TapatalkClient; import de.mtbnews.android.tapatalk.TapatalkException; import de.mtbnews.android.tapatalk.wrapper.Forum; import de.mtbnews.android.tapatalk.wrapper.Topic; +import de.mtbnews.android.util.IBC; import de.mtbnews.android.util.IBCException; import de.mtbnews.android.util.ServerAsyncTask; @@ -63,6 +65,7 @@ public class ForumActivity extends EndlessListActivity<Topic> } forumId = getIntent().getStringExtra(FORUM_ID); + Log.d(IBC.TAG,"Loading forum #"+forumId); ListAdapter adapter = new ListEntryContentAdapter(ForumActivity.this, entries); @@ -140,7 +143,7 @@ public class ForumActivity extends EndlessListActivity<Topic> private void login() { - final TapatalkClient client = ((IBCApplication) getApplication()).client; + final TapatalkClient client = ((IBCApplication) getApplication()).getTapatalkClient(); new ServerAsyncTask(this, R.string.waitingfor_login) { @@ -168,7 +171,7 @@ public class ForumActivity extends EndlessListActivity<Topic> private void logout() { - final TapatalkClient client = ((IBCApplication) getApplication()).client; + final TapatalkClient client = ((IBCApplication) getApplication()).getTapatalkClient(); new ServerAsyncTask(this, R.string.waitingfor_logout) { @@ -199,7 +202,7 @@ public class ForumActivity extends EndlessListActivity<Topic> super.onCreateOptionsMenu(menu); MenuInflater mi = new MenuInflater(getApplication()); - if (((IBCApplication) getApplication()).client.loggedIn) + if (((IBCApplication) getApplication()).getTapatalkClient().loggedIn) mi.inflate(R.menu.forum, menu); else mi.inflate(R.menu.forum_guest, menu); @@ -393,7 +396,7 @@ public class ForumActivity extends EndlessListActivity<Topic> protected void callServer() throws TapatalkException { - TapatalkClient client = ((IBCApplication) getApplication()).client; + TapatalkClient client = ((IBCApplication) getApplication()).getTapatalkClient(); this.forum = client.getForum(forumId, from, to, topicMode); totalSize = this.forum.topicCount; } diff --git a/src/de/mtbnews/android/ForumOverviewActivity.java b/src/de/mtbnews/android/ForumOverviewActivity.java @@ -94,15 +94,19 @@ public class ForumOverviewActivity extends ExpandableListActivity @Override protected void callServer() throws TapatalkException { - // Login. - if (prefs.getBoolean("auto_login", false)) - if (Utils.loginExceeded(client)) - client.login(prefs.getString("username", ""), prefs - .getString("password", "")); + // Forumliste nur laden, wenn noch nicht vorhanden. + if (forumList == null) + { + // Login. + if (prefs.getBoolean("auto_login", false)) + if (Utils.loginExceeded(client)) + client.login(prefs.getString("username", ""), prefs + .getString("password", "")); - forumList = client.getAllForum(); - unterforenFlachkloppen(); + forumList = client.getAllForum(); + unterforenFlachkloppen(); + } } protected void doOnSuccess() @@ -116,29 +120,6 @@ public class ForumOverviewActivity extends ExpandableListActivity final ExpandableListView list = getExpandableListView(); - // list.setOnGroupClickListener(new OnGroupClickListener() - // { - // @Override - // public boolean onGroupClick(ExpandableListView parent, View v, - // int groupPosition, long id) - // { - // Forum forum = forumList.get(groupPosition); - // - // if (forum.subOnly) - // { - // Toast.makeText(ForumOverviewActivity.this, - // R.string.sub_only, Toast.LENGTH_SHORT).show(); - // } - // else - // { - // final Intent intent = new Intent( - // ForumOverviewActivity.this, ForumActivity.class); - // intent.putExtra("forum_id", forum.getId()); - // startActivity(intent); - // } - // return true; - // } - // }); list.setOnChildClickListener(new OnChildClickListener() { @@ -175,7 +156,7 @@ public class ForumOverviewActivity extends ExpandableListActivity public boolean onCreateOptionsMenu(Menu menu) { // see #onPrepareOptionsMenu - + return super.onCreateOptionsMenu(menu); } @@ -209,6 +190,10 @@ public class ForumOverviewActivity extends ExpandableListActivity return super.onSearchRequested(); } + /** + * {@inheritDoc} + * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem) + */ public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) @@ -250,6 +235,10 @@ public class ForumOverviewActivity extends ExpandableListActivity case R.id.menu_logout: logout(); + + // Forum-Übersicht neu laden. + forumList = null; + loadForum(); return true; case R.id.menu_login: @@ -267,7 +256,7 @@ public class ForumOverviewActivity extends ExpandableListActivity if (!TextUtils.isEmpty(prefs.getString("username", ""))) { - final TapatalkClient client = ((IBCApplication) getApplication()).client; + final TapatalkClient client = ((IBCApplication) getApplication()).getTapatalkClient(); new ServerAsyncTask(this, R.string.waitingfor_login) { @@ -287,6 +276,8 @@ public class ForumOverviewActivity extends ExpandableListActivity Toast.makeText(ForumOverviewActivity.this, R.string.login_success, Toast.LENGTH_SHORT) .show(); + forumList = null; + loadForum(); } }.executeSynchronized(); @@ -306,7 +297,7 @@ public class ForumOverviewActivity extends ExpandableListActivity private void logout() { - final TapatalkClient client = ((IBCApplication) getApplication()).client; + final TapatalkClient client = ((IBCApplication) getApplication()).getTapatalkClient(); new ServerAsyncTask(this, R.string.waitingfor_logout) { diff --git a/src/de/mtbnews/android/IBCActivity.java b/src/de/mtbnews/android/IBCActivity.java @@ -58,16 +58,6 @@ public class IBCActivity extends ListActivity super.onCreate(savedInstanceState); setContentView(R.layout.start); - // Gast-Zugang ist ok, daher keine Meldung anzeigen... - // if (globalPrefs.getString("username", "").equals("") ) - // { - // // Noch kein Benutzer konfiguriert. Hinweis anzeigen! - // final AlertDialog.Builder builder = new AlertDialog.Builder(this); - // builder.setMessage(getResources().getString(R.string.noserver)); - // AlertDialog alert = builder.create(); - // alert.show(); - // } - Button forumButton = (Button) findViewById(R.id.forum); forumButton.setOnClickListener(new OnClickListener() { @@ -80,17 +70,6 @@ public class IBCActivity extends ListActivity } }); - // Button photoButton = (Button) findViewById(R.id.photo); - // photoButton.setOnClickListener(new OnClickListener() - // { - // - // @Override - // public void onClick(View v) - // { - // startActivity(new Intent(IBCActivity.this, PhotoActivity.class)); - // } - // }); - reloadFeed(); } @@ -115,29 +94,28 @@ public class IBCActivity extends ListActivity } }); - if (((IBCApplication) getApplication()).newsFeed != null) + new ServerAsyncTask(this, R.string.waitingfor_news) { - RSSFeed feed = ((IBCApplication) getApplication()).newsFeed; - IBCActivity.this.setTitle(feed.getTitle()); + private RSSFeed feed; - ListAdapter adapter = new ListEntryContentAdapter(IBCActivity.this, - feed.getItems()); - setListAdapter(adapter); - } - else - { - new ServerAsyncTask(this, R.string.waitingfor_news) + @Override + protected void callServer() throws IOException { - private RSSFeed feed; - - @Override - protected void callServer() throws IOException + final RSSFeed oldFeed = ((IBCApplication) getApplication()) + .getNewsFeed(); + + if (oldFeed != null) + { + feed = oldFeed; + } + else { + RSSReader reader = new RSSReader(); try { feed = reader.load(IBC.IBC_NEWS_RSS_URL); - ((IBCApplication) getApplication()).newsFeed = feed; + ((IBCApplication) getApplication()).setNewsFeed(feed); } catch (RSSReaderException e) { @@ -148,17 +126,17 @@ public class IBCActivity extends ListActivity throw new ClientProtocolException(e); } } + } - protected void doOnSuccess() - { - IBCActivity.this.setTitle(feed.getTitle()); + protected void doOnSuccess() + { + IBCActivity.this.setTitle(feed.getTitle()); - ListAdapter adapter = new ListEntryContentAdapter( - IBCActivity.this, this.feed.getItems()); - setListAdapter(adapter); - } - }.execute(); - } + ListAdapter adapter = new ListEntryContentAdapter( + IBCActivity.this, this.feed.getItems()); + setListAdapter(adapter); + } + }.execute(); } @Override diff --git a/src/de/mtbnews/android/IBCApplication.java b/src/de/mtbnews/android/IBCApplication.java @@ -3,6 +3,8 @@ */ package de.mtbnews.android; +import java.lang.ref.SoftReference; + import org.mcsoxford.rss.RSSFeed; import android.app.Application; @@ -24,21 +26,24 @@ import de.mtbnews.android.util.IBC; */ public class IBCApplication extends Application { - public RSSFeed newsFeed; - - public RSSFeed photoFeed; + /** + * Speichert eine Referenz auf den Tapatalk-Client. Dieser Client kann vom + * GC jederzeit entfernt werden, wenn der Speicherverbrauch zu hoch ist. + */ + private SoftReference<TapatalkClient> tapatalkClientRef = new SoftReference<TapatalkClient>( + null); - public TapatalkClient client; + /** + * Speichert eine Referenz auf den RSSFeed. Dieser Feed kann vom GC + * jederzeit entfernt werden, wenn der Speicherverbrauch zu hoch ist. + */ + private SoftReference<RSSFeed> newsFeedRef = new SoftReference<RSSFeed>( + null); public SharedPreferences prefs; public int themeResId; - public TapatalkClient getTapatalkClient() - { - return client; - } - /** * {@inheritDoc} * @@ -47,15 +52,12 @@ public class IBCApplication extends Application @Override public void onCreate() { - Log.d(IBC.TAG,"starting main application"); + Log.d(IBC.TAG, "starting main application"); prefs = PreferenceManager.getDefaultSharedPreferences(this); themeResId = (prefs.getBoolean("ibc_theme", true)) ? R.style.IBC : android.R.style.Theme; - client = new TapatalkClient(IBC.IBC_FORUM_CONNECTOR_URL); - client.getXMLRPCClient().setUserAgent("Mozilla/5.0 (compatible; Android)"); - if (prefs.getBoolean("autostart_subscription_service", false)) { startService(new Intent(getApplicationContext(), @@ -76,7 +78,7 @@ public class IBCApplication extends Application @Override public void onLowMemory() { - Log.d(IBC.TAG,"Low memory detected..."); + Log.d(IBC.TAG, "Low memory detected..."); super.onLowMemory(); } @@ -88,7 +90,43 @@ public class IBCApplication extends Application @Override public void onTerminate() { - Log.d(IBC.TAG,"terminating application"); + Log.d(IBC.TAG, "terminating application"); super.onTerminate(); } + + /** + * Liefert den Tapatalk-Client. Der Client wird zwar gecacht, kann jedoch, + * falls der bisherige durch den GC weggeräumt wurde, auch ein neuer Client + * ohne bestehendes Login sein! + * + * @return + */ + public TapatalkClient getTapatalkClient() + { + TapatalkClient client = tapatalkClientRef.get(); + if (client != null) + { + return client; + } + else + { + Log.d(IBC.TAG, "Creating a new tapatalk client"); + + client = new TapatalkClient(IBC.IBC_FORUM_CONNECTOR_URL); + client.getXMLRPCClient().setUserAgent( + "Mozilla/5.0 (compatible; Android)"); + tapatalkClientRef = new SoftReference<TapatalkClient>(client); + return client; + } + } + + public RSSFeed getNewsFeed() + { + return newsFeedRef.get(); + } + + public void setNewsFeed(RSSFeed feed) + { + newsFeedRef = new SoftReference<RSSFeed>(feed); + } } diff --git a/src/de/mtbnews/android/NewsDetailActivity.java b/src/de/mtbnews/android/NewsDetailActivity.java @@ -1,6 +1,13 @@ package de.mtbnews.android; +import java.io.IOException; + +import org.apache.http.client.ClientProtocolException; +import org.mcsoxford.rss.RSSFault; +import org.mcsoxford.rss.RSSFeed; import org.mcsoxford.rss.RSSItem; +import org.mcsoxford.rss.RSSReader; +import org.mcsoxford.rss.RSSReaderException; import android.app.Activity; import android.content.Intent; @@ -14,6 +21,8 @@ import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import de.mtbnews.android.image.URLImageParser; +import de.mtbnews.android.util.IBC; +import de.mtbnews.android.util.ServerAsyncTask; public class NewsDetailActivity extends Activity { @@ -27,43 +36,81 @@ public class NewsDetailActivity extends Activity super.onCreate(savedInstanceState); - final RSSItem item = ((IBCApplication)getApplication()).newsFeed.getItems().get( - getIntent().getIntExtra("itemid", 0)); + final TextView datum = (TextView) findViewById(R.id.item_date); + final TextView desc = (TextView) findViewById(R.id.item_description); + final Button button = (Button) findViewById(R.id.item_button); - TextView datum = (TextView) findViewById(R.id.item_date); - datum.setText(DateFormat.getTimeFormat(this).format(item.getPubDate())); + new ServerAsyncTask(this, R.string.waitingfor_news) + { + private RSSFeed feed; - // TextView name = (TextView) findViewById(R.id.item_title); - // name.setText(item.getTitle()); + @Override + protected void callServer() throws IOException + { + final RSSFeed oldFeed = ((IBCApplication) getApplication()) + .getNewsFeed(); + + if (oldFeed != null) + { + feed = oldFeed; + } + else + { + + RSSReader reader = new RSSReader(); + try + { + feed = reader.load(IBC.IBC_NEWS_RSS_URL); + ((IBCApplication) getApplication()).setNewsFeed(feed); + } + catch (RSSReaderException e) + { + throw new ClientProtocolException(e); + } + catch (RSSFault e) + { + throw new ClientProtocolException(e); + } + } + } - final TextView desc = (TextView) findViewById(R.id.item_description); + protected void doOnSuccess() + { + final RSSItem item = feed.getItems().get( + getIntent().getIntExtra("itemid", 0)); - // if (e.getContent() != null) - final String html = item.getFullContent(); + datum.setText(DateFormat.getTimeFormat(NewsDetailActivity.this) + .format(item.getPubDate())); - Html.ImageGetter imageGetter = null; - if (prefs.getBoolean("load_images", false)) - imageGetter = new URLImageParser(desc,this); + // TextView name = (TextView) findViewById(R.id.item_title); + // name.setText(item.getTitle()); - desc.setText(Html.fromHtml(html, imageGetter, null)); + // if (e.getContent() != null) + final String html = item.getFullContent(); - setTitle(item.getTitle()); + Html.ImageGetter imageGetter = null; + if (prefs.getBoolean("load_images", false)) + imageGetter = new URLImageParser(desc, + NewsDetailActivity.this); - Button button = (Button) findViewById(R.id.item_button); - button.setOnClickListener(new OnClickListener() - { + desc.setText(Html.fromHtml(html, imageGetter, null)); - @Override - public void onClick(View v) + setTitle(item.getTitle()); - { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(item.getLink()); - startActivity(i); - } - }); + button.setOnClickListener(new OnClickListener() + { - } + @Override + public void onClick(View v) + { + Intent i = new Intent(Intent.ACTION_VIEW); + i.setData(item.getLink()); + startActivity(i); + } + }); + } + }.execute(); + } } diff --git a/src/de/mtbnews/android/SearchActivity.java b/src/de/mtbnews/android/SearchActivity.java @@ -174,7 +174,7 @@ public class SearchActivity extends EndlessListActivity<Topic> @Override protected void callServer() throws TapatalkException { - TapatalkClient client = ((IBCApplication) getApplication()).client; + TapatalkClient client = ((IBCApplication) getApplication()).getTapatalkClient(); search = client.searchTopics(searchType, query, username, from, to, searchId); diff --git a/src/de/mtbnews/android/SubscriptionForenActivity.java b/src/de/mtbnews/android/SubscriptionForenActivity.java @@ -95,7 +95,7 @@ public class SubscriptionForenActivity extends ListActivity @Override protected void callServer() throws IOException, TapatalkException { - TapatalkClient client = ((IBCApplication) getApplication()).client; + TapatalkClient client = ((IBCApplication) getApplication()).getTapatalkClient(); // Login. if (Utils.loginExceeded(client)) @@ -156,7 +156,7 @@ public class SubscriptionForenActivity extends ListActivity super.onCreateOptionsMenu(menu); MenuInflater mi = new MenuInflater(getApplication()); - if (((IBCApplication) getApplication()).client.loggedIn) + if (((IBCApplication) getApplication()).getTapatalkClient().loggedIn) mi.inflate(R.menu.forum, menu); else mi.inflate(R.menu.forum_guest, menu); diff --git a/src/de/mtbnews/android/SubscriptionTopicsActivity.java b/src/de/mtbnews/android/SubscriptionTopicsActivity.java @@ -136,7 +136,7 @@ public class SubscriptionTopicsActivity extends EndlessListActivity<Topic> @Override protected void callServer() throws IOException, TapatalkException { - TapatalkClient client = ((IBCApplication) getApplication()).client; + TapatalkClient client = ((IBCApplication) getApplication()).getTapatalkClient(); // Login. if (Utils.loginExceeded(client)) diff --git a/src/de/mtbnews/android/TopicActivity.java b/src/de/mtbnews/android/TopicActivity.java @@ -99,7 +99,7 @@ public class TopicActivity extends EndlessListActivity<Post> @Override protected void callServer() throws TapatalkException { - TapatalkClient client = ((IBCApplication) getApplication()).client; + TapatalkClient client = ((IBCApplication) getApplication()).getTapatalkClient(); topic = client.getTopic(topicId, from, to);