Repository URL to install this package:
|
Version:
0.0.6 ▾
|
react-native-motif-sdk
/
android
/
src
/
main
/
java
/
com
/
vipera
/
de
/
rn
/
assetmanager
/
RNUpdateManager.java
|
|---|
package com.vipera.de.rn.assetmanager;
import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import com.vipera.de.motifconnector.nativekit.assets.DEAssetDownloadRequest;
import com.vipera.de.rn.assetmanager.models.AppCheckRequest;
import com.vipera.de.rn.commons.RNContextHelper;
import com.vipera.de.utility.Version;
import java.io.File;
public class RNUpdateManager {
private static final String LOG_TAG = "RNUpdateManager";
private static final String ASSET_UPDATE_PREFS = "DEAssetUpdate";
private static final String DOWNLOAD_TIMESTAMP_KEY = "downloadTimestamp";
private static final String BUNDLE_NAME_KEY = "bundleName";
private static final String VERSION_KEY = "version";
private static final String DEPLOY_FOLDER_KEY = "deployFolder";
private static final String BACKUP_AVAILABLE_KEY = "backupAvailable";
private static final String LAST_APP_CHECK = "lastAppCheck";
private static final String TMP_FOLDER_NAME = "assets_tmp";
private static final String DEFAULT_DEPLOY_FOLDER = "deployFolder";
private static final String BACKUP_FOLDER = "assets_backup";
private final SharedPreferences preferences;
private final RNAssetManagerConfig configuration;
private final Version appVersion;
private final File fileDir;
public RNUpdateManager(Application application, RNAssetManagerConfig config){
Log.i(LOG_TAG,"RNUpdateManager constructor");
this.configuration = config;
this.preferences = application.getSharedPreferences(ASSET_UPDATE_PREFS,Context.MODE_PRIVATE);
this.appVersion = RNContextHelper.getAppVersion(application);
this.fileDir = application.getFilesDir();
}
public RNAssetBundleInfo getCurrentBundleInfo(){
if(isDownloadedAssetsAvailable()){
return readBundleInfo();
}else{
return createEmbeddedBundleInfo();
}
}
private RNAssetBundleInfo createEmbeddedBundleInfo() {
return RNAssetBundleInfo.createEmbedded(getEmbeddedAssetVersion(),getEmbeddedBundleName());
}
private String getEmbeddedBundleName() {
return configuration.getEmbeddedBundleName();
}
private Version getEmbeddedAssetVersion() {
return configuration.getEmbeddedAssetVersion();
}
private RNAssetBundleInfo readBundleInfo() {
long timestamp = preferences.getLong(DOWNLOAD_TIMESTAMP_KEY,-1);
String versionStr = preferences.getString(VERSION_KEY,null);
if(versionStr == null){
throw new IllegalStateException("invalid version");
}
String bundleName = preferences.getString(BUNDLE_NAME_KEY,null);
String deployFolder = preferences.getString(DEPLOY_FOLDER_KEY,DEFAULT_DEPLOY_FOLDER);
return new RNAssetBundleInfo(RNAssetBundleInfo.Type.DOWNLOADED,Version.tryParse(versionStr),bundleName,deployFolder,timestamp);
}
@SuppressLint("ApplySharedPref")
private void writeBundleInfo(RNAssetBundleInfo bundleInfo){
preferences.edit()
.putLong(DOWNLOAD_TIMESTAMP_KEY,bundleInfo.getDeployTimestamp())
.putString(VERSION_KEY,bundleInfo.getVersion().asString())
.putString(BUNDLE_NAME_KEY,bundleInfo.getBundleName())
.putString(DEPLOY_FOLDER_KEY,bundleInfo.getDeployFolder())
.commit();
}
public boolean isDownloadedAssetsAvailable() {
long lastDownloadedTimestamp = preferences.getLong(DOWNLOAD_TIMESTAMP_KEY,-1);
return lastDownloadedTimestamp >= 0;
}
@SuppressLint("ApplySharedPref")
public void invalidateCurrentInstance() {
preferences.edit()
.remove(DOWNLOAD_TIMESTAMP_KEY)
.remove(VERSION_KEY)
.remove(BUNDLE_NAME_KEY)
.remove(DEPLOY_FOLDER_KEY)
.remove(BACKUP_AVAILABLE_KEY)
.commit();
FileUtils.deleteFileOrFolderSilently(getAssetBackupDir());
FileUtils.deleteFileOrFolderSilently(getAssetDownloadDir());
FileUtils.deleteFileOrFolderSilently(getCurrentAssetDir());
}
public AppCheckRequest buildAppCheckRequest() {
RNAssetBundleInfo rnAssetBundleInfo = getCurrentBundleInfo();
return new AppCheckRequest.Builder()
.setAsset(configuration.getAssetManagerAssetsName())
.setAssetVersion(rnAssetBundleInfo.getVersion())
.setEngine(configuration.getAssetManagerEngineName())
.setEngineVersion(appVersion)
.build();
}
@SuppressLint("ApplySharedPref")
public void markLatestAppCheck() {
preferences.edit()
.putLong(LAST_APP_CHECK,System.currentTimeMillis())
.commit();
}
@SuppressLint("ApplySharedPref")
public void markBackupAvailable(boolean value) {
preferences.edit()
.putBoolean(BACKUP_AVAILABLE_KEY,value)
.commit();
}
public long getLastAppCheck(){
return preferences.getLong(LAST_APP_CHECK,-1);
}
public DEAssetDownloadRequest buildAssetDownloadRequest(Version currentVersion,Version versionToDownload) {
return new DEAssetDownloadRequest(configuration.getAssetManagerAssetsName(),getAssetDownloadDirPath(),versionToDownload);
}
public DEAssetDownloadRequest buildAssetDownloadRequest() {
return new DEAssetDownloadRequest(configuration.getAssetManagerAssetsName(),getAssetDownloadDirPath(),null);
}
private File getAssetDownloadDir(){
return new File(fileDir, TMP_FOLDER_NAME);
}
private File getCurrentAssetDir() {
return new File(fileDir, DEFAULT_DEPLOY_FOLDER);
}
private File getAssetBackupDir() {
return new File(fileDir, BACKUP_FOLDER);
}
public String getAssetDownloadDirPath() {
return getAssetDownloadDir().getAbsolutePath();
}
public boolean prepareDownloadDir(){
return prepareEmptyFolder(getAssetDownloadDir());
}
public boolean prepareBackupdDir(){
return prepareEmptyFolder(getAssetBackupDir());
}
public boolean commitDeploy(Version version) {
Log.i(LOG_TAG,"commitDeploy start..");
File tmpDir = getAssetDownloadDir();
File currentDir = getCurrentAssetDir();
File backupDir = getAssetBackupDir();
if(!currentDir.exists()){
Log.i(LOG_TAG,"No deploy found: rename download folder to deployFolder");
boolean success = tmpDir.renameTo(currentDir);
if(!success){
Log.i(LOG_TAG,"Fail rename: revert to embedded version");
invalidateCurrentInstance();
return false;
}
Log.i(LOG_TAG,"Create bundle info");
RNAssetBundleInfo assetBundleInfo = RNAssetBundleInfo.createDownloaded(version,configuration.getDeployedBundleName(),DEFAULT_DEPLOY_FOLDER,System.currentTimeMillis());
Log.i(LOG_TAG,"Write bundle info: " + assetBundleInfo);
writeBundleInfo(assetBundleInfo);
}else{
Log.i(LOG_TAG,"Found deployFolder: prepare backup dir ");
prepareBackupdDir();
Log.i(LOG_TAG,"Found deployFolder: rename to backup in order to prepare switch");
boolean success = currentDir.renameTo(backupDir);
if(!success){
return false;
}
markBackupAvailable(true);
if(!tmpDir.renameTo(getCurrentAssetDir())){
invalidateCurrentInstance();
return false;
}
RNAssetBundleInfo assetBundleInfo = RNAssetBundleInfo.createDownloaded(version,configuration.getDeployedBundleName(),DEFAULT_DEPLOY_FOLDER,System.currentTimeMillis());
writeBundleInfo(assetBundleInfo);
markBackupAvailable(false);
clearTransitoryFolders();
}
return true;
}
public void clearTransitoryFolders() {
Log.i(LOG_TAG,"clearTransitoryFolders");
try {
FileUtils.deleteFileOrFolderSilently(getAssetBackupDir());
FileUtils.deleteFileOrFolderSilently(getAssetDownloadDir());
}catch (Exception ex){
Log.e(LOG_TAG,"clearTransitoryFolders fail: " + ex.getMessage(),ex);
}
}
private boolean prepareEmptyFolder(File folder) {
Log.d(LOG_TAG,"prepareEmptyFolder : " + folder.getAbsolutePath());
try {
FileUtils.deleteDirectoryAtPath(folder.getPath());
return folder.mkdir();
}catch (Exception ex){
Log.e(LOG_TAG,"prepareEmptyFolder fail: " + ex.getMessage(),ex);
return false;
}
}
}