<commit_before>package com.tasomaniac.muzei.earthview.ui;

import android.annotation.TargetApi;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.TypedArray;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.preference.CheckBoxPreference;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;

import com.tasomaniac.muzei.earthview.App;
import com.tasomaniac.muzei.earthview.R;
import com.tasomaniac.muzei.earthview.util.AppInstallEnabler;

import javax.inject.Inject;

public class IntegrationPreference extends CheckBoxPreference {

    @Inject
    PackageManager packageManager;

    AppInstallEnabler appInstallEnabler;

    Intent originalIntent;
    Intent alternativeIntent;

    public IntegrationPreference(Context context) {
        super(context);
        initialize(context, null);
    }

    public IntegrationPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize(context, attrs);
    }

    public IntegrationPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public IntegrationPreference(
            Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initialize(context, attrs);
    }

    private void initialize(Context context, AttributeSet attrs) {
        App.get(context).component().inject(this);

        setWidgetLayoutResource(R.layout.preference_widget_error_fix);
        setDefaultValue(false);

        final TypedArray sa = context.obtainStyledAttributes(attrs,
                R.styleable.IntegrationPreference);

        extractAlternativeIntent(sa);

        sa.recycle();

        setDisableDependentsState(true);

        appInstallEnabler = new AppInstallEnabler(context, this);
    }

    @Override
    public void onAttached() {
        super.onAttached();
        originalIntent = getIntent();
        if (originalIntent != null && !hasIntent(alternativeIntent)) {
            alternativeIntent = marketIntent(originalIntent);
        }
        checkState();
    }

    public void resume() {
        appInstallEnabler.resume();
    }

    public void pause() {
        appInstallEnabler.pause();
    }

    public void checkState() {
        if (hasIntent(originalIntent)) {
            adjustPreference(originalIntent, false);
        } else {
            adjustPreference(alternativeIntent, true);
        }
    }

    private void extractAlternativeIntent(TypedArray sa) {
        //Parse the alternative Intent
        alternativeIntent = new Intent();

        alternativeIntent.setAction(
                sa.getString(R.styleable.IntegrationPreference_alternativeIntentAction));

        String data = sa.getString(R.styleable.IntegrationPreference_alternativeIntentData);
        String mimeType = sa.getString(R.styleable.IntegrationPreference_alternativeIntentMimeType);
        alternativeIntent.setDataAndType(data != null ? Uri.parse(data) : null, mimeType);

        String packageName =
                sa.getString(R.styleable.IntegrationPreference_alternativeIntentTargetPackage);
        String className =
                sa.getString(R.styleable.IntegrationPreference_alternativeIntentTargetClass);
        if (packageName != null && className != null) {
            alternativeIntent.setComponent(new ComponentName(packageName, className));
        }
    }

    private Intent marketIntent(Intent originalIntent) {
        ComponentName component = originalIntent.getComponent();
        if (component != null) {
            final String packageName = component.getPackageName();
            Intent marketIntent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("market://details?id=" + packageName));
            if (!hasIntent(marketIntent)) {
                marketIntent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("https://play.google.com/store/apps/details?id=" + packageName));
            }
            if (hasIntent(marketIntent)) {
                return marketIntent;
            }
        }
        return null;
    }

    @Override
    public void setSummaryOn(CharSequence summary) {
        if (summary != null) {
            SpannableString summarySpan = getErrorString(summary);
            super.setSummaryOn(summarySpan);
        } else {
            super.setSummaryOn(null);
        }
    }

    private boolean hasIntent(@Nullable Intent intent) {
        return intent != null
                && packageManager.resolveActivity(intent, 0) != null;
    }

    @Override
    protected void onClick() {
    }

    @Override
    public void setChecked(boolean checked) {
        super.setChecked(checked);

        if (!checked) {
            setTitle(getTitle().toString());
        } else {
            SpannableString titleSpan = getErrorString(getTitle());
            setTitle(titleSpan);
        }
    }

    @NonNull
    private SpannableString getErrorString(CharSequence originalString) {
        int errorColor = getContext().getResources().getColor(R.color.error_color);
        SpannableString errorSpan = new SpannableString(originalString);
        errorSpan.setSpan(new ForegroundColorSpan(errorColor), 0, errorSpan.length(), 0);
        return errorSpan;
    }

    private void adjustPreference(Intent intentOff, boolean checked) {
        setIntent(intentOff);
        setChecked(checked);

        if (!hasIntent(intentOff)) {
            setIntent(null);
        }
    }
}<commit_msg>Slight change in market intent handling.<commit_after>package com.tasomaniac.muzei.earthview.ui;

import android.annotation.TargetApi;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.TypedArray;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.preference.CheckBoxPreference;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;

