2021-02-08 10:34:29 +08:00
|
|
|
|
package chaoran.business.activity;
|
|
|
|
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
|
import android.content.SharedPreferences;
|
2021-02-09 17:31:40 +08:00
|
|
|
|
import android.graphics.Bitmap;
|
2021-02-08 10:34:29 +08:00
|
|
|
|
import android.os.Build;
|
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
|
import android.view.Menu;
|
|
|
|
|
|
import android.view.MenuItem;
|
2021-02-09 17:31:40 +08:00
|
|
|
|
import android.view.View;
|
|
|
|
|
|
import android.webkit.*;
|
|
|
|
|
|
import android.widget.ProgressBar;
|
2021-02-08 10:34:29 +08:00
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
|
import androidx.annotation.Nullable;
|
2021-02-19 14:09:07 +08:00
|
|
|
|
import androidx.appcompat.app.ActionBar;
|
2021-02-08 10:34:29 +08:00
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
|
import chaoran.business.BrandEnum;
|
|
|
|
|
|
import chaoran.business.R;
|
|
|
|
|
|
import chaoran.business.adapter.*;
|
2021-02-20 13:58:47 +08:00
|
|
|
|
import chaoran.business.engine.impl.NetworkSettingEngine;
|
2021-02-19 14:09:07 +08:00
|
|
|
|
import chaoran.business.engine.SettingEngine;
|
2021-02-20 13:58:47 +08:00
|
|
|
|
import chaoran.business.engine.impl.TekVoiceEngine;
|
2021-02-19 14:09:07 +08:00
|
|
|
|
import chaoran.business.engine.VoiceEngine;
|
2021-02-08 10:34:29 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 流程:联网认证设备型号,验证通过,查找设备品牌进行调用驱动操作
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 提供文字转语音功能,实时播报PDA状态
|
|
|
|
|
|
*/
|
|
|
|
|
|
public class MainActivity extends AppCompatActivity implements ResultListener {
|
|
|
|
|
|
|
|
|
|
|
|
private WebView webView;
|
|
|
|
|
|
private Adapter adapter;
|
|
|
|
|
|
private VoiceEngine voiceEngine;
|
2021-02-19 14:09:07 +08:00
|
|
|
|
private SettingEngine settingEngine;
|
2021-02-09 17:31:40 +08:00
|
|
|
|
private ProgressBar progressBar;
|
2021-02-19 14:09:07 +08:00
|
|
|
|
private ActionBar actionBar;
|
|
|
|
|
|
|
2021-02-08 10:34:29 +08:00
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
setContentView(R.layout.activity_main);
|
|
|
|
|
|
initView();
|
|
|
|
|
|
initData();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void initData() {
|
|
|
|
|
|
BrandEnum brand = BrandEnum.code(Build.MANUFACTURER);
|
|
|
|
|
|
Toast.makeText(this, Build.MANUFACTURER, Toast.LENGTH_LONG).show();
|
|
|
|
|
|
switch (brand) {
|
|
|
|
|
|
case UROBO:
|
|
|
|
|
|
adapter = new UroBoAdapter(this, this);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case ROCKCHIP:
|
|
|
|
|
|
adapter = new RockChipAdapter(this, this);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case IDATA:
|
|
|
|
|
|
adapter = new IDataAdapter(this, this);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case ALPS:
|
|
|
|
|
|
adapter = new AlpsAdapter(this, this);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (null != adapter) {
|
|
|
|
|
|
adapter.start();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressLint("JavascriptInterface")
|
|
|
|
|
|
private void initView() {
|
2021-02-19 14:09:07 +08:00
|
|
|
|
actionBar = getSupportActionBar();
|
2021-02-08 10:34:29 +08:00
|
|
|
|
voiceEngine = new TekVoiceEngine(this);
|
2021-02-19 14:09:07 +08:00
|
|
|
|
settingEngine = new NetworkSettingEngine(this);
|
2021-02-08 10:34:29 +08:00
|
|
|
|
webView = findViewById(R.id.webView);
|
2021-02-09 17:31:40 +08:00
|
|
|
|
progressBar = findViewById(R.id.loading);
|
|
|
|
|
|
webView.setWebViewClient(disposeView());
|
2021-02-08 10:34:29 +08:00
|
|
|
|
WebSettings settings = webView.getSettings();
|
|
|
|
|
|
settings.setJavaScriptEnabled(true);
|
2021-02-19 14:09:07 +08:00
|
|
|
|
//设置接口进行windows暴露
|
2021-02-09 17:31:40 +08:00
|
|
|
|
settings.setDomStorageEnabled(true);
|
2021-02-19 14:09:07 +08:00
|
|
|
|
//语音引擎
|
|
|
|
|
|
webView.addJavascriptInterface(voiceEngine, "TekVoiceEngine");
|
|
|
|
|
|
//网络设置展示
|
|
|
|
|
|
webView.addJavascriptInterface(settingEngine, "NetworkSettingEngine");
|
|
|
|
|
|
//重新加载页面
|
|
|
|
|
|
webView.addJavascriptInterface(this, "View");
|
2021-02-08 10:34:29 +08:00
|
|
|
|
webView.loadUrl(url());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-09 17:31:40 +08:00
|
|
|
|
//配置客户端
|
|
|
|
|
|
private WebViewClient disposeView() {
|
|
|
|
|
|
return new WebViewClient() {
|
|
|
|
|
|
//页面开始加载时
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void onPageStarted(WebView view, String url, Bitmap favicon) {
|
|
|
|
|
|
super.onPageStarted(view, url, favicon);
|
|
|
|
|
|
progressBar.setVisibility(View.VISIBLE);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//页面加载完成时
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void onPageFinished(WebView view, String url) {
|
2021-02-19 14:09:07 +08:00
|
|
|
|
if (!url.startsWith("file"))
|
|
|
|
|
|
actionBar.hide();
|
|
|
|
|
|
else
|
|
|
|
|
|
actionBar.show();
|
2021-02-09 17:31:40 +08:00
|
|
|
|
super.onPageFinished(view, url);
|
|
|
|
|
|
progressBar.setVisibility(View.INVISIBLE);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//网络发生错误时,先展示错误界面,然后关闭加载条
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
|
|
|
|
|
|
switch (errorCode) {
|
|
|
|
|
|
case 404:
|
|
|
|
|
|
webView.loadUrl("file:///android_asset/error/404.html");
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 500:
|
|
|
|
|
|
webView.loadUrl("file:///android_asset/error/500.html");
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
webView.loadUrl("file:///android_asset/error/index.html");
|
|
|
|
|
|
}
|
2021-02-19 14:09:07 +08:00
|
|
|
|
actionBar.show();
|
|
|
|
|
|
actionBar.setTitle(R.string.title_activity_main);
|
2021-02-09 17:31:40 +08:00
|
|
|
|
super.onReceivedError(view, errorCode, description, failingUrl);
|
|
|
|
|
|
progressBar.setVisibility(View.INVISIBLE);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//安卓6.0以上发生错误时回调
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
|
|
|
switch (error.getErrorCode()) {
|
|
|
|
|
|
case 404:
|
|
|
|
|
|
webView.loadUrl("file:///android_asset/error/404.html");
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 500:
|
|
|
|
|
|
webView.loadUrl("file:///android_asset/error/500.html");
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
webView.loadUrl("file:///android_asset/error/index.html");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-02-19 14:09:07 +08:00
|
|
|
|
actionBar.show();
|
|
|
|
|
|
actionBar.setTitle(R.string.title_activity_main);
|
2021-02-09 17:31:40 +08:00
|
|
|
|
super.onReceivedError(view, request, error);
|
|
|
|
|
|
progressBar.setVisibility(View.INVISIBLE);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-08 10:34:29 +08:00
|
|
|
|
@Override
|
|
|
|
|
|
protected void onResume() {
|
|
|
|
|
|
//再次唤醒该页面时,重新加载页面和语音配置
|
|
|
|
|
|
webView.loadUrl(url());
|
2021-02-20 13:58:47 +08:00
|
|
|
|
voiceEngine.reload();
|
2021-02-08 10:34:29 +08:00
|
|
|
|
super.onResume();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
@SuppressLint("SetJavaScriptEnabled")
|
|
|
|
|
|
public void result(String result) {
|
|
|
|
|
|
runOnUiThread(() -> webView.loadUrl("javascript:render(\'" + result + "\')"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
protected void onDestroy() {
|
|
|
|
|
|
adapter.stop();
|
|
|
|
|
|
super.onDestroy();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
|
|
getMenuInflater().inflate(R.menu.menu_main, menu);
|
|
|
|
|
|
return super.onCreateOptionsMenu(menu);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
|
|
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
|
|
case R.id.action_setting_network:
|
|
|
|
|
|
startActivity(new Intent(this, NetworkSettingActivity.class));
|
|
|
|
|
|
break;
|
|
|
|
|
|
case R.id.action_setting_voice:
|
|
|
|
|
|
startActivity(new Intent(this, VoiceSettingActivity.class));
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-19 14:09:07 +08:00
|
|
|
|
//拼接url
|
2021-02-08 10:34:29 +08:00
|
|
|
|
private String url() {
|
|
|
|
|
|
SharedPreferences spf = this.getSharedPreferences("crtech", Context.MODE_PRIVATE);
|
2021-02-09 17:31:40 +08:00
|
|
|
|
Integer port = spf.getInt("port", -1);
|
|
|
|
|
|
if (port == -1) {
|
|
|
|
|
|
return "file:///android_asset/demo/index.html";
|
|
|
|
|
|
}
|
2021-02-08 10:34:29 +08:00
|
|
|
|
String address = spf.getString("address", "").replaceAll(" ", "");
|
|
|
|
|
|
String path = spf.getString("path", "").replaceAll(" ", "");
|
|
|
|
|
|
String link = address.concat(":").concat(String.valueOf(port)).concat(path);
|
|
|
|
|
|
return link.startsWith("http://") ? link : "http://".concat(link);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-19 14:09:07 +08:00
|
|
|
|
@JavascriptInterface
|
|
|
|
|
|
public void reload() {
|
|
|
|
|
|
runOnUiThread(() -> {
|
|
|
|
|
|
webView.loadUrl(url());
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2021-02-08 10:34:29 +08:00
|
|
|
|
}
|