<commit_before>package com.tellmas.android.redditor;

import java.net.URI;

import com.cd.reddit.json.mapping.RedditLink;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 *
 */
public class LinkDisplayFragment extends Fragment {

    private Redditor parentActivity;

    private WebView webview;
    private ProgressBar linkLoadingProgressBar;

    private String url;

    private String subreddit;
    private String linkId;
    private RedditLink theLinkData;


    /**
     *
     */
    public static LinkDisplayFragment newInstance(final String url, final String subreddit, final String linkId) {
        Log.d(GlobalDefines.LOG_TAG, "LinkDisplayFragment: newInstance()");
        Log.v(GlobalDefines.LOG_TAG, "LinkDisplayFragment: newInstance(): subreddit: " + subreddit);
        Log.v(GlobalDefines.LOG_TAG, "LinkDisplayFragment: newInstance(): linkId: " + linkId);

        final LinkDisplayFragment thisFragment = new LinkDisplayFragment();

        final Bundle args = new Bundle();
        args.putString(GlobalDefines.BUNDLE_KEY_FOR_URL, url);
        args.putString(GlobalDefines.BUNDLE_KEY_FOR_SUBREDDIT_NAME, subreddit);
        args.putString(GlobalDefines.BUNDLE_KEY_FOR_LINK_ID, linkId);
        thisFragment.setArguments(args);

        return thisFragment;
    }


   /**
    *
    */
   @Override
   public void onAttach(final Activity activity) {
       Log.d(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": onAttach()");
       super.onAttach(activity);

       this.parentActivity = (Redditor) activity;
   }


    /**
     *
     */
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        Log.d(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": onCreate()");
        super.onCreate(savedInstanceState);

        this.url = null;
        try {
            this.url = this.getArguments().getString(GlobalDefines.BUNDLE_KEY_FOR_URL);
        } catch (final NullPointerException npe) {
            Log.v(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": onCreateView(): no url in the bundle, so must be the initial instance of " + this.getClass().getSimpleName());
        } catch (final Exception e) {
            Log.v(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": onCreateView(): some other exception...", e);
        }

