From a2cdc82b78e58d12e1ea96133c8260202d48141e Mon Sep 17 00:00:00 2001 From: Hein-Pieter van Braam Date: Tue, 10 Jul 2018 14:24:12 +0200 Subject: [PATCH] Write new permissions to the AndroidManifest.xml Instead of editing the placeholder permissions actually write new ones. This should solve the privacy statement problems for the Play store. This means we also no longer need to placeholder permissions in the template. --- platform/android/AndroidManifest.xml.template | 165 ------------------ platform/android/export/export.cpp | 137 ++++++++++++--- 2 files changed, 111 insertions(+), 191 deletions(-) diff --git a/platform/android/AndroidManifest.xml.template b/platform/android/AndroidManifest.xml.template index 028133aca09..c3afe08f286 100644 --- a/platform/android/AndroidManifest.xml.template +++ b/platform/android/AndroidManifest.xml.template @@ -35,171 +35,6 @@ $$ADD_APPLICATION_CHUNKS$$ $$ADD_PERMISSION_CHUNKS$$ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/platform/android/export/export.cpp b/platform/android/export/export.cpp index 841b68f2490..2f8f044ff0a 100644 --- a/platform/android/export/export.cpp +++ b/platform/android/export/export.cpp @@ -805,27 +805,6 @@ void EditorExportPlatformAndroid::_fix_manifest(Vector &p_manifest, boo }*/ } - if (tname == "uses-permission" && /*nspace=="android" &&*/ attrname == "name") { - - if (value.begins_with("godot.custom")) { - - int which = value.get_slice(".", 2).to_int(); - if (which >= 0 && which < MAX_USER_PERMISSIONS && user_perms[which].strip_edges() != "") { - - string_table[attr_value] = user_perms[which].strip_edges(); - } - - } else if (value.begins_with("godot.")) { - String perm = value.get_slice(".", 1); - - if (perms.has(perm) || (p_give_internet && perm == "INTERNET")) { - - print_line("PERM: " + perm); - string_table[attr_value] = "android.permission." + perm; - } - } - } - if (tname == "supports-screens") { if (attrname == "smallScreens") { @@ -850,6 +829,110 @@ void EditorExportPlatformAndroid::_fix_manifest(Vector &p_manifest, boo } } break; + case CHUNK_XML_END_TAG: { + int iofs = ofs + 8; + uint32_t name = decode_uint32(&p_manifest[iofs + 12]); + String tname = string_table[name]; + + if (tname == "manifest") { + print_line("Found manifest end"); + + // save manifest ending so we can restore it + Vector manifest_end; + uint32_t manifest_cur_size = p_manifest.size(); + uint32_t node_size = size; + + manifest_end.resize(p_manifest.size() - ofs); + memcpy(manifest_end.ptr(), &p_manifest[ofs], manifest_end.size()); + + int32_t attr_name_string = string_table.find("name"); + ERR_EXPLAIN("Template does not have 'name' attribute"); + ERR_FAIL_COND(attr_name_string == -1); + + int32_t ns_android_string = string_table.find("android"); + ERR_EXPLAIN("Template does not have 'android' namespace"); + ERR_FAIL_COND(ns_android_string == -1); + + int32_t attr_uses_permission_string = string_table.find("uses-permission"); + if (attr_uses_permission_string == -1) { + string_table.push_back("uses-permission"); + attr_uses_permission_string = string_table.size() - 1; + } + + Vector apk_perms; + const char **aperms = android_perms; + while (*aperms) { + if (perms.has(*aperms)) { + apk_perms.push_back("android.permission." + String(*aperms)); + } + aperms++; + } + + for (int i = 0; i < MAX_USER_PERMISSIONS; i++) { + if (user_perms[i].strip_edges() != "" && user_perms[i].strip_edges() != "False") + apk_perms.push_back(user_perms[i].strip_edges()); + } + + if (p_give_internet) { + if (apk_perms.find("android.permission.INTERNET") == -1) + apk_perms.push_back("android.permission.INTERNET"); + } + + for (int i = 0; i < apk_perms.size(); ++i) { + print_line("Adding permission " + apk_perms[i]); + + manifest_cur_size += 56 + 24; // node + end node + p_manifest.resize(manifest_cur_size); + + // Add permission to the string pool + int32_t perm_string = string_table.find(apk_perms[i]); + if (perm_string == -1) { + string_table.push_back(apk_perms[i]); + perm_string = string_table.size() - 1; + } + + // start tag + encode_uint16(0x102, &p_manifest[ofs]); // type + encode_uint16(16, &p_manifest[ofs + 2]); // headersize + encode_uint32(56, &p_manifest[ofs + 4]); // size + encode_uint32(0, &p_manifest[ofs + 8]); // lineno + encode_uint32(-1, &p_manifest[ofs + 12]); // comment + encode_uint32(-1, &p_manifest[ofs + 16]); // ns + encode_uint32(attr_uses_permission_string, &p_manifest[ofs + 20]); // name + encode_uint16(20, &p_manifest[ofs + 24]); // attr_start + encode_uint16(20, &p_manifest[ofs + 26]); // attr_size + encode_uint16(1, &p_manifest[ofs + 28]); // num_attrs + encode_uint16(0, &p_manifest[ofs + 30]); // id_index + encode_uint16(0, &p_manifest[ofs + 32]); // class_index + encode_uint16(0, &p_manifest[ofs + 34]); // style_index + + // attribute + encode_uint32(ns_android_string, &p_manifest[ofs + 36]); // ns + encode_uint32(attr_name_string, &p_manifest[ofs + 40]); // 'name' + encode_uint32(perm_string, &p_manifest[ofs + 44]); // raw_value + encode_uint16(8, &p_manifest[ofs + 48]); // typedvalue_size + p_manifest[ofs + 50] = 0; // typedvalue_always0 + p_manifest[ofs + 51] = 0x03; // typedvalue_type (string) + encode_uint32(perm_string, &p_manifest[ofs + 52]); // typedvalue reference + + ofs += 56; + + // end tag + encode_uint16(0x103, &p_manifest[ofs]); // type + encode_uint16(16, &p_manifest[ofs + 2]); // headersize + encode_uint32(24, &p_manifest[ofs + 4]); // size + encode_uint32(0, &p_manifest[ofs + 8]); // lineno + encode_uint32(-1, &p_manifest[ofs + 12]); // comment + encode_uint32(-1, &p_manifest[ofs + 16]); // ns + encode_uint32(attr_uses_permission_string, &p_manifest[ofs + 20]); // name + + ofs += 24; + } + + // copy footer back in + memcpy(&p_manifest[ofs], manifest_end.ptr(), manifest_end.size()); + } + } break; } //printf("chunk %x: size: %d\n",chunk,size); @@ -873,17 +956,17 @@ void EditorExportPlatformAndroid::_fix_manifest(Vector &p_manifest, boo encode_uint32(ofs, &ret[string_table_begins + i * 4]); ofs += string_table[i].length() * 2 + 2 + 2; - //print_line("ofs: "+itos(i)+": "+itos(ofs)); } + ret.resize(ret.size() + ofs); - uint8_t *chars = &ret[ret.size() - ofs]; + string_data_offset = ret.size() - ofs; + uint8_t *chars = &ret[string_data_offset]; for (int i = 0; i < string_table.size(); i++) { String s = string_table[i]; - //print_line("savint string :"+s); encode_uint16(s.length(), chars); chars += 2; - for (int j = 0; j < s.length(); j++) { //include zero? + for (int j = 0; j < s.length(); j++) { encode_uint16(s[j], chars); chars += 2; } @@ -895,6 +978,7 @@ void EditorExportPlatformAndroid::_fix_manifest(Vector &p_manifest, boo ret.push_back(stable_extra[i]); } + //pad while (ret.size() % 4) ret.push_back(0); @@ -910,7 +994,8 @@ void EditorExportPlatformAndroid::_fix_manifest(Vector &p_manifest, boo encode_uint32(ret.size(), &ret[4]); //update new file size encode_uint32(new_stable_end - 8, &ret[12]); //update new string table size - + encode_uint32(string_table.size(), &ret[16]); //update new number of strings + encode_uint32(string_data_offset - 8, &ret[28]); //update new string data offset //print_line("file size: "+itos(ret.size())); p_manifest = ret;