<commit_before>package com.tzapps.tzpalette.ui;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.tzapps.tzpalette.R;
import com.tzapps.tzpalette.data.PaletteData;
import com.tzapps.tzpalette.utils.ActivityUtils;

public class MainActivity extends Activity implements BaseFragment.OnFragmentStatusChangedListener
{
    private final static String TAG = "MainActivity";
    
    /** Called when the user clicks the TakePicture button */
    static final int TAKE_PHOTE_RESULT   = 1; 
    /** Called when the user clicks the LoadPicture button */
    static final int LOAD_PICTURE_RESULT = 2;
    
    ViewPager   mViewPager;
    TabsAdapter mTabsAdapter;
    PaletteData mPaletteData;
    
    CaptureFragment       mCaptureFrag;
    MyPaletteListFragment mPaletteListFragment;
    CaptureFragment       fragment3;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        mViewPager = new ViewPager(this);
        mViewPager.setId(R.id.pager);
        setContentView(mViewPager);
        
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowHomeEnabled(false);
        
        mCaptureFrag = null;
        mPaletteListFragment = null;
        fragment3 = null;
        
        mTabsAdapter = new TabsAdapter(this, mViewPager);
        mTabsAdapter.addTab(actionBar.newTab().setText("Capture"), CaptureFragment.class, null);
        mTabsAdapter.addTab(actionBar.newTab().setText("My Palette"),MyPaletteListFragment.class, null);
        mTabsAdapter.addTab(actionBar.newTab().setText("About"),  CaptureFragment.class, null);
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        
        outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
        
        if (mPaletteData != null)
        {
            outState.putParcelable("bitmap", mPaletteData.getThumb());
            outState.putIntArray("colors", mPaletteData.getColors());
        }
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);
        
        mTabsAdapter.setSelectedTab(savedInstanceState.getInt("tab",0));
        
        Bitmap bitmap = (Bitmap)savedInstanceState.getParcelable("bitmap");
        int[]  colors = savedInstanceState.getIntArray("colors");
               
        if (mPaletteData == null)
            mPaletteData = new PaletteData();

        mPaletteData.setThumb(bitmap);
        mPaletteData.addColors(colors, /*reset*/true);
        
        refresh();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle presses on the action bar items
        switch (item.getItemId())
        {
            case R.id.action_settings:
                // openSettings();
                return true;

            case R.id.action_about:
                // openAbout();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    /** refresh fragments in main activity with persisted mPaletteData */
    private void refresh()
    {
        if (mCaptureFrag != null && mPaletteData != null)
        {
            mCaptureFrag.updateImageView(mPaletteData.getThumb());
            mCaptureFrag.updateColors(mPaletteData.getColors());
        }
    }
    
    @Override
    public void onFragmentViewCreated(Fragment fragment)
    {
        if (fragment instanceof CaptureFragment)
            mCaptureFrag = (CaptureFragment) fragment;
        
        refresh();
    }

    /** Called when the user clicks the TakePhoto button */
    public void takePhoto(View view)
    {
        Log.d(TAG, "take a photo");
        
        if (ActivityUtils.isIntentAvailable(getBaseContext(), MediaStore.ACTION_IMAGE_CAPTURE))
        {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(takePictureIntent, TAKE_PHOTE_RESULT);
        }
        else
        {
            Log.e(TAG, "no camera found");
        }
    }
    
    /** Called when the user clicks the PickPicture button */
    public void loadPicture(View view)
    {
        Log.d(TAG, "load a picture");
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, LOAD_PICTURE_RESULT);
    }
    
    /** Called when the user clicks the Analysis button */
    public void analysisPicture(View view)
    {
        Log.d(TAG, "analysis the picture");
        
        if (mPaletteData == null)
            return;
        
        // TODO: async this process to avoid UI thread block
        mPaletteData.analysis(/*rest*/true);
        mCaptureFrag.updateColors(mPaletteData.getColors());
    }<commit_msg>add AsyncTask for PaletteData analysis process<commit_after>package com.tzapps.tzpalette.ui;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.tzapps.tzpalette.R;
import com.tzapps.tzpalette.data.PaletteData;
import com.tzapps.tzpalette.utils.ActivityUtils;

public class MainActivity extends Activity implements BaseFragment.OnFragmentStatusChangedListener
{
    private final static String TAG = "MainActivity";
    
