ru.kaefik.isaifutdinov.an_weather_widget.utils.Utils.java Source code

Java tutorial

Introduction

Here is the source code for ru.kaefik.isaifutdinov.an_weather_widget.utils.Utils.java

Source

/*
  * Copyright (C) 2016 Ilnur Saifutdinov ( )
  * e-mail: ilnursoft@gmail.com
  * MIT License
  * https://opensource.org/licenses/mit-license.php
*/

package ru.kaefik.isaifutdinov.an_weather_widget.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import ru.kaefik.isaifutdinov.an_weather_widget.ConfigActivity;
import ru.kaefik.isaifutdinov.an_weather_widget.city.ArrayCityModel;

public class Utils {

    static Map mTranslateWeather = new HashMap<String, String>();

    //  ?   strurl
    static public String getHttpRequestFromUrl(String strurl) {
        String resultStr = "";
        try {
            URL url = new URL(strurl);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0");
            InputStream stream = urlConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(stream);
            BufferedReader bufferedReader = new BufferedReader(reader);
            String line;
            resultStr = "";
            while ((line = bufferedReader.readLine()) != null) {
                resultStr += line;
            }
            bufferedReader.close();
            return resultStr;
        } catch (MalformedURLException e) {
            //            e.printStackTrace();
            resultStr = "";
        } catch (IOException e) {
            //            e.printStackTrace();
            resultStr = "";
        }
        return resultStr;
    }

    //    json
    static public String getObjFromJson(String sjosn, String nameParrent, String nameChild) {

        JSONObject parentObject = null;
        JSONObject childObject = null;
        String res = null;
        try {
            parentObject = new JSONObject(sjosn);
            if (nameParrent != null) {
                res = parentObject.get(nameParrent).toString();
                if (nameChild != null) {
                    if (res != null) {
                        childObject = new JSONObject(res);
                        res = childObject.get(nameChild).toString();
                    }
                }
            }
        } catch (JSONException e) {
            res = "0";
        }
        return res;
    }

    //?   
    public static String firstUpCaseString(String word) {
        if (word == null || word.isEmpty())
            return "";
        return word.substring(0, 1).toUpperCase() + word.substring(1);
    }

    //-----   ? 

