CEEX v1.0.1版本的 MD5 值为:d20246587215ec3471b0d051724c6d34
以下内容为反编译后的 WebBinder.java 源代码,内容仅作参考
package com.zopim.android.sdk.api;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.webkit.WebView;
import androidx.annotation.NonNull;
import com.zendesk.logger.Logger;
import com.zendesk.util.StringUtils;
import com.zopim.android.sdk.attachment.FileExtension;
import com.zopim.android.sdk.breadcrumbs.Event;
import com.zopim.android.sdk.breadcrumbs.Events;
import com.zopim.android.sdk.data.LivechatChatLogPath;
import com.zopim.android.sdk.data.WebWidgetListener;
import com.zopim.android.sdk.model.ChatLog;
import com.zopim.android.sdk.util.AppInfo;
import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
public final class WebBinder extends ChatCommunication {
private static final String BASE_URL = "https://dashboard.zopim.com/bin/";
private static final String BRIDGE = "jsinterface";
private static final boolean DEBUG = false;
private static final String LOG_TAG = "WebBinder";
private static final long TEARDOWN_TIMEOUT = TimeUnit.SECONDS.toMillis(1);
private static final String WIDGET_VERSION = "mobile_sdk.js";
Handler mHandler = new Handler(Looper.getMainLooper());
private String mUserAgent;
private WebView mWebView;
static class AnonymousClass4 {
static final int[] $SwitchMap$com$zopim$android$sdk$model$ChatLog$Rating;
static {
int[] iArr = new int[ChatLog.Rating.values().length];
$SwitchMap$com$zopim$android$sdk$model$ChatLog$Rating = iArr;
try {
iArr[ChatLog.Rating.GOOD.ordinal()] = 1;
} catch (NoSuchFieldError unused) {
}
try {
$SwitchMap$com$zopim$android$sdk$model$ChatLog$Rating[ChatLog.Rating.BAD.ordinal()] = 2;
} catch (NoSuchFieldError unused2) {
}
try {
$SwitchMap$com$zopim$android$sdk$model$ChatLog$Rating[ChatLog.Rating.UNRATED.ordinal()] = 3;
} catch (NoSuchFieldError unused3) {
}
}
}
class TeardownRunnable implements Runnable {
private final WebView webView;
TeardownRunnable(WebView webView) {
this.webView = webView;
}
@Override
public void run() {
WebView webView = this.webView;
if (webView == null) {
return;
}
webView.stopLoading();
this.webView.destroy();
}
}
private WebBinder() {
}
private boolean isValid(File file) {
if (file != null && file.getName() != null && !file.getName().isEmpty()) {
if (file.isDirectory()) {
Logger.w(LOG_TAG, "Directory is not supported", new Object[0]);
return false;
} else if (file.exists()) {
return true;
} else {
Logger.w(LOG_TAG, "File can not be found or you don't have permissions to access it", new Object[0]);
return false;
}
}
Logger.w(LOG_TAG, "File can not be null or empty", new Object[0]);
return false;
}
@TargetApi(19)
private synchronized void loadUrl(final String str) {
this.mHandler.post(new Runnable() {
@Override
public void run() {
if (WebBinder.this.mWebView != null) {
if (Build.VERSION.SDK_INT >= 19) {
WebBinder.this.mWebView.evaluateJavascript(str, null);
return;
} else {
WebBinder.this.mWebView.loadUrl(str);
return;
}
}
Logger.w(WebBinder.LOG_TAG, "Can't run the web function, web view is null. WebBinder should be initialized.", new Object[0]);
}
});
}
private void prepareAttachmentUpload(@NonNull File file) {
if (isValid(file)) {
String add = FileTransfers.INSTANCE.add(file);
if (add != null && !add.isEmpty()) {
String escapeEcmaScript = StringUtils.escapeEcmaScript(add);
loadUrl(String.format(Locale.US, "javascript:__z_sdk.sendFile('%s', '%s', '%s');", escapeEcmaScript, StringUtils.escapeEcmaScript("." + FileExtension.getExtension(file).getValue()), String.valueOf(file.length())));
return;
}
Logger.w(LOG_TAG, "File name is invalid. Will not prepare attachment upload.", new Object[0]);
}
}
@Override
public void addNote(String str) {
if (str == null) {
Logger.w(LOG_TAG, "Note must not be null. Will not add the note.", new Object[0]);
return;
}
loadUrl(String.format(Locale.US, "javascript:$zopim.livechat.appendNotes('%s');", StringUtils.escapeEcmaScript(str)));
}
@Override
public void addTags(String... strArr) {
if (strArr == null) {
Logger.w(LOG_TAG, "Tags must not be null. Will not set tags.", new Object[0]);
return;
}
String[] strArr2 = new String[strArr.length];
for (int i = 0; i < strArr.length; i++) {
strArr2[i] = StringUtils.escapeEcmaScript(strArr[i]);
}
String arrays = Arrays.toString(strArr2);
loadUrl(String.format(Locale.US, "javascript:$zopim.livechat.addTags('%s');", arrays.substring(1, arrays.length() - 1)));
}
@Override
public void clearPushToken() {
loadUrl("javascript:__z_sdk.setPushToken('');");
}
@Override
public void disconnect() {
this.mHandler.post(new Runnable() {
@Override
public void run() {
if (WebBinder.this.mWebView != null) {
WebBinder.this.mWebView.removeJavascriptInterface("JSInterface");
WebBinder.this.mWebView.setWebViewClient(null);
WebBinder.this.mWebView.destroy();
WebBinder.this.mWebView = null;
}
}
});
}
@Override
public boolean emailTranscript(String str) {
if (str != null && !str.isEmpty()) {
loadUrl(String.format(Locale.US, "javascript:__z_sdk.sendEmailTranscript('%s');", str));
return true;
}
Logger.w(LOG_TAG, "Email address must not be null or empty. Will not email transcript.", new Object[0]);
return false;
}
@Override
public void endChat() {
loadUrl("javascript:$zopim.livechat.endChat();");
loadUrl("javascript:__z_sdk.sendDisconnectTimeout(1);");
this.mHandler.postDelayed(new TeardownRunnable(this.mWebView), TEARDOWN_TIMEOUT);
this.mHandler.post(new Runnable() {
@Override
public void run() {
WebBinder.this.mWebView = null;
}
});
}
@Override
public void init(String str, String str2, String str3, String str4) {
if (str2 != null) {
Logger.v(LOG_TAG, "Reconnecting to previous chat id: " + str2, new Object[0]);
}
if (str2 == null) {
str2 = "";
}
init(str, BRIDGE, str2, this.mUserAgent, str3, "", str4);
}
@Override
public void keepAlive() {
loadUrl("javascript:__z_sdk.sendActive();");
}
@Override
void removeTags(String... strArr) {
LinkedList linkedList = new LinkedList();
for (String str : strArr) {
linkedList.add(StringUtils.escapeEcmaScript(str));
}
String arrays = Arrays.toString(strArr);
loadUrl(String.format(Locale.US, "javascript:$zopim.livechat.removeTags('%s');", arrays.substring(1, arrays.length() - 1)));
}
@Override
public void resend(String str) {
if (str != null && !str.isEmpty()) {
ChatLog chatLog = LivechatChatLogPath.getInstance().getData().get(str);
if (chatLog == null) {
Logger.i(LOG_TAG, "Could not resend the message. No message with message id = " + str, new Object[0]);
}
chatLog.setFailed(false);
loadUrl(String.format(Locale.US, "javascript:__z_sdk.sendChatMsg('%s', '%s');", StringUtils.escapeEcmaScript(chatLog.getMessage()), StringUtils.escapeEcmaScript(str)));
return;
}
Logger.w(LOG_TAG, "Message ID must not be null or empty. Will not resend message.", new Object[0]);
}
@Override
public void send(String str) {
if (str == null) {
Logger.w(LOG_TAG, "Message must not be null. Will not send message.", new Object[0]);
return;
}
loadUrl(String.format(Locale.US, "javascript:__z_sdk.sendChatMsg('%s');", StringUtils.escapeEcmaScript(str)));
}
@Override
public void sendChatButtonClicked() {
loadUrl("javascript:__z_sdk.sendButtonClicked();");
}
@Override
public void sendChatComment(@NonNull String str) {
if (str == null) {
Logger.w(LOG_TAG, "Comment must not be null. Will not comment on this chat.", new Object[0]);
return;
}
loadUrl(String.format(Locale.US, "javascript:__z_sdk.sendChatComment('%s');", StringUtils.escapeEcmaScript(str)));
}
@Override
public void sendChatRating(@NonNull ChatLog.Rating rating) {
if (rating == null) {
Logger.w(LOG_TAG, "Rating must not be null. Will not rate this chat.", new Object[0]);
return;
}
int i = AnonymousClass4.$SwitchMap$com$zopim$android$sdk$model$ChatLog$Rating[rating.ordinal()];
if (i == 1 || i == 2) {
loadUrl(String.format(Locale.US, "javascript:__z_sdk.sendChatRating('%s');", StringUtils.escapeEcmaScript(rating.getValue())));
} else if (i != 3) {
Logger.v(LOG_TAG, "Unknown rating " + rating + " will not be sent", new Object[0]);
} else {
loadUrl(String.format(Locale.US, "javascript:__z_sdk.sendChatRating(null);", new Object[0]));
}
}
@Override
public void sendEvents(Event... eventArr) {
if (eventArr != null && eventArr.length != 0) {
for (int i = 0; i < eventArr.length; i++) {
if (i > 0) {
while (eventArr[i].equals(eventArr[i - 1])) {
eventArr[i] = new Event(eventArr[i].getTitle(), eventArr[i].getTimestamp() + 1);
}
}
}
if (eventArr.length == 1) {
sendVisitorPath(eventArr[0]);
} else if (eventArr.length > 1) {
sendVisitorPaths(eventArr);
}
for (Event event : eventArr) {
Events.getQueue().offer(event);
}
return;
}
Logger.w(LOG_TAG, "Events must not be null or empty", new Object[0]);
}
@Override
public boolean sendOfflineMessage(String str, String str2, String str3) {
if (str2 != null && !str2.isEmpty()) {
if (str3 != null && !str3.isEmpty()) {
if (str == null) {
str = "";
}
loadUrl(String.format(Locale.US, "javascript:__z_sdk.sendOfflineMsg('%s', '%s', '%s');", StringUtils.escapeEcmaScript(str), StringUtils.escapeEcmaScript(str2), StringUtils.escapeEcmaScript(str3)));
return true;
}
Logger.w(LOG_TAG, "Message must not be null or empty. Will not send email.", new Object[0]);
return false;
}
Logger.w(LOG_TAG, "Email address must not be null or empty. Will not send email.", new Object[0]);
return false;
}
@Override
void sendVisitorPath(Event event) {
if (event == null) {
Logger.w(LOG_TAG, "Visitor path must not be null", new Object[0]);
return;
}
WebView webView = this.mWebView;
Context context = webView != null ? webView.getContext() : null;
Locale locale = Locale.US;
loadUrl(String.format(locale, "javascript:__z_sdk.sendVisitorPath(%s);", String.format(locale, "{title:\"%s\", url:\"%s://%d\"}", event.getTitle(), AppInfo.getApplicationName(context).replaceAll("\\s+", ""), Long.valueOf(event.getTimestamp()))));
}
@Override
void sendVisitorPaths(Event[] eventArr) {
if (eventArr != null && eventArr.length != 0) {
WebView webView = this.mWebView;
Context context = webView != null ? webView.getContext() : null;
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < eventArr.length; i++) {
if (eventArr[i] != null) {
sb.append(String.format(Locale.US, "{title:\"%s\", url:\"%s://%d\"}", eventArr[i].getTitle(), AppInfo.getApplicationName(context).replaceAll("\\s+", ""), Long.valueOf(eventArr[i].getTimestamp())));
if (i < eventArr.length - 1) {
sb.append(", ");
}
}
}
sb.append("]");
loadUrl(String.format(Locale.US, "javascript:__z_sdk.sendVisitorPaths(%s);", sb.toString()));
return;
}
Logger.w(LOG_TAG, "Visitor paths must not be null or zero size", new Object[0]);
}
@Override
public void setDepartment(String str) {
if (str == null) {
Logger.w(LOG_TAG, "Department must not be null. Will not set department.", new Object[0]);
return;
}
loadUrl(String.format(Locale.US, "javascript:$zopim.livechat.departments.setVisitorDepartment('%s');", StringUtils.escapeEcmaScript(str)));
}
@Override
public void setEmail(String str) {
if (str == null) {
Logger.w(LOG_TAG, "Email must not be null. Will not set email.", new Object[0]);
return;
}
loadUrl(String.format(Locale.US, "javascript:$zopim.livechat.setEmail('%s');", StringUtils.escapeEcmaScript(str)));
}
@Override
public void setName(String str) {
if (str == null) {
Logger.w(LOG_TAG, "Name must not be null. Will not set name.", new Object[0]);
return;
}
loadUrl(String.format(Locale.US, "javascript:$zopim.livechat.setName('%s');", StringUtils.escapeEcmaScript(str)));
}
@Override
public void setNote(String str) {
if (str == null) {
Logger.w(LOG_TAG, "Note must not be null. Will not set the note.", new Object[0]);
return;
}
loadUrl(String.format(Locale.US, "javascript:$zopim.livechat.setNotes('%s');", StringUtils.escapeEcmaScript(str)));
}
@Override
public void setPhoneNumber(String str) {
if (str == null) {
Logger.w(LOG_TAG, "Phone number must not be null. Will not set phone number.", new Object[0]);
return;
}
loadUrl(String.format(Locale.US, "javascript:$zopim.livechat.setPhone('%s');", StringUtils.escapeEcmaScript(str)));
}
@Override
public void setPushToken(String str) {
if (str == null) {
Logger.w(LOG_TAG, "Token must not be null. Will not set token.", new Object[0]);
} else {
loadUrl(String.format(Locale.US, "javascript:__z_sdk.setPushToken('%s');", str));
}
}
public WebBinder(Context context) {
WebView webView = new WebView(context.getApplicationContext());
this.mWebView = webView;
webView.getSettings().setJavaScriptEnabled(true);
WebWidgetListener webWidgetListener = new WebWidgetListener();
this.mWebView.addJavascriptInterface(webWidgetListener, "JSInterface");
this.mWebView.setWebViewClient(webWidgetListener);
Locale locale = Locale.US;
this.mUserAgent = String.format(locale, "%s %s %s", this.mWebView.getSettings().getUserAgentString(), String.format(locale, "%s/%s-%s", AppInfo.getApplicationName(context).replaceAll("\\s+", ""), AppInfo.getApplicationVersionName(context), AppInfo.getApplicationStage(context)), String.format(locale, "%s/%s", AppInfo.getChatSdkName().replaceAll("\\s+", ""), AppInfo.getChatSdkVersionName()));
}
private void init(String str, String str2, String str3, String str4, String str5, String str6, String str7) {
Locale locale = Locale.US;
this.mWebView.loadDataWithBaseURL(BASE_URL, String.format(locale, "<!DOCTYPE html><html><head></head><body><script src=\"%s?%s\"></script><script type=\"text/javascript\">%s</script></body></html>", WIDGET_VERSION, str, String.format(locale, "window.__z_sdk.initApp({bridge: '%s',register: {mID: '%s',ua: '%s',title: '%s',url: '%s',ref: '%s',source: 'android_sdk'}});", str2, str3, str4, str5, str6, str7)), "text/html", "UTF-8", "");
}
@Override
public void send(@NonNull File file) {
if (!isValid(file)) {
Logger.w(LOG_TAG, "Could not send file", new Object[0]);
} else {
prepareAttachmentUpload(file);
}
}
}