Skip to content

Instantly share code, notes, and snippets.

@omarmiatello
Last active December 23, 2015 17:39
Show Gist options
  • Save omarmiatello/6669853 to your computer and use it in GitHub Desktop.
Save omarmiatello/6669853 to your computer and use it in GitHub Desktop.
Simplfy TagManager with shortcut
/**
* Copyright 2013 Omar Miatello - omar.miatello@justonetouch.it
*
* 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 it.justonetouch.utils.tagmanager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import com.google.tagmanager.Container;
import com.google.tagmanager.ContainerOpener;
import com.google.tagmanager.TagManager;
import it.justonetouch.demo.BuildConfig;
/**
* Singleton to hold the GTM Container (since it should be only created once
* per run of the app).
*/
public class TagManagerUtils {
public static final String LOG_TAG = TagManagerUtils.class.getSimpleName();
private static Container container;
private static ContainerOpener.ContainerFuture containerFuture;
/**
* Utility class: don't instantiate.
*/
private TagManagerUtils() {}
public static String getContainerId(Context context) {
try {
ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
String CONTAINER_ID = ai.metaData.getString("com.google.android.tagmanager.CONTAINER_ID");
if (CONTAINER_ID != null) return CONTAINER_ID;
} catch (PackageManager.NameNotFoundException e) { // PackageManager.GET_META_DATA not exist
throw new RuntimeException(e); // Never throw (maybe)
}
Log.e(LOG_TAG, "Missing manifest.xml config in <application>: <meta-data android:name=\"com.google.android.tagmanager.CONTAINER_ID\" android:value=\"GTM-xxx\"></meta-data>");
return null;
}
public static ContainerOpener.ContainerFuture startSyncContainerRequest(Context context) {
return startSyncContainerRequest(context, ContainerOpener.OpenType.PREFER_NON_DEFAULT, ContainerOpener.DEFAULT_TIMEOUT_IN_MILLIS);
}
public static ContainerOpener.ContainerFuture startSyncContainerRequest(Context context, ContainerOpener.OpenType openType, Long time) {
containerFuture = ContainerOpener.openContainer(_getTagManager(context), getContainerId(context), openType, time);
return containerFuture;
}
public static void asyncContainer(Context context, ContainerOpener.OpenType openType, Long time, final ContainerOpener.Notifier callback) {
ContainerOpener.openContainer(_getTagManager(context), getContainerId(context), openType, time, new ContainerOpener.Notifier() {
@Override
public void containerAvailable(Container container) {
_setContainer(container);
TagManagerUtils.container = container;
if (container != null) callback.containerAvailable(container);
}
});
}
private static TagManager _getTagManager(Context context) {
TagManager tagManager = TagManager.getInstance(context);
return tagManager;
}
public static void asyncContainer(Context context, final ContainerOpener.Notifier callback) {
asyncContainer(context, ContainerOpener.OpenType.PREFER_NON_DEFAULT, ContainerOpener.DEFAULT_TIMEOUT_IN_MILLIS, callback);
}
public static boolean hasContainer() {
return container != null;
}
public static Container getContainer(Context context) {
if (!hasContainer()) {
if (containerFuture == null) {
startSyncContainerRequest(context);
}
_setContainer(containerFuture.get());
containerFuture = null;
}
return container;
}
private static void _setContainer(Container c) {
if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "Force refresh if BuildConfig.DEBUG = true");
c.refresh();
}
container = c;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.justonetouch.demo"
android:versionCode="1"
android:versionName="1.0" >
<!-- TODO: The following permission is required to use all remote service. -->
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Example" >
<!-- TODO: INSERT CONTAINER_ID -->
<meta-data
android:name="com.google.android.tagmanager.CONTAINER_ID"
android:value="GTM-XXXXX-INSERT-CODE-HERE" />
<!-- Add activity / provider / service here. -->
</application>
</manifest>
import it.justonetouch.utils.tagmanager.TagManagerUtils;
class RssRequestExampleWithTagManagerUtils {
private static final String MY_COLOR = "mycolor";
private void getColor() {
Log.i(TAG, "Looking for " + MY_COLOR);
Container container = TagManagerUtils.getContainer(getContext());
String my_color = container.getString(MY_COLOR);
Log.i(TAG, "Retriving color: " + my_color);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment