<commit_before>/*
 * Copyright 2017 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

package ee.ria.DigiDoc.adapter;

import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import org.spongycastle.asn1.ASN1ObjectIdentifier;
import org.spongycastle.asn1.x500.style.BCStyle;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import ee.ria.DigiDoc.R;
import ee.ria.DigiDoc.certificate.X509Cert;
import ee.ria.DigiDoc.container.ContainerBuilder;
import ee.ria.DigiDoc.container.ContainerFacade;
import ee.ria.DigiDoc.container.SignatureFacade;
import ee.ria.DigiDoc.fragment.ContainerSignaturesFragment;
import ee.ria.DigiDoc.util.DateUtils;
import ee.ria.DigiDoc.util.NotificationUtil;

public class SignatureAdapter extends ArrayAdapter<SignatureFacade> implements Filterable {

    private ContainerFacade containerFacade;
    private ContainerSignaturesFragment containerSignaturesFragment;

    private NotificationUtil notificationUtil;
    private Activity activity;

    static class ViewHolder {
        @BindView(R.id.personName) TextView name;
        @BindView(R.id.fileSize) TextView signed;
        @BindView(R.id.removeSignature) ImageView removeSignature;
        @BindView(R.id.isSigned) TextView isSigned;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public SignatureAdapter(Activity activity, ContainerFacade containerFacade, ContainerSignaturesFragment containerSignaturesFragment) {
        super(activity, 0, containerFacade.getSignatures());
        this.activity = activity;
        this.containerFacade = containerFacade;
        this.containerSignaturesFragment = containerSignaturesFragment;
    }

    public void updateContainerFile(File containerFile) {
        containerFacade = ContainerBuilder.aContainer(getContext()).fromExistingContainer(containerFile).build();
    }

    @NonNull
    @Override
    public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
        notificationUtil = new NotificationUtil(activity);
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_container_signatures, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        SignatureFacade signatureFacade = getItem(position);

        if (signatureFacade != null) {
            X509Cert x509Cert = new X509Cert(signatureFacade.getSigningCertificateDer());

            String surname = x509Cert.getValueByObjectIdentifier(ASN1ObjectIdentifier.getInstance(BCStyle.SURNAME));
            String name = x509Cert.getValueByObjectIdentifier(ASN1ObjectIdentifier.getInstance(BCStyle.GIVENNAME));
            String serialNumber = x509Cert.getValueByObjectIdentifier(ASN1ObjectIdentifier.getInstance(BCStyle.SERIALNUMBER));

            String personInfo;
            if ((surname != null && !surname.isEmpty()) && (name != null && !name.isEmpty())) {
                personInfo = String.format("%s %s (%s)", name, surname, serialNumber);
            } else {
                String commonName = x509Cert.getValueByObjectIdentifier(ASN1ObjectIdentifier.getInstance(BCStyle.CN));
                personInfo = String.format("%s (%s)", commonName, serialNumber);
            }

            viewHolder.name.setText(personInfo);
            viewHolder.signed.setText(DateUtils.formatSignedDate(signatureFacade.getTrustedSigningTime()));

            if (signatureFacade.isSignatureValid()) {
                viewHolder.isSigned.setText(getContext().getText(R.string.signature_valid));
                viewHolder.isSigned.setTextColor(Color.rgb(80, 155, 0));
            } else {
                viewHolder.isSigned.setText(getContext().getText(R.string.signature_invalid));
                viewHolder.isSigned.setTextColor(Color.RED);
            }
            viewHolder.removeSignature.setOnClickListener(new RemoveSignatureListener(position, personInfo));
        }
        return convertView;
    }

    private class RemoveSignatureListener implements View.OnClickListener {

        private int position;<commit_msg>Only display the serial if it exists<commit_after>/*
 * Copyright 2017 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

package ee.ria.DigiDoc.adapter;

import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import org.spongycastle.asn1.ASN1ObjectIdentifier;
import org.spongycastle.asn1.x500.style.BCStyle;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import ee.ria.DigiDoc.R;
import ee.ria.DigiDoc.certificate.X509Cert;
import ee.ria.DigiDoc.container.ContainerBuilder;
import ee.ria.DigiDoc.container.ContainerFacade;
import ee.ria.DigiDoc.container.SignatureFacade;
import ee.ria.DigiDoc.fragment.ContainerSignaturesFragment;
import ee.ria.DigiDoc.util.DateUtils;
import ee.ria.DigiDoc.util.NotificationUtil;

public class SignatureAdapter extends ArrayAdapter<SignatureFacade> implements Filterable {

    private ContainerFacade containerFacade;
    private ContainerSignaturesFragment containerSignaturesFragment;

    private NotificationUtil notificationUtil;
    private Activity activity;

    static class ViewHolder {
        @BindView(R.id.personName) TextView name;
        @BindView(R.id.fileSize) TextView signed;
        @BindView(R.id.removeSignature) ImageView removeSignature;
        @BindView(R.id.isSigned) TextView isSigned;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public SignatureAdapter(Activity activity, ContainerFacade containerFacade, ContainerSignaturesFragment containerSignaturesFragment) {
        super(activity, 0, containerFacade.getSignatures());
        this.activity = activity;
        this.containerFacade = containerFacade;
        this.containerSignaturesFragment = containerSignaturesFragment;
    }

    public void updateContainerFile(File containerFile) {
        containerFacade = ContainerBuilder.aContainer(getContext()).fromExistingContainer(containerFile).build();
    }

    @NonNull
    @Override
    public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
        notificationUtil = new NotificationUtil(activity);
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_container_signatures, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        SignatureFacade signatureFacade = getItem(position);

        if (signatureFacade != null) {
            X509Cert x509Cert = new X509Cert(signatureFacade.getSigningCertificateDer());

            String surname = x509Cert.getValueByObjectIdentifier(ASN1ObjectIdentifier.getInstance(BCStyle.SURNAME));
            String name = x509Cert.getValueByObjectIdentifier(ASN1ObjectIdentifier.getInstance(BCStyle.GIVENNAME));
            String serialNumber = x509Cert.getValueByObjectIdentifier(ASN1ObjectIdentifier.getInstance(BCStyle.SERIALNUMBER));

            String personInfo;
            if ((surname != null && !surname.isEmpty()) && (name != null && !name.isEmpty())) {
                if (serialNumber != null) {
                    personInfo = String.format("%s %s (%s)", name, surname, serialNumber);
                } else {
                    personInfo = String.format("%s %s", name, surname);
                }
            } else {
                String commonName = x509Cert.getValueByObjectIdentifier(ASN1ObjectIdentifier.getInstance(BCStyle.CN));

                if (serialNumber != null) {
                    personInfo = String.format("%s (%s)", commonName, serialNumber);
                } else {
                    personInfo = String.format("%s", commonName);
                }
            }

            viewHolder.name.setText(personInfo);
            viewHolder.signed.setText(DateUtils.formatSignedDate(signatureFacade.getTrustedSigningTime()));

            if (signatureFacade.isSignatureValid()) {
                viewHolder.isSigned.setText(getContext().getText(R.string.signature_valid));
                viewHolder.isSigned.setTextColor(Color.rgb(80, 155, 0));
            } else {
                viewHolder.isSigned.setText(getContext().getText(R.string.signature_invalid));
                viewHolder.isSigned.setTextColor(Color.RED);
            }
            viewHolder.removeSignature.setOnClickListener(new RemoveSignatureListener(position, personInfo));
        }
        return convertView;
    }

    private class RemoveSignatureListener implements View.OnClickListener {

        private int position;
{'fraction_non_alphanumeric': 0.08740499457111835, 'fraction_numerical': 0.010676800579080709, 'mean_word_length': 3.442524115755627, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '3018543', 'n_tokens_mistral': 2986, 'n_tokens_neox': 2836, 'n_words': 685}