Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d84f0b0a authored by Aayush Gupta's avatar Aayush Gupta
Browse files

OpenWeatherMapWeatherProvider: Simplfy logging implementation

parent aa910bf8
Loading
Loading
Loading
Loading
+13 −11
Original line number Diff line number Diff line
@@ -21,9 +21,9 @@ import android.location.Location;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.util.Log;

import org.lineageos.openweathermapprovider.openweathermap.OpenWeatherMapService;
import org.lineageos.openweathermapprovider.utils.Logging;

import java.util.HashMap;
import java.util.List;
@@ -40,6 +40,8 @@ import lineageos.weatherservice.WeatherProviderService;
public class OpenWeatherMapProviderService extends WeatherProviderService
        implements SharedPreferences.OnSharedPreferenceChangeListener {

    private static final String TAG = OpenWeatherMapProviderService.class.getSimpleName();

    private static final String API_KEY = "api_key";
    private static final String API_KEY_VERIFIED_STATE = "api_key_verified_state";

@@ -83,7 +85,7 @@ public class OpenWeatherMapProviderService extends WeatherProviderService
    protected void onRequestSubmitted(ServiceRequest request) {
        RequestInfo requestInfo = request.getRequestInfo();
        int requestType = requestInfo.getRequestType();
        Logging.logd("Received request type " + requestType);
        Log.d(TAG, "Received request type " + requestType);

        if (((requestType == RequestInfo.TYPE_WEATHER_BY_GEO_LOCATION_REQ &&
                isSameGeoLocation(requestInfo.getLocation(), mLastLocation))
@@ -136,7 +138,7 @@ public class OpenWeatherMapProviderService extends WeatherProviderService
                }
                return;
            default:
                Logging.logw("Received unknown request type "
                Log.w(TAG, "Received unknown request type "
                        + request.getRequestInfo().getRequestType());
                break;
        }
@@ -145,7 +147,7 @@ public class OpenWeatherMapProviderService extends WeatherProviderService
    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals(API_KEY)) {
            Logging.logd("API key has changed");
            Log.d(TAG, "API key has changed");
            final String mApiKey = sharedPreferences.getString(key, getString(R.string.default_key));
            mOpenWeatherMapService.setApiKey(mApiKey);
        }
@@ -163,13 +165,13 @@ public class OpenWeatherMapProviderService extends WeatherProviderService
    private boolean isSameGeoLocation(Location newLocation, Location oldLocation) {
        if (newLocation == null || oldLocation == null) return false;
        float distance = newLocation.distanceTo(oldLocation);
        Logging.logd("Distance between locations " + distance);
        Log.d(TAG, "Distance between locations " + distance);
        return (distance < LOCATION_DISTANCE_METERS_THRESHOLD);
    }

    private boolean wasRequestSubmittedTooSoon() {
        final long now = SystemClock.elapsedRealtime();
        Logging.logd("Now " + now + " last request " + mLastRequestTimestamp);
        Log.d(TAG, "Now " + now + " last request " + mLastRequestTimestamp);
        return (mLastRequestTimestamp + REQUEST_THRESHOLD > now);
    }

@@ -201,7 +203,7 @@ public class OpenWeatherMapProviderService extends WeatherProviderService
                }
            } else {
                // We don't know how to handle any other type of request
                Logging.logw("Received unknown request type "+ requestType);
                Log.w(TAG, "Received unknown request type "+ requestType);
                return null;
            }
        }