import com.tasomaniac.muzei.earthview.App;
import com.tasomaniac.muzei.earthview.R;
import com.tasomaniac.muzei.earthview.util.AppInstallEnabler;

import javax.inject.Inject;

public class IntegrationPreference extends CheckBoxPreference {

    @Inject
    PackageManager packageManager;

    AppInstallEnabler appInstallEnabler;

    Intent originalIntent;
    Intent alternativeIntent;

    public IntegrationPreference(Context context) {
        super(context);
        initialize(context, null);
    }

    public IntegrationPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize(context, attrs);
    }

    public IntegrationPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public IntegrationPreference(
            Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initialize(context, attrs);
    }

    private void initialize(Context context, AttributeSet attrs) {
        App.get(context).component().inject(this);

        setWidgetLayoutResource(R.layout.preference_widget_error_fix);
        setDefaultValue(false);

        final TypedArray sa = context.obtainStyledAttributes(attrs,
                R.styleable.IntegrationPreference);

        extractAlternativeIntent(sa);

        sa.recycle();

        setDisableDependentsState(true);

        appInstallEnabler = new AppInstallEnabler(context, this);
    }

    @Override
    public void onAttached() {
        super.onAttached();
        originalIntent = getIntent();
        if (originalIntent != null && !hasIntent(alternativeIntent)) {
            alternativeIntent = marketIntent(originalIntent);
        }
        checkState();
    }

    public void resume() {
        appInstallEnabler.resume();
    }

    public void pause() {
        appInstallEnabler.pause();
    }

    public void checkState() {
        if (hasIntent(originalIntent)) {
            adjustPreference(originalIntent, false);
        } else {
            adjustPreference(alternativeIntent, true);
        }
    }

    private void extractAlternativeIntent(TypedArray sa) {
        //Parse the alternative Intent
        alternativeIntent = new Intent();

        alternativeIntent.setAction(
                sa.getString(R.styleable.IntegrationPreference_alternativeIntentAction));

        String data = sa.getString(R.styleable.IntegrationPreference_alternativeIntentData);
        String mimeType = sa.getString(R.styleable.IntegrationPreference_alternativeIntentMimeType);
        alternativeIntent.setDataAndType(data != null ? Uri.parse(data) : null, mimeType);

        String packageName =
                sa.getString(R.styleable.IntegrationPreference_alternativeIntentTargetPackage);
        String className =
                sa.getString(R.styleable.IntegrationPreference_alternativeIntentTargetClass);
        if (packageName != null && className != null) {
            alternativeIntent.setComponent(new ComponentName(packageName, className));
        }
    }

    private Intent marketIntent(Intent originalIntent) {
        ComponentName component = originalIntent.getComponent();
        if (component != null) {
            final String packageName = component.getPackageName();
            Intent marketIntent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("market://details?id=" + packageName));
            if (!hasIntent(marketIntent)) {
                marketIntent.setData(Uri.parse(
                        "https://play.google.com/store/apps/details?id=" + packageName));
            }
            if (hasIntent(marketIntent)) {
                return marketIntent;
            }
        }
        return null;
    }

    @Override
    public void setSummaryOn(CharSequence summary) {
        if (summary != null) {
            SpannableString summarySpan = getErrorString(summary);
            super.setSummaryOn(summarySpan);
        } else {
            super.setSummaryOn(null);
        }
    }

    private boolean hasIntent(@Nullable Intent intent) {
        return intent != null
                && packageManager.resolveActivity(intent, 0) != null;
    }

    @Override
    protected void onClick() {
    }

    @Override
    public void setChecked(boolean checked) {
        super.setChecked(checked);

        if (!checked) {
            setTitle(getTitle().toString());
        } else {
            SpannableString titleSpan = getErrorString(getTitle());
            setTitle(titleSpan);
        }
    }

    @NonNull
    private SpannableString getErrorString(CharSequence originalString) {
        int errorColor = ContextCompat.getColor(getContext(), R.color.error_color);
        SpannableString errorSpan = new SpannableString(originalString);
        errorSpan.setSpan(new ForegroundColorSpan(errorColor), 0, errorSpan.length(), 0);
        return errorSpan;
    }

    private void adjustPreference(Intent intentOff, boolean checked) {
        setIntent(intentOff);
        setChecked(checked);

        if (!hasIntent(intentOff)) {
            setIntent(null);
        }
    }
}
{'fraction_non_alphanumeric': 0.09139966273187183, 'fraction_numerical': 0.0007588532883642496, 'mean_word_length': 2.8534762833008447, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '2407668', 'n_tokens_mistral': 3148, 'n_tokens_neox': 3070, 'n_words': 592}