diff --git a/www/index.js b/www/index.js index bd353618b..0ae3896a2 100644 --- a/www/index.js +++ b/www/index.js @@ -14,7 +14,6 @@ import './js/main.js'; import './js/diary.js'; import './js/diary/services.js'; import './js/survey/enketo/answer.js'; -import './js/control/emailService.js'; import './js/metrics-factory.js'; import './js/metrics-mappings.js'; import './js/plugin/logger.ts'; diff --git a/www/js/control/LogPage.tsx b/www/js/control/LogPage.tsx index ad369fbff..6d603f19e 100644 --- a/www/js/control/LogPage.tsx +++ b/www/js/control/LogPage.tsx @@ -6,13 +6,13 @@ import { useTranslation } from 'react-i18next'; import { FlashList } from '@shopify/flash-list'; import moment from 'moment'; import AlertBar from './AlertBar'; +import { sendEmail } from './emailService'; type loadStats = { currentStart: number; gotMaxIndex: boolean; reachedEnd: boolean }; const LogPage = ({ pageVis, setPageVis }) => { const { t } = useTranslation(); const { colors } = useTheme(); - const EmailHelper = getAngularService('EmailHelper'); const [loadStats, setLoadStats] = useState(); const [entries, setEntries] = useState([]); @@ -96,7 +96,7 @@ const LogPage = ({ pageVis, setPageVis }) => { }; const emailLog = function () { - EmailHelper.sendEmail('loggerDB'); + sendEmail('loggerDB'); }; const separator = () => ; diff --git a/www/js/control/ProfileSettings.jsx b/www/js/control/ProfileSettings.jsx index b081e642a..f239b8b29 100644 --- a/www/js/control/ProfileSettings.jsx +++ b/www/js/control/ProfileSettings.jsx @@ -13,7 +13,7 @@ import useAppConfig from '../useAppConfig'; import AlertBar from './AlertBar'; import DataDatePicker from './DataDatePicker'; import PrivacyPolicyModal from './PrivacyPolicyModal'; - +import { sendEmail } from './emailService'; import { uploadFile } from './uploadService'; import ActionMenu from '../components/ActionMenu'; import SensedPage from './SensedPage'; @@ -44,7 +44,6 @@ const ProfileSettings = () => { //angular services needed const CarbonDatasetHelper = getAngularService('CarbonDatasetHelper'); - const EmailHelper = getAngularService('EmailHelper'); const NotificationScheduler = getAngularService('NotificationScheduler'); const ControlHelper = getAngularService('ControlHelper'); @@ -256,7 +255,7 @@ const ProfileSettings = () => { const emailLog = function () { // Passing true, we want to send logs - EmailHelper.sendEmail('loggerDB'); + sendEmail('loggerDB'); }; async function updatePrefReminderTime(storeNewVal = true, newTime) { diff --git a/www/js/control/SensedPage.tsx b/www/js/control/SensedPage.tsx index 82fa60581..db1d43535 100644 --- a/www/js/control/SensedPage.tsx +++ b/www/js/control/SensedPage.tsx @@ -5,18 +5,18 @@ import { getAngularService } from '../angular-react-helper'; import { useTranslation } from 'react-i18next'; import { FlashList } from '@shopify/flash-list'; import moment from 'moment'; +import { sendEmail } from './emailService'; const SensedPage = ({ pageVis, setPageVis }) => { const { t } = useTranslation(); const { colors } = useTheme(); - const EmailHelper = getAngularService('EmailHelper'); /* Let's keep a reference to the database for convenience */ const [DB, setDB] = useState(); const [entries, setEntries] = useState([]); const emailCache = function () { - EmailHelper.sendEmail('userCacheDB'); + sendEmail('userCacheDB'); }; async function updateEntries() { diff --git a/www/js/control/emailService.js b/www/js/control/emailService.js deleted file mode 100644 index 8eeaf39bb..000000000 --- a/www/js/control/emailService.js +++ /dev/null @@ -1,114 +0,0 @@ -'use strict'; - -import angular from 'angular'; - -angular - .module('emission.services.email', ['emission.plugin.logger']) - - .service('EmailHelper', function ($window, $http, Logger) { - const getEmailConfig = function () { - return new Promise(function (resolve, reject) { - window.Logger.log(window.Logger.LEVEL_INFO, 'About to get email config'); - var address = []; - $http - .get('json/emailConfig.json') - .then(function (emailConfig) { - window.Logger.log( - window.Logger.LEVEL_DEBUG, - 'emailConfigString = ' + JSON.stringify(emailConfig.data), - ); - address.push(emailConfig.data.address); - resolve(address); - }) - .catch(function (err) { - $http - .get('json/emailConfig.json.sample') - .then(function (emailConfig) { - window.Logger.log( - window.Logger.LEVEL_DEBUG, - 'default emailConfigString = ' + JSON.stringify(emailConfig.data), - ); - address.push(emailConfig.data.address); - resolve(address); - }) - .catch(function (err) { - window.Logger.log( - window.Logger.LEVEL_ERROR, - 'Error while reading default email config' + err, - ); - reject(err); - }); - }); - }); - }; - - const hasAccount = function () { - return new Promise(function (resolve, reject) { - $window.cordova.plugins.email.hasAccount(function (hasAct) { - resolve(hasAct); - }); - }); - }; - - this.sendEmail = function (database) { - Promise.all([getEmailConfig(), hasAccount()]).then(function ([address, hasAct]) { - var parentDir = 'unknown'; - - // Check this only for ios, since for android, the check always fails unless - // the user grants the "GET_ACCOUNTS" dynamic permission - // without the permission, we only see the e-mission account which is not valid - // - // https://developer.android.com/reference/android/accounts/AccountManager#getAccounts() - // - // Caller targeting API level below Build.VERSION_CODES.O that - // have not been granted the Manifest.permission.GET_ACCOUNTS - // permission, will only see those accounts managed by - // AbstractAccountAuthenticators whose signature matches the - // client. - // and on android, if the account is not configured, the gmail app will be launched anyway - // on iOS, nothing will happen. So we perform the check only on iOS so that we can - // generate a reasonably relevant error message - - if (ionic.Platform.isIOS() && !hasAct) { - alert(i18next.t('email-service.email-account-not-configured')); - return; - } - - if (ionic.Platform.isAndroid()) { - parentDir = 'app://databases'; - } - if (ionic.Platform.isIOS()) { - alert(i18next.t('email-service.email-account-mail-app')); - parentDir = cordova.file.dataDirectory + '../LocalDatabase'; - } - - if (parentDir == 'unknown') { - alert('parentDir unexpectedly = ' + parentDir + '!'); - } - - window.Logger.log(window.Logger.LEVEL_INFO, 'Going to email ' + database); - parentDir = parentDir + '/' + database; - /* - window.Logger.log(window.Logger.LEVEL_INFO, - "Going to export logs to "+parentDir); - */ - alert(i18next.t('email-service.going-to-email', { parentDir: parentDir })); - var email = { - to: address, - attachments: [parentDir], - subject: i18next.t('email-service.email-log.subject-logs'), - body: i18next.t('email-service.email-log.body-please-fill-in-what-is-wrong'), - }; - - $window.cordova.plugins.email.open(email, function () { - Logger.log( - 'email app closed while sending, ' + - JSON.stringify(email) + - ' not sure if we should do anything', - ); - // alert(i18next.t('email-service.no-email-address-configured') + err); - return; - }); - }); - }; - }); diff --git a/www/js/control/emailService.ts b/www/js/control/emailService.ts new file mode 100644 index 000000000..3a6e8a5c5 --- /dev/null +++ b/www/js/control/emailService.ts @@ -0,0 +1,52 @@ +import i18next from 'i18next'; +import { logDebug, logInfo, logWarn } from '../plugin/logger'; + +async function hasAccount(): Promise { + return new Promise((resolve, reject) => { + window['cordova'].plugins['email'].hasAccount((hasAct) => { + resolve(hasAct); + }); + }); +} + +export async function sendEmail(database: string) { + let parentDir = 'unknown'; + + if (window['cordova'].platformId == 'ios' && !(await hasAccount())) { + alert(i18next.t('email-service.email-account-not-configured')); + return; + } + + if (window['cordova'].platformId == 'android') { + parentDir = 'app://databases'; + } + + if (window['cordova'].platformId == 'ios') { + alert(i18next.t('email-service.email-account-mail-app')); + logDebug(window['cordova'].file.dataDirectory); + parentDir = window['cordova'].file.dataDirectory + '../LocalDatabase'; + } + + if (parentDir === 'unknown') { + alert('parentDir unexpectedly = ' + parentDir + '!'); + } + + logInfo('Going to email ' + database); + parentDir = parentDir + '/' + database; + + alert(i18next.t('email-service.going-to-email', { parentDir: parentDir })); + + let emailConfig = `k.shankari@nrel.gov`; + + let emailData = { + to: emailConfig, + attachments: [parentDir], + subject: i18next.t('email-service.email-log.subject-logs'), + body: i18next.t('email-service.email-log.body-please-fill-in-what-is-wrong'), + }; + + window['cordova'].plugins['email'].open(emailData, () => { + logWarn(`Email app closed while sending, + emailData = ${JSON.stringify(emailData)}`); + }); +} diff --git a/www/js/ngApp.js b/www/js/ngApp.js index 228c2a989..84b9972c4 100644 --- a/www/js/ngApp.js +++ b/www/js/ngApp.js @@ -40,7 +40,6 @@ angular 'emission.services', 'emission.plugin.logger', 'emission.splash.referral', - 'emission.services.email', 'emission.main', 'pascalprecht.translate', 'LocalStorageModule', diff --git a/www/json/emailConfig.json.sample b/www/json/emailConfig.json.sample deleted file mode 100644 index b1e28e63b..000000000 --- a/www/json/emailConfig.json.sample +++ /dev/null @@ -1,3 +0,0 @@ -{ - "address": "shankari@eecs.berkeley.edu" -} \ No newline at end of file