PageRenderTime 4039ms CodeModel.GetById 21ms RepoModel.GetById 11ms app.codeStats 0ms

/userVoiceSDK/src/main/java/com/uservoice/uservoicesdk/activity/BaseActivity.java

https://gitlab.com/adamlwalker/android-discourse
Java | 124 lines | 109 code | 15 blank | 0 comment | 13 complexity | b559d8ecce62e5044fc24128fd425a2e MD5 | raw file
  1. package com.uservoice.uservoicesdk.activity;
  2. import android.annotation.SuppressLint;
  3. import android.app.ActionBar;
  4. import android.app.ActionBar.Tab;
  5. import android.app.FragmentTransaction;
  6. import android.os.Build;
  7. import android.os.Bundle;
  8. import android.support.v4.app.FragmentActivity;
  9. import android.view.Menu;
  10. import android.view.MenuItem;
  11. import android.widget.ListView;
  12. import android.widget.SearchView;
  13. import android.widget.ViewFlipper;
  14. import com.uservoice.uservoicesdk.R;
  15. import com.uservoice.uservoicesdk.ui.MixedSearchAdapter;
  16. import com.uservoice.uservoicesdk.ui.PortalAdapter;
  17. import com.uservoice.uservoicesdk.ui.SearchAdapter;
  18. import com.uservoice.uservoicesdk.ui.SearchExpandListener;
  19. import com.uservoice.uservoicesdk.ui.SearchQueryListener;
  20. public class BaseActivity extends FragmentActivity {
  21. protected Tab allTab;
  22. protected Tab articlesTab;
  23. protected Tab ideasTab;
  24. protected MixedSearchAdapter searchAdapter;
  25. private int originalNavigationMode = -1;
  26. @Override
  27. @SuppressLint("NewApi")
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. if (hasActionBar()) {
  31. getActionBar().setDisplayHomeAsUpEnabled(true);
  32. }
  33. }
  34. @Override
  35. public boolean onOptionsItemSelected(MenuItem item) {
  36. if (item.getItemId() == android.R.id.home) {
  37. onBackPressed();
  38. return true;
  39. }
  40. return super.onOptionsItemSelected(item);
  41. }
  42. public SearchAdapter<?> getSearchAdapter() {
  43. return searchAdapter;
  44. }
  45. @SuppressLint("NewApi")
  46. protected void setupScopedSearch(Menu menu) {
  47. if (hasActionBar()) {
  48. menu.findItem(R.id.uv_action_search).setOnActionExpandListener(new SearchExpandListener((SearchActivity) this));
  49. SearchView search = (SearchView) menu.findItem(R.id.uv_action_search).getActionView();
  50. search.setOnQueryTextListener(new SearchQueryListener((SearchActivity) this));
  51. searchAdapter = new MixedSearchAdapter(this);
  52. ListView searchView = new ListView(this);
  53. searchView.setAdapter(searchAdapter);
  54. searchView.setOnItemClickListener(searchAdapter);
  55. ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.uv_view_flipper);
  56. viewFlipper.addView(searchView, 1);
  57. ActionBar.TabListener listener = new ActionBar.TabListener() {
  58. @Override
  59. public void onTabUnselected(Tab tab, FragmentTransaction ft) {
  60. }
  61. @Override
  62. public void onTabSelected(Tab tab, FragmentTransaction ft) {
  63. searchAdapter.setScope((Integer) tab.getTag());
  64. }
  65. @Override
  66. public void onTabReselected(Tab tab, FragmentTransaction ft) {
  67. }
  68. };
  69. allTab = getActionBar().newTab().setText(getString(R.string.uv_all_results_filter)).setTabListener(listener).setTag(PortalAdapter.SCOPE_ALL);
  70. getActionBar().addTab(allTab);
  71. articlesTab = getActionBar().newTab().setText(getString(R.string.uv_articles_filter)).setTabListener(listener).setTag(PortalAdapter.SCOPE_ARTICLES);
  72. getActionBar().addTab(articlesTab);
  73. ideasTab = getActionBar().newTab().setText(getString(R.string.uv_ideas_filter)).setTabListener(listener).setTag(PortalAdapter.SCOPE_IDEAS);
  74. getActionBar().addTab(ideasTab);
  75. } else {
  76. menu.findItem(R.id.uv_action_search).setVisible(false);
  77. }
  78. }
  79. @SuppressLint("NewApi")
  80. public void updateScopedSearch(int results, int articleResults, int ideaResults) {
  81. if (hasActionBar()) {
  82. allTab.setText(String.format("%s (%d)", getString(R.string.uv_all_results_filter), results));
  83. articlesTab.setText(String.format("%s (%d)", getString(R.string.uv_articles_filter), articleResults));
  84. ideasTab.setText(String.format("%s (%d)", getString(R.string.uv_ideas_filter), ideaResults));
  85. }
  86. }
  87. @SuppressLint("NewApi")
  88. public void showSearch() {
  89. ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.uv_view_flipper);
  90. viewFlipper.setDisplayedChild(1);
  91. if (hasActionBar()) {
  92. if (originalNavigationMode == -1)
  93. originalNavigationMode = getActionBar().getNavigationMode();
  94. getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  95. }
  96. }
  97. @SuppressLint("NewApi")
  98. public void hideSearch() {
  99. ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.uv_view_flipper);
  100. viewFlipper.setDisplayedChild(0);
  101. if (hasActionBar()) {
  102. getActionBar().setNavigationMode(originalNavigationMode == -1 ? ActionBar.NAVIGATION_MODE_STANDARD : originalNavigationMode);
  103. }
  104. }
  105. @SuppressLint("NewApi")
  106. public boolean hasActionBar() {
  107. return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && getActionBar() != null;
  108. }
  109. }