package com.xlythe.sample.weather;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.xlythe.service.weather.AwarenessWeather;
import com.xlythe.service.weather.AwarenessWeatherService;
import com.xlythe.service.weather.OpenWeatherService;
import com.xlythe.service.weather.PermissionUtils;
import com.xlythe.service.weather.Weather;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    private static final String[] REQUIRED_PERMISSIONS = new String[] {
            Manifest.permission.ACCESS_FINE_LOCATION,
    };
    private static final int REQUEST_CODE_PERMISSIONS = 1;

    @SuppressWarnings("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.sync).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "Sending broadcast action=" + AwarenessWeatherService.ACTION_RUN_MANUALLY);
                Intent intent = new Intent(getBaseContext(), AwarenessWeatherService.class);
                intent.setAction(AwarenessWeatherService.ACTION_RUN_MANUALLY);
                startService(intent);
            }
        });

        if (!PermissionUtils.hasPermissions(this, REQUIRED_PERMISSIONS)) {
            ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS);
        } else {
            AwarenessWeatherService.schedule(this);

            Weather weather = new AwarenessWeather();
            weather.restore(this);

            TextView textView = (TextView) findViewById(R.id.weather);
            textView.setText(weather.toString());
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (REQUEST_CODE_PERMISSIONS == requestCode) {
            recreate();
            return;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

@@ -77,22 +77,133 @@
 content.
-Intent
+BroadcastReceiver;%0Aimport android.content.Context;%0Aimport android.content.Intent;%0Aimport android.content.IntentFilter
 ;%0Aimport
@@ -559,62 +559,8 @@
 ce;%0A
-import com.xlythe.service.weather.OpenWeatherService;%0A
 impo
@@ -972,16 +972,210 @@
 S = 1;%0A%0A
+    private BroadcastReceiver mReceiver = new BroadcastReceiver() %7B%0A        @Override%0A        public void onReceive(Context context, Intent intent) %7B%0A            invalidate();%0A        %7D%0A    %7D;%0A%0A
     @Sup
@@ -1833,16 +1833,46 @@
 ntent);%0A
+                invalidate();%0A
         
@@ -2127,37 +2127,398 @@
 schedule(this);%0A
-%0A
     
+        invalidate();%0A        %7D%0A    %7D%0A%0A    @Override%0A    protected void onStart() %7B%0A        super.onStart();%0A        registerReceiver(mReceiver, new IntentFilter(AwarenessWeatherService.ACTION_DATA_CHANGED));%0A    %7D%0A%0A    @Override%0A    protected void onStop() %7B%0A        unregisterReceiver(mReceiver);%0A        super.onStop();%0A    %7D%0A%0A    private void invalidate() %7B%0A
         Weather 
@@ -2559,20 +2559,16 @@
         
-    
 weather.
@@ -2591,20 +2591,16 @@
         
- 
-   
 TextView
@@ -2658,20 +2658,16 @@
         
-   
- 
 textView
@@ -2696,26 +2696,16 @@
 ing());%0A
-        %7D%0A
     %7D%0A%0A 

update the sample app