Skip to content

Commit

Permalink
Fixed SDK does not send "hello" message or app info to webOS #83
Browse files Browse the repository at this point in the history
  • Loading branch information
khk624 authored and khk624 committed Jun 3, 2014
1 parent 3a89b82 commit 4baa5a7
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 4 deletions.
108 changes: 104 additions & 4 deletions src/com/connectsdk/service/WebOSTVService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand All @@ -51,13 +52,18 @@
import org.json.JSONObject;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Base64;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

import com.connectsdk.core.AppInfo;
import com.connectsdk.core.ChannelInfo;
Expand Down Expand Up @@ -520,6 +526,29 @@ protected void handleMessage(JSONObject message) {
}
}
}
} else if ("hello".equals(type)) {
JSONObject jsonObj = (JSONObject)payload;

if (serviceConfig.getServiceUUID() != null) {
if (!serviceConfig.getServiceUUID().equals(jsonObj.optString("deviceUUID"))) {
((WebOSTVServiceConfig)serviceConfig).setClientKey(null);
String cert = null;
((WebOSTVServiceConfig)serviceConfig).setServerCertificate(cert);
((WebOSTVServiceConfig)serviceConfig).setServiceUUID(null);
serviceDescription.setIpAddress(null);
serviceDescription.setUUID(null);

disconnect();
}
}
else {
String uuid = jsonObj.optString("deviceUUID");
serviceConfig.setServiceUUID(uuid);
serviceDescription.setUUID(uuid);
}

state = State.REGISTERING;
sendRegister();
}
}

Expand Down Expand Up @@ -2909,8 +2938,7 @@ public ServiceSubscription<PlayStateListener> subscribePlayState(PlayStateListen
}

protected void handleConnected() {
state = State.REGISTERING;
sendRegister();
helloTV();
}

protected void handleConnectError(Exception ex) {
Expand All @@ -2926,6 +2954,75 @@ public void run() {
});
}

private void helloTV() {
Context context = DiscoveryManager.getInstance().getContext();
PackageManager packageManager = context.getPackageManager();

// app Id
String packageName = context.getPackageName();

// SDK Version
String sdkVersion = "1.0";
try {
sdkVersion = packageManager.getPackageInfo(packageName, 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}

// Device Model
String deviceModel = Build.MODEL;

// OS Version
String OSVersion = String.valueOf(android.os.Build.VERSION.SDK_INT);

// resolution
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
String screenResolution = String.format("%dx%d", width, height);

// app Name
ApplicationInfo applicationInfo;
try {
applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
} catch (final NameNotFoundException e) {
applicationInfo = null;
}
String applicationName = (String) (applicationInfo != null ? packageManager.getApplicationLabel(applicationInfo) : "(unknown)");

// app Region
Locale current = context.getResources().getConfiguration().locale;
String appRegion = current.getDisplayCountry();

JSONObject payload = new JSONObject();
try {
payload.put("sdkVersion", sdkVersion);
payload.put("deviceModel", deviceModel);
payload.put("OSVersion", OSVersion);
payload.put("resolution", screenResolution);
payload.put("appId", packageName);
payload.put("appName", applicationName);
payload.put("appRegion", appRegion);
} catch (JSONException e) {
e.printStackTrace();
}

int dataId = this.nextRequestId++;

JSONObject sendData = new JSONObject();
try {
sendData.put("id", dataId);
sendData.put("type", "hello");
sendData.put("payload", payload);
} catch (JSONException e) {
e.printStackTrace();
}

ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(this, null, sendData, true, null);
this.sendCommandImmediately(request);
}

protected void sendRegister() {
JSONObject headers = new JSONObject();
JSONObject payload = new JSONObject();
Expand Down Expand Up @@ -3047,8 +3144,11 @@ protected void sendCommandImmediately(ServiceCommand<?> command) {
}

this.sendMessage(headers, null);
} else
{
}
else if (payloadType == "hello") {
this.socket.send(payload.toString());
}
else {
try
{
headers.put("type", command.getHttpMethod());
Expand Down
4 changes: 4 additions & 0 deletions src/com/connectsdk/service/config/ServiceConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public static ServiceConfig getConfig(JSONObject json) {
public String getServiceUUID() {
return serviceUUID;
}

public void setServiceUUID(String serviceUUID) {
this.serviceUUID = serviceUUID;
}

public String toString() {
return serviceUUID;
Expand Down

0 comments on commit 4baa5a7

Please sign in to comment.