Add VasSonic gradle plugin as a dependency in your module's build.gradle
compile 'com.tencent.sonic:sdk:1.1.0'
- Implement a class which extends from
SonicRuntime
SonicRuntime is a class which interacts with the overall running information in the system, including Context, UA, ID (which is the unique identification for the saved data) and other information.
/**
* Here is a sample subclass of SonicRuntime
*/
public class HostSonicRuntime extends SonicRuntime {
public HostSonicRuntime(Context context) {
super(context);
}
/**
* @return User's UA
*/
@Override
public String getUserAgent() {
return "";
}
/**
* @return the ID of user.
*/
@Override
public String getCurrentUserAccount() {
return "";
}
/**
* @return the file path which is used to save Sonic caches.
*/
@Override
public File getSonicCacheDir() {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "sonic/";
File file = new File(path.trim());
if(!file.exists()){
file.mkdir();
}
return file;
}
}
- Implement a subclass which extends from
SonicSessionClient
/**
*
* SonicSessionClient is a thin API class that delegates its public API to a backend WebView class instance, such as loadUrl and loadDataWithBaseUrl.
*/
public class SonicSessionClientImpl extends SonicSessionClient {
private WebView webView;
public void bindWebView(WebView webView) {
this.webView = webView;
}
@Override
public void loadUrl(String url, Bundle extraData) {
webView.loadUrl(url);
}
@Override
public void loadDataWithBaseUrl(String baseUrl, String data, String mimeType, String encoding,
String historyUrl) {
webView.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);
}
}
Here is a simple demo shows how to create an Android activity which uses the VasSonic Framework
public class BrowserActivity extends Activity {
public final static String PARAM_URL = "param_url";
public final static String PARAM_MODE = "param_mode";
private SonicSession sonicSession;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String url = intent.getStringExtra(PARAM_URL);
int mode = intent.getIntExtra(PARAM_MODE, -1);
if (TextUtils.isEmpty(url) || -1 == mode) {
finish();
return;
}
getWindow().addFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
// step 1: Initialize sonic engine if necessary, or maybe u can do this when application created
if (!SonicEngine.isGetInstanceAllowed()) {
SonicEngine.createInstance(new SonicRuntimeImpl(getApplication()), new SonicConfig.Builder().build());
}
SonicSessionClientImpl sonicSessionClient = null;
// step 2: Create SonicSession
sonicSession = SonicEngine.getInstance().createSession(url, new SonicSessionConfig.Builder().build());
if (null != sonicSession) {
sonicSession.bindClient(sonicSessionClient = new SonicSessionClientImpl());
} else {
// this only happen when a same sonic session is already running,
// u can comment following codes to feedback as a default mode.
throw new UnknownError("create session fail!");
}
// step 3: BindWebView for sessionClient and bindClient for SonicSession
// in the real world, the init flow may cost a long time as startup
// runtime、init configs....
setContentView(R.layout.activity_browser);
WebView webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (sonicSession != null) {
sonicSession.getSessionClient().pageFinish(url);
}
}
@TargetApi(21)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
return shouldInterceptRequest(view, request.getUrl().toString());
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if (sonicSession != null) {
//step 6: Call sessionClient.requestResource when host allow the application
// to return the local data .
return (WebResourceResponse) sonicSession.getSessionClient().requestResource(url);
}
return null;
}
});
WebSettings webSettings = webView.getSettings();
// step 4: bind javascript
// note:if api level lower than 17(android 4.2), addJavascriptInterface has security
// issue, please use x5 or see https://developer.android.com/reference/android/webkit/
// WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String)
webSettings.setJavaScriptEnabled(true);
webView.removeJavascriptInterface("searchBoxJavaBridge_");
intent.putExtra(SonicJavaScriptInterface.PARAM_LOAD_URL_TIME, System.currentTimeMillis());
webView.addJavascriptInterface(new SonicJavaScriptInterface(sonicSessionClient, intent), "sonic");
// init webview settings
webSettings.setAllowContentAccess(true);
webSettings.setDatabaseEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setSavePassword(false);
webSettings.setSaveFormData(false);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
// step 5: webview is ready now, just tell session client to bind
if (sonicSessionClient != null) {
sonicSessionClient.bindWebView(webView);
sonicSessionClient.clientReady();
} else { // default mode
webView.loadUrl(url);
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
@Override
protected void onDestroy() {
if (null != sonicSession) {
sonicSession.destroy();
sonicSession = null;
}
super.onDestroy();
}
Any problem?
- Learn more from sample.
- Contact us for help.
VasSonic is under the BSD license. See the LICENSE file for details.