ForumActivity.java (10152B)
1 /** 2 * 3 */ 4 package de.mtbnews.android; 5 6 import java.io.IOException; 7 8 import android.app.AlertDialog; 9 import android.content.DialogInterface; 10 import android.content.Intent; 11 import android.content.SharedPreferences; 12 import android.os.Bundle; 13 import android.preference.PreferenceManager; 14 import android.text.TextUtils; 15 import android.util.Log; 16 import android.view.ContextMenu; 17 import android.view.Menu; 18 import android.view.MenuInflater; 19 import android.view.MenuItem; 20 import android.view.View; 21 import android.view.ContextMenu.ContextMenuInfo; 22 import android.view.View.OnCreateContextMenuListener; 23 import android.widget.AdapterView; 24 import android.widget.ListAdapter; 25 import android.widget.ListView; 26 import android.widget.Toast; 27 import android.widget.AdapterView.OnItemClickListener; 28 import de.mtbnews.android.adapter.ListEntryContentAdapter; 29 import de.mtbnews.android.tapatalk.TapatalkClient; 30 import de.mtbnews.android.tapatalk.TapatalkException; 31 import de.mtbnews.android.tapatalk.wrapper.Forum; 32 import de.mtbnews.android.tapatalk.wrapper.Topic; 33 import de.mtbnews.android.util.IBC; 34 import de.mtbnews.android.util.IBCException; 35 import de.mtbnews.android.util.ServerAsyncTask; 36 import de.mtbnews.android.util.Utils; 37 38 /** 39 * @author dankert 40 * 41 */ 42 public class ForumActivity extends EndlessListActivity<Topic> 43 { 44 public static final String FORUM_ID = "forum_id"; 45 46 private SharedPreferences prefs; 47 private int totalSize; 48 private String forumId; 49 private int topicMode = TapatalkClient.TOPIC_STANDARD; 50 51 private TapatalkClient client; 52 53 /** 54 * Diese Liste immer von oben beginnen. 55 * 56 * @see de.mtbnews.android.EndlessListActivity#isAutoScrolldown() 57 */ 58 @Override 59 protected boolean isAutoScrolldown() 60 { 61 return false; 62 } 63 64 @Override 65 protected void onCreate(Bundle savedInstanceState) 66 { 67 super.onCreate(savedInstanceState); 68 69 setTheme(((IBCApplication) getApplication()).themeResId); 70 setContentView(R.layout.listing); 71 72 prefs = PreferenceManager.getDefaultSharedPreferences(this); 73 74 client = ((IBCApplication) getApplication()).getTapatalkClient(); 75 76 if (!client.loggedIn && prefs.getBoolean("auto_login", false)) 77 { 78 login(); 79 } 80 81 forumId = getIntent().getStringExtra(FORUM_ID); 82 Log.d(IBC.TAG, "Loading forum #" + forumId); 83 84 ListAdapter adapter = new ListEntryContentAdapter(ForumActivity.this, entries); 85 setListAdapter(adapter); 86 initialLoad(); 87 88 // TODO: ggf. das hier in die Oberklasse? 89 ListView list = getListView(); 90 list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() 91 { 92 93 @Override 94 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 95 { 96 if (topicMode == TapatalkClient.TOPIC_ANNOUNCEMENT) 97 return; 98 99 MenuInflater menuInflater = new MenuInflater(getApplication()); 100 menuInflater.inflate(R.menu.topic_context, menu); 101 102 } 103 }); 104 list.setOnItemClickListener(new OnItemClickListener() 105 { 106 @Override 107 public void onItemClick(AdapterView<?> parent, View view, int position, long id) 108 { 109 if (topicMode == TapatalkClient.TOPIC_ANNOUNCEMENT) 110 return; 111 112 // int aktPosition = displayFrom + position + 1; 113 final Intent intent = new Intent(ForumActivity.this, TopicActivity.class); 114 Topic topic = ForumActivity.super.entries.get(position); 115 intent.putExtra(TopicActivity.TOPIC_ID, topic.getId()); 116 startActivity(intent); 117 } 118 }); 119 } 120 121 // TODO: Das auch in die anderen Listviews einbauen. 122 @Override 123 public boolean onContextItemSelected(MenuItem item) 124 { 125 AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); 126 127 switch (item.getItemId()) 128 { 129 case R.id.menu_goto_top: 130 131 final Intent intent = new Intent(ForumActivity.this, TopicActivity.class); 132 intent.putExtra("topic_id", super.entries.get(menuInfo.position).getId()); 133 intent.putExtra("first_post", true); 134 startActivity(intent); 135 return true; 136 137 case R.id.menu_goto_bottom: 138 139 final Intent intent2 = new Intent(ForumActivity.this, TopicActivity.class); 140 intent2.putExtra("topic_id", super.entries.get(menuInfo.position).getId()); 141 intent2.putExtra("last_post", true); 142 startActivity(intent2); 143 return true; 144 } 145 146 return super.onContextItemSelected(item); 147 } 148 149 private void login() 150 { 151 new ServerAsyncTask(this, R.string.waitingfor_login) 152 { 153 154 @Override 155 protected synchronized void callServer() throws IOException, IBCException 156 { 157 158 try 159 { 160 client.login(prefs.getString("username", ""), prefs.getString("password", "")); 161 162 } 163 catch (TapatalkException e) 164 { 165 throw new IBCException(R.string.login_failed, e.getMessage(), e); 166 } 167 168 } 169 170 }.executeSynchronized(); 171 } 172 173 private void logout() 174 { 175 new ServerAsyncTask(this, R.string.waitingfor_logout) 176 { 177 178 @Override 179 protected synchronized void callServer() throws IOException, IBCException 180 { 181 182 try 183 { 184 client.logout(); 185 186 } 187 catch (TapatalkException e) 188 { 189 190 } 191 192 } 193 194 }.executeSynchronized(); 195 } 196 197 @Override 198 public boolean onCreateOptionsMenu(Menu menu) 199 { 200 super.onCreateOptionsMenu(menu); 201 MenuInflater mi = new MenuInflater(getApplication()); 202 203 if (client.loggedIn) 204 mi.inflate(R.menu.forum, menu); 205 else 206 mi.inflate(R.menu.forum_guest, menu); 207 208 return true; 209 } 210 211 public boolean onOptionsItemSelected(MenuItem item) 212 { 213 switch (item.getItemId()) 214 { 215 case R.id.menu_preferences: 216 startActivity(new Intent(this, Configuration.class)); 217 return true; 218 219 case R.id.menu_mailbox: 220 startActivity(new Intent(this, MailboxActivity.class)); 221 return true; 222 223 case R.id.menu_create_topic: 224 Intent intent5 = new Intent(this, CreateTopicActivity.class); 225 intent5.putExtra(FORUM_ID, forumId); 226 startActivity(intent5); 227 return true; 228 229 case R.id.menu_subscribed_forums: 230 startActivity(new Intent(this, SubscriptionForenActivity.class)); 231 return true; 232 233 case R.id.menu_subscribed_topics: 234 startActivity(new Intent(this, SubscriptionTopicsActivity.class)); 235 return true; 236 237 case R.id.menu_participated_topics: 238 Intent intent = new Intent(this, SearchActivity.class); 239 intent.setAction(SearchActivity.ACTION_SEARCH_PARTICIPATED_TOPICS); 240 startActivity(intent); 241 return true; 242 243 case R.id.menu_latest_topics: 244 Intent intent2 = new Intent(this, SearchActivity.class); 245 intent2.setAction(SearchActivity.ACTION_SEARCH_LATEST_TOPICS); 246 startActivity(intent2); 247 return true; 248 case R.id.menu_unread_topics: 249 Intent intent3 = new Intent(this, SearchActivity.class); 250 intent3.setAction(SearchActivity.ACTION_SEARCH_UNREAD_TOPICS); 251 startActivity(intent3); 252 return true; 253 254 case R.id.menu_logout: 255 logout(); 256 return true; 257 258 case R.id.menu_login: 259 260 if (TextUtils.isEmpty(prefs.getString("username", ""))) 261 { 262 Toast.makeText(this, R.string.nousername, Toast.LENGTH_LONG).show(); 263 264 Intent intent4 = new Intent(this, Configuration.class); 265 startActivity(intent4); 266 } 267 // Evtl. gibt es jetzt einen Benutzernamen ... 268 269 if (!TextUtils.isEmpty(prefs.getString("username", ""))) 270 { 271 login(); 272 } 273 else 274 { 275 Toast.makeText(this, R.string.nousername, Toast.LENGTH_LONG).show(); 276 } 277 278 return true; 279 280 case R.id.menu_subscribe: 281 282 AlertDialog.Builder builder = new AlertDialog.Builder(this); 283 builder.setTitle(R.string.subscribe_forum); 284 builder.setItems(R.array.subscription_modes, new DialogInterface.OnClickListener() 285 { 286 public void onClick(DialogInterface dialog, final int item) 287 { 288 289 new ServerAsyncTask(ForumActivity.this, R.string.subscribe_forum) 290 { 291 292 @Override 293 protected void callServer() throws IOException, TapatalkException 294 { 295 client.subscribeForum(forumId, item - 1); 296 } 297 298 @Override 299 protected void doOnSuccess() 300 { 301 Toast 302 .makeText(getApplicationContext(), R.string.subscription_saved, 303 Toast.LENGTH_SHORT).show(); 304 } 305 }.execute(); 306 } 307 }); 308 builder.create().show(); 309 return true; 310 case R.id.menu_mark_read: 311 312 new ServerAsyncTask(ForumActivity.this, R.string.mark_forum_read) 313 { 314 315 @Override 316 protected void callServer() throws TapatalkException 317 { 318 client.markForumAsRead(forumId); 319 } 320 321 @Override 322 protected void doOnSuccess() 323 { 324 } 325 }.execute(); 326 327 return true; 328 329 case R.id.menu_mark_all_read: 330 331 new ServerAsyncTask(ForumActivity.this, R.string.mark_forum_read) 332 { 333 @Override 334 protected void callServer() throws TapatalkException 335 { 336 client.markForumAsRead(null); 337 } 338 339 @Override 340 protected void doOnSuccess() 341 { 342 } 343 }.execute(); 344 345 return true; 346 347 case R.id.menu_mode: 348 349 // Den Topic-Mode ändern (Standard,Wichtig,Ankündigungen) 350 AlertDialog.Builder builder2 = new AlertDialog.Builder(this); 351 builder2.setTitle(R.string.mode); 352 builder2.setItems(R.array.topic_modes, new DialogInterface.OnClickListener() 353 { 354 355 public void onClick(DialogInterface dialog, final int item) 356 { 357 topicMode = item + 1; 358 initialLoad(); 359 } 360 }); 361 builder2.create().show(); 362 return true; 363 } 364 return false; 365 } 366 367 @Override 368 protected int getTotalSize() 369 { 370 return totalSize; 371 } 372 373 @Override 374 protected void loadEntries(final de.mtbnews.android.EndlessListActivity.OnListLoadedListener<Topic> onListLoaded, 375 final int from, final int to, final boolean firstLoad) 376 { 377 378 new ServerAsyncTask(this, R.string.waitingfor_forum) 379 { 380 private Forum forum; 381 382 @Override 383 protected void callServer() throws TapatalkException 384 { 385 if (Utils.loginExceeded(client)) 386 client.login(prefs.getString("username", ""), prefs.getString("password", "")); 387 388 this.forum = client.getForum(forumId, from, to, topicMode); 389 totalSize = this.forum.topicCount; 390 } 391 392 protected void doOnSuccess() 393 { 394 ForumActivity.this.setTitle(forum.getTitle()); 395 onListLoaded.listLoaded(this.forum.getTopics()); 396 397 if (firstLoad) 398 if (prefs.getBoolean("show_hints", true)) 399 Toast.makeText(ForumActivity.this, R.string.hint_press_long, Toast.LENGTH_SHORT).show(); 400 } 401 402 }.execute(); 403 404 } 405 406 }