<commit_before>package org.opencv.engine.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;

import org.opencv.engine.HardwareDetector;
import org.opencv.engine.MarketConnector;
import org.opencv.engine.OpenCVEngineInterface;
import org.opencv.engine.OpenCVLibraryInfo;
import org.opencv.engine.R;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ManagerActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView OsVersionView = (TextView)findViewById(R.id.OsVersionValue);
        OsVersionView.setText(Build.VERSION.CODENAME + " (" + Build.VERSION.RELEASE + "), API " + Build.VERSION.SDK_INT);

        try {
            PackageInfo packageInfo = getPackageManager().getPackageInfo(this.getPackageName(), 0);
            ManagerVersion = packageInfo.versionName;
        } catch (NameNotFoundException e) {
            ManagerVersion = "N/A";
            e.printStackTrace();
        }

        mInstalledPackageView = (ListView)findViewById(R.id.InstalledPackageList);

        mMarket = new MarketConnector(this);

        mInstalledPacksAdapter = new PackageListAdapter(
                this,
                mListViewItems,
                R.layout.info,
                new String[] {"Name", "Version", "Hardware", "Activity"},
                new int[] {R.id.InfoName,R.id.InfoVersion, R.id.InfoHardware}
                );

        mInstalledPackageView.setAdapter(mInstalledPacksAdapter);

        TextView HardwarePlatformView = (TextView)findViewById(R.id.HardwareValue);
        int Platfrom = HardwareDetector.DetectKnownPlatforms();
        int CpuId = HardwareDetector.GetCpuID();

        if (HardwareDetector.PLATFORM_UNKNOWN != Platfrom)
        {
            if (HardwareDetector.PLATFORM_TEGRA == Platfrom)
            {
                HardwarePlatformView.setText("Tegra");
            }
            else if (HardwareDetector.PLATFORM_TEGRA2 == Platfrom)
            {
                HardwarePlatformView.setText("Tegra 2");
            }
            else
            {
                HardwarePlatformView.setText("Tegra 3");
            }
        }
        else
        {
            if ((CpuId & HardwareDetector.ARCH_X86) == HardwareDetector.ARCH_X86)
            {
                HardwarePlatformView.setText("x86 " + JoinIntelFeatures(CpuId));
            }
            else if ((CpuId & HardwareDetector.ARCH_X64) == HardwareDetector.ARCH_X64)
            {
                HardwarePlatformView.setText("x64 " + JoinIntelFeatures(CpuId));
            }
            else if ((CpuId & HardwareDetector.ARCH_ARMv5) == HardwareDetector.ARCH_ARMv5)
            {
                HardwarePlatformView.setText("ARM v5 " + JoinArmFeatures(CpuId));
            }
            else if ((CpuId & HardwareDetector.ARCH_ARMv6) == HardwareDetector.ARCH_ARMv6)
            {
                HardwarePlatformView.setText("ARM v6 " + JoinArmFeatures(CpuId));
            }
            else if ((CpuId & HardwareDetector.ARCH_ARMv7) == HardwareDetector.ARCH_ARMv7)
            {
                HardwarePlatformView.setText("ARM v7 " + JoinArmFeatures(CpuId));
            }
            else if ((CpuId & HardwareDetector.ARCH_ARMv8) == HardwareDetector.ARCH_ARMv8)
            {
                HardwarePlatformView.setText("ARM v8 " + JoinArmFeatures(CpuId));
            }
            else if ((CpuId & HardwareDetector.ARCH_MIPS) == HardwareDetector.ARCH_MIPS)
            {
                HardwarePlatformView.setText("MIPS");
            }
            else
            {
                HardwarePlatformView.setText("not detected");
            }
        }

        mUpdateEngineButton = (Button)findViewById(R.id.CheckEngineUpdate);
        mUpdateEngineButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                if (!mMarket.InstallAppFromMarket("org.opencv.engine"))
                {
                    Toast toast = Toast.makeText(getApplicationContext(), "Google Play is not avaliable", Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        });

        mActionDialog = new AlertDialog.Builder(this).create();

        mActionDialog.setTitle("Choose action");
        mActionDialog.setButton("Update", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                int index = (Integer)mInstalledPackageView.getTag();
                if (!mMarket.InstallAppFromMarket(mInstalledPackageInfo[index].packageName))
                {
                    Toast toast = Toast.makeText(getApplicationContext(), "Google Play is not avaliable", Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        });

        mActionDialog.setButton3("Remove", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                int index = (Integer)mInstalledPackageView.getTag();
                if (!mMarket.RemoveAppFromMarket(mInstalledPackageInfo[index].packageName, true))
                {
                    Toast toast = Toast.makeText(getApplicationContext(), "Google Play is not avaliable", Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        });

        mActionDialog.setButton2("Return", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // nothing
            }
        });

        mInstalledPackageView.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
                //if (!mListViewItems.get((int) id).get("Name").equals("Built-in OpenCV library"));
                if (!mInstalledPackageInfo[(int) id].packageName.equals("org.opencv.engine"))
                {
                    mInstalledPackageView.setTag(Integer.valueOf((int)id));
                    mActionDialog.show();
                }
            }
        });

        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_PACKAGE_ADDED);
        filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
        filter.addAction(Intent.ACTION_PACKAGE_INSTALL);
        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        filter.addAction(Intent.ACTION_PACKAGE_REPLACED);

        registerReceiver(mPackageChangeReciever, filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mPackageChangeReciever);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "Filling package list on resume");
        if (!bindService(new Intent("org.opencv.engine.BIND"), new OpenCVEngineServiceConnection(), Context.BIND_AUTO_CREATE))
        {
            TextView EngineVersionView = (TextView)findViewById(R.id.EngineVersionValue);
            EngineVersionView.setText("not avaliable");
        }
    }

    protected SimpleAdapter mInstalledPacksAdapter;
    protected ListView mInstalledPackageView;
    protected Button mUpdateEngineButton;
    protected PackageInfo[] mInstalledPackageInfo;
    protected final ArrayList<HashMap<String,String>> mListViewItems = new ArrayList<HashMap<String,String>>();
    protected static final String TAG = "OpenCV_Manager/Activity";
    protected MarketConnector mMarket;
    protected AlertDialog mActionDialog;
    protected HashMap<String,String> mActivePackageMap = new HashMap<String, String>();
    protected int ManagerApiLevel = 0;
    protected String ManagerVersion;

    protected BroadcastReceiver mPackageChangeReciever = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("OpenCVManager/Reciever", "Bradcast message " + intent.getAction() + " reciever");
            Log.d("OpenCVManager/Reciever", "Filling package list on broadcast message");
            if (!bindService(new Intent("org.opencv.engine.BIND"), new OpenCVEngineServiceConnection(), Context.BIND_AUTO_CREATE))
            {
                TextView EngineVersionView = (TextView)findViewById(R.id.EngineVersionValue);
                EngineVersionView.setText("not avaliable");
            }
        }
    };

    protected class OpenCVEngineServiceConnection implements ServiceConnection
    {
        public void onServiceDisconnected(ComponentName name) {
        }

        public void onServiceConnected(ComponentName name, IBinder service) {
            OpenCVEngineInterface EngineService = OpenCVEngineInterface.Stub.asInterface(service);
            try {
                ManagerApiLevel = EngineService.getEngineVersion();
            } catch (RemoteException e) {
                e.printStackTrace();
            }

            TextView EngineVersionView = (TextView)findViewById(R.id.EngineVersionValue);
            EngineVersionView.setText(ManagerVersion);

            try {
                String path = EngineService.getLibPathByVersion("2.4");
                Log.d(TAG, "2.4 -> " + path);
                mActivePackageMap.put("24", path);
                path = EngineService.getLibPathByVersion("2.5");
                Log.d(TAG, "2.5 -> " + path);
                mActivePackageMap.put("25", path);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Log.d(TAG, "Filling package list on service connection");
            FillPackageList();

            unbindService(this);
        }
    };

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    synchronized protected void FillPackageList()
    {
        synchronized (mListViewItems) {
            mInstalledPackageInfo = mMarket.GetInstalledOpenCVPackages();
            mListViewItems.clear();

            int RealPackageCount = mInstalledPackageInfo.length;
            for (int i = 0; i < RealPackageCount; i++)
            {
                if (mInstalledPackageInfo[i] == null)
                    break;

                // Convert to Items for package list view
                HashMap<String,String> temp = new HashMap<String,String>();

                String HardwareName = "";
                String NativeLibDir = "";
                String OpenCVersion = "";

                String PublicName = mMarket.GetApplicationName(mInstalledPackageInfo[i].applicationInfo);
                String PackageName = mInstalledPackageInfo[i].packageName;
                String VersionName = mInstalledPackageInfo[i].versionName;

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD)
                    NativeLibDir = mInstalledPackageInfo[i].applicationInfo.nativeLibraryDir;
                else
                    NativeLibDir = "/data/data/" + mInstalledPackageInfo[i].packageName + "/lib";

                OpenCVLibraryInfo NativeInfo = new OpenCVLibraryInfo(NativeLibDir);

                if (PackageName.equals("org.opencv.engine"))
                {
                    if (NativeInfo.status())
                    {
                        PublicName = "Built-in OpenCV library";
                        PackageName = NativeInfo.packageName();
                        VersionName = NativeInfo.versionName();
                    }
                    else
                    {
                        mInstalledPackageInfo[i] = mInstalledPackageInfo[RealPackageCount-1];
                        mInstalledPackageInfo[RealPackageCount-1] = null;
                        RealPackageCount--;
                        i--;
                        continue;
                    }
                }

                int idx = 0;
                Log.d(TAG, PackageName);
                StringTokenizer tokenizer = new StringTokenizer(PackageName, "_");
                while (tokenizer.hasMoreTokens())
                {
                    if (idx == 1)
                    {
                        // version of OpenCV
                        OpenCVersion = tokenizer.nextToken().substring(1);
                    }
                    else if (idx >= 2)
                    {
                        // hardware options
                        HardwareName += tokenizer.nextToken() + " ";
                    }
                    else
                    {
                        tokenizer.nextToken();
                    }
                    idx++;
                }

                String ActivePackagePath;
                String Tags = null;
                ActivePackagePath = mActivePackageMap.get(OpenCVersion);
                Log.d(TAG, OpenCVersion + " -> " + ActivePackagePath);

                if (null != ActivePackagePath)
                {
                    int start = ActivePackagePath.indexOf(mInstalledPackageInfo[i].packageName);
                    int stop = start + mInstalledPackageInfo[i].packageName.length();
                    if (start >= 0 && ActivePackagePath.charAt(stop) == '/')
                    {
                        temp.put("Activity", "y");
                        Tags = "active";
                    }
                    else
                    {
                        temp.put("Activity", "n");
                        if (!PublicName.equals("Built-in OpenCV library"))
                            Tags = "safe to remove";
                    }
                }
                else
                {
                    temp.put("Activity", "n");
                }

                temp.put("Version", NormalizeVersion(OpenCVersion, VersionName));
                // HACK: OpenCV Manager for Armv7-a Neon already has Tegra3 optimizations
                // that is enabled on proper hardware
                if (HardwareDetector.DetectKnownPlatforms() == HardwareDetector.PLATFORM_TEGRA3 &&
                  HardwareName.equals("armv7a neon ") &&  Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD)
                {
                    temp.put("Hardware", "Tegra 3");
                    if (Tags == null)
                    {
                        Tags = "optimized";
                    }
                    else
                    {
                        Tags = Tags + ", optimized";
                    }
                }
                else
                {
                    temp.put("Hardware", HardwareName);
                }

                if (Tags != null)
                    PublicName = PublicName + " (" + Tags + ")";

                temp.put("Name", PublicName);

                mListViewItems.add(temp);
            }

            mInstalledPacksAdapter.notifyDataSetChanged();
        }
    }

    protected String NormalizeVersion(String OpenCVersion, String PackageVersion)
    {
        int dot = PackageVersion.indexOf(".");
        return OpenCVersion.substring(0,  OpenCVersion.length()-1) + "." +
            OpenCVersion.toCharArray()[OpenCVersion.length()-1] + "." +
            PackageVersion.substring(0, dot) + " rev " + PackageVersion.substring(dot+1);
    }

    protected String ConvertPackageName(String Name, String Version)
    {
        return Name + " rev " + Version;
    }

    protected String JoinIntelFeatures(int features)
    {
        // TODO: update if package will be published
        return "";
    }
<commit_msg>OpenCV Manager crashes with exception (Bug #2660)<commit_after>package org.opencv.engine.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;

import org.opencv.engine.HardwareDetector;
import org.opencv.engine.MarketConnector;
import org.opencv.engine.OpenCVEngineInterface;
import org.opencv.engine.OpenCVLibraryInfo;
import org.opencv.engine.R;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ManagerActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView OsVersionView = (TextView)findViewById(R.id.OsVersionValue);
        OsVersionView.setText(Build.VERSION.CODENAME + " (" + Build.VERSION.RELEASE + "), API " + Build.VERSION.SDK_INT);

        try {
            PackageInfo packageInfo = getPackageManager().getPackageInfo(this.getPackageName(), 0);
            ManagerVersion = packageInfo.versionName;
        } catch (NameNotFoundException e) {
            ManagerVersion = "N/A";
            e.printStackTrace();
        }

        mInstalledPackageView = (ListView)findViewById(R.id.InstalledPackageList);

        mMarket = new MarketConnector(this);

        mInstalledPacksAdapter = new PackageListAdapter(
                this,
                mListViewItems,
                R.layout.info,
                new String[] {"Name", "Version", "Hardware", "Activity"},
                new int[] {R.id.InfoName,R.id.InfoVersion, R.id.InfoHardware}
                );

        mInstalledPackageView.setAdapter(mInstalledPacksAdapter);

        TextView HardwarePlatformView = (TextView)findViewById(R.id.HardwareValue);
        int Platfrom = HardwareDetector.DetectKnownPlatforms();
        int CpuId = HardwareDetector.GetCpuID();

        if (HardwareDetector.PLATFORM_UNKNOWN != Platfrom)
        {
            if (HardwareDetector.PLATFORM_TEGRA == Platfrom)
            {
                HardwarePlatformView.setText("Tegra");
            }
            else if (HardwareDetector.PLATFORM_TEGRA2 == Platfrom)
            {
                HardwarePlatformView.setText("Tegra 2");
            }
            else
            {
                HardwarePlatformView.setText("Tegra 3");
            }
        }
        else
        {
            if ((CpuId & HardwareDetector.ARCH_X86) == HardwareDetector.ARCH_X86)
            {
                HardwarePlatformView.setText("x86 " + JoinIntelFeatures(CpuId));
            }
            else if ((CpuId & HardwareDetector.ARCH_X64) == HardwareDetector.ARCH_X64)
            {
                HardwarePlatformView.setText("x64 " + JoinIntelFeatures(CpuId));
            }
            else if ((CpuId & HardwareDetector.ARCH_ARMv5) == HardwareDetector.ARCH_ARMv5)
            {
                HardwarePlatformView.setText("ARM v5 " + JoinArmFeatures(CpuId));
            }
            else if ((CpuId & HardwareDetector.ARCH_ARMv6) == HardwareDetector.ARCH_ARMv6)
            {
                HardwarePlatformView.setText("ARM v6 " + JoinArmFeatures(CpuId));
            }
            else if ((CpuId & HardwareDetector.ARCH_ARMv7) == HardwareDetector.ARCH_ARMv7)
            {
                HardwarePlatformView.setText("ARM v7 " + JoinArmFeatures(CpuId));
            }
            else if ((CpuId & HardwareDetector.ARCH_ARMv8) == HardwareDetector.ARCH_ARMv8)
            {
                HardwarePlatformView.setText("ARM v8 " + JoinArmFeatures(CpuId));
            }
            else if ((CpuId & HardwareDetector.ARCH_MIPS) == HardwareDetector.ARCH_MIPS)
            {
                HardwarePlatformView.setText("MIPS");
            }
            else
            {
                HardwarePlatformView.setText("not detected");
            }
        }

        mUpdateEngineButton = (Button)findViewById(R.id.CheckEngineUpdate);
        mUpdateEngineButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                if (!mMarket.InstallAppFromMarket("org.opencv.engine"))
                {
                    Toast toast = Toast.makeText(getApplicationContext(), "Google Play is not avaliable", Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        });

        mActionDialog = new AlertDialog.Builder(this).create();

        mActionDialog.setTitle("Choose action");
        mActionDialog.setButton("Update", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                int index = (Integer)mInstalledPackageView.getTag();
                if (!mMarket.InstallAppFromMarket(mInstalledPackageInfo[index].packageName))
                {
                    Toast toast = Toast.makeText(getApplicationContext(), "Google Play is not avaliable", Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        });

        mActionDialog.setButton3("Remove", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                int index = (Integer)mInstalledPackageView.getTag();
                if (!mMarket.RemoveAppFromMarket(mInstalledPackageInfo[index].packageName, true))
                {
                    Toast toast = Toast.makeText(getApplicationContext(), "Google Play is not avaliable", Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        });

        mActionDialog.setButton2("Return", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // nothing
            }
        });

        mInstalledPackageView.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
                //if (!mListViewItems.get((int) id).get("Name").equals("Built-in OpenCV library"));
                if (!mInstalledPackageInfo[(int) id].packageName.equals("org.opencv.engine"))
                {
                    mInstalledPackageView.setTag(Integer.valueOf((int)id));
                    mActionDialog.show();
                }
            }
        });

        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_PACKAGE_ADDED);
        filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
        filter.addAction(Intent.ACTION_PACKAGE_INSTALL);
        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        filter.addAction(Intent.ACTION_PACKAGE_REPLACED);

        registerReceiver(mPackageChangeReciever, filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mPackageChangeReciever);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "Filling package list on resume");
        if (!bindService(new Intent("org.opencv.engine.BIND"), new OpenCVEngineServiceConnection(), Context.BIND_AUTO_CREATE))
        {
            TextView EngineVersionView = (TextView)findViewById(R.id.EngineVersionValue);
            EngineVersionView.setText("not avaliable");
        }
    }

    protected SimpleAdapter mInstalledPacksAdapter;
    protected ListView mInstalledPackageView;
    protected Button mUpdateEngineButton;
    protected PackageInfo[] mInstalledPackageInfo;
    protected final ArrayList<HashMap<String,String>> mListViewItems = new ArrayList<HashMap<String,String>>();
    protected static final String TAG = "OpenCV_Manager/Activity";
    protected MarketConnector mMarket;
    protected AlertDialog mActionDialog;
    protected HashMap<String,String> mActivePackageMap = new HashMap<String, String>();
    protected int ManagerApiLevel = 0;
    protected String ManagerVersion;

    protected BroadcastReceiver mPackageChangeReciever = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("OpenCVManager/Reciever", "Bradcast message " + intent.getAction() + " reciever");
            Log.d("OpenCVManager/Reciever", "Filling package list on broadcast message");
            if (!bindService(new Intent("org.opencv.engine.BIND"), new OpenCVEngineServiceConnection(), Context.BIND_AUTO_CREATE))
            {
                TextView EngineVersionView = (TextView)findViewById(R.id.EngineVersionValue);
                EngineVersionView.setText("not avaliable");
            }
        }
    };

    protected class OpenCVEngineServiceConnection implements ServiceConnection
    {
        public void onServiceDisconnected(ComponentName name) {
        }

        public void onServiceConnected(ComponentName name, IBinder service) {
            OpenCVEngineInterface EngineService = OpenCVEngineInterface.Stub.asInterface(service);
            try {
                ManagerApiLevel = EngineService.getEngineVersion();
            } catch (RemoteException e) {
                e.printStackTrace();
            }

            TextView EngineVersionView = (TextView)findViewById(R.id.EngineVersionValue);
            EngineVersionView.setText(ManagerVersion);

            try {
                String path = EngineService.getLibPathByVersion("2.4");
                Log.d(TAG, "2.4 -> " + path);
                mActivePackageMap.put("24", path);
                path = EngineService.getLibPathByVersion("2.5");
                Log.d(TAG, "2.5 -> " + path);
                mActivePackageMap.put("25", path);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Log.d(TAG, "Filling package list on service connection");
            FillPackageList();

            unbindService(this);
        }
    };

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    synchronized protected void FillPackageList()
    {
        synchronized (mListViewItems) {
            mInstalledPackageInfo = mMarket.GetInstalledOpenCVPackages();
            mListViewItems.clear();

            int RealPackageCount = mInstalledPackageInfo.length;
            for (int i = 0; i < RealPackageCount; i++)
            {
                if (mInstalledPackageInfo[i] == null)
                    break;

                // Convert to Items for package list view
                HashMap<String,String> temp = new HashMap<String,String>();

                String HardwareName = "";
                String NativeLibDir = "";
                String OpenCVersion = "";

                String PublicName = mMarket.GetApplicationName(mInstalledPackageInfo[i].applicationInfo);
                String PackageName = mInstalledPackageInfo[i].packageName;
                String VersionName = mInstalledPackageInfo[i].versionName;

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD)
                    NativeLibDir = mInstalledPackageInfo[i].applicationInfo.nativeLibraryDir;
                else
                    NativeLibDir = "/data/data/" + mInstalledPackageInfo[i].packageName + "/lib";

                OpenCVLibraryInfo NativeInfo = new OpenCVLibraryInfo(NativeLibDir);

                if (PackageName.equals("org.opencv.engine"))
                {
                    if (NativeInfo.status())
                    {
                        PublicName = "Built-in OpenCV library";
                        PackageName = NativeInfo.packageName();
                        VersionName = NativeInfo.versionName();
                    }
                    else
                    {
                        mInstalledPackageInfo[i] = mInstalledPackageInfo[RealPackageCount-1];
                        mInstalledPackageInfo[RealPackageCount-1] = null;
                        RealPackageCount--;
                        i--;
                        continue;
                    }
                }

                int idx = 0;
                Log.d(TAG, PackageName);
                StringTokenizer tokenizer = new StringTokenizer(PackageName, "_");
                while (tokenizer.hasMoreTokens())
                {
                    if (idx == 1)
                    {
                        // version of OpenCV
                        OpenCVersion = tokenizer.nextToken().substring(1);
                    }
                    else if (idx >= 2)
                    {
                        // hardware options
                        HardwareName += tokenizer.nextToken() + " ";
                    }
                    else
                    {
                        tokenizer.nextToken();
                    }
                    idx++;
                }

                String ActivePackagePath;
                String Tags = null;
                ActivePackagePath = mActivePackageMap.get(OpenCVersion);
                Log.d(TAG, OpenCVersion + " -> " + ActivePackagePath);

                if (null != ActivePackagePath)
                {
                    int start = ActivePackagePath.indexOf(mInstalledPackageInfo[i].packageName);
                    int stop = start + mInstalledPackageInfo[i].packageName.length();
                    if (start >= 0 && ActivePackagePath.charAt(stop) == '/')
                    {
                        temp.put("Activity", "y");
                        Tags = "active";
                    }
                    else
                    {
                        temp.put("Activity", "n");
                        if (!PublicName.equals("Built-in OpenCV library"))
                            Tags = "safe to remove";
                    }
                }
                else
                {
                    temp.put("Activity", "n");
                }

                temp.put("Version", NormalizeVersion(OpenCVersion, VersionName));
                // HACK: OpenCV Manager for Armv7-a Neon already has Tegra3 optimizations
                // that is enabled on proper hardware
                if (HardwareDetector.DetectKnownPlatforms() == HardwareDetector.PLATFORM_TEGRA3 &&
                  HardwareName.equals("armv7a neon ") &&  Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD)
                {
                    temp.put("Hardware", "Tegra 3");
                    if (Tags == null)
                    {
                        Tags = "optimized";
                    }
                    else
                    {
                        Tags = Tags + ", optimized";
                    }
                }
                else
                {
                    temp.put("Hardware", HardwareName);
                }

                if (Tags != null)
                    PublicName = PublicName + " (" + Tags + ")";

                temp.put("Name", PublicName);

                mListViewItems.add(temp);
            }

            mInstalledPacksAdapter.notifyDataSetChanged();
        }
    }

    protected String NormalizeVersion(String OpenCVersion, String PackageVersion)
    {
        if (OpenCVersion == null || PackageVersion == null)
            return "unknown";

        int dot = PackageVersion.indexOf(".");
        if (dot == -1 || OpenCVersion.length() == 0)
            return "unknown";
        else
            return OpenCVersion.substring(0,  OpenCVersion.length()-1) + "." +
                OpenCVersion.toCharArray()[OpenCVersion.length()-1] + "." +
                PackageVersion.substring(0, dot) + " rev " + PackageVersion.substring(dot+1);
    }

    protected String ConvertPackageName(String Name, String Version)
    {
        return Name + " rev " + Version;
    }

    protected String JoinIntelFeatures(int features)
    {
        // TODO: update if package will be published
        return "";
    }

{'fraction_non_alphanumeric': 0.09497813301161212, 'fraction_numerical': 0.0038606545015834716, 'mean_word_length': 1.8389416902132032, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 35, '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': '3438229', 'n_tokens_mistral': 8690, 'n_tokens_neox': 8096, 'n_words': 1505}