<commit_before>/*
 * VoIP.ms SMS
 * Copyright (C) 2015-2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.kourlas.voipms_sms;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.graphics.*;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.provider.ContactsContract;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.ViewOutlineProvider;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Contains various common utility methods.
 */
@SuppressWarnings("WeakerAccess")
public class Utils {
    /**
     * Gets the name of a contact from the Android contacts provider, given a
     * phone number.
     *
     * @param applicationContext The application context.
     * @param phoneNumber        The phone number of the contact.
     * @return The name of the contact.
     */
    public static String getContactName(Context applicationContext,
                                        String phoneNumber)
    {
        Uri uri = Uri.withAppendedPath(
            ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
            Uri.encode(phoneNumber));
        Cursor cursor = applicationContext.getContentResolver().query(
            uri,
            new String[] {
                ContactsContract.PhoneLookup._ID,
                ContactsContract.PhoneLookup.DISPLAY_NAME
            },
            null, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                String name = cursor.getString(cursor.getColumnIndex(
                    ContactsContract.PhoneLookup.DISPLAY_NAME));
                cursor.close();
                return name;
            } else {
                cursor.close();
            }
        }

        return null;
    }

    /**
     * Gets a URI pointing to a contact's photo, given a phone number.
     *
     * @param applicationContext The application context.
     * @param phoneNumber        The phone number of the contact.
     * @return A URI pointing to the contact's photo.
     */
    public static String getContactPhotoUri(Context applicationContext,
                                            String phoneNumber)
    {
        Uri uri = Uri.withAppendedPath(
            ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
            Uri.encode(phoneNumber));
        return getContactPhotoUri(applicationContext, uri);
    }

    /**
     * Gets a URI pointing to a contact's photo, given the URI for that contact.
     *
     * @param applicationContext The application context.
     * @param uri                The URI of the contact.
     * @return A URI pointing to the contact's photo.
     */
    public static String getContactPhotoUri(Context applicationContext,
                                            Uri uri)
    {
        Cursor cursor = applicationContext.getContentResolver().query(
            uri, new String[] {
                ContactsContract.PhoneLookup._ID,
                ContactsContract.PhoneLookup.PHOTO_THUMBNAIL_URI
            },
            null, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                String photoUri = cursor.getString(cursor.getColumnIndex(
                    ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
                cursor.close();
                return photoUri;
            } else {
                cursor.close();
            }
        }

        return null;<commit_msg>Catch permission problems in Utils<commit_after>/*
 * VoIP.ms SMS
 * Copyright (C) 2015-2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.kourlas.voipms_sms;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.*;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.provider.ContactsContract;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.ViewOutlineProvider;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Contains various common utility methods.
 */
@SuppressWarnings("WeakerAccess")
public class Utils {
    /**
     * Gets the name of a contact from the Android contacts provider, given a
     * phone number.
     *
     * @param applicationContext The application context.
     * @param phoneNumber        The phone number of the contact.
     * @return The name of the contact.
     */
    public static String getContactName(Context applicationContext,
                                        String phoneNumber)
    {
        try {
            Uri uri = Uri.withAppendedPath(
                ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                Uri.encode(phoneNumber));
            Cursor cursor = applicationContext.getContentResolver().query(
                uri,
                new String[] {
                    ContactsContract.PhoneLookup._ID,
                    ContactsContract.PhoneLookup.DISPLAY_NAME
                },
                null, null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    String name = cursor.getString(cursor.getColumnIndex(
                        ContactsContract.PhoneLookup.DISPLAY_NAME));
                    cursor.close();
                    return name;
                } else {
                    cursor.close();
                }
            }
            return null;
        } catch (SecurityException ex) {
            return null;
        }
    }

    /**
     * Gets a URI pointing to a contact's photo, given a phone number.
     *
     * @param applicationContext The application context.
     * @param phoneNumber        The phone number of the contact.
     * @return A URI pointing to the contact's photo.
     */
    public static String getContactPhotoUri(Context applicationContext,
                                            String phoneNumber)
    {
        try {
            Uri uri = Uri.withAppendedPath(
                ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                Uri.encode(phoneNumber));
            return getContactPhotoUri(applicationContext, uri);
        } catch (SecurityException ex) {
            return null;
        }
    }

    /**
     * Gets a URI pointing to a contact's photo, given the URI for that contact.
     *
     * @param applicationContext The application context.
     * @param uri                The URI of the contact.
     * @return A URI pointing to the contact's photo.
     */
    public static String getContactPhotoUri(Context applicationContext,
                                            Uri uri)
    {
        try {
            Cursor cursor = applicationContext.getContentResolver().query(
                uri, new String[] {
                    ContactsContract.PhoneLookup._ID,
                    ContactsContract.PhoneLookup.PHOTO_THUMBNAIL_URI
                },
                null, null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    String photoUri = cursor.getString(cursor.getColumnIndex(
                        ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
                    cursor.close();
                    return photoUri;
                } else {
                    cursor.close();
                }
            }

            return null;
        } catch (SecurityException ex) {
            return null;
        }
{'fraction_non_alphanumeric': 0.0790794979079498, 'fraction_numerical': 0.0029288702928870294, 'mean_word_length': 2.0891760904684977, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5393631', 'n_tokens_mistral': 2412, 'n_tokens_neox': 2312, 'n_words': 703}