diff --git a/android/java/org/chromium/chrome/browser/preferences/BravePrefServiceBridge.java b/android/java/org/chromium/chrome/browser/preferences/BravePrefServiceBridge.java index c84eae4d844d..954e64299c78 100644 --- a/android/java/org/chromium/chrome/browser/preferences/BravePrefServiceBridge.java +++ b/android/java/org/chromium/chrome/browser/preferences/BravePrefServiceBridge.java @@ -189,6 +189,22 @@ public void setString(@Pref int preference, @NonNull String value) { BravePrefServiceBridgeJni.get().setString(preference, value); } + public void setReferralAndroidFirstRunTimestamp(long time) { + BravePrefServiceBridgeJni.get().setReferralAndroidFirstRunTimestamp(time); + } + + public void setReferralCheckedForPromoCodeFile(boolean value) { + BravePrefServiceBridgeJni.get().setReferralCheckedForPromoCodeFile(value); + } + + public void setReferralPromoCode(String promoCode) { + BravePrefServiceBridgeJni.get().setReferralPromoCode(promoCode); + } + + public void setReferralDownloadId(String downloadId) { + BravePrefServiceBridgeJni.get().setReferralDownloadId(downloadId); + } + @NativeMethods interface Natives { void setHTTPSEEnabled(boolean enabled); @@ -236,5 +252,9 @@ interface Natives { String getString(int preference); void setString(int preference, String value); + void setReferralAndroidFirstRunTimestamp(long time); + void setReferralCheckedForPromoCodeFile(boolean value); + void setReferralPromoCode(String promoCode); + void setReferralDownloadId(String downloadId); } } diff --git a/android/java/org/chromium/chrome/browser/upgrade/BraveUpgradeJobIntentService.java b/android/java/org/chromium/chrome/browser/upgrade/BraveUpgradeJobIntentService.java index 82347b919b80..a47850409084 100644 --- a/android/java/org/chromium/chrome/browser/upgrade/BraveUpgradeJobIntentService.java +++ b/android/java/org/chromium/chrome/browser/upgrade/BraveUpgradeJobIntentService.java @@ -7,6 +7,8 @@ import android.content.Context; import android.content.Intent; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager.NameNotFoundException; import java.util.Locale; import android.content.SharedPreferences; import android.support.v4.app.JobIntentService; @@ -65,6 +67,8 @@ public class BraveUpgradeJobIntentService extends JobIntentService { // To detect update from tabs private static final String PREF_STATS_PREFERENCES_NAME = "StatsPreferences"; private static final String PREF_WEEK_OF_INSTALLATION_NAME = "WeekOfInstallation"; + private static final String PREF_STATS_URPC_NAME = "UserReferalProgramCode"; + private static final String PREF_STATS_DOWNLOAD_ID_NAME = "DownloadId"; public static void startMigrationIfNecessary(Context context) { if (BraveUpgradeJobIntentService.needToMigratePreferences()) { @@ -233,6 +237,26 @@ private boolean migrateTotalStatsAndPreferences() { if (!BraveUpgradeJobIntentService.needToMigratePreferences()) { return needToRestart; } + + // Migrate referral data + SharedPreferences prefStatsFromTabs = ContextUtils.getApplicationContext() + .getSharedPreferences(PREF_STATS_PREFERENCES_NAME, 0); + String urpc = prefStatsFromTabs.getString(PREF_STATS_URPC_NAME, null); + if (urpc != null && !urpc.isEmpty()) { + BravePrefServiceBridge.getInstance().setReferralPromoCode(urpc); + BravePrefServiceBridge.getInstance().setReferralCheckedForPromoCodeFile(true); + String downloadId = prefStatsFromTabs.getString(PREF_STATS_DOWNLOAD_ID_NAME, ""); + BravePrefServiceBridge.getInstance().setReferralDownloadId(downloadId); + PackageInfo info = null; + try { + info = ContextUtils.getApplicationContext().getPackageManager() + .getPackageInfo(ContextUtils.getApplicationContext().getPackageName(), 0); + BravePrefServiceBridge.getInstance().setReferralAndroidFirstRunTimestamp(info.firstInstallTime); + } catch (NameNotFoundException e) { + Log.e(TAG, "Could not get package info: " + e); + } + } + SharedPreferences sharedPreferences = ContextUtils.getAppSharedPreferences(); // Total stats migration long trackersBlockedCount = sharedPreferences.getLong(PREF_TRACKERS_BLOCKED_COUNT, 0); diff --git a/browser/android/preferences/brave_pref_service_bridge.cc b/browser/android/preferences/brave_pref_service_bridge.cc index d471ceefc52d..c2abb83527fd 100644 --- a/browser/android/preferences/brave_pref_service_bridge.cc +++ b/browser/android/preferences/brave_pref_service_bridge.cc @@ -14,6 +14,7 @@ #include "brave/components/brave_sync/brave_sync_prefs.h" #include "chrome/browser/android/preferences/pref_service_bridge.h" #include "chrome/browser/content_settings/host_content_settings_map_factory.h" +#include "chrome/browser/browser_process.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_android.h" #include "chrome/browser/profiles/profile_manager.h" @@ -245,6 +246,34 @@ jboolean JNI_BravePrefServiceBridge_GetBooleanForContentSetting(JNIEnv* env, return false; } +void JNI_BravePrefServiceBridge_SetReferralAndroidFirstRunTimestamp( + JNIEnv* env, + jlong time) { + return g_browser_process->local_state()->SetTime( + kReferralAndroidFirstRunTimestamp, base::Time::FromJavaTime(time)); +} + +void JNI_BravePrefServiceBridge_SetReferralCheckedForPromoCodeFile( + JNIEnv* env, + jboolean value) { + return g_browser_process->local_state()->SetBoolean( + kReferralCheckedForPromoCodeFile, value); +} + +void JNI_BravePrefServiceBridge_SetReferralPromoCode( + JNIEnv* env, + const JavaParamRef& promoCode) { + return g_browser_process->local_state()->SetString( + kReferralPromoCode, ConvertJavaStringToUTF8(env, promoCode)); +} + +void JNI_BravePrefServiceBridge_SetReferralDownloadId( + JNIEnv* env, + const JavaParamRef& downloadId) { + return g_browser_process->local_state()->SetString( + kReferralDownloadID, ConvertJavaStringToUTF8(env, downloadId)); +} + static jboolean JNI_BravePrefServiceBridge_GetBoolean( JNIEnv* env, const jint j_pref_index) {