|
| 1 | +package com.github.sensorsreport; |
| 2 | + |
| 3 | +import android.Manifest; |
| 4 | +import android.arch.lifecycle.ViewModelProviders; |
| 5 | +import android.content.Intent; |
| 6 | +import android.content.SharedPreferences; |
| 7 | +import android.content.pm.PackageManager; |
| 8 | +import android.databinding.DataBindingUtil; |
| 9 | +import android.os.Bundle; |
| 10 | +import android.os.Environment; |
| 11 | +import android.preference.PreferenceManager; |
| 12 | +import android.support.annotation.NonNull; |
| 13 | +import android.support.v4.app.ActivityCompat; |
| 14 | +import android.support.v4.content.ContextCompat; |
| 15 | +import android.support.v7.app.AppCompatActivity; |
| 16 | +import android.view.View; |
| 17 | +import android.widget.AdapterView; |
| 18 | +import android.widget.ArrayAdapter; |
| 19 | +import android.widget.Toast; |
| 20 | + |
| 21 | +import com.github.sensorsreport.data.SensorData; |
| 22 | +import com.github.sensorsreport.data.db.AppDatabase; |
| 23 | +import com.github.sensorsreport.data.db.SensorDataDao; |
| 24 | +import com.github.sensorsreport.databinding.MainActivityBinding; |
| 25 | +import com.github.sensorsreport.service.ReportService; |
| 26 | +import com.google.gson.Gson; |
| 27 | +import com.google.gson.reflect.TypeToken; |
| 28 | + |
| 29 | +import java.io.File; |
| 30 | +import java.io.FileWriter; |
| 31 | +import java.io.IOException; |
| 32 | +import java.lang.reflect.Type; |
| 33 | +import java.util.List; |
| 34 | +import java.util.concurrent.Executor; |
| 35 | +import java.util.concurrent.Executors; |
| 36 | + |
| 37 | +public class MainActivity extends AppCompatActivity implements |
| 38 | + SharedPreferences.OnSharedPreferenceChangeListener { |
| 39 | + |
| 40 | + private static final String REPORT_REQUEST_TIME = "report_request_time"; |
| 41 | + private static final String SELECTED_INTERVAL = "selected_interval"; |
| 42 | + |
| 43 | + private static final String[] INTERVALS = {"200", "500", "700", "1000", "1500", "2000"}; |
| 44 | + |
| 45 | + private MainActivityBinding binding; |
| 46 | + |
| 47 | + private MainViewModel viewModel; |
| 48 | + |
| 49 | + private SharedPreferences sharedPreferences; |
| 50 | + |
| 51 | + private Executor diskExecutor; |
| 52 | + |
| 53 | + private long reportStartTime = 0; |
| 54 | + |
| 55 | + @Override |
| 56 | + protected void onCreate(Bundle savedInstanceState) { |
| 57 | + super.onCreate(savedInstanceState); |
| 58 | + binding = DataBindingUtil.setContentView(this, R.layout.activity_main); |
| 59 | + |
| 60 | + diskExecutor = Executors.newSingleThreadExecutor(); |
| 61 | + |
| 62 | + requestPermissions(); |
| 63 | + |
| 64 | + viewModel = ViewModelProviders.of(this).get(MainViewModel.class); |
| 65 | + |
| 66 | + viewModel.getDataReport().observe(this, sensorData -> binding.setData(sensorData)); |
| 67 | + |
| 68 | + Intent reportServiceIntent = |
| 69 | + new Intent(MainActivity.this, ReportService.class); |
| 70 | + |
| 71 | + SensorDataDao sensorDataDao = AppDatabase.getDatabase(this).sensorDataDao(); |
| 72 | + |
| 73 | + sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); |
| 74 | + |
| 75 | + boolean reportRequested = sharedPreferences |
| 76 | + .getBoolean(ReportService.REQUESTING_REPORT, false); |
| 77 | + binding.setStarted(reportRequested); |
| 78 | + |
| 79 | + if (reportRequested) { |
| 80 | + reportStartTime = sharedPreferences |
| 81 | + .getLong(REPORT_REQUEST_TIME, System.currentTimeMillis()); |
| 82 | + } |
| 83 | + |
| 84 | + Type listOfSensorData = new TypeToken<List<SensorData>>() {}.getType(); |
| 85 | + |
| 86 | + binding.startButton.setOnClickListener(view -> { |
| 87 | + reportStartTime = System.currentTimeMillis(); |
| 88 | + ContextCompat.startForegroundService(this, reportServiceIntent); |
| 89 | + sharedPreferences.edit().putLong(REPORT_REQUEST_TIME, reportStartTime).apply(); |
| 90 | + }); |
| 91 | + |
| 92 | + binding.stopButton.setOnClickListener(view -> { |
| 93 | + stopService(reportServiceIntent); |
| 94 | + if (!isExternalStorageWritable()) { |
| 95 | + Toast.makeText(this, "External storage is not available!", |
| 96 | + Toast.LENGTH_SHORT).show(); |
| 97 | + } else { |
| 98 | + diskExecutor.execute(() -> { |
| 99 | + List<SensorData> dataList = sensorDataDao.getSensorsReport(reportStartTime); |
| 100 | + String json = new Gson().toJson(dataList, listOfSensorData); |
| 101 | + |
| 102 | + File file = new File(Environment.getExternalStoragePublicDirectory( |
| 103 | + Environment.DIRECTORY_DOWNLOADS), |
| 104 | + "sensors-report-" + reportStartTime + ".json"); |
| 105 | + try { |
| 106 | + FileWriter fileWriter = new FileWriter(file); |
| 107 | + fileWriter.write(json); |
| 108 | + fileWriter.flush(); |
| 109 | + fileWriter.close(); |
| 110 | + } catch (IOException e) { |
| 111 | + e.printStackTrace(); |
| 112 | + } |
| 113 | + }); |
| 114 | + } |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + protected void onStart() { |
| 120 | + super.onStart(); |
| 121 | + sharedPreferences.registerOnSharedPreferenceChangeListener(this); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + protected void onStop() { |
| 126 | + sharedPreferences.unregisterOnSharedPreferenceChangeListener(this); |
| 127 | + super.onStop(); |
| 128 | + } |
| 129 | + |
| 130 | + private void requestPermissions() { |
| 131 | + ActivityCompat.requestPermissions(MainActivity.this, |
| 132 | + new String[]{Manifest.permission.ACCESS_FINE_LOCATION, |
| 133 | + Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1000); |
| 134 | + } |
| 135 | + |
| 136 | + public boolean isExternalStorageWritable() { |
| 137 | + String state = Environment.getExternalStorageState(); |
| 138 | + if (Environment.MEDIA_MOUNTED.equals(state)) { |
| 139 | + return true; |
| 140 | + } |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, |
| 146 | + @NonNull int[] grantResults) { |
| 147 | + if (requestCode == 1000) { |
| 148 | + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && |
| 149 | + grantResults[1] == PackageManager.PERMISSION_GRANTED) { |
| 150 | + binding.container.setEnabled(true); |
| 151 | + populateIntervalSpinner(); |
| 152 | + } else { |
| 153 | + binding.container.setEnabled(false); |
| 154 | + } |
| 155 | + } |
| 156 | + super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 157 | + } |
| 158 | + |
| 159 | + private void populateIntervalSpinner() { |
| 160 | + ArrayAdapter<String> adapter = new ArrayAdapter<>(this, |
| 161 | + android.R.layout.simple_spinner_dropdown_item, INTERVALS); |
| 162 | + binding.reportInterval.setAdapter(adapter); |
| 163 | + binding.reportInterval.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { |
| 164 | + @Override |
| 165 | + public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { |
| 166 | + viewModel.setReportInterval(Long.parseLong(INTERVALS[i])); |
| 167 | + sharedPreferences.edit().putInt(SELECTED_INTERVAL, i).apply(); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public void onNothingSelected(AdapterView<?> adapterView) { |
| 172 | + |
| 173 | + } |
| 174 | + }); |
| 175 | + binding.reportInterval.setSelection(sharedPreferences.getInt(SELECTED_INTERVAL, 0)); |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) { |
| 180 | + if (s.equals(ReportService.REQUESTING_REPORT)) { |
| 181 | + binding.setStarted(sharedPreferences.getBoolean(s, false)); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments