13Poker by gametower unlimited gems v0.4版本的 MD5 值为:51324fc0f195c34821074948ab266846
以下内容为反编译后的 NVASpeechKit.java 源代码,内容仅作参考
package com.millennialmedia.android;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.text.TextUtils;
import com.adsdk.sdk.Const;
import com.flurry.android.Constants;
import com.millennialmedia.android.HandShake;
import com.millennialmedia.android.MMSDK;
import com.nuance.nmdp.speechkit.DataUploadCommand;
import com.nuance.nmdp.speechkit.DataUploadResult;
import com.nuance.nmdp.speechkit.GenericCommand;
import com.nuance.nmdp.speechkit.GenericResult;
import com.nuance.nmdp.speechkit.Recognition;
import com.nuance.nmdp.speechkit.Recognizer;
import com.nuance.nmdp.speechkit.SpeechError;
import com.nuance.nmdp.speechkit.SpeechKit;
import com.nuance.nmdp.speechkit.Vocalizer;
import com.nuance.nmdp.speechkit.recognitionresult.DetailedResult;
import com.nuance.nmdp.speechkit.util.dataupload.Action;
import com.nuance.nmdp.speechkit.util.dataupload.Data;
import com.nuance.nmdp.speechkit.util.dataupload.DataBlock;
import com.nuance.nmdp.speechkit.util.pdx.PdxValue;
import com.xbtsol.jynvrt161024.IConstants;
import com.xbtsol.jynvrt161024.IMraid;
import java.lang.ref.WeakReference;
import java.security.MessageDigest;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class NVASpeechKit {
static final float AUDIO_LEVEL_CHANGE_INTERVAL = 0.25f;
static final float AUDIO_LEVEL_MAX = 90.0f;
private static final int AUDIO_LEVEL_UPDATE_FREQUENCY = 50;
private static final int AUDIO_SAMPLE_PERIOD = 2000;
static final float SAMPLING_BG_INTERVAL = 0.1f;
private static String nuanceIdCache = null;
HandShake.NuanceCredentials _credentials;
private String nuance_transaction_session_id;
private String packageName;
private DataUploadCommand pendingDataUploadCommand;
private CustomWordsOp pendingDataUploadCommandType;
private SpeechKit sk;
private Recognizer skCurrentRecognizer;
private Vocalizer skCurrentVocalizer;
private Recognizer.Listener skRecogListener;
private Vocalizer.Listener skVocalListener;
private Handler speeckKitHandler;
private State state;
private WeakReference<MMWebView> webViewRef;
public Result[] _results = null;
private AudioLevelTracker audioLevelTracker = new AudioLevelTracker();
private Runnable audioSampleCallback = new Runnable() {
@Override
public void run() {
NVASpeechKit.this.endRecording();
}
};
private Runnable audioLevelCallback = new Runnable() {
@Override
public void run() {
if (NVASpeechKit.this.skCurrentRecognizer != null) {
double normalizedLevel = AudioLevelTracker.normalize(NVASpeechKit.this.skCurrentRecognizer.getAudioLevel());
MMSDK.Log.d("audiolevel changed: level=" + normalizedLevel);
if (NVASpeechKit.this.audioLevelTracker.update(normalizedLevel) && NVASpeechKit.this.speechKitListener != null) {
NVASpeechKit.this.speechKitListener.onAudioLevelUpdate(normalizedLevel);
}
if (NVASpeechKit.this.state == State.RECORDING || NVASpeechKit.this.audioLevelTracker.isTrackingAudioSample) {
NVASpeechKit.this.speeckKitHandler.postDelayed(NVASpeechKit.this.audioLevelCallback, 50L);
}
}
}
};
private GenericCommand.Listener commandListener = new GenericCommand.Listener() {
public void onComplete(GenericCommand command, GenericResult result, SpeechError error) {
if (error != null) {
MMSDK.Log.e("GenericCommand listener. Error: " + error.getErrorDetail());
} else {
MMSDK.Log.d("GenericCommand listener. Success: " + result.getQueryResult());
}
NVASpeechKit.this.notifySpeechResults();
}
};
private DataUploadCommand.Listener dataUploadListener = new DataUploadCommand.Listener() {
public void onResults(DataUploadCommand command, DataUploadResult results) {
MMSDK.Log.d("DataUploadCommand listener successful command:" + command.toString() + " isVocRegenerated:" + results.isVocRegenerated() + " results:" + results.toString());
notifyListener(command);
}
public void onError(DataUploadCommand command, SpeechError error) {
MMSDK.Log.e("DataUploadCommand listener error. command:" + command.toString() + " Error:" + error.getErrorDetail());
notifyListener(command);
}
private void notifyListener(DataUploadCommand command) {
if (NVASpeechKit.this.speechKitListener != null && NVASpeechKit.this.pendingDataUploadCommand == command) {
if (NVASpeechKit.this.pendingDataUploadCommandType == CustomWordsOp.Add) {
NVASpeechKit.this.speechKitListener.onCustomWordsAdded();
} else {
NVASpeechKit.this.speechKitListener.onCustomWordsDeleted();
}
}
NVASpeechKit.this.pendingDataUploadCommand = null;
}
};
private Listener speechKitListener = new Listener() {
@Override
public void onStateChange(State state) {
switch (AnonymousClass8.$SwitchMap$com$millennialmedia$android$NVASpeechKit$State[state.ordinal()]) {
case 1:
NVASpeechKit.this.voiceStateChangeError();
return;
case 2:
NVASpeechKit.this.voiceStateChangeProcessing();
return;
case 3:
NVASpeechKit.this.voiceStateChangeReady();
return;
case 4:
NVASpeechKit.this.voiceStateChangeRecording();
return;
case 5:
NVASpeechKit.this.voiceStateChangeVocalizing();
return;
default:
return;
}
}
@Override
public void onResults() {
JSONArray jsonArray = NVASpeechKit.this.resultsToJSON(NVASpeechKit.this.getResults());
NVASpeechKit.this.recognitionResult(jsonArray.toString());
}
@Override
public void onError() {
}
@Override
public void onCustomWordsDeleted() {
}
@Override
public void onCustomWordsAdded() {
}
@Override
public void onAudioSampleUpdate(double averageAudioLevel) {
NVASpeechKit.this.backgroundAudioLevel(averageAudioLevel);
}
@Override
public void onAudioLevelUpdate(double audioLevel) {
NVASpeechKit.this.audioLevelChange(audioLevel);
}
};
public enum CustomWordsOp {
Add,
Remove
}
public interface Listener {
void onAudioLevelUpdate(double d);
void onAudioSampleUpdate(double d);
void onCustomWordsAdded();
void onCustomWordsDeleted();
void onError();
void onResults();
void onStateChange(State state);
}
public enum State {
ERROR(IMraid.EVENT_ERROR),
VOCALIZING("vocalizing"),
RECORDING("recording"),
READY(IMraid.EVENT_READY),
PROCESSING("processing");
private String name;
State(String name) {
this.name = name;
}
@Override
public String toString() {
return this.name;
}
}
public class Result {
public final int resultScore;
public final String resultString;
public Result(String resultString, double resultScore) {
this.resultString = resultString;
this.resultScore = (int) resultScore;
}
public String getResultString() {
return this.resultString;
}
public int getResultScore() {
return this.resultScore;
}
}
public static class AudioLevelTracker {
private static final double MAX = 80.0d;
private static final double MIN = 40.0d;
private static final double NORMALIZE_FACTOR = 4.004004004004004d;
private static final double SCALE = 9.99d;
double audioLevel;
int audioLevelCount;
double averageLevel;
boolean isTrackingAudioSample;
public AudioLevelTracker() {
reset();
}
public void reset() {
this.averageLevel = 0.0d;
this.audioLevelCount = 0;
this.isTrackingAudioSample = false;
}
public void startTrackingAudioSample() {
reset();
this.isTrackingAudioSample = true;
}
public boolean isTrackingAudioSample() {
return this.isTrackingAudioSample;
}
public static double normalize(double level) {
double normalized = Math.floor(level - MIN) / NORMALIZE_FACTOR;
return Math.min((double) SCALE, Math.max(normalized, 0.0d));
}
public boolean update(double level) {
double oldAverage = this.averageLevel;
double oldLevel = this.audioLevel;
this.audioLevel = level;
this.audioLevelCount++;
this.averageLevel = (((this.audioLevelCount - 1) * oldAverage) + level) / this.audioLevelCount;
return (this.isTrackingAudioSample || this.audioLevel == oldLevel) ? false : true;
}
}
public NVASpeechKit(MMWebView webView) {
if (webView != null) {
this.webViewRef = new WeakReference<>(webView);
initInternalData(webView.getContext().getApplicationContext());
}
this.state = State.READY;
}
private MMWebView getMMWebView() {
if (this.webViewRef != null) {
return this.webViewRef.get();
}
return null;
}
private void releaseWebView() {
if (getMMWebView() != null) {
this.webViewRef.clear();
}
}
public JSONArray resultsToJSON(Result[] resultArray) {
JSONArray list = new JSONArray();
for (int i = 0; i < resultArray.length; i++) {
JSONObject object = new JSONObject();
try {
object.put("score", "" + resultArray[i].getResultScore());
object.put("result", resultArray[i].getResultString());
list.put(object);
} catch (JSONException e) {
MMSDK.Log.d("JSON creation error.");
e.printStackTrace();
return null;
}
}
return list;
}
static class AnonymousClass8 {
static final int[] $SwitchMap$com$millennialmedia$android$NVASpeechKit$State = new int[State.values().length];
static {
try {
$SwitchMap$com$millennialmedia$android$NVASpeechKit$State[State.ERROR.ordinal()] = 1;
} catch (NoSuchFieldError e) {
}
try {
$SwitchMap$com$millennialmedia$android$NVASpeechKit$State[State.PROCESSING.ordinal()] = 2;
} catch (NoSuchFieldError e2) {
}
try {
$SwitchMap$com$millennialmedia$android$NVASpeechKit$State[State.READY.ordinal()] = 3;
} catch (NoSuchFieldError e3) {
}
try {
$SwitchMap$com$millennialmedia$android$NVASpeechKit$State[State.RECORDING.ordinal()] = 4;
} catch (NoSuchFieldError e4) {
}
try {
$SwitchMap$com$millennialmedia$android$NVASpeechKit$State[State.VOCALIZING.ordinal()] = 5;
} catch (NoSuchFieldError e5) {
}
}
}
void voiceStateChangeReady() {
MMWebView webView = getMMWebView();
if (webView != null) {
webView.loadUrl("javascript:MMJS.sdk.voiceStateChange('ready')");
}
}
void voiceStateChangeRecording() {
MMWebView webView = getMMWebView();
if (webView != null) {
webView.loadUrl("javascript:MMJS.sdk.voiceStateChange('recording')");
}
}
void voiceStateChangeProcessing() {
MMWebView webView = getMMWebView();
if (webView != null) {
webView.loadUrl("javascript:MMJS.sdk.voiceStateChange('processing')");
}
}
void voiceStateChangeVocalizing() {
MMWebView webView = getMMWebView();
if (webView != null) {
webView.loadUrl("javascript:MMJS.sdk.voiceStateChange('vocalizing')");
}
}
void voiceStateChangeError() {
MMWebView webView = getMMWebView();
if (webView != null) {
webView.loadUrl("javascript:MMJS.sdk.voiceStateChange('error')");
}
}
void audioLevelChange(double audioLevel) {
MMWebView webView = getMMWebView();
if (webView != null) {
webView.loadUrl("javascript:MMJS.sdk.audioLevelChange(" + audioLevel + ")");
}
}
void recognitionResult(String jsonResults) {
MMWebView webView = getMMWebView();
if (webView != null) {
webView.loadUrl("javascript:MMJS.sdk.recognitionResult(" + jsonResults + ")");
}
}
void voiceError(String error) {
MMWebView webView = getMMWebView();
if (webView != null) {
webView.loadUrl("javascript:MMJS.sdk.voiceError('" + error + "')");
}
}
void backgroundAudioLevel(double audioLevel) {
MMWebView webView = getMMWebView();
if (webView != null) {
webView.loadUrl("javascript:MMJS.sdk.backgroundAudioLevel(" + audioLevel + ")");
}
}
public synchronized State getState() {
return this.state;
}
public void setSpeechKitListener(Listener speechKitListener) {
this.speechKitListener = speechKitListener;
}
public boolean startRecording(String languageCode) {
MMSDK.Log.d("RECORDING INVOKED.");
if (this.state != State.READY || this.sk == null) {
return false;
}
this.nuance_transaction_session_id = null;
this.skCurrentRecognizer = this.sk.createRecognizer("dictation", 1, languageCode, this.skRecogListener, this.speeckKitHandler);
MMSDK.Log.d("START RECORDING");
this.skCurrentRecognizer.start();
return true;
}
public boolean endRecording() {
if (this.skCurrentRecognizer != null) {
MMSDK.Log.d("end RECORDING");
this.skCurrentRecognizer.stopRecording();
this.skCurrentRecognizer = null;
return true;
}
return false;
}
public void cancelRecording() {
if (this.skCurrentRecognizer != null) {
MMSDK.Log.d("cancel RECORDING");
this.skCurrentRecognizer.cancel();
this.skCurrentRecognizer = null;
setState(State.READY);
}
}
public void startSampleRecording() {
this.audioLevelTracker.startTrackingAudioSample();
startRecording("en_US");
}
public Result[] getResults() {
return this._results;
}
public void stopActions() {
if (this.sk != null) {
try {
this.sk.cancelCurrent();
} catch (Exception e) {
MMSDK.Log.e("No speech kit to disconnect.", e);
}
}
}
public boolean textToSpeech(String input, String languageCode) {
MMSDK.Log.d("TTS INVOKED.");
if (this.state != State.READY || this.sk == null) {
return false;
}
this.skCurrentVocalizer = this.sk.createVocalizerWithLanguage(languageCode, this.skVocalListener, this.speeckKitHandler);
this.skCurrentVocalizer.speakString(input, this);
return true;
}
public void release() {
MMSDK.Log.d("release called.");
stopActions();
cancelAudioLevelCallbacks();
if (this.sk != null) {
this.sk.release();
setState(State.READY);
this.sk = null;
}
this.pendingDataUploadCommand = null;
releaseWebView();
}
public void logEvent() {
if (this.sk != null) {
PdxValue.Dictionary content = new PdxValue.Dictionary();
content.put("nva_ad_network_id", "MillenialMedia");
content.put("nva_device_id", getNuanceId());
content.put("nva_ad_publisher_id", this.packageName);
String mmSessionId = "";
if (this._credentials != null && !TextUtils.isEmpty(this._credentials.sessionID)) {
mmSessionId = this._credentials.sessionID;
content.put("nva_ad_session_id", this._credentials.sessionID);
}
String adId = getAdId();
if (!TextUtils.isEmpty(adId)) {
content.put("nva_ad_id", adId);
}
if (this.nuance_transaction_session_id != null) {
content.put("nva_nvc_session_id", this.nuance_transaction_session_id);
String str = this.nuance_transaction_session_id;
this.nuance_transaction_session_id = null;
} else {
this.sk.getSessionId();
}
MMSDK.Log.d("Sending log revision command to server. sessionId[" + this.sk.getSessionId() + "] deviceId[" + getNuanceId() + "] adId[" + adId + "] mmSessionId[" + mmSessionId + "]");
GenericCommand command = this.sk.createLogRevisionCmd("NVA_LOG_EVENT", content, this.sk.getSessionId(), this.commandListener, this.speeckKitHandler);
command.start();
}
}
private String getAdId() {
MMWebView webView;
return (this.webViewRef == null || (webView = this.webViewRef.get()) == null) ? "DEFAULT_AD_ID" : webView.getAdId();
}
private String byte2Str(byte[] byteArray) {
if (byteArray == null) {
return null;
}
StringBuffer hexString = new StringBuffer();
for (byte b : byteArray) {
int v = b & Constants.UNKNOWN;
String hex = Integer.toHexString(v);
if (v < 16) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
private byte[] string2Byte(String string) {
if (string == null) {
return null;
}
byte[] bytes = new byte[string.length() / 2];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt(string.substring(i * 2, (i * 2) + 2), 16);
}
return bytes;
}
public boolean initialize(HandShake.NuanceCredentials credentials, Context context) {
MMSDK.Log.d("initialize called.");
if (credentials == null || context == null) {
return false;
}
this._credentials = credentials;
if (this.sk != null) {
try {
this.sk.connect();
} catch (IllegalStateException e) {
this.sk = null;
}
}
if (this.sk == null) {
byte[] appKeyBytes = string2Byte(credentials.appKey);
MMSDK.Log.d(credentials.toString());
this.sk = SpeechKit.initialize(context, Const.PROTOCOL_VERSION, credentials.appID, credentials.server, credentials.port, false, appKeyBytes, SpeechKit.CmdSetType.NVC);
this.skVocalListener = createVocalizerListener();
this.skRecogListener = createRecognizerListener();
this.speeckKitHandler = new Handler(Looper.getMainLooper());
this.sk.connect();
setState(State.READY);
return true;
}
MMSDK.Log.d("Already initialized. Skipping.");
return false;
}
public String getSessionId() {
return this.sk != null ? this.sk.getSessionId() : "";
}
public void updateCustomWords(CustomWordsOp op, String[] words) {
if (this.sk != null) {
DataBlock datablock = new DataBlock();
MMSDK.Log.d("Creating dataupload command and " + (op == CustomWordsOp.Add ? "adding" : "deleting") + " words.");
Data data = new Data("nva_custom_word_uploads", Data.DataType.CUSTOMWORDS);
Action addAction = new Action(op == CustomWordsOp.Add ? Action.ActionType.ADD : Action.ActionType.REMOVE);
for (String word : words) {
addAction.addWord(word);
MMSDK.Log.d("\tword: '" + word + "'");
}
data.addAction(addAction);
datablock.addData(data);
int checksum = datablock.getChecksum();
this.pendingDataUploadCommandType = op;
this.pendingDataUploadCommand = this.sk.createDataUploadCmd(datablock, checksum, checksum, this.dataUploadListener, this.speeckKitHandler);
this.pendingDataUploadCommand.start();
}
}
public void processResults(List<DetailedResult> inputResults) {
MMSDK.Log.d("processResults called.");
this._results = new Result[inputResults.size()];
int i = 0;
for (DetailedResult r : inputResults) {
this._results[i] = new Result(r.toString(), r.getConfidenceScore());
i++;
}
}
private Vocalizer.Listener createVocalizerListener() {
return new Vocalizer.Listener() {
public void onSpeakingBegin(Vocalizer vocalizer, String text, Object context) {
MMSDK.Log.d("Vocalization begins. text=" + text);
NVASpeechKit.this.setState(State.VOCALIZING);
}
public void onSpeakingDone(Vocalizer vocalizer, String text, SpeechError error, Object context) {
MMSDK.Log.d("Vocalization has ended.");
if (error == null) {
NVASpeechKit.this.setState(State.READY);
return;
}
MMSDK.Log.e("Vocalizer error: " + error.getErrorDetail());
NVASpeechKit.this.handleSpeechError(error);
}
};
}
private String getSpeechError(SpeechError error) {
return error == null ? "No Error given" : "Speech Kit Error code:" + error.getErrorCode() + " detail:" + error.getErrorDetail() + " suggestions:" + error.getSuggestion();
}
private Recognizer.Listener createRecognizerListener() {
return new Recognizer.Listener() {
public void onRecordingBegin(Recognizer recognizer) {
MMSDK.Log.d("recording begins");
NVASpeechKit.this._results = null;
if (!NVASpeechKit.this.audioLevelTracker.isTrackingAudioSample()) {
NVASpeechKit.this.setState(State.RECORDING);
}
NVASpeechKit.this.startProgress(recognizer);
if (NVASpeechKit.this.audioLevelTracker.isTrackingAudioSample()) {
NVASpeechKit.this.speeckKitHandler.removeCallbacks(NVASpeechKit.this.audioSampleCallback);
NVASpeechKit.this.speeckKitHandler.postDelayed(NVASpeechKit.this.audioSampleCallback, 2000L);
}
}
public void onRecordingDone(Recognizer recognizer) {
MMSDK.Log.d("recording has ended");
NVASpeechKit.this.cancelAudioLevelCallbacks();
if (!NVASpeechKit.this.audioLevelTracker.isTrackingAudioSample()) {
NVASpeechKit.this.setState(State.PROCESSING);
}
if (NVASpeechKit.this.sk != null) {
NVASpeechKit.this.nuance_transaction_session_id = NVASpeechKit.this.sk.getSessionId();
}
}
public void onError(Recognizer recognizer, SpeechError error) {
MMSDK.Log.d("Speech Kit Error code:" + error.getErrorCode() + " detail:" + error.getErrorDetail() + " suggestions:" + error.getSuggestion());
NVASpeechKit.this.cancelAudioLevelCallbacks();
NVASpeechKit.this.handleSpeechError(error);
NVASpeechKit.this.skCurrentRecognizer = null;
if (NVASpeechKit.this.sk != null) {
MMSDK.Log.d("Recognizer.Listener.onError: session id [" + NVASpeechKit.this.sk.getSessionId() + "]");
}
}
public void onResults(Recognizer recognizer, Recognition results) {
MMSDK.Log.d("recording results returned.");
NVASpeechKit.this.cancelAudioLevelCallbacks();
if (!NVASpeechKit.this.audioLevelTracker.isTrackingAudioSample) {
NVASpeechKit.this.processResults(results.getDetailedResults());
if (NVASpeechKit.this.nuance_transaction_session_id != null) {
MMSDK.Log.d("Recognizer.Listener.onResults: session id [" + NVASpeechKit.this.nuance_transaction_session_id + "]");
}
NVASpeechKit.this.logEvent();
return;
}
NVASpeechKit.this._results = new Result[0];
NVASpeechKit.this.notifySpeechResults();
}
};
}
synchronized String getNuanceId() {
String mmdid;
MMWebView webView;
String nuanceId = null;
synchronized (this) {
if (nuanceIdCache != null) {
nuanceId = nuanceIdCache;
} else {
Context context = null;
if (this.webViewRef != null && (webView = this.webViewRef.get()) != null) {
context = webView.getContext();
}
if (context != null && (mmdid = Settings.Secure.getString(context.getContentResolver(), IConstants.ANDROID_ID)) != null) {
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
byte[] hashBytes = md.digest(mmdid.getBytes());
nuanceId = MMSDK.byteArrayToString(hashBytes);
nuanceIdCache = nuanceId;
} catch (Exception e) {
MMSDK.Log.v(e.getMessage());
}
}
}
}
return nuanceId;
}
private void initInternalData(Context context) {
if (this.packageName == null) {
this.packageName = context.getApplicationContext().getPackageName();
}
}
public void handleSpeechError(SpeechError error) {
switch (error.getErrorCode()) {
case 2:
if (!this.audioLevelTracker.isTrackingAudioSample) {
setState(State.PROCESSING);
}
this._results = new Result[0];
notifySpeechResults();
return;
case 3:
case 4:
default:
if (this.speechKitListener != null) {
this.speechKitListener.onError();
setState(State.ERROR);
voiceError(getSpeechError(error));
return;
}
return;
case 5:
setState(State.READY);
this.skCurrentRecognizer = null;
return;
}
}
public void notifySpeechResults() {
if (this.speechKitListener != null && this._results != null) {
if (this.audioLevelTracker.isTrackingAudioSample) {
this.speechKitListener.onAudioSampleUpdate(this.audioLevelTracker.averageLevel);
this.audioLevelTracker.reset();
} else {
this.speechKitListener.onResults();
}
}
setState(State.READY);
this.skCurrentRecognizer = null;
}
public void startProgress(Recognizer recognizer) {
this.speeckKitHandler.removeCallbacks(this.audioLevelCallback);
this.speeckKitHandler.postDelayed(this.audioLevelCallback, 50L);
}
public synchronized void setState(State state) {
MMSDK.Log.d("recording results returned. state=" + state);
State old_state = this.state;
this.state = state;
if (this.speechKitListener != null && this.state != old_state) {
this.speechKitListener.onStateChange(state);
}
}
public void cancelAudioLevelCallbacks() {
if (this.speeckKitHandler != null) {
this.speeckKitHandler.removeCallbacks(this.audioSampleCallback);
this.speeckKitHandler.removeCallbacks(this.audioLevelCallback);
}
}
}