This repository has been archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 332
/
AndroidHelper.gradle
202 lines (181 loc) · 7.48 KB
/
AndroidHelper.gradle
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
import groovy.json.JsonBuilder
class AndroidHelper {
static def saveProcessedConfig(project, config) {
def path = project.file("assets/config")
path.mkdirs()
def jsonWriter = new FileWriter(path.absolutePath + "/config.json")
def builder = new JsonBuilder(config)
jsonWriter.withWriter {
builder.writeTo(it)
}
}
static def saveGoogleServicesJson(project, firebaseConfig) {
def googleServiceJsonPath = project.projectDir.toString() + "/google-services.json"
new FileWriter(googleServiceJsonPath).withWriter {
getGoogleServicesContent(project, firebaseConfig).writeTo(it)
}
}
static def getGoogleServicesContent(project, firebaseConfig) {
if (checkRequiredFields(firebaseConfig)) {
def jsonBuilder = new JsonBuilder()
def projectNumber = firebaseConfig.get("PROJECT_NUMBER")
def projectId = firebaseConfig.get("PROJECT_ID")
def oauthClientId = firebaseConfig.get("OAUTH_CLIENT_ID")
def currentKey = firebaseConfig.get("CURRENT_KEY")
def mobileSdkAppId = firebaseConfig.get("MOBILE_SDK_APP_ID")
// construct the google-services.json fields
jsonBuilder {
project_info {
project_number projectNumber
firebase_url "https://$projectId" + ".firebaseio.com"
project_id projectId
storage_bucket projectId + ".appspot.com"
}
client([{
client_info {
"mobilesdk_app_id" mobileSdkAppId
"android_client_info" {
"package_name" project.APPLICATION_ID
}
}
oauth_client([
{
"client_id" oauthClientId
"client_type" 3
}
])
api_key(
[{
"current_key" currentKey
}])
services {
appinvite_service {
other_platform_oauth_client([{
"client_id" oauthClientId
"client_type" 3
}
])
}
}
}])
configuration_version "1"
}
return jsonBuilder
}
}
static def checkRequiredFields(firebaseConfig) {
def available = true
def message = ""
if (!firebaseConfig.get("PROJECT_NUMBER")) {
message += 'FIREBASE:PROJECT_NUMBER is missing or empty\n'
available = false
}
if (!firebaseConfig.get("PROJECT_ID")) {
message += 'FIREBASE:PROJECT_ID is missing or empty\n'
available = false
}
if (!firebaseConfig.get("OAUTH_CLIENT_ID")) {
message += 'FIREBASE:OAUTH_CLIENT_ID is missing or empty\n'
available = false
}
if (!firebaseConfig.get("CURRENT_KEY")) {
message += 'FIREBASE:CURRENT_KEY is missing or empty\n'
available = false
}
if (!firebaseConfig.get("MOBILE_SDK_APP_ID")) {
message += 'FIREBASE:MOBILE_SDK_APP_ID is missing or empty\n'
available = false
}
if (!available) {
throw new GradleException(message)
}
return available
}
static def saveMicrosoftAuthConfigs(project, microsoftConfig) {
def microsoftConfigsJsonPath = project.projectDir.toString() + "/res/raw/auth_config.json"
new FileWriter(microsoftConfigsJsonPath).withWriter {
getMicrosoftConfigurations(project, microsoftConfig).writeTo(it)
}
}
static def getMicrosoftConfigurations(project, microsoftConfig) {
if (isValidConfigurations(microsoftConfig)) {
def jsonBuilder = new JsonBuilder()
def clientId = microsoftConfig.get("CLIENT_ID")
def signatureHash = microsoftConfig.get("SIGNATURE_HASH")
def audienceType = microsoftConfig.get("AUDIENCE_TYPE")
def tenantId = microsoftConfig.get("TENANT_ID")
jsonBuilder {
"client_id" "$clientId"
"authorization_user_agent" "DEFAULT"
"redirect_uri" "msauth://" + project.APPLICATION_ID + "/" + URLEncoder.encode(signatureHash, "UTF-8")
"broker_redirect_uri_registered" false
"account_mode" "MULTIPLE"
authorities([{
"type" "AAD"
audience {
"type" "$audienceType"
"tenant_id" "$tenantId"
}
}])
}
return jsonBuilder
}
}
static def isValidConfigurations(microsoftConfig) {
def available = true
def message = ""
if (!microsoftConfig.get("CLIENT_ID")) {
message += 'MICROSOFT:CLIENT_ID is missing or empty\n'
available = false
}
if (!microsoftConfig.get("SIGNATURE_HASH")) {
message += 'MICROSOFT:SIGNATURE_HASH is missing or empty\n'
available = false
}
if (!microsoftConfig.get("AUDIENCE_TYPE")) {
message += 'MICROSOFT:AUDIENCE_TYPE is missing or empty\n'
available = false
}
if (!microsoftConfig.get("TENANT_ID")) {
message += 'MICROSOFT:TENANT_ID is missing or empty\n'
available = false
}
if (!available) {
throw new GradleException(message)
}
return available
}
/**
* Utility method to check that the mandatory file existence, that shows unused by the
* android studio.
* */
static def checkRequiredFileExistence(project) {
def mandatoryFiles = getListOfMandatoryFiles()
for (item in mandatoryFiles) {
if (!new File(project.projectDir.toString() + item.filePath).exists()) {
throw new GradleException(item.errorMessage)
}
}
}
static def getListOfMandatoryFiles() {
def list = new ArrayList()
def file = new MandatoryFile("/src/main/java/org/edx/mobile/googlecast/CastOptionsProvider.java",
"App must implement the OptionsProvider interface to supply options needed to initialize the CastContext singleton.",
"https://developers.google.com/cast/docs/android_sender/integrate#initialize_the_cast_context")
list.add(file)
return list
}
}
/**
* Class having file information that are mandatory for the codebase but android studio shows
* them unused.
*/
class MandatoryFile {
String filePath
String errorMessage
MandatoryFile(filePath, errorMessage, refLink) {
this.filePath = filePath
this.errorMessage = errorMessage + "\nReference: " + refLink
}
}
ext.AndroidHelper = AndroidHelper