Skip to content

Commit

Permalink
Merge pull request #5080 from brave/pr5076_android_ref_code_migrate_1…
Browse files Browse the repository at this point in the history
….5.x

Added ref code to migration process (uplift to 1.5.x)
  • Loading branch information
kjozwiak authored Mar 27, 2020
2 parents f228f08 + 2eaacf9 commit 69541a6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
Expand Down
29 changes: 29 additions & 0 deletions browser/android/preferences/brave_pref_service_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<jstring>& promoCode) {
return g_browser_process->local_state()->SetString(
kReferralPromoCode, ConvertJavaStringToUTF8(env, promoCode));
}

void JNI_BravePrefServiceBridge_SetReferralDownloadId(
JNIEnv* env,
const JavaParamRef<jstring>& downloadId) {
return g_browser_process->local_state()->SetString(
kReferralDownloadID, ConvertJavaStringToUTF8(env, downloadId));
}

static jboolean JNI_BravePrefServiceBridge_GetBoolean(
JNIEnv* env,
const jint j_pref_index) {
Expand Down

0 comments on commit 69541a6

Please sign in to comment.