<commit_before>/*
 * Copyright 2020 Google LLC. All rights reserved.
 *
 * 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.sample.android.classytaxijava.ui;

import android.app.Application;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.MutableLiveData;

import com.android.billingclient.api.BillingFlowParams;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.SkuDetails;
import com.sample.android.classytaxijava.Constants;
import com.sample.android.classytaxijava.SubApp;
import com.sample.android.classytaxijava.billing.BillingUtilities;
import com.sample.android.classytaxijava.data.SubscriptionStatus;

import java.util.List;
import java.util.Map;

public class BillingViewModel extends AndroidViewModel {

    /**
     * Local billing purchase data.
     */
    private MutableLiveData<List<Purchase>> purchases;

    /**
     * SkuDetails for all known SKUs.
     */
    private MutableLiveData<Map<String, SkuDetails>> skusWithSkuDetails;

    /**
     * Subscriptions record according to the server.
     */
    private MediatorLiveData<List<SubscriptionStatus>> subscriptions;

    /**
     * Send an event when the Activity needs to buy something.
     */
    public SingleLiveEvent<BillingFlowParams> buyEvent = new SingleLiveEvent<>();

    /**
     * Send an event when the UI should open the Google Play
     * Store for the user to manage their subscriptions.
     */
    public SingleLiveEvent<String> openPlayStoreSubscriptionsEvent = new SingleLiveEvent<>();

    public BillingViewModel(Application application) {
        super(application);
        SubApp subApp = ((SubApp) application);
        purchases = subApp.getBillingClientLifecycle().purchases;
        skusWithSkuDetails = subApp.getBillingClientLifecycle().skusWithSkuDetails;
        subscriptions = subApp.getRepository().getSubscriptions();
    }

    /**
     * Open the Play Store subscription center. If the user has exactly one SKU,
     * then open the deeplink to the specific SKU.
     */
    public void openPlayStoreSubscriptions() {
        boolean hasBasic = BillingUtilities.deviceHasGooglePlaySubscription(
                purchases.getValue(), Constants.BASIC_SKU);
        boolean hasPremium = BillingUtilities.deviceHasGooglePlaySubscription
                (purchases.getValue(), Constants.PREMIUM_SKU);
        Log.d("Billing", "hasBasic: $hasBasic, hasPremium: $hasPremium");

        if (hasBasic && !hasPremium) {
            // If we just have a basic subscription, open the basic SKU.
            openPlayStoreSubscriptionsEvent.postValue(Constants.BASIC_SKU);
        } else if (!hasBasic && hasPremium) {
            // If we just have a premium subscription, open the premium SKU.
            openPlayStoreSubscriptionsEvent.postValue(Constants.PREMIUM_SKU);
        } else {
            // If we do not have an active subscription,
            // or if we have multiple subscriptions, open the default subscription center.
            openPlayStoreSubscriptionsEvent.call();
        }
    }

    /**
     * OpenOpen the subscription page on Google Play.
     * <p>
     * Since the purchase tokens will not be returned during account hold or pause,
     * we use the server data to determine the deeplink to Google Play.
     */
    public void openSubscriptionPageOnGooglePlay() {
        boolean isPremiumOnServer = BillingUtilities
                .serverHasSubscription(subscriptions.getValue(), Constants.PREMIUM_SKU);
        boolean isBasicOnServer = BillingUtilities
                .serverHasSubscription(subscriptions.getValue(), Constants.BASIC_SKU);
        if (isPremiumOnServer) {
            openPremiumPlayStoreSubscriptions();
        }

        if (isBasicOnServer) {
            openBasicPlayStoreSubscriptions();
        }
    }
<commit_msg>Fix typo "OpenOpen"<commit_after>/*
 * Copyright 2020 Google LLC. All rights reserved.
 *
 * 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.sample.android.classytaxijava.ui;

import android.app.Application;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.MutableLiveData;

import com.android.billingclient.api.BillingFlowParams;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.SkuDetails;
import com.sample.android.classytaxijava.Constants;
import com.sample.android.classytaxijava.SubApp;
import com.sample.android.classytaxijava.billing.BillingUtilities;
import com.sample.android.classytaxijava.data.SubscriptionStatus;

import java.util.List;
import java.util.Map;

public class BillingViewModel extends AndroidViewModel {

    /**
     * Local billing purchase data.
     */
    private MutableLiveData<List<Purchase>> purchases;

    /**
     * SkuDetails for all known SKUs.
     */
    private MutableLiveData<Map<String, SkuDetails>> skusWithSkuDetails;

    /**
     * Subscriptions record according to the server.
     */
    private MediatorLiveData<List<SubscriptionStatus>> subscriptions;

    /**
     * Send an event when the Activity needs to buy something.
     */
    public SingleLiveEvent<BillingFlowParams> buyEvent = new SingleLiveEvent<>();

    /**
     * Send an event when the UI should open the Google Play
     * Store for the user to manage their subscriptions.
     */
    public SingleLiveEvent<String> openPlayStoreSubscriptionsEvent = new SingleLiveEvent<>();

    public BillingViewModel(Application application) {
        super(application);
        SubApp subApp = ((SubApp) application);
        purchases = subApp.getBillingClientLifecycle().purchases;
        skusWithSkuDetails = subApp.getBillingClientLifecycle().skusWithSkuDetails;
        subscriptions = subApp.getRepository().getSubscriptions();
    }

    /**
     * Open the Play Store subscription center. If the user has exactly one SKU,
     * then open the deeplink to the specific SKU.
     */
    public void openPlayStoreSubscriptions() {
        boolean hasBasic = BillingUtilities.deviceHasGooglePlaySubscription(
                purchases.getValue(), Constants.BASIC_SKU);
        boolean hasPremium = BillingUtilities.deviceHasGooglePlaySubscription
                (purchases.getValue(), Constants.PREMIUM_SKU);
        Log.d("Billing", "hasBasic: $hasBasic, hasPremium: $hasPremium");

        if (hasBasic && !hasPremium) {
            // If we just have a basic subscription, open the basic SKU.
            openPlayStoreSubscriptionsEvent.postValue(Constants.BASIC_SKU);
        } else if (!hasBasic && hasPremium) {
            // If we just have a premium subscription, open the premium SKU.
            openPlayStoreSubscriptionsEvent.postValue(Constants.PREMIUM_SKU);
        } else {
            // If we do not have an active subscription,
            // or if we have multiple subscriptions, open the default subscription center.
            openPlayStoreSubscriptionsEvent.call();
        }
    }

    /**
     * Open the subscription page on Google Play.
     * <p>
     * Since the purchase tokens will not be returned during account hold or pause,
     * we use the server data to determine the deeplink to Google Play.
     */
    public void openSubscriptionPageOnGooglePlay() {
        boolean isPremiumOnServer = BillingUtilities
                .serverHasSubscription(subscriptions.getValue(), Constants.PREMIUM_SKU);
        boolean isBasicOnServer = BillingUtilities
                .serverHasSubscription(subscriptions.getValue(), Constants.BASIC_SKU);
        if (isPremiumOnServer) {
            openPremiumPlayStoreSubscriptions();
        }

        if (isBasicOnServer) {
            openBasicPlayStoreSubscriptions();
        }
    }

{'fraction_non_alphanumeric': 0.08071748878923767, 'fraction_numerical': 0.0017937219730941704, 'mean_word_length': 3.210004719207173, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, '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': '5111530', 'n_tokens_mistral': 2396, 'n_tokens_neox': 2351, 'n_words': 690}