    /** Called when the user clicks the TakePicture button */
    static final int TAKE_PHOTE_RESULT   = 1; 
    /** Called when the user clicks the LoadPicture button */
    static final int LOAD_PICTURE_RESULT = 2;
    
    ViewPager   mViewPager;
    TabsAdapter mTabsAdapter;
    PaletteData mPaletteData;
    
    CaptureFragment       mCaptureFrag;
    MyPaletteListFragment mPaletteListFragment;
    CaptureFragment       fragment3;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        mViewPager = new ViewPager(this);
        mViewPager.setId(R.id.pager);
        setContentView(mViewPager);
        
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowHomeEnabled(false);
        
        mCaptureFrag = null;
        mPaletteListFragment = null;
        fragment3 = null;
        
        mTabsAdapter = new TabsAdapter(this, mViewPager);
        mTabsAdapter.addTab(actionBar.newTab().setText("Capture"), CaptureFragment.class, null);
        mTabsAdapter.addTab(actionBar.newTab().setText("My Palette"),MyPaletteListFragment.class, null);
        mTabsAdapter.addTab(actionBar.newTab().setText("About"),  CaptureFragment.class, null);
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        
        outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
        
        if (mPaletteData != null)
        {
            outState.putParcelable("bitmap", mPaletteData.getThumb());
            outState.putIntArray("colors", mPaletteData.getColors());
        }
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);
        
        mTabsAdapter.setSelectedTab(savedInstanceState.getInt("tab",0));
        
        Bitmap bitmap = (Bitmap)savedInstanceState.getParcelable("bitmap");
        int[]  colors = savedInstanceState.getIntArray("colors");
               
        if (mPaletteData == null)
            mPaletteData = new PaletteData();

        mPaletteData.setThumb(bitmap);
        mPaletteData.addColors(colors, /*reset*/true);
        
        refresh();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle presses on the action bar items
        switch (item.getItemId())
        {
            case R.id.action_settings:
                // openSettings();
                return true;

            case R.id.action_about:
                // openAbout();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    /** refresh fragments in main activity with persisted mPaletteData */
    private void refresh()
    {
        if (mCaptureFrag != null && mPaletteData != null)
        {
            mCaptureFrag.updateImageView(mPaletteData.getThumb());
            mCaptureFrag.updateColors(mPaletteData.getColors());
        }
    }
    
    @Override
    public void onFragmentViewCreated(Fragment fragment)
    {
        if (fragment instanceof CaptureFragment)
            mCaptureFrag = (CaptureFragment) fragment;
        
        refresh();
    }

    /** Called when the user clicks the TakePhoto button */
    public void takePhoto(View view)
    {
        Log.d(TAG, "take a photo");
        
        if (ActivityUtils.isIntentAvailable(getBaseContext(), MediaStore.ACTION_IMAGE_CAPTURE))
        {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(takePictureIntent, TAKE_PHOTE_RESULT);
        }
        else
        {
            Log.e(TAG, "no camera found");
        }
    }
    
    /** Called when the user clicks the PickPicture button */
    public void loadPicture(View view)
    {
        Log.d(TAG, "load a picture");
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, LOAD_PICTURE_RESULT);
    }
    
    /** Called when the user clicks the Analysis button */
    public void analysisPicture(View view)
    {
        Log.d(TAG, "analysis the picture");
        
        if (mPaletteData == null)
            return;
        
        // TODO: async this process to avoid UI thread block
        //mPaletteData.analysis(/*rest*/true);
        //mCaptureFrag.updateColors(mPaletteData.getColors());
        
        new PaletteDataAnalysisTask().execute(mPaletteData);
    }
    
    private class PaletteDataAnalysisTask extends AsyncTask<PaletteData, Void, PaletteData>
    {
        /* The system calls this to perform work in a worker thread and
         * delivers it the parameters given to AsyncTask.execute() 
         */
        protected PaletteData doInBackground(PaletteData ...dataArray)
        {
            PaletteData data = dataArray[0];
            data.analysis(/*reset*/true);
            return data;
        }
        
        /* The system calls this to perform work in the UI thread and
         * delivers the result from doInBackground()
         */
        protected void onPostExecute(PaletteData result)
        {
            mCaptureFrag.updateColors(result.getColors());
        }
    }
{'fraction_non_alphanumeric': 0.08883599508599509, 'fraction_numerical': 0.0013052825552825552, 'mean_word_length': 2.575349986275048, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '4323750', 'n_tokens_mistral': 3641, 'n_tokens_neox': 3529, 'n_words': 759}