Creates a mojo channel between browser and renderer for Leo subscriptions on Android

This commit is contained in:
Serg
2023-12-15 07:30:33 -05:00
parent 6f4a13dff1
commit 284379d904
20 changed files with 338 additions and 53 deletions
@@ -355,6 +355,7 @@ public class InAppPurchaseWrapper {
if (isVPNProduct) {
BraveVpnPrefUtils.setSubscriptionPurchase(true);
} else if (isLeoProduct) {
BraveLeoPrefUtils.setIsSubscriptionActive(true);
}
}
}
@@ -9,6 +9,7 @@ import org.jni_zero.JNINamespace;
import org.jni_zero.NativeMethods;
import org.chromium.ai_chat.mojom.CredentialManagerHelper;
import org.chromium.ai_chat.mojom.PremiumStatus;
import org.chromium.content_public.browser.BrowserContextHandle;
import org.chromium.mojo.bindings.ConnectionErrorHandler;
import org.chromium.mojo.bindings.Interface;
@@ -62,6 +63,7 @@ public class BraveLeoCMHelper implements ConnectionErrorHandler {
public void getPremiumStatus(CredentialManagerHelper.GetPremiumStatus_Response callback) {
if (mCredentialManagerHelper == null) {
callback.call(PremiumStatus.INACTIVE);
return;
}
mCredentialManagerHelper.getPremiumStatus(callback);
@@ -5,6 +5,7 @@
package org.chromium.chrome.browser.brave_leo;
import org.chromium.base.ContextUtils;
import org.chromium.base.Log;
import org.chromium.chrome.browser.app.BraveActivity;
import org.chromium.chrome.browser.preferences.BravePref;
@@ -30,12 +31,52 @@ public class BraveLeoPrefUtils {
}
public static void setIsSubscriptionActive(boolean value) {
UserPrefs.get(BraveLeoPrefUtils.getProfile())
Profile profileToUse = BraveLeoPrefUtils.getProfile();
if (profileToUse == null) {
Log.e(TAG, "BraveLeoPrefUtils.setIsSubscriptionActive profile is null");
return;
}
UserPrefs.get(profileToUse)
.setBoolean(BravePref.BRAVE_CHAT_SUBSCRIPTION_ACTIVE_ANDROID, value);
}
public static boolean getIsSubscriptionActive(Profile profile) {
return UserPrefs.get(profile == null ? BraveLeoPrefUtils.getProfile() : profile)
Profile profileToUse = profile == null ? BraveLeoPrefUtils.getProfile() : profile;
if (profileToUse == null) {
Log.e(TAG, "BraveLeoPrefUtils.getIsSubscriptionActive profile is null");
return false;
}
return UserPrefs.get(profileToUse)
.getBoolean(BravePref.BRAVE_CHAT_SUBSCRIPTION_ACTIVE_ANDROID);
}
public static void setChatPurchaseToken(String token) {
Profile profileToUse = BraveLeoPrefUtils.getProfile();
if (profileToUse == null) {
Log.e(TAG, "BraveLeoPrefUtils.setChatPurchaseToken profile is null");
return;
}
UserPrefs.get(profileToUse).setString(BravePref.BRAVE_CHAT_PURCHASE_TOKEN_ANDROID, token);
}
public static void setChatPackageName() {
Profile profileToUse = BraveLeoPrefUtils.getProfile();
if (profileToUse == null) {
Log.e(TAG, "BraveLeoPrefUtils.setChatPackageName profile is null");
return;
}
UserPrefs.get(profileToUse)
.setString(
BravePref.BRAVE_CHAT_PACKAGE_NAME_ANDROID,
ContextUtils.getApplicationContext().getPackageName());
}
public static void setChatProductId(String productId) {
Profile profileToUse = BraveLeoPrefUtils.getProfile();
if (profileToUse == null) {
Log.e(TAG, "BraveLeoPrefUtils.setChatProductId profile is null");
return;
}
UserPrefs.get(profileToUse).setString(BravePref.BRAVE_CHAT_PRODUCT_ID_ANDROID, productId);
}
}
@@ -5,9 +5,7 @@
package org.chromium.chrome.browser.brave_leo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import org.jni_zero.CalledByNative;
@@ -31,11 +29,14 @@ public class BraveLeoSettingsLauncherHelper {
@CalledByNative
private static void goPremium(WebContents webContents) {
Activity activity = webContents.getTopLevelNativeWindow().getActivity().get();
Intent braveLeoPlansIntent = new Intent(activity, BraveLeoPlansActivity.class);
braveLeoPlansIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
braveLeoPlansIntent.setAction(Intent.ACTION_VIEW);
activity.startActivity(braveLeoPlansIntent);
// TODO(sergz): We need to uncomment that section when our backend can handle
// mobile subscription. It's commented to avoid Purchase happens on Google Play Store
// Activity activity = webContents.getTopLevelNativeWindow().getActivity().get();
// Intent braveLeoPlansIntent = new Intent(activity, BraveLeoPlansActivity.class);
// braveLeoPlansIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// braveLeoPlansIntent.setAction(Intent.ACTION_VIEW);
// activity.startActivity(braveLeoPlansIntent);
}
private static SettingsLauncher getLauncher() {
@@ -24,7 +24,12 @@ public class BraveLeoUtils {
activePurchaseModel -> {
BraveLeoPrefUtils.setIsSubscriptionActive(activePurchaseModel != null);
if (activePurchaseModel != null) {
BraveLeoPrefUtils.setChatPurchaseToken(
activePurchaseModel.getPurchaseToken());
BraveLeoPrefUtils.setChatPackageName();
BraveLeoPrefUtils.setChatProductId(activePurchaseModel.getProductId());
} else {
// TODO(sergz): showRestoreMenu?
}
if (callback != null) {
callback.onResult(null);