Search in sources :

Example 1 with Subscription

use of com.zype.android.Billing.Subscription in project zype-android by zype.

the class SubscribeOrLoginActivity method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
        case REQUEST_CONSUMER:
            if (resultCode == RESULT_OK) {
                List<Purchase> purchases = ZypeApp.marketplaceGateway.getBillingManager().getPurchases();
                if (purchases != null && purchases.size() > 0) {
                    Subscription subscription = ZypeApp.marketplaceGateway.findSubscriptionBySku(purchases.get(0).getSku());
                    if (subscription != null) {
                        showProgress();
                        ZypeApp.marketplaceGateway.verifySubscription(subscription).observe(this, new Observer<Boolean>() {

                            @Override
                            public void onChanged(@Nullable Boolean result) {
                                hideProgress();
                                if (result) {
                                    setResult(RESULT_OK);
                                    finish();
                                } else {
                                    DialogHelper.showErrorAlert(SubscribeOrLoginActivity.this, getString(R.string.subscribe_or_login_error_validation));
                                }
                            }
                        });
                    } else {
                        Logger.e("Not found Zype Plan for existing in-app purchase, sku=" + purchases.get(0).getSku());
                        DialogHelper.showErrorAlert(this, getString(R.string.subscribe_or_login_error_restore_purchase_zype));
                    }
                /*for (String itemSku : purchases.get(0).getSkus()){
                            Subscription subscription = ZypeApp.marketplaceGateway.findSubscriptionBySku(itemSku);
                            if (subscription != null) {
                                showProgress();
                                ZypeApp.marketplaceGateway.verifySubscription(subscription).observe(this, new Observer<Boolean>() {
                                    @Override
                                    public void onChanged(@Nullable Boolean result) {
                                        hideProgress();
                                        if (result) {
                                            setResult(RESULT_OK);
                                            finish();
                                        }
                                        else {
                                            DialogHelper.showErrorAlert(SubscribeOrLoginActivity.this,
                                                    getString(R.string.subscribe_or_login_error_validation));
                                        }
                                    }
                                });
                            }
                            else {
                                Logger.e("Not found Zype Plan for existing in-app purchase, sku=" + itemSku);
                                DialogHelper.showErrorAlert(this, getString(R.string.subscribe_or_login_error_restore_purchase_zype));
                            }
                        }*/
                } else {
                    NavigationHelper.getInstance(this).switchToSubscriptionScreen(this, getIntent().getExtras());
                }
            } else {
            // onCancel();
            }
            break;
        case REQUEST_LOGIN:
            if (resultCode == RESULT_OK) {
                if (SubscriptionHelper.hasSubscription()) {
                    NavigationHelper.getInstance(this).switchToVideoDetailsScreen(this, getIntent().getExtras().getString(BundleConstants.VIDEO_ID), getIntent().getExtras().getString(BundleConstants.PLAYLIST_ID), false);
                    finish();
                } else {
                    NavigationHelper.getInstance(this).switchToSubscriptionScreen(this, getIntent().getExtras());
                }
            } else {
            // onCancel();
            }
            break;
        case REQUEST_SUBSCRIPTION:
            if (resultCode == RESULT_OK) {
                NavigationHelper.getInstance(this).switchToVideoDetailsScreen(this, getIntent().getExtras().getString(BundleConstants.VIDEO_ID), getIntent().getExtras().getString(BundleConstants.PLAYLIST_ID), false);
                finish();
            } else {
            // onCancel();
            }
            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}
Also used : Purchase(com.android.billingclient.api.Purchase) Subscription(com.zype.android.Billing.Subscription)

Example 2 with Subscription

use of com.zype.android.Billing.Subscription in project zype-android by zype.

the class SubscriptionActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_subscription);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    setTitle(R.string.subscription_title);
    initParameters(savedInstanceState);
    listSubscriptions = findViewById(R.id.listSubscriptions);
    adapter = new SubscriptionsAdapter();
    listSubscriptions.setAdapter(adapter);
    layoutLogin = findViewById(R.id.layoutLogin);
    buttonLogin = findViewById(R.id.buttonLogin);
    buttonLogin.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            NavigationHelper.getInstance(SubscriptionActivity.this).switchToLoginScreen(SubscriptionActivity.this);
        }
    });
    layoutLoggedIn = findViewById(R.id.layoutLoggedIn);
    textUsername = findViewById(R.id.textUsername);
    ZypeApp.marketplaceGateway.getSubscriptions().observe(this, new Observer<Map<String, Subscription>>() {

        @Override
        public void onChanged(@Nullable Map<String, Subscription> subscriptions) {
            Log.d("BillingManager", "getSubscriptions: observe: " + subscriptions);
            List<Subscription> subscriptionList = new ArrayList<>();
            for (Map.Entry<String, Subscription> entry : subscriptions.entrySet()) {
                Log.d("BillingManager", "getSubscriptions: map: " + entry.getValue().getMarketplace());
                if (entry.getValue().getMarketplace() != null) {
                    subscriptionList.add(entry.getValue());
                }
            }
            adapter.setData(subscriptionList);
        }
    });
    billingManager = new BillingManager(this, this);
    hideProgress();
    updateViews();
}
Also used : BillingManager(com.zype.android.Billing.BillingManager) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(com.zype.android.Billing.Subscription) Map(java.util.Map)

