/**
 * Copyright 2009 Joe LaPenna
 */

package com.joelapenna.foursquared;

import com.joelapenna.foursquare.Foursquare;
import com.joelapenna.foursquared.error.LocationException;
import com.joelapenna.foursquared.location.LocationUtils;
import com.joelapenna.foursquared.util.NotificationsUtil;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * @author Joe LaPenna (joe@joelapenna.com)
 */
public class StatsActivity extends Activity {
    public static final String TAG = "StatsActivity";
    public static final boolean DEBUG = FoursquaredSettings.DEBUG;

    private BroadcastReceiver mLoggedOutReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (DEBUG) Log.d(TAG, "onReceive: " + intent);
            finish();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.stats_activity);
        registerReceiver(mLoggedOutReceiver, new IntentFilter(Foursquared.INTENT_ACTION_LOGGED_OUT));

        setTitle("Foursquare Scoreboard");

        WebView webView = (WebView) findViewById(R.id.webView);
        webView.setWebViewClient(new MyWebViewClient());
        webView.setWebChromeClient(new MyWebChromeClient());

        Foursquared foursquared = ((Foursquared) getApplication());
        String userId = ((Foursquared) getApplication()).getUserId();
        try {
            String url = Foursquare.createLeaderboardUrl(userId, LocationUtils
                    .createFoursquareLocation(foursquared.getLastKnownLocationOrThrow()));
            Log.d(TAG, url);
            webView.loadUrl(url);
        } catch (LocationException e) {
            NotificationsUtil.ToastReasonForFailure(this, e);
            finish();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mLoggedOutReceiver);
    }

    private class MyWebChromeClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            setProgress(newProgress * 100);
        }
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public void onPageFinished(WebView view, String url) {
            setProgressBarVisibility(false);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            setProgressBarVisibility(true);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}

@@ -1855,24 +1855,89 @@
       try %7B%0A
+            Foursquared.get(this).requestLocationUpdates(false);%0A
             
@@ -2273,32 +2273,111 @@
       finish();%0A
+        %7D finally %7B%0A            Foursquared.get(this).removeLocationUpdates();%0A
         %7D%0A    %7D%0A

make best effort to have location in StatsActivity