<commit_before>package com.bruyu.imagewalker;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.GridView;
import android.widget.AdapterView;
import android.view.View;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;

import java.io.File;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * main activity to display all images in grid
 */
public class GridActivity extends BaseGridActivity {
    private static final String TAG = "::GridActivity";
    public static final String imgPath = "/storage/sdcard0/DCIM/Camera";
    public static final Pattern imgPattern = Pattern.compile("\\.jpg$");

    private GridView mGridView;

    private ImageFileAdapter mAdapter;

    private ArrayList<String> imgNameList = new ArrayList<>();

    private int selectedPosition = -1;
    protected ActionMode mActionMode;  // for action mode

    private int topN = 20;

    /// for all spawned activity which uses OpenCV lib
    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                    Log.i(TAG, " OpenCV loaded successfully");
                    break;
                default:
                    Log.e(TAG, " OpenCV not connect manager!");
                    super.onManagerConnected(status);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_grid);
        mGridView = (GridView)findViewById(R.id.gridview);

        mAdapter = new ImageFileAdapter(this);
        mGridView.setAdapter(mAdapter);

        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(null != mActionMode){ // action mode is working
                    if(selectedPosition != position){ // click another image, ignore this click
                        return;
                    }else if(mActionMode != null){ // click same image, cancel action mode
                        mActionMode.finish();
                        mActionMode = null;
                        selectedPosition = -1;
                    }
                }
                startImageDetailActivity(position);
            }
        });

        mGridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent,
                    View view, int position, long id) {
                if(mActionMode != null){
                    return false;
                }

                mGridView.setChoiceMode(GridView.CHOICE_MODE_SINGLE);

                mActionMode = GridActivity.this.startActionMode(mACB);

                // set the checkable to checked
                mGridView.setItemChecked(position, true);

                selectedPosition = position;
                return false;
            }
        });
    }

    /*
    * to load openCV library<commit_msg>fix a tiny bug of single click when Action mode<commit_after>package com.bruyu.imagewalker;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.GridView;
import android.widget.AdapterView;
import android.view.View;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;

import java.io.File;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * main activity to display all images in grid
 */
public class GridActivity extends BaseGridActivity {
    private static final String TAG = "::GridActivity";
    public static final String imgPath = "/storage/sdcard0/DCIM/Camera";
    public static final Pattern imgPattern = Pattern.compile("\\.jpg$");

    private GridView mGridView;

    private ImageFileAdapter mAdapter;

    private ArrayList<String> imgNameList = new ArrayList<>();

    private int selectedPosition = -1;
    protected ActionMode mActionMode;  // for action mode

    private int topN = 20;

    /// for all spawned activity which uses OpenCV lib
    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                    Log.i(TAG, " OpenCV loaded successfully");
                    break;
                default:
                    Log.e(TAG, " OpenCV not connect manager!");
                    super.onManagerConnected(status);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_grid);
        mGridView = (GridView)findViewById(R.id.gridview);

        mAdapter = new ImageFileAdapter(this);
        mGridView.setAdapter(mAdapter);

        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(null != mActionMode){ // action mode is on
                    selectedPosition = position;
                    return;
                }
                startImageDetailActivity(position); // action mode is off, start full screen
            }
        });

        mGridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent,
                    View view, int position, long id) {
                if(mActionMode != null){
                    return false;
                }

                mGridView.setChoiceMode(GridView.CHOICE_MODE_SINGLE);

                mActionMode = GridActivity.this.startActionMode(mACB);

                // set the checkable to checked
                mGridView.setItemChecked(position, true);

                selectedPosition = position;
                return false;
            }
        });
    }

    /*
    * to load openCV library
{'fraction_non_alphanumeric': 0.08276063752010528, 'fraction_numerical': 0.0013159818686942536, 'mean_word_length': 2.181395348837209, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 11, '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': '5628691', 'n_tokens_mistral': 1755, 'n_tokens_neox': 1635, 'n_words': 431}