Skip to content

Commit 42e4353

Browse files
committed
ensure getAutoInstance apply on init and restore NativeShareLinkManager
1 parent 49f3a49 commit 42e4353

File tree

5 files changed

+294
-36
lines changed

5 files changed

+294
-36
lines changed

Branch-SDK-TestBed/src/main/java/io/branch/branchandroidtestbed/MainActivity.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,19 @@ public void onClick(DialogInterface dialog, int whichButton) {
148148
@Override
149149
public void onClick(View v) {
150150
String currentUserId = PrefHelper.getInstance(MainActivity.this).getIdentity();
151-
Branch.init().logout();
152-
Toast.makeText(getApplicationContext(), "Cleared User ID: " + currentUserId, Toast.LENGTH_LONG).show();
151+
Branch.init().logout(new Branch.LogoutStatusListener() {
152+
@Override
153+
public void onLogoutFinished(boolean loggedOut, BranchError error) {
154+
if (error != null) {
155+
Log.e("BranchSDK_Tester", "onLogoutFinished Error: " + error);
156+
Toast.makeText(getApplicationContext(), "Error Logging Out: " + error.getMessage(), Toast.LENGTH_SHORT).show();
157+
} else {
158+
Log.d("BranchSDK_Tester", "onLogoutFinished succeeded: " + loggedOut);
159+
Toast.makeText(getApplicationContext(), "Cleared User ID: " + currentUserId, Toast.LENGTH_SHORT).show();
160+
}
161+
}
162+
});
163+
153164
}
154165
});
155166

Branch-SDK/src/main/java/io/branch/receivers/SharingBroadcastReceiver.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,11 @@ class SharingBroadcastReceiver: BroadcastReceiver() {
1515

1616
BranchLogger.v("Intent: $intent")
1717
BranchLogger.v("Clicked component: $clickedComponent")
18+
19+
NativeShareLinkManager.getInstance().linkShareListenerCallback?.onChannelSelected(
20+
clickedComponent.toString()
21+
)
22+
23+
NativeShareLinkManager.getInstance().linkShareListenerCallback?.onLinkShareResponse(SharingUtil.sharedURL, null);
1824
}
1925
}

Branch-SDK/src/main/java/io/branch/referral/Branch.java

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import io.branch.referral.network.BranchRemoteInterfaceUrlConnection;
5252
import io.branch.referral.util.DependencyUtilsKt;
5353
import io.branch.referral.util.LinkProperties;
54+
import io.branch.referral.BranchConfigurationManager;
5455

