3030import android .widget .TextView ;
3131import android .widget .Toast ;
3232
33+ import butterknife .BindView ;
34+ import butterknife .ButterKnife ;
3335import io .pslab .R ;
3436import io .pslab .fragment .LuxMeterFragmentConfig ;
3537import io .pslab .fragment .LuxMeterFragmentData ;
38+ import io .pslab .fragment .SettingsFragment ;
3639import io .pslab .others .CSVLogger ;
3740import io .pslab .others .CustomSnackBar ;
3841import io .pslab .others .GPSLogger ;
3942import io .pslab .others .MathUtils ;
40- import io .pslab .fragment .SettingsFragment ;
4143import io .pslab .others .SwipeGestureDetector ;
4244
43- import butterknife .BindView ;
44- import butterknife .ButterKnife ;
45-
4645public class LuxMeterActivity extends AppCompatActivity {
4746
4847 private static final String PREF_NAME = "customDialogPreference" ;
@@ -85,13 +84,16 @@ public class LuxMeterActivity extends AppCompatActivity {
8584 private boolean checkGpsOnResume = false ;
8685 public boolean locationPref ;
8786 private LuxMeterFragmentData selectedFragment ;
87+ public static final String NAME = "realmData" ;
88+ private SharedPreferences realmPreferences ;
8889
8990 @ Override
9091 protected void onCreate (Bundle savedInstanceState ) {
9192 super .onCreate (savedInstanceState );
9293 setContentView (R .layout .activity_lux_main );
9394 ButterKnife .bind (this );
9495 setSupportActionBar (toolbar );
96+ realmPreferences = getSharedPreferences (NAME , Context .MODE_PRIVATE );
9597 setUpBottomSheet ();
9698 bottomNavigationView .setOnNavigationItemSelectedListener
9799 (new BottomNavigationView .OnNavigationItemSelectedListener () {
@@ -215,36 +217,34 @@ public boolean onPrepareOptionsMenu(Menu menu) {
215217 public boolean onOptionsItemSelected (MenuItem item ) {
216218 switch (item .getItemId ()) {
217219 case R .id .record_data :
218- if (ContextCompat .checkSelfPermission (this ,
219- Manifest .permission .WRITE_EXTERNAL_STORAGE )
220- != PackageManager .PERMISSION_GRANTED ) {
221- ActivityCompat .requestPermissions (this ,
222- new String []{Manifest .permission .WRITE_EXTERNAL_STORAGE }, MY_PERMISSIONS_REQUEST_STORAGE_FOR_DATA );
223- return true ;
224- }
225220 if (recordData ) {
226- ((LuxMeterFragmentData )selectedFragment ).stopSensorFetching ();
221+ ((LuxMeterFragmentData ) selectedFragment ).stopSensorFetching ();
227222 invalidateOptionsMenu ();
223+ Long uniqueRef = realmPreferences .getLong ("uniqueCount" , 0 );
224+ selectedFragment .saveDataInRealm (uniqueRef ,locationPref ,gpsLogger );
225+ CustomSnackBar .showSnackBar (coordinatorLayout , getString (R .string .exp_data_saved ), null , null );
226+ SharedPreferences .Editor editor = realmPreferences .edit ();
227+ editor .putLong ("uniqueCount" , uniqueRef + 1 );
228+ editor .commit ();
228229 recordData = false ;
229230 } else {
230- luxLogger = new CSVLogger (getString (R .string .lux_meter ));
231- luxLogger .writeCSVFile ("Timestamp,X,Y,Z\n " );
232- recordData = true ;
233- ((LuxMeterFragmentData )selectedFragment ).startSensorFetching ();
234- invalidateOptionsMenu ();
235231 if (locationPref ) {
236232 gpsLogger = new GPSLogger (this , (LocationManager ) getSystemService (Context .LOCATION_SERVICE ));
237233 if (gpsLogger .isGPSEnabled ()) {
238234 recordData = true ;
239- CustomSnackBar .showSnackBar (coordinatorLayout , getString (R .string .data_recording_start ) + "\n " + getString (R .string .location_enabled ), null , null );
235+ ((LuxMeterFragmentData ) selectedFragment ).startSensorFetching ();
236+ invalidateOptionsMenu ();
240237 } else {
241238 checkGpsOnResume = true ;
242239 }
243240 gpsLogger .startFetchingLocation ();
244241 } else {
245242 recordData = true ;
246- CustomSnackBar .showSnackBar (coordinatorLayout , getString (R .string .data_recording_start ) + "\n " + getString (R .string .location_disabled ), null , null );
243+ ((LuxMeterFragmentData ) selectedFragment ).startSensorFetching ();
244+ invalidateOptionsMenu ();
247245 }
246+ String snackText = getString (R .string .data_recording_start )+"\n " +(locationPref ?getString (R .string .location_enabled ):getString (R .string .location_disabled ));
247+ CustomSnackBar .showSnackBar (coordinatorLayout , snackText , null , null );
248248 }
249249 break ;
250250 case R .id .show_map :
@@ -260,6 +260,12 @@ public boolean onOptionsItemSelected(MenuItem item) {
260260 break ;
261261 case R .id .settings :
262262 startActivity (new Intent (this , SettingsActivity .class ));
263+ break ;
264+ case R .id .show_logged_data :
265+ Intent intent = new Intent (this , DataLoggerActivity .class );
266+ intent .putExtra (DataLoggerActivity .CALLER_ACTIVITY , "Lux Meter" );
267+ startActivity (intent );
268+
263269 break ;
264270 default :
265271 break ;
@@ -273,38 +279,27 @@ protected void onResume() {
273279 if (checkGpsOnResume ) {
274280 if (gpsLogger .isGPSEnabled ()) {
275281 recordData = true ;
276- CustomSnackBar .showSnackBar (coordinatorLayout , getString (R .string .data_recording_start ), null , null );
282+ ((LuxMeterFragmentData ) selectedFragment ).startSensorFetching ();
283+ invalidateOptionsMenu ();
284+ gpsLogger .startFetchingLocation ();
285+ CustomSnackBar .showSnackBar (coordinatorLayout ,getString (R .string .data_recording_start )+getString (R .string .location_enabled ) , null , null );
277286 } else {
278287 recordData = false ;
279288 Toast .makeText (getApplicationContext (), getString (R .string .gps_not_enabled ),
280289 Toast .LENGTH_SHORT ).show ();
290+ gpsLogger .removeUpdate ();
281291 }
292+ checkGpsOnResume = false ;
282293 }
283294 locationPref = PreferenceManager .getDefaultSharedPreferences (getBaseContext ()).getBoolean (SettingsFragment .KEY_INCLUDE_LOCATION , false );
295+ if (!locationPref && gpsLogger !=null ){
296+ gpsLogger = null ;
297+ }
284298 }
285299
286300 @ Override
287301 public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults ) {
288- if (requestCode == MY_PERMISSIONS_REQUEST_STORAGE_FOR_DATA ) {
289- if (grantResults .length > 0
290- && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
291- if (locationPref ) {
292- gpsLogger = new GPSLogger (this , (LocationManager ) getSystemService (Context .LOCATION_SERVICE ));
293- if (gpsLogger .isGPSEnabled ()) {
294- recordData = true ;
295- CustomSnackBar .showSnackBar (coordinatorLayout , getString (R .string .data_recording_start ) + "\n " + getString (R .string .location_enabled ), null , null );
296- } else {
297- checkGpsOnResume = true ;
298- }
299- gpsLogger .startFetchingLocation ();
300- } else {
301- recordData = true ;
302- CustomSnackBar .showSnackBar (coordinatorLayout , getString (R .string .data_recording_start ) + "\n " + getString (R .string .location_disabled ), null , null );
303- }
304- } else {
305- Toast .makeText (this , R .string .prmsn_denied_storage , Toast .LENGTH_SHORT ).show ();
306- }
307- } else if (requestCode == MY_PERMISSIONS_REQUEST_STORAGE_FOR_MAPS
302+ if (requestCode == MY_PERMISSIONS_REQUEST_STORAGE_FOR_MAPS
308303 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
309304 Intent MAP = new Intent (getApplicationContext (), MapsActivity .class );
310305 startActivity (MAP );
0 commit comments