@@ -209,10 +211,10 @@ public class OpenWeatherMapProviderService extends WeatherProviderService
        @Override
        protected void onPostExecute(WeatherInfo weatherInfo) {
            if (weatherInfo == null) {
                Logging.logd("Received null weather info, failing request");
                Log.d(TAG, "Received null weather info, failing request");
                mRequest.fail();
            } else {
                Logging.logd(weatherInfo.toString());
                Log.d(TAG, weatherInfo.toString());
                ServiceRequestResult result = new ServiceRequestResult.Builder(weatherInfo).build();
                mRequest.complete(result);
                if (mRequest.getRequestInfo().getRequestType()
@@ -241,7 +243,7 @@ public class OpenWeatherMapProviderService extends WeatherProviderService
        protected List<WeatherLocation> doInBackground(Void... params) {
            RequestInfo requestInfo = mRequest.getRequestInfo();
            if (requestInfo.getRequestType() != RequestInfo.TYPE_LOOKUP_CITY_NAME_REQ) {
                Logging.logw("Received unsupported request type " + requestInfo.getRequestType());
                Log.w(TAG, "Received unsupported request type " + requestInfo.getRequestType());
                return null;
            }
            try {
@@ -256,7 +258,7 @@ public class OpenWeatherMapProviderService extends WeatherProviderService
        protected void onPostExecute(List<WeatherLocation> locations) {
            if (locations != null) {
                for (WeatherLocation location : locations) {
                    Logging.logd(location.toString());
                    Log.d(TAG, location.toString());
                }
                ServiceRequestResult request = new ServiceRequestResult.Builder(locations).build();
                mRequest.complete(request);
+12 −11
Original line number Diff line number Diff line
@@ -20,8 +20,7 @@ package org.lineageos.openweathermapprovider.openweathermap;
import android.content.Context;
import android.location.Location;
import android.text.TextUtils;

import org.lineageos.openweathermapprovider.utils.Logging;
import android.util.Log;

import java.io.IOException;
import java.util.ArrayList;
@@ -44,6 +43,8 @@ import retrofit2.converter.gson.GsonConverterFactory;

public class OpenWeatherMapService {

    private static final String TAG = OpenWeatherMapService.class.getSimpleName();

    private static final int FORECAST_ITEMS_PER_DAY = 8;

    private static final double MPS_TO_KPH = 3.6;
@@ -89,7 +90,7 @@ public class OpenWeatherMapService {
                units, language, mApiKey);
        Response<CurrentWeatherResponse> currentWeatherResponse;
        try {
            Logging.logd(weatherResponseCall.request().toString());
            Log.d(TAG, weatherResponseCall.request().toString());
            currentWeatherResponse = weatherResponseCall.execute();
        } catch (IOException e) {
            //An error occurred while talking to the server
@@ -104,12 +105,12 @@ public class OpenWeatherMapService {
                    units, language, mApiKey);
            ForecastResponse forecastResponse = null;
            try {
                Logging.logd(forecastResponseCall.request().toString());
                Log.d(TAG, forecastResponseCall.request().toString());
                Response<ForecastResponse> r = forecastResponseCall.execute();
                if (r.code() == 200) forecastResponse = r.body();
            } catch (IOException e) {
                //this is an error we can live with
                Logging.logd("IOException while requesting forecast " + e);
                Log.d(TAG,"IOException while requesting forecast " + e);
            }
            return processWeatherResponse(currentWeatherResponse.body(), forecastResponse,
                    tempUnit);
@@ -137,11 +138,11 @@ public class OpenWeatherMapService {
                location.getLongitude(), units, language, mApiKey);
        Response<CurrentWeatherResponse> currentWeatherResponse;
        try {
            Logging.logd(weatherResponseCall.request().toString());
            Log.d(TAG, weatherResponseCall.request().toString());
            currentWeatherResponse = weatherResponseCall.execute();
        } catch (IOException e) {
            //An error occurred while talking to the server
            Logging.logd("IOException while requesting weather " + e);
            Log.d(TAG, "IOException while requesting weather " + e);
            return null;
        }

@@ -153,12 +154,12 @@ public class OpenWeatherMapService {
                    location.getLongitude(), units, language, mApiKey);
            ForecastResponse forecastResponse = null;
            try {
                Logging.logd(forecastResponseCall.request().toString());
                Log.d(TAG, forecastResponseCall.request().toString());
                Response<ForecastResponse> r = forecastResponseCall.execute();
                if (r.code() == 200) forecastResponse = r.body();
            } catch (IOException e) {
                //this is an error we can live with
                Logging.logd("IOException while requesting forecast " + e);
                Log.d(TAG, "IOException while requesting forecast " + e);
            }
            return processWeatherResponse(currentWeatherResponse.body(), forecastResponse,
                    tempUnit);
@@ -300,10 +301,10 @@ public class OpenWeatherMapService {

        Response<LookupCityResponse> lookupResponse;
        try {
            Logging.logd(lookupCityCall.request().toString());
            Log.d(TAG, lookupCityCall.request().toString());
            lookupResponse = lookupCityCall.execute();
        } catch (IOException e) {
            Logging.logd("IOException while looking up city name " + e);
            Log.d(TAG, "IOException while looking up city name " + e);
            //Return empty list to prevent NPE
            return new ArrayList<>();
        }
+0 −37
Original line number Diff line number Diff line
/*
 *  Copyright (C) 2016 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.lineageos.openweathermapprovider.utils;

import android.util.Log;

public class Logging {
    private static final boolean DEBUG = false;
    private static final String TAG = "OpenWeatherMapProvider";

    public static final void logd(String log) {
        if (DEBUG) Log.d(TAG, log);
    }

    public static final void logw(String log) {
        if (DEBUG) Log.w(TAG, log);
    }

    public static final void loge(String log) {
        //This is an actual error, so it might be important, no check for debug flag
        Log.e(TAG, log);
    }
}