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