Android Open Source - getback_gps Main Activity






From Project

Back to project page getback_gps.

License

The source code is released under:

GNU General Public License

If you think the Android project getback_gps listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * Main Activity//from ww w .  j  a v a 2s  . c  o  m
 *
 * Copyright (C) 2012-2015 Dieter Adriaenssens
 *
 * This program 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 3 of the License, or
 * any later version.
 *
 * This program 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package com.github.ruleant.getback_gps
 * @author Dieter Adriaenssens <ruleant@users.sourceforge.net>
 */
package com.github.ruleant.getback_gps;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.ruleant.getback_gps.lib.AriadneLocation;
import com.github.ruleant.getback_gps.lib.CardinalDirection;
import com.github.ruleant.getback_gps.lib.DebugLevel;
import com.github.ruleant.getback_gps.lib.FormatUtils;
import com.github.ruleant.getback_gps.lib.Navigator;

/**
 * Main Activity class.
 *
 * @author Dieter Adriaenssens <ruleant@users.sourceforge.net>
 */
public class MainActivity extends AbstractGetBackGpsActivity
        implements View.OnClickListener {
    /**
     * Maximum length when displaying destination name (Portrait orientation).
     */
    private static final int DESTINATION_NAME_LENGTH_PORTRAIT = 23;

    /**
     * Maximum length when displaying destination name (Landscape orientation).
     */
    private static final int DESTINATION_NAME_LENGTH_LANDSCAPE = 75;

    /**
     * String to append to shortened string, to indicate it was shortened.
     */
    private static final String SHORTENER = "(...)";


    @Override
    protected final void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // add onClicklistener to Destination name text view
        TextView tvDestinationName
                = (TextView) findViewById(R.id.textView_toDestName);
        tvDestinationName.setOnClickListener(this);
    }

    @Override
    public final boolean onCreateOptionsMenu(final Menu menu) {
        boolean superResult = super.onCreateOptionsMenu(menu);

        DebugLevel debug = new DebugLevel(this);

        // don't add details button when debugging is disabled
        if (debug.checkDebugLevel(DebugLevel.DEBUG_LEVEL_LOW)) {
            // Inflate the menu;
            // this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
        }

        return superResult;
    }

    @Override
    public final boolean onOptionsItemSelected(final MenuItem item) {
        // One of the group items (using the onClick attribute) was clicked
        // The item parameter passed here indicates which item it is
        // All other menu item clicks are handled by onOptionsItemSelected()
        if (item.getItemId() == R.id.menu_details) {
            displayDetails(item);
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public boolean onPrepareOptionsMenu(final Menu menu) {
        MenuItem miDetails = menu.findItem(R.id.menu_details);
        DebugLevel debug = new DebugLevel(this);

        if (miDetails != null) {
            // hide details button when debugging is disabled
            miDetails.setVisible(
                    debug.checkDebugLevel(DebugLevel.DEBUG_LEVEL_LOW));
        }

        return super.onPrepareOptionsMenu(menu);
    }

    /**
     * Called when the user clicks the About menu item.
     *
     * @param item MenuItem object that was clicked
     */
    final void displayDetails(final MenuItem item) {
        Intent intent = new Intent(this, DetailsActivity.class);
        startActivity(intent);
    }

    @Override
    protected final boolean refreshDisplay() {
        if (!super.refreshDisplay()) {
            return false;
        }

        // refresh views with "current" info
        // don't display value if it is inaccurate
        refreshCurrentViews(false);

        // only refresh items if activity is bound to service
        // connection state is checked in getNavigator
        Navigator navigator = getNavigator();

        if (navigator == null) {
            return false;
        }

        Resources res = getResources();

        // get Destination from service
        AriadneLocation destination = navigator.getDestination();

        // Refresh Directions to destination
        NavigationView nvToDestination
                = (NavigationView) findViewById(R.id.navigationView_ToDest);
        TextView tvToDestinationName
                = (TextView) findViewById(R.id.textView_toDestName);
        TextView tvToDestinationDistance
                = (TextView) findViewById(R.id.textView_toDestDist);
        TextView tvToDestinationDirection
                = (TextView) findViewById(R.id.textView_toDestDir);

        LinearLayout sectionToDestination
                = (LinearLayout) findViewById(R.id.section_toDestination);
        TextView tvToDestinationMessage
                = (TextView) findViewById(R.id.textView_toDest_Message);

        String toDestinationNameText = res.getString(R.string.notset);
        String toDestinationDistanceText = res.getString(R.string.unknown);
        String toDestinationDirectionText = res.getString(R.string.unknown);
        String toDestinationMessage = res.getString(R.string.unknown);
        NavigationView.Mode nvNavigationMode = NavigationView.Mode.Disabled;
        NavigationView.Mode nvOrientationMode = NavigationView.Mode.Disabled;
        Boolean displayToDest = false;

        if (destination == null) {
            toDestinationMessage
                    = res.getString(R.string.no_destination);
        } else if (navigator.isDestinationReached()) {
            toDestinationMessage
                    = res.getString(R.string.destination_reached);
        } else {
            displayToDest = true;

            // Set destination name
            toDestinationNameText = navigator.getDestination().getName();

            // if name is not set, use 'location name'
            if (toDestinationNameText == null
                    || toDestinationDirectionText.length() == 0) {
                toDestinationNameText = res.getString(R.string.location_name);
            }

            // set maxLength depending on screen orientation
            int maxLength;
            if (res.getConfiguration().orientation
                    == Configuration.ORIENTATION_PORTRAIT) {
                maxLength = DESTINATION_NAME_LENGTH_PORTRAIT;
            } else {
                maxLength = DESTINATION_NAME_LENGTH_LANDSCAPE;
            }

            // shorten long names
            if (toDestinationNameText.length() > maxLength) {
                int lastCharPosition = maxLength - SHORTENER.length();
                toDestinationNameText
                        = toDestinationNameText.subSequence(0, lastCharPosition)
                            .toString().trim()
                            + SHORTENER;
            }

            if (navigator.isLocationAccurate()) {
                // Set distance to destination
                toDestinationDistanceText
                        = FormatUtils.formatDist(navigator.getDistance());

                // Set direction to destination
                CardinalDirection cd = new CardinalDirection(
                        this,
                        FormatUtils.normalizeAngle(
                                navigator.getAbsoluteDirection()));
                toDestinationDirectionText = cd.format();

                // if bearing is accurate, display relative direction
                // if not, display absolute direction
                if (navigator.isBearingAccurate()) {
                    nvToDestination.setDirection(
                            navigator.getRelativeDirection());
                    nvNavigationMode = NavigationView.Mode.Accurate;
                } else {
                    nvToDestination.setDirection(
                            navigator.getAbsoluteDirection());
                    nvNavigationMode = NavigationView.Mode.Inaccurate;
                }
            }
        }

        // if orientation is accurate, display compass rose
        if (navigator.isBearingAccurate()) {
            nvToDestination.setAzimuth(
                    navigator.getCurrentBearing());
            nvOrientationMode = NavigationView.Mode.Accurate;
        }

        if (displayToDest) {
            // show 'to Destination' info, hide message
            sectionToDestination.setVisibility(LinearLayout.VISIBLE);
            tvToDestinationMessage.setVisibility(LinearLayout.INVISIBLE);

            // update views
            tvToDestinationName.setText(toDestinationNameText);
            tvToDestinationDistance.setText(toDestinationDistanceText);
            tvToDestinationDirection.setText(toDestinationDirectionText);
        } else {
            // hide 'to Destination' info, show message
            sectionToDestination.setVisibility(LinearLayout.INVISIBLE);
            tvToDestinationMessage.setVisibility(LinearLayout.VISIBLE);

            // update views
            tvToDestinationMessage.setText(toDestinationMessage);
        }

        // update views
        nvToDestination.setNavigationMode(nvNavigationMode);
        nvToDestination.setOrientationMode(nvOrientationMode);
        nvToDestination.invalidate();

        return true;
    }

    /**
     * Implement the OnClickListener callback for destination name textView.
     *
     * @param view View object that was clicked
     */
    public final void onClick(final View view) {
        if (view.getId() == R.id.textView_toDestName) {
            renameDestination();
        }
    }
}




Java Source Code List

com.github.ruleant.getback_gps.AboutActivity.java
com.github.ruleant.getback_gps.AbstractGetBackGpsActivity.java
com.github.ruleant.getback_gps.DetailsActivity.java
com.github.ruleant.getback_gps.LocationService.java
com.github.ruleant.getback_gps.MainActivity.java
com.github.ruleant.getback_gps.NavigationView.java
com.github.ruleant.getback_gps.SettingsActivity.java
com.github.ruleant.getback_gps.lib.AbstractGeoCoordinate.java
com.github.ruleant.getback_gps.lib.AriadneLocation.java
com.github.ruleant.getback_gps.lib.CardinalDirection.java
com.github.ruleant.getback_gps.lib.CircularAverage.java
com.github.ruleant.getback_gps.lib.CoordinateConverterInterface.java
com.github.ruleant.getback_gps.lib.CoordinateRotation.java
com.github.ruleant.getback_gps.lib.Coordinate.java
com.github.ruleant.getback_gps.lib.Coordinates.java
com.github.ruleant.getback_gps.lib.DebugLevel.java
com.github.ruleant.getback_gps.lib.FormatUtils.java
com.github.ruleant.getback_gps.lib.Latitude.java
com.github.ruleant.getback_gps.lib.Longitude.java
com.github.ruleant.getback_gps.lib.LowPassFilter.java
com.github.ruleant.getback_gps.lib.Navigator.java
com.github.ruleant.getback_gps.lib.SensorOrientation.java
com.github.ruleant.getback_gps.lib.StoredDestination.java
com.github.ruleant.getback_gps.lib.StoredLocation.java
com.github.ruleant.getback_gps.lib.Tools.java
com.github.ruleant.getback_gps.lib.package-info.java
com.github.ruleant.getback_gps.package-info.java
com.github.ruleant.unitconversion.UnitConversionInterface.java
com.github.ruleant.unitconversion.package-info.java