ForumOverviewActivity.java (8778B)
1 /** 2 * 3 */ 4 package de.mtbnews.android; 5 6 import java.io.IOException; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import android.app.ExpandableListActivity; 11 import android.content.Intent; 12 import android.content.SharedPreferences; 13 import android.net.Uri; 14 import android.os.Bundle; 15 import android.os.Parcelable; 16 import android.preference.PreferenceManager; 17 import android.text.TextUtils; 18 import android.util.Log; 19 import android.view.Menu; 20 import android.view.MenuInflater; 21 import android.view.MenuItem; 22 import android.view.View; 23 import android.widget.ExpandableListAdapter; 24 import android.widget.ExpandableListView; 25 import android.widget.Toast; 26 import android.widget.ExpandableListView.OnChildClickListener; 27 import de.mtbnews.android.adapter.ExpandableForumContentAdapter; 28 import de.mtbnews.android.tapatalk.TapatalkClient; 29 import de.mtbnews.android.tapatalk.TapatalkException; 30 import de.mtbnews.android.tapatalk.wrapper.Forum; 31 import de.mtbnews.android.util.ServerAsyncTask; 32 import de.mtbnews.android.util.Utils; 33 34 /** 35 * @author dankert 36 * 37 */ 38 public class ForumOverviewActivity extends ExpandableListActivity 39 { 40 private SharedPreferences prefs; 41 private List<Forum> forumList; 42 private TapatalkClient client; 43 44 @Override 45 protected void onCreate(Bundle savedInstanceState) 46 { 47 super.onCreate(savedInstanceState); 48 49 setTheme(((IBCApplication) getApplication()).themeResId); 50 setContentView(R.layout.exp_listing); 51 52 prefs = PreferenceManager.getDefaultSharedPreferences(this); 53 54 loadForum(); 55 } 56 57 private void unterforenFlachkloppen() 58 { 59 for (Forum forum : this.forumList) 60 { 61 final List<Forum> newSubForen = new ArrayList<Forum>(); 62 63 // Wenn Forum Themen enthalten kann, dann mit aufnehmen in die 2. 64 // Hierarchie. 65 if (!forum.subOnly) 66 newSubForen.add(forum); 67 68 newSubForen.addAll(machFlach(forum.subForen)); 69 70 forum.subForen = newSubForen; 71 } 72 } 73 74 private List<Forum> machFlach(List<Forum> subForen) 75 { 76 final List<Forum> newSubForen = new ArrayList<Forum>(); 77 if (subForen == null) 78 return newSubForen; 79 80 for (Forum subForum : subForen) 81 { 82 newSubForen.add(subForum); 83 newSubForen.addAll(machFlach(subForum.subForen)); 84 } 85 return newSubForen; 86 } 87 88 private void loadForum() 89 { 90 client = ((IBCApplication) getApplication()).getTapatalkClient(); 91 92 new ServerAsyncTask(this, R.string.waitingfor_forum) 93 { 94 95 @Override 96 protected void callServer() throws TapatalkException 97 { 98 forumList = ((IBCApplication) getApplication()).getForumList(); 99 100 // Forumliste nur laden, wenn noch nicht vorhanden. 101 if (forumList == null) 102 { 103 // Login. 104 if (prefs.getBoolean("auto_login", false)) 105 if (Utils.loginExceeded(client)) 106 client.login(prefs.getString("username", ""), prefs.getString("password", "")); 107 108 forumList = client.getAllForum(); 109 ((IBCApplication) getApplication()).setForumList(forumList); 110 111 unterforenFlachkloppen(); 112 } 113 } 114 115 protected void doOnSuccess() 116 { 117 ExpandableListAdapter adapter = new ExpandableForumContentAdapter(ForumOverviewActivity.this, forumList); 118 setListAdapter(adapter); 119 } 120 121 }.executeSynchronized(); 122 123 final ExpandableListView list = getExpandableListView(); 124 125 list.setOnChildClickListener(new OnChildClickListener() 126 { 127 128 @Override 129 public boolean onChildClick(ExpandableListView arg0, View arg1, int groupPosition, int childPosition, 130 long rowId) 131 { 132 Forum forum = forumList.get(groupPosition).subForen.get(childPosition); 133 if (forum.subOnly) 134 { 135 Toast.makeText(ForumOverviewActivity.this, R.string.sub_only, Toast.LENGTH_SHORT).show(); 136 } 137 else if (!TextUtils.isEmpty(forum.url)) 138 { 139 if (!Intent.ACTION_CREATE_SHORTCUT.equals(getIntent().getAction())) 140 { 141 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(forum.url))); 142 } 143 } 144 else 145 { 146 if (Intent.ACTION_CREATE_SHORTCUT.equals(getIntent().getAction())) 147 { 148 149 Intent shortcutIntent = new Intent(ForumOverviewActivity.this, ForumActivity.class); 150 // Intent shortcutIntent = new 151 // Intent(Intent.ACTION_MAIN); 152 // shortcutIntent.setClassName(ForumActivity.class.getPackage().getName(), 153 // "." + ForumActivity.class.getSimpleName()); 154 // shortcutIntent.setClassName(ForumActivity.class.getPackage().getName(), 155 // ForumActivity.class.getName()); 156 shortcutIntent.putExtra("forum_id", forum.getId()); 157 158 // Then, set up the container intent (the response to 159 // the caller) 160 161 Intent intent = new Intent(); 162 intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 163 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, forum.getTitle()); 164 Parcelable iconResource = Intent.ShortcutIconResource.fromContext(ForumOverviewActivity.this, 165 R.drawable.ibc_icon); 166 intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); 167 168 // Now, return the result to the launcher 169 170 setResult(RESULT_OK, intent); 171 finish(); 172 173 } 174 else 175 { 176 final Intent intent = new Intent(ForumOverviewActivity.this, ForumActivity.class); 177 intent.putExtra("forum_id", forum.getId()); 178 startActivity(intent); 179 } 180 } 181 return true; 182 } 183 184 }); 185 } 186 187 @Override 188 public boolean onCreateOptionsMenu(Menu menu) 189 { 190 // see #onPrepareOptionsMenu 191 192 return super.onCreateOptionsMenu(menu); 193 } 194 195 @Override 196 public boolean onPrepareOptionsMenu(Menu menu) 197 { 198 super.onPrepareOptionsMenu(menu); 199 200 menu.clear(); 201 MenuInflater mi = new MenuInflater(getApplication()); 202 203 if (client.loggedIn) 204 mi.inflate(R.menu.forumoverview, menu); 205 else 206 mi.inflate(R.menu.forum_guest, menu); 207 208 return true; 209 } 210 211 /** 212 * {@inheritDoc} 213 * 214 * @see android.app.Activity#onSearchRequested() 215 */ 216 @Override 217 public boolean onSearchRequested() 218 { 219 return super.onSearchRequested(); 220 } 221 222 /** 223 * {@inheritDoc} 224 * 225 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem) 226 */ 227 public boolean onOptionsItemSelected(MenuItem item) 228 { 229 switch (item.getItemId()) 230 { 231 case R.id.menu_mailbox: 232 startActivity(new Intent(this, MailboxActivity.class)); 233 return true; 234 235 case R.id.menu_search: 236 onSearchRequested(); 237 return true; 238 239 case R.id.menu_subscribed_forums: 240 startActivity(new Intent(this, SubscriptionForenActivity.class)); 241 return true; 242 243 case R.id.menu_subscribed_topics: 244 startActivity(new Intent(this, SubscriptionTopicsActivity.class)); 245 return true; 246 247 case R.id.menu_participated_topics: 248 Intent intent = new Intent(this, SearchActivity.class); 249 intent.setAction(SearchActivity.ACTION_SEARCH_PARTICIPATED_TOPICS); 250 startActivity(intent); 251 return true; 252 253 case R.id.menu_latest_topics: 254 Intent intent2 = new Intent(this, SearchActivity.class); 255 intent2.setAction(SearchActivity.ACTION_SEARCH_LATEST_TOPICS); 256 startActivity(intent2); 257 return true; 258 259 case R.id.menu_unread_topics: 260 Intent intent3 = new Intent(this, SearchActivity.class); 261 intent3.setAction(SearchActivity.ACTION_SEARCH_UNREAD_TOPICS); 262 startActivity(intent3); 263 return true; 264 265 case R.id.menu_logout: 266 logout(); 267 268 // Forum-Übersicht neu laden. 269 forumList = null; 270 loadForum(); 271 return true; 272 273 case R.id.menu_preferences: 274 startActivity(new Intent(this, Configuration.class)); 275 return true; 276 277 case R.id.menu_login: 278 279 if (TextUtils.isEmpty(prefs.getString("username", ""))) 280 { 281 Toast.makeText(this, R.string.nousername, Toast.LENGTH_LONG).show(); 282 283 Intent intent4 = new Intent(this, Configuration.class); 284 startActivity(intent4); 285 } 286 // Evtl. gibt es jetzt einen Benutzernamen ... 287 288 if (!TextUtils.isEmpty(prefs.getString("username", ""))) 289 { 290 new ServerAsyncTask(this, R.string.waitingfor_login) 291 { 292 293 @Override 294 protected void callServer() throws IOException, TapatalkException 295 { 296 client.login(prefs.getString("username", ""), prefs.getString("password", "")); 297 298 } 299 300 @Override 301 protected void doOnSuccess() 302 { 303 Log.d("IBC", "login success"); 304 Toast.makeText(ForumOverviewActivity.this, R.string.login_success, Toast.LENGTH_SHORT) 305 .show(); 306 forumList = null; 307 loadForum(); 308 } 309 310 }.executeSynchronized(); 311 312 } 313 else 314 { 315 Toast.makeText(this, R.string.nousername, Toast.LENGTH_LONG).show(); 316 } 317 318 return true; 319 } 320 return false; 321 } 322 323 private void logout() 324 { 325 new ServerAsyncTask(this, R.string.waitingfor_logout) 326 { 327 328 @Override 329 protected synchronized void callServer() throws IOException, TapatalkException 330 { 331 client.logout(); 332 } 333 334 @Override 335 protected void doOnSuccess() 336 { 337 Toast.makeText(ForumOverviewActivity.this, R.string.logout, Toast.LENGTH_SHORT).show(); 338 } 339 340 }.executeSynchronized(); 341 } 342 343 }