<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.mozstumbler.client.subactivities;

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import org.mozilla.mozstumbler.R;

public class LeaderboardActivity extends ActionBarActivity {
    private static final String LEADERBOARD_URL = "https://location.services.mozilla.com/leaders";
    private WebView mWebView;
    private boolean mHasError;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.activity_leaderboard);
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.setVisibility(View.INVISIBLE);
        mWebView.getSettings().setJavaScriptEnabled(true);
    }

    @Override
    protected void onStart() {
        super.onStart();
        setSupportProgressBarVisibility(true);
        final Activity activity = this;
        mWebView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                //activity.setProgress(progress * 100);
                setSupportProgress(progress * 100);
                if (progress > 45 && !mHasError) {
                    mWebView.setVisibility(View.VISIBLE);
                }
                if (progress > 60) {
                    injectJS();
                }
                if (progress > 90) {
                    setSupportProgressBarVisibility(false);
                }
            }
        });
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                mHasError = true;
                Toast.makeText(activity, "The Leaderboard requires an Internet connection.", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onPageFinished(WebView webview, String url) {
                if (!mHasError) {
                    mWebView.setVisibility(View.VISIBLE);
                    injectJS();
                }
                setSupportProgressBarVisibility(false);
            }
        });

        setProgress(0);
        mWebView.loadUrl(LEADERBOARD_URL);
    }

    // onPageFinished takes way too long to get called, and the page appears for a second before scrolling down.
    // As a work around just call this frequently while the page is loading
    private void injectJS() {
        String js = "(function(id){" +
                "var elem = document.getElementById(id);" +
                "var x = 0;" +
                "var y = 0;" +
                "while (elem != null) {" +
                "    x += elem.offsetLeft;" +
                "    y += elem.offsetTop;" +
                "    elem = elem.offsetParent;" +
                "}" +
                "window.scrollTo(x, y);" +
                "})('main-content')";
        mWebView.loadUrl("javascript:" + js);
    }
}<commit_msg>975 - fixed jumping to anchor for a nick, but if no nick set we need this ichnaea bug fixed https://github.com/mozilla/ichnaea/issues/327 to jump to the start of the leaderboard<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.mozstumbler.client.subactivities;

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import org.mozilla.mozstumbler.R;
import org.mozilla.mozstumbler.client.ClientPrefs;

public class LeaderboardActivity extends ActionBarActivity {
    private static final String LEADERBOARD_URL = "https://location.services.mozilla.com/leaders";
    private WebView mWebView;
    private boolean mHasError;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.activity_leaderboard);
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.setVisibility(View.INVISIBLE);
        mWebView.getSettings().setJavaScriptEnabled(true);
    }

    @Override
    protected void onStart() {
        super.onStart();
        setSupportProgressBarVisibility(true);
        final Activity activity = this;
        mWebView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                //activity.setProgress(progress * 100);
                setSupportProgress(progress * 100);
                if (progress > 45 && !mHasError) {
                    mWebView.setVisibility(View.VISIBLE);
                }

                if (progress > 90) {
                    setSupportProgressBarVisibility(false);
                }
            }
        });
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                mHasError = true;
                Toast.makeText(activity, "The Leaderboard requires an Internet connection.", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onPageFinished(WebView webview, String url) {
                if (!mHasError) {
                    mWebView.setVisibility(View.VISIBLE);
                }
                setSupportProgressBarVisibility(false);
            }
        });

        setProgress(0);
        ClientPrefs prefs = ClientPrefs.getInstance();
        if (prefs == null) {
            prefs = ClientPrefs.createGlobalInstance(this.getApplicationContext());
        }
        String nick = prefs.getNickname();
        String url = LEADERBOARD_URL;
        if (nick != null) {
            url += "#" + nick;
        } else {
            // TODO Get server side to add this anchor
            // https://github.com/mozilla/ichnaea/issues/327
            url += "#leaderboard_start";
        }
        mWebView.loadUrl(url);
    }
}
{'fraction_non_alphanumeric': 0.09409512590192902, 'fraction_numerical': 0.006626417317037255, 'mean_word_length': 2.1977401129943503, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5048396', 'n_tokens_mistral': 1881, 'n_tokens_neox': 1757, 'n_words': 433}