Skip to content

Commit

Permalink
Feat: tokenscript engine js (#3346)
Browse files Browse the repository at this point in the history
* feat: initial embedded viewer PoC

* feat: Move embedded viewer to a new activity

This is needed to prevent scrolling issues

* feat: Complete transaction signing

* feat: Various cleanup & fixes

* chore: remove redundant and outdated stuff

* chore: remove redundant and outdated stuff

* feat: add schema detection to enable embedded view

* fix: various fixes for chain ID

* fix: various fixes for chain ID

* feat: add refresh button

* chore: update viewer URL

* Rebase and update

* Fix for token send and tidy up code

* fix integration test

---------

Co-authored-by: James Brown <[email protected]>
  • Loading branch information
micwallace and JamesSmartCell authored Feb 20, 2024
1 parent b36cc41 commit f60184a
Show file tree
Hide file tree
Showing 27 changed files with 1,077 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static com.alphawallet.app.steps.Steps.closeSecurityWarning;
import static com.alphawallet.app.steps.Steps.createNewWallet;
import static com.alphawallet.app.steps.Steps.gotoSettingsPage;
import static com.alphawallet.app.steps.Steps.scrollToImproved;
import static com.alphawallet.app.steps.Steps.selectMenu;
import static com.alphawallet.app.util.Helper.click;

Expand Down Expand Up @@ -38,7 +39,10 @@ public void title_should_see_crash_report_settings_page()
gotoSettingsPage();
selectMenu("Advanced");
Helper.wait(1);
onView(withId(R.id.layout)).perform(swipeUp());
onView(withId(R.id.scroll_layer)).perform(swipeUp());
onView(withId(R.id.scroll_layer)).perform(swipeUp());
onView(withSubstring("Crash")).perform(scrollToImproved());

click(withSubstring("Crash"));
shouldSee("Share Anonymous Data");
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@
android:hardwareAccelerated="true"
android:label="ERC1155 Asset Details" />

<activity
android:name=".ui.TokenScriptJsActivity"
android:hardwareAccelerated="true"
android:label="TokenScript JS Viewer" />

<activity
android:name=".ui.Erc1155AssetListActivity"
android:hardwareAccelerated="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,8 @@ public interface PreferenceRepositoryType
boolean isPostNotificationsPermissionRequested(String address);

void setPostNotificationsPermissionRequested(String address, boolean hasRequested);

boolean getUseTSViewer();

void setUseTSViewer(boolean toggleState);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class SharedPreferenceRepository implements PreferenceRepositoryType {
public static final String HIDE_ZERO_BALANCE_TOKENS = "hide_zero_balance_tokens";
public static final String FULL_SCREEN_STATE = "full_screen";
public static final String EXPERIMENTAL_1559_TX = "ex_1559_tx";
public static final String USE_TOKENSCRIPT_VIEWER = "use_ts_viewer";
public static final String DEVELOPER_OVERRIDE = "developer_override";
public static final String TESTNET_ENABLED = "testnet_enabled";
public static final String PRICE_ALERTS = "price_alerts";
Expand Down Expand Up @@ -469,10 +470,23 @@ public boolean isPostNotificationsPermissionRequested(String address) {
}

@Override
public void setPostNotificationsPermissionRequested(String address, boolean hasRequested) {
public void setPostNotificationsPermissionRequested(String address, boolean hasRequested)
{
pref.edit().putBoolean(getAddressKey(POST_NOTIFICATIONS_PERMISSION_REQUESTED, address), hasRequested).apply();
}

@Override
public boolean getUseTSViewer()
{
return pref.getBoolean(USE_TOKENSCRIPT_VIEWER, true);
}

@Override
public void setUseTSViewer(boolean state)
{
pref.edit().putBoolean(USE_TOKENSCRIPT_VIEWER, state).apply();
}

@NonNull
private String getAddressKey(String key, String address)
{
Expand Down
148 changes: 81 additions & 67 deletions app/src/main/java/com/alphawallet/app/ui/AdvancedSettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class AdvancedSettingsActivity extends BaseActivity
private SettingsItemView analytics;
private SettingsItemView crashReporting;
private SettingsItemView developerOverride;
private SettingsItemView tokenScriptViewer;
private AWalletAlertDialog waitDialog = null;

@Nullable
Expand All @@ -57,7 +58,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
viewModel = new ViewModelProvider(this)
.get(AdvancedSettingsViewModel.class);
.get(AdvancedSettingsViewModel.class);

setContentView(R.layout.activity_generic_settings);
toolbar();
Expand All @@ -82,68 +83,74 @@ public void onDestroy()
private void initializeSettings()
{
nodeStatus = new SettingsItemView.Builder(this)
.withIcon(R.drawable.ic_settings_node_status)
.withTitle(R.string.action_node_status)
.withListener(this::onNodeStatusClicked)
.build();
.withIcon(R.drawable.ic_settings_node_status)
.withTitle(R.string.action_node_status)
.withListener(this::onNodeStatusClicked)
.build();

console = new SettingsItemView.Builder(this)
.withIcon(R.drawable.ic_settings_console)
.withTitle(R.string.title_console)
.withListener(this::onConsoleClicked)
.build();
.withIcon(R.drawable.ic_settings_console)
.withTitle(R.string.title_console)
.withListener(this::onConsoleClicked)
.build();

clearBrowserCache = new SettingsItemView.Builder(this)
.withIcon(R.drawable.ic_settings_cache)
.withTitle(R.string.title_clear_browser_cache)
.withListener(this::onClearBrowserCacheClicked)
.build();
.withIcon(R.drawable.ic_settings_cache)
.withTitle(R.string.title_clear_browser_cache)
.withListener(this::onClearBrowserCacheClicked)
.build();

tokenScript = new SettingsItemView.Builder(this)
.withIcon(R.drawable.ic_settings_tokenscript)
.withTitle(R.string.title_tokenscript)
.withListener(this::onTokenScriptClicked)
.build();
.withIcon(R.drawable.ic_settings_tokenscript)
.withTitle(R.string.title_tokenscript)
.withListener(this::onTokenScriptClicked)
.build();

//TODO Change Icon
tokenScriptManagement = new SettingsItemView.Builder(this)
.withIcon(R.drawable.ic_settings_tokenscript_manage)
.withTitle(R.string.tokenscript_management)
.withListener(this::onTokenScriptManagementClicked)
.build();
.withIcon(R.drawable.ic_settings_tokenscript_manage)
.withTitle(R.string.tokenscript_management)
.withListener(this::onTokenScriptManagementClicked)
.build();

tokenScriptViewer = new SettingsItemView.Builder(this)
.withType(SettingsItemView.Type.TOGGLE)
.withIcon(R.drawable.ic_tokenscript)
.withTitle(R.string.use_tokenscript_viewer)
.withListener(this::onUseTokenScriptViewer)
.build();

fullScreenSettings = new SettingsItemView.Builder(this)
.withType(SettingsItemView.Type.TOGGLE)
.withIcon(R.drawable.ic_phoneicon)
.withTitle(R.string.fullscreen)
.withListener(this::onFullScreenClicked)
.build();
.withType(SettingsItemView.Type.TOGGLE)
.withIcon(R.drawable.ic_phoneicon)
.withTitle(R.string.fullscreen)
.withListener(this::onFullScreenClicked)
.build();

refreshTokenDatabase = new SettingsItemView.Builder(this)
.withIcon(R.drawable.ic_settings_reset_tokens)
.withTitle(R.string.title_reload_token_data)
.withListener(this::onReloadTokenDataClicked)
.build();
.withIcon(R.drawable.ic_settings_reset_tokens)
.withTitle(R.string.title_reload_token_data)
.withListener(this::onReloadTokenDataClicked)
.build();

eip1559Transactions = new SettingsItemView.Builder(this)
.withType(SettingsItemView.Type.TOGGLE)
.withIcon(R.drawable.ic_icons_settings_1559)
.withTitle(R.string.experimental_1559)
// .withSubtitle(R.string.experimental_1559_tx_sub)
.withListener(this::on1559TransactionsClicked)
.build();
.withType(SettingsItemView.Type.TOGGLE)
.withIcon(R.drawable.ic_icons_settings_1559)
.withTitle(R.string.experimental_1559)
.withListener(this::on1559TransactionsClicked)
.build();

analytics = new SettingsItemView.Builder(this)
.withIcon(R.drawable.ic_settings_analytics)
.withTitle(R.string.settings_title_analytics)
.withListener(this::onAnalyticsClicked)
.build();
.withIcon(R.drawable.ic_settings_analytics)
.withTitle(R.string.settings_title_analytics)
.withListener(this::onAnalyticsClicked)
.build();

crashReporting = new SettingsItemView.Builder(this)
.withIcon(R.drawable.ic_settings_crash_reporting)
.withTitle(R.string.settings_title_crash_reporting)
.withListener(this::onCrashReportingClicked)
.build();
.withIcon(R.drawable.ic_settings_crash_reporting)
.withTitle(R.string.settings_title_crash_reporting)
.withListener(this::onCrashReportingClicked)
.build();

developerOverride = new SettingsItemView.Builder(this)
.withType(SettingsItemView.Type.TOGGLE)
Expand All @@ -155,6 +162,7 @@ private void initializeSettings()
fullScreenSettings.setToggleState(viewModel.getFullScreenState());
eip1559Transactions.setToggleState(viewModel.get1559TransactionsState());
developerOverride.setToggleState(viewModel.getDeveloperOverrideState());
tokenScriptViewer.setToggleState(viewModel.getTokenScriptViewerState());
}

private void onDeveloperOverride()
Expand All @@ -172,6 +180,11 @@ private void on1559TransactionsClicked()
viewModel.toggle1559Transactions(eip1559Transactions.getToggleState());
}

private void onUseTokenScriptViewer()
{
viewModel.toggleUseViewer(tokenScriptViewer.getToggleState());
}

private void addSettingsToLayout()
{
LinearLayout advancedSettingsLayout = findViewById(R.id.layout);
Expand All @@ -186,6 +199,7 @@ private void addSettingsToLayout()
advancedSettingsLayout.addView(fullScreenSettings);
advancedSettingsLayout.addView(refreshTokenDatabase);
advancedSettingsLayout.addView(eip1559Transactions);
advancedSettingsLayout.addView(tokenScriptViewer);
advancedSettingsLayout.addView(analytics);
advancedSettingsLayout.addView(crashReporting);
advancedSettingsLayout.addView(developerOverride);
Expand All @@ -204,25 +218,25 @@ private void onConsoleClicked()
private void onClearBrowserCacheClicked()
{
Single.fromCallable(() ->
{
WebView webView = new WebView(this);
webView.clearCache(true);
webView.clearFormData();
webView.clearHistory();
webView.clearSslPreferences();
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookies(null);
WebStorage.getInstance().deleteAllData();
viewModel.blankFilterSettings();
Glide.get(this).clearDiskCache();
return 1;
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(v ->
{
Toast.makeText(this, getString(R.string.toast_browser_cache_cleared), Toast.LENGTH_SHORT).show();
finish();
}).isDisposed();
{
WebView webView = new WebView(this);
webView.clearCache(true);
webView.clearFormData();
webView.clearHistory();
webView.clearSslPreferences();
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookies(null);
WebStorage.getInstance().deleteAllData();
viewModel.blankFilterSettings();
Glide.get(this).clearDiskCache();
return 1;
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(v ->
{
Toast.makeText(this, getString(R.string.toast_browser_cache_cleared), Toast.LENGTH_SHORT).show();
finish();
}).isDisposed();
}

private void onReloadTokenDataClicked()
Expand All @@ -242,9 +256,9 @@ private void onReloadTokenDataClicked()
viewModel.stopChainActivity();
showWaitDialog();
clearTokenCache = viewModel.resetTokenData()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::showResetResult);
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::showResetResult);

viewModel.blankFilterSettings();
});
Expand Down Expand Up @@ -340,7 +354,7 @@ private void askWritePermission()
private boolean checkWritePermission()
{
return ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED;
== PackageManager.PERMISSION_GRANTED;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ private void displayCloseWC()
private void setupWeb3(Wallet wallet)
{
if (wallet == null) { return; }
web3.setChainId(activeNetwork.chainId);
web3.setChainId(activeNetwork.chainId, false);
web3.setWalletAddress(new Address(wallet.address));

web3.setWebChromeClient(new WebChromeClient()
Expand Down
Loading

0 comments on commit f60184a

Please sign in to comment.