From 3aadb92250b71d45e8b8762180132f0dac48cddd Mon Sep 17 00:00:00 2001 From: TebbeUbben Date: Fri, 20 Aug 2021 19:37:38 +0200 Subject: [PATCH] Update Open Humans uploader for new database --- app/build.gradle | 1 + app/src/main/AndroidManifest.xml | 14 - .../activities/MyPreferenceFragment.kt | 2 +- .../dependencyInjection/ActivitiesModule.kt | 2 - .../dependencyInjection/AppComponent.kt | 5 +- .../dependencyInjection/FragmentsModule.kt | 6 - .../dependencyInjection/OHUploaderModule.kt | 12 - .../dependencyInjection/PluginsModule.kt | 7 + .../openAPSAMA/DetermineBasalAdapterAMAJS.kt | 3 - .../openAPSSMB/DetermineBasalAdapterSMBJS.kt | 3 - .../general/openhumans/OHUploadWorker.kt | 70 -- .../general/openhumans/OpenHumansAPI.kt | 191 ----- .../general/openhumans/OpenHumansFragment.kt | 114 --- .../openhumans/OpenHumansLoginActivity.kt | 138 ---- .../general/openhumans/OpenHumansUploader.kt | 651 ---------------- app/src/main/res/drawable/open_humans.xml | 10 - .../res/layout/activity_open_humans_login.xml | 58 -- app/src/main/res/values/strings.xml | 31 - build.gradle | 4 +- .../androidaps/database/AppRepository.kt | 21 +- .../androidaps/database/daos/APSResultDao.kt | 4 +- .../database/daos/APSResultLinkDao.kt | 4 +- .../database/daos/BolusCalculatorResultDao.kt | 4 + .../androidaps/database/daos/BolusDao.kt | 5 + .../androidaps/database/daos/CarbsDao.kt | 4 + .../daos/EffectiveProfileSwitchDao.kt | 5 + .../database/daos/ExtendedBolusDao.kt | 5 + .../database/daos/GlucoseValueDao.kt | 5 + .../database/daos/MultiwaveBolusLinkDao.kt | 5 + .../database/daos/OfflineEventDao.kt | 5 + .../database/daos/PreferenceChangeDao.kt | 5 + .../database/daos/ProfileSwitchDao.kt | 5 + .../database/daos/TemporaryBasalDao.kt | 5 + .../database/daos/TemporaryTargetDao.kt | 5 + .../database/daos/TherapyEventDao.kt | 5 + .../database/daos/TotalDailyDoseDao.kt | 5 + .../database/daos/VersionChangeDao.kt | 5 + .../androidaps/database/data/NewEntries.kt | 23 + openhumans/.gitignore | 1 + openhumans/build.gradle | 15 + openhumans/consumer-rules.pro | 0 openhumans/proguard-rules.pro | 21 + openhumans/src/main/AndroidManifest.xml | 24 + .../openhumans/AllowedPreferenceKeys.kt | 6 +- .../general/openhumans/OpenHumansAPI.kt | 182 +++++ .../general/openhumans/OpenHumansState.kt | 9 + .../general/openhumans/OpenHumansUploader.kt | 656 ++++++++++++++++ .../general/openhumans/OpenHumansWorker.kt | 38 + .../general/openhumans/dagger/Helpers.kt | 40 + .../openhumans/dagger/OpenHumansModule.kt | 58 ++ .../openhumans/delegates/OHAppIDDelegate.kt | 28 + .../openhumans/delegates/OHCounterDelegate.kt | 20 + .../openhumans/delegates/OHStateDelegate.kt | 51 ++ .../general/openhumans/ui/OHFragment.kt | 70 ++ .../general/openhumans/ui/OHLoginActivity.kt | 121 +++ .../general/openhumans/ui/OHLoginViewModel.kt | 72 ++ openhumans/src/main/res/drawable/dot.xml | 6 + .../src/main/res/drawable/open_humans.xml | 10 + .../res/drawable/open_humans_notification.xml | 24 + .../main/res/drawable/open_humans_white.xml | 0 .../src/main/res/drawable/tick_mark.xml | 12 + openhumans/src/main/res/font/montserrat.xml | 19 + .../src/main/res/font/montserrat_bold.ttf | Bin 0 -> 244468 bytes .../src/main/res/font/montserrat_light.ttf | Bin 0 -> 242068 bytes .../src/main/res/font/montserrat_medium.ttf | Bin 0 -> 243180 bytes .../src/main/res/font/montserrat_regular.ttf | Bin 0 -> 245708 bytes .../layout/activity_open_humans_login_new.xml | 723 ++++++++++++++++++ .../res/layout/fragment_open_humans_new.xml | 63 +- openhumans/src/main/res/values/colors.xml | 7 + openhumans/src/main/res/values/strings.xml | 20 + openhumans/src/main/res/values/styles.xml | 13 + .../src/main/res/xml/pref_openhumans.xml | 22 + settings.gradle | 1 + 73 files changed, 2437 insertions(+), 1342 deletions(-) delete mode 100644 app/src/main/java/info/nightscout/androidaps/dependencyInjection/OHUploaderModule.kt delete mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OHUploadWorker.kt delete mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansAPI.kt delete mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansFragment.kt delete mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansLoginActivity.kt delete mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansUploader.kt delete mode 100644 app/src/main/res/drawable/open_humans.xml delete mode 100644 app/src/main/res/layout/activity_open_humans_login.xml create mode 100644 database/src/main/java/info/nightscout/androidaps/database/data/NewEntries.kt create mode 100644 openhumans/.gitignore create mode 100644 openhumans/build.gradle create mode 100644 openhumans/consumer-rules.pro create mode 100644 openhumans/proguard-rules.pro create mode 100644 openhumans/src/main/AndroidManifest.xml rename {app/src/main/java/info/nightscout/androidaps/plugins => openhumans/src/main/java/info/nightscout/androidaps/plugin}/general/openhumans/AllowedPreferenceKeys.kt (95%) create mode 100644 openhumans/src/main/java/info/nightscout/androidaps/plugin/general/openhumans/OpenHumansAPI.kt create mode 100644 openhumans/src/main/java/info/nightscout/androidaps/plugin/general/openhumans/OpenHumansState.kt create mode 100644 openhumans/src/main/java/info/nightscout/androidaps/plugin/general/openhumans/OpenHumansUploader.kt create mode 100644 openhumans/src/main/java/info/nightscout/androidaps/plugin/general/openhumans/OpenHumansWorker.kt create mode 100644 openhumans/src/main/java/info/nightscout/androidaps/plugin/general/openhumans/dagger/Helpers.kt create mode 100644 openhumans/src/main/java/info/nightscout/androidaps/plugin/general/openhumans/dagger/OpenHumansModule.kt create mode 100644 openhumans/src/main/java/info/nightscout/androidaps/plugin/general/openhumans/delegates/OHAppIDDelegate.kt create mode 100644 openhumans/src/main/java/info/nightscout/androidaps/plugin/general/openhumans/delegates/OHCounterDelegate.kt create mode 100644 openhumans/src/main/java/info/nightscout/androidaps/plugin/general/openhumans/delegates/OHStateDelegate.kt create mode 100644 openhumans/src/main/java/info/nightscout/androidaps/plugin/general/openhumans/ui/OHFragment.kt create mode 100644 openhumans/src/main/java/info/nightscout/androidaps/plugin/general/openhumans/ui/OHLoginActivity.kt create mode 100644 openhumans/src/main/java/info/nightscout/androidaps/plugin/general/openhumans/ui/OHLoginViewModel.kt create mode 100644 openhumans/src/main/res/drawable/dot.xml create mode 100644 openhumans/src/main/res/drawable/open_humans.xml create mode 100644 openhumans/src/main/res/drawable/open_humans_notification.xml rename {app => openhumans}/src/main/res/drawable/open_humans_white.xml (100%) create mode 100644 openhumans/src/main/res/drawable/tick_mark.xml create mode 100644 openhumans/src/main/res/font/montserrat.xml create mode 100644 openhumans/src/main/res/font/montserrat_bold.ttf create mode 100644 openhumans/src/main/res/font/montserrat_light.ttf create mode 100644 openhumans/src/main/res/font/montserrat_medium.ttf create mode 100644 openhumans/src/main/res/font/montserrat_regular.ttf create mode 100644 openhumans/src/main/res/layout/activity_open_humans_login_new.xml rename app/src/main/res/layout/fragment_open_humans.xml => openhumans/src/main/res/layout/fragment_open_humans_new.xml (59%) create mode 100644 openhumans/src/main/res/values/colors.xml create mode 100644 openhumans/src/main/res/values/strings.xml create mode 100644 openhumans/src/main/res/values/styles.xml create mode 100644 openhumans/src/main/res/xml/pref_openhumans.xml diff --git a/app/build.gradle b/app/build.gradle index 5118670c6c..026ac17eaf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -185,6 +185,7 @@ dependencies { implementation project(':omnipod-eros') implementation project(':omnipod-dash') implementation project(':diaconn') + implementation project(':openhumans') implementation fileTree(include: ['*.jar'], dir: 'libs') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ab34d9dfe1..f5aa72364e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -243,20 +243,6 @@ - - - - - - - - - - - diff --git a/app/src/main/java/info/nightscout/androidaps/activities/MyPreferenceFragment.kt b/app/src/main/java/info/nightscout/androidaps/activities/MyPreferenceFragment.kt index 4c253c55f8..eb74f7b8cf 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/MyPreferenceFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/activities/MyPreferenceFragment.kt @@ -20,6 +20,7 @@ import info.nightscout.androidaps.events.EventPreferenceChange import info.nightscout.androidaps.events.EventRebuildTabs import info.nightscout.androidaps.interfaces.PluginBase import info.nightscout.androidaps.interfaces.ProfileFunction +import info.nightscout.androidaps.plugin.general.openhumans.OpenHumansUploader import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin import info.nightscout.androidaps.plugins.aps.openAPSAMA.OpenAPSAMAPlugin import info.nightscout.androidaps.plugins.aps.openAPSSMB.OpenAPSSMBPlugin @@ -30,7 +31,6 @@ import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin import info.nightscout.androidaps.plugins.general.maintenance.MaintenancePlugin import info.nightscout.androidaps.plugins.general.nsclient.NSClientPlugin import info.nightscout.androidaps.plugins.general.nsclient.data.NSSettingsStatus -import info.nightscout.androidaps.plugins.general.openhumans.OpenHumansUploader import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin import info.nightscout.androidaps.plugins.general.tidepool.TidepoolPlugin import info.nightscout.androidaps.plugins.general.wear.WearPlugin diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/ActivitiesModule.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/ActivitiesModule.kt index 08e25f456f..eff546fb62 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/ActivitiesModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/ActivitiesModule.kt @@ -6,7 +6,6 @@ import info.nightscout.androidaps.MainActivity import info.nightscout.androidaps.activities.* import info.nightscout.androidaps.activities.HistoryBrowseActivity import info.nightscout.androidaps.plugins.general.maintenance.activities.LogSettingActivity -import info.nightscout.androidaps.plugins.general.openhumans.OpenHumansLoginActivity import info.nightscout.androidaps.plugins.general.overview.activities.QuickWizardListActivity import info.nightscout.androidaps.plugins.general.smsCommunicator.activities.SmsCommunicatorOtpActivity import info.nightscout.androidaps.setupwizard.SetupWizardActivity @@ -28,6 +27,5 @@ abstract class ActivitiesModule { @ContributesAndroidInjector abstract fun contributesStatsActivity(): StatsActivity @ContributesAndroidInjector abstract fun contributesSurveyActivity(): SurveyActivity @ContributesAndroidInjector abstract fun contributesDefaultProfileActivity(): ProfileHelperActivity - @ContributesAndroidInjector abstract fun contributesOpenHumansLoginActivity(): OpenHumansLoginActivity } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt index 5432082ada..73ada1ebf6 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt @@ -16,6 +16,7 @@ import info.nightscout.androidaps.di.CoreModule import info.nightscout.androidaps.diaconn.di.DiaconnG8Module import info.nightscout.androidaps.insight.di.InsightDatabaseModule import info.nightscout.androidaps.insight.di.InsightModule +import info.nightscout.androidaps.plugin.general.openhumans.dagger.OpenHumansModule import info.nightscout.androidaps.plugins.pump.common.di.PumpCommonModule import info.nightscout.androidaps.plugins.pump.common.di.RileyLinkModule import info.nightscout.androidaps.plugins.pump.medtronic.di.MedtronicModule @@ -57,8 +58,8 @@ import javax.inject.Singleton InsightModule::class, InsightDatabaseModule::class, WorkersModule::class, - OHUploaderModule::class, - DiaconnG8Module::class + DiaconnG8Module::class, + OpenHumansModule::class ] ) interface AppComponent : AndroidInjector { diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/FragmentsModule.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/FragmentsModule.kt index 84e4ef80fb..f758398d62 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/FragmentsModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/FragmentsModule.kt @@ -20,8 +20,6 @@ import info.nightscout.androidaps.plugins.general.automation.dialogs.EditTrigger import info.nightscout.androidaps.plugins.general.food.FoodFragment import info.nightscout.androidaps.plugins.general.maintenance.MaintenanceFragment import info.nightscout.androidaps.plugins.general.nsclient.NSClientFragment -import info.nightscout.androidaps.plugins.general.openhumans.OpenHumansFragment -import info.nightscout.androidaps.plugins.general.openhumans.OpenHumansLoginActivity import info.nightscout.androidaps.plugins.general.overview.OverviewFragment import info.nightscout.androidaps.plugins.general.overview.dialogs.EditQuickWizardDialog import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorFragment @@ -70,8 +68,6 @@ abstract class FragmentsModule { @ContributesAndroidInjector abstract fun contributesVirtualPumpFragment(): VirtualPumpFragment - @ContributesAndroidInjector abstract fun contributesOpenHumansFragment(): OpenHumansFragment - @ContributesAndroidInjector abstract fun contributesCalibrationDialog(): CalibrationDialog @ContributesAndroidInjector abstract fun contributesCarbsDialog(): CarbsDialog @ContributesAndroidInjector abstract fun contributesCareDialog(): CareDialog @@ -95,7 +91,5 @@ abstract class FragmentsModule { @ContributesAndroidInjector abstract fun contributesWizardDialog(): WizardDialog @ContributesAndroidInjector abstract fun contributesWizardInfoDialog(): WizardInfoDialog - @ContributesAndroidInjector abstract fun contributesExchangeAuthTokenDialog(): OpenHumansLoginActivity.ExchangeAuthTokenDialog - @ContributesAndroidInjector abstract fun contributesPasswordCheck(): PasswordCheck } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/OHUploaderModule.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/OHUploaderModule.kt deleted file mode 100644 index bbf0765f96..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/OHUploaderModule.kt +++ /dev/null @@ -1,12 +0,0 @@ -package info.nightscout.androidaps.dependencyInjection - -import dagger.Module -import dagger.android.ContributesAndroidInjector -import info.nightscout.androidaps.plugins.general.openhumans.OHUploadWorker - -@Module -@Suppress("unused") -abstract class OHUploaderModule { - - @ContributesAndroidInjector abstract fun contributesOHUploadWorkerInjector(): OHUploadWorker -} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/PluginsModule.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/PluginsModule.kt index 7b467893d3..cd8e179160 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/PluginsModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/PluginsModule.kt @@ -10,6 +10,7 @@ import info.nightscout.androidaps.danar.DanaRPlugin import info.nightscout.androidaps.danars.DanaRSPlugin import info.nightscout.androidaps.diaconn.DiaconnG8Plugin import info.nightscout.androidaps.interfaces.PluginBase +import info.nightscout.androidaps.plugin.general.openhumans.OpenHumansUploader import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin import info.nightscout.androidaps.plugins.aps.openAPSAMA.OpenAPSAMAPlugin import info.nightscout.androidaps.plugins.aps.openAPSSMB.OpenAPSSMBPlugin @@ -345,6 +346,12 @@ abstract class PluginsModule { // @IntKey(480) // abstract fun bindOpenHumansPlugin(plugin: OpenHumansUploader): PluginBase + @Binds + @NotNSClient + @IntoMap + @IntKey(480) + abstract fun bindsOpenHumansPlugin(plugin: OpenHumansUploader): PluginBase + @Binds @AllConfigs @IntoMap diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/DetermineBasalAdapterAMAJS.kt b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/DetermineBasalAdapterAMAJS.kt index adb15e8b29..5eb53e3e46 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/DetermineBasalAdapterAMAJS.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/DetermineBasalAdapterAMAJS.kt @@ -17,7 +17,6 @@ import info.nightscout.androidaps.plugins.aps.logger.LoggerCallback import info.nightscout.androidaps.plugins.aps.loop.ScriptReader import info.nightscout.androidaps.plugins.aps.openAPSSMB.SMBDefaults import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker -import info.nightscout.androidaps.plugins.general.openhumans.OpenHumansUploader import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus import info.nightscout.androidaps.utils.sharedPreferences.SP import org.json.JSONArray @@ -40,7 +39,6 @@ class DetermineBasalAdapterAMAJS internal constructor(scriptReader: ScriptReader @Inject lateinit var sp: SP @Inject lateinit var profileFunction: ProfileFunction @Inject lateinit var iobCobCalculator: IobCobCalculator - @Inject lateinit var openHumansUploader: OpenHumansUploader private val mScriptReader: ScriptReader private var profile = JSONObject() @@ -116,7 +114,6 @@ class DetermineBasalAdapterAMAJS internal constructor(scriptReader: ScriptReader aapsLogger.debug(LTag.APS, "Result: $result") try { val resultJson = JSONObject(result) - openHumansUploader.enqueueAMAData(profile, glucoseStatus, iobData, mealData, currentTemp, autosensData, resultJson) determineBasalResultAMA = DetermineBasalResultAMA(injector, jsResult, resultJson) } catch (e: JSONException) { aapsLogger.error(LTag.APS, "Unhandled exception", e) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/DetermineBasalAdapterSMBJS.kt b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/DetermineBasalAdapterSMBJS.kt index b32682c102..bdb1039bd2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/DetermineBasalAdapterSMBJS.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/DetermineBasalAdapterSMBJS.kt @@ -17,7 +17,6 @@ import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.aps.logger.LoggerCallback import info.nightscout.androidaps.plugins.aps.loop.ScriptReader import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker -import info.nightscout.androidaps.plugins.general.openhumans.OpenHumansUploader import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus import info.nightscout.androidaps.utils.SafeParse import info.nightscout.androidaps.utils.resources.ResourceHelper @@ -41,7 +40,6 @@ class DetermineBasalAdapterSMBJS internal constructor(private val scriptReader: @Inject lateinit var profileFunction: ProfileFunction @Inject lateinit var iobCobCalculator: IobCobCalculator @Inject lateinit var activePlugin: ActivePlugin - @Inject lateinit var openHumansUploader: OpenHumansUploader private var profile = JSONObject() private var mGlucoseStatus = JSONObject() @@ -129,7 +127,6 @@ class DetermineBasalAdapterSMBJS internal constructor(private val scriptReader: aapsLogger.debug(LTag.APS, "Result: $result") try { val resultJson = JSONObject(result) - openHumansUploader.enqueueSMBData(profile, mGlucoseStatus, iobData, mealData, currentTemp, autosensData, microBolusAllowed, smbAlwaysAllowed, resultJson) determineBasalResultSMB = DetermineBasalResultSMB(injector, resultJson) } catch (e: JSONException) { aapsLogger.error(LTag.APS, "Unhandled exception", e) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OHUploadWorker.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OHUploadWorker.kt deleted file mode 100644 index 16c9dddec6..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OHUploadWorker.kt +++ /dev/null @@ -1,70 +0,0 @@ -package info.nightscout.androidaps.plugins.general.openhumans - -import android.app.Notification -import android.content.Context -import android.net.wifi.WifiManager -import androidx.core.app.NotificationCompat -import androidx.work.ForegroundInfo -import androidx.work.RxWorker -import androidx.work.WorkerParameters -import info.nightscout.androidaps.MainApp -import info.nightscout.androidaps.plugins.general.openhumans.OpenHumansUploader.Companion.NOTIFICATION_CHANNEL -import info.nightscout.androidaps.plugins.general.openhumans.OpenHumansUploader.Companion.UPLOAD_NOTIFICATION_ID -import info.nightscout.androidaps.utils.resources.ResourceHelper -import info.nightscout.androidaps.utils.sharedPreferences.SP -import io.reactivex.Single -import java.util.concurrent.TimeUnit -import javax.inject.Inject - -class OHUploadWorker(context: Context, workerParameters: WorkerParameters) - : RxWorker(context, workerParameters) { - - @Inject - lateinit var sp: SP - - @Inject - lateinit var openHumansUploader: OpenHumansUploader - - @Inject - lateinit var resourceHelper: ResourceHelper - - @kotlin.ExperimentalStdlibApi - override fun createWork(): Single =Single.just(Result.success()) -/* - = Single.defer { - - // Here we inject every time we create work - // We could build our own WorkerFactory with dagger but this will create conflicts with other Workers - // (see https://medium.com/wonderquill/how-to-pass-custom-parameters-to-rxworker-worker-using-dagger-2-f4cfbc9892ba) - // This class will be replaced with new DB - - (applicationContext as MainApp).androidInjector().inject(this) - - val wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as? WifiManager - val wifiOnly = sp.getBoolean("key_oh_wifi_only", true) - val isConnectedToWifi = wifiManager?.isWifiEnabled ?: false && wifiManager?.connectionInfo?.networkId != -1 - if (!wifiOnly || (wifiOnly && isConnectedToWifi)) { - setForegroundAsync(createForegroundInfo()) - openHumansUploader.uploadDataSegmentally() - .andThen(Single.just(Result.success())) - .onErrorResumeNext { Single.just(Result.retry()) } - } else { - Single.just(Result.retry()) - } - } -*/ - private fun createForegroundInfo(): ForegroundInfo { - val title = resourceHelper.gs(info.nightscout.androidaps.R.string.open_humans) - - val notification: Notification = NotificationCompat.Builder(applicationContext, NOTIFICATION_CHANNEL) - .setContentTitle(title) - .setTicker(title) - .setContentText(resourceHelper.gs(info.nightscout.androidaps.R.string.your_phone_is_upload_data)) - .setSmallIcon(info.nightscout.androidaps.R.drawable.notif_icon) - .setOngoing(true) - .setProgress(0, 0 , true) - .build() - return ForegroundInfo(UPLOAD_NOTIFICATION_ID, notification) - } - -} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansAPI.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansAPI.kt deleted file mode 100644 index 91fa60ca25..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansAPI.kt +++ /dev/null @@ -1,191 +0,0 @@ -package info.nightscout.androidaps.plugins.general.openhumans - -import android.annotation.SuppressLint -import android.util.Base64 -import io.reactivex.Completable -import io.reactivex.Single -import io.reactivex.disposables.Disposables -import okhttp3.* -import okio.BufferedSink -import org.json.JSONArray -import org.json.JSONObject -import java.io.IOException -import java.text.SimpleDateFormat -import java.util.* - -class OpenHumansAPI( - private val baseUrl: String, - clientId: String, - clientSecret: String, - private val redirectUri: String -) { - - private val authHeader = "Basic " + Base64.encodeToString("$clientId:$clientSecret".toByteArray(), Base64.NO_WRAP) - private val client = OkHttpClient() - - fun exchangeAuthToken(code: String): Single = sendTokenRequest(FormBody.Builder() - .add("grant_type", "authorization_code") - .add("redirect_uri", redirectUri) - .add("code", code) - .build()) - - fun refreshAccessToken(refreshToken: String): Single = sendTokenRequest(FormBody.Builder() - .add("grant_type", "refresh_token") - .add("redirect_uri", redirectUri) - .add("refresh_token", refreshToken) - .build()) - - private fun sendTokenRequest(body: FormBody) = Request.Builder() - .url("$baseUrl/oauth2/token/") - .addHeader("Authorization", authHeader) - .post(body) - .build() - .toSingle() - .map { response -> - response.use { _ -> - val responseBody = response.body - val jsonObject = responseBody?.let { JSONObject(it.string()) } - if (!response.isSuccessful) throw OHHttpException(response.code, response.message, jsonObject?.getString("error")) - if (jsonObject == null) throw OHHttpException(response.code, response.message, "No body") - if (!jsonObject.has("expires_in")) throw OHMissingFieldException("expires_in") - OAuthTokens( - accessToken = jsonObject.getString("access_token") - ?: throw OHMissingFieldException("access_token"), - refreshToken = jsonObject.getString("refresh_token") - ?: throw OHMissingFieldException("refresh_token"), - expiresAt = response.sentRequestAtMillis + jsonObject.getInt("expires_in") * 1000L - ) - } - } - - fun getProjectMemberId(accessToken: String): Single = Request.Builder() - .url("$baseUrl/api/direct-sharing/project/exchange-member/?access_token=$accessToken") - .get() - .build() - .toSingle() - .map { - it.jsonBody.getString("project_member_id") - ?: throw OHMissingFieldException("project_member_id") - } - - fun prepareFileUpload(accessToken: String, fileName: String, metadata: FileMetadata): Single = Request.Builder() - .url("$baseUrl/api/direct-sharing/project/files/upload/direct/?access_token=$accessToken") - .post(FormBody.Builder() - .add("filename", fileName) - .add("metadata", metadata.toJSON().toString()) - .build()) - .build() - .toSingle() - .map { - val json = it.jsonBody - PreparedUpload( - fileId = json.getString("id") ?: throw OHMissingFieldException("id"), - uploadURL = json.getString("url") ?: throw OHMissingFieldException("url") - ) - } - - fun uploadFile(url: String, content: ByteArray): Completable = Request.Builder() - .url(url) - .put(object : RequestBody() { - override fun contentType(): MediaType? = null - - override fun contentLength() = content.size.toLong() - - override fun writeTo(sink: BufferedSink) { - sink.write(content) - } - }) - .build() - .toSingle() - .doOnSuccess { response -> - response.use { _ -> - if (!response.isSuccessful) throw OHHttpException(response.code, response.message, null) - } - } - .ignoreElement() - - fun completeFileUpload(accessToken: String, fileId: String): Completable = Request.Builder() - .url("$baseUrl/api/direct-sharing/project/files/upload/complete/?access_token=$accessToken") - .post(FormBody.Builder() - .add("file_id", fileId) - .build()) - .build() - .toSingle() - .doOnSuccess { it.jsonBody } - .ignoreElement() - - private fun Request.toSingle() = Single.create { - val call = client.newCall(this) - call.enqueue(object : Callback { - override fun onFailure(call: Call, e: IOException) { - it.tryOnError(e) - } - - override fun onResponse(call: Call, response: Response) { - it.onSuccess(response) - } - }) - it.setDisposable(Disposables.fromRunnable { call.cancel() }) - } - - private val Response.jsonBody - get() = use { _ -> - val jsonObject = body?.let { JSONObject(it.string()) } - ?: throw OHHttpException(code, message, null) - if (!isSuccessful) throw OHHttpException(code, message, jsonObject.getString("detail")) - jsonObject - } - - data class OAuthTokens( - val accessToken: String, - val refreshToken: String, - val expiresAt: Long - ) - - data class FileMetadata( - val tags: List, - val description: String, - val md5: String? = null, - val creationDate: Long? = null, - val startDate: Long? = null, - val endDate: Long? = null - ) { - - fun toJSON(): JSONObject { - val jsonObject = JSONObject() - jsonObject.put("tags", JSONArray().apply { tags.forEach { put(it) } }) - jsonObject.put("description", description) - jsonObject.put("md5", md5) - creationDate?.let { jsonObject.put("creation_date", iso8601DateFormatter.format(Date(it))) } - startDate?.let { jsonObject.put("start_date", iso8601DateFormatter.format(Date(it))) } - endDate?.let { jsonObject.put("end_date", iso8601DateFormatter.format(Date(it))) } - return jsonObject - } - } - - data class PreparedUpload( - val fileId: String, - val uploadURL: String - ) - - data class OHHttpException( - val code: Int, - val meaning: String, - val detail: String? - ) : RuntimeException() { - - override val message: String get() = toString() - } - - data class OHMissingFieldException( - val name: String - ) : RuntimeException() { - - override val message: String get() = toString() - } - - companion object { - @SuppressLint("SimpleDateFormat") - private val iso8601DateFormatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX") - } -} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansFragment.kt deleted file mode 100644 index 9178e8aca8..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansFragment.kt +++ /dev/null @@ -1,114 +0,0 @@ -package info.nightscout.androidaps.plugins.general.openhumans - -import android.content.Intent -import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.widget.Button -import android.widget.TextView -import androidx.work.WorkManager -import dagger.android.support.DaggerFragment -import info.nightscout.androidaps.R -import info.nightscout.androidaps.events.Event -import info.nightscout.androidaps.plugins.bus.RxBusWrapper -import info.nightscout.androidaps.utils.alertDialogs.OKDialog -import info.nightscout.androidaps.utils.resources.ResourceHelper -import info.nightscout.androidaps.utils.rx.AapsSchedulers -import io.reactivex.BackpressureStrategy -import io.reactivex.Single -import io.reactivex.disposables.CompositeDisposable -import io.reactivex.rxkotlin.plusAssign -import java.util.concurrent.TimeUnit -import javax.inject.Inject - -class OpenHumansFragment : DaggerFragment() { - - private var viewsCreated = false - private var login: Button? = null - private var logout: Button? = null - private var memberId: TextView? = null - private var queueSize: TextView? = null - private var workerState: TextView? = null - private var queueSizeValue = 0L - private val compositeDisposable = CompositeDisposable() - - @Inject lateinit var rxBus: RxBusWrapper - @Inject lateinit var openHumansUploader: OpenHumansUploader - @Inject lateinit var resourceHelper: ResourceHelper - @Inject lateinit var aapsSchedulers: AapsSchedulers - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - // compositeDisposable += Single.fromCallable { databaseHelper.getOHQueueSize() } - // .subscribeOn(aapsSchedulers.io) - // .repeatWhen { - // rxBus.toObservable(UpdateViewEvent::class.java) - // .cast(Any::class.java) - // .mergeWith(rxBus.toObservable(UpdateQueueEvent::class.java) - // .throttleLatest(5, TimeUnit.SECONDS)) - // .toFlowable(BackpressureStrategy.LATEST) - // } - // .observeOn(aapsSchedulers.main) - // .subscribe({ - // queueSizeValue = it - // updateGUI() - // }, {}) - context?.applicationContext?.let { appContext -> - WorkManager.getInstance(appContext).getWorkInfosForUniqueWorkLiveData(OpenHumansUploader.WORK_NAME).observe(this, { - val workInfo = it.lastOrNull() - if (workInfo == null) { - workerState?.visibility = View.GONE - } else { - workerState?.visibility = View.VISIBLE - workerState?.text = getString(R.string.worker_state, workInfo.state.toString()) - } - }) - } - } - - override fun onDestroy() { - compositeDisposable.clear() - super.onDestroy() - } - - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { - val view = inflater.inflate(R.layout.fragment_open_humans, container, false) - login = view.findViewById(R.id.login) - logout = view.findViewById(R.id.logout) - memberId = view.findViewById(R.id.member_id) - queueSize = view.findViewById(R.id.queue_size) - workerState = view.findViewById(R.id.worker_state) - login!!.setOnClickListener { startActivity(Intent(context, OpenHumansLoginActivity::class.java)) } - logout!!.setOnClickListener { - activity?.let { activity -> OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.oh_logout_confirmation)) { openHumansUploader.logout() } } - } - viewsCreated = true - updateGUI() - return view - } - - override fun onDestroyView() { - viewsCreated = false - login = null - logout = null - memberId = null - queueSize = null - super.onDestroyView() - } - - fun updateGUI() { - if (viewsCreated) { - queueSize!!.text = getString(R.string.queue_size, queueSizeValue) - val projectMemberId = openHumansUploader.projectMemberId - memberId!!.text = getString(R.string.project_member_id, projectMemberId - ?: getString(R.string.not_logged_in)) - login!!.visibility = if (projectMemberId == null) View.VISIBLE else View.GONE - logout!!.visibility = if (projectMemberId != null) View.VISIBLE else View.GONE - } - } - - object UpdateViewEvent : Event() - - object UpdateQueueEvent : Event() -} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansLoginActivity.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansLoginActivity.kt deleted file mode 100644 index ecb110d6dd..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/openhumans/OpenHumansLoginActivity.kt +++ /dev/null @@ -1,138 +0,0 @@ -package info.nightscout.androidaps.plugins.general.openhumans - -import android.app.Dialog -import android.content.Intent -import android.net.Uri -import android.os.Bundle -import android.widget.Button -import android.widget.CheckBox -import android.widget.Toast -import androidx.appcompat.app.AlertDialog -import androidx.browser.customtabs.CustomTabsIntent -import androidx.fragment.app.DialogFragment -import androidx.fragment.app.FragmentActivity -import dagger.android.support.DaggerDialogFragment -import info.nightscout.androidaps.R -import info.nightscout.androidaps.activities.NoSplashAppCompatActivity -import info.nightscout.androidaps.utils.rx.AapsSchedulers -import io.reactivex.disposables.Disposable -import javax.inject.Inject - -class OpenHumansLoginActivity : NoSplashAppCompatActivity() { - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(R.layout.activity_open_humans_login) - val button = findViewById