|
3 | 3 | import java.util.Date; |
4 | 4 |
|
5 | 5 | import android.app.Activity; |
| 6 | +import android.content.ContentUris; |
6 | 7 | import android.content.ContentValues; |
7 | 8 | import android.database.Cursor; |
8 | | -import android.database.sqlite.SQLiteDatabase; |
| 9 | +import android.net.Uri; |
9 | 10 | import android.os.Bundle; |
10 | | -import android.provider.BaseColumns; |
11 | 11 | import android.view.View; |
12 | 12 | import android.view.View.OnClickListener; |
13 | 13 | import android.widget.Button; |
14 | 14 | import android.widget.EditText; |
15 | | -import de.mvhs.android.zeiterfassung.db.DBHelper; |
16 | 15 | import de.mvhs.android.zeiterfassung.db.ZeitContracts; |
17 | 16 |
|
18 | 17 | public class MainActivity extends Activity { |
19 | 18 |
|
20 | | - private boolean _IsStarted = false; |
21 | | - |
22 | | - @Override |
23 | | - protected void onCreate(Bundle savedInstanceState) { |
24 | | - super.onCreate(savedInstanceState); |
25 | | - |
26 | | - setContentView(R.layout.activity_main); |
27 | | - |
28 | | - if (savedInstanceState != null) { |
29 | | - _IsStarted = savedInstanceState.getBoolean("CurrentState", false); |
30 | | - } |
31 | | - |
32 | | - setButtonState(); |
33 | | - } |
34 | | - |
35 | | - @Override |
36 | | - protected void onStart() { |
37 | | - super.onStart(); |
38 | | - |
39 | | - // Buttons registrieren |
40 | | - Button commandStart = (Button) findViewById(R.id.StartCommand); |
41 | | - Button commandEnd = (Button) findViewById(R.id.EndCommand); |
42 | | - |
43 | | - // Click Event registrieren |
44 | | - commandStart.setOnClickListener(new OnStartButtonClicked()); |
45 | | - |
46 | | - commandEnd.setOnClickListener(new OnEndButtonClicked()); |
47 | | - } |
48 | | - |
49 | | - @Override |
50 | | - protected void onSaveInstanceState(Bundle outState) { |
51 | | - outState.putBoolean("CurrentState", _IsStarted); |
52 | | - super.onSaveInstanceState(outState); |
53 | | - } |
54 | | - |
55 | | - @Override |
56 | | - protected void onStop() { |
57 | | - // Buttons registrieren |
58 | | - Button commandStart = (Button) findViewById(R.id.StartCommand); |
59 | | - Button commandEnd = (Button) findViewById(R.id.EndCommand); |
60 | | - |
61 | | - // Click Event deregistrieren |
62 | | - commandStart.setOnClickListener(null); |
63 | | - commandEnd.setOnClickListener(null); |
64 | | - super.onStop(); |
65 | | - } |
66 | | - |
67 | | - private void setButtonState() { |
68 | | - Button commandStart = (Button) findViewById(R.id.StartCommand); |
69 | | - Button commandEnd = (Button) findViewById(R.id.EndCommand); |
70 | | - |
71 | | - commandStart.setEnabled(_IsStarted == false); |
72 | | - commandEnd.setEnabled(_IsStarted); |
73 | | - } |
74 | | - |
75 | | - private final class OnEndButtonClicked implements OnClickListener { |
76 | | - @Override |
77 | | - public void onClick(View v) { |
78 | | - // Verhalten beim Click auf den Ende-Button |
79 | | - EditText endTime = (EditText) findViewById(R.id.EndTime); |
80 | | - endTime.setText(new Date().toString()); |
81 | | - _IsStarted = false; |
82 | | - setButtonState(); |
83 | | - |
84 | | - DBHelper helper = new DBHelper(MainActivity.this); |
85 | | - SQLiteDatabase db = helper.getReadableDatabase(); |
86 | | - Cursor data = db.query("zeit", // Tabelle |
87 | | - new String[] { BaseColumns._ID }, // Spalten |
88 | | - "IFNULL(end_time,'')=''", // Bedingung |
89 | | - null, // Argumente für die bedingung |
90 | | - null, // Grupierung |
91 | | - null, // Having |
92 | | - null); // Sortierung |
93 | | - if (data != null && data.moveToFirst()) { |
94 | | - // Datensatz gefunden, kann aktualisiert werden |
95 | | - SQLiteDatabase updateDb = helper.getWritableDatabase(); |
96 | | - long id = data.getLong(0); |
97 | | - |
98 | | - ContentValues values = new ContentValues(); |
99 | | - values.put("end_time", new Date().toString()); |
100 | | - |
101 | | - updateDb.update("zeit", values, "_id=?", |
102 | | - new String[] { String.valueOf(id) }); |
103 | | - |
104 | | - updateDb.close(); |
105 | | - } |
106 | | - |
107 | | - // Alles schließen |
108 | | - if (data != null) { |
109 | | - data.close(); |
110 | | - } |
111 | | - db.close(); |
112 | | - helper.close(); |
113 | | - } |
114 | | - } |
115 | | - |
116 | | - private final class OnStartButtonClicked implements OnClickListener { |
117 | | - @Override |
118 | | - public void onClick(View v) { |
119 | | - // Verhalten beim Klick auf den Strat Button |
120 | | - EditText startTime = (EditText) findViewById(R.id.StartTime); |
121 | | - startTime.setText(new Date().toString()); |
122 | | - _IsStarted = true; |
123 | | - setButtonState(); |
124 | | - |
125 | | - // DBHelper helper = new DBHelper(MainActivity.this); |
126 | | - // SQLiteDatabase db = helper.getWritableDatabase(); |
127 | | - // |
128 | | - // ContentValues values = new ContentValues(); |
129 | | - // values.put("start_time", new Date().toString()); |
130 | | - // |
131 | | - // db.insert("zeit", null, values); |
132 | | - // |
133 | | - // db.close(); |
134 | | - |
135 | | - ContentValues values = new ContentValues(); |
136 | | - values.put(ZeitContracts.Zeit.Columns.START, new Date().toString()); |
137 | | - |
138 | | - getContentResolver().insert(ZeitContracts.Zeit.CONTENT_URI, values); |
139 | | - } |
140 | | - |
141 | | - } |
| 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; |
| 25 | + |
| 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 + ",'')=''"; |
| 28 | + |
| 29 | + @Override |
| 30 | + protected void onCreate(Bundle savedInstanceState) { |
| 31 | + super.onCreate(savedInstanceState); |
| 32 | + |
| 33 | + setContentView(R.layout.activity_main); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + protected void onStart() { |
| 38 | + super.onStart(); |
| 39 | + |
| 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); |
| 45 | + |
| 46 | + // Click Event registrieren |
| 47 | + _StartCommand.setOnClickListener(new OnStartButtonClicked()); |
| 48 | + _StopCommand.setOnClickListener(new OnEndButtonClicked()); |
| 49 | + |
| 50 | + // Bearbeitung in den Textfeldern verbieten |
| 51 | + _StartTime.setKeyListener(null); |
| 52 | + _EndTime.setKeyListener(null); |
| 53 | + |
| 54 | + // Prüfen, ob ein angefangener Eintrag in der Datenbank vorliegt |
| 55 | + checkTrackState(); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + protected void onStop() { |
| 60 | + // Click Event deregistrieren |
| 61 | + _StartCommand.setOnClickListener(null); |
| 62 | + _StopCommand.setOnClickListener(null); |
| 63 | + |
| 64 | + super.onStop(); |
| 65 | + } |
| 66 | + |
| 67 | + private void setButtonState() { |
| 68 | + |
| 69 | + _StartCommand.setEnabled(_IsStarted == false); |
| 70 | + _StopCommand.setEnabled(_IsStarted); |
| 71 | + } |
| 72 | + |
| 73 | + private void checkTrackState() { |
| 74 | + Cursor data = getContentResolver().query(ZeitContracts.Zeit.CONTENT_URI, _SEARCH_PROJECTION, _SEARCH_SELECTION, null, null); |
| 75 | + |
| 76 | + if (data != null && data.moveToFirst()) { |
| 77 | + // Ein Eintrag gefunden |
| 78 | + _CurrentId = data.getLong(0); |
| 79 | + |
| 80 | + String startDate = data.getString(1); |
| 81 | + |
| 82 | + _StartTime.setText(startDate); |
| 83 | + _EndTime.setText(""); |
| 84 | + |
| 85 | + _IsStarted = true; |
| 86 | + } else { |
| 87 | + // Keine Einträge gefunden |
| 88 | + _StartTime.setText(""); |
| 89 | + _EndTime.setText(""); |
| 90 | + |
| 91 | + _IsStarted = false; |
| 92 | + } |
| 93 | + |
| 94 | + setButtonState(); |
| 95 | + } |
| 96 | + |
| 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()); |
| 103 | + |
| 104 | + ContentValues values = new ContentValues(); |
| 105 | + values.put(ZeitContracts.Zeit.Columns.END, currentTime.toString()); |
| 106 | + |
| 107 | + Uri updateUri = ContentUris.withAppendedId(ZeitContracts.Zeit.CONTENT_URI, _CurrentId); |
| 108 | + |
| 109 | + getContentResolver().update(updateUri, values, null, null); |
| 110 | + |
| 111 | + _IsStarted = false; |
| 112 | + setButtonState(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 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()); |
| 122 | + |
| 123 | + ContentValues values = new ContentValues(); |
| 124 | + values.put(ZeitContracts.Zeit.Columns.START, currentTime.toString()); |
| 125 | + |
| 126 | + getContentResolver().insert(ZeitContracts.Zeit.CONTENT_URI, values); |
| 127 | + |
| 128 | + _IsStarted = true; |
| 129 | + setButtonState(); |
| 130 | + } |
| 131 | + |
| 132 | + } |
142 | 133 | } |
0 commit comments