<commit_before>/*
Copyright (c) 2014-2015 F-Secure
See LICENSE for details
*/
package cc.softwarefactory.lokki.android.utilities;

import android.content.ContentUris;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;

import android.os.Build;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.Toast;

import cc.softwarefactory.lokki.android.MainApplication;
import cc.softwarefactory.lokki.android.R;
import cc.softwarefactory.lokki.android.models.JSONModel;
import cc.softwarefactory.lokki.android.services.LocationService;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Locale;
import java.util.Map;

import static android.text.format.DateUtils.getRelativeTimeSpanString;


public class Utils {


    private static final String TAG = "Utils";


    public static String getDeviceId() {

        return "35" + //we make this look like a valid IMEI
                Build.BOARD.length() % 10 + Build.BRAND.length() % 10 +
                Build.SERIAL.length() % 10 + Build.DEVICE.length() % 10 +
                Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 +
                Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 +
                Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 +
                Build.TAGS.length() % 10 + Build.TYPE.length() % 10 +
                Build.USER.length() % 10; //13 digits
    }

    private static Boolean loadContacts(Context context) {

        if (context == null) {
            return false;
        }
        if (MainApplication.contacts != null) {
            return true;
        }

        String jsonData = PreferenceUtils.getString(context, PreferenceUtils.KEY_CONTACTS);
        if (jsonData.isEmpty()) {
            return false;
        }
        try {
            MainApplication.contacts = JSONModel.createFromJson(jsonData, MainApplication.Contacts.class);
        } catch (IOException e) {
            MainApplication.contacts = new MainApplication.Contacts();
            Log.e(TAG, "Reading contacts from JSON failed. Empty contacts created.");
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static String getIdFromEmail(Context context, String email) {

        if (context == null || MainApplication.dashboard == null) {
            return null;
        }

        Map<String, String> idMapping = MainApplication.dashboard.getIdMapping();
        for (String userId : idMapping.keySet()) {
            String emailInObject = idMapping.get(userId);
            if (email.equals(emailInObject)) {
                Log.d(TAG, "email: " + email + ", Id from mapping: " + userId);
                return userId;
            }
        }

        return null;
    }

    public static String getNameFromEmail(Context context, String email) {

        if (context == null) {
            return null;
        }

        if (loadContacts(context)) {
            try {
                Log.d(TAG, MainApplication.contacts.serialize());
                String name = MainApplication.contacts.getContactByEmail(email).getName();
                Log.d(TAG, "getNameFromEmail - Email: " + email + ", Name: " + name);
                return name;
            } catch (JsonProcessingException e) {
                Log.e(TAG, "serializing contact to JSON failed");
                e.printStackTrace();
            }
        }

        Log.d(TAG, "getNameFromEmail - Name queried: " + email);
        Cursor emailCursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, "lower(" + ContactsContract.CommonDataKinds.Email.DATA + ")=lower('" + email + "')", null, null);
        if (emailCursor == null) {
            return "???";
        }
        if (emailCursor.moveToNext()) {
            String name = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DISPLAY_NAME_PRIMARY));
            Log.d(TAG, "getNameFromEmail - Email: " + email + ", Name: " + name);
            emailCursor.close();
            return name;
        }
        emailCursor.close();
        return email;
    }

    public static Bitmap getPhotoFromEmail(Context context, String email) {

        if (context == null || email == null) {
            return null;
        }

        Bitmap result = MainApplication.avatarCache.get(email);
        if (result != null) {
            Log.d(TAG, "getPhotoFromEmail IN cache, Email: " + email);
            return result;
        }

        if (loadContacts(context)) {
            long id = MainApplication.contacts.getContactByEmail(email).getId();
            Log.d(TAG, "getPhotoFromEmail - Email: " + email + ", id: " + id);
            result = openPhoto(context, id);
        } else {<commit_msg>prevent crashing when contact is null when getting contact's photo<commit_after>/*
Copyright (c) 2014-2015 F-Secure
See LICENSE for details
*/
package cc.softwarefactory.lokki.android.utilities;

import android.content.ContentUris;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;

import android.os.Build;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.Toast;

import cc.softwarefactory.lokki.android.MainApplication;
import cc.softwarefactory.lokki.android.R;
import cc.softwarefactory.lokki.android.models.Contact;
import cc.softwarefactory.lokki.android.models.JSONModel;
import cc.softwarefactory.lokki.android.services.LocationService;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Locale;
import java.util.Map;

import static android.text.format.DateUtils.getRelativeTimeSpanString;


public class Utils {


    private static final String TAG = "Utils";


    public static String getDeviceId() {

        return "35" + //we make this look like a valid IMEI
                Build.BOARD.length() % 10 + Build.BRAND.length() % 10 +
                Build.SERIAL.length() % 10 + Build.DEVICE.length() % 10 +
                Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 +
                Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 +
                Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 +
                Build.TAGS.length() % 10 + Build.TYPE.length() % 10 +
                Build.USER.length() % 10; //13 digits
    }

    private static Boolean loadContacts(Context context) {

        if (context == null) {
            return false;
        }
        if (MainApplication.contacts != null) {
            return true;
        }

        String jsonData = PreferenceUtils.getString(context, PreferenceUtils.KEY_CONTACTS);
        if (jsonData.isEmpty()) {
            return false;
        }
        try {
            MainApplication.contacts = JSONModel.createFromJson(jsonData, MainApplication.Contacts.class);
        } catch (IOException e) {
            MainApplication.contacts = new MainApplication.Contacts();
            Log.e(TAG, "Reading contacts from JSON failed. Empty contacts created.");
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static String getIdFromEmail(Context context, String email) {

        if (context == null || MainApplication.dashboard == null) {
            return null;
        }

        Map<String, String> idMapping = MainApplication.dashboard.getIdMapping();
        for (String userId : idMapping.keySet()) {
            String emailInObject = idMapping.get(userId);
            if (email.equals(emailInObject)) {
                Log.d(TAG, "email: " + email + ", Id from mapping: " + userId);
                return userId;
            }
        }

        return null;
    }

    public static String getNameFromEmail(Context context, String email) {

        if (context == null) {
            return null;
        }

        if (loadContacts(context)) {
            try {
                Log.d(TAG, MainApplication.contacts.serialize());
                Contact contact = MainApplication.contacts.getContactByEmail(email);
                String name = contact.getName();
                Log.d(TAG, "getNameFromEmail - Email: " + email + ", Name: " + name);
                return name;
            } catch (NullPointerException e) {
                Log.e(TAG, "getNameFromEmail - failed (contact was null): " + email);
            } catch (JsonProcessingException e) {
                Log.e(TAG, "serializing contacts to JSON failed");
                e.printStackTrace();
            }
        }

        Log.d(TAG, "getNameFromEmail - Name queried: " + email);
        Cursor emailCursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, "lower(" + ContactsContract.CommonDataKinds.Email.DATA + ")=lower('" + email + "')", null, null);
        if (emailCursor == null) {
            return "???";
        }
        if (emailCursor.moveToNext()) {
            String name = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DISPLAY_NAME_PRIMARY));
            Log.d(TAG, "getNameFromEmail - Email: " + email + ", Name: " + name);
            emailCursor.close();
            return name;
        }
        emailCursor.close();
        return email;
    }

    public static Bitmap getPhotoFromEmail(Context context, String email) {

        if (context == null || email == null) {
            return null;
        }

        Bitmap result = MainApplication.avatarCache.get(email);
        if (result != null) {
            Log.d(TAG, "getPhotoFromEmail IN cache, Email: " + email);
            return result;
        }

        if (loadContacts(context)) {
            Contact contact = MainApplication.contacts.getContactByEmail(email);
            try {
                long id = contact.getId();
                Log.d(TAG, "getPhotoFromEmail - Email: " + email + ", id: " + id);
                result = openPhoto(context, id);
            } catch (NullPointerException e) {
                Log.e(TAG, "getNameFromEmail - failed (contact was null): " + email);
            }
        } else {
{'fraction_non_alphanumeric': 0.11053827333100315, 'fraction_numerical': 0.006641034603285564, 'mean_word_length': 2.428699820251648, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2144878', 'n_tokens_mistral': 3240, 'n_tokens_neox': 3070, 'n_words': 700}