Created
March 29, 2017 15:19
-
-
Save dbof10/1d56ca54d4ce4e4d0bbdccca9c93d920 to your computer and use it in GitHub Desktop.
Update React bundle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ReactUpdater extends IntentService { | |
private static final String BUNDLE_API_KEY = "serve"; | |
private static final String INITIAL_BUNDLE_PATH = "assets://index.android.bundle"; | |
private static final String LAST_UPDATED_KEY = "last_updated"; | |
private static final String MAPPINGS_KEY = "name"; | |
private static final String SETUP_KEY = "setup_3.13.0"; | |
private static final String SHARED_PREFERENCE_NAME = "com.myntra.update.react"; | |
private static final String TAG; | |
private static final String VERSION_KEY = "version"; | |
public static final String relativeVersionLocation = "/version"; | |
static { | |
TAG = MYNReactUpdater.class.getSimpleName(); | |
} | |
public MYNReactUpdater(String name) { | |
super(name); | |
} | |
public MYNReactUpdater() { | |
super("MYNReactUpdater"); | |
} | |
private void initialize() throws IOException { | |
if (StringUtils.isEmpty(SharedPreferenceHelper.getPref(SHARED_PREFERENCE_NAME, SETUP_KEY, StringUtils.EMPTY))) { | |
try { | |
String mappingsText = readRawTextFile(2131230721); | |
if (!StringUtils.isEmpty(mappingsText)) { | |
JSONObject mappingsObject = new JSONObject(mappingsText); | |
String packageName = mappingsObject.getString(MAPPINGS_KEY); | |
String version = mappingsObject.getString(VERSION_KEY); | |
String bundleApi = mappingsObject.getString(BUNDLE_API_KEY); | |
SharedPreferenceHelper.putPref(SHARED_PREFERENCE_NAME, MAPPINGS_KEY, packageName); | |
SharedPreferenceHelper.putPref(SHARED_PREFERENCE_NAME, VERSION_KEY, version); | |
SharedPreferenceHelper.putPref(SHARED_PREFERENCE_NAME, BUNDLE_API_KEY, bundleApi); | |
SharedPreferenceHelper.putPref(SHARED_PREFERENCE_NAME, SETUP_KEY, Boolean.TRUE.toString()); | |
updateFromVersion(version); | |
return; | |
} | |
return; | |
} catch (Throwable e) { | |
C1732L.m419e(e); | |
return; | |
} | |
} | |
updateFromVersion(SharedPreferenceHelper.getPref(SHARED_PREFERENCE_NAME, VERSION_KEY, StringUtils.EMPTY)); | |
} | |
private void updateFromVersion(String presentVersion) throws IOException { | |
if (!StringUtils.isEmpty(presentVersion)) { | |
int reactUpdaterCacheInMinutes = Configurator.getSharedInstance().reactUpdaterCacheInMinutes; | |
if (reactUpdaterCacheInMinutes != -1) { | |
if (System.currentTimeMillis() - SharedPreferenceHelper.getPref(SHARED_PREFERENCE_NAME, LAST_UPDATED_KEY, 0) >= TimeUnit.MINUTES.toMillis((long) reactUpdaterCacheInMinutes)) { | |
String packageName = SharedPreferenceHelper.getPref(SHARED_PREFERENCE_NAME, MAPPINGS_KEY, StringUtils.EMPTY); | |
if (!StringUtils.isEmpty(packageName)) { | |
String apiKey = SharedPreferenceHelper.getPref(SHARED_PREFERENCE_NAME, BUNDLE_API_KEY, StringUtils.EMPTY); | |
if (!StringUtils.isEmpty(apiKey)) { | |
UpdateMap updateMap = ReactUpdaterService.getCurrentVersion(apiKey, packageName); | |
if (updateMap != null && !StringUtils.isEmpty(updateMap.version)) { | |
if (presentVersion.equalsIgnoreCase(updateMap.version)) { | |
setUpdateCompleteTime(); | |
} else if (StringUtils.isNotEmpty(updateMap.minAppVersion) && shouldUpdate("3.13.0", updateMap.minAppVersion)) { | |
setUpdateCompleteTime(); | |
} else if (fileExists(packageName, updateMap.version)) { | |
SharedPreferenceHelper.putPref(SHARED_PREFERENCE_NAME, VERSION_KEY, updateMap.version); | |
SharedPreferenceHelper.putPref(SHARED_PREFERENCE_NAME, "updated", Boolean.TRUE.toString()); | |
setUpdateCompleteTime(); | |
} else { | |
String bundle = ReactUpdaterService.getBundle(apiKey, packageName, updateMap.version); | |
if (!StringUtils.isNotEmpty(bundle)) { | |
return; | |
} | |
if (StringUtils.isNotEmpty(updateMap.hash) && !updateMap.hash.equals(md5(bundle))) { | |
return; | |
} | |
if (saveFileToDisk(bundle, packageName + '-' + updateMap.version)) { | |
SharedPreferenceHelper.putPref(SHARED_PREFERENCE_NAME, VERSION_KEY, updateMap.version); | |
SharedPreferenceHelper.putPref(SHARED_PREFERENCE_NAME, "updated", Boolean.TRUE.toString()); | |
setUpdateCompleteTime(); | |
return; | |
} | |
SharedPreferenceHelper.putPref(SHARED_PREFERENCE_NAME, "updated", Boolean.FALSE.toString()); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
private static final String md5(String s) { | |
String MD5 = "MD5"; | |
try { | |
MessageDigest digest = MessageDigest.getInstance("MD5"); | |
digest.update(s.getBytes()); | |
byte[] messageDigest = digest.digest(); | |
StringBuilder hexString = new StringBuilder(); | |
for (byte aMessageDigest : messageDigest) { | |
String h = Integer.toHexString(aMessageDigest & JfifUtil.MARKER_FIRST_BYTE); | |
while (h.length() < 2) { | |
h = InstallationHelper.EMPTY + h; | |
} | |
hexString.append(h); | |
} | |
return hexString.toString(); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
return StringUtils.EMPTY; | |
} | |
} | |
public static boolean shouldUpdate(String currentVersion, String latestVersion) { | |
String[] tokenizedCurrentVersion = currentVersion.replaceAll("[^0-9]+", StringUtils.SPACE).replaceAll(" +", StringUtils.SPACE).split(StringUtils.SPACE); | |
String[] tokenizedLatestVersion = latestVersion.replaceAll("[^0-9]+", StringUtils.SPACE).replaceAll(" +", StringUtils.SPACE).split(StringUtils.SPACE); | |
int leastSize = tokenizedCurrentVersion.length > tokenizedLatestVersion.length ? tokenizedLatestVersion.length : tokenizedCurrentVersion.length; | |
for (int i = 0; i < leastSize; i++) { | |
if (Integer.parseInt(tokenizedCurrentVersion[i]) < Integer.parseInt(tokenizedLatestVersion[i])) { | |
return true; | |
} | |
} | |
if (tokenizedCurrentVersion.length >= tokenizedLatestVersion.length) { | |
return false; | |
} | |
return true; | |
} | |
public static void setUpdateCompleteTime() { | |
AnalyticsHelper.clearCustomDimensions(); | |
SharedPreferenceHelper.putPref(SHARED_PREFERENCE_NAME, LAST_UPDATED_KEY, System.currentTimeMillis()); | |
} | |
public static String getUpdatedFilePath() { | |
if (StringUtils.isEmpty(SharedPreferenceHelper.getPref(SHARED_PREFERENCE_NAME, SETUP_KEY, StringUtils.EMPTY))) { | |
return INITIAL_BUNDLE_PATH; | |
} | |
if ("ENV_RELEASE".equals("ENV_DEBUG")) { | |
return INITIAL_BUNDLE_PATH; | |
} | |
String packageName = SharedPreferenceHelper.getPref(SHARED_PREFERENCE_NAME, MAPPINGS_KEY, StringUtils.EMPTY); | |
String isUpdated = SharedPreferenceHelper.getPref(SHARED_PREFERENCE_NAME, "updated", StringUtils.EMPTY); | |
if (StringUtils.isNotEmpty(isUpdated) && isUpdated.equalsIgnoreCase(Boolean.TRUE.toString())) { | |
String path = MyntraApplication.getInstance().getFilesDir() + "/" + packageName + "-" + SharedPreferenceHelper.getPref(SHARED_PREFERENCE_NAME, VERSION_KEY, StringUtils.EMPTY); | |
if (fileExists(path)) { | |
return path; | |
} | |
} | |
return INITIAL_BUNDLE_PATH; | |
} | |
public static boolean saveFileToDisk(String fileText, String fileName) { | |
if (fileExists(MyntraApplication.getInstance().getFilesDir() + "/" + fileName)) { | |
return true; | |
} | |
FileOutputStream outputStream = null; | |
try { | |
outputStream = MyntraApplication.getInstance().openFileOutput(fileName, 0); | |
outputStream.write(fileText.getBytes()); | |
if (outputStream == null) { | |
return true; | |
} | |
try { | |
outputStream.close(); | |
return true; | |
} catch (Throwable e) { | |
C1732L.m419e(e); | |
return true; | |
} | |
} catch (Throwable e2) { | |
C1732L.m419e(e2); | |
if (outputStream != null) { | |
try { | |
outputStream.close(); | |
} catch (Throwable e22) { | |
C1732L.m419e(e22); | |
} | |
} | |
return false; | |
} catch (Throwable th) { | |
if (outputStream != null) { | |
try { | |
outputStream.close(); | |
} catch (Throwable e222) { | |
C1732L.m419e(e222); | |
} | |
} | |
} | |
} | |
private static String readRawTextFile(int resId) { | |
InputStream inputStream = MyntraApplication.getInstance().getResources().openRawResource(resId); | |
try { | |
String iOUtils = IOUtils.toString(inputStream); | |
if (inputStream == null) { | |
return iOUtils; | |
} | |
try { | |
inputStream.close(); | |
return iOUtils; | |
} catch (Throwable e) { | |
C1732L.m419e(e); | |
return iOUtils; | |
} | |
} catch (Throwable e2) { | |
C1732L.m419e(e2); | |
if (inputStream != null) { | |
try { | |
inputStream.close(); | |
} catch (Throwable e22) { | |
C1732L.m419e(e22); | |
} | |
} | |
return null; | |
} catch (Throwable th) { | |
if (inputStream != null) { | |
try { | |
inputStream.close(); | |
} catch (Throwable e222) { | |
C1732L.m419e(e222); | |
} | |
} | |
} | |
} | |
private static boolean fileExists(String path) { | |
return new File(path).exists(); | |
} | |
private static boolean fileExists(String packageName, String version) { | |
return fileExists(MyntraApplication.getInstance().getFilesDir() + "/" + packageName + "-" + version); | |
} | |
protected void onHandleIntent(Intent intent) { | |
try { | |
initialize(); | |
} catch (Throwable e) { | |
C1732L.m419e(e); | |
} | |
} | |
public static String getCurrentJSBundleVersion() { | |
return SharedPreferenceHelper.getPref(SHARED_PREFERENCE_NAME, VERSION_KEY, StringUtils.EMPTY); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment