#!/usr/bin/python
# -*- coding: utf-8 -*-

from pychrysalide import PluginModule
import zipfile


class ApkFiles(PluginModule):
    """Open and process APK files."""


    def get_interface(self):
        """Provide the full plugin description."""

        desc = {

            'name' : 'Welcome',
            'desc' : 'Add suppport for the APK file format',
            'version' : '0.1',

            'actions' : [ PluginModule.PGA_PLUGIN_INIT ]

        }

        return desc


    def init(self, ref):
        """Initialise l'extension."""

        return True


    def get_action(self):
        """Register the plugin for given actions."""

        return Plugin.PGA_FORMAT_MATCHER


    def is_matching(self, filename, data):
        """Define if the given file can be handled."""

        if not zipfile.is_zipfile(filename):
            return Plugin.MFA_NONE, None, None

        zf = zipfile.ZipFile(filename)

        if zf.namelist().count('classes.dex') > 0 \
                and zf.namelist().count('AndroidManifest.xml') > 0:

            f = zf.open('classes.dex', 'r')
            data = f.read()
            f.closed

            return Plugin.MFA_RELOAD, None, bytearray(data)

        else:
            return Plugin.MFA_NONE, None, None