use of ch.hsr.sa.radiotour.R in project app by TourLive.
the class MainActivity method initViewsAndHandlers.
private void initViewsAndHandlers() {
uiHandler = new Handler();
timerForRace = new Timer();
heightView = findViewById(R.id.txtHeightValue);
topFieldActualGapView = findViewById(R.id.txtTopFieldActualGap);
topRadioTourActualGapView = findViewById(R.id.txtTopRadioTourActualGap);
stageView = findViewById(R.id.txtStageValue);
velocityView = findViewById(R.id.txtVelocityValue);
raceKilometerView = findViewById(R.id.txtRacekilometerValue);
raceTimeView = findViewById(R.id.txtRacetimeValue);
startStopView = findViewById(R.id.btnStartStopRace);
resetView = findViewById(R.id.btnReset);
if (StagePresenter.getInstance().getStage() != null)
updateStageInfo(StagePresenter.getInstance().getStage());
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, 0);
}
startStopView.setOnClickListener((View click) -> {
if (!raceInProgress) {
raceInProgress = true;
startStopView.setBackgroundColor(getColor(R.color.colorOlive));
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationManager.getProvider(LocationManager.GPS_PROVIDER).supportsAltitude();
locationManager.addNmeaListener((OnNmeaMessageListener) (message, timestamp) -> {
if (message.startsWith("$")) {
String[] tokens = message.split(",");
String type = tokens[0];
if (type.startsWith("$GPGGA") && !tokens[11].isEmpty()) {
correctionHeight = Double.parseDouble(tokens[11]);
}
}
});
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (actualLocation != null) {
distanceInMeter += actualLocation.distanceTo(location);
}
actualLocation = location;
uiHandler.post(() -> {
heightView.setText(getString(R.string.header_prefix_m, (actualLocation.getAltitude() - correctionHeight)));
raceKilometerView.setText(getString(R.string.header_prefix_km, distanceInMeter / 1000f, wholeDistanceInKm));
double seconds = TimeUnit.MILLISECONDS.toSeconds(raceTime.getTime());
double averageSpeed = (distanceInMeter / seconds) * 3.6;
velocityView.setText(getString(R.string.header_prefix_kmh, averageSpeed));
});
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// Has to be implemented, but not needed
}
@Override
public void onProviderEnabled(String provider) {
// Has to be implemented, but not needed
}
@Override
public void onProviderDisabled(String provider) {
// Has to be implemented, but not needed
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, updateTime, minDistanceChange, locationListener);
timerForRace = new Timer();
timerTaskForRace = new TimerTask() {
@Override
public void run() {
raceTime.setTime(raceTime.getTime() + updateTimeForRace);
uiHandler.post(() -> raceTimeView.setText(convertLongToTimeString(raceTime.getTime())));
timeInRaceGroupCounter++;
if (timeInRaceGroupCounter == 60) {
timeInRaceGroupCounter = 0;
double tempDistanceInLeadGroup = distanceInLeadGroup;
distanceInLeadGroup = distanceInMeter / 1000f;
RaceGroup leadRaceGroup = RaceGroupPresenter.getInstance().getLeadRaceGroup();
if (leadRaceGroup != null) {
for (Rider r : RaceGroupPresenter.getInstance().getLeadRaceGroup().getRiders()) {
RiderStageConnectionPresenter.getInstance().appendTimeInLeadGroup(r.getRiderStages().first(), 1);
RiderStageConnectionPresenter.getInstance().appendDistanceInLeadGroup(r.getRiderStages().first(), (distanceInLeadGroup - tempDistanceInLeadGroup));
}
}
}
}
};
timerForRace.schedule(timerTaskForRace, delayZero, updateTimeForRace);
}
} else {
raceInProgress = false;
locationManager.removeUpdates(locationListener);
startStopView.setBackgroundColor(getColor(R.color.colorPrimaryUltraLight));
timerForRace.cancel();
}
});
resetView.setOnClickListener(event -> {
new AlertDialog.Builder(this).setTitle(R.string.resettime_title).setMessage(R.string.resettime_message).setNegativeButton(android.R.string.cancel, null).setPositiveButton(android.R.string.ok, (DialogInterface dialog, int which) -> resetTime()).create().show();
});
raceTimeView.setOnClickListener((View event) -> {
TimePickerDialog dialog = new TimePickerDialog(this, (view, hourOfDay, minute) -> {
raceTime.setTime(new Time(TimeUnit.HOURS.toMillis(view.getHour()) + TimeUnit.MINUTES.toMillis(view.getMinute())).getTime());
raceTimeView.setText(convertLongToTimeString(raceTime.getTime()));
}, 0, 0, true);
dialog.setTitle(R.string.header_select_time);
dialog.show();
});
timerForUpdate = new Timer();
timerTaskForUpdate = new TimerTask() {
@Override
public void run() {
updateUIInfos();
}
};
timerForUpdate.schedule(timerTaskForUpdate, delayTime, updateTime);
PostHandler postHandler = new PostHandler();
postHandler.start();
}
Aggregations