diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/plugin.xml b/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/plugin.xml new file mode 100644 index 00000000..63d1d203 --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/plugin.xml @@ -0,0 +1,29 @@ + + + CordovaHMSGMSCheckPlugin + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/scripts/after_prepare.js b/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/scripts/after_prepare.js new file mode 100644 index 00000000..b3da28cf --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/scripts/after_prepare.js @@ -0,0 +1,37 @@ +#!/usr/bin/env node + +"use strict"; + +/** + * This hook makes sure projects using [cordova-plugin-firebase](https://github.com/arnesson/cordova-plugin-firebase) + * will build properly and have the required key files copied to the proper destinations when the app is build on Ionic Cloud using the package command. + * Credits: https://github.com/arnesson. + */ +var fs = require("fs"); +var path = require("path"); +var utilities = require("./lib/utilities"); + +var config = fs.readFileSync("config.xml").toString(); +var name = utilities.getValue(config, "name"); + +var ANDROID_DIR = "platforms/android"; + +var PLATFORM = { + ANDROID: { + dest: [ANDROID_DIR + "/app/agconnect-services.json"], + src: ["agconnect-services.json"], + }, +}; + +module.exports = function (context) { + //get platform from the context supplied by cordova + var platforms = context.opts.platforms; + // Copy key files to their platform specific folders + if ( + platforms.indexOf("android") !== -1 && + utilities.directoryExists(ANDROID_DIR) + ) { + console.log("Preparing HMS GMS Check Kit on Android"); + // utilities.copyKey(PLATFORM.ANDROID); + } +}; diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/scripts/android/after_plugin_install.js b/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/scripts/android/after_plugin_install.js new file mode 100644 index 00000000..09e73419 --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/scripts/android/after_plugin_install.js @@ -0,0 +1,9 @@ +var helper = require('./helper'); + +module.exports = function(context) { + + // Modify the Gradle build file to add a task that will upload the debug symbols + // at build time. + helper.restoreRootBuildGradle(); + helper.modifyRootBuildGradle(); +}; \ No newline at end of file diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/scripts/android/before_plugin_uninstall.js b/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/scripts/android/before_plugin_uninstall.js new file mode 100644 index 00000000..9c32e552 --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/scripts/android/before_plugin_uninstall.js @@ -0,0 +1,7 @@ +var helper = require('./helper'); + +module.exports = function(context) { + + // Remove the Gradle modifications that were added when the plugin was installed. + helper.restoreRootBuildGradle(); +}; \ No newline at end of file diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/scripts/android/helper.js b/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/scripts/android/helper.js new file mode 100644 index 00000000..cd4b8111 --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/scripts/android/helper.js @@ -0,0 +1,131 @@ +var fs = require("fs"); +var path = require("path"); + +function rootBuildGradleExists() { + var target = path.join("platforms", "android", "build.gradle"); + return fs.existsSync(target); +} + +/* + * Helper function to read the build.gradle that sits at the root of the project + */ +function readRootBuildGradle() { + var target = path.join("platforms", "android", "build.gradle"); + return fs.readFileSync(target, "utf-8"); +} + +/* + * Added a dependency on 'com.google.gms' based on the position of the know 'com.android.tools.build' dependency in the build.gradle + */ +function addDependencies(buildGradle) { + // find the known line to match + var match = buildGradle.match( + /^(\s*)classpath 'com.android.tools.build(.*)/m + ); + var whitespace = match[1]; + + // modify the line to add the necessary dependencies + var agcDependency = + whitespace + "classpath 'com.huawei.agconnect:agcp:1.2.0.300'"; + + var modifiedLine = match[0] + "\n" + agcDependency; + + // modify the actual line + return buildGradle.replace( + /^(\s*)classpath 'com.android.tools.build(.*)/m, + modifiedLine + ); +} + +/* + * Add 'google()' and Crashlytics to the repository repo list + */ +function addRepos(buildGradle) { + // find the known line to match + var match = buildGradle.match(/^(\s*)jcenter\(\)/m); + var whitespace = match[1]; + + // modify the line to add the necessary repo + var huaweiMavenRepo = + whitespace + "maven { url 'http://developer.huawei.com/repo/' }"; + var modifiedLine = match[0] + "\n" + huaweiMavenRepo; + + // modify the actual line + buildGradle = buildGradle.replace(/^(\s*)jcenter\(\)/m, modifiedLine); + + // update the all projects grouping + var allProjectsIndex = buildGradle.indexOf("allprojects"); + if (allProjectsIndex > 0) { + // split the string on allprojects because jcenter is in both groups and we need to modify the 2nd instance + var firstHalfOfFile = buildGradle.substring(0, allProjectsIndex); + var secondHalfOfFile = buildGradle.substring(allProjectsIndex); + + // Add google() to the allprojects section of the string + match = secondHalfOfFile.match(/^(\s*)jcenter\(\)/m); + var huaweiMavenRepo = + whitespace + "maven { url 'http://developer.huawei.com/repo/' }"; + modifiedLine = match[0] + "\n" + huaweiMavenRepo; + // modify the part of the string that is after 'allprojects' + secondHalfOfFile = secondHalfOfFile.replace( + /^(\s*)jcenter\(\)/m, + modifiedLine + ); + + // recombine the modified line + buildGradle = firstHalfOfFile + secondHalfOfFile; + } else { + // this should not happen, but if it does, we should try to add the dependency to the buildscript + match = buildGradle.match(/^(\s*)jcenter\(\)/m); + var huaweiMavenRepo = + whitespace + "maven { url 'http://developer.huawei.com/repo/' }"; + modifiedLine = match[0] + "\n" + huaweiMavenRepo; + // modify the part of the string that is after 'allprojects' + buildGradle = buildGradle.replace(/^(\s*)jcenter\(\)/m, modifiedLine); + } + + return buildGradle; +} + +/* + * Helper function to write to the build.gradle that sits at the root of the project + */ +function writeRootBuildGradle(contents) { + var target = path.join("platforms", "android", "build.gradle"); + fs.writeFileSync(target, contents); +} + +module.exports = { + modifyRootBuildGradle: function () { + // be defensive and don't crash if the file doesn't exist + if (!rootBuildGradleExists) { + return; + } + + var buildGradle = readRootBuildGradle(); + + // Add Google Play Services Dependency + buildGradle = addDependencies(buildGradle); + + // Add Google's Maven Repo + buildGradle = addRepos(buildGradle); + + writeRootBuildGradle(buildGradle); + }, + + restoreRootBuildGradle: function () { + // be defensive and don't crash if the file doesn't exist + if (!rootBuildGradleExists) { + return; + } + + var buildGradle = readRootBuildGradle(); + + // remove any lines we added + buildGradle = buildGradle.replace( + /(?:^|\r?\n)(.*)com.huawei.cordovahmsgmscheckplugin*?(?=$|\r?\n)/g, + "" + ); + + writeRootBuildGradle(buildGradle); + }, +}; diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/scripts/lib/utilities.js b/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/scripts/lib/utilities.js new file mode 100644 index 00000000..89ecc01f --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/scripts/lib/utilities.js @@ -0,0 +1,93 @@ +/** + * Utilities and shared functionality for the build hooks. + */ +var fs = require("fs"); +var path = require("path"); + +fs.ensureDirSync = function (dir) { + if (!fs.existsSync(dir)) { + dir.split(path.sep).reduce(function (currentPath, folder) { + currentPath += folder + path.sep; + if (!fs.existsSync(currentPath)) { + fs.mkdirSync(currentPath); + } + return currentPath; + }, ""); + } +}; + +module.exports = { + /** + * Used to get the name of the application as defined in the config.xml. + * + * @param {object} context - The Cordova context. + * @returns {string} The value of the name element in config.xml. + */ + getAppName: function (context) { + var ConfigParser = context.requireCordovaModule("cordova-lib").configparser; + var config = new ConfigParser("config.xml"); + return config.name(); + }, + + /** + * The ID of the plugin; this should match the ID in plugin.xml. + */ + getPluginId: function () { + return "com.huawei.cordovahmsgmscheckplugin"; + }, + + copyKey: function (platform) { + for (var i = 0; i < platform.src.length; i++) { + var file = platform.src[i]; + if (this.fileExists(file)) { + try { + var contents = fs.readFileSync(file).toString(); + + try { + platform.dest.forEach(function (destinationPath) { + var folder = destinationPath.substring( + 0, + destinationPath.lastIndexOf("/") + ); + fs.ensureDirSync(folder); + fs.writeFileSync(destinationPath, contents); + }); + } catch (e) { + // skip + } + } catch (err) { + console.log(err); + } + + break; + } + } + }, + + getValue: function (config, name) { + var value = config.match( + new RegExp("<" + name + "(.*?)>(.*?)", "i") + ); + if (value && value[2]) { + return value[2]; + } else { + return null; + } + }, + + fileExists: function (path) { + try { + return fs.statSync(path).isFile(); + } catch (e) { + return false; + } + }, + + directoryExists: function (path) { + try { + return fs.statSync(path).isDirectory(); + } catch (e) { + return false; + } + }, +}; diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/src/android/CordovaHMSGMSCheckPlugin.java b/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/src/android/CordovaHMSGMSCheckPlugin.java new file mode 100644 index 00000000..86307e6b --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/src/android/CordovaHMSGMSCheckPlugin.java @@ -0,0 +1,88 @@ +package com.huawei.cordovahmsgmscheckplugin; + +import android.content.Context; +import android.content.Intent; +import android.util.Log; + +import com.huawei.hms.api.HuaweiApiAvailability; +// import com.google.android.gms.common.GoogleApiAvailability; + +import org.apache.cordova.CordovaInterface; +import org.apache.cordova.CordovaInterfaceImpl; +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.CallbackContext; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +/** + * This class echoes a string called from JavaScript. + */ +public class CordovaHMSGMSCheckPlugin extends CordovaPlugin { + + private static final String TAG = CordovaHMSGMSCheckPlugin.class.getSimpleName(); + private CallbackContext mCallbackContext; + + @Override + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { + switch (action) { + case "isHmsAvailable": + mCallbackContext = callbackContext; + cordova.getThreadPool().execute(this::isHmsAvailable); + return true; + case "isGmsAvailable": + // mCallbackContext = callbackContext; + // cordova.getThreadPool().execute(this::isGmsAvailable); + return false; + } + return false; + } + + private void isHmsAvailable() { + boolean isAvailable = false; + Context context = cordova.getContext(); + if (null != cordova.getContext()) { + int result = HuaweiApiAvailability.getInstance().isHuaweiMobileServicesAvailable(context); + isAvailable = (com.huawei.hms.api.ConnectionResult.SUCCESS == result); + } + Log.i("Cordova", "isHmsAvailable: " + isAvailable); + String msg = "false"; + if(isAvailable){ + msg = "true"; + } + outputCallbackContext(0, msg); + } + + // private void isGmsAvailable() { + // boolean isAvailable = false; + // Context context = cordova.getContext(); + // if (null != context) { + // int result = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context); + // isAvailable = (com.google.android.gms.common.ConnectionResult.SUCCESS == result); + // } + // Log.i("Cordova", "isGmsAvailable: " + isAvailable); + // String msg = "false"; + // if(isAvailable){ + // msg = "true"; + // } + // outputCallbackContext(0, msg); + // } + + /** + * @param type 0-success,1-error + * @param msg message + */ + private void outputCallbackContext(int type, String msg) { + if (mCallbackContext != null) { + switch (type) { + case 0: + mCallbackContext.success(msg); + break; + case 1: + mCallbackContext.error(msg); + break; + } + } + } +} diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/src/android/build.gradle b/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/src/android/build.gradle new file mode 100644 index 00000000..30dda1bf --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSGMSCheckPlugin/src/android/build.gradle @@ -0,0 +1,29 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. + +buildscript { + repositories { + google() + jcenter() + // huawei maven + maven { url 'http://developer.huawei.com/repo/' } + } + dependencies { + classpath 'com.android.tools.build:gradle:3.2.0' + // classpath 'com.huawei.agconnect:agcp:1.2.0.300' + // NOTE: Do not place your application dependencies here; they belong + // in the individual module build.gradle files + } +} + +allprojects { + repositories { + google() + jcenter() + // huawei maven + maven { url 'http://developer.huawei.com/repo/' } + } +} + +cdvPluginPostBuildExtras.add({ + apply plugin: 'com.huawei.agconnect' +}) diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/plugin.xml b/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/plugin.xml new file mode 100644 index 00000000..126ffbc8 --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/plugin.xml @@ -0,0 +1,29 @@ + + + CordovaHMSLocationPlugin + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/scripts/after_prepare.js b/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/scripts/after_prepare.js new file mode 100644 index 00000000..adc5a480 --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/scripts/after_prepare.js @@ -0,0 +1,38 @@ +#!/usr/bin/env node + +'use strict'; + +/** + * This hook makes sure projects using [cordova-plugin-firebase](https://github.com/arnesson/cordova-plugin-firebase) + * will build properly and have the required key files copied to the proper destinations when the app is build on Ionic Cloud using the package command. + * Credits: https://github.com/arnesson. + */ +var fs = require('fs'); +var path = require('path'); +var utilities = require("./lib/utilities"); + +var config = fs.readFileSync('config.xml').toString(); +var name = utilities.getValue(config, 'name'); + +var ANDROID_DIR = 'platforms/android'; + +var PLATFORM = { + ANDROID: { + dest: [ + ANDROID_DIR + '/app/agconnect-services.json' + ], + src: [ + 'agconnect-services.json' + ], + } +}; + +module.exports = function (context) { + //get platform from the context supplied by cordova + var platforms = context.opts.platforms; + // Copy key files to their platform specific folders + if (platforms.indexOf('android') !== -1 && utilities.directoryExists(ANDROID_DIR)) { + console.log('Preparing HMS Location Kit on Android'); + utilities.copyKey(PLATFORM.ANDROID); + } +}; \ No newline at end of file diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/scripts/android/after_plugin_install.js b/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/scripts/android/after_plugin_install.js new file mode 100644 index 00000000..09e73419 --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/scripts/android/after_plugin_install.js @@ -0,0 +1,9 @@ +var helper = require('./helper'); + +module.exports = function(context) { + + // Modify the Gradle build file to add a task that will upload the debug symbols + // at build time. + helper.restoreRootBuildGradle(); + helper.modifyRootBuildGradle(); +}; \ No newline at end of file diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/scripts/android/before_plugin_uninstall.js b/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/scripts/android/before_plugin_uninstall.js new file mode 100644 index 00000000..9c32e552 --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/scripts/android/before_plugin_uninstall.js @@ -0,0 +1,7 @@ +var helper = require('./helper'); + +module.exports = function(context) { + + // Remove the Gradle modifications that were added when the plugin was installed. + helper.restoreRootBuildGradle(); +}; \ No newline at end of file diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/scripts/android/helper.js b/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/scripts/android/helper.js new file mode 100644 index 00000000..da715c24 --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/scripts/android/helper.js @@ -0,0 +1,117 @@ +var fs = require("fs"); +var path = require("path"); + +function rootBuildGradleExists() { + var target = path.join("platforms", "android", "build.gradle"); + return fs.existsSync(target); +} + +/* + * Helper function to read the build.gradle that sits at the root of the project + */ +function readRootBuildGradle() { + var target = path.join("platforms", "android", "build.gradle"); + return fs.readFileSync(target, "utf-8"); +} + +/* + * Added a dependency on 'com.google.gms' based on the position of the know 'com.android.tools.build' dependency in the build.gradle + */ +function addDependencies(buildGradle) { + // find the known line to match + var match = buildGradle.match(/^(\s*)classpath 'com.android.tools.build(.*)/m); + var whitespace = match[1]; + + // modify the line to add the necessary dependencies + var agcDependency = whitespace + 'classpath \'com.huawei.agconnect:agcp:1.2.0.300\'' + + var modifiedLine = match[0] + '\n' + agcDependency; + + // modify the actual line + return buildGradle.replace(/^(\s*)classpath 'com.android.tools.build(.*)/m, modifiedLine); +} + +/* + * Add 'google()' and Crashlytics to the repository repo list + */ +function addRepos(buildGradle) { + // find the known line to match + var match = buildGradle.match(/^(\s*)jcenter\(\)/m); + var whitespace = match[1]; + + // modify the line to add the necessary repo + var huaweiMavenRepo = whitespace + 'maven { url \'http://developer.huawei.com/repo/\' }' + var modifiedLine = match[0] + '\n' + huaweiMavenRepo; + + // modify the actual line + buildGradle = buildGradle.replace(/^(\s*)jcenter\(\)/m, modifiedLine); + + // update the all projects grouping + var allProjectsIndex = buildGradle.indexOf('allprojects'); + if (allProjectsIndex > 0) { + // split the string on allprojects because jcenter is in both groups and we need to modify the 2nd instance + var firstHalfOfFile = buildGradle.substring(0, allProjectsIndex); + var secondHalfOfFile = buildGradle.substring(allProjectsIndex); + + // Add google() to the allprojects section of the string + match = secondHalfOfFile.match(/^(\s*)jcenter\(\)/m); + var huaweiMavenRepo = whitespace + 'maven { url \'http://developer.huawei.com/repo/\' }' + modifiedLine = match[0] + '\n' + huaweiMavenRepo; + // modify the part of the string that is after 'allprojects' + secondHalfOfFile = secondHalfOfFile.replace(/^(\s*)jcenter\(\)/m, modifiedLine); + + // recombine the modified line + buildGradle = firstHalfOfFile + secondHalfOfFile; + } else { + // this should not happen, but if it does, we should try to add the dependency to the buildscript + match = buildGradle.match(/^(\s*)jcenter\(\)/m); + var huaweiMavenRepo = whitespace + 'maven { url \'http://developer.huawei.com/repo/\' }' + modifiedLine = match[0] + '\n' + huaweiMavenRepo; + // modify the part of the string that is after 'allprojects' + buildGradle = buildGradle.replace(/^(\s*)jcenter\(\)/m, modifiedLine); + } + + return buildGradle; +} + +/* + * Helper function to write to the build.gradle that sits at the root of the project + */ +function writeRootBuildGradle(contents) { + var target = path.join("platforms", "android", "build.gradle"); + fs.writeFileSync(target, contents); +} + +module.exports = { + + modifyRootBuildGradle: function() { + // be defensive and don't crash if the file doesn't exist + if (!rootBuildGradleExists) { + return; + } + + var buildGradle = readRootBuildGradle(); + + // Add Google Play Services Dependency + buildGradle = addDependencies(buildGradle); + + // Add Google's Maven Repo + buildGradle = addRepos(buildGradle); + + writeRootBuildGradle(buildGradle); + }, + + restoreRootBuildGradle: function() { + // be defensive and don't crash if the file doesn't exist + if (!rootBuildGradleExists) { + return; + } + + var buildGradle = readRootBuildGradle(); + + // remove any lines we added + buildGradle = buildGradle.replace(/(?:^|\r?\n)(.*)com.huawei.cordovahmspushplugin*?(?=$|\r?\n)/g, ''); + + writeRootBuildGradle(buildGradle); + } +}; \ No newline at end of file diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/scripts/lib/utilities.js b/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/scripts/lib/utilities.js new file mode 100644 index 00000000..015f6831 --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/scripts/lib/utilities.js @@ -0,0 +1,88 @@ +/** + * Utilities and shared functionality for the build hooks. + */ +var fs = require('fs'); +var path = require("path"); + +fs.ensureDirSync = function (dir) { + if (!fs.existsSync(dir)) { + dir.split(path.sep).reduce(function (currentPath, folder) { + currentPath += folder + path.sep; + if (!fs.existsSync(currentPath)) { + fs.mkdirSync(currentPath); + } + return currentPath; + }, ''); + } +}; + +module.exports = { + /** + * Used to get the name of the application as defined in the config.xml. + * + * @param {object} context - The Cordova context. + * @returns {string} The value of the name element in config.xml. + */ + getAppName: function (context) { + var ConfigParser = context.requireCordovaModule("cordova-lib").configparser; + var config = new ConfigParser("config.xml"); + return config.name(); + }, + + /** + * The ID of the plugin; this should match the ID in plugin.xml. + */ + getPluginId: function () { + return "com.huawei.cordovahmspushplugin"; + }, + + copyKey: function (platform) { + for (var i = 0; i < platform.src.length; i++) { + var file = platform.src[i]; + if (this.fileExists(file)) { + try { + var contents = fs.readFileSync(file).toString(); + + try { + platform.dest.forEach(function (destinationPath) { + var folder = destinationPath.substring(0, destinationPath.lastIndexOf('/')); + fs.ensureDirSync(folder); + fs.writeFileSync(destinationPath, contents); + }); + } catch (e) { + // skip + } + } catch (err) { + console.log(err); + } + + break; + } + } + }, + + getValue: function (config, name) { + var value = config.match(new RegExp('<' + name + '(.*?)>(.*?)', 'i')); + if (value && value[2]) { + return value[2] + } else { + return null + } + }, + + fileExists: function (path) { + try { + return fs.statSync(path).isFile(); + } catch (e) { + return false; + } + }, + + directoryExists: function (path) { + try { + return fs.statSync(path).isDirectory(); + } catch (e) { + return false; + } + } +}; \ No newline at end of file diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/src/android/CordovaHMSLocationPlugin.java b/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/src/android/CordovaHMSLocationPlugin.java new file mode 100644 index 00000000..caef5fd0 --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/src/android/CordovaHMSLocationPlugin.java @@ -0,0 +1,185 @@ +package com.huawei.cordovahmslocationplugin; + +import android.Manifest; +import android.content.IntentSender; +import android.location.Location; +import android.os.Build; +import android.os.Looper; +import android.util.Log; + +import com.huawei.hms.common.ApiException; +import com.huawei.hms.common.ResolvableApiException; +import com.huawei.hms.location.FusedLocationProviderClient; +import com.huawei.hms.location.LocationCallback; +import com.huawei.hms.location.LocationRequest; +import com.huawei.hms.location.LocationResult; +import com.huawei.hms.location.LocationServices; +import com.huawei.hms.location.LocationSettingsRequest; +import com.huawei.hms.location.LocationSettingsStatusCodes; +import com.huawei.hms.location.SettingsClient; + +import org.apache.cordova.CallbackContext; +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.PluginResult; +import org.json.JSONArray; +import org.json.JSONException; + +/** + * + */ +public class CordovaHMSLocationPlugin extends CordovaPlugin { + + private static final String TAG = CordovaHMSLocationPlugin.class.getSimpleName(); + + private FusedLocationProviderClient fusedLocationProviderClient; + private LocationRequest mLocationRequest; + private LocationCallback mLocationCallback; + private CallbackContext mCallbackContext; + + @Override + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { + requestPermission(); + if (fusedLocationProviderClient == null) { + fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(cordova.getContext()); + } + switch (action) { + case "requestLocation": + this.setLocationRequest(); + this.setLocationCallback(); + this.checkLocationSetting(callbackContext); + return true; + case "removeLocation": + this.stopLocation(); + return true; + case "getLastlocation": + this.getLastlocation(callbackContext); + return true; + default: + return false; + } + } + + private void returnLocation(Location location) { + if (mCallbackContext != null) { + Log.d(TAG, "returnLocation"); + String message = location.getLatitude() + "," + location.getLongitude(); + PluginResult result = new PluginResult(PluginResult.Status.OK, message); + result.setKeepCallback(true); + mCallbackContext.sendPluginResult(result); + } + } + + private void getLastlocation(CallbackContext callbackContext) { + fusedLocationProviderClient.getLastLocation().addOnSuccessListener(location -> { + Log.d(TAG, "getLastlocation success"); + if (location == null) { + return; + } + // Location对象的处理逻辑 + String message = location.getLatitude() + "," + location.getLongitude(); + callbackContext.success(message); + }).addOnFailureListener(e -> { + // 异常处理逻辑 + callbackContext.error("getLastlocation fail"); + }); + } + + private void stopLocation() { + if (fusedLocationProviderClient == null) { + Log.d(TAG, "fusedLocationProviderClient is null"); + return; + } + // 注意:停止位置更新时,mLocationCallback必须与requestLocationUpdates方法中的LocationCallback参数为同一对象。 + fusedLocationProviderClient.removeLocationUpdates(mLocationCallback) + .addOnSuccessListener(aVoid -> { + // 停止位置更新成功 + Log.d(TAG, "stop success"); + mCallbackContext = null; + }) + .addOnFailureListener(e -> { + // 停止位置更新失败 + Log.d(TAG, "stop fail"); + }); + } + + private void setLocationCallback() { + mLocationCallback = new LocationCallback() { + @Override + public void onLocationResult(LocationResult locationResult) { + if (locationResult != null) { + //处理位置回调结果 + if (mLocationCallback != null) { + Log.d(TAG, "onLocationResult"); + Location location = locationResult.getLocations().get(0); + returnLocation(location); + } + } + } + }; + } + + private void setLocationRequest() { + mLocationRequest = new LocationRequest(); + // 设置位置更新的间隔(毫秒为单位) + mLocationRequest.setInterval(10000); +// mLocationRequest.setNumUpdates(1); + // 设置权重 + mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); + } + + private void checkLocationSetting(CallbackContext callbackContext) { + SettingsClient settingsClient = LocationServices.getSettingsClient(cordova.getContext()); + LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); + builder.addLocationRequest(mLocationRequest); + LocationSettingsRequest locationSettingsRequest = builder.build(); + // 检查设备定位设置 + settingsClient.checkLocationSettings(locationSettingsRequest) + .addOnSuccessListener(locationSettingsResponse -> { + + // 设置满足定位条件,再发起位置请求 + fusedLocationProviderClient + .requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.getMainLooper()) + .addOnSuccessListener(aVoid -> { + // 接口调用成功的处理 + Log.d(TAG, "request success"); + mCallbackContext = callbackContext; + }); + }) + .addOnFailureListener(e -> { + // 设置不满足定位条件 + int statusCode = ((ApiException) e).getStatusCode(); + switch (statusCode) { + case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: + try { + ResolvableApiException rae = (ResolvableApiException) e; + // 调用startResolutionForResult可以弹窗提示用户打开相应权限 + rae.startResolutionForResult(cordova.getActivity(), 0); + } catch (IntentSender.SendIntentException sie) { + Log.d(TAG, sie.getMessage()); + } + break; + } + }); + } + + private void requestPermission() { + if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) { + Log.i(TAG, "sdk < 29 Q"); + if (!cordova.hasPermission(Manifest.permission.ACCESS_FINE_LOCATION) + || !cordova.hasPermission(Manifest.permission.ACCESS_COARSE_LOCATION)) { + String[] strings = + {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}; + cordova.requestPermissions(this, 1, strings); + } + } else { + if (!cordova.hasPermission(Manifest.permission.ACCESS_FINE_LOCATION) + || !cordova.hasPermission(Manifest.permission.ACCESS_COARSE_LOCATION) + || !cordova.hasPermission("android.permission.ACCESS_BACKGROUND_LOCATION")) { + String[] strings = {android.Manifest.permission.ACCESS_FINE_LOCATION, + android.Manifest.permission.ACCESS_COARSE_LOCATION, + "android.permission.ACCESS_BACKGROUND_LOCATION"}; + cordova.requestPermissions(this, 2, strings); + } + } + } +} diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/src/android/build.gradle b/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/src/android/build.gradle new file mode 100644 index 00000000..23296fb7 --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSLocationPlugin/src/android/build.gradle @@ -0,0 +1,29 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. + +buildscript { + repositories { + google() + jcenter() + // huawei maven + maven { url 'http://developer.huawei.com/repo/' } + } + dependencies { + classpath 'com.android.tools.build:gradle:3.2.0' + classpath 'com.huawei.agconnect:agcp:1.2.0.300' + // NOTE: Do not place your application dependencies here; they belong + // in the individual module build.gradle files + } +} + +allprojects { + repositories { + google() + jcenter() + // huawei maven + maven { url 'http://developer.huawei.com/repo/' } + } +} + +cdvPluginPostBuildExtras.add({ + apply plugin: 'com.huawei.agconnect' +}) diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/plugin.xml b/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/plugin.xml new file mode 100644 index 00000000..d7ccbfa7 --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/plugin.xml @@ -0,0 +1,38 @@ + + + + + + + CordovaHMSPushPlugin + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/scripts/after_prepare.js b/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/scripts/after_prepare.js new file mode 100644 index 00000000..ab6c7b7e --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/scripts/after_prepare.js @@ -0,0 +1,38 @@ +#!/usr/bin/env node + +'use strict'; + +/** + * This hook makes sure projects using [cordova-plugin-firebase](https://github.com/arnesson/cordova-plugin-firebase) + * will build properly and have the required key files copied to the proper destinations when the app is build on Ionic Cloud using the package command. + * Credits: https://github.com/arnesson. + */ +var fs = require('fs'); +var path = require('path'); +var utilities = require("./lib/utilities"); + +var config = fs.readFileSync('config.xml').toString(); +var name = utilities.getValue(config, 'name'); + +var ANDROID_DIR = 'platforms/android'; + +var PLATFORM = { + ANDROID: { + dest: [ + ANDROID_DIR + '/app/agconnect-services.json' + ], + src: [ + 'agconnect-services.json' + ], + } +}; + +module.exports = function (context) { + //get platform from the context supplied by cordova + var platforms = context.opts.platforms; + // Copy key files to their platform specific folders + if (platforms.indexOf('android') !== -1 && utilities.directoryExists(ANDROID_DIR)) { + console.log('Preparing HMS Push Kit on Android'); + utilities.copyKey(PLATFORM.ANDROID); + } +}; \ No newline at end of file diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/scripts/android/after_plugin_install.js b/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/scripts/android/after_plugin_install.js new file mode 100644 index 00000000..09e73419 --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/scripts/android/after_plugin_install.js @@ -0,0 +1,9 @@ +var helper = require('./helper'); + +module.exports = function(context) { + + // Modify the Gradle build file to add a task that will upload the debug symbols + // at build time. + helper.restoreRootBuildGradle(); + helper.modifyRootBuildGradle(); +}; \ No newline at end of file diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/scripts/android/before_plugin_uninstall.js b/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/scripts/android/before_plugin_uninstall.js new file mode 100644 index 00000000..9c32e552 --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/scripts/android/before_plugin_uninstall.js @@ -0,0 +1,7 @@ +var helper = require('./helper'); + +module.exports = function(context) { + + // Remove the Gradle modifications that were added when the plugin was installed. + helper.restoreRootBuildGradle(); +}; \ No newline at end of file diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/scripts/android/helper.js b/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/scripts/android/helper.js new file mode 100644 index 00000000..da715c24 --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/scripts/android/helper.js @@ -0,0 +1,117 @@ +var fs = require("fs"); +var path = require("path"); + +function rootBuildGradleExists() { + var target = path.join("platforms", "android", "build.gradle"); + return fs.existsSync(target); +} + +/* + * Helper function to read the build.gradle that sits at the root of the project + */ +function readRootBuildGradle() { + var target = path.join("platforms", "android", "build.gradle"); + return fs.readFileSync(target, "utf-8"); +} + +/* + * Added a dependency on 'com.google.gms' based on the position of the know 'com.android.tools.build' dependency in the build.gradle + */ +function addDependencies(buildGradle) { + // find the known line to match + var match = buildGradle.match(/^(\s*)classpath 'com.android.tools.build(.*)/m); + var whitespace = match[1]; + + // modify the line to add the necessary dependencies + var agcDependency = whitespace + 'classpath \'com.huawei.agconnect:agcp:1.2.0.300\'' + + var modifiedLine = match[0] + '\n' + agcDependency; + + // modify the actual line + return buildGradle.replace(/^(\s*)classpath 'com.android.tools.build(.*)/m, modifiedLine); +} + +/* + * Add 'google()' and Crashlytics to the repository repo list + */ +function addRepos(buildGradle) { + // find the known line to match + var match = buildGradle.match(/^(\s*)jcenter\(\)/m); + var whitespace = match[1]; + + // modify the line to add the necessary repo + var huaweiMavenRepo = whitespace + 'maven { url \'http://developer.huawei.com/repo/\' }' + var modifiedLine = match[0] + '\n' + huaweiMavenRepo; + + // modify the actual line + buildGradle = buildGradle.replace(/^(\s*)jcenter\(\)/m, modifiedLine); + + // update the all projects grouping + var allProjectsIndex = buildGradle.indexOf('allprojects'); + if (allProjectsIndex > 0) { + // split the string on allprojects because jcenter is in both groups and we need to modify the 2nd instance + var firstHalfOfFile = buildGradle.substring(0, allProjectsIndex); + var secondHalfOfFile = buildGradle.substring(allProjectsIndex); + + // Add google() to the allprojects section of the string + match = secondHalfOfFile.match(/^(\s*)jcenter\(\)/m); + var huaweiMavenRepo = whitespace + 'maven { url \'http://developer.huawei.com/repo/\' }' + modifiedLine = match[0] + '\n' + huaweiMavenRepo; + // modify the part of the string that is after 'allprojects' + secondHalfOfFile = secondHalfOfFile.replace(/^(\s*)jcenter\(\)/m, modifiedLine); + + // recombine the modified line + buildGradle = firstHalfOfFile + secondHalfOfFile; + } else { + // this should not happen, but if it does, we should try to add the dependency to the buildscript + match = buildGradle.match(/^(\s*)jcenter\(\)/m); + var huaweiMavenRepo = whitespace + 'maven { url \'http://developer.huawei.com/repo/\' }' + modifiedLine = match[0] + '\n' + huaweiMavenRepo; + // modify the part of the string that is after 'allprojects' + buildGradle = buildGradle.replace(/^(\s*)jcenter\(\)/m, modifiedLine); + } + + return buildGradle; +} + +/* + * Helper function to write to the build.gradle that sits at the root of the project + */ +function writeRootBuildGradle(contents) { + var target = path.join("platforms", "android", "build.gradle"); + fs.writeFileSync(target, contents); +} + +module.exports = { + + modifyRootBuildGradle: function() { + // be defensive and don't crash if the file doesn't exist + if (!rootBuildGradleExists) { + return; + } + + var buildGradle = readRootBuildGradle(); + + // Add Google Play Services Dependency + buildGradle = addDependencies(buildGradle); + + // Add Google's Maven Repo + buildGradle = addRepos(buildGradle); + + writeRootBuildGradle(buildGradle); + }, + + restoreRootBuildGradle: function() { + // be defensive and don't crash if the file doesn't exist + if (!rootBuildGradleExists) { + return; + } + + var buildGradle = readRootBuildGradle(); + + // remove any lines we added + buildGradle = buildGradle.replace(/(?:^|\r?\n)(.*)com.huawei.cordovahmspushplugin*?(?=$|\r?\n)/g, ''); + + writeRootBuildGradle(buildGradle); + } +}; \ No newline at end of file diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/scripts/lib/utilities.js b/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/scripts/lib/utilities.js new file mode 100644 index 00000000..015f6831 --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/scripts/lib/utilities.js @@ -0,0 +1,88 @@ +/** + * Utilities and shared functionality for the build hooks. + */ +var fs = require('fs'); +var path = require("path"); + +fs.ensureDirSync = function (dir) { + if (!fs.existsSync(dir)) { + dir.split(path.sep).reduce(function (currentPath, folder) { + currentPath += folder + path.sep; + if (!fs.existsSync(currentPath)) { + fs.mkdirSync(currentPath); + } + return currentPath; + }, ''); + } +}; + +module.exports = { + /** + * Used to get the name of the application as defined in the config.xml. + * + * @param {object} context - The Cordova context. + * @returns {string} The value of the name element in config.xml. + */ + getAppName: function (context) { + var ConfigParser = context.requireCordovaModule("cordova-lib").configparser; + var config = new ConfigParser("config.xml"); + return config.name(); + }, + + /** + * The ID of the plugin; this should match the ID in plugin.xml. + */ + getPluginId: function () { + return "com.huawei.cordovahmspushplugin"; + }, + + copyKey: function (platform) { + for (var i = 0; i < platform.src.length; i++) { + var file = platform.src[i]; + if (this.fileExists(file)) { + try { + var contents = fs.readFileSync(file).toString(); + + try { + platform.dest.forEach(function (destinationPath) { + var folder = destinationPath.substring(0, destinationPath.lastIndexOf('/')); + fs.ensureDirSync(folder); + fs.writeFileSync(destinationPath, contents); + }); + } catch (e) { + // skip + } + } catch (err) { + console.log(err); + } + + break; + } + } + }, + + getValue: function (config, name) { + var value = config.match(new RegExp('<' + name + '(.*?)>(.*?)', 'i')); + if (value && value[2]) { + return value[2] + } else { + return null + } + }, + + fileExists: function (path) { + try { + return fs.statSync(path).isFile(); + } catch (e) { + return false; + } + }, + + directoryExists: function (path) { + try { + return fs.statSync(path).isDirectory(); + } catch (e) { + return false; + } + } +}; \ No newline at end of file diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/src/android/CordovaHMSPushPlugin.java b/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/src/android/CordovaHMSPushPlugin.java new file mode 100644 index 00000000..d2ef10b4 --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/src/android/CordovaHMSPushPlugin.java @@ -0,0 +1,112 @@ +package com.huawei.cordovahmspushplugin; + +import android.text.TextUtils; +import android.util.Log; + +import com.huawei.agconnect.config.AGConnectServicesConfig; +import com.huawei.hms.aaid.HmsInstanceId; +import com.huawei.hms.push.HmsMessaging; +import com.huawei.hmf.tasks.OnCompleteListener; +import com.huawei.hmf.tasks.Task; + +import org.apache.cordova.CallbackContext; +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.PluginResult; +import org.json.JSONArray; +import org.json.JSONException; + +/** + * This class echoes a string called from JavaScript. + */ +public class CordovaHMSPushPlugin extends CordovaPlugin { + + private static final String TAG = CordovaHMSPushPlugin.class.getSimpleName(); + + private static CallbackContext mCallbackContext; + private static CallbackContext mTokenCallback; + + @Override + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { + switch (action) { + case "getToken": + this.getToken(callbackContext); + return true; + case "getMessageCallback": + Log.d(TAG, "getMessageCallback"); + mCallbackContext = callbackContext; + return true; + case "subscribeTopic": + Log.d(TAG, "subscribeTopic"); + try { + String topic = args.getString(0); + this.subscribeTopic(topic, callbackContext); + } catch (JSONException e) { + return true; + } + return true; + default: + return false; + } + } + + public static void returnMessage(String message) { + if (mCallbackContext != null) { + Log.d(TAG, "returnMessage"); + PluginResult result = new PluginResult(PluginResult.Status.OK, message); + result.setKeepCallback(true); + mCallbackContext.sendPluginResult(result); + } + } + + public static void returnToken(String token) { + if (mTokenCallback != null) { + mTokenCallback.success(token); + mTokenCallback = null; + } + } + + /** + * get push token + */ + private void getToken(CallbackContext callbackContext) { + Log.i(TAG, "get token: begin"); + + try { + String appId = AGConnectServicesConfig.fromContext(cordova.getContext()).getString("client/app_id"); + String pushToken = HmsInstanceId.getInstance(cordova.getContext()).getToken(appId, "HCM"); + if (!TextUtils.isEmpty(pushToken)) { + Log.i(TAG, "get token:" + pushToken); + callbackContext.success(pushToken); + }else { + mTokenCallback = callbackContext; + } + } catch (Exception e) { + Log.e(TAG, "getToken Failed, " + e); + callbackContext.error("getToken Failed, error : " + e.getMessage()); + } + } + + public void subscribeTopic(String topic, final CallbackContext callBack) { + // callBack.success("user subscribe to topic named as: "+ topic); + if (topic == null || topic.toString().equals("")) { + callBack.error("topic is empty!"); + return; + } + try { + HmsMessaging.getInstance(cordova.getContext()).subscribe(topic). + addOnCompleteListener(new OnCompleteListener() { + @Override + public void onComplete(Task task) { + if (task.isSuccessful()) { + callBack.success("user subscribe to topic: "+ topic); + } else { + callBack.error("getToken Failed, error : " + task.getException().getMessage()); + } + } + }); + } catch (Exception e) { + callBack.error("getToken Failed, error : " + e.getMessage()); + } + } + +} diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/src/android/MessageService.java b/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/src/android/MessageService.java new file mode 100644 index 00000000..d32542ef --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/src/android/MessageService.java @@ -0,0 +1,32 @@ +package com.huawei.cordovahmspushplugin; + +import com.huawei.cordovahmspushplugin.CordovaHMSPushPlugin; +import com.huawei.hms.push.HmsMessageService; +import com.huawei.hms.push.RemoteMessage; + +import android.util.Log; + +public class MessageService extends HmsMessageService { + + private static final String TAG = MessageService.class.getSimpleName(); + + @Override + public void onMessageReceived(RemoteMessage remoteMessage) { + super.onMessageReceived(remoteMessage); + Log.d(TAG, "onMessageReceived"); + if (remoteMessage != null) { + String message = remoteMessage.getData(); + Log.d(TAG, message); + CordovaHMSPushPlugin.returnMessage(message); + } + } + + @Override + public void onNewToken(String s) { + super.onNewToken(s); + if (s != null) { + Log.d(TAG, "token:" + s); + CordovaHMSPushPlugin.returnToken(s); + } + } +} diff --git a/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/src/android/build.gradle b/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/src/android/build.gradle new file mode 100644 index 00000000..23296fb7 --- /dev/null +++ b/Mohem/CordovaHMSPlugin/CordovaHMSPushPlugin/src/android/build.gradle @@ -0,0 +1,29 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. + +buildscript { + repositories { + google() + jcenter() + // huawei maven + maven { url 'http://developer.huawei.com/repo/' } + } + dependencies { + classpath 'com.android.tools.build:gradle:3.2.0' + classpath 'com.huawei.agconnect:agcp:1.2.0.300' + // NOTE: Do not place your application dependencies here; they belong + // in the individual module build.gradle files + } +} + +allprojects { + repositories { + google() + jcenter() + // huawei maven + maven { url 'http://developer.huawei.com/repo/' } + } +} + +cdvPluginPostBuildExtras.add({ + apply plugin: 'com.huawei.agconnect' +}) diff --git a/Mohem/WifiWizard2.java b/Mohem/WifiWizard2.java new file mode 100644 index 00000000..03647d0a --- /dev/null +++ b/Mohem/WifiWizard2.java @@ -0,0 +1,1891 @@ +// /* +// * Copyright 2018 Myles McNamara +// * +// * 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. +// */ +// package wifiwizard2; + +// import org.apache.cordova.*; + +// import java.util.List; +// import java.lang.InterruptedException; + +// import org.json.JSONArray; +// import org.json.JSONException; +// import org.json.JSONObject; + +// import android.annotation.SuppressLint; +// import android.app.PendingIntent; +// import android.content.pm.PackageManager; +// import android.content.BroadcastReceiver; +// import android.content.Intent; +// import android.content.IntentFilter; + +// import android.net.Network; +// import android.net.NetworkCapabilities; +// import android.net.NetworkInfo; +// import android.net.NetworkRequest; +// import android.net.DhcpInfo; + +// import android.net.wifi.WifiManager; +// import android.net.wifi.WifiConfiguration; +// import android.net.wifi.ScanResult; +// import android.net.wifi.WifiInfo; +// import android.net.wifi.SupplicantState; +// import android.net.wifi.WifiNetworkSpecifier; +// import android.net.ConnectivityManager; + +// import android.content.Context; +// import android.os.AsyncTask; +// import android.util.Log; +// import android.os.Build.VERSION; +// import android.os.PatternMatcher; + +// import java.net.URL; +// import java.net.InetAddress; +// import java.net.Inet4Address; +// import java.net.InterfaceAddress; +// import java.net.NetworkInterface; +// import java.net.HttpURLConnection; + +// import java.net.UnknownHostException; + +// public class WifiWizard2 extends CordovaPlugin { +// private boolean hiddenSSID = false; + +// private static final String TAG = "WifiWizard2"; +// private static final int API_VERSION = VERSION.SDK_INT; + +// private static final String ADD_NETWORK = "add"; +// private static final String REMOVE_NETWORK = "remove"; +// private static final String CONNECT_NETWORK = "connect"; +// private static final String DISCONNECT_NETWORK = "disconnectNetwork"; +// private static final String DISCONNECT = "disconnect"; +// private static final String LIST_NETWORKS = "listNetworks"; +// private static final String START_SCAN = "startScan"; +// private static final String GET_SCAN_RESULTS = "getScanResults"; +// private static final String GET_CONNECTED_SSID = "getConnectedSSID"; +// private static final String GET_CONNECTED_BSSID = "getConnectedBSSID"; +// private static final String GET_CONNECTED_NETWORKID = "getConnectedNetworkID"; +// private static final String IS_WIFI_ENABLED = "isWifiEnabled"; +// private static final String SET_WIFI_ENABLED = "setWifiEnabled"; +// private static final String SCAN = "scan"; +// private static final String ENABLE_NETWORK = "enable"; +// private static final String DISABLE_NETWORK = "disable"; +// private static final String GET_SSID_NET_ID = "getSSIDNetworkID"; +// private static final String REASSOCIATE = "reassociate"; +// private static final String RECONNECT = "reconnect"; +// private static final String REQUEST_FINE_LOCATION = "requestFineLocation"; +// private static final String GET_WIFI_IP_ADDRESS = "getWifiIP"; +// private static final String GET_WIFI_ROUTER_IP_ADDRESS = "getWifiRouterIP"; +// private static final String CAN_PING_WIFI_ROUTER = "canPingWifiRouter"; +// private static final String CAN_CONNECT_TO_ROUTER = "canConnectToRouter"; +// private static final String CAN_CONNECT_TO_INTERNET = "canConnectToInternet"; +// private static final String IS_CONNECTED_TO_INTERNET = "isConnectedToInternet"; +// private static final String RESET_BIND_ALL = "resetBindAll"; +// private static final String SET_BIND_ALL = "setBindAll"; +// private static final String GET_WIFI_IP_INFO = "getWifiIPInfo"; + + + +// private static final int SCAN_RESULTS_CODE = 0; // Permissions request code for getScanResults() +// private static final int SCAN_CODE = 1; // Permissions request code for scan() +// private static final int LOCATION_REQUEST_CODE = 2; // Permissions request code +// private static final int WIFI_SERVICE_INFO_CODE = 3; +// private static final String ACCESS_FINE_LOCATION = android.Manifest.permission.ACCESS_FINE_LOCATION; + +// private static int LAST_NET_ID = -1; +// // This is for when SSID or BSSID is requested but permissions have not been granted for location +// // we store whether or not BSSID was requested, to recall the getWifiServiceInfo fn after permissions are granted +// private static boolean bssidRequested = false; + +// private WifiManager wifiManager; +// private CallbackContext callbackContext; +// private JSONArray passedData; + +// private ConnectivityManager connectivityManager; +// private ConnectivityManager.NetworkCallback networkCallback; + +// // Store AP, previous, and desired wifi info +// private AP previous, desired; + +// private PendingIntent pending_intent_net_req_received; +// private final BroadcastReceiver networkChangedReceiver = new NetworkChangedReceiver(); +// private static final IntentFilter NETWORK_STATE_CHANGED_FILTER = new IntentFilter(); + +// static { +// NETWORK_STATE_CHANGED_FILTER.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); +// } + +// /** +// * WEP has two kinds of password, a hex value that specifies the key or a character string used to +// * generate the real hex. This checks what kind of password has been supplied. The checks +// * correspond to WEP40, WEP104 & WEP232 +// */ +// private static boolean getHexKey(String s) { +// if (s == null) { +// return false; +// } + +// int len = s.length(); +// if (len != 10 && len != 26 && len != 58) { +// return false; +// } + +// for (int i = 0; i < len; ++i) { +// char c = s.charAt(i); +// if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) { +// return false; +// } +// } +// return true; +// } + +// @Override +// public void initialize(CordovaInterface cordova, CordovaWebView webView) { +// super.initialize(cordova, webView); +// this.wifiManager = (WifiManager) cordova.getActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE); +// this.connectivityManager = (ConnectivityManager) cordova.getActivity().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); +// } + +// @Override +// public boolean execute(String action, JSONArray data, CallbackContext callbackContext) +// throws JSONException { + +// this.callbackContext = callbackContext; +// this.passedData = data; + +// // Actions that do not require WiFi to be enabled +// if (action.equals(IS_WIFI_ENABLED)) { +// this.isWifiEnabled(callbackContext); +// return true; +// } else if (action.equals(SET_WIFI_ENABLED)) { +// this.setWifiEnabled(callbackContext, data); +// return true; +// } else if (action.equals(REQUEST_FINE_LOCATION)) { +// this.requestLocationPermission(LOCATION_REQUEST_CODE); +// return true; +// } else if (action.equals(GET_WIFI_ROUTER_IP_ADDRESS)) { + +// String ip = getWiFiRouterIP(); + +// if ( ip == null || ip.equals("0.0.0.0")) { +// callbackContext.error("NO_VALID_ROUTER_IP_FOUND"); +// return true; +// } else { +// callbackContext.success(ip); +// return true; +// } + +// } else if (action.equals(GET_WIFI_IP_ADDRESS) || action.equals(GET_WIFI_IP_INFO)) { +// String[] ipInfo = getWiFiIPAddress(); +// String ip = ipInfo[0]; +// String subnet = ipInfo[1]; +// if (ip == null || ip.equals("0.0.0.0")) { +// callbackContext.error("NO_VALID_IP_IDENTIFIED"); +// return true; +// } + +// // Return only IP address +// if( action.equals( GET_WIFI_IP_ADDRESS ) ){ +// callbackContext.success(ip); +// return true; +// } + +// // Return Wifi IP Info (subnet and IP as JSON object) +// JSONObject result = new JSONObject(); + +// result.put("ip", ip); +// result.put("subnet", subnet); + +// callbackContext.success(result); +// return true; +// } + +// boolean wifiIsEnabled = verifyWifiEnabled(); +// if (!wifiIsEnabled) { +// callbackContext.error("WIFI_NOT_ENABLED"); +// return true; // Even though enable wifi failed, we still return true and handle error in callback +// } + +// // Actions that DO require WiFi to be enabled +// if (action.equals(ADD_NETWORK)) { +// this.add(callbackContext, data); +// } else if (action.equals(IS_CONNECTED_TO_INTERNET)) { +// this.canConnectToInternet(callbackContext, true); +// } else if (action.equals(CAN_CONNECT_TO_INTERNET)) { +// this.canConnectToInternet(callbackContext, false); +// } else if (action.equals(CAN_PING_WIFI_ROUTER)) { +// this.canConnectToRouter(callbackContext, true); +// } else if (action.equals(CAN_CONNECT_TO_ROUTER)) { +// this.canConnectToRouter(callbackContext, false); +// } else if (action.equals(ENABLE_NETWORK)) { +// this.enable(callbackContext, data); +// } else if (action.equals(DISABLE_NETWORK)) { +// this.disable(callbackContext, data); +// } else if (action.equals(GET_SSID_NET_ID)) { +// this.getSSIDNetworkID(callbackContext, data); +// } else if (action.equals(REASSOCIATE)) { +// this.reassociate(callbackContext); +// } else if (action.equals(RECONNECT)) { +// this.reconnect(callbackContext); +// } else if (action.equals(SCAN)) { +// this.scan(callbackContext, data); +// } else if (action.equals(REMOVE_NETWORK)) { +// this.remove(callbackContext, data); +// } else if (action.equals(CONNECT_NETWORK)) { +// this.connect(callbackContext, data); +// } else if (action.equals(DISCONNECT_NETWORK)) { +// this.disconnectNetwork(callbackContext, data); +// } else if (action.equals(LIST_NETWORKS)) { +// this.listNetworks(callbackContext); +// } else if (action.equals(START_SCAN)) { +// this.startScan(callbackContext); +// } else if (action.equals(GET_SCAN_RESULTS)) { +// this.getScanResults(callbackContext, data); +// } else if (action.equals(DISCONNECT)) { +// this.disconnect(callbackContext); +// } else if (action.equals(GET_CONNECTED_SSID)) { +// this.getConnectedSSID(callbackContext); +// } else if (action.equals(GET_CONNECTED_BSSID)) { +// this.getConnectedBSSID(callbackContext); +// } else if (action.equals(GET_CONNECTED_NETWORKID)) { +// this.getConnectedNetworkID(callbackContext); +// } else if (action.equals(RESET_BIND_ALL)) { +// this.resetBindAll(callbackContext); +// } else if (action.equals(SET_BIND_ALL)) { +// this.setBindAll(callbackContext); +// } else { +// callbackContext.error("Incorrect action parameter: " + action); +// // The ONLY time to return FALSE is when action does not exist that was called +// // Returning false results in an INVALID_ACTION error, which translates to an error callback invoked on the JavaScript side +// // All other errors should be handled with the fail callback (callbackContext.error) +// // @see https://cordova.apache.org/docs/en/latest/guide/platforms/android/plugin.html +// return false; +// } + +// return true; +// } + +// /** +// * Scans networks and sends the list back on the success callback +// * +// * @param callbackContext A Cordova callback context +// * @param data JSONArray with [0] == JSONObject +// * @return true +// */ +// private boolean scan(final CallbackContext callbackContext, final JSONArray data) { +// Log.v(TAG, "Entering startScan"); +// final ScanSyncContext syncContext = new ScanSyncContext(); + +// final BroadcastReceiver receiver = new BroadcastReceiver() { +// public void onReceive(Context context, Intent intent) { +// Log.v(TAG, "Entering onReceive"); + +// synchronized (syncContext) { +// if (syncContext.finished) { +// Log.v(TAG, "In onReceive, already finished"); +// return; +// } +// syncContext.finished = true; +// context.unregisterReceiver(this); +// } + +// Log.v(TAG, "In onReceive, success"); +// getScanResults(callbackContext, data); +// } +// }; + +// final Context context = cordova.getActivity().getApplicationContext(); + +// Log.v(TAG, "Submitting timeout to threadpool"); + +// cordova.getThreadPool().submit(new Runnable() { + +// public void run() { + +// Log.v(TAG, "Entering timeout"); + +// final int TEN_SECONDS = 10000; + +// try { +// Thread.sleep(TEN_SECONDS); +// } catch (InterruptedException e) { +// Log.e(TAG, "Received InterruptedException e, " + e); +// // keep going into error +// } + +// Log.v(TAG, "Thread sleep done"); + +// synchronized (syncContext) { +// if (syncContext.finished) { +// Log.v(TAG, "In timeout, already finished"); +// return; +// } +// syncContext.finished = true; +// context.unregisterReceiver(receiver); +// } + +// Log.v(TAG, "In timeout, error"); +// callbackContext.error("TIMEOUT_WAITING_FOR_SCAN"); +// } + +// }); + +// Log.v(TAG, "Registering broadcastReceiver"); +// context.registerReceiver( +// receiver, +// new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) +// ); + +// if (!wifiManager.startScan()) { +// Log.v(TAG, "Scan failed"); +// callbackContext.error("SCAN_FAILED"); +// return false; +// } + +// Log.v(TAG, "Starting wifi scan"); +// return true; +// } + +// /** +// * This methods adds a network to the list of available WiFi networks. If the network already +// * exists, then it updates it. +// * +// * @return true if add successful, false if add fails +// * @params callbackContext A Cordova callback context. +// * @params data JSON Array with [0] == SSID, [1] == password +// */ +// private boolean add(CallbackContext callbackContext, JSONArray data) { + +// Log.d(TAG, "WifiWizard2: add entered."); + + +// try { +// // data's order for ANY object is +// // 0: SSID +// // 1: authentication algorithm, +// // 2: authentication information +// // 3: whether or not the SSID is hidden +// String newSSID = data.getString(0); +// String newPass = data.getString(2); +// String authType = data.getString(1); +// boolean isHiddenSSID = data.getBoolean(3); + +// if(API_VERSION >= 29){ +// // Remove Double Quotes +// newSSID = newSSID.replace("\"", ""); +// newPass = newPass.replace("\"", ""); + +// this.networkCallback = new ConnectivityManager.NetworkCallback() { +// @Override +// public void onAvailable(Network network) { +// connectivityManager.bindProcessToNetwork(network); +// Log.d(TAG, "onAvailable"); +// callbackContext.success("onAvailable"); +// } +// @Override +// public void onUnavailable() { +// super.onUnavailable(); +// Log.d(TAG, "onUnavailable"); +// callbackContext.error("onUnavailable"); +// } +// }; + +// // Connect Wifi using WifiNetworkSpecifier +// WifiNetworkSpecifier.Builder builder = new WifiNetworkSpecifier.Builder(); +// builder.setIsHiddenSsid(this.hiddenSSID); + +// if (newSSID.endsWith("#")) { +// newSSID = newSSID.replace("#", ""); +// builder.setSsidPattern(new PatternMatcher(newSSID, PatternMatcher.PATTERN_PREFIX)); +// }else { +// builder.setSsid(newSSID); +// } +// builder.setWpa2Passphrase(newPass); + +// WifiNetworkSpecifier wifiNetworkSpecifier = builder.build(); + +// NetworkRequest.Builder networkRequestBuilder1 = new NetworkRequest.Builder(); +// networkRequestBuilder1.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); +// //removeCapability added for hotspots without internet +// networkRequestBuilder1.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); +// networkRequestBuilder1.setNetworkSpecifier(wifiNetworkSpecifier); + +// NetworkRequest nr = networkRequestBuilder1.build(); +// //timeout add because "No devices found" wasn't handled correct and doesn't throw Unavailable +// connectivityManager.requestNetwork(nr, this.networkCallback, 30000); + +// }else{ + +// // Initialize the WifiConfiguration object +// WifiConfiguration wifi = new WifiConfiguration(); +// wifi.hiddenSSID = this.hiddenSSID; +// wifi.SSID = newSSID; +// wifi.preSharedKey = newPass; +// wifi.status = WifiConfiguration.Status.ENABLED; +// wifi.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); +// wifi.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); +// wifi.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); +// wifi.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); +// wifi.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); +// wifi.allowedProtocols.set(WifiConfiguration.Protocol.RSN); +// wifi.allowedProtocols.set(WifiConfiguration.Protocol.WPA); + +// wifi.networkId = ssidToNetworkId(newSSID); + +// // Set network to highest priority (deprecated in API >= 26) +// if(API_VERSION < 26) { +// wifi.priority = getMaxWifiPriority(wifiManager) + 1; +// } + +// // After processing authentication types, add or update network +// if(wifi.networkId == -1) { // -1 means SSID configuration does not exist yet + +// int newNetId = wifiManager.addNetwork(wifi); +// if( newNetId > -1 ){ +// callbackContext.success( newNetId ); +// } else { +// callbackContext.error( "ERROR_ADDING_NETWORK" ); +// } + +// } else { + +// int updatedNetID = wifiManager.updateNetwork(wifi); + +// if(updatedNetID == -1) +// updatedNetID = wifiManager.addNetwork(wifi); + +// if(updatedNetID > -1) { +// callbackContext.success( updatedNetID ); +// } else { +// callbackContext.error("ERROR_UPDATING_NETWORK"); +// } + +// } + +// // WifiManager configurations are presistent for API 26+ +// if(API_VERSION < 26) { +// wifiManager.saveConfiguration(); // Call saveConfiguration for older < 26 API +// } +// } + +// return true; + + +// } catch (Exception e) { +// callbackContext.error(e.getMessage()); +// Log.d(TAG, e.getMessage()); +// return false; +// } +// } + +// /** +// * This method connects a network. +// * +// * @param callbackContext A Cordova callback context +// * @param data JSON Array, with [0] being SSID to connect +// */ +// private void enable(CallbackContext callbackContext, JSONArray data) { +// Log.d(TAG, "WifiWizard2: enable entered."); + +// if (!validateData(data)) { +// callbackContext.error("ENABLE_INVALID_DATA"); +// Log.d(TAG, "WifiWizard2: enable invalid data."); +// return; +// } + +// String ssidToEnable = ""; +// String bindAll = "false"; +// String waitForConnection = "false"; + +// try { +// ssidToEnable = data.getString(0); +// bindAll = data.getString(1); +// waitForConnection = data.getString(2); +// } catch (Exception e) { +// callbackContext.error(e.getMessage()); +// Log.d(TAG, e.getMessage()); +// return; +// } + +// int networkIdToEnable = ssidToNetworkId(ssidToEnable); + +// try { + +// if(networkIdToEnable > -1) { + +// Log.d(TAG, "Valid networkIdToEnable: attempting connection"); + +// // Bind all requests to WiFi network (only necessary for Lollipop+ - API 21+) +// if(bindAll.equals("true")) { +// registerBindALL(networkIdToEnable); +// } + +// if(wifiManager.enableNetwork(networkIdToEnable, true)) { + +// if( waitForConnection.equals("true") ){ +// callbackContext.success("NETWORK_ENABLED"); +// return; +// } else { +// new ConnectAsync().execute(callbackContext, networkIdToEnable); +// return; +// } + +// } else { +// callbackContext.error("ERROR_ENABLING_NETWORK"); +// return; +// } + +// } else { +// callbackContext.error("UNABLE_TO_ENABLE"); +// return; +// } + +// } catch (Exception e) { +// callbackContext.error(e.getMessage()); +// Log.d(TAG, e.getMessage()); +// return; +// } + +// } + +// /** +// * This method disables a network. +// * +// * @param callbackContext A Cordova callback context +// * @param data JSON Array, with [0] being SSID to connect +// * @return true if network disconnected, false if failed +// */ +// private boolean disable(CallbackContext callbackContext, JSONArray data) { +// Log.d(TAG, "WifiWizard2: disable entered."); + +// if (!validateData(data)) { +// callbackContext.error("DISABLE_INVALID_DATA"); +// Log.d(TAG, "WifiWizard2: disable invalid data"); +// return false; +// } + +// String ssidToDisable = ""; + +// try { +// ssidToDisable = data.getString(0); +// } catch (Exception e) { +// callbackContext.error(e.getMessage()); +// Log.d(TAG, e.getMessage()); +// return false; +// } + +// int networkIdToDisconnect = ssidToNetworkId(ssidToDisable); + +// try { + +// if (networkIdToDisconnect > 0) { +// if(wifiManager.disableNetwork(networkIdToDisconnect)){ +// maybeResetBindALL(); +// callbackContext.success("Network " + ssidToDisable + " disabled!"); +// } else { +// callbackContext.error("UNABLE_TO_DISABLE"); +// } +// return true; +// } else { +// callbackContext.error("DISABLE_NETWORK_NOT_FOUND"); +// Log.d(TAG, "WifiWizard2: Network not found to disable."); +// return false; +// } + +// } catch (Exception e) { + +// callbackContext.error(e.getMessage()); +// Log.d(TAG, e.getMessage()); +// return false; + +// } +// } + +// /** +// * This method removes a network from the list of configured networks. +// * +// * @param callbackContext A Cordova callback context +// * @param data JSON Array, with [0] being SSID to remove +// * @return true if network removed, false if failed +// */ +// private boolean remove(CallbackContext callbackContext, JSONArray data) { +// Log.d(TAG, "WifiWizard2: remove entered."); + +// if (!validateData(data)) { +// callbackContext.error("REMOVE_INVALID_DATA"); +// Log.d(TAG, "WifiWizard2: remove data invalid"); +// return false; +// } + +// // TODO: Verify the type of data! +// try { +// String ssidToDisconnect = data.getString(0); + +// if(API_VERSION >= 29){ +// if (networkCallback != null) { +// connectivityManager.unregisterNetworkCallback(networkCallback); +// networkCallback = null; +// } +// connectivityManager.releaseNetworkRequest(pending_intent_net_req_received); +// // WifiNetworkSuggestion wifiNetworkSuggestion = new WifiNetworkSuggestion.Builder() +// // .setSsid(ssidToDisconnect) +// // .build(); +// // List list = new ArrayList(); +// // list.add(wifiNetworkSuggestion); +// // wifiManager.removeNetworkSuggestions(list); +// callbackContext.success("NETWORK_REMOVED"); +// return true; +// } + + +// int networkIdToRemove = ssidToNetworkId(ssidToDisconnect); + +// if (networkIdToRemove > -1) { + +// if( wifiManager.removeNetwork(networkIdToRemove) ){ + +// // Configurations persist by default in API 26+ +// if (API_VERSION < 26) { +// wifiManager.saveConfiguration(); +// } + +// callbackContext.success("NETWORK_REMOVED"); + +// } else { + +// callbackContext.error( "UNABLE_TO_REMOVE" ); +// } + +// return true; +// } else { +// callbackContext.error("REMOVE_NETWORK_NOT_FOUND"); +// Log.d(TAG, "WifiWizard2: Network not found, can't remove."); +// return false; +// } +// } catch (Exception e) { +// callbackContext.error(e.getMessage()); +// Log.d(TAG, e.getMessage()); +// return false; +// } +// } + +// /** +// * This method connects a network. +// * +// * @param callbackContext A Cordova callback context +// * @param data JSON Array, with [0] being SSID to connect +// */ +// private void connect(CallbackContext callbackContext, JSONArray data) { +// Log.d(TAG, "WifiWizard2: connect entered."); + +// if(API_VERSION >= 29){ +// // No need to connect, its already connected via WifiNetworkSpecifier at add(...) method +// callbackContext.success("NETWORK_CONNECTION_COMPLETED"); +// return; +// } + +// if (!validateData(data)) { +// callbackContext.error("CONNECT_INVALID_DATA"); +// Log.d(TAG, "WifiWizard2: connect invalid data."); +// return; +// } + +// String ssidToConnect = ""; +// String bindAll = "false"; + +// try { +// ssidToConnect = data.getString(0); +// bindAll = data.getString(1); +// } catch (Exception e) { +// callbackContext.error(e.getMessage()); +// Log.d(TAG, e.getMessage()); +// return; +// } + +// int networkIdToConnect = ssidToNetworkId(ssidToConnect); + +// if (networkIdToConnect > -1) { +// // We disable the network before connecting, because if this was the last connection before +// // a disconnect(), this will not reconnect. + +// Log.d(TAG, "Valid networkIdToConnect: attempting connection"); + +// // Bind all requests to WiFi network (only necessary for Lollipop+ - API 21+) +// if( bindAll.equals("true") ){ +// registerBindALL(networkIdToConnect); +// } + +// if (API_VERSION >= 26) { +// // wifiManager.disconnect(); +// } else { +// wifiManager.disableNetwork(networkIdToConnect); +// } + +// wifiManager.enableNetwork(networkIdToConnect, true); + +// if (API_VERSION >= 26) { +// // wifiManager.reassociate(); +// } + +// new ConnectAsync().execute(callbackContext, networkIdToConnect); +// return; + +// } else { +// callbackContext.error("INVALID_NETWORK_ID_TO_CONNECT"); +// return; +// } +// } + +// /** +// * Wait for connection before returning error or success +// * +// * This method will wait up to 60 seconds for WiFi connection to specified network ID be in COMPLETED state, otherwise will return error. +// * +// * @param callbackContext +// * @param networkIdToConnect +// * @return +// */ +// private class ConnectAsync extends AsyncTask { +// CallbackContext callbackContext; +// @Override +// protected void onPostExecute(String[] results) { +// String error = results[0]; +// String success = results[1]; +// if (error != null) { +// this.callbackContext.error(error); +// } else { +// this.callbackContext.success(success); +// } +// } + +// @Override +// protected String[] doInBackground(Object... params) { +// this.callbackContext = (CallbackContext) params[0]; +// int networkIdToConnect = (Integer) params[1]; + +// final int TIMES_TO_RETRY = 15; +// for (int i = 0; i < TIMES_TO_RETRY; i++) { + +// WifiInfo info = wifiManager.getConnectionInfo(); +// NetworkInfo.DetailedState connectionState = info +// .getDetailedStateOf(info.getSupplicantState()); + +// boolean isConnected = +// // need to ensure we're on correct network because sometimes this code is +// // reached before the initial network has disconnected +// info.getNetworkId() == networkIdToConnect && ( +// connectionState == NetworkInfo.DetailedState.CONNECTED || +// // Android seems to sometimes get stuck in OBTAINING_IPADDR after it has received one +// (connectionState == NetworkInfo.DetailedState.OBTAINING_IPADDR +// && info.getIpAddress() != 0) +// ); + +// if (isConnected) { +// return new String[]{ null, "NETWORK_CONNECTION_COMPLETED" }; +// } + +// Log.d(TAG, "WifiWizard: Got " + connectionState.name() + " on " + (i + 1) + " out of " + TIMES_TO_RETRY); +// final int ONE_SECOND = 1000; + +// try { +// Thread.sleep(ONE_SECOND); +// } catch (InterruptedException e) { +// Log.e(TAG, e.getMessage()); +// return new String[]{ "INTERRUPT_EXCEPT_WHILE_CONNECTING", null }; +// } +// } +// Log.d(TAG, "WifiWizard: Network failed to finish connecting within the timeout"); +// return new String[]{ "CONNECT_FAILED_TIMEOUT", null }; +// } +// } + +// /** +// * This method disconnects a network. +// * +// * @param callbackContext A Cordova callback context +// * @param data JSON Array, with [0] being SSID to connect +// * @return true if network disconnected, false if failed +// */ +// private boolean disconnectNetwork(CallbackContext callbackContext, JSONArray data) { +// Log.d(TAG, "WifiWizard2: disconnectNetwork entered."); +// if (!validateData(data)) { +// callbackContext.error("DISCONNECT_NET_INVALID_DATA"); +// Log.d(TAG, "WifiWizard2: disconnectNetwork invalid data"); +// return false; +// } + +// String ssidToDisconnect = ""; + +// // TODO: Verify type of data here! +// try { +// ssidToDisconnect = data.getString(0); +// } catch (Exception e) { +// callbackContext.error(e.getMessage()); +// Log.d(TAG, e.getMessage()); +// return false; +// } + +// if(API_VERSION < 29){ +// int networkIdToDisconnect = ssidToNetworkId(ssidToDisconnect); + +// if(networkIdToDisconnect > 0) { + +// if(wifiManager.disableNetwork(networkIdToDisconnect)) { + +// maybeResetBindALL(); + +// // We also remove the configuration from the device (use "disable" to keep config) +// if( wifiManager.removeNetwork(networkIdToDisconnect) ){ +// callbackContext.success("Network " + ssidToDisconnect + " disconnected and removed!"); +// } else { +// callbackContext.error("DISCONNECT_NET_REMOVE_ERROR"); +// Log.d(TAG, "WifiWizard2: Unable to remove network!"); +// return false; +// } + +// } else { +// callbackContext.error("DISCONNECT_NET_DISABLE_ERROR"); +// Log.d(TAG, "WifiWizard2: Unable to disable network!"); +// return false; +// } + +// return true; +// } else { +// callbackContext.error("DISCONNECT_NET_ID_NOT_FOUND"); +// Log.d(TAG, "WifiWizard2: Network not found to disconnect."); +// return false; +// } +// } else { +// try{ +// ConnectivityManager cm = (ConnectivityManager) cordova.getActivity().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); +// cm.unregisterNetworkCallback(this.networkCallback); +// connectivityManager.bindProcessToNetwork(null); +// return true; +// } +// catch(Exception e) { +// callbackContext.error(e.getMessage()); +// return false; +// } +// } +// } + +// /** +// * This method disconnects the currently connected network. +// * +// * @param callbackContext A Cordova callback context +// * @return true if network disconnected, false if failed +// */ +// private boolean disconnect(CallbackContext callbackContext) { +// Log.d(TAG, "WifiWizard2: disconnect entered."); + +// if (wifiManager.disconnect()) { +// maybeResetBindALL(); +// callbackContext.success("Disconnected from current network"); +// return true; +// } else { +// callbackContext.error("ERROR_DISCONNECT"); +// return false; +// } +// } + +// /** +// * Reconnect Network +// *