        try {
            this.subreddit = savedInstanceState.getString(GlobalDefines.BUNDLE_KEY_FOR_SUBREDDIT_NAME);
            this.linkId = savedInstanceState.getString(GlobalDefines.BUNDLE_KEY_FOR_LINK_ID);
        } catch (final Exception e) {
            Log.v(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": onCreateView(): exception getting the subreddit name and link id from the Bundle", e);
        }
    }


   /**
    *
    */
   @Override
   public View onCreateView(
           final LayoutInflater inflater,
           final ViewGroup container,
           final Bundle savedInstanceState
   ) {
       Log.d(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": onCreateView()");
       super.onCreateView(inflater, container, savedInstanceState);

       return inflater.inflate(R.layout.link_display_fragment, container, false);
   }


   /**
    *
    */
   @Override
   public void onViewCreated(final View view, final Bundle savedInstanceState) {
       Log.d(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": onViewCreated()");

       // --- progress bar for page load ---
       this.linkLoadingProgressBar = (ProgressBar) this.getView().findViewById(R.id.link_loading_progress);

       Log.v(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": getting the WebView...");
       this.webview = (WebView) this.getView().findViewById(R.id.link_display);
       Log.v(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": getting the WebView settings...");
       final WebSettings settings = this.webview.getSettings();
       Log.v(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": setting the WebView javascript...");
       settings.setJavaScriptEnabled(true);
       Log.v(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": setting the WebView LoadWithOverviewMode...");
       settings.setLoadWithOverviewMode(true);
       Log.v(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": setting the WebView WideViewPort...");
       settings.setUseWideViewPort(true);
       Log.v(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": setting the WebView client...");
       //this.webview.setWebViewClient(new WebViewClient());

       this.webview.setWebChromeClient(new WebChromeClient() {
           private boolean areDisplayingProgress = false;

           @Override
           public void onProgressChanged(final WebView view, final int newProgress) {
               Log.v(GlobalDefines.LOG_TAG, "WebChromeClient: onProgressChanged()");
               Log.v(GlobalDefines.LOG_TAG, "WebChromeClient: onProgressChanged(): new progress: " + Integer.valueOf(newProgress).toString());
               // if we are coming back from a redirect or if we're loading a new Link's url...
               if (newProgress == 0) {
                   // ...reset the progress bar.
                   Log.v(GlobalDefines.LOG_TAG, "WebChromeClient: onProgressChanged(): progress is ZERO. setting to Indeterminate");
                   this.areDisplayingProgress = false;
                   LinkDisplayFragment.this.linkLoadingProgressBar.setIndeterminate(true);
                   LinkDisplayFragment.this.linkLoadingProgressBar.setProgress(newProgress);
               // ...else if we're not yet displaying the progress and the reported progress has passed the threshold...
               } else if (!this.areDisplayingProgress && newProgress > GlobalDefines.PROGRESS_LOADING_SHOW_THRESHOLD - 1) {
                       Log.v(GlobalDefines.LOG_TAG, "WebChromeClient: onProgressChanged(): progress above threshold. setting Indeterminate OFF");
                       this.areDisplayingProgress = true;
                       LinkDisplayFragment.this.linkLoadingProgressBar.setIndeterminate(false);
                       LinkDisplayFragment.this.linkLoadingProgressBar.setProgress(newProgress);
               // ...else we are set to display the progress...
               } else {
                   // ...so display the progress.
                   Log.v(GlobalDefines.LOG_TAG, "WebChromeClient: onProgressChanged(): setting progress (and that's it)");
                   LinkDisplayFragment.this.linkLoadingProgressBar.setProgress(newProgress);
               }
           }

       });
       this.webview.setWebViewClient(new WebViewClient() {
           @Override
           public boolean shouldOverrideUrlLoading(final WebView view, final String urlNewString) {
               Log.v(GlobalDefines.LOG_TAG, "WebViewClient: shouldOverrideUrlLoading()");<commit_msg>Set progress bar to NOT indeterminate in order to show 100%<commit_after>package com.tellmas.android.redditor;

import java.net.URI;

import com.cd.reddit.json.mapping.RedditLink;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 *
 */
public class LinkDisplayFragment extends Fragment {

    private Redditor parentActivity;

    private WebView webview;
    private ProgressBar linkLoadingProgressBar;

    private String url;

    private String subreddit;
    private String linkId;
    private RedditLink theLinkData;


    /**
     *
     */
    public static LinkDisplayFragment newInstance(final String url, final String subreddit, final String linkId) {
        Log.d(GlobalDefines.LOG_TAG, "LinkDisplayFragment: newInstance()");
        Log.v(GlobalDefines.LOG_TAG, "LinkDisplayFragment: newInstance(): subreddit: " + subreddit);
        Log.v(GlobalDefines.LOG_TAG, "LinkDisplayFragment: newInstance(): linkId: " + linkId);

        final LinkDisplayFragment thisFragment = new LinkDisplayFragment();

        final Bundle args = new Bundle();
        args.putString(GlobalDefines.BUNDLE_KEY_FOR_URL, url);
        args.putString(GlobalDefines.BUNDLE_KEY_FOR_SUBREDDIT_NAME, subreddit);
        args.putString(GlobalDefines.BUNDLE_KEY_FOR_LINK_ID, linkId);
        thisFragment.setArguments(args);

        return thisFragment;
    }


   /**
    *
    */
   @Override
   public void onAttach(final Activity activity) {
       Log.d(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": onAttach()");
       super.onAttach(activity);

       this.parentActivity = (Redditor) activity;
   }


    /**
     *
     */
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        Log.d(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": onCreate()");
        super.onCreate(savedInstanceState);

        this.url = null;
        try {
            this.url = this.getArguments().getString(GlobalDefines.BUNDLE_KEY_FOR_URL);
        } catch (final NullPointerException npe) {
            Log.v(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": onCreateView(): no url in the bundle, so must be the initial instance of " + this.getClass().getSimpleName());
        } catch (final Exception e) {
            Log.v(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": onCreateView(): some other exception...", e);
        }

        try {
            this.subreddit = savedInstanceState.getString(GlobalDefines.BUNDLE_KEY_FOR_SUBREDDIT_NAME);
            this.linkId = savedInstanceState.getString(GlobalDefines.BUNDLE_KEY_FOR_LINK_ID);
        } catch (final Exception e) {
            Log.v(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": onCreateView(): exception getting the subreddit name and link id from the Bundle", e);
        }
    }


   /**
    *
    */
   @Override
   public View onCreateView(
           final LayoutInflater inflater,
           final ViewGroup container,
           final Bundle savedInstanceState
   ) {
       Log.d(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": onCreateView()");
       super.onCreateView(inflater, container, savedInstanceState);

       return inflater.inflate(R.layout.link_display_fragment, container, false);
   }


   /**
    *
    */
   @Override
   public void onViewCreated(final View view, final Bundle savedInstanceState) {
       Log.d(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": onViewCreated()");

       // --- progress bar for page load ---
       this.linkLoadingProgressBar = (ProgressBar) this.getView().findViewById(R.id.link_loading_progress);

       Log.v(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": getting the WebView...");
       this.webview = (WebView) this.getView().findViewById(R.id.link_display);
       Log.v(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": getting the WebView settings...");
       final WebSettings settings = this.webview.getSettings();
       Log.v(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": setting the WebView javascript...");
       settings.setJavaScriptEnabled(true);
       Log.v(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": setting the WebView LoadWithOverviewMode...");
       settings.setLoadWithOverviewMode(true);
       Log.v(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": setting the WebView WideViewPort...");
       settings.setUseWideViewPort(true);
       Log.v(GlobalDefines.LOG_TAG, this.getClass().getSimpleName() + ": setting the WebView client...");
       //this.webview.setWebViewClient(new WebViewClient());

       this.webview.setWebChromeClient(new WebChromeClient() {
           private boolean areDisplayingProgress = false;

           @Override
           public void onProgressChanged(final WebView view, final int newProgress) {
               Log.v(GlobalDefines.LOG_TAG, "WebChromeClient: onProgressChanged()");
               Log.v(GlobalDefines.LOG_TAG, "WebChromeClient: onProgressChanged(): new progress: " + Integer.valueOf(newProgress).toString());
               // if we are coming back from a redirect or if we're loading a new Link's url...
               if (newProgress == 0) {
                   // ...reset the progress bar.
                   Log.v(GlobalDefines.LOG_TAG, "WebChromeClient: onProgressChanged(): progress is ZERO. setting to Indeterminate");
                   this.areDisplayingProgress = false;
                   LinkDisplayFragment.this.linkLoadingProgressBar.setIndeterminate(true);
                   LinkDisplayFragment.this.linkLoadingProgressBar.setProgress(newProgress);
               // ...else if we're not yet displaying the progress and the reported progress has passed the threshold...
               } else if (!this.areDisplayingProgress && newProgress > GlobalDefines.PROGRESS_LOADING_SHOW_THRESHOLD - 1) {
                       Log.v(GlobalDefines.LOG_TAG, "WebChromeClient: onProgressChanged(): progress above threshold. setting Indeterminate OFF");
                       this.areDisplayingProgress = true;
                       LinkDisplayFragment.this.linkLoadingProgressBar.setIndeterminate(false);
                       LinkDisplayFragment.this.linkLoadingProgressBar.setProgress(newProgress);
               // ...else we are set to display the progress...
               } else {
                   // ...so display the progress.
                   Log.v(GlobalDefines.LOG_TAG, "WebChromeClient: onProgressChanged(): setting progress (and that's it)");
                   LinkDisplayFragment.this.linkLoadingProgressBar.setIndeterminate(false);
                   LinkDisplayFragment.this.linkLoadingProgressBar.setProgress(newProgress);
               }
           }

       });
       this.webview.setWebViewClient(new WebViewClient() {
           @Override
           public boolean shouldOverrideUrlLoading(final WebView view, final String urlNewString) {
               Log.v(GlobalDefines.LOG_TAG, "WebViewClient: shouldOverrideUrlLoading()");
{'fraction_non_alphanumeric': 0.10779785355250646, 'fraction_numerical': 0.0004754788751528325, 'mean_word_length': 2.974892008639309, 'pattern_counts': {'":': 26, '<': 3, '<?xml version=': 0, '>': 5, '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': '253825', 'n_tokens_mistral': 4086, 'n_tokens_neox': 3964, 'n_words': 826}