Android Open Source - Tacere Pro Upgrade Activity






From Project

Back to project page Tacere.

License

The source code is released under:

MIT License

If you think the Android project Tacere listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (c) 2014 Jonathan Nelson/* ww  w  .  j a v a  2 s. c o  m*/
 * Released under the BSD license.  For details see the COPYING file.
 */

package org.ciasaboark.tacere.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.ciasaboark.tacere.R;
import org.ciasaboark.tacere.billing.Authenticator;
import org.ciasaboark.tacere.billing.KeySet;
import org.ciasaboark.tacere.billing.google.IabHelper;
import org.ciasaboark.tacere.billing.google.IabResult;
import org.ciasaboark.tacere.billing.google.Inventory;
import org.ciasaboark.tacere.billing.google.Purchase;
import org.ciasaboark.tacere.billing.google.SkuDetails;
import org.ciasaboark.tacere.notifier.WidgetNotifier;

import java.util.ArrayList;
import java.util.UUID;

public class ProUpgradeActivity extends Activity {
    private static final String TAG = "ProUpgradeActivity";
    private final String uuidString = UUID.randomUUID().toString();
    private final String SKU = KeySet.SKU_UPGRADE_PRO;
    private LinearLayout upgradeIntro;
    private LinearLayout upgradeWaiting;
    private LinearLayout upgradePurchaseNotAvailable;
    private LinearLayout upgradePurchaseAlreadyPurchased;
    private LinearLayout upgradePurchaseAvailable;
    private LinearLayout upgradePurchaseSuccess;
    private LinearLayout upgradePurchaseFailed;
    private String upgradePrice = "unknown";
    private IabHelper mHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pro_upgrade);
        init();
    }

    private void init() {
        upgradeIntro = (LinearLayout) findViewById(R.id.upgrade_intro);
        upgradeWaiting = (LinearLayout) findViewById(R.id.upgrade_wait);
        upgradePurchaseNotAvailable = (LinearLayout) findViewById(R.id.upgrade_purchase_not_available);
        upgradePurchaseAlreadyPurchased = (LinearLayout) findViewById(R.id.upgrade_purchase_already_purchased);
        upgradePurchaseAvailable = (LinearLayout) findViewById(R.id.upgrade_purchase_available);
        upgradePurchaseSuccess = (LinearLayout) findViewById(R.id.upgrade_purchase_success);
        upgradePurchaseFailed = (LinearLayout) findViewById(R.id.upgrade_purchase_failed);

        showIntroOrThanks();
    }

    private void showIntroOrThanks() {
        Authenticator authenticator = new Authenticator(this);
        if (authenticator.isAuthenticated()) {
            showPurchaseAlreadyPurchased();
        } else {
            showIntro();
        }
    }

    private void showPurchaseAlreadyPurchased() {
        upgradeIntro.setVisibility(View.GONE);
        upgradeWaiting.setVisibility(View.GONE);
        upgradePurchaseNotAvailable.setVisibility(View.GONE);
        upgradePurchaseAlreadyPurchased.setVisibility(View.VISIBLE);
        upgradePurchaseAvailable.setVisibility(View.GONE);
        upgradePurchaseSuccess.setVisibility(View.GONE);
        upgradePurchaseFailed.setVisibility(View.GONE);

        Button closeButton = (Button) findViewById(R.id.upgrade_purchase_already_purchased_close_button);
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void showIntro() {
        upgradeIntro.setVisibility(View.VISIBLE);
        upgradeWaiting.setVisibility(View.GONE);
        upgradePurchaseNotAvailable.setVisibility(View.GONE);
        upgradePurchaseAlreadyPurchased.setVisibility(View.GONE);
        upgradePurchaseAvailable.setVisibility(View.GONE);
        upgradePurchaseSuccess.setVisibility(View.GONE);
        upgradePurchaseFailed.setVisibility(View.GONE);

        Button startUpgradeButton = (Button) findViewById(R.id.upgrades_start_upgrade);
        startUpgradeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                beginUpgrade();
            }
        });

        Button cancelUpgradeButton = (Button) findViewById(R.id.upgrades_cancel_upgrade);
        cancelUpgradeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void beginUpgrade() {
        upgradeIntro.setVisibility(View.GONE);
        upgradeWaiting.setVisibility(View.VISIBLE);
        upgradePurchaseNotAvailable.setVisibility(View.GONE);
        upgradePurchaseAlreadyPurchased.setVisibility(View.GONE);
        upgradePurchaseAvailable.setVisibility(View.GONE);
        upgradePurchaseSuccess.setVisibility(View.GONE);
        upgradePurchaseFailed.setVisibility(View.GONE);

        Log.d(TAG, "beginning upgrade process");
        mHelper = new IabHelper(this, KeySet.GOOGLE_LICENSE_KEY);
        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
            @Override
            public void onIabSetupFinished(IabResult result) {
                Log.d(TAG, "upgrade setup finished");
                if (result.isSuccess()) {
                    Log.d(TAG, "success, in-app billing is available");
                    // Fill a list of SKUs that we want the price infos for
                    // (SKU = "stockable unit" = buyable things)
                    ArrayList<String> moreSkus = new ArrayList<String>();
                    moreSkus.add(SKU);

                    // We initialize the price field with a "retrieving price" message while we wait
                    // for the price
//                    final TextView tvPrice = (TextView)BuyPremiumActivity.this.findViewById(R.id.price);
//                    tvPrice.setText(R.string.waiting_for_price);

                    // Start the query for the details for the SKUs. This runs asynchronously, so
                    // it may be that the price appears a bit later after the rest of the Activity is shown.
                    mHelper.queryInventoryAsync(true, moreSkus, new IabHelper.QueryInventoryFinishedListener() {
                        @Override
                        public void onQueryInventoryFinished(IabResult result, Inventory inv) {
                            if (result.isSuccess()) {
                                // If we successfully got the price, show it in the text field
                                SkuDetails details = inv.getSkuDetails(SKU);
                                String price = details.getPrice();

                                // On successful init and price getting, enable the "buy me" button
                                showPurchaseAvailable();
                            } else {
                                // Error getting the price... show a sorry text in the price field now
                                showPurchaseNotAvailable("Error getting pricing information " + result.getMessage());
                            }
                        }
                    });
                } else {
                    Log.d(TAG, "Unable to initialize the in app billing process");
                    // If the billing API could not be initialized at all, show a sorry dialog. This
                    // will surely prevent the user from being able to buy anything.
                    showPurchaseNotAvailable("Unable to initialize the in app billing process: " + result.getMessage());
                }
            }
        });
    }

    private void showPurchaseAvailable() {
        upgradeIntro.setVisibility(View.GONE);
        upgradeWaiting.setVisibility(View.GONE);
        upgradePurchaseNotAvailable.setVisibility(View.GONE);
        upgradePurchaseAlreadyPurchased.setVisibility(View.GONE);
        upgradePurchaseAvailable.setVisibility(View.VISIBLE);
        upgradePurchaseSuccess.setVisibility(View.GONE);
        upgradePurchaseFailed.setVisibility(View.GONE);

        IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
            public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
                Authenticator authenticator = new Authenticator(getApplicationContext());
                if (result.isFailure() || purchase == null) {
                    authenticator.setAuthenticateSuccess(false);
                    switch (result.getResponse()) {
                        case IabHelper.IABHELPER_USER_CANCELLED:
                            finish();
                            break;
                        case IabHelper.BILLING_RESPONSE_RESULT_DEVELOPER_ERROR:
                            showPurchaseFailed("Something went wrong during the purchase process.  Please try again later");
                            break;
                        case IabHelper.BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED:
                            showPurchaseAlreadyPurchased();
                            break;
                        default:
                            showPurchaseFailed("Purchase failed (" + result.getMessage() + ").  Please try again later");
                    }
                } else if (purchase.getSku().equals(SKU)) {
                    String developerPayload = purchase.getDeveloperPayload();
                    if (TextUtils.equals(uuidString, developerPayload)) {
                        authenticator.setAuthenticateSuccess(true);
                        showPurchaseSuccess();
                    } else {
                        showPurchaseFailed("Unable to verify the purchase details.  This should not have happened.  Please contact the developer for more information.");
                    }
                } else {
                    //something was purchased, but I don't know what it was
                    authenticator.setAuthenticateSuccess(false);
                    showPurchaseFailed("The Google Play Store has notified me that something was purchased, buy I have no idea what it was.  This should not have happened.  Please contact the developer for more information");
                }

            }
        };

        //launch the in app billing activity
        mHelper.launchPurchaseFlow(this, SKU, 1, mPurchaseFinishedListener,
                uuidString);
    }

    private void showPurchaseNotAvailable(String message) {
        upgradeIntro.setVisibility(View.GONE);
        upgradeWaiting.setVisibility(View.GONE);
        upgradePurchaseNotAvailable.setVisibility(View.VISIBLE);
        upgradePurchaseAlreadyPurchased.setVisibility(View.GONE);
        upgradePurchaseAvailable.setVisibility(View.GONE);
        upgradePurchaseSuccess.setVisibility(View.GONE);
        upgradePurchaseFailed.setVisibility(View.GONE);
        TextView responseText = (TextView) findViewById(R.id.upgrade_purchase_not_available_text);
        responseText.setText("Purchase not available, " + message);
        //TODO
    }

    private void showPurchaseFailed(String reason) {
        upgradeIntro.setVisibility(View.GONE);
        upgradeWaiting.setVisibility(View.GONE);
        upgradePurchaseNotAvailable.setVisibility(View.GONE);
        upgradePurchaseAlreadyPurchased.setVisibility(View.GONE);
        upgradePurchaseAvailable.setVisibility(View.GONE);
        upgradePurchaseSuccess.setVisibility(View.GONE);
        upgradePurchaseFailed.setVisibility(View.VISIBLE);

        TextView failedMessage = (TextView) findViewById(R.id.upgrade_purchase_failed_message);
        failedMessage.setText(reason);

        Button closeButton = (Button) findViewById(R.id.upgrade_purchase_failed_close_button);
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void showPurchaseSuccess() {
        upgradeIntro.setVisibility(View.GONE);
        upgradeWaiting.setVisibility(View.GONE);
        upgradePurchaseNotAvailable.setVisibility(View.GONE);
        upgradePurchaseAlreadyPurchased.setVisibility(View.GONE);
        upgradePurchaseAvailable.setVisibility(View.GONE);
        upgradePurchaseSuccess.setVisibility(View.VISIBLE);
        upgradePurchaseFailed.setVisibility(View.GONE);

        Button closeButton = (Button) findViewById(R.id.upgrade_purchase_success_close_button);
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        WidgetNotifier widgetNotifier = new WidgetNotifier(this);
        widgetNotifier.updateAllWidgets();

        if (mHelper != null) mHelper.dispose();
        mHelper = null;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.pro_upgrade, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}




Java Source Code List

.EventInstanceTest.java
com.viewpagerindicator.BuildConfig.java
com.viewpagerindicator.BuildConfig.java
com.viewpagerindicator.CirclePageIndicator.java
com.viewpagerindicator.IconPageIndicator.java
com.viewpagerindicator.IconPagerAdapter.java
com.viewpagerindicator.IcsLinearLayout.java
com.viewpagerindicator.LinePageIndicator.java
com.viewpagerindicator.PageIndicator.java
com.viewpagerindicator.TabPageIndicator.java
com.viewpagerindicator.TitlePageIndicator.java
com.viewpagerindicator.UnderlinePageIndicator.java
org.ciasaboark.tacere.ApplicationTest.java
org.ciasaboark.tacere.Tacere.java
org.ciasaboark.tacere.activity.AboutActivity.java
org.ciasaboark.tacere.activity.AboutLicenseActivity.java
org.ciasaboark.tacere.activity.AdvancedSettingsActivity.java
org.ciasaboark.tacere.activity.BetaReleaseActivity.java
org.ciasaboark.tacere.activity.BetaSettingsActivity.java
org.ciasaboark.tacere.activity.BugReportActivity.java
org.ciasaboark.tacere.activity.DonationActivityTest.java
org.ciasaboark.tacere.activity.LicenseCheckWrapper.java
org.ciasaboark.tacere.activity.MainActivity.java
org.ciasaboark.tacere.activity.ProUpgradeActivity.java
org.ciasaboark.tacere.activity.SelectCalendarsActivity.java
org.ciasaboark.tacere.activity.SettingsActivity.java
org.ciasaboark.tacere.activity.ShowUpdatesActivity.java
org.ciasaboark.tacere.activity.TutorialActivity.java
org.ciasaboark.tacere.activity.fragment.AdvancedSettingsFragment.java
org.ciasaboark.tacere.activity.fragment.BetaSettingsFragment.java
org.ciasaboark.tacere.activity.fragment.EventDetailsFragment.java
org.ciasaboark.tacere.activity.fragment.MainSettingsFragment.java
org.ciasaboark.tacere.activity.fragment.SelectCalendarsFragment.java
org.ciasaboark.tacere.activity.fragment.TutorialCrashReporterFragment.java
org.ciasaboark.tacere.activity.fragment.TutorialEndFragment.java
org.ciasaboark.tacere.activity.fragment.TutorialEventListFragment.java
org.ciasaboark.tacere.activity.fragment.TutorialProVersionFragment.java
org.ciasaboark.tacere.activity.fragment.TutorialRingerPriorityFragment.java
org.ciasaboark.tacere.activity.fragment.TutorialWelcomeFragment.java
org.ciasaboark.tacere.adapter.EventCursorAdapter.java
org.ciasaboark.tacere.billing.Authenticator.java
org.ciasaboark.tacere.billing.google.Base64DecoderException.java
org.ciasaboark.tacere.billing.google.Base64.java
org.ciasaboark.tacere.billing.google.IabException.java
org.ciasaboark.tacere.billing.google.IabHelper.java
org.ciasaboark.tacere.billing.google.IabResult.java
org.ciasaboark.tacere.billing.google.Inventory.java
org.ciasaboark.tacere.billing.google.Purchase.java
org.ciasaboark.tacere.billing.google.Security.java
org.ciasaboark.tacere.billing.google.SkuDetails.java
org.ciasaboark.tacere.bug.CrashReportManager.java
org.ciasaboark.tacere.converter.DateConverterTest.java
org.ciasaboark.tacere.converter.DateConverter.java
org.ciasaboark.tacere.converter.TimeConverter.java
org.ciasaboark.tacere.database.Columns.java
org.ciasaboark.tacere.database.DataSetManager.java
org.ciasaboark.tacere.database.DatabaseInterface.java
org.ciasaboark.tacere.database.EventDatabaseOpenHelper.java
org.ciasaboark.tacere.database.NoSuchEventInstanceException.java
org.ciasaboark.tacere.database.TooltipManager.java
org.ciasaboark.tacere.event.Calendar.java
org.ciasaboark.tacere.event.EventInstance.java
org.ciasaboark.tacere.event.EventManager.java
org.ciasaboark.tacere.event.ringer.Intervals.java
org.ciasaboark.tacere.event.ringer.RingerSource.java
org.ciasaboark.tacere.event.ringer.RingerType.java
org.ciasaboark.tacere.key.ApplicationTest.java
org.ciasaboark.tacere.key.MainActivity.java
org.ciasaboark.tacere.manager.AlarmManagerWrapper.java
org.ciasaboark.tacere.manager.NotificationManagerWrapper.java
org.ciasaboark.tacere.manager.RingerStateManager.java
org.ciasaboark.tacere.manager.ServiceStateManager.java
org.ciasaboark.tacere.manager.VolumesManager.java
org.ciasaboark.tacere.notifier.WidgetNotifier.java
org.ciasaboark.tacere.prefs.BetaPrefs.java
org.ciasaboark.tacere.prefs.DefaultPrefs.java
org.ciasaboark.tacere.prefs.PrefsTest.java
org.ciasaboark.tacere.prefs.Prefs.java
org.ciasaboark.tacere.prefs.Updates.java
org.ciasaboark.tacere.receiver.BootCompletedReceiver.java
org.ciasaboark.tacere.receiver.ProviderChangedReceiver.java
org.ciasaboark.tacere.service.EventSilencerService.java
org.ciasaboark.tacere.service.ExtendEventService.java
org.ciasaboark.tacere.service.ExtendQuicksilenceService.java
org.ciasaboark.tacere.service.RequestTypes.java
org.ciasaboark.tacere.service.ResetEventServiceTest.java
org.ciasaboark.tacere.service.ResetEventService.java
org.ciasaboark.tacere.service.SkipEventService.java
org.ciasaboark.tacere.versioning.Versioning.java
org.ciasaboark.tacere.view.EventListItem.java
org.ciasaboark.tacere.widget.ActiveEventWidgetProvider.java
org.ciasaboark.tacere.widget.EventListWidgetProvider.java
org.ciasaboark.tacere.widget.QuickSilenceTinyWidgetProvider.java
org.ciasaboark.tacere.widget.QuickSilenceWidgetProvider.java
org.ciasaboark.tacere.widget.activity.WidgetPopupRinger.java
org.ciasaboark.tacere.widget.adapter.WidgetEventListAdapter.java
org.ciasaboark.tacere.widget.service.WidgetService.java