Bug 1374889 - Gather user attributes before MMA inits. r=maliu

MozReview-Commit-ID: 5v28pWzYYWj

--HG--
extra : rebase_source : a36744f125a54e05a29702d75c9449ce4afcaae9
This commit is contained in:
Nevin Chen 2017-07-10 10:16:19 +08:00
Родитель 57864641f5
Коммит 9849094a87
4 изменённых файлов: 44 добавлений и 45 удалений

Просмотреть файл

@ -12,6 +12,7 @@ import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
@ -20,10 +21,14 @@ import org.mozilla.gecko.MmaConstants;
import org.mozilla.gecko.PrefsHelper;
import org.mozilla.gecko.Tab;
import org.mozilla.gecko.Tabs;
import org.mozilla.gecko.fxa.FirefoxAccounts;
import org.mozilla.gecko.preferences.GeckoPreferences;
import org.mozilla.gecko.switchboard.SwitchBoard;
import org.mozilla.gecko.util.ContextUtils;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
public class MmaDelegate {
@ -40,6 +45,12 @@ public class MmaDelegate {
public static final String NEW_TAB = "E_Opened_New_Tab";
public static final String USER_ATT_FOCUS_INSTALLED = "Focus Installed";
public static final String USER_ATT_KLAR_INSTALLED = "Klar Installed";
public static final String USER_ATT_DEFAULT_BROWSER = "Default Browser";
public static final String USER_ATT_SIGNED_IN = "Signed In Sync";
private static final String TAG = "MmaDelegate";
private static final String KEY_PREF_BOOLEAN_MMA_ENABLED = "mma.enabled";
private static final String[] PREFS = { KEY_PREF_BOOLEAN_MMA_ENABLED };
@ -65,7 +76,14 @@ public class MmaDelegate {
if (pref.equals(KEY_PREF_BOOLEAN_MMA_ENABLED)) {
Log.d(TAG, "prefValue() called with: pref = [" + pref + "], value = [" + value + "]");
if (value) {
mmaHelper.init(activity);
// Since user attributes are gathered in Fennec, not in MMA implementation,
// we gather the information here then pass to mmaHelper.init()
// Note that generateUserAttribute always return a non null HashMap.
Map<String, Object> attributes = gatherUserAttributes(activity);
mmaHelper.init(activity, attributes);
if (!isDefaultBrowser(activity)) {
mmaHelper.event(MmaDelegate.LAUNCH_BUT_NOT_DEFAULT_BROWSER);
}
@ -79,6 +97,20 @@ public class MmaDelegate {
PrefsHelper.addObserver(PREFS, handler);
}
/* This method must be called at background thread to avoid performance issues in some API level */
@NonNull
private static Map<String, Object> gatherUserAttributes(final Context context) {
final Map<String, Object> attributes = new HashMap<>();
attributes.put(USER_ATT_FOCUS_INSTALLED, ContextUtils.isPackageInstalled(context, "org.mozilla.focus"));
attributes.put(USER_ATT_KLAR_INSTALLED, ContextUtils.isPackageInstalled(context, "org.mozilla.klar"));
attributes.put(USER_ATT_DEFAULT_BROWSER, isDefaultBrowser(context));
attributes.put(USER_ATT_SIGNED_IN, FirefoxAccounts.firefoxAccountsExist(context));
return attributes;
}
public static void track(String event) {
if (isMmaEnabled()) {
@ -123,5 +155,4 @@ public class MmaDelegate {
return (TextUtils.equals(packageName, context.getPackageName()));
}
}

Просмотреть файл

@ -9,9 +9,11 @@ package org.mozilla.gecko.mma;
import android.app.Activity;
import android.content.Context;
import java.util.Map;
public interface MmaInterface {
void init(Activity Activity);
void init(Activity Activity, Map<String, ?> attributes);
void start(Context context);

Просмотреть файл

@ -9,16 +9,13 @@ package org.mozilla.gecko.mma;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import com.leanplum.Leanplum;
import com.leanplum.LeanplumActivityHelper;
import org.mozilla.gecko.AppConstants;
import org.mozilla.gecko.MmaConstants;
import org.mozilla.gecko.fxa.FirefoxAccounts;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@ -28,7 +25,7 @@ public class MmaLeanplumImp implements MmaInterface {
private static final String KEY_ANDROID_PREF_STRING_LEANPLUM_DEVICE_ID = "android.not_a_preference.leanplum.device_id";
@Override
public void init(final Activity activity) {
public void init(final Activity activity, Map<String, ?> attributes) {
if (activity == null) {
return;
}
@ -42,21 +39,6 @@ public class MmaLeanplumImp implements MmaInterface {
Leanplum.setAppIdForDevelopmentMode(MmaConstants.MOZ_LEANPLUM_SDK_CLIENTID, MmaConstants.MOZ_LEANPLUM_SDK_KEY);
}
Map<String, Object> attributes = new HashMap<>();
boolean installedFocus = isPackageInstalled(activity, "org.mozilla.focus");
boolean installedKlar = isPackageInstalled(activity, "org.mozilla.klar");
if (installedFocus) {
attributes.put("Focus Installed", true);
} else {
attributes.put("Focus Installed", false);
}
if (installedKlar) {
attributes.put("Klar Installed", true);
} else {
attributes.put("Klar Installed", false);
}
final SharedPreferences sharedPreferences = activity.getPreferences(0);
String deviceId = sharedPreferences.getString(KEY_ANDROID_PREF_STRING_LEANPLUM_DEVICE_ID, null);
if (deviceId == null) {
@ -64,22 +46,13 @@ public class MmaLeanplumImp implements MmaInterface {
sharedPreferences.edit().putString(KEY_ANDROID_PREF_STRING_LEANPLUM_DEVICE_ID, deviceId).apply();
}
Leanplum.setDeviceId(deviceId);
if (MmaDelegate.isDefaultBrowser(activity)) {
attributes.put("Default Browser", true);
} else {
attributes.put("Default Browser", false);
}
// In order to trigger the campaign lazily, you check the account existence as an attribute,
// not when account login success callback is invoked. Because the attribute update lazily when process start.
if (FirefoxAccounts.firefoxAccountsExist(activity)) {
attributes.put("Signed In Sync", true);
if (attributes != null) {
Leanplum.start(activity, attributes);
} else {
attributes.put("Signed In Sync", false);
Leanplum.start(activity);
}
Leanplum.start(activity, attributes);
// this is special to Leanplum. Since we defer LeanplumActivityHelper's onResume call till
// switchboard completes loading. We miss the call to LeanplumActivityHelper.onResume.
// So I manually call it here.
@ -119,13 +92,4 @@ public class MmaLeanplumImp implements MmaInterface {
Leanplum.stop();
}
private static boolean isPackageInstalled(final Context context, String packageName) {
try {
PackageManager pm = context.getPackageManager();
pm.getPackageInfo(packageName, 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
}

Просмотреть файл

@ -7,13 +7,14 @@
package org.mozilla.gecko.mma;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import java.util.Map;
public class MmaStubImp implements MmaInterface {
@Override
public void init(Activity activity) {
public void init(Activity activity, Map<String, ?> attributes) {
}
@ -36,4 +37,5 @@ public class MmaStubImp implements MmaInterface {
public void stop() {
}
}