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 boolean IS_DETACHED;
24     public String SHELL_APP_SCHEME;
25     public String RELEASE_CHANNEL;
26     public boolean SHOW_LOADING_VIEW_IN_SHELL_APP;
27     public boolean ARE_REMOTE_UPDATES_ENABLED;
28     public List<Constants.EmbeddedResponse> EMBEDDED_RESPONSES;
29     public int ANDROID_VERSION_CODE;
30     public boolean FCM_ENABLED;
31     // no longer used, but we need to leave this here so that people's old detached apps don't break
32     public boolean ANALYTICS_ENABLED;
33   }
34 
35   private static final String TAG = Constants.class.getSimpleName();
36 
37   public static String VERSION_NAME = null;
38   public static String INITIAL_URL = null;
39   public static boolean IS_DETACHED = false;
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_31
72     abiVersions.add("31.0.0");
73     // END_SDK_31
74     // BEGIN_SDK_30
75     abiVersions.add("30.0.0");
76     // END_SDK_30
77     // BEGIN_SDK_29
78     abiVersions.add("29.0.0");
79     // END_SDK_29
80     // BEGIN_SDK_28
81     abiVersions.add("28.0.0");
82     // END_SDK_28
83     // BEGIN_SDK_27
84     abiVersions.add("27.0.0");
85     // END_SDK_27
86     // BEGIN_SDK_26
87     abiVersions.add("26.0.0");
88     // END_SDK_26
89     // BEGIN_SDK_25
90     abiVersions.add("25.0.0");
91     // END_SDK_25
92     // WHEN_PREPARING_SHELL_REMOVE_TO_HERE
93     // WHEN_DISTRIBUTING_REMOVE_TO_HERE
94 
95     if (TEMPORARY_ABI_VERSION != null) {
96       abiVersions.add(TEMPORARY_ABI_VERSION);
97     }
98 
99     setSdkVersions(new ArrayList<>(abiVersions));
100 
101     List<EmbeddedResponse> embeddedResponses = new ArrayList<>();
102     embeddedResponses.add(new EmbeddedResponse("https://exp.host/@exponent/home/bundle", EMBEDDED_KERNEL_PATH, "application/javascript"));
103 
104     // ADD EMBEDDED RESPONSES HERE
105     // START EMBEDDED RESPONSES
106     // END EMBEDDED RESPONSES
107 
108     try {
109       Class appConstantsClass = Class.forName("host.exp.exponent.generated.AppConstants");
110       ExpoViewAppConstants appConstants = (ExpoViewAppConstants) appConstantsClass.getMethod("get").invoke(null);
111       VERSION_NAME = appConstants.VERSION_NAME;
112       INITIAL_URL = appConstants.INITIAL_URL;
113       IS_DETACHED = appConstants.IS_DETACHED;
114       SHELL_APP_SCHEME = appConstants.SHELL_APP_SCHEME;
115       RELEASE_CHANNEL = appConstants.RELEASE_CHANNEL;
116       SHOW_LOADING_VIEW_IN_SHELL_APP = appConstants.SHOW_LOADING_VIEW_IN_SHELL_APP;
117       ARE_REMOTE_UPDATES_ENABLED = appConstants.ARE_REMOTE_UPDATES_ENABLED;
118       ANDROID_VERSION_CODE = appConstants.ANDROID_VERSION_CODE;
119       FCM_ENABLED = appConstants.FCM_ENABLED;
120       ANALYTICS_ENABLED = !isShellApp();
121 
122       embeddedResponses.addAll(appConstants.EMBEDDED_RESPONSES);
123       EMBEDDED_RESPONSES = embeddedResponses;
124     } catch (ClassNotFoundException e) {
125       e.printStackTrace();
126     } catch (IllegalAccessException e) {
127       e.printStackTrace();
128     } catch (NoSuchMethodException e) {
129       e.printStackTrace();
130     } catch (InvocationTargetException e) {
131       e.printStackTrace();
132     }
133   }
134 
135   public static final boolean DEBUG_COLD_START_METHOD_TRACING = false;
136   public static final boolean DEBUG_MANIFEST_METHOD_TRACING = false;
137   public static final boolean DEBUG_METHOD_TRACING = DEBUG_COLD_START_METHOD_TRACING || DEBUG_MANIFEST_METHOD_TRACING;
138   public static final boolean ENABLE_LEAK_CANARY = false;
139   public static final boolean WRITE_BUNDLE_TO_LOG = false;
140   public static final boolean WAIT_FOR_DEBUGGER = false;
141 
142   public static boolean isShellApp() {
143     return INITIAL_URL != null;
144   }
145 
146   public static class EmbeddedResponse {
147     public final String url;
148     public final String responseFilePath;
149     public final String mediaType;
150 
151     public EmbeddedResponse(final String url, final String responseFilePath, final String mediaType) {
152       this.url = url;
153       this.responseFilePath = responseFilePath;
154       this.mediaType = mediaType;
155     }
156   }
157 
158   public static String getVersionName(Context context) {
159     if (VERSION_NAME != null) {
160       // This will be set in shell apps
161       return VERSION_NAME;
162     } else {
163       try {
164         PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
165         return pInfo.versionName;
166       } catch (PackageManager.NameNotFoundException e) {
167         EXL.e(TAG, e.toString());
168         return "";
169       }
170     }
171   }
172 
173   public static boolean isDetached() {
174     return IS_DETACHED;
175   }
176 
177   private static boolean sIsTest = false;
178 
179   public static void setInTest() {
180     sIsTest = true;
181   }
182 
183   public static boolean isTest() {
184     return sIsTest;
185   }
186 }
187