Skip to content

Commit

Permalink
Optimise TS Loading and fix memory issue
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesSmartCell committed Oct 8, 2023
1 parent 8c7c7e1 commit b43e6ba
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 73 deletions.
2 changes: 0 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,6 @@ dependencies {
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
implementation 'com.google.zxing:core:3.5.2'

implementation 'com.gu.android:toolargetool:0.3.0'

// Sugar
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,16 @@ public boolean isValidTokenScript()
return active || resourceFile;
}


public String calcMD5()
{
return calcMD5(getInputStream());
}

public static String calcMD5(InputStream fis)
{
StringBuilder sb = new StringBuilder();
try
{
InputStream fis = getInputStream();
MessageDigest digest = MessageDigest.getInstance("MD5");

byte[] byteArray = new byte[1024];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import org.xml.sax.SAXException;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
Expand All @@ -93,6 +94,7 @@
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -1304,6 +1306,11 @@ private File storeEntry(Token token, Pair<String, Pair<String, Boolean>> scriptD

String tempFileKey = token.getTSKey();

if (!checkFileDiff(tempFileKey, scriptData.second))
{
return new File(UNCHANGED_SCRIPT);
}

File tempStoreFile = storeFile(tempFileKey, scriptData.second);

TokenScriptFile tsf = new TokenScriptFile(context, tempStoreFile.getAbsolutePath());
Expand Down Expand Up @@ -2072,6 +2079,27 @@ private XMLDsigDescriptor IPFSSigDescriptor()
return sig;
}

private boolean checkFileDiff(String address, Pair<String, Boolean> result)
{
if (result.first == null || result.first.length() < 10)
{
return false;
}

//calc MD5 of this new script
String newMD5Hash = TokenScriptFile.calcMD5(new ByteArrayInputStream(result.first.getBytes(StandardCharsets.UTF_8)));
TokenScriptFile tsf = new TokenScriptFile(context, defineDownloadTSFile(address).getAbsolutePath());

if (tsf.exists())
{
return !tsf.calcMD5().equals(newMD5Hash);
}
else
{
return true;
}
}

/**
* Use internal directory to store contracts fetched from the server
*
Expand All @@ -2082,13 +2110,12 @@ private XMLDsigDescriptor IPFSSigDescriptor()
*/
private File storeFile(String address, Pair<String, Boolean> result) throws IOException
{
if (result.first == null || result.first.length() < 10) return new File("");

String fName = address + TS_EXTENSION;

//Store received files in the internal storage area - no need to ask for permissions
File file = new File(context.getFilesDir(), fName);
if (result.first == null || result.first.length() < 10)
{
return new File("");
}

File file = defineDownloadTSFile(address);
FileOutputStream fos = new FileOutputStream(file);
OutputStream os = new BufferedOutputStream(fos);
os.write(result.first.getBytes());
Expand All @@ -2107,6 +2134,13 @@ private File storeFile(String address, Pair<String, Boolean> result) throws IOEx
return file;
}

private File defineDownloadTSFile(String address)
{
String fName = address + TS_EXTENSION;
//Store received files in the internal storage area - no need to ask for permissions
return new File(context.getFilesDir(), fName);
}

public boolean hasDefinition(Token token)
{
boolean hasDefinition = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private void viewHeight(int fetchedViewHeight)
{
if (fetchedViewHeight < 100)
{
initWebViewCheck();
initWebViewCheck(viewModel.getAssetDefinitionService().getAssetDefinition(token));
handler.postDelayed(this, TOKEN_SIZING_DELAY); //wait 3 seconds until ending height check
}
else
Expand All @@ -196,14 +196,14 @@ private void viewHeight(int fetchedViewHeight)
private void onNewScript(TokenDefinition td)
{
//need to reload tokens, now we have an updated/new script
if (td != null)
if (td != null && td.isChanged())
{
initWebViewCheck();
initWebViewCheck(td);
handler.postDelayed(this, TOKEN_SIZING_DELAY);
}
}

private void initWebViewCheck()
private void initWebViewCheck(TokenDefinition td)
{
checkVal = 0;
itemViewHeight = 0;
Expand All @@ -212,7 +212,7 @@ private void initWebViewCheck()
{
BigInteger tokenId = token.getArrayBalance().get(0);
TicketRange data = new TicketRange(tokenId, token.getAddress());
testView.renderTokenScriptView(token, data, viewModel.getAssetDefinitionService(), ViewType.ITEM_VIEW);
testView.renderTokenScriptView(token, data, viewModel.getAssetDefinitionService(), ViewType.ITEM_VIEW, td);
testView.setOnReadyCallback(this);
}
else
Expand Down
34 changes: 23 additions & 11 deletions app/src/main/java/com/alphawallet/app/ui/FunctionActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;
import com.gu.toolargetool.TooLargeTool;

/**
* Created by James on 4/04/2019.
Expand All @@ -111,16 +110,23 @@ public class FunctionActivity extends BaseActivity implements FunctionCallback,
private TSAction action;
private ActionSheet confirmationDialog;

private void initViews() {
private void initViews()
{
actionMethod = getIntent().getStringExtra(C.EXTRA_STATE);
String tokenIdStr = getIntent().getStringExtra(C.EXTRA_TOKEN_ID);
if (tokenIdStr == null || tokenIdStr.length() == 0) tokenIdStr = "0";
if (tokenIdStr == null || tokenIdStr.length() == 0)
{
tokenIdStr = "0";
}

Wallet wallet = getIntent().getParcelableExtra(C.Key.WALLET);
asset = getIntent().getParcelableExtra(C.EXTRA_NFTASSET);
if (wallet == null) {
if (wallet == null)
{
viewModel.getCurrentWallet();
} else {
}
else
{
viewModel.loadWallet(wallet.address);
}

Expand Down Expand Up @@ -168,15 +174,17 @@ private void displayFunction(String tokenAttrs)
TSAction action = functions.get(actionMethod);
String magicValues = viewModel.getAssetDefinitionService().getMagicValuesForInjection(token.tokenInfo.chainId);

if (Objects.equals(action.view.getUrl(), ""))
if (TextUtils.isEmpty(Objects.requireNonNull(action).view.getUrl()))
{
String injectedView = tokenView.injectWeb3TokenInit(action.view.getTokenView(), tokenAttrs, tokenId);
injectedView = tokenView.injectJSAtEnd(injectedView, magicValues);
injectedView = tokenView.injectStyleAndWrapper(injectedView, action.style + "\n" + action.view.getStyle());

String base64 = Base64.encodeToString(injectedView.getBytes(StandardCharsets.UTF_8), Base64.DEFAULT);
tokenView.loadData(base64 + (!Objects.equals(action.view.getUrlFragment(), "") ? "#" + action.view.getUrlFragment() : ""), "text/html; charset=utf-8", "base64");
} else {
}
else
{
tokenView.loadUrl(action.view.getUrl());
}
}
Expand Down Expand Up @@ -282,14 +290,16 @@ protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_script_view);
setupViews();

TooLargeTool.startLogging(getApplication());
}

private void setupViews()
{
initViewModel();
resumeViews();
}

private void resumeViews()
{
initViews();
toolbar();
setTitle(actionMethod);
Expand All @@ -302,11 +312,12 @@ public void onResume()
super.onResume();
if (viewModel == null)
{
initViews();
setupViews();
}

if (parsePass == 0)
{
resumeViews();
parsePass = 1;
viewModel.getAssetDefinitionService().clearResultMap();
args.clear();
Expand Down Expand Up @@ -666,6 +677,7 @@ public void onPause()
{
super.onPause();
viewModel.resetSignDialog();
tokenView.destroy();
}

public void onSaveInstanceState(@NonNull Bundle savedInstanceState)
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/java/com/alphawallet/app/ui/HomeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
import com.journeyapps.barcodescanner.ScanContract;
import com.journeyapps.barcodescanner.ScanOptions;
import com.walletconnect.android.CoreClient;
import com.gu.toolargetool.TooLargeTool;

import net.yslibrary.android.keyboardvisibilityevent.KeyboardVisibilityEvent;

Expand Down Expand Up @@ -278,8 +277,6 @@ public void onPageScrollStateChanged(int state)
viewModel.defaultWallet().observe(this, this::onDefaultWallet);
viewModel.updateAvailable().observe(this, this::onUpdateAvailable);

TooLargeTool.startLogging(getApplication());

if (CustomViewSettings.hideDappBrowser())
{
removeDappBrowser();
Expand Down Expand Up @@ -612,7 +609,7 @@ protected void onPause()
@Override
public void onSaveInstanceState(@NonNull Bundle savedInstanceState)
{
super.onSaveInstanceState(savedInstanceState);
super.onSaveInstanceState(new Bundle());
savedInstanceState.putInt(STORED_PAGE, viewPager.getCurrentItem());
if (getSelectedItem() != null)
{
Expand Down
Loading

0 comments on commit b43e6ba

Please sign in to comment.