Android Open Source - android-appwidget-cirrus Retrieve Client Raw Task






From Project

Back to project page android-appwidget-cirrus.

License

The source code is released under:

MIT License

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

/* Copyright 2014 Wayne D Grant (www.waynedgrant.com)
   Licensed under the MIT License */
//from  w  w  w  . j  ava2 s . co m
package com.waynedgrant.cirrus.clientraw;

import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;

import com.waynedgrant.cirrus.UpdateWidgetService;

public class RetrieveClientRawTask extends AsyncTask<ClientRawRequest, Void, List<ClientRawResponse>>
{
    private static final int MAX_FETCH_CLIENT_RAW_ATTEMPTS = 3;
    private static final long WAIT_BETWEEN_FETCH_CLIENT_RAW_ATTEMPTS_MSECS = 1000;
    
    private UpdateWidgetService updateWidgetService;
    private int connectTimeoutMs;
    private int readTimeoutMs;
    
    public RetrieveClientRawTask(UpdateWidgetService updateWidgetService, int connectTimeoutMs, int readTimeoutMs)
    {
        this.updateWidgetService = updateWidgetService;
        this.connectTimeoutMs = connectTimeoutMs;
        this.readTimeoutMs = readTimeoutMs;
    }

    protected List<ClientRawResponse> doInBackground(ClientRawRequest... requests)
    {
        List<ClientRawResponse> responses = new ArrayList<ClientRawResponse>();
        
        for (ClientRawRequest request : requests)
        {
            int appWidgetId = request.getAppWidgetId();
            ClientRawUrl clientRawUrl = request.getClientRawUrl();
            
            ClientRawResponse response = null;
            
            if (!isOnline())
            {
                response = new ClientRawResponse(appWidgetId, "not online");
            }
            else
            {
                try
                {
                    ClientRaw clientRaw = whileClientRawEmptyRetryFetch(clientRawUrl, fetchClientRaw(clientRawUrl));
                    response = handleClientRaw(appWidgetId, clientRaw);
                }
                catch (IOException ex)
                {
                    response = handleException(appWidgetId, ex);
                }
            }
            
            responses.add(response);
        }

        return responses;
    }

    private ClientRaw whileClientRawEmptyRetryFetch(ClientRawUrl clientRawUrl, ClientRaw clientRaw) throws IOException
    {
        /* clientraw.txt may have been being written to on the server-side when we read.
           This results in an empty file being returned in which case we try fetching again */
        int fetchClientRawAttempts = 1;
        
        while (clientRaw.isEmpty() && fetchClientRawAttempts <= MAX_FETCH_CLIENT_RAW_ATTEMPTS) 
        {
            waitFor(WAIT_BETWEEN_FETCH_CLIENT_RAW_ATTEMPTS_MSECS);
            clientRaw = fetchClientRaw(clientRawUrl);
            fetchClientRawAttempts++;
        }
        
        return clientRaw;
    }

    private ClientRaw fetchClientRaw(ClientRawUrl clientRawUrl) throws IOException
    {
        ClientRaw clientRaw;
        URLConnection connection = new URL(clientRawUrl.getUrl()).openConnection();
        
        connection.setConnectTimeout(connectTimeoutMs);
        connection.setReadTimeout(readTimeoutMs);
        
        BufferedInputStream reader = null;
        
        try
        {
            reader = new BufferedInputStream(connection.getInputStream());
            clientRaw = ClientRaw.getInstance(reader);
        }
        finally
        {
            if (reader != null)
            {
                reader.close();
            }
        }
        return clientRaw;
    }
    
    private ClientRawResponse handleClientRaw(int appWidgetId, ClientRaw clientRaw) throws IOException
    {
        ClientRawResponse response;
        
        if (clientRaw.isEmpty())
        {
            response = new ClientRawResponse(appWidgetId, "empty clientraw.txt received");
        }            
        else if (!clientRaw.isValid())
        {
            response = new ClientRawResponse(appWidgetId, "invalid clientraw.txt received");
        }
        else
        {
            response = new ClientRawResponse(appWidgetId, clientRaw);
        }
        
        return response;
    }

    private void waitFor(long waitTimeMsecs)
    {
        try
        {
            Thread.sleep(waitTimeMsecs);
        }
        catch (InterruptedException ex)
        {
            // Ignore
        }
    }
    
