<commit_before>package com.gobbledygook.theawless.eventlock;

import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.MultiSelectListPreference;
import android.preference.PreferenceFragment;
import android.provider.CalendarContract;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;

public class SettingsFragment extends PreferenceFragment {
    private static final int CALENDAR_READ_REQUEST_CODE = 0;
    private static final String TAG = "SETTINGS_FRAGMENT";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        checkAndRequestPermission();
    }

    void checkAndRequestPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && getActivity().checkSelfPermission(Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.READ_CALENDAR}, CALENDAR_READ_REQUEST_CODE);
        } else {
            createMultiSelectCalendars();
        }
    }

    private void createMultiSelectCalendars() {
        ContentResolver resolver = getActivity().getContentResolver();
        Uri calendarUri = CalendarContract.Calendars.CONTENT_URI;
        String[] projection = new String[]{CalendarContract.Calendars._ID, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME};
        Cursor cursor = resolver.query(calendarUri, projection, null, null, null);
        final ArrayList<String> calendarNames = new ArrayList<>();
        final ArrayList<String> calendarIds = new ArrayList<>();
        while (cursor != null && cursor.moveToNext()) {
            calendarNames.add(cursor.getString(1));
            calendarIds.add(Integer.valueOf(cursor.getInt(0)).toString());
        }
        MultiSelectListPreference selectedCalendarPref = (MultiSelectListPreference) findPreference(getString(R.string.selected_calendars_key));
        selectedCalendarPref.setEntries(calendarNames.toArray(new CharSequence[calendarNames.size()]));
        selectedCalendarPref.setEntryValues(calendarIds.toArray(new CharSequence[calendarNames.size()]));
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == CALENDAR_READ_REQUEST_CODE) {
            if (grantResults.length <= 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getActivity(), "Calendar read permission is required. Exiting!", Toast.LENGTH_SHORT).show();
                getActivity().finish();<commit_msg>suppresed warning and closed cursor after use<commit_after>package com.gobbledygook.theawless.eventlock;

import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.MultiSelectListPreference;
import android.preference.PreferenceFragment;
import android.provider.CalendarContract;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;

public class SettingsFragment extends PreferenceFragment {
    private static final int CALENDAR_READ_REQUEST_CODE = 0;
    private static final String TAG = "SETTINGS_FRAGMENT";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        checkAndRequestPermission();
    }

    void checkAndRequestPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && getActivity().checkSelfPermission(Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.READ_CALENDAR}, CALENDAR_READ_REQUEST_CODE);
        } else {
            createMultiSelectCalendars();
        }
    }

    @SuppressWarnings("MissingPermission")
    private void createMultiSelectCalendars() {
        ContentResolver resolver = getActivity().getContentResolver();
        Uri calendarUri = CalendarContract.Calendars.CONTENT_URI;
        String[] projection = new String[]{CalendarContract.Calendars._ID, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME};
        Cursor cursor = resolver.query(calendarUri, projection, null, null, null);
        final ArrayList<String> calendarNames = new ArrayList<>();
        final ArrayList<String> calendarIds = new ArrayList<>();
        if (cursor != null) {
            while (cursor.moveToNext()) {
                calendarNames.add(cursor.getString(1));
                calendarIds.add(Integer.valueOf(cursor.getInt(0)).toString());
            }
            cursor.close();
        }
        MultiSelectListPreference selectedCalendarPref = (MultiSelectListPreference) findPreference(getString(R.string.selected_calendars_key));
        selectedCalendarPref.setEntries(calendarNames.toArray(new CharSequence[calendarNames.size()]));
        selectedCalendarPref.setEntryValues(calendarIds.toArray(new CharSequence[calendarNames.size()]));
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == CALENDAR_READ_REQUEST_CODE) {
            if (grantResults.length <= 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getActivity(), "Calendar read permission is required. Exiting!", Toast.LENGTH_SHORT).show();
                getActivity().finish();
{'fraction_non_alphanumeric': 0.09849402804223646, 'fraction_numerical': 0.0017310022503029255, 'mean_word_length': 4.17741935483871, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3837770', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1489, 'n_words': 280}