5556
/**
5657
* <p>
@@ -369,6 +370,8 @@ synchronized private static Branch initBranchSDK(@NonNull Context context, Strin
369370
branchReferral_.prefHelper_.setBranchKey(branchKey);
370371
}
371372

373+
BranchConfigurationManager.loadConfiguration(context, branchReferral_);
374+
372375
/* If {@link Application} is instantiated register for activity life cycle events. */
373376
if (context instanceof Application) {
374377
branchReferral_.setActivityLifeCycleObserver((Application) context);
@@ -1120,9 +1123,6 @@ public void share(@NonNull Activity activity, @NonNull BranchUniversalObject buo
11201123
* @param builder A {@link BranchShareSheetBuilder} instance to build share link.
11211124
*/
11221125

1123-
1124-
1125-
11261126
// PRIVATE FUNCTIONS
11271127

11281128
private String generateShortLinkSync(ServerRequestCreateUrl req) {
@@ -1479,32 +1479,36 @@ public interface BranchLastAttributedTouchDataListener {
14791479
* @see String
14801480
* @see BranchError
14811481
*/
1482-
public interface BranchNativeLinkShareListener {
1482+
public interface BranchLinkShareListener {
14831483
/**
1484-
* Called when a link is shared successfully.
1485-
*
1486-
* @param sharedLink The shared link URL
1487-
* @param sharedBy The channel through which the link was shared
1488-
* @param error null if successful, otherwise contains error information
1484+
* <p> Callback method to update when share link dialog is launched.</p>
14891485
*/
1490-
void onLinkShareResponse(String sharedLink, String sharedBy, BranchError error);
1491-
1486+
void onShareLinkDialogLaunched();
1487+
14921488
/**
1493-
* Called when a channel is selected for sharing.
1494-
*
1495-
* @param selectedChannel The name of the selected channel
1489+
* <p> Callback method to update when sharing dialog is dismissed.</p>
14961490
*/
1497-
void onChannelSelected(String selectedChannel);
1498-
1491+
void onShareLinkDialogDismissed();
1492+
14991493
/**
1500-
* Called when the share link dialog is launched.
1494+
* <p> Callback method to update the sharing status. Called on sharing completed or on error.</p>
1495+
*
1496+
* @param sharedLink The link shared to the channel.
1497+
* @param sharedChannel Channel selected for sharing.
1498+
* @param error A {@link BranchError} to update errors, if there is any.
15011499
*/
1502-
void onShareLinkDialogLaunched();
1503-
1500+
void onLinkShareResponse(String sharedLink, String sharedChannel, BranchError error);
1501+
15041502
/**
1505-
* Called when the share link dialog is dismissed.
1503+
* <p>Called when user select a channel for sharing a deep link.
1504+
* Branch will create a deep link for the selected channel and share with it after calling this
1505+
* method. On sharing complete, status is updated by onLinkShareResponse() callback. Consider
1506+
* having a sharing in progress UI if you wish to prevent user activity in the window between selecting a channel
1507+
* and sharing complete.</p>
1508+
*
1509+
* @param channelName Name of the selected application to share the link. An empty string is returned if unable to resolve selected client name.
15061510
*/
1507-
void onShareLinkDialogDismissed();
1511+
void onChannelSelected(String channelName);
15081512
}
15091513

15101514
/**
@@ -1545,6 +1549,16 @@ public interface BranchNativeLinkShareListener {
15451549
* Callback interface for listening logout status
15461550
* </p>
15471551
*/
1552+
public interface LogoutStatusListener {
1553+
/**
1554+
* Called on finishing the the logout process
1555+
*
1556+
* @param loggedOut A {@link Boolean} which is set to true if logout succeeded
1557+
* @param error An instance of {@link BranchError} to notify any error occurred during logout.
1558+
* A null value is set if logout succeeded.
1559+
*/
1560+
void onLogoutFinished(boolean loggedOut, BranchError error);
1561+
}
15481562

15491563

15501564
/**
@@ -2433,13 +2447,24 @@ public void getLastAttributedTouchData(BranchLastAttributedTouchDataListener cal
24332447
}
24342448

24352449
/**
2436-
* Gets the link share listener callback.
2437-
*
2438-
* @return {@link Branch.BranchNativeLinkShareListener} the current link share listener callback, or null if not set.
2450+
* <p>An Interface class that is implemented by all classes that make use of
2451+
* {@link BranchNativeLinkShareListener}, defining methods to listen for link sharing status.</p>
24392452
*/
2440-
public Branch.BranchNativeLinkShareListener getLinkShareListenerCallback() {
2441-
// This method was removed during modernization but is kept for backward compatibility
2442-
// The actual link sharing functionality has been moved to NativeShareLinkManager
2443-
return null;
2453+
public interface BranchNativeLinkShareListener {
2454+
2455+
/**
2456+
* <p> Callback method to report error/response.</p>
2457+
*
2458+
* @param sharedLink The link shared to the channel.
2459+
* @param error A {@link BranchError} to update errors, if there is any.
2460+
*/
2461+
void onLinkShareResponse(String sharedLink, BranchError error);
2462+
2463+
/**
2464+
* <p>Called when user select a channel for sharing a deep link.
2465+
*
2466+
* @param channelName Name of the selected application to share the link. An empty string is returned if unable to resolve selected client name.
2467+
*/
2468+
void onChannelSelected(String channelName);
24442469
}
24452470
}
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
package io.branch.referral;
2+
3+
import android.content.Context;
4+
import android.text.TextUtils;
5+
import androidx.annotation.NonNull;
6+
7+
/**
8+
* Modular configuration manager for Branch SDK initialization.
9+
*
10+
* This class handles all configuration loading that was previously done in the
11+
* getAutoInstance method, ensuring proper separation of concerns and modularity.
12+
*
13+
* Follows Single Responsibility Principle by focusing solely on configuration management.
14+
* Follows Dependency Inversion Principle by depending on abstractions (BranchJsonConfig).
15+
*/
16+
public class BranchConfigurationManager {
17+
18+
private static final String TAG = "BranchConfigurationManager";
19+
20+
/**
21+
* Loads all configuration settings from various sources.
22+
*
23+
* This method centralizes all configuration loading logic that was previously
24+
* scattered across the getAutoInstance method, ensuring proper initialization
25+
* order and error handling.
26+
*
27+
* @param context Android context for resource access
28+
* @param branchInstance The Branch instance to configure
29+
*/
30+
public static void loadConfiguration(@NonNull Context context, @NonNull Branch branchInstance) {
31+
try {
32+
// Load logging configuration
33+
loadLoggingConfiguration(context);
34+
35+
// Load plugin runtime configuration
36+
loadPluginRuntimeConfiguration(context);
37+
38+
// Load API configuration
39+
loadApiConfiguration(context);
40+
41+
// Load Facebook configuration
42+
loadFacebookConfiguration(context);
43+
44+
// Load consumer protection configuration
45+
loadConsumerProtectionConfiguration(context, branchInstance);
46+
47+
// Load test mode configuration
48+
loadTestModeConfiguration(context);
49+
50+
// Load preinstall system data
51+
loadPreinstallSystemData(branchInstance, context);
52+
53+
BranchLogger.v("Branch configuration loaded successfully");
54+
55+
} catch (Exception e) {
56+
BranchLogger.e("Failed to load Branch configuration: " + e.getMessage());
57+
// Continue with default configuration to avoid breaking initialization
58+
}
59+
}
60+
61+
/**
62+
* Loads logging configuration from branch.json.
63+
*
64+
* @param context Android context for resource access
65+
*/
66+
private static void loadLoggingConfiguration(@NonNull Context context) {
67+
if (BranchUtil.getEnableLoggingConfig(context)) {
68+
Branch.enableLogging();
69+
BranchLogger.v("Logging enabled from configuration");
70+
}
71+
}
72+
73+
/**
74+
* Loads plugin runtime configuration from branch.json.
75+
*
76+
* @param context Android context for resource access
77+
*/
78+
private static void loadPluginRuntimeConfiguration(@NonNull Context context) {
79+
boolean deferInitForPluginRuntime = BranchUtil.getDeferInitForPluginRuntimeConfig(context);
80+
Branch.deferInitForPluginRuntime(deferInitForPluginRuntime);
81+
82+
if (deferInitForPluginRuntime) {
83+
BranchLogger.v("Plugin runtime initialization deferred from configuration");
84+
}
85+
}
86+
87+
/**
88+
* Loads API configuration from branch.json.
89+
*
90+
* @param context Android context for resource access
91+
*/
92+
private static void loadApiConfiguration(@NonNull Context context) {
93+
BranchUtil.setAPIBaseUrlFromConfig(context);
94+
BranchLogger.v("API configuration loaded from branch.json");
95+
}
96+
97+
/**
98+
* Loads Facebook configuration from branch.json.
99+
*
100+
* @param context Android context for resource access
101+
*/
102+
private static void loadFacebookConfiguration(@NonNull Context context) {
103+
BranchUtil.setFbAppIdFromConfig(context);
104+
BranchLogger.v("Facebook configuration loaded from branch.json");
105+
}
106+
107+
/**
108+
* Loads consumer protection configuration from branch.json.
109+
*
110+
* @param context Android context for resource access
111+
* @param branchInstance The Branch instance to configure
112+
*/
113+
private static void loadConsumerProtectionConfiguration(@NonNull Context context, @NonNull Branch branchInstance) {
114+
BranchUtil.setCPPLevelFromConfig(context);
115+
BranchLogger.v("Consumer protection configuration loaded from branch.json");
116+
}
117+
118+
/**
119+
* Loads test mode configuration from branch.json and manifest.
120+
*
121+
* @param context Android context for resource access
122+
*/
123+
private static void loadTestModeConfiguration(@NonNull Context context) {
124+
BranchUtil.setTestMode(BranchUtil.checkTestMode(context));
125+
BranchLogger.v("Test mode configuration loaded from configuration");
126+
}
127+
128+
/**
129+
* Loads preinstall system data if available.
130+
*
131+
* @param branchInstance The Branch instance to configure
132+
* @param context Android context for resource access
133+
*/
134+
private static void loadPreinstallSystemData(@NonNull Branch branchInstance, @NonNull Context context) {
135+
BranchPreinstall.getPreinstallSystemData(branchInstance, context);
136+
BranchLogger.v("Preinstall system data loaded");
137+
}
138+
139+
/**
140+
* Validates that essential configuration is present.
141+
*
142+
* @param context Android context for resource access
143+
* @return true if essential configuration is valid, false otherwise
144+
*/
145+
public static boolean validateConfiguration(@NonNull Context context) {
146+
String branchKey = BranchUtil.readBranchKey(context);
147+
if (TextUtils.isEmpty(branchKey)) {
148+
BranchLogger.w("Branch key is missing from configuration");
149+
return false;
150+
}
151+
152+
BranchLogger.v("Configuration validation passed");
153+
return true;
154+
}
155+
156+
/**
157+
* Resets all configuration to default values.
158+
*
159+
* This method is useful for testing or when configuration needs to be cleared.
160+
*/
161+
public static void resetConfiguration() {
162+
Branch.disableTestMode();
163+
Branch.disableLogging();
164+
Branch.deferInitForPluginRuntime(false);
165+
BranchLogger.v("Configuration reset to default values");
166+
}
167+
}

0 commit comments

Comments
 (0)