@hmscore/cordova-plugin-hms-location
Version:
Cordova HMS Location Plugin
176 lines (143 loc) • 6.46 kB
JavaScript
/*
Copyright 2020-2026. Huawei Technologies Co., Ltd. All rights reserved.
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
https://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.
*/
;
var FSUtils = require("./FSUtils");
var ROOT_BUILD_GRADLE_FILE = "platforms/android/build.gradle";
var ROOT_REPOSITORIES_GRADLE_FILE = "platforms/android/repositories.gradle";
var APP_REPOSITORIES_GRADLE_FILE = "platforms/android/app/repositories.gradle";
var APP_ANDROID_MANIFEST_FILE = "platforms/android/app/src/main/AndroidManifest.xml"
var COMMENT = "//This line is added by cordova-plugin-hms-location plugin";
var NEW_LINE = "\n";
var BACKGROUND_SERVICE_NAME = "com.huawei.location.service.BackGroundService";
var FOREGROUND_SERVICE_PERMISSION = "android.permission.FOREGROUND_SERVICE";
var FOREGROUND_SERVICE_LOCATION_PERMISSION = "android.permission.FOREGROUND_SERVICE_LOCATION";
module.exports = function (context) {
if (!FSUtils.exists(ROOT_BUILD_GRADLE_FILE)) {
console.log("root gradle file does not exist. after_plugin_install script wont be executed.");
}
var rootGradleContent = FSUtils.readFile(ROOT_BUILD_GRADLE_FILE, "UTF-8");
var lines = rootGradleContent.split(NEW_LINE);
var depAddedLines = addAGConnectDependency(lines);
var repoAddedLines = addHuaweiRepo(depAddedLines);
depAddedLines = addR8Dependency(depAddedLines);
FSUtils.writeFile(ROOT_BUILD_GRADLE_FILE, depAddedLines.join(NEW_LINE));
FSUtils.writeFile(ROOT_BUILD_GRADLE_FILE, repoAddedLines.join(NEW_LINE));
updateAndroidManifest(APP_ANDROID_MANIFEST_FILE);
updateRepositoriesGradle(ROOT_REPOSITORIES_GRADLE_FILE);
updateRepositoriesGradle(APP_REPOSITORIES_GRADLE_FILE);
};
function updateAndroidManifest(param){
if (FSUtils.exists(param)) {
var data = FSUtils.readFile(param, 'utf8');
var result = data;
result = addPermission(result, FOREGROUND_SERVICE_PERMISSION);
result = addPermission(result, FOREGROUND_SERVICE_LOCATION_PERMISSION);
result = addForegroundServiceType(result);
if(result !== data){
FSUtils.writeFile(param, result, 'utf8', function (err) {
if (err) throw new Error('Unable to write AndroidManifest.xml: ' + err);
})
};
} else {
console.log("Check Android Manifest file exist or not.")
}
}
function addPermission(data, permission) {
if (data.indexOf('android:name="' + permission + '"') !== -1) {
return data;
}
return data.replace(/<manifest([^>]*)>/, '<manifest$1>' + NEW_LINE
+ ' <uses-permission android:name="' + permission + '" />');
}
function addForegroundServiceType(data) {
var serviceRegex = new RegExp('<service\\b(?=[^>]*android:name="' + BACKGROUND_SERVICE_NAME + '")[^>]*(?:/>|>[\\s\\S]*?<\\/service>)');
var serviceMatch = data.match(serviceRegex);
if (serviceMatch) {
var service = serviceMatch[0];
var updatedService = service;
if (updatedService.indexOf('android:foregroundServiceType=') === -1) {
updatedService = updatedService.replace(/\/?>/, ' android:foregroundServiceType="location"$&');
}
if (updatedService.indexOf('android:exported=') === -1) {
updatedService = updatedService.replace(/\/?>/, ' android:exported="false"$&');
}
return data.replace(service, updatedService);
}
return data.replace(/<application([^>]*)>/, '<application$1>' + NEW_LINE
+ ' <service android:name="' + BACKGROUND_SERVICE_NAME + '"' + NEW_LINE
+ ' android:foregroundServiceType="location"' + NEW_LINE
+ ' android:exported="false" />');
}
function addAGConnectDependency(lines) {
var AG_CONNECT_DEPENDENCY = "classpath 'com.huawei.agconnect:agcp:1.9.3.300' " + COMMENT;
var pattern = /(\s*)classpath(\s+)[\',\"]com.android.tools.build:gradle.*[^\]\n]/m;
var index;
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (pattern.test(line)) {
index = i;
break;
}
}
lines.splice(index + 1, 0, AG_CONNECT_DEPENDENCY);
return lines;
}
function addHuaweiRepo(lines) {
var HUAWEI_REPO = "maven { url 'https://developer.huawei.com/repo/' } " + COMMENT;
var pattern = /(\s*)jcenter\(\)/m;
var indexList = [];
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (pattern.test(line)) {
indexList.push(i);
}
}
for (var i = 0; i < indexList.length; i++) {
lines.splice(indexList[i] + 1, 0, HUAWEI_REPO);
if (i < indexList.length - 1) {
indexList[i + 1] = indexList[i + 1] + 1;
}
}
return lines;
}
function updateRepositoriesGradle(file) {
if (FSUtils.exists(file)) {
var repoGradleContent = FSUtils.readFile(file, "UTF-8");
if (repoGradleContent.indexOf("developer.huawei.com/repo") === -1) {
var lastIndexOfCurlyBracket = repoGradleContent.lastIndexOf("}");
repoGradleContent =
repoGradleContent.substring(0, lastIndexOfCurlyBracket) +
" maven { url 'https://developer.huawei.com/repo/' } "+COMMENT+" \n}" +
repoGradleContent.substring(lastIndexOfCurlyBracket + 1);
FSUtils.writeFile(file, repoGradleContent);
}
}
}
function addR8Dependency(lines) {
let R8_DEPENDENCY = 'classpath "com.android.tools:r8:8.3.37" ' + COMMENT;
let pattern = /(\s*)classpath(\s+)[\',\"]com.android.tools.build:gradle.*[^\]\n]/m;
let index;
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
if (pattern.test(line)) {
index = i;
break;
}
}
if (index !== undefined) {
lines.splice(index + 1, 0, R8_DEPENDENCY);
} else {
console.log("Unable to find gradle dependencies block.");
}
return lines;
}