|
1 | 1 | package org.sofwerx.torgi.ui; |
2 | 2 |
|
| 3 | +import android.content.SharedPreferences; |
3 | 4 | import android.os.Bundle; |
| 5 | +import android.preference.PreferenceManager; |
| 6 | +import android.view.View; |
| 7 | +import android.widget.CompoundButton; |
| 8 | +import android.widget.Switch; |
4 | 9 | import android.widget.TextView; |
5 | 10 |
|
6 | 11 | import org.sofwerx.torgi.R; |
| 12 | +import org.sofwerx.torgi.service.TorgiService; |
7 | 13 |
|
8 | 14 | public class MainActivity extends AbstractTORGIActivity { |
9 | | - private TextView status; |
| 15 | + private TextView status, infoBigData; |
| 16 | + private Switch switchBigData; |
| 17 | + private boolean systemChangingSwitch = false; |
10 | 18 |
|
11 | 19 | @Override |
12 | 20 | public void onCreate(Bundle savedInstanceState) { |
13 | 21 | super.onCreate(savedInstanceState); |
14 | 22 | status = findViewById(R.id.textView); |
| 23 | + switchBigData = findViewById(R.id.switchBigData); |
| 24 | + infoBigData = findViewById(R.id.switchInfo); |
| 25 | + switchBigData.setOnCheckedChangeListener((buttonView, isChecked) -> { |
| 26 | + if (!systemChangingSwitch) { |
| 27 | + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); |
| 28 | + SharedPreferences.Editor edit = prefs.edit(); |
| 29 | + edit.putBoolean(TorgiService.PREFS_BIG_DATA,isChecked); |
| 30 | + edit.apply(); |
| 31 | + if (serviceBound) { |
| 32 | + if (isChecked) |
| 33 | + torgiService.startSensorService(); |
| 34 | + else |
| 35 | + torgiService.stopSensorService(); |
| 36 | + } |
| 37 | + showStatus(); |
| 38 | + } |
| 39 | + }); |
15 | 40 | } |
16 | 41 |
|
17 | 42 | @Override |
18 | 43 | public void onResume() { |
19 | 44 | super.onResume(); |
20 | | - if (permissionsPassed) |
21 | | - status.setText(getString(R.string.notification)); |
22 | | - else |
| 45 | + showStatus(); |
| 46 | + systemChangingSwitch = true; |
| 47 | + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); |
| 48 | + switchBigData.setChecked(prefs.getBoolean(TorgiService.PREFS_BIG_DATA,false)); |
| 49 | + systemChangingSwitch = false; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected void onTorgiServiceConnected() { |
| 54 | + super.onTorgiServiceConnected(); |
| 55 | + showStatus(); |
| 56 | + } |
| 57 | + |
| 58 | + private void showStatus() { |
| 59 | + if (permissionsPassed) { |
| 60 | + if (serviceBound) { |
| 61 | + status.setText(getString(R.string.notification)); |
| 62 | + status.setVisibility(View.VISIBLE); |
| 63 | + switchBigData.setVisibility(View.VISIBLE); |
| 64 | + if (switchBigData.isChecked()) |
| 65 | + infoBigData.setVisibility(View.VISIBLE); |
| 66 | + else |
| 67 | + infoBigData.setVisibility(View.INVISIBLE); |
| 68 | + } else { |
| 69 | + status.setVisibility(View.INVISIBLE); |
| 70 | + switchBigData.setVisibility(View.INVISIBLE); |
| 71 | + infoBigData.setVisibility(View.INVISIBLE); |
| 72 | + } |
| 73 | + } else { |
23 | 74 | status.setText(getString(R.string.torgi_not_running)); |
| 75 | + status.setVisibility(View.VISIBLE); |
| 76 | + switchBigData.setVisibility(View.INVISIBLE); |
| 77 | + infoBigData.setVisibility(View.INVISIBLE); |
| 78 | + } |
24 | 79 | } |
25 | 80 |
|
26 | 81 | @Override |
|
0 commit comments