Create device info proto on c++ side, pass it over JNI by serialization

We used to pass just a debug string of proto.
Now we pass the whole proto.
Also renames package name:
device_info => androidgamesdk_deviceinfo

Bug: 116373678
Fix: 119858889

Test: manual on walleye
Test: tapas device_info_app && make
Test: adb install out/target/product/generic/system/app/device_info_app/device_info_app.apk
Test: Run installed APK and 'press button'
Change-Id: Iff33ce46f731b169deb0150856e38e21606656d8
diff --git a/Android.bp b/Android.bp
index 0a66be8..e88a601 100644
--- a/Android.bp
+++ b/Android.bp
@@ -45,3 +45,8 @@
     type: "lite",
   },
 }
+
+filegroup {
+    name: "device_info_proto",
+    srcs: [ "include/device_info/device_info.proto" ],
+}
\ No newline at end of file
diff --git a/include/device_info/device_info.h b/include/device_info/device_info.h
index 8f7ac92..3c5de2c 100644
--- a/include/device_info/device_info.h
+++ b/include/device_info/device_info.h
@@ -12,6 +12,8 @@
  * limitations under the License.
  */
 
+#include "frameworks/opt/gamesdk/include/device_info/device_info.pb.h"
+
 #include <EGL/egl.h>
 #include <GLES3/gl32.h>
 
@@ -23,9 +25,6 @@
 #include <vector>
 #include <set>
 
