<commit_before>package com.ternaryop.photoshelf.fragment;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.ternaryop.photoshelf.Constants;
import com.ternaryop.photoshelf.ImageDOMSelectorFinder;
import com.ternaryop.photoshelf.ImagePickerWebViewClient;
import com.ternaryop.photoshelf.ImageUrlRetriever;
import com.ternaryop.photoshelf.ImageUrlRetriever.OnImagesRetrieved;
import com.ternaryop.photoshelf.R;
import com.ternaryop.photoshelf.dialogs.TumblrPostDialog;
import com.ternaryop.photoshelf.parsers.TitleData;
import com.ternaryop.photoshelf.parsers.TitleParser;
import com.ternaryop.utils.URLUtils;

@SuppressLint("SetJavaScriptEnabled")
public class ImagePickerFragment extends AbsPhotoShelfFragment implements OnLongClickListener, OnImagesRetrieved {
    private WebView webView;
    private ImageUrlRetriever imageUrlRetriever;
    private ProgressBar progressBar;

    private ImageDOMSelectorFinder domSelectorFinder;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_webview, container, false);
        getActivity().setTitle(R.string.image_picker_activity_title);

        imageUrlRetriever = new ImageUrlRetriever(getActivity(), this);
        progressBar = (ProgressBar) rootView.findViewById(R.id.webview_progressbar);
        prepareWebView(rootView);
        domSelectorFinder = new ImageDOMSelectorFinder(getActivity());

        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        openUrl(getTextWithUrl());             
    }
    
    private String getTextWithUrl() {
        // Search on fragment arguments
        Bundle arguments = getArguments();
        if (arguments != null && arguments.containsKey(Constants.EXTRA_URL)) {
            return arguments.getString(Constants.EXTRA_URL);
        }

        // Search on activity intent
        // Get intent, action and MIME type
        Intent intent = getActivity().getIntent();
        String action = intent.getAction();
        String type = intent.getType();
        Uri uri = intent.getData();
        
        String textWithUrl = null;

        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if ("text/plain".equals(type)) {
                textWithUrl = intent.getStringExtra(Intent.EXTRA_TEXT);
            }
        } else if (Intent.ACTION_VIEW.equals(action) && uri != null) {
            textWithUrl = uri.toString();
        }
        return textWithUrl;
    }
    
    private void prepareWebView(View view) {
        webView = (WebView) view.findViewById(R.id.webview_view);
        webView.setWebViewClient(new ImagePickerWebViewClient(progressBar));
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                progressBar.setProgress(newProgress);
            }
        });
        webView.setOnLongClickListener(this);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.addJavascriptInterface(this, "targetUrlRetriever");
    }

    private void openUrl(String textWithUrl) {
        if (textWithUrl == null) {
            return;
        }
        final Matcher m = Pattern.compile("(http:.*)").matcher(textWithUrl);

        if (m.find()) {
            String url = m.group(1);
            // resolveShortenURL can't be called on main thread so we
            // resolve into a separated thread<commit_msg>Force to use a desktop browser User Agent<commit_after>package com.ternaryop.photoshelf.fragment;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.ternaryop.photoshelf.Constants;
import com.ternaryop.photoshelf.ImageDOMSelectorFinder;
import com.ternaryop.photoshelf.ImagePickerWebViewClient;
import com.ternaryop.photoshelf.ImageUrlRetriever;
import com.ternaryop.photoshelf.ImageUrlRetriever.OnImagesRetrieved;
import com.ternaryop.photoshelf.R;
import com.ternaryop.photoshelf.dialogs.TumblrPostDialog;
import com.ternaryop.photoshelf.parsers.TitleData;
import com.ternaryop.photoshelf.parsers.TitleParser;
import com.ternaryop.utils.URLUtils;

@SuppressLint("SetJavaScriptEnabled")
public class ImagePickerFragment extends AbsPhotoShelfFragment implements OnLongClickListener, OnImagesRetrieved {
    private WebView webView;
    private ImageUrlRetriever imageUrlRetriever;
    private ProgressBar progressBar;

    private ImageDOMSelectorFinder domSelectorFinder;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_webview, container, false);
        getActivity().setTitle(R.string.image_picker_activity_title);

        imageUrlRetriever = new ImageUrlRetriever(getActivity(), this);
        progressBar = (ProgressBar) rootView.findViewById(R.id.webview_progressbar);
        prepareWebView(rootView);
        domSelectorFinder = new ImageDOMSelectorFinder(getActivity());

        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        openUrl(getTextWithUrl());             
    }
    
    private String getTextWithUrl() {
        // Search on fragment arguments
        Bundle arguments = getArguments();
        if (arguments != null && arguments.containsKey(Constants.EXTRA_URL)) {
            return arguments.getString(Constants.EXTRA_URL);
        }

        // Search on activity intent
        // Get intent, action and MIME type
        Intent intent = getActivity().getIntent();
        String action = intent.getAction();
        String type = intent.getType();
        Uri uri = intent.getData();
        
        String textWithUrl = null;

        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if ("text/plain".equals(type)) {
                textWithUrl = intent.getStringExtra(Intent.EXTRA_TEXT);
            }
        } else if (Intent.ACTION_VIEW.equals(action) && uri != null) {
            textWithUrl = uri.toString();
        }
        return textWithUrl;
    }
    
    private void prepareWebView(View view) {
        webView = (WebView) view.findViewById(R.id.webview_view);
        webView.setWebViewClient(new ImagePickerWebViewClient(progressBar));
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                progressBar.setProgress(newProgress);
            }
        });
        webView.setOnLongClickListener(this);
        webView.getSettings().setJavaScriptEnabled(true);
        // force a desktop browser UA
        webView.getSettings().setUserAgentString("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:25.0) Gecko/20100101 Firefox/25.0");
        webView.addJavascriptInterface(this, "targetUrlRetriever");
    }

    private void openUrl(String textWithUrl) {
        if (textWithUrl == null) {
            return;
        }
        final Matcher m = Pattern.compile("(http:.*)").matcher(textWithUrl);

        if (m.find()) {
            String url = m.group(1);
            // resolveShortenURL can't be called on main thread so we
            // resolve into a separated thread
{'fraction_non_alphanumeric': 0.09201734849372875, 'fraction_numerical': 0.002461610596647521, 'mean_word_length': 3.302571860816944, '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': '3109371', 'n_tokens_mistral': 2409, 'n_tokens_neox': 2301, 'n_words': 473}