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.List;
13 
14 import host.exp.exponent.analytics.EXL;
15 
16 public class Constants {
17 
18   public static class ExpoViewAppConstants {
19     public String VERSION_NAME;
20     public String INITIAL_URL;
21     public boolean IS_DETACHED;
22     public String SHELL_APP_SCHEME;
23     public String RELEASE_CHANNEL;
24     public boolean SHOW_LOADING_VIEW_IN_SHELL_APP;
25     public boolean ARE_REMOTE_UPDATES_ENABLED;
26     public List<Constants.EmbeddedResponse> EMBEDDED_RESPONSES;
27     public int ANDROID_VERSION_CODE;
28     public boolean FCM_ENABLED;
29   }
30 
31   private static final String TAG = Constants.class.getSimpleName();
32 
33   public static String VERSION_NAME = null;
34   public static String INITIAL_URL = null;
35   public static boolean IS_DETACHED = false;
36   public static String SHELL_APP_SCHEME = null;
37   public static final String SHELL_APP_EMBEDDED_MANIFEST_PATH = "shell-app-manifest.json";
38   public static final String API_HOST = "https://exp.host";
39   public static String ABI_VERSIONS;
40   public static String SDK_VERSIONS;
41   public static List<String> SDK_VERSIONS_LIST;
42   public static final String TEMPORARY_ABI_VERSION = null;
43   public static final String EMBEDDED_KERNEL_PATH = "assets://kernel.android.bundle";
44   public static List<EmbeddedResponse> EMBEDDED_RESPONSES;
45   public static boolean DISABLE_NUX = false;
46   public static String RELEASE_CHANNEL = "default";
47   public static boolean SHOW_LOADING_VIEW_IN_SHELL_APP = false;
48   public static boolean ARE_REMOTE_UPDATES_ENABLED = true;
49   public static int ANDROID_VERSION_CODE;
50   public static boolean FCM_ENABLED;
51 
52   public static void setSdkVersions(List<String> sdkVersions) {
53     ABI_VERSIONS = TextUtils.join(",", sdkVersions);
54 
55     // NOTE: Currently public-facing SDK versions and internal ABI versions are the same, but
56     // eventually we may decouple them
57     SDK_VERSIONS = ABI_VERSIONS;
58     SDK_VERSIONS_LIST = sdkVersions;
59   }
60 
61   static {
62     List<String> abiVersions = new ArrayList<>();
63     // THIS COMMENT IS USED BY android-build-aar.sh DO NOT MODIFY
64     abiVersions.add("28.0.0");
65     abiVersions.add("27.0.0");
66     abiVersions.add("26.0.0");
67     abiVersions.add("25.0.0");
68     abiVersions.add("24.0.0");
69     abiVersions.add("23.0.0");
70     abiVersions.add("22.0.0");
71 
72     if (TEMPORARY_ABI_VERSION != null) {
73       abiVersions.add(TEMPORARY_ABI_VERSION);
74     }
75 
76     setSdkVersions(abiVersions);
77 
78     List<EmbeddedResponse> embeddedResponses = new ArrayList<>();
79     embeddedResponses.add(new EmbeddedResponse("https://exp.host/@exponent/home/bundle", EMBEDDED_KERNEL_PATH, "application/javascript"));
80 
81     // ADD EMBEDDED RESPONSES HERE
82     // START EMBEDDED RESPONSES
83     // END EMBEDDED RESPONSES
84 
85     try {
86       Class appConstantsClass = Class.forName("host.exp.exponent.generated.AppConstants");
87       ExpoViewAppConstants appConstants = (ExpoViewAppConstants) appConstantsClass.getMethod("get").invoke(null);
88       VERSION_NAME = appConstants.VERSION_NAME;
89       INITIAL_URL = appConstants.INITIAL_URL;
90       IS_DETACHED = appConstants.IS_DETACHED;
91       SHELL_APP_SCHEME = appConstants.SHELL_APP_SCHEME;
92       RELEASE_CHANNEL = appConstants.RELEASE_CHANNEL;
93       SHOW_LOADING_VIEW_IN_SHELL_APP = appConstants.SHOW_LOADING_VIEW_IN_SHELL_APP;
94       ARE_REMOTE_UPDATES_ENABLED = appConstants.ARE_REMOTE_UPDATES_ENABLED;
95       ANDROID_VERSION_CODE = appConstants.ANDROID_VERSION_CODE;
96       FCM_ENABLED = appConstants.FCM_ENABLED;
97 
98       embeddedResponses.addAll(appConstants.EMBEDDED_RESPONSES);
99       EMBEDDED_RESPONSES = embeddedResponses;
100     } catch (ClassNotFoundException e) {
101       e.printStackTrace();
102     } catch (IllegalAccessException e) {
103       e.printStackTrace();
104     } catch (NoSuchMethodException e) {
105       e.printStackTrace();
106     } catch (InvocationTargetException e) {
107       e.printStackTrace();
108     }
109   }
110 
111   public static final boolean DEBUG_COLD_START_METHOD_TRACING = false;
112   public static final boolean DEBUG_MANIFEST_METHOD_TRACING = false;
113   public static final boolean DEBUG_METHOD_TRACING = DEBUG_COLD_START_METHOD_TRACING || DEBUG_MANIFEST_METHOD_TRACING;
114   public static final boolean ENABLE_LEAK_CANARY = false;
115   public static final boolean WRITE_BUNDLE_TO_LOG = false;
116   public static final boolean WAIT_FOR_DEBUGGER = false;
117 
118   public static boolean isShellApp() {
119     return INITIAL_URL != null;
120   }
121 
122   public static class EmbeddedResponse {
123     public final String url;
124     public final String responseFilePath;
125     public final String mediaType;
126 
127     public EmbeddedResponse(final String url, final String responseFilePath, final String mediaType) {
128       this.url = url;
129       this.responseFilePath = responseFilePath;
130       this.mediaType = mediaType;
131     }
132   }
133 
134   public static String getVersionName(Context context) {
135     if (VERSION_NAME != null) {
136       // This will be set in shell apps
137       return VERSION_NAME;
138     } else {
139       try {
140         PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
141         return pInfo.versionName;
142       } catch (PackageManager.NameNotFoundException e) {
143         EXL.e(TAG, e.toString());
144         return "";
145       }
146     }
147   }
148 
149   public static boolean isDetached() {
150     return IS_DETACHED;
151   }
152 }
153