增加webview显示网页

This commit is contained in:
zhl 2023-03-29 10:50:46 +08:00
parent c386669773
commit 7739778c0e
5 changed files with 210 additions and 89 deletions

View File

@ -2,42 +2,41 @@ console.log(">>begin load wallet main file");
function promiseCb(funId, promiseFun, dataParser) { function promiseCb(funId, promiseFun, dataParser) {
dataParser = dataParser || ((v) => v); dataParser = dataParser || ((v) => v);
promiseFun.then((result) => { promiseFun
jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: result })); .then((result) => {
}) jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: result }));
.catch((err) => { })
jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err })); .catch((err) => {
}); jsb.jcCallback(funId, JSON.stringify({ errcode: 1, errmsg: err }));
});
} }
/** /**
* init wallet, must call this before all other method * init wallet, must call this before all other method
* @param {string} type: wallet type, 0: internal wallet, 1: third party wallet * @param {string} type: wallet type, 0: internal wallet, 1: third party wallet
* @param {string} chain: chain type, 1: ETH, 2: ICON
* @param {string} channel: channel type, 0: google, 1: apple, 2: tiktok, 3: facebook, 4: twitter
*/ */
function initWallet(funId, type, chain, channel, test) { function initWallet(funId, type, chain, channel = 0) {
type = parseInt(type);
chain = parseInt(chain); chain = parseInt(chain);
channel = channel || 0; // 0:google, 1: apple, 2: tiktok
channel = parseInt(channel);
console.log( console.log(
`initWallet:: type: ${type}, chain: ${chain}, channel: ${channel}` `initWallet:: type: ${type}, chain: ${chain}, channel: ${channel}`
); );
try { try {
let wallet; const wallet =
if (!window.jc || !jc.wallet) { !window.jc || !jc.wallet
wallet = new jcwallet.default({ chain, type }); ? new jcwallet.default({ chain, type })
} else { : jc.wallet;
wallet = jc.wallet;
} if (parseInt(type) === 1) {
type = parseInt(type);
if (type === 1) {
console.log("wallet init success, begin connect"); console.log("wallet init success, begin connect");
wallet wallet
.initThirdPartyWallet() .initThirdPartyWallet()
.then(() => { .then(() => {
console.log("walletconnect connect success"); console.log("walletconnect connect success");
let account = jc.wallet.currentAccount(); jsb.jcCallback(
jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: account })); funId,
JSON.stringify({ errcode: 0, data: jc.wallet.currentAccount() })
);
}) })
.catch((err) => { .catch((err) => {
console.log("walletconnect connect error: " + JSON.stringify(err)); console.log("walletconnect connect error: " + JSON.stringify(err));
@ -45,11 +44,13 @@ function initWallet(funId, type, chain, channel, test) {
}); });
} else { } else {
wallet wallet
.initInternalWallet(channel) .initInternalWallet(parseInt(channel))
.then(() => { .then(() => {
console.log("internal init success"); console.log("internal init success");
let address = jc.wallet.nativeAccount; jsb.jcCallback(
jsb.jcCallback(funId, JSON.stringify({ errcode: 0, data: address })); funId,
JSON.stringify({ errcode: 0, data: jc.wallet.nativeAccount })
);
}) })
.catch((err) => { .catch((err) => {
console.log("internal wallet error: " + JSON.stringify(err)); console.log("internal wallet error: " + JSON.stringify(err));
@ -131,7 +132,7 @@ function getEthBalance(funId, account) {
*/ */
function sendEth(funId, to, amount, estimate) { function sendEth(funId, to, amount, estimate) {
estimate = (estimate || "0") | 0; estimate = (estimate || "0") | 0;
promiseCb(funId, jc.wallet.sendEth(to, amount, estimate)) promiseCb(funId, jc.wallet.sendEth(to, amount, estimate));
} }
/** /**
@ -197,7 +198,10 @@ function sendErc1155(funId, address, to, tokenIds, amounts, estimate) {
tokenIds = JSON.parse(tokenIds); tokenIds = JSON.parse(tokenIds);
amounts = JSON.parse(amounts); amounts = JSON.parse(amounts);
estimate = (estimate || "0") | 0; estimate = (estimate || "0") | 0;
promiseCb(funId, jc.wallet.sendErc1155(address, to, tokenIds, amounts, estimate)); promiseCb(
funId,
jc.wallet.sendErc1155(address, to, tokenIds, amounts, estimate)
);
} }
function showQRCode(funId, content) { function showQRCode(funId, content) {
@ -212,7 +216,7 @@ function showQRCode(funId, content) {
function showWebPage(funId, url) { function showWebPage(funId, url) {
try { try {
jsb.showWebPage(funId, url); jsb.showWebPage(funId, url);
// jsb.openURL(url); // jsb.openURL(url);
return JSON.stringify({ errcode: 0, data: 1 }); return JSON.stringify({ errcode: 0, data: 1 });
} catch (err) { } catch (err) {
return JSON.stringify({ errcode: 1, errmsg: err }); return JSON.stringify({ errcode: 1, errmsg: err });
@ -238,12 +242,16 @@ function buyNft721(funId, addresses, values, signature, estimate) {
addresses = JSON.parse(addresses); addresses = JSON.parse(addresses);
values = JSON.parse(values); values = JSON.parse(values);
estimate = (estimate || "0") | 0; estimate = (estimate || "0") | 0;
promiseCb(funId, jc.wallet.jcStandard.buyNft721({ promiseCb(
funId,
jc.wallet.jcStandard.buyNft721({
addresses, addresses,
values, values,
signature, signature,
estimate, estimate,
}), (v)=>JSON.stringify(v)); }),
(v) => JSON.stringify(v)
);
} }
function buyNft1155( function buyNft1155(
@ -261,15 +269,18 @@ function buyNft1155(
amounts = JSON.parse(amounts); amounts = JSON.parse(amounts);
estimate = (estimate || "0") | 0; estimate = (estimate || "0") | 0;
promiseCb(funId, jc.wallet.jcStandard promiseCb(
.buyNft1155({ funId,
jc.wallet.jcStandard.buyNft1155({
addresses, addresses,
values, values,
ids, ids,
amounts, amounts,
signature, signature,
estimate, estimate,
}), (v)=>JSON.stringify(v)); }),
(v) => JSON.stringify(v)
);
} }
function evolveNft721( function evolveNft721(
@ -283,30 +294,34 @@ function evolveNft721(
) { ) {
tokenIds = JSON.parse(tokenIds); tokenIds = JSON.parse(tokenIds);
estimate = (estimate || "0") | 0; estimate = (estimate || "0") | 0;
promiseCb(funId, promiseCb(
jc.wallet.jcStandard funId,
.evolve721NFT({ jc.wallet.jcStandard.evolve721NFT({
nftAddress, nftAddress,
tokenIds,
startTime,
nonce,
signature,
estimate,
}), (v)=>JSON.stringify(v));
}
function evolveChip(funId, tokenIds, startTime, nonce, signature, estimate) {
tokenIds = JSON.parse(tokenIds);
estimate = (estimate || "0") | 0;
promiseCb(funId,
jc.wallet.jcStandard
.evolveChip({
tokenIds, tokenIds,
startTime, startTime,
nonce, nonce,
signature, signature,
estimate, estimate,
}), (v)=>JSON.stringify(v)); }),
(v) => JSON.stringify(v)
);
}
function evolveChip(funId, tokenIds, startTime, nonce, signature, estimate) {
tokenIds = JSON.parse(tokenIds);
estimate = (estimate || "0") | 0;
promiseCb(
funId,
jc.wallet.jcStandard.evolveChip({
tokenIds,
startTime,
nonce,
signature,
estimate,
}),
(v) => JSON.stringify(v)
);
} }
function mintShardBatchUser( function mintShardBatchUser(
@ -321,16 +336,18 @@ function mintShardBatchUser(
tokenIds = JSON.parse(tokenIds); tokenIds = JSON.parse(tokenIds);
amounts = JSON.parse(amounts); amounts = JSON.parse(amounts);
estimate = (estimate || "0") | 0; estimate = (estimate || "0") | 0;
promiseCb(funId, promiseCb(
jc.wallet.jcStandard funId,
.mintShardBatchUser({ jc.wallet.jcStandard.mintShardBatchUser({
tokenIds, tokenIds,
amounts, amounts,
startTime, startTime,
nonce, nonce,
signature, signature,
estimate, estimate,
}), (v)=>JSON.stringify(v)); }),
(v) => JSON.stringify(v)
);
} }
function shardMixByUser( function shardMixByUser(
@ -349,9 +366,9 @@ function shardMixByUser(
ids = JSON.parse(ids); ids = JSON.parse(ids);
amounts = JSON.parse(amounts); amounts = JSON.parse(amounts);
estimate = (estimate || "0") | 0; estimate = (estimate || "0") | 0;
promiseCb(funId, promiseCb(
jc.wallet.jcStandard funId,
.shardMixByUser({ jc.wallet.jcStandard.shardMixByUser({
tokenId, tokenId,
nftType, nftType,
payToken, payToken,
@ -362,7 +379,9 @@ function shardMixByUser(
nonce, nonce,
signature, signature,
estimate, estimate,
}), (v)=>JSON.stringify(v)); }),
(v) => JSON.stringify(v)
);
} }
// addresses: [nftId, chip, sign_address] // addresses: [nftId, chip, sign_address]
@ -382,16 +401,18 @@ function pluginChip(
values = JSON.parse(values); values = JSON.parse(values);
chipIds = JSON.parse(chipIds); chipIds = JSON.parse(chipIds);
slots = JSON.parse(slots); slots = JSON.parse(slots);
promiseCb(funId, promiseCb(
jc.wallet.jcStandard funId,
.pluginChip({ jc.wallet.jcStandard.pluginChip({
addresses, addresses,
values, values,
chipIds, chipIds,
slots, slots,
signature, signature,
estimate, estimate,
}), (v)=>JSON.stringify(v)); }),
(v) => JSON.stringify(v)
);
} }
// addresses: [nftId, chip, sign_address] // addresses: [nftId, chip, sign_address]
@ -411,16 +432,18 @@ function unplugChip(
chipIds = JSON.parse(chipIds); chipIds = JSON.parse(chipIds);
slots = JSON.parse(slots); slots = JSON.parse(slots);
estimate = (estimate || "0") | 0; estimate = (estimate || "0") | 0;
promiseCb(funId, promiseCb(
jc.wallet.jcStandard funId,
.unplugChip({ jc.wallet.jcStandard.unplugChip({
addresses, addresses,
values, values,
chipIds, chipIds,
slots, slots,
signature, signature,
estimate, estimate,
}), (v)=>JSON.stringify(v)); }),
(v) => JSON.stringify(v)
);
} }
// ======= end of interact with contract ======= // ======= end of interact with contract =======
@ -430,7 +453,7 @@ function ethHistory(funId, start, limit) {
} }
function tokenHistory(funId, start, limit, address, tokenId) { function tokenHistory(funId, start, limit, address, tokenId) {
var data = {start, limit, address, tokenId} var data = { start, limit, address, tokenId };
promiseCb(funId, jc.wallet.historySvr.tokenRecords(data)) promiseCb(funId, jc.wallet.historySvr.tokenRecords(data));
} }
// ======= end of transaction history ======= // ======= end of transaction history =======

View File

@ -48,6 +48,9 @@
<activity <activity
android:name="com.king.zxing.CaptureActivity" android:name="com.king.zxing.CaptureActivity"
android:theme="@style/CaptureTheme"/> android:theme="@style/CaptureTheme"/>
<activity
android:name=".activity.WebPageActivity"
android:theme="@style/CaptureTheme"/>
<activity <activity
android:name=".activity.CustomCaptureActivity" android:name=".activity.CustomCaptureActivity"
android:theme="@style/CaptureTheme"/> android:theme="@style/CaptureTheme"/>

View File

@ -1,6 +1,7 @@
package com.cege.games.release; package com.cege.games.release;
import android.Manifest; import android.Manifest;
import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
@ -13,6 +14,8 @@ import android.provider.MediaStore;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.view.Window; import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast; import android.widget.Toast;
import com.bytedance.sdk.open.tiktok.TikTokOpenApiFactory; import com.bytedance.sdk.open.tiktok.TikTokOpenApiFactory;
@ -121,7 +124,6 @@ public class MainActivity extends UnityPlayerActivity
private String oid; private String oid;
private QRCodeActivity qrCodeActivity; private QRCodeActivity qrCodeActivity;
private WebPageActivity webPageActivity;
// AppAuth // AppAuth
private AuthorizationService mAuthService; private AuthorizationService mAuthService;
@ -876,13 +878,12 @@ public class MainActivity extends UnityPlayerActivity
} }
} }
public void showPage(String fid, String url) { public void showPage(String fid, final String url) {
runOnUiThread(() -> { runOnUiThread(() -> {
if (webPageActivity == null) { Log.i(TAG, "show page: " + url);
webPageActivity = new WebPageActivity(getContext()); Intent intent = new Intent(this, WebPageActivity.class);
} intent.putExtra("url", url);
webPageActivity.showWebPage(url); startActivity(intent);
webPageActivity.show();
}); });
} }

View File

@ -1,21 +1,103 @@
package com.cege.games.release.activity; package com.cege.games.release.activity;
import android.app.Dialog; import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.webkit.ConsoleMessage;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView; import android.webkit.WebView;
public class WebPageActivity extends Dialog { import android.webkit.WebViewClient;
public WebPageActivity(Context context) { import com.cege.games.release.R;
super(context); import com.cege.games.release.dialog.BaseDialog;
public class WebPageActivity extends Activity {
private WebView mWebView;
private static final String TAG = WebPageActivity.class.getSimpleName();
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_page);
WebView.setWebContentsDebuggingEnabled(true);
mWebView = findViewById(R.id.web_view);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setDatabaseEnabled(true);
webSettings.setAllowContentAccess(true);
webSettings.setAppCacheEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView,true);
// get url from intent
Intent intent = getIntent();
String url = intent.getStringExtra("url");
// show web view
mWebView.loadUrl(url);
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
Log.i(TAG, "onReceivedTitle: " + title);
}
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Log.e("TAG", consoleMessage.message());
return true;
}
});
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
// if ("www.example.com".equals(request.getUrl().getHost())) {
// // This is my website, so do not override; let my WebView load the page
// return false;
// }
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.i(TAG, "onPageFinished: " + url);
}
@Override
public void onLoadResource(WebView view, String url) {
Log.i(TAG, "onLoadResource: " + url);
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
Log.e(TAG, "onReceivedError: " + error);
}
@Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
super.onReceivedHttpError(view, request, errorResponse);
Log.e(TAG, "onReceivedHttpError: " + errorResponse);
}
@Override
public void onReceivedSslError(WebView view, android.webkit.SslErrorHandler handler,
android.net.http.SslError error) {
Log.e(TAG, "onReceivedSslError: " + error.toString());
}
});
} }
// show web view
public void showWebPage(String url) {
WebView webView = new WebView(getContext());
setContentView(webView);
webView.loadUrl(url);
show();
}
} }

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>