<commit_before>package justaway.signinwithtwitter;

import twitter4j.Status;
import twitter4j.URLEntity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;

/**
 * タブのベースクラス
 */
public abstract class BaseFragment extends ListFragment {

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        MainActivity activity = (MainActivity) getActivity();

        ListView listView = getListView();

        // コンテキストメニューを使える様にする為の指定、但しデフォルトではロングタップで開く
        registerForContextMenu(listView);

        // Status(ツイート)をViewに描写するアダプター
        TwitterAdapter adapter = new TwitterAdapter(activity,
                R.layout.tweet_row);
        setListAdapter(adapter);

        /**
         * シングルタップでコンテキストメニューを開くための指定
         */
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                view.showContextMenu();
            }
        });
    }

    public void goToTop() {
        ListView listView = getListView();
        if (listView == null) {
            getActivity().finish();
            return;
        }
        listView.setSelection(0);
    }

    public Boolean isTop() {
        ListView listView = getListView();
        if (listView == null) {
            return false;
        }
        return listView.getFirstVisiblePosition() == 0 ? true : false;
    }

    /**
     * UserStreamでonStatusを受信した時の挙動
     * 
     * @param status
     */
    public abstract void add(Row row);

    static final int CONTEXT_MENU_REPLY_ID = 1;
    static final int CONTEXT_MENU_FAV_ID = 2;
    static final int CONTEXT_MENU_FAVRT_ID = 3;
    static final int CONTEXT_MENU_RT_ID = 4;
    static final int CONTEXT_MENU_QT_ID = 5;
    static final int CONTEXT_MENU_LINK_ID = 6;
    static final int CONTEXT_MENU_TOFU_ID = 7;
    static final int CONTEXT_MENU_DM_ID = 8;

    public void onCreateContextMenu(ContextMenu menu, View view,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, view, menuInfo);

        AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
        ListView listView = (ListView) view;
        MainActivity activity = (MainActivity) getActivity();
        Row row = (Row) listView.getItemAtPosition(info.position);
        activity.setSelectedRow(row);

        if (row.isDirectMessage()) {
            menu.setHeaderTitle(row.getMessage().getSenderScreenName());
            menu.add(0, CONTEXT_MENU_DM_ID, 0, "返信(DM)");
            return;
        }


        /*
         * statusの保持はActivityで行わないとなぜか2タブ目以降の値が保持できない..
         */

        Status status = row.getStatus();
        Status retweet = status.getRetweetedStatus();

        menu.setHeaderTitle(status.getText());
        menu.add(0, CONTEXT_MENU_REPLY_ID, 0, "リプ");
        menu.add(0, CONTEXT_MENU_QT_ID, 0, "引用");
        menu.add(0, CONTEXT_MENU_FAV_ID, 0, "ふぁぼ");
        menu.add(0, CONTEXT_MENU_FAVRT_ID, 0, "ふぁぼ＆公式RT");
        menu.add(0, CONTEXT_MENU_RT_ID, 0, "公式RT");

        // ツイート内のURLへアクセスできるようにメニューに展開する
        URLEntity[] urls = retweet != null ? retweet.getURLEntities() : status
                .getURLEntities();
        for (URLEntity url : urls) {
            menu.add(0, CONTEXT_MENU_LINK_ID, 0, url.getExpandedURL()
                    .toString());
        }

        // ツイート内のURL(画像)へアクセスできるようにメニューに展開する
        URLEntity[] medias = retweet != null ? retweet.getMediaEntities()
                : status.getMediaEntities();
        for (URLEntity url : medias) {
            menu.add(0, CONTEXT_MENU_LINK_ID, 0, url.getExpandedURL()
                    .toString());
        }

        menu.add(0, CONTEXT_MENU_TOFU_ID, 0, "TofuBuster");
    }

    public boolean onContextItemSelected(MenuItem item) {

        MainActivity activity = (MainActivity) getActivity();
        Row row = activity.getSelectedRow();
        Status status = row.getStatus();
        Intent intent;

        switch (item.getItemId()) {
        case CONTEXT_MENU_REPLY_ID:
            intent = new Intent(activity, PostActivity.class);
            String text = "@" + status.getUser().getScreenName() + " ";
            intent.putExtra("status", text);
            intent.putExtra("selection", text.length());
            intent.putExtra("inReplyToStatusId", status.getId());
            startActivity(intent);
            return true;
        case CONTEXT_MENU_QT_ID:
            intent = new Intent(activity, PostActivity.class);
            intent.putExtra("status",
                    " https://twitter.com/" + status.getUser().getScreenName()
                            + "/status/" + String.valueOf(status.getId()));
            intent.putExtra("inReplyToStatusId", status.getId());
            startActivity(intent);
            return true;
        case CONTEXT_MENU_DM_ID:
            String msg = "D " + row.getMessage().getSenderScreenName() + " ";
            intent = new Intent(activity, PostActivity.class);
            intent.putExtra("status", msg);
            intent.putExtra("selection", msg.length());
            startActivity(intent);
            return true;
        case CONTEXT_MENU_RT_ID:
            activity.doRetweet(status.getId());
            return true;
        case CONTEXT_MENU_FAV_ID:
            activity.doFavorite(status.getId());
            return true;
        case CONTEXT_MENU_FAVRT_ID:
            activity.doFavorite(status.getId());
            activity.doRetweet(status.getId());
            return true;
        case CONTEXT_MENU_LINK_ID:

            /**
             * 現在は全てIntentでブラウザなどに飛ばしているが、 画像やツイートは自アプリで参照できるように対応する予定
             */
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(item.getTitle()
                    .toString()));
            startActivity(intent);
            return true;
        case CONTEXT_MENU_TOFU_ID:
            try {
                intent = new Intent(
                        "com.product.kanzmrsw.tofubuster.ACTION_SHOW_TEXT");
                intent.putExtra(Intent.EXTRA_TEXT, status.getText());
                intent.putExtra(Intent.EXTRA_SUBJECT, "Justaway");
                intent.putExtra("isCopyEnabled", true);
                startActivity(intent); // TofuBusterがインストールされていない場合、startActivityで落ちる<commit_msg>Guy to open the link in WebView.<commit_after>package justaway.signinwithtwitter;

import twitter4j.Status;
import twitter4j.URLEntity;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.DisplayMetrics;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;

/**
 * タブのベースクラス
 */
public abstract class BaseFragment extends ListFragment {

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        MainActivity activity = (MainActivity) getActivity();

        ListView listView = getListView();

        // コンテキストメニューを使える様にする為の指定、但しデフォルトではロングタップで開く
        registerForContextMenu(listView);

        // Status(ツイート)をViewに描写するアダプター
        TwitterAdapter adapter = new TwitterAdapter(activity,
                R.layout.tweet_row);
        setListAdapter(adapter);

        /**
         * シングルタップでコンテキストメニューを開くための指定
         */
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                view.showContextMenu();
            }
        });
    }

    public void goToTop() {
        ListView listView = getListView();
        if (listView == null) {
            getActivity().finish();
            return;
        }
        listView.setSelection(0);
    }

    public Boolean isTop() {
        ListView listView = getListView();
        if (listView == null) {
            return false;
        }
        return listView.getFirstVisiblePosition() == 0 ? true : false;
    }

    /**
     * UserStreamでonStatusを受信した時の挙動
     * 
     * @param status
     */
    public abstract void add(Row row);

    static final int CONTEXT_MENU_REPLY_ID = 1;
    static final int CONTEXT_MENU_FAV_ID = 2;
    static final int CONTEXT_MENU_FAVRT_ID = 3;
    static final int CONTEXT_MENU_RT_ID = 4;
    static final int CONTEXT_MENU_QT_ID = 5;
    static final int CONTEXT_MENU_LINK_ID = 6;
    static final int CONTEXT_MENU_TOFU_ID = 7;
    static final int CONTEXT_MENU_DM_ID = 8;

    public void onCreateContextMenu(ContextMenu menu, View view,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, view, menuInfo);

        AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
        ListView listView = (ListView) view;
        MainActivity activity = (MainActivity) getActivity();
        Row row = (Row) listView.getItemAtPosition(info.position);
        activity.setSelectedRow(row);

        if (row.isDirectMessage()) {
            menu.setHeaderTitle(row.getMessage().getSenderScreenName());
            menu.add(0, CONTEXT_MENU_DM_ID, 0, "返信(DM)");
            return;
        }

        /*
         * statusの保持はActivityで行わないとなぜか2タブ目以降の値が保持できない..
         */

        Status status = row.getStatus();
        Status retweet = status.getRetweetedStatus();

        menu.setHeaderTitle(status.getText());
        menu.add(0, CONTEXT_MENU_REPLY_ID, 0, "リプ");
        menu.add(0, CONTEXT_MENU_QT_ID, 0, "引用");
        menu.add(0, CONTEXT_MENU_FAV_ID, 0, "ふぁぼ");
        menu.add(0, CONTEXT_MENU_FAVRT_ID, 0, "ふぁぼ＆公式RT");
        menu.add(0, CONTEXT_MENU_RT_ID, 0, "公式RT");

        // ツイート内のURLへアクセスできるようにメニューに展開する
        URLEntity[] urls = retweet != null ? retweet.getURLEntities() : status
                .getURLEntities();
        for (URLEntity url : urls) {
            menu.add(0, CONTEXT_MENU_LINK_ID, 0, url.getExpandedURL()
                    .toString());
        }

        // ツイート内のURL(画像)へアクセスできるようにメニューに展開する
        URLEntity[] medias = retweet != null ? retweet.getMediaEntities()
                : status.getMediaEntities();
        for (URLEntity url : medias) {
            menu.add(0, CONTEXT_MENU_LINK_ID, 0, url.getExpandedURL()
                    .toString());
        }

        menu.add(0, CONTEXT_MENU_TOFU_ID, 0, "TofuBuster");
    }

    @SuppressLint("SetJavaScriptEnabled")
    public boolean onContextItemSelected(MenuItem item) {

        MainActivity activity = (MainActivity) getActivity();
        Row row = activity.getSelectedRow();
        Status status = row.getStatus();
        Intent intent;

        switch (item.getItemId()) {
        case CONTEXT_MENU_REPLY_ID:
            intent = new Intent(activity, PostActivity.class);
            String text = "@" + status.getUser().getScreenName() + " ";
            intent.putExtra("status", text);
            intent.putExtra("selection", text.length());
            intent.putExtra("inReplyToStatusId", status.getId());
            startActivity(intent);
            return true;
        case CONTEXT_MENU_QT_ID:
            intent = new Intent(activity, PostActivity.class);
            intent.putExtra("status",
                    " https://twitter.com/" + status.getUser().getScreenName()
                            + "/status/" + String.valueOf(status.getId()));
            intent.putExtra("inReplyToStatusId", status.getId());
            startActivity(intent);
            return true;
        case CONTEXT_MENU_DM_ID:
            String msg = "D " + row.getMessage().getSenderScreenName() + " ";
            intent = new Intent(activity, PostActivity.class);
            intent.putExtra("status", msg);
            intent.putExtra("selection", msg.length());
            startActivity(intent);
            return true;
        case CONTEXT_MENU_RT_ID:
            activity.doRetweet(status.getId());
            return true;
        case CONTEXT_MENU_FAV_ID:
            activity.doFavorite(status.getId());
            return true;
        case CONTEXT_MENU_FAVRT_ID:
            activity.doFavorite(status.getId());
            activity.doRetweet(status.getId());
            return true;
        case CONTEXT_MENU_LINK_ID:

            /**
             * 現在は全てIntentでブラウザなどに飛ばしているが、 画像やツイートは自アプリで参照できるように対応する予定
             */
            // いちいちブラウザを開くのはダルイのでWebViewを開くことにする
            // intent = new Intent(Intent.ACTION_VIEW, Uri.parse(item.getTitle()
            // .toString()));
            // startActivity(intent);
            WebView webView = new WebView(getActivity());
            webView.getSettings().setLoadWithOverviewMode(true);
            webView.getSettings().setUseWideViewPort(true);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.getSettings().setCacheMode(
                    WebSettings.LOAD_CACHE_ELSE_NETWORK);
            webView.getSettings().setBuiltInZoomControls(true);
            // こいつをセットしないloadUrl出来ない
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }
            });
            webView.loadUrl(item.getTitle().toString());
            Dialog dialog = new Dialog(getActivity());
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(webView);
            // 標準だとダイアログが大きすぎるので小さくし下に固定で最新のツイートは読めるように
            DisplayMetrics metrics = getResources().getDisplayMetrics();
            WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
            lp.width = (int) (metrics.widthPixels * 1);
            lp.height = (int) (metrics.heightPixels * 0.75);
            dialog.getWindow().setAttributes(lp);
            dialog.getWindow().setGravity(Gravity.BOTTOM);
            dialog.show();
            return true;
        case CONTEXT_MENU_TOFU_ID:
            try {
                intent = new Intent(
                        "com.product.kanzmrsw.tofubuster.ACTION_SHOW_TEXT");
                intent.putExtra(Intent.EXTRA_TEXT, status.getText());
                intent.putExtra(Intent.EXTRA_SUBJECT, "Justaway");
                intent.putExtra("isCopyEnabled", true);
                startActivity(intent); // TofuBusterがインストールされていない場合、startActivityで落ちる
{'fraction_non_alphanumeric': 0.10255572259624812, 'fraction_numerical': 0.004444734949996732, 'mean_word_length': 2.2896151365297785, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '2005903', 'n_tokens_mistral': 4901, 'n_tokens_neox': 4678, 'n_words': 795}