Merge pull request #2259 from rohankuruvilla/ccbreader

fixed #1861: CocosPlayer Android update.
This commit is contained in:
James Chen 2013-03-25 02:10:43 -07:00
commit 092eb30d06
9 changed files with 474 additions and 430 deletions

View File

@ -1 +1 @@
349baa101ddbdf4edf6d20b17362bd22c0008284
ba6bcddd1ff6445feb931434c6a04546bc936a4e

View File

@ -1 +1 @@
fe7e93e8e974f39426858d2bfbed7dff26c2b9c4
e386fa2dd33381cb9905ec5472ba7b48bdd60808

View File

@ -0,0 +1 @@
263d1eb95a83c1320c7c988e633f348479d38fb5

View File

@ -1 +1 @@
20213456280fa1bced942496820fd71cb35ddb64
823b99b16a9fdddb84bf8af36e2be53cf928ea04

View File

@ -0,0 +1 @@
7b5dab861d78877bb1354fc832404f9dfb68b0ee

View File

@ -1,7 +1,9 @@
package org.cocos2dx.cocosplayer;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
import org.cocos2dx.lib.Cocos2dxHelper;
import java.util.Arrays;
import java.util.Enumeration;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
@ -16,6 +18,8 @@ import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipException;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.FileOutputStream;
@ -49,6 +53,11 @@ public class CCBFileUtilsHelper {
}
}
private static boolean containsDirectory(String name) {
if(name.contains("/")) {
return true;
} return false;
}
private static void unzip(InputStream is, Context cw) {
try {
@ -66,6 +75,10 @@ public class CCBFileUtilsHelper {
_dirChecker(path, ze.getName());
} else {
File fileToWrite = new File(path, ze.getName());
File parent = fileToWrite.getParentFile();
if(!parent.exists() && !parent.mkdirs()){
throw new IllegalStateException("Couldn't create dir: " + parent);
}
FileOutputStream fout = new FileOutputStream(fileToWrite);
while ((length = zin.read(buffer))>0) {
@ -84,53 +97,8 @@ public class CCBFileUtilsHelper {
}
// public static void unzip(String zipname) {
// try {
// String filename;
// //Context cw = getApplicationContext();
// //ContextWrapper cw = new ContextWrapper(this);
// File path = new File(CCBFileUtilsHelper.getBaseDirectory(cw));
// //File path = android.os.Environment.getExternalStorageDirectory(); //Environment.getExternalFilesDir();
// File ccbFile = new File(path, zipname);
// if(!isValid(ccbFile))
// Log.i(TAG, "Zip file recieved is INVALID: ");
// else {
// Log.i(TAG, "Zip file recieved is VALID: ");
// }
// FileInputStream fin = new FileInputStream(ccbFile);
// ZipInputStream zin = new ZipInputStream(fin);
// ZipEntry ze = null;
// byte[] buffer = new byte[1024];
// int length;
// while ((ze = zin.getNextEntry()) != null) {
// Log.v(TAG, "Unzipping " + ze.getName());
// if(ze.isDirectory()) {
// _dirChecker(path, ze.getName());
// } else {
// File fileToWrite = new File(path, ze.getName());
// FileOutputStream fout = new FileOutputStream(fileToWrite);
// while ((length = zin.read(buffer))>0) {
// fout.write(buffer, 0, length);
// }
// zin.closeEntry();
// fout.close();
// }
// }
// zin.close();
// } catch(Exception e) {
// Log.e(TAG, "unzip", e);
// }
// }
private static void _dirChecker(File path, String dir) {
File f = new File(path, dir);
if(!f.isDirectory()) {
@ -138,13 +106,30 @@ public class CCBFileUtilsHelper {
}
}
private static void list( String path ) {
File root = new File( path );
File[] list = root.listFiles();
for ( File f : list ) {
if ( f.isDirectory() ) {
list( f.getAbsolutePath() );
Log.i(TAG, "Dir:" + f.getAbsoluteFile() );
}
else {
Log.i(TAG, "File:" + f.getAbsoluteFile() );
}
}
}
public static void unzipCCB(byte[] data, Context cw) {
String zipname = "ccb.zip";
File path = new File(CCBFileUtilsHelper.getBaseDirectory(cw));
unzip(new ByteArrayInputStream(data), cw);
}
public static String getBaseDirectory(Context cw) {
return cw.getCacheDir().getAbsolutePath();
return Cocos2dxHelper.getCocos2dxWritablePath();
}
private static File getBasePath() {

View File

@ -17,9 +17,11 @@ import com.dd.plist.UID;
import com.dd.plist.PropertyListParser;
import com.dd.plist.BinaryPropertyListParser;
import com.dd.plist.BinaryPropertyListWriter;
import java.net.Socket;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Random;
public class CCBStreamHandler {
@ -89,6 +91,30 @@ public class CCBStreamHandler {
return length;
}
public static void sendString(String data, Socket client, PrintWriter out) {
try {
byte[] header = getHeader(data);
Log.i(TAG, "Sending string "+data);
client.getOutputStream().write(header, 0, header.length);
out.write(data);
out.flush();
} catch(Exception e) {
}
}
public static byte[] getHeader(String data) {
byte[] header;
ByteBuffer b = ByteBuffer.allocate(4);
try {
b.order(ByteOrder.BIG_ENDIAN);
Log.i(TAG, "Payload Length: "+data.length());
b.putInt(data.length());
} catch(Exception e) {
}
header = b.array();
return header;
}
public static String getDeviceInfo() {
try {
@ -97,6 +123,8 @@ public class CCBStreamHandler {
root.put("devicename", android.os.Build.MODEL);
root.put("devicetype", "Android");
root.put("preferredresourcetype", resolution);
Random randomGenerator = new Random();
root.put("uuid", ""+ randomGenerator.nextInt(100000));
String payload = root.toXMLPropertyList();
// String data = new String(header, 0 , header.length);
@ -106,6 +134,22 @@ public class CCBStreamHandler {
return null;
}
public static String getFileSystem() {
try {
NSDictionary root = new NSDictionary();
root.put("cmd", "filelist");
NSDictionary fileList = new NSDictionary();
root.put("filelist", fileList);
String payload = root.toXMLPropertyList();
// String data = new String(header, 0 , header.length);
return payload;
} catch(Exception e) {
}
return null;
}
public static void setDeviceResolution(String res) {
CocosPlayerSocket server = new CocosPlayerSocket();
Log.i(TAG, "Starting with resolution: "+res);

View File

@ -26,6 +26,7 @@ package org.cocos2dx.cocosplayer;
import org.cocos2dx.lib.Cocos2dxActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.ContextWrapper;
import android.content.Context;
@ -33,8 +34,16 @@ import android.content.Context;
public class CocosPlayer extends Cocos2dxActivity{
public static Context c;
public static Activity activity;
public static void setOrientation(int orient) {
activity.setRequestedOrientation(orient);
}
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
activity = this;
c = getApplicationContext();
}

View File

@ -1,5 +1,7 @@
package org.cocos2dx.cocosplayer;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.cocosplayer.CocosPlayer;
import java.util.Arrays;
import java.io.BufferedReader;
@ -15,6 +17,7 @@ import android.os.IBinder;
import android.util.Log;
import android.content.ContextWrapper;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Environment;
import android.os.AsyncTask;
import java.io.OutputStream;
@ -23,6 +26,7 @@ import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import com.dd.plist.NSArray;
import com.dd.plist.NSDictionary;
import com.dd.plist.NSData;
import com.dd.plist.UID;
@ -72,6 +76,11 @@ public class CocosPlayerSocket {
private static native void nativeStopCCB();
private static native void nativeRunScript(final String script);
private static void setOrientation(String isPortrait) {
CocosPlayer.setOrientation(isPortrait.equalsIgnoreCase("true") ?
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
private void switchCmd(NSDictionary data) {
try {
String cmd = data.objectForKey("cmd").toString();
@ -93,6 +102,9 @@ public class CocosPlayerSocket {
stopCCB();
} else if(cmd.equalsIgnoreCase("script")) {
runScript(data.objectForKey("script").toString());
} else if(cmd.equalsIgnoreCase("settings")) {
NSArray orient = (NSArray)data.objectForKey("orientations");
setOrientation(orient.objectAtIndex(0).toString());
}
} catch(Exception e) {
Log.i(TAG, "JSON Error: "+e.toString());
@ -178,23 +190,15 @@ public class CocosPlayerSocket {
Log.i(TAG, "Client null");
break;
}
try {
String data = CCBStreamHandler.getDeviceInfo();
ByteBuffer b = ByteBuffer.allocate(4);
b.order(ByteOrder.BIG_ENDIAN);
Log.i(TAG, "Payload Length: "+data.length());
b.putInt(data.length());
byte[] header = b.array();
Log.i(TAG, "Sending name "+data);
client.getOutputStream().write(header, 0 , header.length);
// Send deviceInfo and filelist
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
out.write(data);
out.flush();
CCBStreamHandler.sendString(CCBStreamHandler.getDeviceInfo(), client, out);
CCBStreamHandler.sendString(CCBStreamHandler.getFileSystem(), client, out);
//out.close();
// Process CocosBuilder input data stream
processStream(client);
} catch (Exception e) {