<commit_before>package org.adaptlab.chpir.android.participanttracker;

import org.adaptlab.chpir.android.participanttracker.Receivers.InstrumentListReceiver;
import org.adaptlab.chpir.android.participanttracker.models.Participant;
import org.adaptlab.chpir.android.participanttracker.models.ParticipantProperty;
import org.adaptlab.chpir.android.participanttracker.models.Relationship;
import org.json.JSONException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ParticipantDetailFragment extends Fragment {
    private static final String TAG = "ParticipantDetailFragment";
    public final static String EXTRA_PARTICIPANT_ID = 
            "org.adaptlab.chpir.participanttracker.participantdetailfragment.participant_id";
    public final static String EXTRA_PARTICIPANT_METADATA =
            "org.adaptlab.chpir.android.survey.metadata";
    
    private Participant mParticipant;
    private static String sParticipantMetadata;
    private LinearLayout mParticipantPropertiesContainer;
    private Button mNewSurveyButton;
    private static Activity sActivity;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sActivity = getActivity();
        
        if (savedInstanceState != null) {
            mParticipant = Participant.findById(savedInstanceState.getLong(EXTRA_PARTICIPANT_ID));
        } else {
            Long participantId = getActivity().getIntent().getLongExtra(EXTRA_PARTICIPANT_ID, -1);
            if (participantId == -1) return;

            mParticipant = Participant.findById(participantId);
        }
        
        try {
            sParticipantMetadata = mParticipant.getMetadata();
        } catch (JSONException e) {
            Log.e(TAG, "Could not parse participant metadata for " + mParticipant.getId());
        }  
        
        getActivity().setTitle(mParticipant.getLabel());
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
            Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        View v = inflater.inflate(R.layout.fragment_participant_detail, parent,
                false);       
        
        mParticipantPropertiesContainer = (LinearLayout) v.findViewById(R.id.participant_properties_container);

        for (ParticipantProperty participantProperty : mParticipant.getParticipantProperties()) {
            addKeyValueLabel(participantProperty.getProperty().getLabel(), participantProperty.getValue());
        }

        for (Relationship relationship : mParticipant.getRelationships()) {
            addKeyValueLabel(relationship.getRelationshipType().getLabel(), relationship.getParticipantRelated().getLabel());
        }
        
        addKeyValueLabel("UUID", mParticipant.getUUID());
          
        return v;
    }
    
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.participant_detail, menu);
        super.onCreateOptionsMenu(menu,inflater);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_new:
                newSurvey();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    public static void displayInstrumentPicker(String[] instrumentTitleList, final long[] instrumentIdList) {
        AlertDialog.Builder builder = new AlertDialog.Builder(sActivity);
        builder.setTitle(sActivity.getString(R.string.choose_instrument));
        builder.setSingleChoiceItems(instrumentTitleList, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {             <commit_msg>Show button to view participant details for relationships<commit_after>package org.adaptlab.chpir.android.participanttracker;

import org.adaptlab.chpir.android.participanttracker.Receivers.InstrumentListReceiver;
import org.adaptlab.chpir.android.participanttracker.models.Participant;
import org.adaptlab.chpir.android.participanttracker.models.ParticipantProperty;
import org.adaptlab.chpir.android.participanttracker.models.Relationship;
import org.json.JSONException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ParticipantDetailFragment extends Fragment {
    private static final String TAG = "ParticipantDetailFragment";
    public final static String EXTRA_PARTICIPANT_ID = 
            "org.adaptlab.chpir.participanttracker.participantdetailfragment.participant_id";
    public final static String EXTRA_PARTICIPANT_METADATA =
            "org.adaptlab.chpir.android.survey.metadata";
    
    private Participant mParticipant;
    private static String sParticipantMetadata;
    private LinearLayout mParticipantPropertiesContainer;
    private Button mNewSurveyButton;
    private static Activity sActivity;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sActivity = getActivity();
        
        if (savedInstanceState != null) {
            mParticipant = Participant.findById(savedInstanceState.getLong(EXTRA_PARTICIPANT_ID));
        } else {
            Long participantId = getActivity().getIntent().getLongExtra(EXTRA_PARTICIPANT_ID, -1);
            if (participantId == -1) return;

            mParticipant = Participant.findById(participantId);
        }
        
        try {
            sParticipantMetadata = mParticipant.getMetadata();
        } catch (JSONException e) {
            Log.e(TAG, "Could not parse participant metadata for " + mParticipant.getId());
        }  
        
        getActivity().setTitle(mParticipant.getLabel());
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
            Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        View v = inflater.inflate(R.layout.fragment_participant_detail, parent,
                false);       
        
        mParticipantPropertiesContainer = (LinearLayout) v.findViewById(R.id.participant_properties_container);

        for (ParticipantProperty participantProperty : mParticipant.getParticipantProperties()) {
            addKeyValueLabel(participantProperty.getProperty().getLabel(), participantProperty.getValue());
        }

        for (final Relationship relationship : mParticipant.getRelationships()) {
            TextView textView = new TextView(getActivity());
            textView.setTextAppearance(getActivity(), R.style.sectionHeader);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(0, 25, 0, 0);
            textView.setLayoutParams(layoutParams);
            mParticipantPropertiesContainer.addView(textView);
            textView.setText(relationship.getRelationshipType().getLabel());
            
            Button button = new Button(getActivity());
            button.setText(relationship.getParticipantRelated().getLabel());
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Intent i = new Intent(getActivity(), ParticipantDetailActivity.class);
                    i.putExtra(ParticipantDetailFragment.EXTRA_PARTICIPANT_ID, relationship.getParticipantRelated().getId());
                    startActivity(i);
                }
            });
            mParticipantPropertiesContainer.addView(button);
        }
        
        addKeyValueLabel("UUID", mParticipant.getUUID());
          
        return v;
    }
    
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.participant_detail, menu);
        super.onCreateOptionsMenu(menu,inflater);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_new:
                newSurvey();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    public static void displayInstrumentPicker(String[] instrumentTitleList, final long[] instrumentIdList) {
        AlertDialog.Builder builder = new AlertDialog.Builder(sActivity);
        builder.setTitle(sActivity.getString(R.string.choose_instrument));
        builder.setSingleChoiceItems(instrumentTitleList, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {             
{'fraction_non_alphanumeric': 0.08436047686401293, 'fraction_numerical': 0.0013133966457870277, 'mean_word_length': 3.1574968500629987, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6723767', 'n_tokens_mistral': 2682, 'n_tokens_neox': 2492, 'n_words': 469}