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

Commit 9585abad authored by Romain Hunault's avatar Romain Hunault
Browse files

init commit

parent 639a5a15
Loading
Loading
Loading
Loading
(97.2 KiB)

File mode changed from 100755 to 100644.

+0 −25
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import org.asdtm.goodweather.service.CurrentWeatherService;
import org.asdtm.goodweather.utils.Utils;
@@ -104,34 +103,10 @@ public class BaseActivity extends AppCompatActivity {
                        case R.id.nav_menu_current_weather:
                            startActivity(new Intent(BaseActivity.this, MainActivity.class));
                            break;
                        case R.id.nav_menu_graphs:
                            createBackStack(new Intent(BaseActivity.this,
                                                       GraphsActivity.class));
                            break;
                        case R.id.nav_menu_weather_forecast:
                            createBackStack(new Intent(BaseActivity.this,
                                                       WeatherForecastActivity.class));
                            break;
                        case R.id.nav_settings:
                            createBackStack(new Intent(BaseActivity.this,
                                                       SettingsActivity.class));
                            break;
                        case R.id.nav_feedback:
                            Intent sendMessage = new Intent(Intent.ACTION_SEND);
                            sendMessage.setType("message/rfc822");
                            sendMessage.putExtra(Intent.EXTRA_EMAIL, new String[]{
                                    getResources().getString(R.string.feedback_email)});
                            try {
                                startActivity(Intent.createChooser(sendMessage, "Send feedback"));
                            } catch (android.content.ActivityNotFoundException e) {
                                Toast.makeText(BaseActivity.this, "Communication app not found",
                                               Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case R.id.nav_menu_bitcoin_donation:
                            BitcoinDonationDialog dialog = BitcoinDonationDialog.newInstance();
                            dialog.show(getFragmentManager(), "bitcoinDonationDialog");
                            break;
                    }

                    mDrawerLayout.closeDrawer(GravityCompat.START);
+189 −0
Original line number Diff line number Diff line
@@ -29,6 +29,8 @@ import android.support.v4.content.ContextCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
@@ -38,19 +40,32 @@ import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import org.asdtm.goodweather.adapter.WeatherForecastAdapter;
import org.asdtm.goodweather.model.CitySearch;
import org.asdtm.goodweather.model.Weather;
import org.asdtm.goodweather.model.WeatherForecast;
import org.asdtm.goodweather.service.CurrentWeatherService;
import org.asdtm.goodweather.utils.AppPreference;
import org.asdtm.goodweather.utils.Constants;
import org.asdtm.goodweather.utils.LanguageUtil;
import org.asdtm.goodweather.utils.PermissionUtil;
import org.asdtm.goodweather.utils.PreferenceUtil;
import org.asdtm.goodweather.utils.Utils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import static org.asdtm.goodweather.utils.AppPreference.saveLastUpdateTimeMillis;
import static org.asdtm.goodweather.utils.Utils.getWeatherForecastUrl;

public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetChangedListener {

@@ -106,11 +121,18 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC

    public Context storedContext;

    private RecyclerView mRecyclerView;
    private List<WeatherForecast> mWeatherForecastList;
    private static Handler mHandler;
    private ProgressDialog mGetWeatherProgress;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ((GoodWeatherApp) getApplication()).applyTheme(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mGetWeatherProgress = getProgressDialog();
        mWeatherForecastList = new ArrayList<>();

        mWeather = new Weather();
        mCitySearch = new CitySearch();
@@ -143,6 +165,37 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        this.storedContext = this;
        fab.setOnClickListener(fabListener);

        getWeather();

        updateUI();
        mHandler = new Handler() {
            public void handleMessage(android.os.Message msg) {
                switch (msg.what) {
                    case Constants.TASK_RESULT_ERROR:
                        Toast.makeText(MainActivity.this,
                                R.string.toast_parse_error,
                                Toast.LENGTH_SHORT).show();
                        setVisibleUpdating(false);
                        break;
                    case Constants.PARSE_RESULT_ERROR:
                        Toast.makeText(MainActivity.this,
                                R.string.toast_parse_error,
                                Toast.LENGTH_SHORT).show();
                        setVisibleUpdating(false);
                        break;
                    case Constants.PARSE_RESULT_SUCCESS:
                        setVisibleUpdating(false);
                        updateUI();
                        if (!mWeatherForecastList.isEmpty()) {
                            AppPreference.saveWeatherForecast(MainActivity.this,
                                    mWeatherForecastList);
                        }
                        break;
                }
            }
        };

    }

    private void updateCurrentWeather() {
@@ -183,6 +236,7 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC
        mSunriseView.setText(getString(R.string.sunrise_label, sunrise));
        mSunsetView.setText(getString(R.string.sunset_label, sunset));


        configEditor.putString(Constants.APP_SETTINGS_CITY, mWeather.location.getCityName());
        configEditor.putString(Constants.APP_SETTINGS_COUNTRY_CODE,
                mWeather.location.getCountryCode());
@@ -192,6 +246,10 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC
    @Override
    public void onResume() {
        super.onResume();
        if (mWeatherForecastList.isEmpty()) {
            mWeatherForecastList = AppPreference.loadWeatherForecast(this);
        }
        updateUI();
        preLoadWeather();
        mAppBarLayout.addOnOffsetChangedListener(this);
        LocalBroadcastManager.getInstance(this).registerReceiver(mWeatherUpdateReceiver,
@@ -227,6 +285,8 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC
                if (connectionDetector.isNetworkAvailableAndConnected()) {
                    startService(new Intent(this, CurrentWeatherService.class));
                    setUpdateButtonState(true);
                    getWeather();
                    setVisibleUpdating(true);
                } else {
                    Toast.makeText(MainActivity.this,
                            R.string.connection_not_found,
@@ -363,6 +423,7 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC
        mSunriseView.setText(getString(R.string.sunrise_label, sunrise));
        mSunsetView.setText(getString(R.string.sunset_label, sunset));
        setTitle(Utils.getCityAndCountry(this));
        //updateUI();
    }

    private void initializeTextView() {
@@ -388,6 +449,10 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC
        mSunsetView = (TextView) findViewById(R.id.main_sunset);
        mAppBarLayout = (AppBarLayout) findViewById(R.id.main_app_bar);

        mRecyclerView = (RecyclerView) findViewById(R.id.forecast_recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));


        mIconWeatherView.setTypeface(weatherFontIcon);
        mTemperatureView.setTypeface(robotoThin);
        mWindSpeedView.setTypeface(robotoLight);
@@ -397,6 +462,7 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC
        mSunriseView.setTypeface(robotoLight);
        mSunsetView.setTypeface(robotoLight);


        /**
         * Initialize and configure weather icons
         */
@@ -658,4 +724,127 @@ public class MainActivity extends BaseActivity implements AppBarLayout.OnOffsetC
                break;
        }
    }

    private void getWeather() {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                SharedPreferences pref = getSharedPreferences(Constants.APP_SETTINGS_NAME, 0);
                String latitude = pref.getString(Constants.APP_SETTINGS_LATITUDE, "51.51");
                String longitude = pref.getString(Constants.APP_SETTINGS_LONGITUDE, "-0.13");
                String locale = LanguageUtil.getLanguageName(PreferenceUtil.getLanguage(MainActivity.this));
                String units = AppPreference.getTemperatureUnit(MainActivity.this);

                String requestResult = "";
                HttpURLConnection connection = null;
                try {
                    URL url = getWeatherForecastUrl(Constants.WEATHER_FORECAST_ENDPOINT, latitude, longitude, units, locale);
                    connection = (HttpURLConnection) url.openConnection();

                    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
                        InputStream inputStream = connection.getInputStream();

                        int bytesRead;
                        byte[] buffer = new byte[1024];
                        while ((bytesRead = inputStream.read(buffer)) > 0) {
                            byteArray.write(buffer, 0, bytesRead);
                        }
                        byteArray.close();
                        requestResult = byteArray.toString();
                        AppPreference.saveLastUpdateTimeMillis(MainActivity.this);
                    }
                } catch (IOException e) {
                    mHandler.sendEmptyMessage(Constants.TASK_RESULT_ERROR);
                    Log.e(TAG, "IOException: " + requestResult);
                } finally {
                    if (connection != null) {
                        connection.disconnect();
                    }
                }
                parseWeatherForecast(requestResult);
            }
        });
        t.start();
    }

    private void parseWeatherForecast(String data) {
        try {
            if (!mWeatherForecastList.isEmpty()) {
                mWeatherForecastList.clear();
            }

            JSONObject jsonObject = new JSONObject(data);
            JSONArray listArray = jsonObject.getJSONArray("list");

            int listArrayCount = listArray.length();
            for (int i = 0; i < listArrayCount; i++) {
                WeatherForecast weatherForecast = new WeatherForecast();
                JSONObject resultObject = listArray.getJSONObject(i);
                weatherForecast.setDateTime(resultObject.getLong("dt"));
                weatherForecast.setPressure(resultObject.getString("pressure"));
                weatherForecast.setHumidity(resultObject.getString("humidity"));
                weatherForecast.setWindSpeed(resultObject.getString("speed"));
                weatherForecast.setWindDegree(resultObject.getString("deg"));
                weatherForecast.setCloudiness(resultObject.getString("clouds"));
                if (resultObject.has("rain")) {
                    weatherForecast.setRain(resultObject.getString("rain"));
                } else {
                    weatherForecast.setRain("0");
                }
                if (resultObject.has("snow")) {
                    weatherForecast.setSnow(resultObject.getString("snow"));
                } else {
                    weatherForecast.setSnow("0");
                }
                JSONObject temperatureObject = resultObject.getJSONObject("temp");
                weatherForecast.setTemperatureMin(
                        Float.parseFloat(temperatureObject.getString("min")));
                weatherForecast.setTemperatureMax(
                        Float.parseFloat(temperatureObject.getString("max")));
                weatherForecast.setTemperatureMorning(
                        Float.parseFloat(temperatureObject.getString("morn")));
                weatherForecast.setTemperatureDay(
                        Float.parseFloat(temperatureObject.getString("day")));
                weatherForecast.setTemperatureEvening(
                        Float.parseFloat(temperatureObject.getString("eve")));
                weatherForecast.setTemperatureNight(
                        Float.parseFloat(temperatureObject.getString("night")));
                JSONArray weatherArray = resultObject.getJSONArray("weather");
                JSONObject weatherObject = weatherArray.getJSONObject(0);
                weatherForecast.setDescription(weatherObject.getString("description"));
                weatherForecast.setIcon(weatherObject.getString("icon"));

                mWeatherForecastList.add(weatherForecast);
                mHandler.sendEmptyMessage(Constants.PARSE_RESULT_SUCCESS);
            }
        } catch (JSONException e) {
            mHandler.sendEmptyMessage(Constants.TASK_RESULT_ERROR);
            e.printStackTrace();
        }
    }

    private void setVisibleUpdating(boolean visible) {
        if (visible) {
            mGetWeatherProgress.show();
        } else {
            mGetWeatherProgress.cancel();
        }
    }

    private void updateUI() {
        //ImageView android = (ImageView) findViewById(R.id.android);
        if (mWeatherForecastList.size() < 5) {
            mRecyclerView.setVisibility(View.INVISIBLE);
            //android.setVisibility(View.VISIBLE);
        } else {
            mRecyclerView.setVisibility(View.VISIBLE);
            //android.setVisibility(View.GONE);
        }
        WeatherForecastAdapter adapter = new WeatherForecastAdapter(this,
                mWeatherForecastList,
                getSupportFragmentManager());

        mRecyclerView.setAdapter(adapter);
    }
}
+7 −0
Original line number Diff line number Diff line
@@ -155,6 +155,13 @@
                android:orientation="vertical"
                android:padding="@dimen/activity_vertical_margin">

                <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/forecast.recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"></android.support.v7.widget.RecyclerView>

                <LinearLayout
                    style="@style/MainLinearLayout"
                    android:layout_width="match_parent"
+1 −17
Original line number Diff line number Diff line
@@ -5,14 +5,6 @@
            android:id="@+id/nav_menu_current_weather"
            android:icon="@drawable/ic_current_weather_black_24dp"
            android:title="@string/nav_menu_current_weather"/>
        <item
            android:id="@+id/nav_menu_graphs"
            android:icon="@drawable/ic_graphs_black_32dp"
            android:title="@string/nav_menu_graphs"/>
        <item
            android:id="@+id/nav_menu_weather_forecast"
            android:icon="@drawable/ic_daily_forecast_dark_24dp"
            android:title="@string/nav_menu_daily_forecast"/>
    </group>
    <group
        android:id="@+id/nav_menu_communication"
@@ -21,13 +13,5 @@
            android:id="@+id/nav_settings"
            android:icon="@drawable/ic_menu_settings"
            android:title="@string/nav_menu_settings"/>
        <item
            android:id="@+id/nav_feedback"
            android:icon="@drawable/ic_feedback"
            android:title="@string/nav_feedback"/>
        <item
            android:id="@+id/nav_menu_bitcoin_donation"
            android:icon="@drawable/ic_thumb_up_black_24dp"
            android:title="@string/nav_menu_bitcoin_donation"/>
    </group>
</menu>
Loading