protected void onCreate()

in ClassyTaxiJava/app/src/main/java/com/sample/android/classytaxijava/ui/MainActivity.java [74:164]


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        // Set up the ViewPager with the sections adapter.
        container = findViewById(R.id.container);
        tabs = findViewById(R.id.tabs);
        container.setAdapter(sectionsPagerAdapter);
        container.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabs));
        tabs.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(container));

        authenticationViewModel = ViewModelProviders.of(this).get(FirebaseUserViewModel.class);
        billingViewModel = ViewModelProviders.of(this).get(BillingViewModel.class);
        subscriptionViewModel = ViewModelProviders.of(this).get(SubscriptionStatusViewModel.class);

        billingClientLifecycle = ((SubApp) getApplication()).getBillingClientLifecycle();
        getLifecycle().addObserver(billingClientLifecycle);

        // Register purchases when they change.
        billingClientLifecycle.purchaseUpdateEvent.observe(this, new Observer<List<Purchase>>() {
            @Override
            public void onChanged(List<Purchase> purchases) {
                if (purchases != null) {
                    registerPurchases(purchases);
                }
            }
        });

        // Launch billing flow when user clicks button to buy something.
        billingViewModel.buyEvent.observe(this, new Observer<BillingFlowParams>() {
            @Override
            public void onChanged(BillingFlowParams billingFlowParams) {
                if (billingFlowParams != null) {
                    billingClientLifecycle
                            .launchBillingFlow(MainActivity.this, billingFlowParams);
                }
            }
        });

        // Open the Play Store when event is triggered.
        billingViewModel.openPlayStoreSubscriptionsEvent.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String sku) {
                Log.i(TAG, "Viewing subscriptions on the Google Play Store");
                String url;
                if (sku == null) {
                    // If the SKU is not specified, just open the Google Play subscriptions URL.
                    url = Constants.PLAY_STORE_SUBSCRIPTION_URL;
                } else {
                    // If the SKU is specified, open the deeplink for this SKU on Google Play.
                    url = String.format(Constants.PLAY_STORE_SUBSCRIPTION_DEEPLINK_URL,
                            sku, getApplicationContext().getPackageName());
                }
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);
            }
        });

        // Update authentication UI.
        final Observer<FirebaseUser> fireaseUserObserver = new Observer<FirebaseUser>() {
            @Override
            public void onChanged(@Nullable final FirebaseUser firebaseUser) {
                invalidateOptionsMenu();
                if (firebaseUser == null) {
                    triggerSignIn();
                } else {
                    Log.d(TAG, "Current user: "
                            + firebaseUser.getEmail() + " " + firebaseUser.getDisplayName());
                }
            }
        };
        authenticationViewModel.firebaseUser.observe(this, fireaseUserObserver);

        // Update subscription information when user changes.
        authenticationViewModel.userChangeEvent.observe(this, new Observer<Void>() {
            @Override
            public void onChanged(Void aVoid) {
                subscriptionViewModel.userChanged();
                List<Purchase> purchases = billingClientLifecycle.purchaseUpdateEvent.getValue();
                if (purchases != null) {
                    registerPurchases(purchases);
                }
            }
        });
    }