Android App für SRCP gesteuerte Modelleisenbahnen
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

311 rindas
11KB

  1. package de.erich.railcontrol;
  2. import android.app.Activity;
  3. import android.app.ActionBar;
  4. import android.app.Fragment;
  5. import android.support.v4.app.ActionBarDrawerToggle;
  6. import android.support.v4.view.GravityCompat;
  7. import android.support.v4.widget.DrawerLayout;
  8. import android.content.SharedPreferences;
  9. import android.content.res.Configuration;
  10. import android.os.Bundle;
  11. import android.preference.PreferenceManager;
  12. import android.view.LayoutInflater;
  13. import android.view.Menu;
  14. import android.view.MenuInflater;
  15. import android.view.MenuItem;
  16. import android.view.View;
  17. import android.view.ViewGroup;
  18. import android.widget.AdapterView;
  19. import android.widget.ArrayAdapter;
  20. import android.widget.ListView;
  21. import android.widget.Toast;
  22. import java.io.IOException;
  23. /**
  24. * Fragment used for managing interactions for and presentation of a navigation drawer.
  25. * See the <a href="https://developer.android.com/design/patterns/navigation-drawer.html#Interaction">
  26. * design guidelines</a> for a complete explanation of the behaviors implemented here.
  27. */
  28. public class NavigationDrawerFragment extends Fragment {
  29. /**
  30. * Remember the position of the selected item.
  31. */
  32. private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";
  33. /**
  34. * Per the design guidelines, you should show the drawer on launch until the user manually
  35. * expands it. This shared preference tracks this.
  36. */
  37. private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";
  38. /**
  39. * A pointer to the current callbacks instance (the Activity).
  40. */
  41. private NavigationDrawerCallbacks mCallbacks;
  42. /**
  43. * Helper component that ties the action bar to the navigation drawer.
  44. */
  45. private ActionBarDrawerToggle mDrawerToggle;
  46. private DrawerLayout mDrawerLayout;
  47. private ListView mDrawerListView;
  48. private View mFragmentContainerView;
  49. private int mCurrentSelectedPosition = 0;
  50. private boolean mFromSavedInstanceState;
  51. private boolean mUserLearnedDrawer;
  52. public static String titles[];
  53. public NavigationDrawerFragment() {
  54. }
  55. @Override
  56. public void onCreate(Bundle savedInstanceState) {
  57. super.onCreate(savedInstanceState);
  58. // Read in the flag indicating whether or not the user has demonstrated awareness of the
  59. // drawer. See PREF_USER_LEARNED_DRAWER for details.
  60. SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
  61. mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);
  62. if (savedInstanceState != null) {
  63. mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
  64. mFromSavedInstanceState = true;
  65. }
  66. // Select either the default item (0) or the last selected item.
  67. selectItem(mCurrentSelectedPosition);
  68. }
  69. @Override
  70. public void onActivityCreated (Bundle savedInstanceState) {
  71. super.onActivityCreated(savedInstanceState);
  72. // Indicate that this fragment would like to influence the set of actions in the action bar.
  73. setHasOptionsMenu(true);
  74. }
  75. @Override
  76. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  77. Bundle savedInstanceState) {
  78. System.out.println("NavDrawerOnCreateView");
  79. mDrawerListView = (ListView) inflater.inflate(
  80. R.layout.fragment_navigation_drawer, container, false);
  81. mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  82. @Override
  83. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  84. selectItem(position);
  85. }
  86. });
  87. mDrawerListView.setAdapter(new ArrayAdapter<String>(
  88. getActionBar().getThemedContext(),
  89. android.R.layout.simple_list_item_activated_1,
  90. android.R.id.text1, titles
  91. /*new String[]{
  92. getString(R.string.title_section1),
  93. getString(R.string.title_section2),
  94. getString(R.string.title_section3),
  95. "Test",
  96. }*/
  97. ));
  98. mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
  99. return mDrawerListView;
  100. }
  101. public boolean isDrawerOpen() {
  102. return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
  103. }
  104. /**
  105. * Users of this fragment must call this method to set up the navigation drawer interactions.
  106. *
  107. * @param fragmentId The android:id of this fragment in its activity's layout.
  108. * @param drawerLayout The DrawerLayout containing this fragment's UI.
  109. */
  110. public void setUp(int fragmentId, DrawerLayout drawerLayout) {
  111. mFragmentContainerView = getActivity().findViewById(fragmentId);
  112. mDrawerLayout = drawerLayout;
  113. // set a custom shadow that overlays the main content when the drawer opens
  114. mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
  115. // set up the drawer's list view with items and click listener
  116. ActionBar actionBar = getActionBar();
  117. actionBar.setDisplayHomeAsUpEnabled(true);
  118. actionBar.setHomeButtonEnabled(true);
  119. // ActionBarDrawerToggle ties together the the proper interactions
  120. // between the navigation drawer and the action bar app icon.
  121. mDrawerToggle = new ActionBarDrawerToggle(
  122. getActivity(), /* host Activity */
  123. mDrawerLayout, /* DrawerLayout object */
  124. R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
  125. R.string.navigation_drawer_open, /* "open drawer" description for accessibility */
  126. R.string.navigation_drawer_close /* "close drawer" description for accessibility */
  127. ) {
  128. @Override
  129. public void onDrawerClosed(View drawerView) {
  130. super.onDrawerClosed(drawerView);
  131. if (!isAdded()) {
  132. return;
  133. }
  134. getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
  135. }
  136. @Override
  137. public void onDrawerOpened(View drawerView) {
  138. super.onDrawerOpened(drawerView);
  139. if (!isAdded()) {
  140. return;
  141. }
  142. if (!mUserLearnedDrawer) {
  143. // The user manually opened the drawer; store this flag to prevent auto-showing
  144. // the navigation drawer automatically in the future.
  145. mUserLearnedDrawer = true;
  146. SharedPreferences sp = PreferenceManager
  147. .getDefaultSharedPreferences(getActivity());
  148. sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply();
  149. }
  150. getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
  151. }
  152. };
  153. // If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer,
  154. // per the navigation drawer design guidelines.
  155. if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
  156. mDrawerLayout.openDrawer(mFragmentContainerView);
  157. }
  158. // Defer code dependent on restoration of previous instance state.
  159. mDrawerLayout.post(new Runnable() {
  160. @Override
  161. public void run() {
  162. mDrawerToggle.syncState();
  163. }
  164. });
  165. mDrawerLayout.setDrawerListener(mDrawerToggle);
  166. }
  167. private void selectItem(int position) {
  168. mCurrentSelectedPosition = position;
  169. if (mDrawerListView != null) {
  170. mDrawerListView.setItemChecked(position, true);
  171. }
  172. if (mDrawerLayout != null) {
  173. mDrawerLayout.closeDrawer(mFragmentContainerView);
  174. }
  175. if (mCallbacks != null) {
  176. mCallbacks.onNavigationDrawerItemSelected(position);
  177. }
  178. }
  179. @Override
  180. public void onAttach(Activity activity) {
  181. super.onAttach(activity);
  182. try {
  183. mCallbacks = (NavigationDrawerCallbacks) activity;
  184. } catch (ClassCastException e) {
  185. throw new ClassCastException("Activity must implement NavigationDrawerCallbacks.");
  186. }
  187. }
  188. @Override
  189. public void onDetach() {
  190. super.onDetach();
  191. mCallbacks = null;
  192. }
  193. @Override
  194. public void onSaveInstanceState(Bundle outState) {
  195. super.onSaveInstanceState(outState);
  196. outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
  197. }
  198. @Override
  199. public void onConfigurationChanged(Configuration newConfig) {
  200. super.onConfigurationChanged(newConfig);
  201. // Forward the new configuration the drawer toggle component.
  202. mDrawerToggle.onConfigurationChanged(newConfig);
  203. }
  204. @Override
  205. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  206. // If the drawer is open, show the global app actions in the action bar. See also
  207. // showGlobalContextActionBar, which controls the top-left area of the action bar.
  208. if (mDrawerLayout != null && isDrawerOpen()) {
  209. inflater.inflate(R.menu.global, menu);
  210. showGlobalContextActionBar();
  211. }
  212. super.onCreateOptionsMenu(menu, inflater);
  213. }
  214. @Override
  215. public boolean onOptionsItemSelected(MenuItem item) {
  216. if (mDrawerToggle.onOptionsItemSelected(item)) {
  217. return true;
  218. }
  219. if (item.getItemId() == R.id.action_example) {
  220. if(!(btn_listeners.client == null || btn_listeners.client.connected == false)) {
  221. if (MyActivity.power == true) {
  222. try {
  223. btn_listeners.client.sendMessage("SET 1 POWER OFF");
  224. MyActivity.power = false;
  225. Toast.makeText(getActivity(), "Strom aus", Toast.LENGTH_SHORT).show();
  226. } catch (IOException e) {
  227. e.printStackTrace();
  228. }
  229. } else {
  230. try {
  231. btn_listeners.client.sendMessage("SET 1 POWER ON");
  232. MyActivity.power = true;
  233. Toast.makeText(getActivity(), "Strom an", Toast.LENGTH_SHORT).show();
  234. } catch (IOException e) {
  235. e.printStackTrace();
  236. }
  237. }
  238. } else {
  239. Toast.makeText(getActivity(), "Nicht Verbunden!", Toast.LENGTH_SHORT).show();
  240. }
  241. return true;
  242. }
  243. return super.onOptionsItemSelected(item);
  244. }
  245. /**
  246. * Per the navigation drawer design guidelines, updates the action bar to show the global app
  247. * 'context', rather than just what's in the current screen.
  248. */
  249. private void showGlobalContextActionBar() {
  250. ActionBar actionBar = getActionBar();
  251. actionBar.setDisplayShowTitleEnabled(true);
  252. actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
  253. actionBar.setTitle(R.string.app_name);
  254. }
  255. private ActionBar getActionBar() {
  256. return getActivity().getActionBar();
  257. }
  258. /**
  259. * Callbacks interface that all activities using this fragment must implement.
  260. */
  261. public static interface NavigationDrawerCallbacks {
  262. /**
  263. * Called when an item in the navigation drawer is selected.
  264. */
  265. void onNavigationDrawerItemSelected(int position);
  266. }
  267. }