Java tutorial
/******************************************************************************* * This file is part of GPSLogger for Android. * <p/> * GPSLogger for Android is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * <p/> * GPSLogger for Android is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * <p/> * You should have received a copy of the GNU General Public License * along with GPSLogger for Android. If not, see <http://www.gnu.org/licenses/>. ******************************************************************************/ package com.crearo.gpslogger.ui.fragments.display; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.location.Location; import android.location.LocationManager; import android.net.Uri; import android.os.Bundle; import android.provider.Settings; import android.support.v4.content.ContextCompat; import android.text.Html; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.animation.AlphaAnimation; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.crearo.gpslogger.R; import com.crearo.gpslogger.common.EventBusHook; import com.crearo.gpslogger.common.PreferenceHelper; import com.crearo.gpslogger.common.Session; import com.crearo.gpslogger.common.Strings; import com.crearo.gpslogger.common.events.ServiceEvents; import com.crearo.gpslogger.common.slf4j.Logs; import com.crearo.gpslogger.loggers.Files; import com.crearo.gpslogger.ui.Dialogs; import com.dd.processbutton.iml.ActionProcessButton; import org.slf4j.Logger; import java.text.NumberFormat; public class GpsSimpleViewFragment extends GenericViewFragment implements View.OnClickListener { Context context; private static final Logger LOG = Logs.of(GpsSimpleViewFragment.class); private PreferenceHelper preferenceHelper = PreferenceHelper.getInstance(); private View rootView; private ActionProcessButton actionButton; private TextView displayTV, doneTV, distanceTV; Button removeNotiBtn; public GpsSimpleViewFragment() { } public static GpsSimpleViewFragment newInstance() { GpsSimpleViewFragment fragment = new GpsSimpleViewFragment(); Bundle bundle = new Bundle(1); bundle.putInt("a_number", 1); fragment.setArguments(bundle); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { rootView = inflater.inflate(R.layout.fragment_simple_view, container, false); if (getActivity() != null) { this.context = getActivity().getApplicationContext(); } setImageTooltips(); showPreferencesSummary(); actionButton = (ActionProcessButton) rootView.findViewById(R.id.btnActionProcess); actionButton.setMode(ActionProcessButton.Mode.ENDLESS); actionButton.setBackgroundColor(ContextCompat.getColor(context, (R.color.accentColor))); displayTV = (TextView) rootView.findViewById(R.id.fsv_display); doneTV = (TextView) rootView.findViewById(R.id.fsv_done); distanceTV = (TextView) rootView.findViewById(R.id.fsv_distance); removeNotiBtn = (Button) rootView.findViewById(R.id.simpleview_removenoti); actionButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { requestToggleLogging(); } }); if (Session.hasValidLocation()) { displayLocationInfo(Session.getCurrentLocationInfo()); displayTV.setVisibility(View.GONE); } doneTV.setVisibility(View.GONE); if (Session.isStarted()) { doneTV.setVisibility(View.VISIBLE); } distanceTV.setText( "Thanks for downloading the app! We need you to keep this app on till the end of this semester. We are researching offline social networks using human movement patterns on a university campus and we really appreciate you for helping us in collecting data. Simple leave this application on. Turn on logging and forget that it exists :)\nIf you choose to disable the notification on top, PLEASE, PLEASE make sure you leave GPS on!"); if (Session.isStarted()) { removeNotiBtn.setVisibility(View.VISIBLE); } else removeNotiBtn.setVisibility(View.INVISIBLE); rootView.findViewById(R.id.simpleview_removenoti).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Dialogs.alert("Hold On!", "Yes, we know the sticky notification on top is slightly unattractive. Disable notifications in the next page. But PLEASE make sure you keep GPS on till this semester ends! PLEASE, PLEASE, PLEASE.", getActivity(), new Dialogs.MessageBoxCallback() { @Override public void messageBoxResult(int which) { startInstalledAppDetailsActivity(getActivity()); } }); } }); return rootView; } private void startInstalledAppDetailsActivity(final Activity context) { if (context == null) { return; } final Intent i = new Intent(); i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); i.addCategory(Intent.CATEGORY_DEFAULT); i.setData(Uri.parse("package:" + context.getPackageName())); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); context.startActivity(i); } private void setActionButtonStart() { actionButton.setText(R.string.btn_start_logging); actionButton.setBackgroundColor(ContextCompat.getColor(context, R.color.accentColor)); actionButton.setAlpha(0.8f); doneTV.setVisibility(View.GONE); } private void setActionButtonStop() { actionButton.setText(R.string.btn_stop_logging); actionButton.setBackgroundColor(ContextCompat.getColor(context, R.color.accentColorComplementary)); actionButton.setAlpha(0.8f); doneTV.setVisibility(View.VISIBLE); } private void showPreferencesSummary() { showCurrentFileName(Session.getCurrentFileName(getActivity())); ImageView imgGpx = (ImageView) rootView.findViewById(R.id.simpleview_imgGpx); ImageView imgKml = (ImageView) rootView.findViewById(R.id.simpleview_imgKml); ImageView imgCsv = (ImageView) rootView.findViewById(R.id.simpleview_imgCsv); ImageView imgNmea = (ImageView) rootView.findViewById(R.id.simpleview_imgNmea); ImageView imgLink = (ImageView) rootView.findViewById(R.id.simpleview_imgLink); if (preferenceHelper.shouldLogToGpx()) { imgGpx.setVisibility(View.VISIBLE); } else { imgGpx.setVisibility(View.GONE); } if (preferenceHelper.shouldLogToKml()) { imgKml.setVisibility(View.VISIBLE); } else { imgKml.setVisibility(View.GONE); } if (preferenceHelper.shouldLogToNmea()) { imgNmea.setVisibility(View.VISIBLE); } else { imgNmea.setVisibility(View.GONE); } if (preferenceHelper.shouldLogToPlainText()) { imgCsv.setVisibility(View.VISIBLE); } else { imgCsv.setVisibility(View.GONE); } if (preferenceHelper.shouldLogToCustomUrl()) { imgLink.setVisibility(View.VISIBLE); } else { imgLink.setVisibility(View.GONE); } if (!preferenceHelper.shouldLogToGpx() && !preferenceHelper.shouldLogToKml() && !preferenceHelper.shouldLogToPlainText()) { showCurrentFileName(null); } } private void showCurrentFileName(String newFileName) { TextView txtFilename = (TextView) rootView.findViewById(R.id.simpleview_txtfilepath); if (newFileName == null || newFileName.length() <= 0) { txtFilename.setText(""); txtFilename.setVisibility(View.INVISIBLE); return; } txtFilename.setVisibility(View.VISIBLE); txtFilename.setText(Html.fromHtml("<em>" + preferenceHelper.getGpsLoggerFolder() + "/<strong><br />" + Session.getCurrentFileName(getActivity()) + "</strong></em>")); Files.setFileExplorerLink(txtFilename, Html.fromHtml("<em><font color='blue'><u>" + preferenceHelper.getGpsLoggerFolder() + "</u></font>" + "/<strong><br />" + Session.getCurrentFileName(getActivity()) + "</strong></em>"), preferenceHelper.getGpsLoggerFolder(), context); } private enum IconColorIndicator { Good, Warning, Bad, Inactive } private void clearColor(ImageView imgView) { setColor(imgView, IconColorIndicator.Inactive); } private void setColor(ImageView imgView, IconColorIndicator colorIndicator) { imgView.clearColorFilter(); if (colorIndicator == IconColorIndicator.Inactive) { return; } int color = -1; switch (colorIndicator) { case Bad: color = Color.parseColor("#FFEEEE"); break; case Good: color = ContextCompat.getColor(context, R.color.accentColor); break; case Warning: color = Color.parseColor("#D4FFA300"); break; } imgView.setColorFilter(color); } private void setImageTooltips() { ImageView imgSatellites = (ImageView) rootView.findViewById(R.id.simpleview_imgSatelliteCount); imgSatellites.setOnClickListener(this); ImageView imgAccuracy = (ImageView) rootView.findViewById(R.id.simpleview_imgAccuracy); imgAccuracy.setOnClickListener(this); ImageView imgElevation = (ImageView) rootView.findViewById(R.id.simpleview_imgAltitude); imgElevation.setOnClickListener(this); ImageView imgBearing = (ImageView) rootView.findViewById(R.id.simpleview_imgDirection); imgBearing.setOnClickListener(this); ImageView imgDuration = (ImageView) rootView.findViewById(R.id.simpleview_imgDuration); imgDuration.setOnClickListener(this); ImageView imgSpeed = (ImageView) rootView.findViewById(R.id.simpleview_imgSpeed); imgSpeed.setOnClickListener(this); ImageView imgDistance = (ImageView) rootView.findViewById(R.id.simpleview_distance); imgDistance.setOnClickListener(this); ImageView imgPoints = (ImageView) rootView.findViewById(R.id.simpleview_points); imgPoints.setOnClickListener(this); ImageView imgLink = (ImageView) rootView.findViewById(R.id.simpleview_imgLink); imgLink.setOnClickListener(this); } @Override public void onStart() { setActionButtonStop(); super.onStart(); } @Override public void onResume() { showPreferencesSummary(); if (Session.isStarted()) { setActionButtonStop(); doneTV.setVisibility(View.VISIBLE); removeNotiBtn.setVisibility(View.VISIBLE); } else { setActionButtonStart(); doneTV.setVisibility(View.GONE); removeNotiBtn.setVisibility(View.INVISIBLE); } super.onResume(); } @Override public void onPause() { super.onPause(); } @EventBusHook public void onEventMainThread(ServiceEvents.LocationUpdate locationUpdate) { displayLocationInfo(locationUpdate.location); displayTV.setVisibility(View.GONE); } @EventBusHook public void onEventMainThread(ServiceEvents.SatellitesVisible satellitesVisible) { setSatelliteCount(satellitesVisible.satelliteCount); } @EventBusHook public void onEventMainThread(ServiceEvents.WaitingForLocation waitingForLocation) { onWaitingForLocation(waitingForLocation.waiting); displayTV.setVisibility(View.GONE); } @EventBusHook public void onEventMainThread(ServiceEvents.LoggingStatus loggingStatus) { if (loggingStatus.loggingStarted) { showPreferencesSummary(); clearLocationDisplay(); setActionButtonStop(); displayTV.setVisibility(View.GONE); } else { setSatelliteCount(-1); displayTV.setVisibility(View.VISIBLE); setActionButtonStart(); } } @EventBusHook public void onEventMainThread(ServiceEvents.FileNamed fileNamed) { showCurrentFileName(fileNamed.newFileName); } public void displayLocationInfo(Location locationInfo) { showPreferencesSummary(); NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); EditText txtLatitude = (EditText) rootView.findViewById(R.id.simple_lat_text); txtLatitude.setText(String.valueOf(nf.format(locationInfo.getLatitude())) + ", " + String.valueOf(nf.format(locationInfo.getLongitude()))); nf.setMaximumFractionDigits(3); ImageView imgAccuracy = (ImageView) rootView.findViewById(R.id.simpleview_imgAccuracy); clearColor(imgAccuracy); if (locationInfo.hasAccuracy()) { TextView txtAccuracy = (TextView) rootView.findViewById(R.id.simpleview_txtAccuracy); float accuracy = locationInfo.getAccuracy(); txtAccuracy.setText(Strings.getDistanceDisplay(getActivity(), accuracy, preferenceHelper.shouldDisplayImperialUnits())); if (accuracy > 500) { setColor(imgAccuracy, IconColorIndicator.Warning); } if (accuracy > 900) { setColor(imgAccuracy, IconColorIndicator.Bad); } else { setColor(imgAccuracy, IconColorIndicator.Good); } } ImageView imgAltitude = (ImageView) rootView.findViewById(R.id.simpleview_imgAltitude); clearColor(imgAltitude); if (locationInfo.hasAltitude()) { setColor(imgAltitude, IconColorIndicator.Good); TextView txtAltitude = (TextView) rootView.findViewById(R.id.simpleview_txtAltitude); txtAltitude.setText(Strings.getDistanceDisplay(getActivity(), locationInfo.getAltitude(), preferenceHelper.shouldDisplayImperialUnits())); } ImageView imgSpeed = (ImageView) rootView.findViewById(R.id.simpleview_imgSpeed); clearColor(imgSpeed); if (locationInfo.hasSpeed()) { setColor(imgSpeed, IconColorIndicator.Good); TextView txtSpeed = (TextView) rootView.findViewById(R.id.simpleview_txtSpeed); txtSpeed.setText(Strings.getSpeedDisplay(getActivity(), locationInfo.getSpeed(), preferenceHelper.shouldDisplayImperialUnits())); } ImageView imgDirection = (ImageView) rootView.findViewById(R.id.simpleview_imgDirection); clearColor(imgDirection); if (locationInfo.hasBearing()) { setColor(imgDirection, IconColorIndicator.Good); imgDirection.setRotation(locationInfo.getBearing()); TextView txtDirection = (TextView) rootView.findViewById(R.id.simpleview_txtDirection); txtDirection.setText( String.valueOf(Math.round(locationInfo.getBearing())) + getString(R.string.degree_symbol)); } TextView txtDuration = (TextView) rootView.findViewById(R.id.simpleview_txtDuration); long startTime = Session.getStartTimeStamp(); long currentTime = System.currentTimeMillis(); txtDuration.setText(Strings.getTimeDisplay(getActivity(), currentTime - startTime)); double distanceValue = Session.getTotalTravelled(); TextView txtPoints = (TextView) rootView.findViewById(R.id.simpleview_txtPoints); TextView txtTravelled = (TextView) rootView.findViewById(R.id.simpleview_txtDistance); txtTravelled.setText(Strings.getDistanceDisplay(getActivity(), distanceValue, preferenceHelper.shouldDisplayImperialUnits())); txtPoints.setText(Session.getNumLegs() + " " + getString(R.string.points)); String providerName = locationInfo.getProvider(); if (!providerName.equalsIgnoreCase(LocationManager.GPS_PROVIDER)) { setSatelliteCount(-1); } } private void clearLocationDisplay() { EditText txtLatitude = (EditText) rootView.findViewById(R.id.simple_lat_text); txtLatitude.setText(""); ImageView imgAccuracy = (ImageView) rootView.findViewById(R.id.simpleview_imgAccuracy); clearColor(imgAccuracy); TextView txtAccuracy = (TextView) rootView.findViewById(R.id.simpleview_txtAccuracy); txtAccuracy.setText(""); txtAccuracy.setTextColor(ContextCompat.getColor(context, android.R.color.black)); ImageView imgAltitude = (ImageView) rootView.findViewById(R.id.simpleview_imgAltitude); clearColor(imgAltitude); TextView txtAltitude = (TextView) rootView.findViewById(R.id.simpleview_txtAltitude); txtAltitude.setText(""); ImageView imgDirection = (ImageView) rootView.findViewById(R.id.simpleview_imgDirection); clearColor(imgDirection); TextView txtDirection = (TextView) rootView.findViewById(R.id.simpleview_txtDirection); txtDirection.setText(""); ImageView imgSpeed = (ImageView) rootView.findViewById(R.id.simpleview_imgSpeed); clearColor(imgSpeed); TextView txtSpeed = (TextView) rootView.findViewById(R.id.simpleview_txtSpeed); txtSpeed.setText(""); TextView txtDuration = (TextView) rootView.findViewById(R.id.simpleview_txtDuration); txtDuration.setText(""); TextView txtPoints = (TextView) rootView.findViewById(R.id.simpleview_txtPoints); TextView txtTravelled = (TextView) rootView.findViewById(R.id.simpleview_txtDistance); txtPoints.setText(""); txtTravelled.setText(""); } public void setSatelliteCount(int count) { ImageView imgSatelliteCount = (ImageView) rootView.findViewById(R.id.simpleview_imgSatelliteCount); TextView txtSatelliteCount = (TextView) rootView.findViewById(R.id.simpleview_txtSatelliteCount); if (count > -1) { setColor(imgSatelliteCount, IconColorIndicator.Good); AlphaAnimation fadeIn = new AlphaAnimation(0.6f, 1.0f); fadeIn.setDuration(1200); fadeIn.setFillAfter(true); txtSatelliteCount.startAnimation(fadeIn); txtSatelliteCount.setText(String.valueOf(count)); } else { clearColor(imgSatelliteCount); txtSatelliteCount.setText(""); } } public void onWaitingForLocation(boolean inProgress) { LOG.debug(inProgress + ""); if (!Session.isStarted()) { actionButton.setProgress(0); setActionButtonStart(); return; } if (inProgress) { actionButton.setProgress(1); setActionButtonStop(); } else { actionButton.setProgress(0); setActionButtonStop(); } } @Override public void onClick(View view) { Toast toast = new Toast(getActivity()); switch (view.getId()) { case R.id.simpleview_imgSatelliteCount: toast = getToast(R.string.txt_satellites); break; case R.id.simpleview_imgAccuracy: toast = getToast(R.string.txt_accuracy); break; case R.id.simpleview_imgAltitude: toast = getToast(R.string.txt_altitude); break; case R.id.simpleview_imgDirection: toast = getToast(R.string.txt_direction); break; case R.id.simpleview_imgDuration: toast = getToast(R.string.txt_travel_duration); break; case R.id.simpleview_imgSpeed: toast = getToast(R.string.txt_speed); break; case R.id.simpleview_distance: toast = getToast(R.string.txt_travel_distance); break; case R.id.simpleview_points: toast = getToast(R.string.txt_number_of_points); break; case R.id.simpleview_imgLink: toast = getToast(preferenceHelper.getCustomLoggingUrl()); break; } int location[] = new int[2]; view.getLocationOnScreen(location); toast.setGravity(Gravity.TOP | Gravity.LEFT, location[0], location[1]); toast.show(); } private Toast getToast(String message) { return Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT); } private Toast getToast(int stringResourceId) { return getToast(getString(stringResourceId).replace(":", "")); } }