+// * Reconnect to the currently active access point, if we are currently disconnected. This may +// * result in the asynchronous delivery of state change events. +// */ +// private boolean reconnect(CallbackContext callbackContext) { +// Log.d(TAG, "WifiWizard2: reconnect entered."); + +// if (wifiManager.reconnect()) { +// callbackContext.success("Reconnected network"); +// return true; +// } else { +// callbackContext.error("ERROR_RECONNECT"); +// return false; +// } +// } + +// /** +// * Reassociate Network +// *

+// * Reconnect to the currently active access point, even if we are already connected. This may +// * result in the asynchronous delivery of state change events. +// */ +// private boolean reassociate(CallbackContext callbackContext) { +// Log.d(TAG, "WifiWizard2: reassociate entered."); + +// if (wifiManager.reassociate()) { +// callbackContext.success("Reassociated network"); +// return true; +// } else { +// callbackContext.error("ERROR_REASSOCIATE"); +// return false; +// } +// } + +// /** +// * This method uses the callbackContext.success method to send a JSONArray of the currently +// * configured networks. +// * +// * @param callbackContext A Cordova callback context +// * @return true if network disconnected, false if failed +// */ +// private boolean listNetworks(CallbackContext callbackContext) { +// Log.d(TAG, "WifiWizard2: listNetworks entered."); +// @SuppressLint("MissingPermission") List wifiList = wifiManager.getConfiguredNetworks(); + +// JSONArray returnList = new JSONArray(); + +// for (WifiConfiguration wifi : wifiList) { +// returnList.put(wifi.SSID); +// } + +// callbackContext.success(returnList); + +// return true; +// } + +// /** +// * This method uses the callbackContext.success method to send a JSONArray of the scanned +// * networks. +// * +// * @param callbackContext A Cordova callback context +// * @param data JSONArray with [0] == JSONObject +// * @return true +// */ +// private boolean getScanResults(CallbackContext callbackContext, JSONArray data) { + +// if (cordova.hasPermission(ACCESS_FINE_LOCATION)) { + +// List scanResults = wifiManager.getScanResults(); + +// JSONArray returnList = new JSONArray(); + +// Integer numLevels = null; + +// if (!validateData(data)) { +// callbackContext.error("GET_SCAN_RESULTS_INVALID_DATA"); +// Log.d(TAG, "WifiWizard2: getScanResults invalid data"); +// return false; +// } else if (!data.isNull(0)) { +// try { +// JSONObject options = data.getJSONObject(0); + +// if (options.has("numLevels")) { +// Integer levels = options.optInt("numLevels"); + +// if (levels > 0) { +// numLevels = levels; +// } else if (options.optBoolean("numLevels", false)) { +// // use previous default for {numLevels: true} +// numLevels = 5; +// } +// } +// } catch (JSONException e) { +// e.printStackTrace(); +// callbackContext.error(e.toString()); +// return false; +// } +// } + +// for (ScanResult scan : scanResults) { +// /* +// * @todo - breaking change, remove this notice when tidying new release and explain changes, e.g.: +// * 0.y.z includes a breaking change to WifiWizard2.getScanResults(). +// * Earlier versions set scans' level attributes to a number derived from wifiManager.calculateSignalLevel. +// * This update returns scans' raw RSSI value as the level, per Android spec / APIs. +// * If your application depends on the previous behaviour, we have added an options object that will modify behaviour: +// * - if `(n == true || n < 2)`, `*.getScanResults({numLevels: n})` will return data as before, split in 5 levels; +// * - if `(n > 1)`, `*.getScanResults({numLevels: n})` will calculate the signal level, split in n levels; +// * - if `(n == false)`, `*.getScanResults({numLevels: n})` will use the raw signal level; +// */ + +// int level; + +// if (numLevels == null) { +// level = scan.level; +// } else { +// level = wifiManager.calculateSignalLevel(scan.level, numLevels); +// } + +// JSONObject lvl = new JSONObject(); +// try { +// lvl.put("level", level); +// lvl.put("SSID", scan.SSID); +// lvl.put("BSSID", scan.BSSID); +// lvl.put("frequency", scan.frequency); +// lvl.put("capabilities", scan.capabilities); +// lvl.put("timestamp", scan.timestamp); + +// if (API_VERSION >= 23) { // Marshmallow +// lvl.put("channelWidth", scan.channelWidth); +// lvl.put("centerFreq0", scan.centerFreq0); +// lvl.put("centerFreq1", scan.centerFreq1); +// } else { +// lvl.put("channelWidth", JSONObject.NULL); +// lvl.put("centerFreq0", JSONObject.NULL); +// lvl.put("centerFreq1", JSONObject.NULL); +// } + +// returnList.put(lvl); +// } catch (JSONException e) { +// e.printStackTrace(); +// callbackContext.error(e.toString()); +// return false; +// } +// } + +// callbackContext.success(returnList); +// return true; + +// } else { + +// requestLocationPermission(SCAN_RESULTS_CODE); +// return true; +// } + +// } + +// /** +// * This method uses the callbackContext.success method. It starts a wifi scanning +// * +// * @param callbackContext A Cordova callback context +// * @return true if started was successful +// */ +// private boolean startScan(CallbackContext callbackContext) { + +// if (wifiManager.startScan()) { +// callbackContext.success(); +// return true; +// } else { +// callbackContext.error("STARTSCAN_FAILED"); +// return false; +// } +// } + +// /** +// * This method returns the connected WiFi network ID (if connected) +// * +// * @return -1 if no network connected, or network id if connected +// */ +// private int getConnectedNetId() { +// int networkId = -1; + +// WifiInfo info = wifiManager.getConnectionInfo(); + +// if (info == null) { +// Log.d(TAG, "Unable to read wifi info"); +// return networkId; +// } + +// networkId = info.getNetworkId(); + +// if (networkId == -1) { +// Log.d(TAG, "NO_CURRENT_NETWORK_FOUND"); +// } + +// return networkId; +// } + +// /** +// * Get Network ID from SSID +// * +// * @param callbackContext A Cordova callback context +// * @param data JSON Array, with [0] being SSID to connect +// * @return true if network connected, false if failed +// */ +// private boolean getSSIDNetworkID(CallbackContext callbackContext, JSONArray data) { +// Log.d(TAG, "WifiWizard2: getSSIDNetworkID entered."); + +// if (!validateData(data)) { +// callbackContext.error("GET_SSID_INVALID_DATA"); +// Log.d(TAG, "WifiWizard2: getSSIDNetworkID invalid data."); +// return false; +// } + +// String ssidToGetNetworkID = ""; + +// try { +// ssidToGetNetworkID = data.getString(0); +// } catch (Exception e) { +// callbackContext.error(e.getMessage()); +// Log.d(TAG, e.getMessage()); +// return false; +// } + +// int networkIdToConnect = ssidToNetworkId(ssidToGetNetworkID); +// callbackContext.success(networkIdToConnect); + +// return true; +// } + +// /** +// * This method returns the connected WiFi network ID (if connected) +// * +// * @param callbackContext A Cordova callback context +// * @return -1 if no network connected, or network id if connected +// */ +// private boolean getConnectedNetworkID(CallbackContext callbackContext) { +// int networkId = getConnectedNetId(); + +// if (networkId == -1) { +// callbackContext.error("GET_CONNECTED_NET_ID_ERROR"); +// return false; +// } + +// callbackContext.success(networkId); +// return true; +// } + +// /** +// * This method retrieves the SSID for the currently connected network +// * +// * @param callbackContext A Cordova callback context +// * @return true if SSID found, false if not. +// */ +// private boolean getConnectedSSID(CallbackContext callbackContext) { +// return getWifiServiceInfo(callbackContext, false); +// } + +// /** +// * This method retrieves the BSSID for the currently connected network +// * +// * @param callbackContext A Cordova callback context +// * @return true if SSID found, false if not. +// */ +// private boolean getConnectedBSSID(CallbackContext callbackContext) { +// return getWifiServiceInfo(callbackContext, true); +// } + +// /** +// * This method retrieves the WifiInformation for the (SSID or BSSID) currently connected network. +// * +// * @param callbackContext A Cordova callback context +// * @param basicIdentifier A flag to get BSSID if true or SSID if false. +// * @return true if SSID found, false if not. +// */ +// private boolean getWifiServiceInfo(CallbackContext callbackContext, boolean basicIdentifier) { +// if (API_VERSION >= 23 && !cordova.hasPermission(ACCESS_FINE_LOCATION)) { //Android 9 (Pie) or newer +// requestLocationPermission(WIFI_SERVICE_INFO_CODE); +// bssidRequested = basicIdentifier; +// return true; +// } else { +// WifiInfo info = wifiManager.getConnectionInfo(); + +// if (info == null) { +// callbackContext.error("UNABLE_TO_READ_WIFI_INFO"); +// return false; +// } + +// // Only return SSID or BSSID when actually connected to a network +// SupplicantState state = info.getSupplicantState(); +// if (!state.equals(SupplicantState.COMPLETED)) { +// callbackContext.error("CONNECTION_NOT_COMPLETED"); +// return false; +// } + +// String serviceInfo; +// if (basicIdentifier) { +// serviceInfo = info.getBSSID(); +// } else { +// serviceInfo = info.getSSID(); +// } + +// if (serviceInfo == null || serviceInfo.isEmpty() || serviceInfo == "0x") { +// callbackContext.error("WIFI_INFORMATION_EMPTY"); +// return false; +// } + +// // http://developer.android.com/reference/android/net/wifi/WifiInfo.html#getSSID() +// if (serviceInfo.startsWith("\"") && serviceInfo.endsWith("\"")) { +// serviceInfo = serviceInfo.substring(1, serviceInfo.length() - 1); +// } + +// callbackContext.success(serviceInfo); +// return true; +// } +// } + +// /** +// * This method retrieves the current WiFi status +// * +// * @param callbackContext A Cordova callback context +// * @return true if WiFi is enabled, fail will be called if not. +// */ +// private boolean isWifiEnabled(CallbackContext callbackContext) { +// boolean isEnabled = wifiManager.isWifiEnabled(); +// callbackContext.success(isEnabled ? "1" : "0"); +// return isEnabled; +// } + +// /** +// * This method takes a given String, searches the current list of configured WiFi networks, and +// * returns the networkId for the network if the SSID matches. If not, it returns -1. +// */ +// private int ssidToNetworkId(String ssid) { + +// try { + +// int maybeNetId = Integer.parseInt(ssid); +// Log.d(TAG, "ssidToNetworkId passed SSID is integer, probably a Network ID: " + ssid); +// return maybeNetId; + +// } catch (NumberFormatException e) { + +// List currentNetworks = wifiManager.getConfiguredNetworks(); +// int networkId = -1; + +// // For each network in the list, compare the SSID with the given one +// for (WifiConfiguration test : currentNetworks) { +// if (test.SSID != null && test.SSID.equals(ssid)) { +// networkId = test.networkId; +// } +// } + +// return networkId; + +// } +// } + +// /** +// * This method enables or disables the wifi +// */ +// private boolean setWifiEnabled(CallbackContext callbackContext, JSONArray data) { +// if (!validateData(data)) { +// callbackContext.error("SETWIFIENABLED_INVALID_DATA"); +// Log.d(TAG, "WifiWizard2: setWifiEnabled invalid data"); +// return false; +// } + +// String status = ""; + +// try { +// status = data.getString(0); +// } catch (Exception e) { +// callbackContext.error(e.getMessage()); +// Log.d(TAG, e.getMessage()); +// return false; +// } + +// if (wifiManager.setWifiEnabled(status.equals("true"))) { +// callbackContext.success(); +// return true; +// } else { +// callbackContext.error("ERROR_SETWIFIENABLED"); +// return false; +// } +// } + +// /** +// * This method will check if WiFi is enabled, and enable it if not, waiting up to 10 seconds for +// * it to enable +// * +// * @return True if wifi is enabled, false if unable to enable wifi +// */ +// private boolean verifyWifiEnabled() { + +// Log.d(TAG, "WifiWizard2: verifyWifiEnabled entered."); + +// if (!wifiManager.isWifiEnabled()) { + +// Log.i(TAG, "Enabling wi-fi..."); + +// if (wifiManager.setWifiEnabled(true)) { +// Log.i(TAG, "Wi-fi enabled"); +// } else { +// Log.e(TAG, "VERIFY_ERROR_ENABLE_WIFI"); +// return false; +// } + +// // This happens very quickly, but need to wait for it to enable. A little busy wait? +// int count = 0; + +// while (!wifiManager.isWifiEnabled()) { +// if (count >= 10) { +// Log.i(TAG, "Took too long to enable wi-fi, quitting"); +// return false; +// } + +// Log.i(TAG, "Still waiting for wi-fi to enable..."); + +// try { +// Thread.sleep(1000L); +// } catch (InterruptedException ie) { +// // continue +// } + +// count++; +// } + +// // If we make it this far, wifi should be enabled by now +// return true; + +// } else { + +// return true; + +// } + +// } + +// /** +// * Format and return WiFi IPv4 Address +// * @return +// */ +// private String[] getWiFiIPAddress() { +// WifiInfo wifiInfo = wifiManager.getConnectionInfo(); +// int ip = wifiInfo.getIpAddress(); + +// String ipString = formatIP(ip); +// String subnet = ""; + +// try { +// InetAddress inetAddress = InetAddress.getByName(ipString); +// subnet = getIPv4Subnet(inetAddress); +// } catch (Exception e) { +// } + +// return new String[]{ipString, subnet}; +// } + +// /** +// * Get WiFi Router IP from DHCP +// * @return +// */ +// private String getWiFiRouterIP() { +// DhcpInfo dhcp = wifiManager.getDhcpInfo(); +// int ip = dhcp.gateway; +// return formatIP(ip); +// } + +// /** +// * Format IPv4 Address +// * @param ip +// * @return +// */ +// private String formatIP(int ip) { +// return String.format( +// "%d.%d.%d.%d", +// (ip & 0xff), +// (ip >> 8 & 0xff), +// (ip >> 16 & 0xff), +// (ip >> 24 & 0xff) +// ); +// } + +// /** +// * Get IPv4 Subnet +// * @param inetAddress +// * @return +// */ +// public static String getIPv4Subnet(InetAddress inetAddress) { +// try { +// NetworkInterface ni = NetworkInterface.getByInetAddress(inetAddress); +// List intAddrs = ni.getInterfaceAddresses(); +// for (InterfaceAddress ia : intAddrs) { +// if (!ia.getAddress().isLoopbackAddress() && ia.getAddress() instanceof Inet4Address) { +// return getIPv4SubnetFromNetPrefixLength(ia.getNetworkPrefixLength()).getHostAddress() +// .toString(); +// } +// } +// } catch (Exception e) { +// } +// return ""; +// } + +// /** +// * Get Subnet from Prefix Length +// * @param netPrefixLength +// * @return +// */ +// public static InetAddress getIPv4SubnetFromNetPrefixLength(int netPrefixLength) { +// try { +// int shift = (1 << 31); +// for (int i = netPrefixLength - 1; i > 0; i--) { +// shift = (shift >> 1); +// } +// String subnet = +// Integer.toString((shift >> 24) & 255) + "." + Integer.toString((shift >> 16) & 255) + "." +// + Integer.toString((shift >> 8) & 255) + "." + Integer.toString(shift & 255); +// return InetAddress.getByName(subnet); +// } catch (Exception e) { +// } +// return null; +// } + +// /** +// * Validate JSON data +// */ +// private boolean validateData(JSONArray data) { +// try { +// if (data == null || data.get(0) == null) { +// callbackContext.error("DATA_IS_NULL"); +// return false; +// } +// return true; +// } catch (Exception e) { +// callbackContext.error(e.getMessage()); +// } +// return false; +// } + +// /** +// * Request ACCESS_FINE_LOCATION Permission +// * @param requestCode +// */ +// protected void requestLocationPermission(int requestCode) { +// cordova.requestPermission(this, requestCode, ACCESS_FINE_LOCATION); +// } + +// /** +// * Handle Android Permission Requests +// */ +// public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) +// throws JSONException { + +// for (int r : grantResults) { +// if (r == PackageManager.PERMISSION_DENIED) { +// callbackContext.error( "PERMISSION_DENIED" ); +// return; +// } +// } + +// switch (requestCode) { +// case SCAN_RESULTS_CODE: +// getScanResults(callbackContext, passedData); // Call method again after permissions approved +// break; +// case SCAN_CODE: +// scan(callbackContext, passedData); // Call method again after permissions approved +// break; +// case LOCATION_REQUEST_CODE: +// callbackContext.success("PERMISSION_GRANTED"); +// break; +// case WIFI_SERVICE_INFO_CODE: +// getWifiServiceInfo(callbackContext, bssidRequested); +// break; +// } +// } + +// /** +// * Figure out what the highest priority network in the network list is and return that priority +// */ +// private static int getMaxWifiPriority(final WifiManager wifiManager) { +// final List configurations = wifiManager.getConfiguredNetworks(); +// int maxPriority = 0; +// for (WifiConfiguration config : configurations) { +// if (config.priority > maxPriority) { +// maxPriority = config.priority; +// } +// } + +// Log.d(TAG, "WifiWizard: Found max WiFi priority of " +// + maxPriority); + +// return maxPriority; +// } + +// /** +// * Check if device is connected to Internet +// */ +// private boolean canConnectToInternet(CallbackContext callbackContext, boolean doPing) { + +// try { + +// if ( hasInternetConnection(doPing) ) { +// // Send success as 1 to return true from Promise (handled in JS) +// callbackContext.success("1"); +// return true; +// } else { +// callbackContext.success("0"); +// return false; +// } + +// } catch (Exception e) { +// callbackContext.error(e.getMessage()); +// Log.d(TAG, e.getMessage()); +// return false; +// } +// } + +// /** +// * Check if we can conenct to router via HTTP connection +// * +// * @param callbackContext +// * @param doPing +// * @return boolean +// */ +// private boolean canConnectToRouter(CallbackContext callbackContext, boolean doPing) { + +// try { + +// if (hasConnectionToRouter(doPing)) { +// // Send success as 1 to return true from Promise (handled in JS) +// callbackContext.success("1"); +// return true; +// } else { +// callbackContext.success("0"); +// return false; +// } + +// } catch (Exception e) { +// callbackContext.error(e.getMessage()); +// Log.d(TAG, e.getMessage()); +// return false; +// } +// } + +// /** +// * Check if The Device Is Connected to Internet +// * +// * @return true if device connect to Internet or return false if not +// */ +// public boolean hasInternetConnection(boolean doPing) { +// if (connectivityManager != null) { +// NetworkInfo info = connectivityManager.getActiveNetworkInfo(); +// if (info != null) { +// if (info.isConnected()) { +// if( doPing ){ +// return pingCmd("8.8.8.8"); +// } else { +// return isHTTPreachable("http://www.google.com/"); +// } +// } +// } +// } +// return false; +// } + +// /** +// * Check for connection to router by pinging router IP +// * @return +// */ +// public boolean hasConnectionToRouter( boolean doPing ) { + +// String ip = getWiFiRouterIP(); + +// if ( ip == null || ip.equals("0.0.0.0") || connectivityManager == null) { + +// return false; + +// } else { + +// NetworkInfo info = connectivityManager.getActiveNetworkInfo(); + +// if (info != null && info.isConnected()) { + +// if( doPing ){ +// return pingCmd(ip); +// } else { +// return isHTTPreachable("http://" + ip + "/"); +// } +// } else { +// return false; +// } + +// } + +// } + +// /** +// * Check if HTTP connection to URL is reachable +// * +// * @param checkURL +// * @return boolean +// */ +// public static boolean isHTTPreachable(String checkURL) { +// try { +// // make a URL to a known source +// URL url = new URL(checkURL); + +// // open a connection to that source +// HttpURLConnection urlConnect = (HttpURLConnection) url.openConnection(); + +// // trying to retrieve data from the source. If there +// // is no connection, this line will fail +// Object objData = urlConnect.getContent(); + +// } catch (Exception e) { +// e.printStackTrace(); +// return false; +// } + +// return true; +// } + +// /** +// * Method to Ping IP Address +// * +// * @param addr IP address you want to ping it +// * @return true if the IP address is reachable +// */ +// public boolean pingCmd(String addr) { + +// try { + +// String ping = "ping -c 1 -W 3 " + addr; +// Runtime run = Runtime.getRuntime(); +// Process pro = run.exec(ping); + +// try { +// pro.waitFor(); +// } catch (InterruptedException e) { +// Log.e(TAG, "InterruptedException error.", e); +// } + +// int exit = pro.exitValue(); + +// Log.d(TAG, "pingCmd exitValue" + exit); + +// if (exit == 0) { +// return true; +// } else { +// // ip address is not reachable +// return false; +// } +// } catch (UnknownHostException e) { +// Log.d(TAG, "UnknownHostException: " + e.getMessage()); +// } catch (Exception e) { +// Log.d(TAG, e.getMessage()); +// } + +// return false; +// } + +// /** +// * Network Changed Broadcast Receiver +// */ +// private class NetworkChangedReceiver extends BroadcastReceiver { + +// @Override +// public void onReceive(final Context context, final Intent intent) { + +// if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) { + +// Log.d(TAG, "NETWORK_STATE_CHANGED_ACTION"); + +// NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); +// WifiInfo info = WifiWizard2.this.wifiManager.getConnectionInfo(); + +// // Checks that you're connected to the desired network +// if (networkInfo.isConnected() && info.getNetworkId() > -1) { + +// final String ssid = info.getSSID().replaceAll("\"", ""); +// final String bssid = info.getBSSID(); + +// Log.d(TAG, "Connected to '" + ssid + "' @ " + bssid); + +// // Verify the desired network ID is what we actually connected to +// if ( desired != null && info.getNetworkId() == desired.apId ) { +// onSuccessfulConnection(); +// } else { +// Log.e(TAG, "Could not connect to the desired ssid: " + ssid); +// } + +// } + +// } + +// } + +// } + +// /** +// * Register Receiver for Network Changed to handle BindALL +// * @param netID +// */ +// private void registerBindALL(int netID){ + +// // Bind all requests to WiFi network (only necessary for Lollipop+ - API 21+) +// if( API_VERSION > 21 ){ +// Log.d(TAG, "registerBindALL: registering net changed receiver"); +// desired = new AP(netID,null,null); +// cordova.getActivity().getApplicationContext().registerReceiver(networkChangedReceiver, NETWORK_STATE_CHANGED_FILTER); +// } else { +// Log.d(TAG, "registerBindALL: API older than 21, bindall ignored."); +// } +// } + +// /** +// * Maybe reset bind all after disconnect/disable +// * +// * This method unregisters the network changed receiver, as well as setting null for +// * bindProcessToNetwork or setProcessDefaultNetwork to prevent future sockets from application +// * being routed through Wifi. +// */ +// private void maybeResetBindALL(){ + +// Log.d(TAG, "maybeResetBindALL"); + +// // desired should have a value if receiver is registered +// if( desired != null ){ + +// if( API_VERSION > 21 ){ + +// try { +// // Unregister net changed receiver -- should only be registered in API versions > 21 +// cordova.getActivity().getApplicationContext().unregisterReceiver(networkChangedReceiver); +// } catch (Exception e) {} + +// } + +// // Lollipop OS or newer +// if ( API_VERSION >= 23 ) { +// connectivityManager.bindProcessToNetwork(null); +// } else if( API_VERSION >= 21 && API_VERSION < 23 ){ +// connectivityManager.setProcessDefaultNetwork(null); +// } + +// if ( API_VERSION > 21 && networkCallback != null) { + +// try { +// // Same behavior as releaseNetworkRequest +// connectivityManager.unregisterNetworkCallback(networkCallback); // Added in API 21 +// } catch (Exception e) {} +// } + +// networkCallback = null; +// previous = null; +// desired = null; + +// } + +// } + +// /** +// * Will un-bind to network (use Cellular network) +// * +// * @param callbackContext A Cordova callback context +// */ +// private void resetBindAll(CallbackContext callbackContext) { +// Log.d(TAG, "WifiWizard2: resetBindALL"); + +// try { +// maybeResetBindALL(); +// callbackContext.success("Successfully reset BindALL"); +// } catch (Exception e) { +// Log.e(TAG, "InterruptedException error.", e); +// callbackContext.error("ERROR_NO_BIND_ALL"); +// } +// } + +// /** +// * Will bind to network (use Wifi network) +// * +// * @param callbackContext A Cordova callback context +// */ +// private void setBindAll(CallbackContext callbackContext) { +// Log.d(TAG, "WifiWizard2: setBindALL"); + +// try { +// int networkId = getConnectedNetId(); +// registerBindALL(networkId); +// callbackContext.success("Successfully bindAll to network"); +// } catch (Exception e) { +// Log.e(TAG, "InterruptedException error.", e); +// callbackContext.error("ERROR_CANT_BIND_ALL"); +// } +// } + + +// /** +// * Called after successful connection to WiFi when using BindAll feature +// * +// * This method is called by the NetworkChangedReceiver after network changed action, and confirming that we are in fact connected to wifi, +// * and the wifi we're connected to, is the correct network set in enable, or connect. +// */ +// private void onSuccessfulConnection() { +// // On Lollipop+ the OS routes network requests through mobile data +// // when phone is attached to a wifi that doesn't have Internet connection +// // We use the ConnectivityManager to force bind all requests from our process +// // to the wifi without internet +// // see https://android-developers.googleblog.com/2016/07/connecting-your-app-to-wi-fi-device.html + +// // Marshmallow OS or newer +// if ( API_VERSION >= 23 ) { + +// Log.d(TAG, "BindALL onSuccessfulConnection API >= 23"); + +// // Marshmallow (API 23+) or newer uses bindProcessToNetwork +// final NetworkRequest request = new NetworkRequest.Builder() +// .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) +// .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) //removeCapability added for hotspots without internet +// .build(); + +// networkCallback = new ConnectivityManager.NetworkCallback() { +// @Override +// public void onAvailable(Network network) { +// if( connectivityManager.bindProcessToNetwork(network) ){ +// Log.d(TAG, "bindProcessToNetwork TRUE onSuccessfulConnection"); +// } else { +// Log.d(TAG, "bindProcessToNetwork FALSE onSuccessfulConnection"); +// } +// } +// }; + +// connectivityManager.requestNetwork(request, networkCallback); + +// // Only lollipop (API 21 && 22) use setProcessDefaultNetwork, API < 21 already does this by default +// } else if( API_VERSION >= 21 && API_VERSION < 23 ){ + +// Log.d(TAG, "BindALL onSuccessfulConnection API >= 21 && < 23"); + +// // Lollipop (API 21-22) use setProcessDefaultNetwork (deprecated in API 23 - Marshmallow) +// final NetworkRequest request = new NetworkRequest.Builder() +// .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) +// .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) //removeCapability addded for hotspots without internet +// .build(); + +// networkCallback = new ConnectivityManager.NetworkCallback() { +// @Override +// public void onAvailable(Network network) { +// connectivityManager.setProcessDefaultNetwork(network); +// } +// }; + +// connectivityManager.requestNetwork(request, networkCallback); + +// } else { +// // Technically we should never reach this with older API, but just in case +// Log.d(TAG, "BindALL onSuccessfulConnection API older than 21, no need to do any binding"); +// networkCallback = null; +// previous = null; +// desired = null; + +// } +// } + +// /** +// * Class to store finished boolean in +// */ +// private class ScanSyncContext { + +// public boolean finished = false; +// } + +// /** +// * Used for storing access point information +// */ +// private static class AP { +// final String ssid, bssid; +// final int apId; + +// AP(int apId, final String ssid, final String bssid) { +// this.apId = apId; +// this.ssid = ssid; +// this.bssid = bssid; +// } + +// } +// } \ No newline at end of file diff --git a/Mohem/config.xml b/Mohem/config.xml index 20f4924f..457e3654 100644 --- a/Mohem/config.xml +++ b/Mohem/config.xml @@ -103,7 +103,7 @@ - + diff --git a/Mohem/cordova b/Mohem/cordova new file mode 100644 index 00000000..e69de29b diff --git a/Mohem/package.json b/Mohem/package.json index a096b849..8248ab01 100755 --- a/Mohem/package.json +++ b/Mohem/package.json @@ -61,7 +61,7 @@ "app": "0.1.0", "chart.js": "^2.9.3", "com-badrit-base64": "^0.2.0", - "cordova-android": "^8.1.0", + "cordova-android": "9.0.0", "cordova-android-support-gradle-release": "^3.0.1", "cordova-open-native-settings": "^1.5.2", "cordova-opentok-android-permissions": "^1.0.1", @@ -99,13 +99,12 @@ "cordova.plugins.diagnostic": "^5.0.1", "core-js": "^2.5.4", "date-fns": "^1.30.1", - "es6-promise-plugin": "^4.1.0", + "ionic-native": "1.3.8", "ionic2-calendar": "^0.5.8", "ng-circle-progress": "^1.5.1", "ng2-file-upload": "^1.3.0", "ng2-pdf-viewer": "^5.3.2", "ngx-gauge": "^1.0.0-beta.10", - "npm": "^6.14.11", "phonegap-nfc": "^1.2.0", "phonegap-plugin-barcodescanner": "^8.1.0", "phonegap-plugin-multidex": "^1.0.0", @@ -129,11 +128,15 @@ "@angular/language-service": "~7.2.2", "@ionic/angular-toolkit": "~1.4.0", "@ionic/lab": "3.1.2", + "@mauron85/cordova-plugin-background-geolocation": "^3.1.0", "@types/jasmine": "~2.8.8", "@types/jasminewd2": "~2.0.3", "@types/node": "~10.12.0", "codelyzer": "~4.5.0", + "com.huawei.cordovahmsgmscheckplugin": "file:CordovaHMSPlugin/CordovaHMSGMSCheckPlugin", + "com.huawei.cordovahmslocationplugin": "file:CordovaHMSPlugin/CordovaHMSLocationPlugin", "cordova-ios": "^6.2.0", + "cordova-plugin-background-geolocation": "^1.0.6", "cordova-plugin-wifiwizard2": "^3.1.1", "es6-promise-plugin": "^4.1.0", "jasmine-core": "~2.99.1", @@ -210,15 +213,15 @@ }, "cordova-plugin-androidx": {}, "cordova-plugin-androidx-adapter": {}, - "cordova-plugin-background-geolocation": { - "GOOGLE_PLAY_SERVICES_VERSION": "+" - }, "phonegap-nfc": {}, - "wifiwizard2": {} + "com.huawei.cordovahmsgmscheckplugin": {}, + "com.huawei.cordovahmslocationplugin": {}, + "wifiwizard2": {}, + "cordova-plugin-background-geolocation": {} }, "platforms": [ - "android", - "ios" + "ios", + "android" ] } -} +} \ No newline at end of file diff --git a/Mohem/src/app/hmg-common/hmg_utils.ts b/Mohem/src/app/hmg-common/hmg_utils.ts new file mode 100644 index 00000000..f8a7737b --- /dev/null +++ b/Mohem/src/app/hmg-common/hmg_utils.ts @@ -0,0 +1,121 @@ +import { Injectable } from "@angular/core"; +import { BackgroundGeolocation } from '@ionic-native/background-geolocation/ngx'; +import { CommonService } from "src/app/hmg-common/services/common/common.service"; +import { TranslatorService } from "src/app/hmg-common/services/translator/translator.service"; +import { DevicePermissionsService } from 'src/app/hmg-common/services/device-permissions/device-permissions.service'; +import { Geolocation } from '@ionic-native/geolocation/ngx'; +import { Platform } from "@ionic/angular"; + +declare var cordova: any; + +@Injectable({ + providedIn: 'root' +}) +export class HMGUtils { + constructor( + public backgroundGeolocation: BackgroundGeolocation, + public common: CommonService, + public ts: TranslatorService, + private geolocation: Geolocation, + public platform: Platform, + public devicePermissionsService:DevicePermissionsService, + ) { } + + async getCurrentLocation(callBack: Function) { + if (this.platform.is('android')) { + this.devicePermissionsService.requestLocationAutherization().then( async granted => { + if(granted == true){ + if ((await this.isHuaweiDevice())) { + this.getHMSLocation(callBack); + } else { + this.getGMSLocation(callBack); + } + }else{ + this.common.presentAlert(this.ts.trPK('general', 'location-permission-dialog')); + } + }); + } else { + this.getIOSLocation(callBack); + } + } + + + async isHuaweiDevice(): Promise { + var result = await new Promise((resolve, reject) => { + cordova.plugins.CordovaHMSGMSCheckPlugin.isHmsAvailable( + "index.js", + (_res) => { + var hmsAvailable = _res === "true"; + resolve(hmsAvailable); + }, + (_err) => { + reject({ "status": false, "error": JSON.stringify(_err) }); + alert("Error checking device HMS/GMS") + } + ); + }); + + return (result == true); + } + + private getHMSLocation(callBack: Function) { + try { + + console.log("Huawei Location [Getting]"); + cordova.plugins.CordovaHMSLocationPlugin.requestLocation( + "index.js", + (_res) => { + console.log("Huawei Location Success: [Stop Getting]"); + cordova.plugins.CordovaHMSLocationPlugin.removeLocation("", (_res) => { }, (_err) => { }); + var location = _res.split(',') + if (location.length == 2) { + var lat = Number(_res.split(',')[0]); + var long = Number(_res.split(',')[1]); + callBack({"latitude":lat, "longitude":long, "isfake":false}) + } else { + alert("Invalid Huawei location from plugin."); + } + }, + (_err) => { + console.log("Huawei Location [Getting Error]: " + + JSON.stringify(_err)); + alert("Error while getting Huawei location"); + } + ); + + } catch (_err) { + console.log("Huawei Location Plugin [Error]: " + + JSON.stringify(_err)); + alert("Error while initializing Huawei Location Plugin"); + } + } + + private getGMSLocation(callBack: Function) { + this.backgroundGeolocation.getCurrentLocation({ timeout: 10000, enableHighAccuracy: true, maximumAge: 3000 }).then((resp) => { + if (resp && (resp.latitude && resp.longitude)) { + var isFakeLocation = resp.isFromMockProvider || resp.mockLocationsEnabled; + var lat = resp.latitude; + var long = resp.longitude; + callBack({"latitude":lat, "longitude":long, "isfake":isFakeLocation}) + } else { + this.common.presentAlert(this.ts.trPK('home', 'position-error')); + } + }, (error) => { + this.common.presentAlert(this.ts.trPK('home', 'position-error')); + }); + } + + private getIOSLocation(callBack: Function) { + this.geolocation.getCurrentPosition({ maximumAge: 3000, timeout: 10000, enableHighAccuracy: true }).then(resp => { + if (resp && resp.coords.latitude && resp.coords.longitude) { + var lat = resp.coords.latitude; + var long = resp.coords.longitude; + callBack({"latitude":lat, "longitude":long, "isfake":false}) + } else { + this.common.presentAlert(this.ts.trPK('home', 'position-error')); + } + }).catch(error => { + this.common.presentAlert(this.ts.trPK('home', 'position-error')); + }); + } + + +} \ No newline at end of file diff --git a/Mohem/src/app/hmg-common/services/authentication/authentication.service.ts b/Mohem/src/app/hmg-common/services/authentication/authentication.service.ts index 018cb146..0aeb7969 100644 --- a/Mohem/src/app/hmg-common/services/authentication/authentication.service.ts +++ b/Mohem/src/app/hmg-common/services/authentication/authentication.service.ts @@ -133,7 +133,7 @@ export class AuthenticationService { }else{ mobileType = 'android'; } - request.VersionID = 2.8; + request.VersionID = 2.7; request.Channel = 31; request.LanguageID = TranslatorService.getCurrentLanguageCode(); request.MobileType = mobileType; diff --git a/Mohem/src/app/hmg-common/services/connector/connector.service.ts b/Mohem/src/app/hmg-common/services/connector/connector.service.ts index e0e27780..5b221dfc 100644 --- a/Mohem/src/app/hmg-common/services/connector/connector.service.ts +++ b/Mohem/src/app/hmg-common/services/connector/connector.service.ts @@ -23,8 +23,8 @@ export class ConnectorService { public static retryTimes = 0; public static timeOut = 120 * 1000; -public static host = 'https://uat.hmgwebservices.com/'; - // public static host = 'https://hmgwebservices.com/'; +// public static host = 'https://uat.hmgwebservices.com/'; + public static host = 'https://hmgwebservices.com/'; constructor(public httpClient: HttpClient, public cs: CommonService) {} diff --git a/Mohem/src/app/home/attendance-options/attendance-options.component.ts b/Mohem/src/app/home/attendance-options/attendance-options.component.ts index 6cc32f42..eb9942f2 100644 --- a/Mohem/src/app/home/attendance-options/attendance-options.component.ts +++ b/Mohem/src/app/home/attendance-options/attendance-options.component.ts @@ -17,8 +17,11 @@ import { WifiWizard2 } from "@ionic-native/wifi-wizard-2/ngx"; import { OpenNativeSettings } from '@ionic-native/open-native-settings/ngx'; import { DevicePermissionsService } from 'src/app/hmg-common/services/device-permissions/device-permissions.service'; import { Geolocation } from '@ionic-native/geolocation/ngx'; -import { BackgroundGeolocation } from '@ionic-native/background-geolocation/ngx'; +import { BackgroundGeolocation } from '@ionic-native/background-geolocation/index'; +import { HMGUtils } from 'src/app/hmg-common/hmg_utils'; +declare var cordova: any; +var FORCE_LOCATION = true; @Component({ selector: "app-attendance-options", @@ -42,6 +45,7 @@ export class AttendanceOptionsComponent implements OnInit { public longt = 0; public isFakeLocationUsed = false; public deviceID: string; + public dontForceLocation = true; constructor( private nfc: NFC, @@ -55,10 +59,11 @@ export class AttendanceOptionsComponent implements OnInit { public ngZone: NgZone, private wifiWizard2: WifiWizard2, private openNativeSettings: OpenNativeSettings, - private devicePermissionsService:DevicePermissionsService, + private devicePermissionsService: DevicePermissionsService, private geolocation: Geolocation, - public backgroundGeolocation: BackgroundGeolocation - ) {} + public backgroundGeolocation: BackgroundGeolocation, + public hmgUtils: HMGUtils + ) { } ngOnInit() { this.direction = TranslatorService.getCurrentLanguageName(); @@ -157,7 +162,7 @@ export class AttendanceOptionsComponent implements OnInit { component: NfcModalComponent, showBackdrop: true, backdropDismiss: true, - componentProps:{ enableLocationNFC: this.enableLocationNFC, lat: this.lat, longt: this.longt}, + componentProps: { enableLocationNFC: this.enableLocationNFC, lat: this.lat, longt: this.longt }, }); modal.cssClass = "nfc-modal"; await modal.present(); @@ -199,25 +204,42 @@ export class AttendanceOptionsComponent implements OnInit { } } + public startQRCode() { - if (this.enableLocationQR) { - this.attendScanService.getDeviceLocation(); + if (this.enableLocationQR || FORCE_LOCATION) { + this.hmgUtils.getCurrentLocation((data) => { + console.log(data); + this.attendScanService.lat = data.latitude; + this.attendScanService.longt = data.longitude; + (data.isfake == true) ? this.attendScanService.fakeSwipeAttendance() : this.attendScanService.attendance(false); + }); + } else { this.attendScanService.attendance(false); } } public startNFCCode() { - if (this.enableLocationNFC) { - this.getDeviceLocation('NFC'); + if (this.enableLocationNFC || FORCE_LOCATION) { + this.hmgUtils.getCurrentLocation((data) => { + console.log(data); + this.lat = data.latitude; + this.longt = data.longitude; + (data.isfake == true) ? this.fakeSwipeAttendance('WIFI') : this.checkNFCStatus('two'); + }); } else { this.checkNFCStatus('two'); } } public startWIFICode() { - if (this.enableLocationWIFI) { - this.getDeviceLocation('WIFI'); + if (this.enableLocationWIFI || FORCE_LOCATION) { + this.hmgUtils.getCurrentLocation((data) => { + console.log(data); + this.lat = data.latitude; + this.longt = data.longitude; + (data.isfake == true) ? this.fakeSwipeAttendance('WIFI') : this.startWifi(); + }); } else { this.startWifi(); } @@ -230,93 +252,44 @@ export class AttendanceOptionsComponent implements OnInit { const isWifiEnabled = await this.wifiWizard2.isWifiEnabled(); if (isIOS) { - if(!isWifiEnabled){ + if (!isWifiEnabled) { this.showWifiNotEnabled(); return; } - } else if(isAndroid) { + } else if (isAndroid) { let locationPermission = await this.devicePermissionsService.requestLocationAutherization(); if (!locationPermission) { return; } - if(!isWifiEnabled){ - this.wifiWizard2.setWifiEnabled(true).then(()=>{ + if (!isWifiEnabled) { + this.wifiWizard2.setWifiEnabled(true).then(() => { this.startWifi(); - }).catch((err) => { + }).catch((err) => { console.debug(err); }); return; + } + + // opening wifi dailog if all above conditions are passed (isWifiEnabled == true) + let modal = await this.modalController.create({ + component: WifiModalComponent, + showBackdrop: true, + backdropDismiss: false, + componentProps: { enableLocationWIFI: this.enableLocationWIFI, lat: this.lat, longt: this.longt }, + }); + modal.cssClass = "wifi-modal"; + await modal.present(); } - - // opening wifi dailog if all above conditions are passed (isWifiEnabled == true) - let modal = await this.modalController.create({ - component: WifiModalComponent, - showBackdrop: true, - backdropDismiss: false, - componentProps:{ enableLocationWIFI: this.enableLocationWIFI, lat: this.lat, longt: this.longt}, - }); - modal.cssClass = "wifi-modal"; - await modal.present(); } -} - showWifiNotEnabled(){ - this.common.showErrorMessageDialog(()=>{ + showWifiNotEnabled() { + this.common.showErrorMessageDialog(() => { }, - this.ts.trPK("general", "ok"), - this.ts.trPK("general","wifi-not-enable-text")); - } - - getDeviceLocation(source: string) { - this.isFakeLocationUsed = false; - this.devicePermissionsService.requestLocationAutherization().then(granted => { - this.location = granted as boolean; - if (this.location) { - if (this.platform.is('android')) { - this.backgroundGeolocation.getCurrentLocation({ timeout: 10000, enableHighAccuracy: true, maximumAge: 3000 }).then((resp) => { - if (resp && (resp.latitude && resp.longitude)) { - if (resp.isFromMockProvider || resp.mockLocationsEnabled) { - this.isFakeLocationUsed = true; - this.fakeSwipeAttendance(source); - } else { - this.lat = resp.latitude; - this.longt = resp.longitude; - if (source === 'WIFI') { - this.startWifi(); - } else if (source === 'NFC') { - this.checkNFCStatus('two'); - } - } - } else { - this.common.presentAlert(this.ts.trPK('home', 'position-error')); - } - }, (error) => { - this.common.presentAlert(this.ts.trPK('home', 'position-error')); - }); - } else { - this.geolocation.getCurrentPosition({maximumAge: 3000, timeout: 10000, enableHighAccuracy: true}).then(resp => { - if(resp && resp.coords.latitude && resp.coords.longitude) { - this.lat = resp.coords.latitude; - this.longt = resp.coords.longitude; - if (source === 'WIFI') { - this.startWifi(); - } else if (source === 'NFC') { - this.checkNFCStatus('two'); - } - } else { - this.common.presentAlert(this.ts.trPK('home', 'position-error')); - } - }).catch(error => { - this.common.presentAlert(this.ts.trPK('home', 'position-error')); - }); - } - } else { - return false; - } - }); + this.ts.trPK("general", "ok"), + this.ts.trPK("general", "wifi-not-enable-text")); } fakeSwipeAttendance(sourceName: string) { @@ -328,12 +301,12 @@ export class AttendanceOptionsComponent implements OnInit { request.WifiValue = sourceName === 'WIFI' ? sourceName : ''; request.UID = this.deviceID; request.EmployeeID = this.userData.EMPLOYEE_NUMBER; - this.attendance_service.fakeAttendanceSwipeScanner(request, () => {console.log('Error inside in swipe attendance');}) + this.attendance_service.fakeAttendanceSwipeScanner(request, () => { console.log('Error inside in swipe attendance'); }) .subscribe((result: Response) => { if (this.common.validResponse(result)) { - this.common.presentAlert(this.ts.trPK('home', 'fake-location')); + this.common.presentAlert(this.ts.trPK('home', 'fake-location')); } - }); + }); } }