<commit_before>/*
 * Copyright 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 com.guerinet.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.support.annotation.RawRes;
import android.support.annotation.StringRes;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.util.Pair;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import okio.BufferedSource;
import okio.Okio;
import timber.log.Timber;

/**
 * Static utility classes
 * @author <NAME>
 * @since 1.0.0
 */
public class Utils {

    /**
     * Displays a toast of short length
     *
     * @param context App context
     * @param message Message to show
     */
    public static void toast(Context context, @StringRes int message) {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

    /**
     * Opens a given URL
     *
     * @param context App context
     * @param url      URL to open
     */
    public static void openURL(Context context, String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW)
                .setData(Uri.parse(url));
        context.startActivity(intent);
    }

    /**
     * @param context App context
     * @return App package info, null if an error
     */
    private static PackageInfo getPackageInfo(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        } catch(PackageManager.NameNotFoundException e) {
            Timber.e(e, "PackageInfo not found");
            return null;
        }
    }

    /**
     * @param context App context
     * @return App version code
     */
    public static int versionCode(Context context) {
        PackageInfo info = getPackageInfo(context);
        return info == null ? -1 : info.versionCode;
    }

    /**
     * @param context App context
     * @return App version name
     */
    public static String versionName(Context context) {
        PackageInfo info = getPackageInfo(context);
        return info == null ? null : info.versionName;
    }<commit_msg>Added method to open an app on the Play Store.<commit_after>/*
 * Copyright 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 com.guerinet.utils;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.support.annotation.RawRes;
import android.support.annotation.StringRes;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.util.Pair;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import okio.BufferedSource;
import okio.Okio;
import timber.log.Timber;

/**
 * Static utility classes
 * @author <NAME>
 * @since 1.0.0
 */
public class Utils {

    /**
     * Displays a toast of short length
     *
     * @param context App context
     * @param message Message to show
     */
    public static void toast(Context context, @StringRes int message) {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

    /**
     * Opens a given URL
     *
     * @param context App context
     * @param url      URL to open
     */
    public static void openURL(Context context, String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW)
                .setData(Uri.parse(url));
        context.startActivity(intent);
    }

    /**
     * Opens the URL to an app in the Play Store or in the browser if the Play Store does not exist
     *
     * @param context     App context
     * @param packageName App package name
     */
    public static void openPlayStoreApp(Context context, String packageName) {
        try {
            context.startActivity(new Intent(
                    Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName)));
        } catch(ActivityNotFoundException e) {
            //If the user user dot have the play store installed, open it in their browser
            openURL(context, "https://play.google.com/store/apps/details?id=" + packageName);
        }
    }

    /**
     * @param context App context
     * @return App package info, null if an error
     */
    private static PackageInfo getPackageInfo(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        } catch(PackageManager.NameNotFoundException e) {
            Timber.e(e, "PackageInfo not found");
            return null;
        }
    }

    /**
     * @param context App context
     * @return App version code
     */
    public static int versionCode(Context context) {
        PackageInfo info = getPackageInfo(context);
        return info == null ? -1 : info.versionCode;
    }

    /**
     * @param context App context
     * @return App version name
     */
    public static String versionName(Context context) {
        PackageInfo info = getPackageInfo(context);
        return info == null ? null : info.versionName;
    }
{'fraction_non_alphanumeric': 0.09339439356092145, 'fraction_numerical': 0.004718290313627533, 'mean_word_length': 3.0534308211473564, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 1, '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': '6810341', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1923, 'n_words': 616}