diff --git a/.idea/misc.xml b/.idea/misc.xml index 48eb1be..3b4d06d 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -45,7 +45,7 @@ - + diff --git a/catch/src/main/java/uk/ac/masts/sifids/CatchApplication.java b/catch/src/main/java/uk/ac/masts/sifids/CatchApplication.java index 85c7768..8f0d826 100644 --- a/catch/src/main/java/uk/ac/masts/sifids/CatchApplication.java +++ b/catch/src/main/java/uk/ac/masts/sifids/CatchApplication.java @@ -12,6 +12,7 @@ import android.os.SystemClock; import android.preference.PreferenceActivity; import android.preference.PreferenceManager; +import android.util.Log; import android.widget.Toast; import uk.ac.masts.sifids.activities.SettingsActivity; @@ -47,12 +48,17 @@ public boolean isFishing() { */ public void setFishing(boolean fishing) { this.fishing = fishing; + Log.e("LOCATION", "Fishing: " + fishing); if (this.isTrackingLocation()) { + Log.e("LOCATION", "Tracking location"); ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { + Log.e("LOCATION","Getting service"); CatchLocationService locationService = ((CatchLocationService.CatchLocationBinder) service).getService(); - locationService.saveFirstFishingLocation(); + locationService.forceWriteLocation(); + Log.e("LOCATION","Forced writing..."); + unbindService(this); } @Override @@ -61,6 +67,7 @@ public void onServiceDisconnected(ComponentName name) { } }; bindService(new Intent(this, CatchLocationService.class), connection, Context.BIND_AUTO_CREATE); +// unbindService(connection); } } @@ -69,6 +76,14 @@ public boolean isTrackingLocation() { } public void setTrackingLocation(boolean trackingLocation) { + if (trackingLocation) { + startService(new Intent(this, CatchLocationService.class)); + } + else { + stopService(new Intent(this, CatchLocationService.class)); + Toast.makeText(getBaseContext(), getString(R.string.stopped_tracking_location), + Toast.LENGTH_LONG).show(); + } this.trackingLocation = trackingLocation; } diff --git a/catch/src/main/java/uk/ac/masts/sifids/activities/Fish1FormsActivity.java b/catch/src/main/java/uk/ac/masts/sifids/activities/Fish1FormsActivity.java index 911b1aa..31352fe 100644 --- a/catch/src/main/java/uk/ac/masts/sifids/activities/Fish1FormsActivity.java +++ b/catch/src/main/java/uk/ac/masts/sifids/activities/Fish1FormsActivity.java @@ -1,6 +1,7 @@ package uk.ac.masts.sifids.activities; import android.Manifest; +import android.app.ActivityManager; import android.app.DatePickerDialog; import android.content.DialogInterface; import android.content.Intent; @@ -14,6 +15,7 @@ import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; +import android.util.Log; import android.view.View; import android.widget.CompoundButton; import android.widget.DatePicker; @@ -63,26 +65,21 @@ protected void onCreate(Bundle savedInstanceState) { locationSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - if (isChecked) { - if ( - ContextCompat.checkSelfPermission( - Fish1FormsActivity.this, - Manifest.permission.ACCESS_FINE_LOCATION - ) != PackageManager.PERMISSION_GRANTED) { - ActivityCompat.requestPermissions( + if ( + isChecked && ContextCompat.checkSelfPermission( Fish1FormsActivity.this, - new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, - PERMISSION_REQUEST_FINE_LOCATION); - } - else { - startService(new Intent(Fish1FormsActivity.this, CatchLocationService.class)); + Manifest.permission.ACCESS_FINE_LOCATION + ) != PackageManager.PERMISSION_GRANTED) { + ActivityCompat.requestPermissions( + Fish1FormsActivity.this, + new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, + PERMISSION_REQUEST_FINE_LOCATION); + } else { + ((CatchApplication) Fish1FormsActivity.this.getApplication()).setTrackingLocation(isChecked); + if (!isChecked) { + ((Switch) findViewById(R.id.toggle_fishing)).setChecked(false); } } - else { - stopService(new Intent(Fish1FormsActivity.this, CatchLocationService.class)); - Toast.makeText(getBaseContext(), getString(R.string.stopped_tracking_location), - Toast.LENGTH_LONG).show(); - } } }); @@ -90,21 +87,19 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { CatchApplication app = (CatchApplication) getApplication(); + app.setFishing(isChecked); if (isChecked) { - app.setFishing(true); ((Switch) findViewById(R.id.toggle_location_tracking)).setChecked(true); Toast.makeText(getBaseContext(), getString(R.string.started_fishing), Toast.LENGTH_LONG).show(); - } - else { - app.setFishing(false); + } else { Toast.makeText(getBaseContext(), getString(R.string.stopped_fishing), Toast.LENGTH_LONG).show(); } } }); - fab=(FloatingActionButton)findViewById(R.id.fab); + fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override @@ -112,12 +107,12 @@ public void onClick(View v) { final AlertDialog.Builder builder = new AlertDialog.Builder(Fish1FormsActivity.this); final Calendar mostRecentSunday = Calendar.getInstance(); mostRecentSunday.add(Calendar.DATE, -1 * (mostRecentSunday.get(Calendar.DAY_OF_WEEK) - 1)); - mostRecentSunday.set(Calendar.HOUR_OF_DAY,0); - mostRecentSunday.set(Calendar.MINUTE,0); - mostRecentSunday.set(Calendar.SECOND,0); + mostRecentSunday.set(Calendar.HOUR_OF_DAY, 0); + mostRecentSunday.set(Calendar.MINUTE, 0); + mostRecentSunday.set(Calendar.SECOND, 0); final Calendar sundayPreviousToMostRecent = (Calendar) mostRecentSunday.clone(); sundayPreviousToMostRecent.add(Calendar.DATE, -7); - DateFormat df=new SimpleDateFormat("dd MMM"); + DateFormat df = new SimpleDateFormat("dd MMM"); selectedWeekStart = null; final CharSequence[] items = { String.format(getString(R.string.this_week), df.format(mostRecentSunday.getTime())), @@ -128,15 +123,14 @@ public void onClick(View v) { @Override public void onClick(DialogInterface dialog, int which) { if (which <= 1) { - Intent i = new Intent(Fish1FormsActivity.this,EditFish1FormActivity.class); + Intent i = new Intent(Fish1FormsActivity.this, EditFish1FormActivity.class); if (which == 0) selectedWeekStart = mostRecentSunday; else if (which == 1) selectedWeekStart = sundayPreviousToMostRecent; i.putExtra("start_date", selectedWeekStart.getTime()); selectedWeekStart.add(Calendar.DATE, 7); i.putExtra("end_date", selectedWeekStart.getTime()); startActivity(i); - } - else { + } else { DatePickerDialog picker = new DatePickerDialog( Fish1FormsActivity.this, new DatePickerDialog.OnDateSetListener() { @@ -145,7 +139,7 @@ public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { Calendar chosenDate = Calendar.getInstance(); chosenDate.set(year, month, dayOfMonth, 0, 0); chosenDate.add(Calendar.DATE, -1 * (chosenDate.get(Calendar.DAY_OF_WEEK) - 1)); - Intent i = new Intent(Fish1FormsActivity.this,EditFish1FormActivity.class); + Intent i = new Intent(Fish1FormsActivity.this, EditFish1FormActivity.class); i.putExtra("start_date", chosenDate.getTime()); chosenDate.add(Calendar.DATE, 7); i.putExtra("end_date", chosenDate.getTime()); @@ -168,16 +162,16 @@ public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { @Override protected void onResume() { super.onResume(); - Runnable r = new Runnable(){ + Runnable r = new Runnable() { @Override public void run() { forms = db.catchDao().getForms(); - adapter= new Fish1FormAdapter(forms, Fish1FormsActivity.this); + adapter = new Fish1FormAdapter(forms, Fish1FormsActivity.this); adapter.notifyDataSetChanged(); runOnUiThread(new Runnable() { @Override public void run() { - recyclerView= (RecyclerView)findViewById(R.id.form_recycler_view); + recyclerView = (RecyclerView) findViewById(R.id.form_recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(getApplication())); recyclerView.setAdapter(adapter); } @@ -185,7 +179,7 @@ public void run() { } }; - Thread newThread= new Thread(r); + Thread newThread = new Thread(r); newThread.start(); locationSwitch.setChecked(((CatchApplication) this.getApplication()).isTrackingLocation()); diff --git a/catch/src/main/java/uk/ac/masts/sifids/services/CatchLocationService.java b/catch/src/main/java/uk/ac/masts/sifids/services/CatchLocationService.java index b8a0c22..6cf10c7 100644 --- a/catch/src/main/java/uk/ac/masts/sifids/services/CatchLocationService.java +++ b/catch/src/main/java/uk/ac/masts/sifids/services/CatchLocationService.java @@ -16,6 +16,7 @@ import android.os.IBinder; import android.support.v4.app.ActivityCompat; import android.support.v4.app.NotificationCompat; +import android.util.Log; import java.util.Date; @@ -106,7 +107,6 @@ public IBinder onBind(Intent arg0) { @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); - ((CatchApplication) getApplication()).setTrackingLocation(true); return START_STICKY; } @@ -189,8 +189,8 @@ private Notification getNotification() { @Override public void onDestroy() { + Log.e("LOCATION", "onDestroy..."); super.onDestroy(); - ((CatchApplication) getApplication()).setTrackingLocation(false); if (mLocationManager != null) { try { if ( @@ -225,7 +225,7 @@ public CatchLocationService getService() { } } - public void saveFirstFishingLocation() { + public void forceWriteLocation() { this.mLocationListener.writeLocation(); }