|
1 | 1 | package de.mvhs.android.zeiterfassung; |
2 | 2 |
|
| 3 | +import java.text.DateFormat; |
| 4 | +import java.text.ParseException; |
3 | 5 | import java.util.Date; |
4 | 6 |
|
5 | 7 | import android.app.Activity; |
6 | 8 | import android.content.ContentUris; |
7 | 9 | import android.content.ContentValues; |
| 10 | +import android.content.Intent; |
8 | 11 | import android.database.Cursor; |
9 | 12 | import android.net.Uri; |
10 | 13 | import android.os.Bundle; |
| 14 | +import android.view.Menu; |
| 15 | +import android.view.MenuItem; |
11 | 16 | import android.view.View; |
12 | 17 | import android.view.View.OnClickListener; |
13 | 18 | import android.widget.Button; |
|
16 | 21 |
|
17 | 22 | public class MainActivity extends Activity { |
18 | 23 |
|
19 | | - private boolean _IsStarted = false; |
20 | | - private Button _StartCommand = null; |
21 | | - private Button _StopCommand = null; |
22 | | - private EditText _StartTime = null; |
23 | | - private EditText _EndTime = null; |
24 | | - private long _CurrentId = -1; |
| 24 | + private boolean _IsStarted = false; |
| 25 | + private Button _StartCommand = null; |
| 26 | + private Button _StopCommand = null; |
| 27 | + private EditText _StartTime = null; |
| 28 | + private EditText _EndTime = null; |
| 29 | + private long _CurrentId = -1; |
25 | 30 |
|
26 | | - private final static String[] _SEARCH_PROJECTION = { ZeitContracts.Zeit.Columns._ID, ZeitContracts.Zeit.Columns.START }; |
27 | | - private final static String _SEARCH_SELECTION = "IFNULL(" + ZeitContracts.Zeit.Columns.START + ",'')=''"; |
| 31 | + private final static String[] _SEARCH_PROJECTION = { |
| 32 | + ZeitContracts.Zeit.Columns._ID, ZeitContracts.Zeit.Columns.START }; |
| 33 | + private final static String _SEARCH_SELECTION = "IFNULL(" |
| 34 | + + ZeitContracts.Zeit.Columns.END + ",'')=''"; |
28 | 35 |
|
29 | | - @Override |
30 | | - protected void onCreate(Bundle savedInstanceState) { |
31 | | - super.onCreate(savedInstanceState); |
| 36 | + private final static DateFormat _UI_DATE_FORMATTER = DateFormat |
| 37 | + .getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); |
32 | 38 |
|
33 | | - setContentView(R.layout.activity_main); |
34 | | - } |
| 39 | + @Override |
| 40 | + protected void onCreate(Bundle savedInstanceState) { |
| 41 | + super.onCreate(savedInstanceState); |
35 | 42 |
|
36 | | - @Override |
37 | | - protected void onStart() { |
38 | | - super.onStart(); |
| 43 | + setContentView(R.layout.activity_main); |
| 44 | + } |
39 | 45 |
|
40 | | - // UI Elemente initialisieren |
41 | | - _StartCommand = (Button) findViewById(R.id.StartCommand); |
42 | | - _StopCommand = (Button) findViewById(R.id.EndCommand); |
43 | | - _StartTime = (EditText) findViewById(R.id.StartTime); |
44 | | - _EndTime = (EditText) findViewById(R.id.EndTime); |
| 46 | + @Override |
| 47 | + protected void onStart() { |
| 48 | + super.onStart(); |
45 | 49 |
|
46 | | - // Click Event registrieren |
47 | | - _StartCommand.setOnClickListener(new OnStartButtonClicked()); |
48 | | - _StopCommand.setOnClickListener(new OnEndButtonClicked()); |
| 50 | + // UI Elemente initialisieren |
| 51 | + _StartCommand = (Button) findViewById(R.id.StartCommand); |
| 52 | + _StopCommand = (Button) findViewById(R.id.EndCommand); |
| 53 | + _StartTime = (EditText) findViewById(R.id.StartTime); |
| 54 | + _EndTime = (EditText) findViewById(R.id.EndTime); |
49 | 55 |
|
50 | | - // Bearbeitung in den Textfeldern verbieten |
51 | | - _StartTime.setKeyListener(null); |
52 | | - _EndTime.setKeyListener(null); |
| 56 | + // Click Event registrieren |
| 57 | + _StartCommand.setOnClickListener(new OnStartButtonClicked()); |
| 58 | + _StopCommand.setOnClickListener(new OnEndButtonClicked()); |
53 | 59 |
|
54 | | - // Prüfen, ob ein angefangener Eintrag in der Datenbank vorliegt |
55 | | - checkTrackState(); |
56 | | - } |
| 60 | + // Bearbeitung in den Textfeldern verbieten |
| 61 | + _StartTime.setKeyListener(null); |
| 62 | + _EndTime.setKeyListener(null); |
57 | 63 |
|
58 | | - @Override |
59 | | - protected void onStop() { |
60 | | - // Click Event deregistrieren |
61 | | - _StartCommand.setOnClickListener(null); |
62 | | - _StopCommand.setOnClickListener(null); |
| 64 | + // Prüfen, ob ein angefangener Eintrag in der Datenbank vorliegt |
| 65 | + checkTrackState(); |
| 66 | + } |
63 | 67 |
|
64 | | - super.onStop(); |
65 | | - } |
| 68 | + @Override |
| 69 | + protected void onStop() { |
| 70 | + // Click Event deregistrieren |
| 71 | + _StartCommand.setOnClickListener(null); |
| 72 | + _StopCommand.setOnClickListener(null); |
66 | 73 |
|
67 | | - private void setButtonState() { |
| 74 | + super.onStop(); |
| 75 | + } |
68 | 76 |
|
69 | | - _StartCommand.setEnabled(_IsStarted == false); |
70 | | - _StopCommand.setEnabled(_IsStarted); |
71 | | - } |
| 77 | + @Override |
| 78 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 79 | + getMenuInflater().inflate(R.menu.main_menu, menu); |
72 | 80 |
|
73 | | - private void checkTrackState() { |
74 | | - Cursor data = getContentResolver().query(ZeitContracts.Zeit.CONTENT_URI, _SEARCH_PROJECTION, _SEARCH_SELECTION, null, null); |
| 81 | + return super.onCreateOptionsMenu(menu); |
| 82 | + } |
75 | 83 |
|
76 | | - if (data != null && data.moveToFirst()) { |
77 | | - // Ein Eintrag gefunden |
78 | | - _CurrentId = data.getLong(0); |
| 84 | + @Override |
| 85 | + public boolean onOptionsItemSelected(MenuItem item) { |
79 | 86 |
|
80 | | - String startDate = data.getString(1); |
| 87 | + // Menüpunkt herausfinden |
| 88 | + switch (item.getItemId()) { |
| 89 | + case R.id.mnu_list: |
| 90 | + // Aktion für unser List-Menü-Eintrag |
| 91 | + Intent listIntent = new Intent(this, AuflistungActivity.class); |
| 92 | + startActivity(listIntent); |
| 93 | + break; |
81 | 94 |
|
82 | | - _StartTime.setText(startDate); |
83 | | - _EndTime.setText(""); |
| 95 | + default: |
| 96 | + break; |
| 97 | + } |
84 | 98 |
|
85 | | - _IsStarted = true; |
86 | | - } else { |
87 | | - // Keine Einträge gefunden |
88 | | - _StartTime.setText(""); |
89 | | - _EndTime.setText(""); |
| 99 | + return super.onOptionsItemSelected(item); |
| 100 | + } |
90 | 101 |
|
91 | | - _IsStarted = false; |
92 | | - } |
| 102 | + private void setButtonState() { |
93 | 103 |
|
94 | | - setButtonState(); |
95 | | - } |
| 104 | + _StartCommand.setEnabled(_IsStarted == false); |
| 105 | + _StopCommand.setEnabled(_IsStarted); |
| 106 | + } |
96 | 107 |
|
97 | | - private final class OnEndButtonClicked implements OnClickListener { |
98 | | - @Override |
99 | | - public void onClick(View v) { |
100 | | - // Verhalten beim Click auf den Ende-Button |
101 | | - Date currentTime = new Date(); |
102 | | - _EndTime.setText(currentTime.toString()); |
| 108 | + private void checkTrackState() { |
| 109 | + Cursor data = getContentResolver().query( |
| 110 | + ZeitContracts.Zeit.CONTENT_URI, _SEARCH_PROJECTION, |
| 111 | + _SEARCH_SELECTION, null, null); |
103 | 112 |
|
104 | | - ContentValues values = new ContentValues(); |
105 | | - values.put(ZeitContracts.Zeit.Columns.END, currentTime.toString()); |
| 113 | + if (data != null && data.moveToFirst()) { |
| 114 | + // Ein Eintrag gefunden |
| 115 | + _CurrentId = data.getLong(0); |
106 | 116 |
|
107 | | - Uri updateUri = ContentUris.withAppendedId(ZeitContracts.Zeit.CONTENT_URI, _CurrentId); |
| 117 | + String startDate = data.getString(1); |
108 | 118 |
|
109 | | - getContentResolver().update(updateUri, values, null, null); |
| 119 | + try { |
| 120 | + // Konvertierung des Datums aus der Datenbank |
| 121 | + Date startTime = ZeitContracts.Converters.DB_FORMATTER |
| 122 | + .parse(startDate); |
110 | 123 |
|
111 | | - _IsStarted = false; |
112 | | - setButtonState(); |
113 | | - } |
114 | | - } |
| 124 | + // Ausgabe an der UI |
| 125 | + _StartTime.setText(_UI_DATE_FORMATTER.format(startTime)); |
115 | 126 |
|
116 | | - private final class OnStartButtonClicked implements OnClickListener { |
117 | | - @Override |
118 | | - public void onClick(View v) { |
119 | | - // Verhalten beim Klick auf den Strat Button |
120 | | - Date currentTime = new Date(); |
121 | | - _StartTime.setText(currentTime.toString()); |
| 127 | + } catch (ParseException e) { |
| 128 | + e.printStackTrace(); |
| 129 | + } |
122 | 130 |
|
123 | | - ContentValues values = new ContentValues(); |
124 | | - values.put(ZeitContracts.Zeit.Columns.START, currentTime.toString()); |
| 131 | + _EndTime.setText(""); |
125 | 132 |
|
126 | | - getContentResolver().insert(ZeitContracts.Zeit.CONTENT_URI, values); |
| 133 | + _IsStarted = true; |
| 134 | + } else { |
| 135 | + // Keine Einträge gefunden |
| 136 | + _StartTime.setText(""); |
| 137 | + _EndTime.setText(""); |
127 | 138 |
|
128 | | - _IsStarted = true; |
129 | | - setButtonState(); |
130 | | - } |
| 139 | + _IsStarted = false; |
| 140 | + } |
131 | 141 |
|
132 | | - } |
| 142 | + setButtonState(); |
| 143 | + } |
| 144 | + |
| 145 | + private final class OnEndButtonClicked implements OnClickListener { |
| 146 | + @Override |
| 147 | + public void onClick(View v) { |
| 148 | + // Verhalten beim Click auf den Ende-Button |
| 149 | + Date currentTime = new Date(); |
| 150 | + _EndTime.setText(_UI_DATE_FORMATTER.format(currentTime)); |
| 151 | + |
| 152 | + ContentValues values = new ContentValues(); |
| 153 | + values.put(ZeitContracts.Zeit.Columns.END, |
| 154 | + ZeitContracts.Converters.DB_FORMATTER.format(currentTime)); |
| 155 | + |
| 156 | + Uri updateUri = ContentUris.withAppendedId( |
| 157 | + ZeitContracts.Zeit.CONTENT_URI, _CurrentId); |
| 158 | + |
| 159 | + getContentResolver().update(updateUri, values, null, null); |
| 160 | + |
| 161 | + _CurrentId = -1; |
| 162 | + |
| 163 | + _IsStarted = false; |
| 164 | + setButtonState(); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + private final class OnStartButtonClicked implements OnClickListener { |
| 169 | + @Override |
| 170 | + public void onClick(View v) { |
| 171 | + // Verhalten beim Klick auf den Strat Button |
| 172 | + Date currentTime = new Date(); |
| 173 | + _StartTime.setText(_UI_DATE_FORMATTER.format(currentTime)); |
| 174 | + |
| 175 | + ContentValues values = new ContentValues(); |
| 176 | + values.put(ZeitContracts.Zeit.Columns.START, |
| 177 | + ZeitContracts.Converters.DB_FORMATTER.format(currentTime)); |
| 178 | + |
| 179 | + Uri insertUri = getContentResolver().insert( |
| 180 | + ZeitContracts.Zeit.CONTENT_URI, values); |
| 181 | + |
| 182 | + _CurrentId = ContentUris.parseId(insertUri); |
| 183 | + |
| 184 | + _IsStarted = true; |
| 185 | + setButtonState(); |
| 186 | + } |
| 187 | + |
| 188 | + } |
133 | 189 | } |
0 commit comments