<commit_before>// Copyright (c) 2014, https://github.com/rhcad/touchvg

package touchvg.demo1;

import rhcad.touchvg.IViewHelper;
import rhcad.touchvg.ViewFactory;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

// Testing Activity using SFGraphView in PagerView
public class ExampleActivityImagePager extends ExampleActivity1 {
    private ViewPager mViewPager;
    private FrameLayout[] mViews = new FrameLayout[5];
    private int[] mChangeCounts = new int[5];
    private Bundle mInstanceState;
    private Drawable mBackground;

    @Override
    protected void createGraphView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_pager1);

        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mViewPager.setAdapter(new ViewPagerAdapter());
    }

    private String getFileName(int position) {
        return position + ".vg";
    }

    @Override
    protected String getFileName() {
        return (mViewPager != null ? mViewPager.getCurrentItem() : 0) + ".vg";
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("page", mViewPager.getCurrentItem());
        outState.putBoolean("drawing", hlp.getView() != null);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        mInstanceState = savedInstanceState;
        mViewPager.setCurrentItem(savedInstanceState.getInt("page"));
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        if (mViewPager != null) {
            closeDrawing(hlp, mViewPager.getCurrentItem());
        }
        super.onDestroy();
    }

    @Override
    protected void switchGestureEnabled() {
        int position = mViewPager.getCurrentItem();
        if (!closeDrawing(position)) {
            addDrawingView(position, PATH + getFileName(position));
        }
    }

    private boolean closeDrawing(int position) {
        return closeDrawing(ViewFactory.createHelper(mViews[position]), position);
    }

    private boolean closeDrawing(IViewHelper tmphlp, int position) {
        if (tmphlp != null && tmphlp.getView() != null) {
            if (tmphlp != hlp && tmphlp.getView() == hlp.getView()) {
                hlp.setGraphView(null);
            }

            if (mChangeCounts[position] != tmphlp.getChangeCount()) {
                tmphlp.saveToFile(PATH + getFileName(position));
                tmphlp.exportPNG(PATH + getFileName(position));
            }
            tmphlp.close();
            return true;
        }
<commit_msg>Add OnViewDetachedListener demo.<commit_after>// Copyright (c) 2014, https://github.com/rhcad/touchvg

package touchvg.demo1;

import rhcad.touchvg.IGraphView.OnViewDetachedListener;
import rhcad.touchvg.IViewHelper;
import rhcad.touchvg.ViewFactory;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

// Testing Activity using SFGraphView in PagerView
public class ExampleActivityImagePager extends ExampleActivity1 {
    private ViewPager mViewPager;
    private FrameLayout[] mViews = new FrameLayout[5];
    private int[] mChangeCounts = new int[5];
    private Bundle mInstanceState;
    private Drawable mBackground;

    @Override
    protected void createGraphView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_pager1);

        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mViewPager.setAdapter(new ViewPagerAdapter());
    }

    private String getFileName(int position) {
        return position + ".vg";
    }

    @Override
    protected String getFileName() {
        return (mViewPager != null ? mViewPager.getCurrentItem() : 0) + ".vg";
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("page", mViewPager.getCurrentItem());
        outState.putBoolean("drawing", hlp.getView() != null);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        mInstanceState = savedInstanceState;
        mViewPager.setCurrentItem(savedInstanceState.getInt("page"));
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        if (mViewPager != null) {
            closeDrawing(hlp, mViewPager.getCurrentItem());
        }
        super.onDestroy();
    }

    @Override
    protected void switchGestureEnabled() {
        int position = mViewPager.getCurrentItem();
        if (!closeDrawing(position)) {
            addDrawingView(position, PATH + getFileName(position));
        }
    }

    private boolean closeDrawing(int position) {
        return closeDrawing(ViewFactory.createHelper(mViews[position]), position);
    }

    private boolean closeDrawing(IViewHelper tmphlp, int position) {
        if (tmphlp != null && tmphlp.getView() != null) {
            if (tmphlp != hlp && tmphlp.getView() == hlp.getView()) {
                hlp.setGraphView(null);
            }

            if (mChangeCounts[position] != tmphlp.getChangeCount()) {
                tmphlp.saveToFile(PATH + getFileName(position));
                tmphlp.exportPNG(PATH + getFileName(position));
            }
            tmphlp.close(new OnViewDetachedListener() {
                @Override
                public void onGraphViewDetached() {
                    Log.d("touchvg", "You can show image in onGraphViewDetached");
                }
            });
            return true;
        }

{'fraction_non_alphanumeric': 0.10267206477732793, 'fraction_numerical': 0.0038866396761133605, 'mean_word_length': 3.0208333333333335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6271613', 'n_tokens_mistral': 1788, 'n_tokens_neox': 1700, 'n_words': 348}