package at.fhj.swd.dailyhelper.ui.fragemnt; import android.content.Context; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.graphics.Color; import android.os.Bundle; import android.os.Handler; import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.widget.CardView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import at.fhj.swd.dailyhelper.R; import at.fhj.swd.dailyhelper.adapter.WeatherListAdapter; import at.fhj.swd.dailyhelper.db.room.WeatherDB; import at.fhj.swd.dailyhelper.model.Weather; import at.fhj.swd.dailyhelper.network.DownloadImageTask; import at.fhj.swd.dailyhelper.service.LocationService; public class HomeFragment extends Fragment { private TextView tvLongitude; private TextView tvLatitude; private TextView tvSpeed; private TextView tvAltitude; private TextView tvAccuracy; private Context context; private Handler handler; private Runnable runnableCode; private SharedPreferences preferences; private View view; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_home, container, false); context = this.getContext(); preferences = PreferenceManager.getDefaultSharedPreferences(this.getContext()); loadPosition(); loadWeather(); return view; } @Override public void onResume() { super.onResume(); } @Override public void onPause() { handler.removeCallbacks(runnableCode); super.onPause(); } private void loadPosition () { tvLongitude = view.findViewById(R.id.tv_longitude); tvLatitude = view.findViewById(R.id.tv_latitude); tvSpeed = view.findViewById(R.id.tv_speed); tvAltitude = view.findViewById(R.id.tv_altitude); tvAccuracy = view.findViewById(R.id.tv_accuracy); // Create the Handler object (on the main thread by default) handler = new Handler(); // Define the code block to be executed runnableCode = new Runnable() { @Override public void run() { tvLongitude.setText(context.getString(R.string.longitude) +": "+ preferences.getFloat("longitude", -1000)); tvLatitude.setText(context.getString(R.string.latitude) +": "+ preferences.getFloat("latitude", -1000)); tvSpeed.setText(context.getString(R.string.speed) +": "+ preferences.getFloat("speed", -1000)); tvAltitude.setText(context.getString(R.string.altitude) +": "+ preferences.getFloat("altitude", -1000)); tvAccuracy.setText(context.getString(R.string.accuracy) +": "+ preferences.getFloat("accuracy", -1000)); handler.postDelayed(this, 2000); } }; // Start the initial runnable task by posting through the handler handler.post(runnableCode); } private void loadWeather (){ Weather weather; CardView cvCardView; TextView tvDate; ImageView imageView; TextView tvDescription; TextView tvTemperature; TextView tvHumity; cvCardView = view.findViewById(R.id.incWeather); tvDate = view.findViewById(R.id.tvDate); imageView = view.findViewById(R.id.card_view_image); tvDescription = view.findViewById(R.id.tvDescription); tvTemperature = view.findViewById(R.id.tvTemperature); tvHumity = view.findViewById(R.id.tvHumity); weather = WeatherDB.getDatabase(getContext()).weatherDAO().getFirst(); if (weather != null) { cvCardView.setCardElevation(0); tvDate.setText(weather.getDate()); new DownloadImageTask(imageView).execute("https://openweathermap.org/img/w/" + weather.getIcon() + ".png"); tvDescription.setText(weather.getDescription()); tvTemperature.setText(context.getString(R.string.temperature) + ": " + weather.getTemp() + "°C"); tvHumity.setText(context.getString(R.string.humidity) + ": " + weather.getHumidity() + "%"); } } public void refresh(){ loadPosition(); loadWeather(); } }