<commit_before>package org.inaturalist.android;

import java.util.HashMap;

import org.inaturalist.android.INaturalistService.LoginType;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class WebActivity extends Activity {
    private static String TAG = "WebActivity";
    private WebView mWebView;
    private INaturalistApp app;
    private ActivityHelper helper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        requestWindowFeature(Window.FEATURE_PROGRESS);
        app = (INaturalistApp) getApplicationContext();
        setContentView(R.layout.web);
        helper = new ActivityHelper(this);
        mWebView = (WebView) findViewById(R.id.webview);

        mWebView.getSettings().setJavaScriptEnabled(true);

        final Activity activity = this;
        mWebView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                // Activities and WebViews measure progress with different scales.
                // The progress meter will automatically disappear when we reach 100%
                activity.setProgress(progress * 1000);
            }
        });
        mWebView.setWebViewClient(new WebViewClient() {
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                helper.loading();
            }
            public void onPageFinished(WebView view, String url) {
                helper.stopLoading();
            }
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                helper.stopLoading();
                helper.alert(String.format(getString(R.string.oh_no), description));
            }
        });


        if (app.loggedIn()) {
            Log.d(TAG, "setting auth...");
            HashMap<String,String> headers = new HashMap<String,String>();
            
            if (app.getLoginType() == LoginType.PASSWORD) {
                headers.put("Authorization", "Basic " + app.getPrefs().getString("credentials", null));
            } else {
                headers.put("Authorization", "Bearer " + app.getPrefs().getString("credentials", null));
            }
            mWebView.getSettings().setUserAgentString(INaturalistService.USER_AGENT);
            mWebView.loadUrl(INaturalistService.HOST + "/home", headers);
        } else {
            helper.confirm(getString(R.string.need_to_login), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    Intent intent = new Intent(
                            app.currentUserLogin() == null ? "signin" : INaturalistPrefsActivity.REAUTHENTICATE_ACTION, 
                                    null, getBaseContext(), INaturalistPrefsActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            });
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN){
            switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (mWebView.canGoBack() == true) {
                    mWebView.goBack();
                } else {
                    finish();
                }
                return true;
            }

        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.web_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu:
            startActivity(new Intent(this, MenuActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP));
            return true;
        case R.id.reload:
            mWebView.reload();
            return true;
        case R.id.view:
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(mWebView.getUrl()));
            startActivity(i);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
}<commit_msg>Bugfix: back button in updates feed showed blank page when going back to the feed.<commit_after>package org.inaturalist.android;

import java.util.HashMap;

import org.inaturalist.android.INaturalistService.LoginType;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.*;

public class WebActivity extends Activity {
    private static String TAG = "WebActivity";
    private static String HOME_URL = INaturalistService.HOST + "/home";
    private WebView mWebView;
    private INaturalistApp app;
    private ActivityHelper helper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        requestWindowFeature(Window.FEATURE_PROGRESS);
        app = (INaturalistApp) getApplicationContext();
        setContentView(R.layout.web);
        helper = new ActivityHelper(this);
        mWebView = (WebView) findViewById(R.id.webview);

        mWebView.getSettings().setJavaScriptEnabled(true);

        final Activity activity = this;
        mWebView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                // Activities and WebViews measure progress with different scales.
                // The progress meter will automatically disappear when we reach 100%
                activity.setProgress(progress * 1000);
            }
        });
        mWebView.setWebViewClient(new WebViewClient() {
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                helper.loading();
            }
            public void onPageFinished(WebView view, String url) {
                helper.stopLoading();
            }
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                helper.stopLoading();
                helper.alert(String.format(getString(R.string.oh_no), description));
            }
            // TODO this works for get requests, but it doesn't intercept POSTs, 
            // so it leads to some weird behavior. Apparently this has been a bug 
            // since 2010, which is pretty disgusting:
            // https://code.google.com/p/android/issues/detail?id=9122
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (!app.loggedIn()) {
                    return false;
                }
                mWebView.loadUrl(url, getAuthHeaders());
                return true;
            }
        });
        goHome();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN){
            switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (mWebView.canGoBack() == true) {
                    WebBackForwardList webBackForwardList = mWebView.copyBackForwardList();
                    String historyUrl = webBackForwardList.getItemAtIndex(webBackForwardList.getCurrentIndex()-1).getUrl();
                    // weird bug shows a blank page when just going back to the first url in history
                    if (historyUrl.equals(HOME_URL)) {
                        mWebView.clearHistory();
                        goHome();
                    } else {
                        mWebView.goBack();
                    }
                } else {
                    finish();
                }
                return true;
            }

        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.web_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu:
            startActivity(new Intent(this, MenuActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP));
            return true;
        case R.id.reload:
            mWebView.reload();
            return true;
        case R.id.view:
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(mWebView.getUrl()));
            startActivity(i);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
    public HashMap<String,String> getAuthHeaders() {
        HashMap<String,String> headers = new HashMap<String,String>();
        if (!app.loggedIn()) {
            return headers;
        }
        if (app.getLoginType() == LoginType.PASSWORD) {
            headers.put("Authorization", "Basic " + app.getPrefs().getString("credentials", null));
        } else {
            headers.put("Authorization", "Bearer " + app.getPrefs().getString("credentials", null));
        }
        return headers;
    }
    
    public void goHome() {
        if (app.loggedIn()) {
            mWebView.getSettings().setUserAgentString(INaturalistService.USER_AGENT);
            mWebView.loadUrl(HOME_URL, getAuthHeaders());
        } else {
            helper.confirm(getString(R.string.need_to_login), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    Intent intent = new Intent(
                            app.currentUserLogin() == null ? "signin" : INaturalistPrefsActivity.REAUTHENTICATE_ACTION, 
                                    null, getBaseContext(), INaturalistPrefsActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            });
        }
    }
}
{'fraction_non_alphanumeric': 0.09364130919468475, 'fraction_numerical': 0.002407919379291893, 'mean_word_length': 2.114134962510414, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3424357', 'n_tokens_mistral': 2994, 'n_tokens_neox': 2850, 'n_words': 621}