Android Open Source - cirrus Sample Activity






From Project

Back to project page cirrus.

License

The source code is released under:

Apache License

If you think the Android project cirrus 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

package com.inktomi.cirrus;
//  w w  w . j av a  2s  .  c om
import android.app.Dialog;
import android.content.Intent;
import android.content.IntentSender;
import android.location.Location;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationClient;
import com.inktomi.cirrus.forecast.WeatherResponse;
import com.inktomi.cirrus.forecast.Data;
import com.inktomi.cirrus.forecast.Parameters;
import com.inktomi.cirrus.forecast.TemperatureValue;

import java.util.Date;
import java.util.List;

/**
 * Retrieves the user's location, and then gets the current conditions and forecast for that location
 * from the Weather.gov National Digital Forecast service.
 *
 * For example:
 * http://graphical.weather.gov/xml/sample_products/browser_interface/ndfdXMLclient.php?lat=36.0397&lon=-114.9811&product=time-series&begin=2013-06-13T00:00:00&end=2013-06-14T10:00:00
 */
public class SampleActivity extends FragmentActivity implements GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener {

    /*
     * Define a request code to send to Google Play services
     * This code is returned in Activity.onActivityResult
     */
    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;

    /*
     * Define a location client that will get location updates for us to use.
     */
    private LocationClient mLocationClient;

    /*
     * Define the weather client which we'll use to talk to the National Digital Forecast Database
     */
    private CirrusClient mCirrusClient;

    private TextView mApparentTemperature;
    private TextView mHourlyMaxTemperature;
    private NetworkImageView mConditionsIcon;

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

        /*
         * Create a new location client, using the enclosing class to
         * handle callbacks.
         */
        mLocationClient = new LocationClient(this, this, this);

        /*
         * Wire up our weather client.
         */
        mCirrusClient = new CirrusClient(this);

