<commit_before>package com.pennapps.labs.pennmobile;

import android.Manifest;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.OperationApplicationException;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ListFragment;
import android.support.v4.content.ContextCompat;
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.Toast;

import com.pennapps.labs.pennmobile.adapters.PhoneSaveAdapter;
import com.pennapps.labs.pennmobile.classes.Person;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SaveContactsFragment extends ListFragment {

    private MainActivity mActivity;
    private List<Person> contacts_list;
    private List<Person> selected;
    private Set<String> current_numbers;
    private Set<String> current_names;
    private final int permission_read = 0;

    @Override
    public void onActivityCreated(Bundle save) {
        super.onActivityCreated(save);
        mActivity = (MainActivity) getActivity();
        mActivity.closeKeyboard();
        loadData();
        getListView().setAdapter(new PhoneSaveAdapter(getActivity(), contacts_list, selected));
        loadCurrent();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle save) {
        View view = inflater.inflate(R.layout.fragment_save_contacts, container, false);
        setHasOptionsMenu(true);
        current_numbers = new HashSet<>();
        current_names = new HashSet<>();
        selected = new ArrayList<>();
        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        mActivity.getMenuInflater().inflate(R.menu.phone_save_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.support_contacts_add) {
            if (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.WRITE_CONTACTS)
                    != PackageManager.PERMISSION_GRANTED ||
                    ContextCompat.checkSelfPermission(mActivity, Manifest.permission.READ_CONTACTS)
                            != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(mActivity,
                        new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS},
                        permission_read);
            } else {
                loadCurrent();
                addContacts();
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onResume() {
        super.onResume();
        getActivity().setTitle(R.string.save_contacts);
        mActivity.getActionBarToggle().setDrawerIndicatorEnabled(true);
        mActivity.getActionBarToggle().syncState();
        mActivity.setNav(R.id.nav_support);
    }

    private void addContacts() {
        for (Person p : selected) {
            if (this.current_names.contains(p.name) || current_numbers.contains(p.phone)) {
                continue;
            }

            ArrayList<ContentProviderOperation> ops = new ArrayList<>();
            int id = ops.size();

            ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                    .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                    .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
                    .build());

            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, id)
                    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, p.name)
                    .build());

            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, id)
                    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, p.phone)
                    .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
                    .build());
            try {
                ContentProviderResult[] results = mActivity.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
                current_names.add(p.name);
                current_numbers.add(p.phone);
            } catch (RemoteException e) {
                Toast.makeText(mActivity, "Could not save contacts", Toast.LENGTH_SHORT).show();
            } catch (OperationApplicationException e) {
                Toast.makeText(mActivity, "Could not save contacts", Toast.LENGTH_SHORT).show();
            }
        }
        Toast.makeText(mActivity, selected.size() + " contact"+ (selected.size() > 1 ? "s" : "") +" saved", Toast.LENGTH_SHORT).show();
    }<commit_msg>return to contacts list after save<commit_after>package com.pennapps.labs.pennmobile;

import android.Manifest;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.OperationApplicationException;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.support.v4.content.ContextCompat;
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.Toast;

import com.pennapps.labs.pennmobile.adapters.PhoneSaveAdapter;
import com.pennapps.labs.pennmobile.classes.Person;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SaveContactsFragment extends ListFragment {

    private MainActivity mActivity;
    private List<Person> contacts_list;
    private List<Person> selected;
    private Set<String> current_numbers;
    private Set<String> current_names;
    private final int permission_read = 0;

    @Override
    public void onActivityCreated(Bundle save) {
        super.onActivityCreated(save);
        mActivity = (MainActivity) getActivity();
        mActivity.closeKeyboard();
        loadData();
        getListView().setAdapter(new PhoneSaveAdapter(getActivity(), contacts_list, selected));
        loadCurrent();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle save) {
        View view = inflater.inflate(R.layout.fragment_save_contacts, container, false);
        setHasOptionsMenu(true);
        current_numbers = new HashSet<>();
        current_names = new HashSet<>();
        selected = new ArrayList<>();
        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        mActivity.getMenuInflater().inflate(R.menu.phone_save_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.support_contacts_add) {
            if (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.WRITE_CONTACTS)
                    != PackageManager.PERMISSION_GRANTED ||
                    ContextCompat.checkSelfPermission(mActivity, Manifest.permission.READ_CONTACTS)
                            != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(mActivity,
                        new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS},
                        permission_read);
            } else {
                loadCurrent();
                addContacts();
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onResume() {
        super.onResume();
        getActivity().setTitle(R.string.save_contacts);
        mActivity.getActionBarToggle().setDrawerIndicatorEnabled(true);
        mActivity.getActionBarToggle().syncState();
        mActivity.setNav(R.id.nav_support);
    }

    private void addContacts() {
        for (Person p : selected) {
            if (this.current_names.contains(p.name) || current_numbers.contains(p.phone)) {
                continue;
            }

            ArrayList<ContentProviderOperation> ops = new ArrayList<>();
            int id = ops.size();

            ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                    .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                    .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
                    .build());

            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, id)
                    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, p.name)
                    .build());

            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, id)
                    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, p.phone)
                    .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
                    .build());
            try {
                ContentProviderResult[] results = mActivity.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
                current_names.add(p.name);
                current_numbers.add(p.phone);
            } catch (RemoteException e) {
                Toast.makeText(mActivity, "Could not save contacts", Toast.LENGTH_SHORT).show();
            } catch (OperationApplicationException e) {
                Toast.makeText(mActivity, "Could not save contacts", Toast.LENGTH_SHORT).show();
            }
        }
        Toast.makeText(mActivity, selected.size() + " contact"+ (selected.size() > 1 ? "s" : "") +" saved", Toast.LENGTH_SHORT).show();
        SupportFragment frag = new SupportFragment();
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(((ViewGroup)getView().getParent()).getId(), frag, "SUPPORT_FRAGMENT")
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                .addToBackStack(null)
                .commitAllowingStateLoss();
    }
{'fraction_non_alphanumeric': 0.09613928841786526, 'fraction_numerical': 0.0010093363613424174, 'mean_word_length': 2.856633149529679, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, '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': '6819346', 'n_tokens_mistral': 3247, 'n_tokens_neox': 3115, 'n_words': 513}