Android Open Source - GADemoGTM Splash Screen






From Project

Back to project page GADemoGTM.

License

The source code is released under:

GNU General Public License

If you think the Android project GADemoGTM 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

package com.pdro.gademogtm;
/*from   w ww. jav  a 2s  .c  o m*/
import java.util.concurrent.TimeUnit;

import com.pdro.gademogtm.gtm.ContainerHolderSingleton;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.tagmanager.Container;
import com.google.android.gms.tagmanager.ContainerHolder;
import com.google.android.gms.tagmanager.TagManager;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

/**
 * Displays simple splash screen while GTM container is loading.
 * Launches the {@link MainActivity} once the container is loaded.
 */
// todo clean up splash screen
public class SplashScreen extends Activity {

    // class name for a logging message
    private static final String TAG = SplashScreen.class.getCanonicalName();

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

        // GTM code
        TagManager myTagManager = TagManager.getInstance(this);

        /* Modify the log level of the logger to print out not only warning and error messages,
         *  but also verbose, debug, info messages.
         */
        myTagManager.setVerboseLoggingEnabled(true);

        
        /* Use the TagManager singleton to make a request to load a container, specifying a
         *  Google Tag Manager container ID as well as your default container file. The call to
         *  loadContainerPreferNonDefault() is non-blocking and returns a PendingResult
         *  (which is an object returned from the Google Play Services)
         */
        PendingResult<ContainerHolder> pending =
                myTagManager.loadContainerPreferNonDefault(getString(R.string.gtmContainer),
                        R.raw.gtm_default_container);

        /* The onResult method will be called as soon as one of the following happens:
         *  1. a saved container is loaded
         *  2. if there is no saved container, a network container is loaded
         *  3. the request times out. The example below uses a constant to manage the timeout period
         */
        pending.setResultCallback(new ResultCallback<ContainerHolder>() {
            @Override
            public void onResult(ContainerHolder containerHolder) {
                //
                ContainerHolderSingleton.setContainerHolder(containerHolder);
                Container container = containerHolder.getContainer();
                if (!containerHolder.getStatus().isSuccess()) {
                    Log.e("Test App", "failure loading container");
                    displayErrorToUser(R.string.load_error);
                    return;
                }
                ContainerHolderSingleton.setContainerHolder(containerHolder);
                ContainerLoadedCallback.registerCallbacksForContainer(container);
                containerHolder.setContainerAvailableListener(new ContainerLoadedCallback());

            }
        }, 5, TimeUnit.SECONDS);
        startMainActivity();
    }

    private static class ContainerLoadedCallback implements
            ContainerHolder.ContainerAvailableListener {
        @Override
        public void onContainerAvailable(ContainerHolder containerHolder,
                                         String containerVersion) {
            // Load each container when it becomes available
            Container container = containerHolder.getContainer();
            registerCallbacksForContainer(container);
        }

        public static void registerCallbacksForContainer(Container container) {
            Log.e("Logging a Callback!", TAG);
        }
    }

    /**
     * Looks up the externalized string resource and displays it in a pop-up dialog box.
     *
     * @param stringKey
     */
    private void displayErrorToUser(int stringKey) {
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Error from code - check code for this message!");
        alertDialog.setMessage(getResources().getString(stringKey));
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE,
                "OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
        alertDialog.show();
    }

    private void startMainActivity() {
        Intent intent = new Intent(SplashScreen.this, MainActivity.class);
        startActivity(intent);
    }
}




Java Source Code List

com.pdro.gademogtm.MainActivity.java
com.pdro.gademogtm.MessageActivity.java
com.pdro.gademogtm.SplashScreen.java
com.pdro.gademogtm.gtm.ContainerHolderSingleton.java
com.pdro.gademogtm.gtm.Utils.java
com.pdro.gademogtm.tools.ColorWheel.java
com.pdro.gademogtm.tools.FactBook.java