-namespace device_info {
-class root;
-void createProto(device_info::root& proto);
-
-std::string getDebugString();
+namespace androidgamesdk_deviceinfo {
+void createProto(root& proto);
 }  // namespace device_info
diff --git a/include/device_info/device_info.proto b/include/device_info/device_info.proto
index 2b4c758..c352702 100644
--- a/include/device_info/device_info.proto
+++ b/include/device_info/device_info.proto
@@ -1,6 +1,8 @@
 syntax = "proto2";
 
-package device_info;
+package androidgamesdk_deviceinfo;
+
+option java_package = "com.google.androidgamesdk";
 
 message cpu_core{
   optional int64 freq_max = 1;
diff --git a/samples/device_info_app/Android.bp b/samples/device_info_app/Android.bp
index 4fa97a4..fe02286 100644
--- a/samples/device_info_app/Android.bp
+++ b/samples/device_info_app/Android.bp
@@ -14,7 +14,8 @@
   manifest: "AndroidManifest.xml",
   sdk_version: "27",
   srcs: [
-    "*.java"
+    "*.java",
+    ":device_info_proto",
   ],
   resource_dirs: [
     "res",
@@ -24,5 +25,9 @@
   ],
   static_libs: [
     "android-support-constraint-layout",
+    "libprotobuf-java-lite",
   ],
+  proto: {
+    type: "lite",
+  },
 }
diff --git a/samples/device_info_app/MainActivity.java b/samples/device_info_app/MainActivity.java
index 9301ac6..da8783e 100644
--- a/samples/device_info_app/MainActivity.java
+++ b/samples/device_info_app/MainActivity.java
@@ -21,11 +21,13 @@
 import android.widget.TextView;
 import android.os.Build;
 
+import com.google.androidgamesdk.DeviceInfo;  // proto
+
 public class MainActivity extends Activity {
   static {
     System.loadLibrary("device_info_app");
   }
-  public native String jniGetDeviceInfoDebugString();
+  public native byte[] jniGetProtoSerialized();
 
   @Override
   protected void onCreate(Bundle savedInstanceState) {
@@ -38,9 +40,20 @@
       public void onClick(View view) {
         TextView tv = (TextView) findViewById(R.id.sample_text);
         String javaDebugString = Build.FINGERPRINT;
-        String nativeDebugString = jniGetDeviceInfoDebugString();
-
-        String message = String.format("Fingerprint = %s\nNative String = %s", javaDebugString, nativeDebugString);
+        String nativeDebugString = "UNSET";
+        try{
+          com.google.androidgamesdk.DeviceInfo.root proto;
+          byte[] nativeBytes = jniGetProtoSerialized();
+          proto = com.google.androidgamesdk.DeviceInfo.root.parseFrom(nativeBytes);
+          nativeDebugString = proto.getCpuPresent();
+        }catch(Exception e){
+          android.util.Log.e("device_info", "could not create proto.", e);
+        }
+        String message = String.format(
+          "Fingerprint = %s\nNative String = %s",
+          javaDebugString,
+          nativeDebugString
+        );
         tv.setText(message);
       }
     });
diff --git a/samples/device_info_app/device_info_app.cpp b/samples/device_info_app/device_info_app.cpp
index 43b2e4b..51ebddf 100644
--- a/samples/device_info_app/device_info_app.cpp
+++ b/samples/device_info_app/device_info_app.cpp
@@ -14,15 +14,23 @@
 
 #include <jni.h>
 #include <string>
+#include <cassert>
 
 #include "device_info/device_info.h"
 
 extern "C" {
-JNIEXPORT jstring
+JNIEXPORT jbyteArray JNICALL
+Java_com_google_deviceinfotest_MainActivity_jniGetProtoSerialized(
+                                          JNIEnv *env, jobject) {
+  androidgamesdk_deviceinfo::root proto;
+  androidgamesdk_deviceinfo::createProto(proto);
 
-JNICALL
-Java_com_google_deviceinfotest_MainActivity_jniGetDeviceInfoDebugString(
-                                                 	    JNIEnv *env, jobject) {
-  return env->NewStringUTF(device_info::getDebugString().c_str());
+  size_t bufferSize = proto.ByteSize();
+  void* buffer = malloc(bufferSize);
+  proto.SerializeToArray(buffer, bufferSize);
+  jbyteArray result = env->NewByteArray(bufferSize);
+  env->SetByteArrayRegion(result, 0, bufferSize, static_cast<jbyte*>(buffer));
+  free(buffer);
+  return result;
 }
-}  // extern "C"
\ No newline at end of file
+}  // extern "C"
diff --git a/samples/device_info_exe/main.cpp b/samples/device_info_exe/main.cpp
index a8cf254..14cee38 100644
--- a/samples/device_info_exe/main.cpp
+++ b/samples/device_info_exe/main.cpp
@@ -16,7 +16,9 @@
 
 int main(){
   std::cout << "*Proto debug begin:" << std::endl;
-  std::cout << device_info::getDebugString()  << std::endl;
+  androidgamesdk_deviceinfo::root proto;
+  androidgamesdk_deviceinfo::createProto(proto);
+  std::cout << proto.cpu_present() << std::endl;
   std::cout << "*Proto debug end." << std::endl;
   std::cout << "fin." << std::endl;
   return 0;
diff --git a/src/device_info/device_info.cpp b/src/device_info/device_info.cpp
index ef48e42..e2c01ca 100644
--- a/src/device_info/device_info.cpp
+++ b/src/device_info/device_info.cpp
@@ -13,7 +13,6 @@
  */
 
  #include "device_info/device_info.h"
- #include "frameworks/opt/gamesdk/include/device_info/device_info.pb.h"
 
 namespace {
 typedef int64_t int64;
@@ -200,7 +199,7 @@
 }
 }  // namespace gl_util
 
-void addGlConstsV2_0(device_info::gl& gl) {
+void addGlConstsV2_0(androidgamesdk_deviceinfo::gl& gl) {
   gl.set_gl_aliased_line_width_range(
     ::gl_util::getFloat(GL_ALIASED_LINE_WIDTH_RANGE));
   gl.set_gl_aliased_point_size_range(
@@ -289,7 +288,7 @@
   gl.set_spf_fragment_int_hig_range(spfr);
   gl.set_spf_fragment_int_hig_prec(spfp);
 }
-void addGlConstsV3_0(device_info::gl& gl) {
+void addGlConstsV3_0(androidgamesdk_deviceinfo::gl& gl) {
   gl.set_gl_max_3d_texture_size(
     ::gl_util::getInt(GL_MAX_3D_TEXTURE_SIZE));
   gl.set_gl_max_array_texture_layers(
@@ -349,7 +348,7 @@
   gl.set_gl_max_uniform_block_size(
     ::gl_util::getInt64(GL_MAX_UNIFORM_BLOCK_SIZE));
 }
-void addGlConstsV3_1(device_info::gl& gl) {
+void addGlConstsV3_1(androidgamesdk_deviceinfo::gl& gl) {
   gl.set_gl_max_atomic_counter_buffer_bindings(
     ::gl_util::getInt(GL_MAX_ATOMIC_COUNTER_BUFFER_BINDINGS));
   gl.set_gl_max_atomic_counter_buffer_size(
@@ -452,7 +451,7 @@
   gl.set_gl_max_compute_work_group_size_2(
     ::gl_util::getIntIndexed(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 2));
 }
-void addGlConstsV3_2(device_info::gl& gl) {
+void addGlConstsV3_2(androidgamesdk_deviceinfo::gl& gl) {
   gl.set_gl_context_flags(
     ::gl_util::getInt(GL_CONTEXT_FLAGS));
   gl.set_gl_fragment_interpolation_offset_bits(
@@ -563,13 +562,13 @@
 }
 }  // namespace
 
-namespace device_info {
-void createProto(device_info::root& proto) {
+namespace androidgamesdk_deviceinfo {
+void createProto(root& proto) {
   int cpuIndexMax = readCpuIndexMax();
   proto.set_cpu_max_index(cpuIndexMax);
 
   for (int cpuIndex = 0; cpuIndex <= cpuIndexMax; cpuIndex++) {
-    device_info::cpu_core* newCore = proto.add_cpu_core();
+    cpu_core* newCore = proto.add_cpu_core();
     int64 freqMax = readCpuFreqMax(cpuIndex);
     if (freqMax > 0) {
       newCore->set_freq_max(freqMax);
@@ -588,7 +587,7 @@
 
   setupEGl();
 
-  device_info::gl& gl = *proto.mutable_gl();
+  gl& gl = *proto.mutable_gl();
   gl.set_renderer(reinterpret_cast<const char*>(glGetString(GL_RENDERER)));
   gl.set_vendor(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));
   gl.set_version(reinterpret_cast<const char*>(glGetString(GL_VERSION)));
@@ -645,15 +644,4 @@
 
   flushGlErrors();
 }
-
-std::string getDebugString() {
-  device_info::root proto;
-  createProto(proto);
-
-  std::string output;
-  output.append("renderer = ");
-  output.append(proto.gl().renderer());
-  return output;
-}
-
 }  // namespace device_info