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