Skip to content

Commit

Permalink
Possible fix for tracking not turning of on Galaxy S7
Browse files Browse the repository at this point in the history
  • Loading branch information
pgmccann committed Jul 5, 2018
1 parent 90cfb55 commit 50166c5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion catch/src/main/java/uk/ac/masts/sifids/CatchApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -61,6 +67,7 @@ public void onServiceDisconnected(ComponentName name) {
}
};
bindService(new Intent(this, CatchLocationService.class), connection, Context.BIND_AUTO_CREATE);
// unbindService(connection);
}
}

Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -63,61 +65,54 @@ 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();
}
}
});

fishingSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@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
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())),
Expand All @@ -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() {
Expand All @@ -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());
Expand All @@ -168,24 +162,24 @@ 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);
}
});
}
};

Thread newThread= new Thread(r);
Thread newThread = new Thread(r);
newThread.start();

locationSwitch.setChecked(((CatchApplication) this.getApplication()).isTrackingLocation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -225,7 +225,7 @@ public CatchLocationService getService() {
}
}

public void saveFirstFishingLocation() {
public void forceWriteLocation() {
this.mLocationListener.writeLocation();
}

Expand Down

0 comments on commit 50166c5

Please sign in to comment.