<commit_before>package org.wikipedia.page;

import android.content.*;
import android.net.*;
import android.preference.*;
import android.util.*;
import com.squareup.otto.*;
import org.json.*;
import org.wikipedia.*;
import org.wikipedia.bridge.*;
import org.wikipedia.events.*;

/**
 * Handles any html links coming from a {@link org.wikipedia.page.PageViewFragment}
 */
public abstract class LinkHandler implements CommunicationBridge.JSEventListener {
    private final Context context;
    private final CommunicationBridge bridge;
    private final Bus bus;
    private final Site currentSite;
    private WikipediaApp app;

    public LinkHandler(Context context, CommunicationBridge bridge, Site currentSite) {
        this.context = context;
        this.bridge = bridge;
        this.app = (WikipediaApp)context.getApplicationContext();
        this.bus = app.getBus();
        this.currentSite = currentSite;

        this.bridge.addListener("linkClicked", this);
    }

    private void handleExternalLink(final Uri uri) {
        if (WikipediaApp.isWikipediaZeroDevmodeOn() && WikipediaApp.getWikipediaZeroDisposition()) {
            SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
            if (sharedPref.getBoolean(WikipediaApp.PREFERENCE_ZERO_INTERSTITIAL, true)) {
                bus.post(new WikipediaZeroInterstitialEvent(uri));
            } else {
                visitExternalLink(uri);
            }
        } else {
            visitExternalLink(uri);
        }
    }

    private void visitExternalLink(Uri uri) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(uri);
        context.startActivity(intent);
    }

    public abstract void onInternalLinkClicked(PageTitle title);

    @Override
    public void onMessage(String messageType, JSONObject messagePayload) {
        try {
            String href = messagePayload.getString("href");
            if (href.startsWith("//")) {
                // That's a protocol specific link! Make it https!
                href = "https:" + href;
            }
            Log.d("Wikipedia", "Link clicked was " + href);
            if (href.startsWith("/wiki/")) {
                // TODO: Handle fragments

                PageTitle title = currentSite.titleForInternalLink(href);
                onInternalLinkClicked(title);
            } else {
                Uri uri = Uri.parse(href);
                String authority = uri.getAuthority();
                // FIXME: Make this more complete, only to not handle URIs that contain unsupported actions
                if (authority != null && Site.isSupportedSite(authority) && uri.getPath().startsWith("/wiki/")) {
                    Site site = new Site(authority);
                    //TODO: Handle fragments
                    PageTitle title = site.titleForInternalLink(uri.getPath());
                    onInternalLinkClicked(title);
                } else {
                    // if it's a /w/ URI, turn it into a full URI and go external
                    if (href.startsWith("/w/")) {
                        href = String.format("%1$s://%2$s", WikipediaApp.PROTOCOL, currentSite.getDomain()) + href;
                    }
                    handleExternalLink(Uri.parse(href));
                }
            }
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }
}<commit_msg>Properly handle links to article titles with Unicode characters.<commit_after>package org.wikipedia.page;

import android.content.*;
import android.net.*;
import android.preference.*;
import android.util.*;
import com.squareup.otto.*;
import org.json.*;
import org.wikipedia.*;
import org.wikipedia.bridge.*;
import org.wikipedia.events.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * Handles any html links coming from a {@link org.wikipedia.page.PageViewFragment}
 */
public abstract class LinkHandler implements CommunicationBridge.JSEventListener {
    private final Context context;
    private final CommunicationBridge bridge;
    private final Bus bus;
    private final Site currentSite;
    private WikipediaApp app;

    public LinkHandler(Context context, CommunicationBridge bridge, Site currentSite) {
        this.context = context;
        this.bridge = bridge;
        this.app = (WikipediaApp)context.getApplicationContext();
        this.bus = app.getBus();
        this.currentSite = currentSite;

        this.bridge.addListener("linkClicked", this);
    }

    private void handleExternalLink(final Uri uri) {
        if (WikipediaApp.isWikipediaZeroDevmodeOn() && WikipediaApp.getWikipediaZeroDisposition()) {
            SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
            if (sharedPref.getBoolean(WikipediaApp.PREFERENCE_ZERO_INTERSTITIAL, true)) {
                bus.post(new WikipediaZeroInterstitialEvent(uri));
            } else {
                visitExternalLink(uri);
            }
        } else {
            visitExternalLink(uri);
        }
    }

    private void visitExternalLink(Uri uri) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(uri);
        context.startActivity(intent);
    }

    public abstract void onInternalLinkClicked(PageTitle title);

    @Override
    public void onMessage(String messageType, JSONObject messagePayload) {
        try {
            String href = URLDecoder.decode(messagePayload.getString("href"), "UTF-8");
            if (href.startsWith("//")) {
                // That's a protocol specific link! Make it https!
                href = "https:" + href;
            }
            Log.d("Wikipedia", "Link clicked was " + href);
            if (href.startsWith("/wiki/")) {
                // TODO: Handle fragments

                PageTitle title = currentSite.titleForInternalLink(href);
                onInternalLinkClicked(title);
            } else {
                Uri uri = Uri.parse(href);
                String authority = uri.getAuthority();
                // FIXME: Make this more complete, only to not handle URIs that contain unsupported actions
                if (authority != null && Site.isSupportedSite(authority) && uri.getPath().startsWith("/wiki/")) {
                    Site site = new Site(authority);
                    //TODO: Handle fragments
                    PageTitle title = site.titleForInternalLink(uri.getPath());
                    onInternalLinkClicked(title);
                } else {
                    // if it's a /w/ URI, turn it into a full URI and go external
                    if (href.startsWith("/w/")) {
                        href = String.format("%1$s://%2$s", WikipediaApp.PROTOCOL, currentSite.getDomain()) + href;
                    }
                    handleExternalLink(Uri.parse(href));
                }
            }
        } catch (UnsupportedEncodingException e) {
            // will not happen
            throw new RuntimeException(e);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }
}
{'fraction_non_alphanumeric': 0.10075041689827682, 'fraction_numerical': 0.0006948304613674264, 'mean_word_length': 2.145541958041958, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '826236', 'n_tokens_mistral': 1842, 'n_tokens_neox': 1753, 'n_words': 448}