Example 3 with Subscription

use of com.zype.android.Billing.Subscription in project zype-android by zype.

the class SubscribeOrLoginFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_subscribe_or_login, container, false);
    final NavigationHelper navigationHelper = NavigationHelper.getInstance(getActivity());
    TextView textTitle = rootView.findViewById(R.id.textTitle);
    textTitle.setText(String.format(getString(R.string.subscribe_or_login_title), getString(R.string.app_name)));
    Button buttonSubscribe = rootView.findViewById(R.id.buttonSubscribe);
    buttonSubscribe.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (AuthHelper.isLoggedIn()) {
                navigationHelper.switchToSubscriptionScreen(getActivity(), getActivity().getIntent().getExtras());
            } else {
                navigationHelper.switchToConsumerScreen(getActivity());
            }
        }
    });
    Button buttonLogin = rootView.findViewById(R.id.buttonLogin);
    buttonLogin.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            navigationHelper.switchToLoginScreen(getActivity(), null);
        }
    });
    final List<Purchase> purchases = ZypeApp.marketplaceGateway.getBillingManager().getPurchases();
    Button buttonRestorePurchases = rootView.findViewById(R.id.buttonRestorePurchases);
    buttonRestorePurchases.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (AuthHelper.isLoggedIn()) {
                // Get first purchase from the purchase list and try to validate it
                Subscription subscription = ZypeApp.marketplaceGateway.findSubscriptionBySku(purchases.get(0).getSku());
                if (subscription != null) {
                    showProgress(getString(R.string.subscription_verify));
                    ZypeApp.marketplaceGateway.verifySubscription(subscription).observe(SubscribeOrLoginFragment.this, new Observer<Boolean>() {

                        @Override
                        public void onChanged(@Nullable Boolean result) {
                            hideProgress();
                            if (result) {
                                getActivity().setResult(RESULT_OK);
                                getActivity().finish();
                            } else {
                                DialogHelper.showErrorAlert(getActivity(), getString(R.string.subscribe_or_login_error_validation));
                            }
                        }
                    });
                } else {
                    Logger.e("Not found Zype Plan for existing in-app purchase, sku=" + purchases.get(0).getSku());
                    DialogHelper.showErrorAlert(getActivity(), getString(R.string.subscribe_or_login_error_restore_purchase_zype));
                }
            /*for (String itemSku : purchases.get(0).getSkus()){
                        Subscription subscription = ZypeApp.marketplaceGateway.findSubscriptionBySku(itemSku);
                        if (subscription != null) {
                            showProgress(getString(R.string.subscription_verify));
                            ZypeApp.marketplaceGateway.verifySubscription(subscription).observe(SubscribeOrLoginFragment.this, new Observer<Boolean>() {
                                @Override
                                public void onChanged(@Nullable Boolean result) {
                                    hideProgress();
                                    if (result) {
                                        getActivity().setResult(RESULT_OK);
                                        getActivity().finish();
                                    }
                                    else {
                                        DialogHelper.showErrorAlert(getActivity(),
                                                getString(R.string.subscribe_or_login_error_validation));
                                    }
                                }
                            });
                        }
                        else {
                            Logger.e("Not found Zype Plan for existing in-app purchase, sku=" + itemSku);
                            DialogHelper.showErrorAlert(getActivity(), getString(R.string.subscribe_or_login_error_restore_purchase_zype));
                        }
                    }*/
            } else {
                navigationHelper.switchToConsumerScreen(getActivity());
            }
        }
    });
    if (purchases != null && purchases.size() > 0) {
        Logger.d("There are purchases, size=" + purchases.size());
        buttonRestorePurchases.setVisibility(View.VISIBLE);
    } else {
        Logger.d("There are no purchases on the device");
        buttonRestorePurchases.setVisibility(View.GONE);
    }
    api.subscribe(this);
    return rootView;
}
Also used : Purchase(com.android.billingclient.api.Purchase) NavigationHelper(com.zype.android.ui.NavigationHelper) Button(android.widget.Button) Observer(androidx.lifecycle.Observer) TextView(android.widget.TextView) Subscription(com.zype.android.Billing.Subscription) TextView(android.widget.TextView) View(android.view.View) Nullable(androidx.annotation.Nullable)

Aggregations

Subscription (com.zype.android.Billing.Subscription)3 View (android.view.View)2 TextView (android.widget.TextView)2 Purchase (com.android.billingclient.api.Purchase)2 Button (android.widget.Button)1 Nullable (androidx.annotation.Nullable)1 Observer (androidx.lifecycle.Observer)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 BillingManager (com.zype.android.Billing.BillingManager)1 NavigationHelper (com.zype.android.ui.NavigationHelper)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1