michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko; michael@0: michael@0: import org.json.JSONException; michael@0: import org.json.JSONObject; michael@0: michael@0: import android.content.BroadcastReceiver; michael@0: import android.content.Context; michael@0: import android.content.Intent; michael@0: import android.util.Log; michael@0: michael@0: import java.net.URLDecoder; michael@0: import java.util.HashMap; michael@0: michael@0: public class ReferrerReceiver michael@0: extends BroadcastReceiver michael@0: { michael@0: private static final String LOGTAG = "GeckoReferrerReceiver"; michael@0: michael@0: public static final String ACTION_INSTALL_REFERRER = "com.android.vending.INSTALL_REFERRER"; michael@0: public static final String UTM_SOURCE = "mozilla"; michael@0: michael@0: @Override michael@0: public void onReceive(Context context, Intent intent) { michael@0: if (ACTION_INSTALL_REFERRER.equals(intent.getAction())) { michael@0: String referrer = intent.getStringExtra("referrer"); michael@0: if (referrer == null) michael@0: return; michael@0: michael@0: HashMap values = new HashMap(); michael@0: try { michael@0: String referrers[] = referrer.split("&"); michael@0: for (String referrerValue : referrers) { michael@0: String keyValue[] = referrerValue.split("="); michael@0: values.put(URLDecoder.decode(keyValue[0]), URLDecoder.decode(keyValue[1])); michael@0: } michael@0: } catch (Exception e) { michael@0: } michael@0: michael@0: String source = values.get("utm_source"); michael@0: String campaign = values.get("utm_campaign"); michael@0: michael@0: if (source != null && UTM_SOURCE.equals(source) && campaign != null) { michael@0: try { michael@0: JSONObject data = new JSONObject(); michael@0: data.put("id", "playstore"); michael@0: data.put("version", campaign); michael@0: michael@0: // Try to make sure the prefs are written as a group michael@0: GeckoEvent event = GeckoEvent.createBroadcastEvent("Campaign:Set", data.toString()); michael@0: GeckoAppShell.sendEventToGecko(event); michael@0: } catch (JSONException e) { michael@0: Log.e(LOGTAG, "Error setting distribution", e); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: }