Android App für SRCP gesteuerte Modelleisenbahnen
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

367 linhas
13KB

  1. package de.erich.railcontrol;
  2. import android.app.Activity;
  3. import android.app.ActionBar;
  4. import android.app.Fragment;
  5. import android.app.FragmentManager;
  6. import android.app.FragmentTransaction;
  7. import android.os.Bundle;
  8. import android.view.LayoutInflater;
  9. import android.view.Menu;
  10. import android.view.MenuItem;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.support.v4.widget.DrawerLayout;
  14. import android.widget.Button;
  15. import android.widget.CheckBox;
  16. import android.widget.Switch;
  17. import android.widget.Toast;
  18. import java.io.IOException;
  19. public class MyActivity extends Activity
  20. implements NavigationDrawerFragment.NavigationDrawerCallbacks, View.OnClickListener {
  21. public Konfiguration fragm_config;
  22. public FragmentManager fragManager;
  23. public FragmentTransaction fragTransaction;
  24. public static zug E_10 = new zug(10, "E 12", 28);
  25. public zug E_12 = new zug(200, "Hans", 28);
  26. public static Lok1 currentFrag;
  27. public Lok1 [] loks = new Lok1[10];
  28. public static zug [] zuege = new zug[10];
  29. public zug BR_86 = new zug(86, "BR 86" , 28);
  30. static String defaultDataDir;
  31. static boolean power;
  32. /**
  33. * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
  34. */
  35. private NavigationDrawerFragment mNavigationDrawerFragment;
  36. /**
  37. * Used to store the last screen title. For use in {@link #restoreActionBar()}.
  38. */
  39. private CharSequence mTitle;
  40. @Override
  41. protected void onCreate(Bundle savedInstanceState) {
  42. System.out.println("OnCreate");
  43. defaultDataDir = "/data/data/" + getPackageName();
  44. try {
  45. MainCall maincall = new MainCall(defaultDataDir);
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
  49. /*zuege[0] = E_10;
  50. zuege[1] = E_12;
  51. zuege[2] = BR_86; */
  52. int zuegeLength = 0;
  53. for (int i = 0; i < MyActivity.zuege.length; i++) {
  54. if(!(MyActivity.zuege[i] == null)) {
  55. zuegeLength++;
  56. }
  57. }
  58. NavigationDrawerFragment.titles = new String[zuegeLength + 3];
  59. System.out.println("Anzahl Züge: " + zuegeLength);
  60. NavigationDrawerFragment.titles[0] = getString(R.string.title_section1);
  61. NavigationDrawerFragment.titles[1] = getString(R.string.title_section2);
  62. NavigationDrawerFragment.titles[2] = "Neue Lok";
  63. for (int i = 3; i < zuege.length; i++) {
  64. if(!(MyActivity.zuege[i - 3] == null)) {
  65. NavigationDrawerFragment.titles[i] = zuege[i - 3].name;
  66. System.out.println("Created: " + i);
  67. }
  68. else {
  69. System.out.println("Something is null.");
  70. }
  71. }
  72. super.onCreate(savedInstanceState);
  73. setContentView(R.layout.activity_my);
  74. mNavigationDrawerFragment = (NavigationDrawerFragment)
  75. getFragmentManager().findFragmentById(R.id.navigation_drawer);
  76. mTitle = getTitle();
  77. // Set up the drawer.
  78. mNavigationDrawerFragment.setUp(
  79. R.id.navigation_drawer,
  80. (DrawerLayout) findViewById(R.id.drawer_layout));
  81. fragm_config = (Konfiguration) Fragment.instantiate(this, Konfiguration.class.getName(), null);
  82. }
  83. @Override
  84. public void onNavigationDrawerItemSelected(int position) {
  85. // update the main content by replacing fragments
  86. FragmentManager fragmentManager = getFragmentManager();
  87. fragmentManager.beginTransaction()
  88. .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
  89. .commit();
  90. System.out.println(position);
  91. }
  92. public void onSectionAttached(int number) {
  93. System.out.println("onSectionAttached: "+number);
  94. switch (number) {
  95. case 1:
  96. mTitle = getString(R.string.title_section1);
  97. System.out.println("Section Attached");
  98. break;
  99. case 2:
  100. mTitle = getString(R.string.title_section2);
  101. fragManager = getFragmentManager();
  102. fragTransaction = fragManager.beginTransaction();
  103. fragTransaction.replace(R.id.container,fragm_config);
  104. fragTransaction.commit();
  105. break;
  106. case 3:
  107. mTitle = "Lok hinzufügen";
  108. AddLok addlok = new AddLok();
  109. fragManager = getFragmentManager();
  110. fragTransaction = fragManager.beginTransaction();
  111. fragTransaction.replace(R.id.container, addlok);
  112. fragTransaction.commit();
  113. System.out.println("Lok hinzufügen - Fragment");
  114. break;
  115. default:
  116. System.out.println("Section: " + number);
  117. if(loks[number-4] == null) {
  118. Lok1 lok1 = new Lok1();
  119. loks[number-4] = lok1;
  120. lok1.initFragment(zuege[number-4]);
  121. }
  122. Lok1 lok1 = loks[number-4];
  123. fragManager = getFragmentManager();
  124. fragTransaction = fragManager.beginTransaction();
  125. fragTransaction.replace(R.id.container, lok1);
  126. fragTransaction.commit();
  127. mTitle = lok1.fragsLok.name;
  128. //lok1.initFragment(E_10);
  129. currentFrag = lok1;
  130. break;
  131. }
  132. }
  133. public void restoreActionBar() {
  134. ActionBar actionBar = getActionBar();
  135. actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
  136. actionBar.setDisplayShowTitleEnabled(true);
  137. actionBar.setTitle(mTitle);
  138. }
  139. @Override
  140. public boolean onCreateOptionsMenu(Menu menu) {
  141. System.out.println("OnCreateOptionsMenu");
  142. /* try {
  143. MySAXApp.initReader();
  144. } catch (Exception e) {
  145. e.printStackTrace();
  146. } */
  147. if (!mNavigationDrawerFragment.isDrawerOpen()) {
  148. // Only show items in the action bar relevant to this screen
  149. // if the drawer is not showing. Otherwise, let the drawer
  150. // decide what to show in the action bar.
  151. getMenuInflater().inflate(R.menu.my, menu);
  152. restoreActionBar();
  153. return true;
  154. }
  155. return super.onCreateOptionsMenu(menu);
  156. }
  157. @Override
  158. public boolean onOptionsItemSelected(MenuItem item) {
  159. // Handle action bar item clicks here. The action bar will
  160. // automatically handle clicks on the Home/Up button, so long
  161. // as you specify a parent activity in AndroidManifest.xml.
  162. int id = item.getItemId();
  163. if (id == R.id.action_settings) {
  164. if(currentFrag == null) {
  165. System.out.println("Erst Lok auswählen");
  166. Toast.makeText(this, "Erst Lok auswählen", Toast.LENGTH_SHORT).show();
  167. }
  168. else {
  169. mTitle = "Lok ändern";
  170. EditLok editlok = new EditLok();
  171. fragManager = getFragmentManager();
  172. fragTransaction = fragManager.beginTransaction();
  173. fragTransaction.replace(R.id.container, editlok);
  174. fragTransaction.commit();
  175. }
  176. return true;
  177. }
  178. return super.onOptionsItemSelected(item);
  179. }
  180. /*@Override
  181. public void onDestroy() {
  182. System.out.println("OnDestroy");
  183. try {
  184. MainCall.lConf.printConfiguration(defaultDataDir + "/configuration.txt");
  185. } catch (IOException e) {
  186. System.out.println("Error: " + e);
  187. System.exit(0);
  188. }
  189. }*/
  190. public void onCheckboxClicked(View view) {
  191. int status = 0;
  192. boolean checked = ((CheckBox) view).isChecked();
  193. if(checked) {
  194. status = 1;
  195. }
  196. else if(!checked) {
  197. status = 0;
  198. }
  199. if(view == Lok1.funktion_1) {
  200. currentFrag.fragsLok.setFunction(1, status);
  201. }
  202. else if(view == Lok1.funktion_2) {
  203. currentFrag.fragsLok.setFunction(2, status);
  204. }
  205. else if(view == Lok1.funktion_3) {
  206. currentFrag.fragsLok.setFunction(3, status);
  207. }
  208. else if(view == Lok1.funktion_4) {
  209. currentFrag.fragsLok.setFunction(4, status);
  210. }
  211. }
  212. @Override
  213. public void onClick(View view) {
  214. if(view == EditLok.btn_editLok_save) {
  215. int number = 100;
  216. String name = EditLok.et_editLok_name.getText().toString();
  217. int adresse;
  218. int maxspeed;
  219. try {
  220. adresse = Integer.parseInt(EditLok.et_editLok_adresse.getText().toString());
  221. maxspeed = Integer.parseInt(EditLok.et_editLok_maxspeed.getText().toString());
  222. } catch (NumberFormatException e) {
  223. adresse = 0;
  224. maxspeed = 0;
  225. }
  226. for (int i = 0;i < MyActivity.zuege.length; i++) {
  227. if (MyActivity.zuege[i].equals(MyActivity.currentFrag.fragsLok)) {
  228. number = i;
  229. i = MyActivity.zuege.length;
  230. System.out.println("Runned");
  231. }
  232. }
  233. if(!(name.equals("") || adresse == 0 || maxspeed == 0)) {
  234. System.out.println("Setzen der Werte, Number :"+number);
  235. MyActivity.currentFrag.fragsLok.name = name;
  236. MyActivity.currentFrag.fragsLok.adresse = adresse;
  237. MyActivity.currentFrag.fragsLok.maxspeed = maxspeed;
  238. if(!(number == 100)) {
  239. System.out.println("Nicht 100");
  240. MyActivity.zuege[number].name = name;
  241. MyActivity.zuege[number].adresse = adresse;
  242. MyActivity.zuege[number].maxspeed = maxspeed;
  243. System.out.println("Gespeichert");
  244. Toast.makeText(EditLok.btn_editLok_save.getContext(), "Lok gespeichert", Toast.LENGTH_SHORT).show();
  245. }
  246. else {
  247. System.out.println("Error occurred, EditLok");
  248. Toast.makeText(EditLok.btn_editLok_save.getContext(), "Es ist ein Fehler aufgetreten :(", Toast.LENGTH_SHORT).show();
  249. }
  250. }
  251. else {
  252. System.out.println("Fehlende Eingaben");
  253. Toast.makeText(EditLok.btn_editLok_save.getContext(), "Fehlende/Fehlerhafte Eingaben", Toast.LENGTH_SHORT).show();
  254. }
  255. }
  256. else if (view == EditLok.btn_editLok_delete) {
  257. int number = 100;
  258. for (int i = 0;i < MyActivity.zuege.length; i++) {
  259. if (MyActivity.zuege[i].equals(MyActivity.currentFrag.fragsLok)) {
  260. number = i;
  261. i = MyActivity.zuege.length;
  262. }
  263. }
  264. MyActivity.zuege[number] = null;
  265. /*for (int i = number; i < MyActivity.zuege.length; i++) {
  266. if(i == number) {
  267. System.out.println("Lok selber hihihi");
  268. }
  269. else {
  270. MyActivity.zuege[i - 1] = MyActivity.zuege[i];
  271. }
  272. }*/
  273. System.out.println(MyActivity.zuege[number]);
  274. Toast.makeText(EditLok.btn_editLok_save.getContext(), "Lok gelöscht", Toast.LENGTH_SHORT).show();
  275. }
  276. }
  277. /**
  278. * A placeholder fragment containing a simple view.
  279. */
  280. public static class PlaceholderFragment extends Fragment {
  281. public static Button btn_connect;
  282. public static Button btn_quit;
  283. public static Switch ip_selector;
  284. /**
  285. * The fragment argument representing the section number for this
  286. * fragment.
  287. */
  288. private static final String ARG_SECTION_NUMBER = "section_number";
  289. /**
  290. * Returns a new instance of this fragment for the given section
  291. * number.
  292. */
  293. public static PlaceholderFragment newInstance(int sectionNumber) {
  294. PlaceholderFragment fragment = new PlaceholderFragment();
  295. Bundle args = new Bundle();
  296. args.putInt(ARG_SECTION_NUMBER, sectionNumber);
  297. fragment.setArguments(args);
  298. return fragment;
  299. }
  300. public PlaceholderFragment() {
  301. }
  302. @Override
  303. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  304. Bundle savedInstanceState) {
  305. View rootView = inflater.inflate(R.layout.fragment_my, container, false);
  306. this.btn_quit = (Button) rootView.findViewById(R.id.btn_quit);
  307. this.btn_connect = (Button) rootView.findViewById(R.id.btn_connect);
  308. System.out.println("Buttons gesetzt");
  309. System.out.println(this.btn_connect + ", " + this.btn_quit);
  310. btn_quit.setOnClickListener(new btn_listeners());
  311. btn_connect.setOnClickListener(new btn_listeners());
  312. ip_selector = (Switch) rootView.findViewById(R.id.select_ip);
  313. return rootView;
  314. }
  315. @Override
  316. public void onAttach(Activity activity) {
  317. super.onAttach(activity);
  318. ((MyActivity) activity).onSectionAttached(
  319. getArguments().getInt(ARG_SECTION_NUMBER));
  320. }
  321. }
  322. }