        // Get handles to the views.
        mApparentTemperature = (TextView) findViewById(R.id.apparent_temp);
        mHourlyMaxTemperature = (TextView) findViewById(R.id.hourly_max_temp);
        mConditionsIcon = (NetworkImageView) findViewById(R.id.conditions_icon);
    }

    /*
     * Called when the Activity becomes visible.
     */
    @Override
    protected void onStart() {
        super.onStart();

        // Connect the client.
        mLocationClient.connect();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.sample, menu);
        return true;
    }

    /*
     * Called when the Activity is no longer visible.
     */
    @Override
    protected void onStop() {
        // Disconnecting the client invalidates it.
        mLocationClient.disconnect();

        super.onStop();
    }

    /*
     * Handle results returned to the FragmentActivity
     * by Google Play services
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Decide what to do based on the original request code
        switch (requestCode) {
            case CONNECTION_FAILURE_RESOLUTION_REQUEST :
            /*
             * If the result code is Activity.RESULT_OK, try
             * to connect again
             */
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        mLocationClient.connect();
                        break;
                }
        }
    }

    public Location getLocation() {

        // If Google Play Services is available
        Location currentLocation = null;
        if (servicesConnected()) {

            // Get the current location
            currentLocation = mLocationClient.getLastLocation();
        }

        return currentLocation;
    }

    private boolean servicesConnected() {
        // Check that Google Play services is available
        final int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

        // If Google Play services is available
        if (ConnectionResult.SUCCESS == resultCode) {
            // In debug mode, log the status
            Log.d("Location Updates", "Google Play services is available.");
            // Continue
            return true;
        } else {
            // Google Play services was not available for some reason
            // Get the error dialog from Google Play services
            Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(
                    resultCode,
                    this,
                    CONNECTION_FAILURE_RESOLUTION_REQUEST);

            // If Google Play services can provide an error dialog
            if (errorDialog != null) {
                // Create a new DialogFragment for the error dialog
                ErrorDialogFragment errorFragment = new ErrorDialogFragment();

                // Set the dialog in the DialogFragment
                errorFragment.setDialog(errorDialog);

                // Show the error dialog in the DialogFragment
                errorFragment.show(getSupportFragmentManager(), "Location Updates");
            }
        }

        return false;
    }

    @Override
    public void onConnected(Bundle bundle) {
        // Display the connection status
        Toast.makeText(this, "Connected to Location Service", Toast.LENGTH_SHORT).show();

        Toast.makeText(this, "Location is " + getLocation().getLatitude() + ", " + getLocation().getLongitude(), Toast.LENGTH_LONG).show();

        mCirrusClient.getWeatherForecast(getLocation().getLatitude(), getLocation().getLongitude(),
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(SampleActivity.this, "Failed to get a forecast!", Toast.LENGTH_SHORT).show();
                    }
                },
                new Response.Listener<WeatherResponse>() {
                    @Override
                    public void onResponse(WeatherResponse response) {
                        Toast.makeText(SampleActivity.this, "Got a forecast!", Toast.LENGTH_SHORT).show();

                        setResponse(response);
                    }
                }
        );
    }

    private void setResponse(WeatherResponse response){
        Data responseData = null;
        if (null != response.data && !response.data.isEmpty()) {
            responseData = response.data.get(0);
        }

        Parameters parameters = null;
        if (null != responseData && null != responseData.parameters && !responseData.parameters.isEmpty() ) {
            parameters = responseData.parameters.get(0);
        }

        // Set the current temperature
        TemperatureValue hourlyTemp = WeatherUtils.getForecastMaximumTemperature(response, new Date());
        mHourlyMaxTemperature.setText(getString(R.string.hourly_max_temp, hourlyTemp.value));

        if( null != parameters ){
            setWeatherConditionIcon(parameters.conditionsIcon);
        } else {
            Toast.makeText(SampleActivity.this, "Got a forecast, but couldn't use it.", Toast.LENGTH_SHORT).show();
        }
    }

    private void setWeatherConditionIcon(List<Parameters.ConditionsIcon> conditionsIcon){
        if( null != conditionsIcon && !conditionsIcon.isEmpty() ){
            // Take the first icon, since our sample date range is small.
            ImageLoader imageLoader = new ImageLoader(Volley.newRequestQueue(this), new BitmapLruCache(10485760)); // 10 meg cache.

            if( !conditionsIcon.get(0).iconLink.isEmpty() ){
                mConditionsIcon.setImageUrl(conditionsIcon.get(0).iconLink.get(0), imageLoader);
            }
        }
    }

    @Override
    public void onDisconnected() {
        // Display the connection status
        Toast.makeText(this, "Disconnected from Location Service", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        /*
         * Google Play services can resolve some errors it detects.
         * If the error has a resolution, try sending an Intent to
         * start a Google Play services activity that can resolve
         * error.
         */
        if (connectionResult.hasResolution()) {
            try {
                // Start an Activity that tries to resolve the error
                connectionResult.startResolutionForResult(
                        this,
                        CONNECTION_FAILURE_RESOLUTION_REQUEST);
                /*
                 * Thrown if Google Play services canceled the original
                 * PendingIntent
                 */
            } catch (IntentSender.SendIntentException e) {
                // Log the error
                Log.e("SampleActivity", "Failed to start resolution activity for play services error", e);
            }
        } else {
            /*
             * If no resolution is available, display a dialog to the
             * user with the error.
             */

            // Google Play services was not available for some reason
            // Get the error dialog from Google Play services
            Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(
                    connectionResult.getErrorCode(),
                    this,
                    CONNECTION_FAILURE_RESOLUTION_REQUEST);

            // If Google Play services can provide an error dialog
            if (errorDialog != null) {
                // Create a new DialogFragment for the error dialog
                ErrorDialogFragment errorFragment = new ErrorDialogFragment();

                // Set the dialog in the DialogFragment
                errorFragment.setDialog(errorDialog);

                // Show the error dialog in the DialogFragment
                errorFragment.show(getSupportFragmentManager(), "Play Store Connection");
            }
        }
    }

    // Define a DialogFragment that displays the error dialog
    public static class ErrorDialogFragment extends DialogFragment {
        // Global field to contain the error dialog
        private Dialog mDialog;
        // Default constructor. Sets the dialog field to null
        public ErrorDialogFragment() {
            super();
            mDialog = null;
        }
        // Set the dialog to display
        public void setDialog(Dialog dialog) {
            mDialog = dialog;
        }
        // Return a Dialog to the DialogFragment.
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            return mDialog;
        }
    }
    
}




Java Source Code List

