package chaoran.business.activity; import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.webkit.WebSettings; import android.webkit.WebView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import chaoran.business.BrandEnum; import chaoran.business.R; import chaoran.business.adapter.*; import chaoran.business.vioce.TekVoiceEngine; import chaoran.business.vioce.VoiceEngine; /** * 流程:联网认证设备型号,验证通过,查找设备品牌进行调用驱动操作 */ /** * 提供文字转语音功能,实时播报PDA状态 */ public class MainActivity extends AppCompatActivity implements ResultListener { private WebView webView; private Adapter adapter; private VoiceEngine voiceEngine; @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() { setTitle(R.string.title_activity_main); voiceEngine = new TekVoiceEngine(this); webView = findViewById(R.id.webView); WebSettings settings = webView.getSettings(); settings.setJavaScriptEnabled(true); webView.addJavascriptInterface(voiceEngine, "VoiceEngine"); webView.loadUrl(url()); } @Override protected void onResume() { //再次唤醒该页面时,重新加载页面和语音配置 webView.loadUrl(url()); voiceEngine.loadParam(); 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; case R.id.action_about: startActivity(new Intent(this, AboutActivity.class)); break; } return super.onOptionsItemSelected(item); } private String url() { SharedPreferences spf = this.getSharedPreferences("crtech", Context.MODE_PRIVATE); String address = spf.getString("address", "").replaceAll(" ", ""); String path = spf.getString("path", "").replaceAll(" ", ""); Integer port = spf.getInt("port", -1); String link = address.concat(":").concat(String.valueOf(port)).concat(path); return link.startsWith("http://") ? link : "http://".concat(link); } }