<commit_before>/*
 * Copyright 2015 <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 com.chromium.fontinstaller.models;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Parcel;
import android.os.Parcelable;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by priyeshpatel on 15-02-06.
 */
public class FontPackage implements Parcelable {
    private String name;
    private String nameFormatted;
    private HashMap<Font, Style> fontList = new HashMap<>();

    private static final String BASE_URL = "https://raw.githubusercontent.com/ItsPriyesh/FontsterFontsRepo/master/";

    public FontPackage(String name) {
        this.name = name;
        this.nameFormatted = name.replace(" ", "") + "FontPack";

        generateFonts();
    }

    private void generateFonts() {
        for (Style style : Style.values()) {
            Font font = new Font(style, BASE_URL + nameFormatted + "/" + style.getRemoteName());
            fontList.put(font, style);
        }
    }

    public ArrayList<Font> getFontList() {
        return new ArrayList<>(fontList.keySet());
    }

    public String getName() {
        return name;
    }

    public String getNameFormatted() {
        return nameFormatted;
    }

    public Typeface getTypeface(Style style, Context context) {
        String path = context.getExternalCacheDir() + File.separator +
                nameFormatted + File.separator + style.getLocalName();
        return Typeface.createFromFile(path);
    }

    public Font getFont(Style style) {
        for (Font font : fontList.keySet())
            if (fontList.get(font).equals(style))
                return font;

        return null;
    }

    protected FontPackage(Parcel in) {
        name = in.readString();
        nameFormatted = in.readString();
        fontList = (HashMap) in.readValue(HashMap.class.getClassLoader());
    }

    @Override
    public int describeContents() {
        return 0;<commit_msg>Check that font file exists before returning Typeface object<commit_after>/*
 * Copyright 2015 <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 com.chromium.fontinstaller.models;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Parcel;
import android.os.Parcelable;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by priyeshpatel on 15-02-06.
 */
public class FontPackage implements Parcelable {
    private String name;
    private String nameFormatted;
    private HashMap<Font, Style> fontList = new HashMap<>();

    private static final String BASE_URL = "https://raw.githubusercontent.com/ItsPriyesh/FontsterFontsRepo/master/";

    public FontPackage(String name) {
        this.name = name;
        this.nameFormatted = name.replace(" ", "") + "FontPack";

        generateFonts();
    }

    private void generateFonts() {
        for (Style style : Style.values()) {
            Font font = new Font(style, BASE_URL + nameFormatted + "/" + style.getRemoteName());
            fontList.put(font, style);
        }
    }

    public ArrayList<Font> getFontList() {
        return new ArrayList<>(fontList.keySet());
    }

    public String getName() {
        return name;
    }

    public String getNameFormatted() {
        return nameFormatted;
    }

    public Typeface getTypeface(Style style, Context context) {
        String path = context.getExternalCacheDir() + File.separator +
                nameFormatted + File.separator + style.getLocalName();

        if (new File(path).exists()) return Typeface.createFromFile(path);
        else return Typeface.DEFAULT;
    }

    public Font getFont(Style style) {
        for (Font font : fontList.keySet())
            if (fontList.get(font).equals(style))
                return font;

        return null;
    }

    protected FontPackage(Parcel in) {
        name = in.readString();
        nameFormatted = in.readString();
        fontList = (HashMap) in.readValue(HashMap.class.getClassLoader());
    }

    @Override
    public int describeContents() {
        return 0;
{'fraction_non_alphanumeric': 0.10075130032748988, 'fraction_numerical': 0.005779233288383741, 'mean_word_length': 2.8862275449101795, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '1890942', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1406, 'n_words': 437}