com.android.volley.AuthFailureError.java
com.android.volley.CacheDispatcher.java
com.android.volley.Cache.java
com.android.volley.DefaultRetryPolicy.java
com.android.volley.ExecutorDelivery.java
com.android.volley.NetworkDispatcher.java
com.android.volley.NetworkError.java
com.android.volley.NetworkResponse.java
com.android.volley.Network.java
com.android.volley.NoConnectionError.java
com.android.volley.ParseError.java
com.android.volley.RequestQueue.java
com.android.volley.Request.java
com.android.volley.ResponseDelivery.java
com.android.volley.Response.java
com.android.volley.RetryPolicy.java
com.android.volley.ServerError.java
com.android.volley.TimeoutError.java
com.android.volley.VolleyError.java
com.android.volley.VolleyLog.java
com.android.volley.toolbox.AndroidAuthenticator.java
com.android.volley.toolbox.Authenticator.java
com.android.volley.toolbox.BasicNetwork.java
com.android.volley.toolbox.ByteArrayPool.java
com.android.volley.toolbox.ClearCacheRequest.java
com.android.volley.toolbox.DiskBasedCache.java
com.android.volley.toolbox.HttpClientStack.java
com.android.volley.toolbox.HttpHeaderParser.java
com.android.volley.toolbox.HttpStack.java
com.android.volley.toolbox.HurlStack.java
com.android.volley.toolbox.ImageLoader.java
com.android.volley.toolbox.ImageRequest.java
com.android.volley.toolbox.JsonArrayRequest.java
com.android.volley.toolbox.JsonObjectRequest.java
com.android.volley.toolbox.JsonRequest.java
com.android.volley.toolbox.NetworkImageView.java
com.android.volley.toolbox.NoCache.java
com.android.volley.toolbox.PoolingByteArrayOutputStream.java
com.android.volley.toolbox.RequestFuture.java
com.android.volley.toolbox.StringRequest.java
com.android.volley.toolbox.Volley.java
com.inktomi.cirrus.BitmapLruCache.java
com.inktomi.cirrus.CirrusClient.java
com.inktomi.cirrus.DateTransform.java
com.inktomi.cirrus.EnumTransform.java
com.inktomi.cirrus.NDFDRequest.java
com.inktomi.cirrus.SampleActivity.java
com.inktomi.cirrus.URLStrings.java
com.inktomi.cirrus.WeatherUtils.java
com.inktomi.cirrus.forecast.Anomaly.java
com.inktomi.cirrus.forecast.Area.java
com.inktomi.cirrus.forecast.Categories.java
com.inktomi.cirrus.forecast.Category.java
com.inktomi.cirrus.forecast.Circle.java
com.inktomi.cirrus.forecast.City.java
com.inktomi.cirrus.forecast.ConciseName.java
com.inktomi.cirrus.forecast.CreationDate.java
com.inktomi.cirrus.forecast.DataSource.java
com.inktomi.cirrus.forecast.Data.java
com.inktomi.cirrus.forecast.Datum.java
com.inktomi.cirrus.forecast.DecimalVal.java
com.inktomi.cirrus.forecast.Error.java
com.inktomi.cirrus.forecast.Field.java
com.inktomi.cirrus.forecast.Head.java
com.inktomi.cirrus.forecast.HeightUnits.java
com.inktomi.cirrus.forecast.Height.java
com.inktomi.cirrus.forecast.Icon.java
com.inktomi.cirrus.forecast.Layer.java
com.inktomi.cirrus.forecast.Level.java
com.inktomi.cirrus.forecast.LikelihoodUnits.java
com.inktomi.cirrus.forecast.Location.java
com.inktomi.cirrus.forecast.MoreWeatherInformation.java
com.inktomi.cirrus.forecast.NwsZone.java
com.inktomi.cirrus.forecast.OperationalMode.java
com.inktomi.cirrus.forecast.Parameters.java
com.inktomi.cirrus.forecast.PercentageValue.java
com.inktomi.cirrus.forecast.Point.java
com.inktomi.cirrus.forecast.Probability.java
com.inktomi.cirrus.forecast.Product.java
com.inktomi.cirrus.forecast.ProductionCenter.java
com.inktomi.cirrus.forecast.Radius.java
com.inktomi.cirrus.forecast.Rectangle.java
com.inktomi.cirrus.forecast.Source.java
com.inktomi.cirrus.forecast.SrsName.java
com.inktomi.cirrus.forecast.StartValidTime.java
com.inktomi.cirrus.forecast.TemperatureValue.java
com.inktomi.cirrus.forecast.TimeCoordinate.java
com.inktomi.cirrus.forecast.TimeLayout.java
com.inktomi.cirrus.forecast.Uncertainty.java
com.inktomi.cirrus.forecast.ValueForRange.java
com.inktomi.cirrus.forecast.ValueList.java
com.inktomi.cirrus.forecast.WeatherResponse.java
com.inktomi.cirrus.forecast.WindDirectionValue.java
com.inktomi.cirrus.forecast.WindspeedValue.java