    private ClientRawResponse handleException(int appWidgetId, IOException exception)
    {
        ClientRawResponse response;
        
        if (exception instanceof SocketTimeoutException)
        {
            response = new ClientRawResponse(appWidgetId, "connection timed out");
        }
        else if (exception instanceof FileNotFoundException)
        {
            response = new ClientRawResponse(appWidgetId, "clientraw.txt not found");
        }
        else if (exception instanceof UnknownHostException)
        {
            response = new ClientRawResponse(appWidgetId, "unknown host");
        }
        else
        {
            response = new ClientRawResponse(appWidgetId, "connection issue");
        }
        
        return response;
    }
    
    private boolean isOnline()
    {
        ConnectivityManager cm = (ConnectivityManager)updateWidgetService.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected())
        {
            return true;
        }
        return false;
    }

    protected void onPostExecute(List<ClientRawResponse> responses)
    {
        updateWidgetService.handleClientRawResponses(responses);
    }
}




Java Source Code List

com.waynedgrant.cirrus.UpdateWidgetService.java
com.waynedgrant.cirrus.WidgetConfigActivity.java
com.waynedgrant.cirrus.WidgetProvider.java
com.waynedgrant.cirrus.clientraw.ClientRawCache.java
com.waynedgrant.cirrus.clientraw.ClientRawRequest.java
com.waynedgrant.cirrus.clientraw.ClientRawResponse.java
com.waynedgrant.cirrus.clientraw.ClientRawUrl.java
com.waynedgrant.cirrus.clientraw.ClientRaw.java
com.waynedgrant.cirrus.clientraw.RetrieveClientRawTask.java
com.waynedgrant.cirrus.measures.Conditions.java
com.waynedgrant.cirrus.measures.Pressure.java
com.waynedgrant.cirrus.measures.Rainfall.java
com.waynedgrant.cirrus.measures.Temperature.java
com.waynedgrant.cirrus.measures.Trend.java
com.waynedgrant.cirrus.measures.WeatherItem.java
com.waynedgrant.cirrus.measures.WindDirection.java
com.waynedgrant.cirrus.measures.WindSpeed.java
com.waynedgrant.cirrus.preferences.Preferences.java
com.waynedgrant.cirrus.presentation.colorizers.TemperatureColorizer.java
com.waynedgrant.cirrus.presentation.colorizers.UvIndexColorizer.java
com.waynedgrant.cirrus.presentation.colorizers.WeatherItemColorizer.java
com.waynedgrant.cirrus.presentation.formatters.ConditionsFormatter.java
com.waynedgrant.cirrus.presentation.formatters.DateFormat.java
com.waynedgrant.cirrus.presentation.formatters.DateFormatter.java
com.waynedgrant.cirrus.presentation.formatters.FormattedWeatherItem.java
com.waynedgrant.cirrus.presentation.formatters.HumidityFormatter.java
com.waynedgrant.cirrus.presentation.formatters.PressureFormatter.java
com.waynedgrant.cirrus.presentation.formatters.RainfallFormatter.java
com.waynedgrant.cirrus.presentation.formatters.RainfallRateFormatter.java
com.waynedgrant.cirrus.presentation.formatters.SolarPercentageFormatter.java
com.waynedgrant.cirrus.presentation.formatters.SolarRadiationFormatter.java
com.waynedgrant.cirrus.presentation.formatters.StringFormatter.java
com.waynedgrant.cirrus.presentation.formatters.TemperatureFormatter.java
com.waynedgrant.cirrus.presentation.formatters.TimeFormat.java
com.waynedgrant.cirrus.presentation.formatters.TimeFormatter.java
com.waynedgrant.cirrus.presentation.formatters.TrendFormatter.java
com.waynedgrant.cirrus.presentation.formatters.UvIndexFormatter.java
com.waynedgrant.cirrus.presentation.formatters.WeatherItemFormatter.java
com.waynedgrant.cirrus.presentation.formatters.WindDirectionFormatter.java
com.waynedgrant.cirrus.presentation.formatters.WindSpeedFormatter.java
com.waynedgrant.cirrus.units.CardinalDirection.java
com.waynedgrant.cirrus.units.PressureUnit.java
com.waynedgrant.cirrus.units.RainfallUnit.java
com.waynedgrant.cirrus.units.TemperatureUnit.java
com.waynedgrant.cirrus.units.WindDirectionUnit.java
com.waynedgrant.cirrus.units.WindSpeedUnit.java
com.waynedgrant.cirrus.update.Timeout.java