1 // Copyright 2015-present 650 Industries. All rights reserved.
2 
3 package host.exp.exponent;
4 
5 import android.content.Context;
6 import android.content.pm.PackageInfo;
7 import android.content.pm.PackageManager;
8 import android.text.TextUtils;
9 
10 import java.lang.reflect.InvocationTargetException;
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Set;
15 
16 import host.exp.exponent.analytics.EXL;
17 
18 public class Constants {
19 
20   public static class ExpoViewAppConstants {
21     public String VERSION_NAME;
22     public String INITIAL_URL;
23     public String SHELL_APP_SCHEME;
24     public String RELEASE_CHANNEL;
25     public boolean SHOW_LOADING_VIEW_IN_SHELL_APP;
26     public boolean ARE_REMOTE_UPDATES_ENABLED;
27     public List<Constants.EmbeddedResponse> EMBEDDED_RESPONSES;
28     public int ANDROID_VERSION_CODE;
29     public boolean FCM_ENABLED;
30     // no longer used, but we need to leave this here so that people's old detached apps don't break
31     public boolean ANALYTICS_ENABLED;
32     // same but since SDK32
33     public boolean IS_DETACHED;
34   }
35 
36   private static final String TAG = Constants.class.getSimpleName();
37 
38   public static String VERSION_NAME = null;
39   public static String INITIAL_URL = null;
40   public static String SHELL_APP_SCHEME = null;
41   public static final String SHELL_APP_EMBEDDED_MANIFEST_PATH = "shell-app-manifest.json";
42   public static final String API_HOST = "https://exp.host";
43   public static String ABI_VERSIONS;
44   public static String SDK_VERSIONS;
45   public static List<String> SDK_VERSIONS_LIST;
46   public static final String TEMPORARY_ABI_VERSION = null;
47   public static final String EMBEDDED_KERNEL_PATH = "assets://kernel.android.bundle";
48   public static List<EmbeddedResponse> EMBEDDED_RESPONSES;
49   public static boolean DISABLE_NUX = false;
50   public static String RELEASE_CHANNEL = "default";
51   public static boolean SHOW_LOADING_VIEW_IN_SHELL_APP = false;
52   public static boolean ARE_REMOTE_UPDATES_ENABLED = true;
53   public static int ANDROID_VERSION_CODE;
54   public static boolean FCM_ENABLED;
55   public static boolean ANALYTICS_ENABLED;
56 
57   public static void setSdkVersions(List<String> sdkVersions) {
58     ABI_VERSIONS = TextUtils.join(",", sdkVersions);
59 
60     // NOTE: Currently public-facing SDK versions and internal ABI versions are the same, but
61     // eventually we may decouple them
62     SDK_VERSIONS = ABI_VERSIONS;
63     SDK_VERSIONS_LIST = sdkVersions;
64   }
65 
66   static {
67     Set<String> abiVersions = new HashSet<>();
68     // WHEN_DISTRIBUTING_REMOVE_FROM_HERE
69     // WHEN_PREPARING_SHELL_REMOVE_FROM_HERE
70     // ADD ABI VERSIONS HERE DO NOT MODIFY
71     // BEGIN_SDK_34
72     abiVersions.add("34.0.0");
73     // END_SDK_34
74     // BEGIN_SDK_33
75     abiVersions.add("33.0.0");
76     // END_SDK_33
77     // BEGIN_SDK_32
78     abiVersions.add("32.0.0");
79     // END_SDK_32
80     // BEGIN_SDK_31
81     abiVersions.add("31.0.0");
82     // END_SDK_31
83     // WHEN_PREPARING_SHELL_REMOVE_TO_HERE
84     // WHEN_DISTRIBUTING_REMOVE_TO_HERE
85 
86     if (TEMPORARY_ABI_VERSION != null) {
87       abiVersions.add(TEMPORARY_ABI_VERSION);
88     }
89 
90     setSdkVersions(new ArrayList<>(abiVersions));
91 
92     List<EmbeddedResponse> embeddedResponses = new ArrayList<>();
93     // WHEN_PREPARING_SHELL_REMOVE_FROM_HERE
94     embeddedResponses.add(new EmbeddedResponse("https://exp.host/@exponent/home/bundle", EMBEDDED_KERNEL_PATH, "application/javascript"));
95     // WHEN_PREPARING_SHELL_REMOVE_TO_HERE
96 
97     // ADD EMBEDDED RESPONSES HERE
98     // START EMBEDDED RESPONSES
99     // END EMBEDDED RESPONSES
100 
101     try {
102       Class appConstantsClass = Class.forName("host.exp.exponent.generated.AppConstants");
103       ExpoViewAppConstants appConstants = (ExpoViewAppConstants) appConstantsClass.getMethod("get").invoke(null);
104       VERSION_NAME = appConstants.VERSION_NAME;
105       INITIAL_URL = appConstants.INITIAL_URL;
106       SHELL_APP_SCHEME = appConstants.SHELL_APP_SCHEME;
107       RELEASE_CHANNEL = appConstants.RELEASE_CHANNEL;
108       SHOW_LOADING_VIEW_IN_SHELL_APP = appConstants.SHOW_LOADING_VIEW_IN_SHELL_APP;
109       ARE_REMOTE_UPDATES_ENABLED = appConstants.ARE_REMOTE_UPDATES_ENABLED;
110       ANDROID_VERSION_CODE = appConstants.ANDROID_VERSION_CODE;
111       FCM_ENABLED = appConstants.FCM_ENABLED;
112       ANALYTICS_ENABLED = !isStandaloneApp();
113 
114       embeddedResponses.addAll(appConstants.EMBEDDED_RESPONSES);
115       EMBEDDED_RESPONSES = embeddedResponses;
116     } catch (ClassNotFoundException e) {
117       e.printStackTrace();
118     } catch (IllegalAccessException e) {
119       e.printStackTrace();
120     } catch (NoSuchMethodException e) {
121       e.printStackTrace();
122     } catch (InvocationTargetException e) {
123       e.printStackTrace();
124     }
125   }
126 
127   public static final boolean DEBUG_COLD_START_METHOD_TRACING = false;
128   public static final boolean DEBUG_MANIFEST_METHOD_TRACING = false;
129   public static final boolean DEBUG_METHOD_TRACING = DEBUG_COLD_START_METHOD_TRACING || DEBUG_MANIFEST_METHOD_TRACING;
130   public static final boolean ENABLE_LEAK_CANARY = false;
131   public static final boolean WRITE_BUNDLE_TO_LOG = false;
132   public static final boolean WAIT_FOR_DEBUGGER = false;
133 
134   public static boolean isStandaloneApp() {
135     return INITIAL_URL != null;
136   }
137 
138   public static class EmbeddedResponse {
139     public final String url;
140     public final String responseFilePath;
141     public final String mediaType;
142 
143     public EmbeddedResponse(final String url, final String responseFilePath, final String mediaType) {
144       this.url = url;
145       this.responseFilePath = responseFilePath;
146       this.mediaType = mediaType;
147     }
148   }
149 
150   public static String getVersionName(Context context) {
151     if (VERSION_NAME != null) {
152       // This will be set in shell apps
153       return VERSION_NAME;
154     } else {
155       try {
156         PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
157         return pInfo.versionName;
158       } catch (PackageManager.NameNotFoundException e) {
159         EXL.e(TAG, e.toString());
160         return "";
161       }
162     }
163   }
164 
165   private static boolean sIsTest = false;
166 
167   public static void setInTest() {
168     sIsTest = true;
169   }
170 
171   public static boolean isTest() {
172     return sIsTest;
173   }
174 }
175