<commit_before>package org.msf.records.ui;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.Filter;
import android.widget.ListView;

import org.msf.records.App;
import org.msf.records.R;
import org.msf.records.events.CreatePatientSucceededEvent;
import org.msf.records.events.sync.SyncFinishedEvent;
import org.msf.records.filter.FilterGroup;
import org.msf.records.filter.FilterManager;
import org.msf.records.filter.FilterQueryProviderFactory;
import org.msf.records.filter.LocationUuidFilter;
import org.msf.records.filter.SimpleSelectionFilter;
import org.msf.records.model.Location;
import org.msf.records.model.LocationTree;
import org.msf.records.net.Constants;
import org.msf.records.sync.GenericAccountService;
import org.msf.records.sync.PatientProjection;
import org.msf.records.sync.PatientProviderContract;
import org.msf.records.sync.SyncManager;

import de.greenrobot.event.EventBus;

/**
 * A list fragment representing a list of Patients. This fragment
 * also supports tablet devices by allowing list items to be given an
 * 'activated' state upon selection. This helps indicate which item is
 * currently being viewed in a {@link PatientDetailFragment}.
 * <p>
 * Activities containing this fragment MUST implement the {@link Callbacks}
 * interface.
 */
public class PatientListFragment extends ProgressFragment implements
        ExpandableListView.OnChildClickListener, LoaderManager.LoaderCallbacks<Cursor>,
        SwipeRefreshLayout.OnRefreshListener {

    private static final String TAG = PatientListFragment.class.getSimpleName();
    private static final String ITEM_LIST_KEY = "ITEM_LIST_KEY";

    /**
     * The serialization (saved instance state) Bundle key representing the
     * activated item position. Only used on tablets.
     */
    private static final String STATE_ACTIVATED_POSITION = "activated_position";

    /**
     * The id to identify which Cursor is returned
     */
    private static final int LOADER_LIST_ID = 0;

    /**
     * The fragment's current callback object, which is notified of list item
     * clicks.
     */
    private Callbacks mCallbacks = sDummyCallbacks;

    /**
     * The current activated item position. Only used on tablets.
     */
    private int mActivatedPosition = ListView.INVALID_POSITION;

    private ExpandablePatientListAdapter mPatientAdapter;

    private ExpandableListView mListView;

    private SwipeRefreshLayout mSwipeToRefresh;

    // TODO(akalachman): Figure out how to break reliance on this cursor--we already have the info.
    private FilterQueryProviderFactory mFactory =
            new FilterQueryProviderFactory().setUri(
                    PatientProviderContract.CONTENT_URI_TENT_PATIENT_COUNTS)
            .setSortClause(LocationTree.getLocationSortClause(
                    PatientProviderContract.PatientColumns.COLUMN_NAME_LOCATION_UUID));


    private boolean isRefreshing;

    String mFilterQueryTerm = "";

    SimpleSelectionFilter mFilter;

    public void filterBy(SimpleSelectionFilter filter) {
        // Tack on a location filter to the filter to show only known locations.
        mFilter = new FilterGroup(
                new LocationUuidFilter(
                        LocationTree.getRootLocation(getActivity()).getLocation().uuid), filter);
        mPatientAdapter.setSelectionFilter(mFilter);
        mPatientAdapter.setFilterQueryProvider(
                mFactory.getFilterQueryProvider(getActivity(), filter));
        loadSearchResults();
    }

    /**
     * A callback interface that all activities containing this fragment must
     * implement. This mechanism allows activities to be notified of item
     * selections.
     */
    public interface Callbacks {
        /**
         * Callback for when an item has been selected.
         */
        public void onItemSelected(String uuid, String givenName, String familyName, String id);
    }

    /**
     * A dummy implementation of the {@link Callbacks} interface that does
     * nothing. Used only when this fragment is not attached to an activity.
     */
    private static Callbacks sDummyCallbacks = new Callbacks() {
        @Override
        public void onItemSelected(String uuid, String givenName, String familyName, String id) {
        }
    };<commit_msg>Fixed NPE on patient tree load<commit_after>package org.msf.records.ui;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.Filter;
import android.widget.ListView;

import org.msf.records.App;
import org.msf.records.R;
import org.msf.records.events.CreatePatientSucceededEvent;
import org.msf.records.events.sync.SyncFinishedEvent;
import org.msf.records.filter.FilterGroup;
import org.msf.records.filter.FilterManager;
import org.msf.records.filter.FilterQueryProviderFactory;
import org.msf.records.filter.LocationUuidFilter;
import org.msf.records.filter.SimpleSelectionFilter;
import org.msf.records.model.Location;
import org.msf.records.model.LocationTree;
import org.msf.records.net.Constants;
import org.msf.records.sync.GenericAccountService;
import org.msf.records.sync.PatientProjection;
import org.msf.records.sync.PatientProviderContract;
import org.msf.records.sync.SyncManager;

import de.greenrobot.event.EventBus;

/**
 * A list fragment representing a list of Patients. This fragment
 * also supports tablet devices by allowing list items to be given an
 * 'activated' state upon selection. This helps indicate which item is
 * currently being viewed in a {@link PatientDetailFragment}.
 * <p>
 * Activities containing this fragment MUST implement the {@link Callbacks}
 * interface.
 */
public class PatientListFragment extends ProgressFragment implements
        ExpandableListView.OnChildClickListener, LoaderManager.LoaderCallbacks<Cursor>,
        SwipeRefreshLayout.OnRefreshListener {

    private static final String TAG = PatientListFragment.class.getSimpleName();
    private static final String ITEM_LIST_KEY = "ITEM_LIST_KEY";

    /**
     * The serialization (saved instance state) Bundle key representing the
     * activated item position. Only used on tablets.
     */
    private static final String STATE_ACTIVATED_POSITION = "activated_position";

    /**
     * The id to identify which Cursor is returned
     */
    private static final int LOADER_LIST_ID = 0;

    /**
     * The fragment's current callback object, which is notified of list item
     * clicks.
     */
    private Callbacks mCallbacks = sDummyCallbacks;

    /**
     * The current activated item position. Only used on tablets.
     */
    private int mActivatedPosition = ListView.INVALID_POSITION;

    private ExpandablePatientListAdapter mPatientAdapter;

    private ExpandableListView mListView;

    private SwipeRefreshLayout mSwipeToRefresh;

    // TODO(akalachman): Figure out how to break reliance on this cursor--we already have the info.
    private FilterQueryProviderFactory mFactory =
            new FilterQueryProviderFactory().setUri(
                    PatientProviderContract.CONTENT_URI_TENT_PATIENT_COUNTS)
            .setSortClause(LocationTree.getLocationSortClause(
                    PatientProviderContract.PatientColumns.COLUMN_NAME_LOCATION_UUID));


    private boolean isRefreshing;

    String mFilterQueryTerm = "";

    SimpleSelectionFilter mFilter;

    public void filterBy(SimpleSelectionFilter filter) {
        // Tack on a location filter to the filter to show only known locations.
        LocationTree tree = LocationTree.getRootLocation(getActivity());
        if (tree == null || tree.getLocation() == null) {
            mFilter = filter;
        } else {
            // Tack on a location filter to the filter to show only known locations.
            mFilter = new FilterGroup(
                    new LocationUuidFilter(
                            LocationTree.getRootLocation(getActivity()).getLocation().uuid), filter);
        }

        mPatientAdapter.setSelectionFilter(mFilter);
        mPatientAdapter.setFilterQueryProvider(
                mFactory.getFilterQueryProvider(getActivity(), filter));
        loadSearchResults();
    }

    /**
     * A callback interface that all activities containing this fragment must
     * implement. This mechanism allows activities to be notified of item
     * selections.
     */
    public interface Callbacks {
        /**
         * Callback for when an item has been selected.
         */
        public void onItemSelected(String uuid, String givenName, String familyName, String id);
    }

    /**
     * A dummy implementation of the {@link Callbacks} interface that does
     * nothing. Used only when this fragment is not attached to an activity.
     */
    private static Callbacks sDummyCallbacks = new Callbacks() {
        @Override
        public void onItemSelected(String uuid, String givenName, String familyName, String id) {
        }
    };
{'fraction_non_alphanumeric': 0.07602461153404944, 'fraction_numerical': 0.0010428616122640526, 'mean_word_length': 3.4855004677268475, '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': '5394436', 'n_tokens_mistral': 2558, 'n_tokens_neox': 2447, 'n_words': 721}