    //  ? ? 
    public static String openFile(String filename, Context context) {
        String res = "";
        try {
            FileInputStream inputStream = context.openFileInput(filename);
            if (inputStream != null) {
                InputStreamReader isr = new InputStreamReader(inputStream);
                BufferedReader reader = new BufferedReader(isr);
                String line;
                StringBuilder builder = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }
                inputStream.close();
                res = builder.toString();
            }
        } catch (Throwable t) {
            // TODO: ?  ??   ,  ?  ?  
        }
        return res;
    }

    //  ? ?? 
    public static void saveFile(String filename, String strText, Context context) {
        try {
            FileOutputStream outputStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
            OutputStreamWriter osw = new OutputStreamWriter(outputStream);
            osw.write(strText);
            osw.close();
        } catch (Throwable t) {
            //TODO: ?  ??   ,  ?  ?  
        }
    }

    //----- END   ? 

    //  ?   (, , ,    )
    public static String windGradus2Rumb(Float gradusWind) {
        String windRumb = "";

        if ((gradusWind < 0) | (gradusWind > 360)) {
            windRumb = "";
        }

        if ((gradusWind >= 0) & (gradusWind <= 45)) {
            gradusWind = Utils.getNearNumber(gradusWind, 0f, 45f);
        }
        if ((gradusWind >= 45) & (gradusWind <= 90)) {
            gradusWind = Utils.getNearNumber(gradusWind, 45f, 90f);
        }
        if ((gradusWind >= 90) & (gradusWind <= 135)) {
            gradusWind = Utils.getNearNumber(gradusWind, 90f, 135f);
        }
        if ((gradusWind >= 135) & (gradusWind <= 180)) {
            gradusWind = Utils.getNearNumber(gradusWind, 135f, 180f);
        }
        if ((gradusWind >= 180) & (gradusWind <= 225)) {
            gradusWind = Utils.getNearNumber(gradusWind, 180f, 225f);
        }
        if ((gradusWind >= 225) & (gradusWind <= 270)) {
            gradusWind = Utils.getNearNumber(gradusWind, 225f, 270f);
        }
        if ((gradusWind >= 270) & (gradusWind <= 315)) {
            gradusWind = Utils.getNearNumber(gradusWind, 270f, 315f);
        }
        if ((gradusWind >= 315) & (gradusWind <= 360)) {
            gradusWind = Utils.getNearNumber(gradusWind, 315f, 360f);
        }

        if ((gradusWind == 0f) | (gradusWind == 360f)) {
            windRumb = "";
        }
        if (gradusWind == 45f) {
            windRumb = "";
        }
        if (gradusWind == 90f) {
            windRumb = "";
        }
        if (gradusWind == 135f) {
            windRumb = "";
        }
        if (gradusWind == 180f) {
            windRumb = "";
        }
        if (gradusWind == 225f) {
            windRumb = "";
        }
        if (gradusWind == 270f) {
            windRumb = "";
        }
        if (gradusWind == 315f) {
            windRumb = "";
        }
        return windRumb;

    }

    //     ? ch1  ch2 ? ch
    public static Float getNearNumber(Float ch, Float ch1, Float ch2) {
        Float d = Math.abs(ch2 - ch1) / 2;
        if (ch <= ch1 + d) {
            return ch1;
        } else {
            return ch2;
        }

    }

    // ?  ??  ? ?  ??
    // (http://openweathermap.org/weather-conditions)
    public static void createTranslateWeatherDescription() {
        mTranslateWeather.put("clear sky", "??");
        mTranslateWeather.put("few clouds", "");
        mTranslateWeather.put("scattered clouds", "");
        mTranslateWeather.put("broken clouds", "? ? ?");
        mTranslateWeather.put("shower rain", "");
        mTranslateWeather.put("rain", "");
        mTranslateWeather.put("thunderstorm", "");
        mTranslateWeather.put("snow", "?");
        mTranslateWeather.put("mist", "");
        //----Rain
        mTranslateWeather.put("light rain", " ");
        mTranslateWeather.put("moderate rain", " ");
        mTranslateWeather.put("heavy intensity rain", "? ");
        mTranslateWeather.put("very heavy rain", " ? ");
        mTranslateWeather.put("extreme rain", "?? ");
        mTranslateWeather.put("freezing rain", "? ");
        mTranslateWeather.put("light intensity shower rain", " ");
        mTranslateWeather.put("shower rain", "");
        mTranslateWeather.put("heavy intensity shower rain", "? ");
        mTranslateWeather.put("ragged shower rain", " ?");
        //----Snow
        mTranslateWeather.put("light snow ", " ?");
        mTranslateWeather.put("heavy snow", "?");
        mTranslateWeather.put("sleet", " ? ?");
        mTranslateWeather.put("shower sleet", " ? ?");
        mTranslateWeather.put("light rain and snow", "  ? ?");
        mTranslateWeather.put("rain and snow", " ? ?");
        mTranslateWeather.put("light shower snow", " ?");
        mTranslateWeather.put("shower snow", "?");
        mTranslateWeather.put("heavy shower snow", "? ?");
        //----Atmosphere
        mTranslateWeather.put("smoke", "?");
        mTranslateWeather.put("haze", "");
        mTranslateWeather.put("sand, dust whirls", "?");
        mTranslateWeather.put("fog", "");
        mTranslateWeather.put("sand", "?");
        mTranslateWeather.put("dust", "");
        mTranslateWeather.put("volcanic ash", "? ");
        mTranslateWeather.put("squalls", " ");
        mTranslateWeather.put("tornado", "");
        //----Clouds
        mTranslateWeather.put("overcast clouds", "?");
        //----Thunderstorm
        mTranslateWeather.put("thunderstorm with light rain", "  ? ");
        mTranslateWeather.put("thunderstorm with rain", " ? ");
        mTranslateWeather.put("thunderstorm with heavy rain", "?  ? ");
        mTranslateWeather.put("light thunderstorm", "? ");
        mTranslateWeather.put("thunderstorm", "");
        mTranslateWeather.put("heavy thunderstorm", "?? ");
        mTranslateWeather.put("ragged thunderstorm", "?? ");
        mTranslateWeather.put("thunderstorm with light drizzle", " ?  ?");
        mTranslateWeather.put("thunderstorm with drizzle", "  ? ");
        mTranslateWeather.put("thunderstorm with heavy drizzle", " ? ? ");
        //----Drizzle
        mTranslateWeather.put("light intensity drizzle", "? ?");
        mTranslateWeather.put("drizzle", "?");
        mTranslateWeather.put("heavy intensity drizzle", "?? ?");
        mTranslateWeather.put("light intensity drizzle rain", " ? ?");
        mTranslateWeather.put("drizzle rain", "? ? ");
        mTranslateWeather.put("heavy intensity drizzle rain", "?  ? ?");
        mTranslateWeather.put("shower rain and drizzle", " ? ?");
        mTranslateWeather.put("heavy shower rain and drizzle", "?  ? ?");
        mTranslateWeather.put("shower drizzle", "? ?");
    }

    //  ??  ? ?  ??
    // (http://openweathermap.org/weather-conditions)
    public static String translateWeatherDescription(String descWeather) {
        //--------
        if (mTranslateWeather.get(descWeather) != null) {
            return mTranslateWeather.get(descWeather).toString();
        } else {
            return "?";
        }
    }

    //    ?  .  true - ??  
    public static boolean isConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni != null && ni.isConnected()) {
            return true;
        }
        return false;
    }

    //  ?? ?    
    public static ArrayList<String> getLikeNameCity(String searchNameCity) {
        String APPID = "76d6de6e46c704733f12c8738307dbb5";
        Log.i(ConfigActivity.TAG_SERVICE, " CityModel getLikeNameCity -> start ");
        String url = "http://api.openweathermap.org/data/2.5/find?q=" + searchNameCity + "&type=like&APPID="
                + APPID;
        ArrayList<String> result = new ArrayList<String>();
        String res = Utils.getHttpRequestFromUrl(url);

        Gson gson = new Gson();
        if (res == null) {
            // TODO:      
            System.out.println("   ");
        } else {
            Log.i(ConfigActivity.TAG_SERVICE, " CityModel getLikeNameCity -> res " + res);
            ArrayCityModel cc = gson.fromJson(res, ArrayCityModel.class);
            System.out.println(cc.getCount());
            for (int i = 0; i < cc.getCount(); i++) {
                result.add(cc.getList(i).getName());
            }
        }
        Log.i(ConfigActivity.TAG_SERVICE, " CityModel getLikeNameCity ->  " + result.toString());
        return result;
    }

}