-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgs.java
371 lines (306 loc) · 14.3 KB
/
gs.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
Refer the following link for original code of Google Services Gradle Plugin(this is modified from that code to use it with java):
https://github.com/google/play-services-plugins/tree/master/google-services-plugin
*/
import java.io.File;
import java.util.TreeMap;
import java.util.Map;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import java.io.IOException;
import com.google.common.base.Strings;
public class gs{
private static File intermediateDir;
private static final String STATUS_DISABLED = "1";
private static final String STATUS_ENABLED = "2";
private static final String OAUTH_CLIENT_TYPE_WEB = "3";
public static void main(String[] args) throws IOException{
System.out.println("Started");
File quickstartFile = new File("google-services.json");
if (quickstartFile == null || !quickstartFile.isFile()) {
System.out.println("The Google Services Plugin cannot function without it.");
}
System.out.println("Parsing json file: " + quickstartFile.getPath());
// delete content of outputdir.
intermediateDir = new File("Interm");
deleteFolder(intermediateDir);
if (!intermediateDir.mkdirs()) {
System.out.println("Failed to create folder: " + intermediateDir);
}
try
{
JsonElement root = new JsonParser().parse(Files.newReader(quickstartFile, Charsets.UTF_8));
if (!root.isJsonObject()) {
System.out.println("Malformed root json");
}
JsonObject rootObject = root.getAsJsonObject();
Map<String, String> resValues = new TreeMap<>();
Map<String, Map<String, String>> resAttributes = new TreeMap<>();
handleProjectNumberAndProjectId(rootObject, resValues);
handleFirebaseUrl(rootObject, resValues);
JsonObject clientObject = getClientForPackageName(rootObject);
if (clientObject != null) {
handleAnalytics(clientObject, resValues);
handleMapsService(clientObject, resValues);
handleGoogleApiKey(clientObject, resValues);
handleGoogleAppId(clientObject, resValues);
handleWebClientId(clientObject, resValues);
} else {
System.out.println("No matching client found for package name ");
}
// write the values file.
File values = new File(intermediateDir, "values");
if (!values.exists() && !values.mkdirs()) {
System.out.println("Failed to create folder: " + values);
}
Files.asCharSink(new File(values, "values.xml"), Charsets.UTF_8)
.write(getValuesContent(resValues, resAttributes));
}
catch (IOException ex)
{
System.out.println("FileNotFoundException");
}
}
static void deleteFolder(final File folder) {
if (!folder.exists()) {
return;
}
File[] files = folder.listFiles();
if (files != null) {
for (final File file : files) {
if (file.isDirectory()) {
deleteFolder(file);
} else {
if (!file.delete()) {
System.out.println("Failed to delete: " + file);
}
}
}
}
if (!folder.delete()) {
System.out.println("Failed to delete: " + folder);
}
}
static void handleProjectNumberAndProjectId(JsonObject rootObject, Map<String, String> resValues)
throws IOException {
JsonObject projectInfo = rootObject.getAsJsonObject("project_info");
if (projectInfo == null) {
System.out.println("Missing project_info object");
}
JsonPrimitive projectNumber = projectInfo.getAsJsonPrimitive("project_number");
if (projectNumber == null) {
System.out.println("Missing project_info/project_number object");
}
resValues.put("gcm_defaultSenderId", projectNumber.getAsString());
JsonPrimitive projectId = projectInfo.getAsJsonPrimitive("project_id");
if (projectId == null) {
System.out.println("Missing project_info/project_id object");
}
resValues.put("project_id", projectId.getAsString());
JsonPrimitive bucketName = projectInfo.getAsJsonPrimitive("storage_bucket");
if (bucketName != null) {
resValues.put("google_storage_bucket", bucketName.getAsString());
}
}
static void handleFirebaseUrl(JsonObject rootObject, Map<String, String> resValues)
throws IOException {
JsonObject projectInfo = rootObject.getAsJsonObject("project_info");
if (projectInfo == null) {
System.out.println("Missing project_info object");
}
JsonPrimitive firebaseUrl = projectInfo.getAsJsonPrimitive("firebase_url");
if (firebaseUrl != null) {
resValues.put("firebase_database_url", firebaseUrl.getAsString());
}
}
static void handleAnalytics(JsonObject clientObject, Map<String, String> resValues)
throws IOException {
JsonObject analyticsService = getServiceByName(clientObject, "analytics_service");
if (analyticsService == null) return;
JsonObject analyticsProp = analyticsService.getAsJsonObject("analytics_property");
if (analyticsProp == null) return;
JsonPrimitive trackingId = analyticsProp.getAsJsonPrimitive("tracking_id");
if (trackingId == null) return;
resValues.put("ga_trackingId", trackingId.getAsString());
File xml = new File(intermediateDir, "xml");
if (!xml.exists() && !xml.mkdirs()) {
System.out.println("Failed to create folder: " + xml);
}
Files.asCharSink(new File(xml, "global_tracker.xml"), Charsets.UTF_8)
.write(getGlobalTrackerContent(trackingId.getAsString()));
}
static void handleMapsService(JsonObject clientObject, Map<String, String> resValues)
throws IOException {
JsonObject mapsService = getServiceByName(clientObject, "maps_service");
if (mapsService == null) return;
String apiKey = getAndroidApiKey(clientObject);
if (apiKey != null) {
resValues.put("google_maps_key", apiKey);
return;
}
System.out.println("Missing api_key/current_key object");
}
static JsonObject getClientForPackageName(JsonObject jsonObject) {
JsonArray array = jsonObject.getAsJsonArray("client");
if (array != null) {
final int count = array.size();
for (int i = 0; i < count; i++) {
JsonElement clientElement = array.get(i);
if (clientElement == null || !clientElement.isJsonObject()) {
continue;
}
JsonObject clientObject = clientElement.getAsJsonObject();
JsonObject clientInfo = clientObject.getAsJsonObject("client_info");
if (clientInfo == null) continue;
JsonObject androidClientInfo = clientInfo.getAsJsonObject("android_client_info");
if (androidClientInfo == null) continue;
JsonPrimitive clientPackageName = androidClientInfo.getAsJsonPrimitive("package_name");
if (clientPackageName == null) continue;
if (getApplicationId().equals(clientPackageName.getAsString())) {
return clientObject;
}
}
}
return null;
}
static JsonObject getServiceByName(JsonObject clientObject, String serviceName) {
JsonObject services = clientObject.getAsJsonObject("services");
if (services == null) return null;
JsonObject service = services.getAsJsonObject(serviceName);
if (service == null) return null;
JsonPrimitive status = service.getAsJsonPrimitive("status");
if (status == null) return null;
String statusStr = status.getAsString();
if (STATUS_DISABLED.equals(statusStr)) return null;
if (!STATUS_ENABLED.equals(statusStr)) {
System.out.println(
String.format(
"Status with value '%1$s' for service '%2$s' is unknown",
statusStr, serviceName));
return null;
}
return service;
}
static String getGlobalTrackerContent(String ga_trackingId) {
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
+ "<resources>\n"
+ " <string name=\"ga_trackingId\" translatable=\"false\">"
+ ga_trackingId
+ "</string>\n"
+ "</resources>\n";
}
static String getAndroidApiKey(JsonObject clientObject) {
JsonArray array = clientObject.getAsJsonArray("api_key");
if (array != null) {
final int count = array.size();
for (int i = 0; i < count; i++) {
JsonElement apiKeyElement = array.get(i);
if (apiKeyElement == null || !apiKeyElement.isJsonObject()) {
continue;
}
JsonObject apiKeyObject = apiKeyElement.getAsJsonObject();
JsonPrimitive currentKey = apiKeyObject.getAsJsonPrimitive("current_key");
if (currentKey == null) {
continue;
}
return currentKey.getAsString();
}
}
return null;
}
static String getApplicationId(){
return "com.example.testjsontostrings";
}
static void handleGoogleApiKey(JsonObject clientObject, Map<String, String> resValues) {
String apiKey = getAndroidApiKey(clientObject);
if (apiKey != null) {
resValues.put("google_api_key", apiKey);
// TODO: remove this once SDK starts to use google_api_key.
resValues.put("google_crash_reporting_api_key", apiKey);
return;
}
// if google_crash_reporting_api_key is missing.
System.out.println("Missing api_key/current_key object");
}
static void handleGoogleAppId(JsonObject clientObject, Map<String, String> resValues)
throws IOException {
JsonObject clientInfo = clientObject.getAsJsonObject("client_info");
if (clientInfo == null) {
// Should not happen
System.out.println("Client does not have client info");
}
JsonPrimitive googleAppId = clientInfo.getAsJsonPrimitive("mobilesdk_app_id");
String googleAppIdStr = googleAppId == null ? null : googleAppId.getAsString();
if (Strings.isNullOrEmpty(googleAppIdStr)) {
System.out.println(
"Missing Google App Id. "
+ "Please follow instructions on https://firebase.google.com/ to get a valid "
+ "config file that contains a Google App Id");
}
resValues.put("google_app_id", googleAppIdStr);
}
static void handleWebClientId(JsonObject clientObject, Map<String, String> resValues) {
JsonArray array = clientObject.getAsJsonArray("oauth_client");
if (array != null) {
final int count = array.size();
for (int i = 0; i < count; i++) {
JsonElement oauthClientElement = array.get(i);
if (oauthClientElement == null || !oauthClientElement.isJsonObject()) {
continue;
}
JsonObject oauthClientObject = oauthClientElement.getAsJsonObject();
JsonPrimitive clientType = oauthClientObject.getAsJsonPrimitive("client_type");
if (clientType == null) {
continue;
}
String clientTypeStr = clientType.getAsString();
if (!OAUTH_CLIENT_TYPE_WEB.equals(clientTypeStr)) {
continue;
}
JsonPrimitive clientId = oauthClientObject.getAsJsonPrimitive("client_id");
if (clientId == null) {
continue;
}
resValues.put("default_web_client_id", clientId.getAsString());
return;
}
}
}
static String getValuesContent(
Map<String, String> values, Map<String, Map<String, String>> attributes) {
StringBuilder sb = new StringBuilder(256);
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n");
for (Map.Entry<String, String> entry : values.entrySet()) {
String name = entry.getKey();
sb.append(" <string name=\"").append(name).append("\" translatable=\"false\"");
if (attributes.containsKey(name)) {
for (Map.Entry<String, String> attr : attributes.get(name).entrySet()) {
sb.append(" ").append(attr.getKey()).append("=\"").append(attr.getValue()).append("\"");
}
}
sb.append(">").append(entry.getValue()).append("</string>\n");
}
sb.append("</resources>\n");
return sb.toString();
}
}