Skip to content

Commit

Permalink
Fixed a minor issue on headphone_motion sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
tetujin committed Nov 12, 2024
1 parent f55222f commit 3a9d44f
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 308 deletions.
2 changes: 1 addition & 1 deletion AWAREFramework.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'AWAREFramework'
s.version = '1.14.14'
s.version = '1.14.15'
s.summary = 'AWARE: An Open-source Context Instrumentation Framework'

# This description is used to generate tags and improve search results.
Expand Down
14 changes: 11 additions & 3 deletions AWAREFramework/Classes/Plugins/HeadphoneMotion/HeadphoneMotion.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ - (instancetype)initWithAwareStudy:(AWAREStudy *)study dbType:(AwareDBType)dbTyp
self = [super initWithAwareStudy:study
sensorName:SENSOR_PLUGIN_HEADPHONE_MOTION
storage:storage];


if (@available(iOS 14.0, *)) {
sensorManager = [[CMHeadphoneMotionManager alloc] init];
}

return self;
}

Expand Down Expand Up @@ -107,10 +113,12 @@ -(BOOL)startSensor{

if (@available(iOS 14.0, *)) {

NSLog(@"%d", [NSThread isMainThread]);
if ([NSThread isMainThread]) {
if (self.isDebug) NSLog(@"[headphone_motion] the sensor is started in main-thread");
} else {
if (self.isDebug) NSLog(@"[headphone_motion] the sensor is started in NOT main-thread");
}

sensorManager = [[CMHeadphoneMotionManager alloc] init];

if (sensorManager.isDeviceMotionAvailable){
[sensorManager startDeviceMotionUpdatesToQueue:NSOperationQueue.currentQueue
withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {
Expand Down
68 changes: 35 additions & 33 deletions Example/AWARE-SensingApp/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,49 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
core.requestPermissionForPushNotification(completion: nil)
core.activate()

print(cleanOldDataTypeDaily)
AWAREStudy.shared().setCleanOldDataType(cleanOldDataTypeDaily)
AWAREStudy.shared().setDebug(true)
let study = AWAREStudy.shared()

AWAREStudy.shared().setStudyURL("https://YOUR_STUDY_URL")
study.setCleanOldDataType(cleanOldDataTypeDaily)
study.setDebug(true)
// study.setStudyURL("")


print(study.getCleanOldDataType())

print(AWAREStudy.shared().getCleanOldDataType())

let study = AWAREStudy.shared()
// init sensors
let accelerometer = Accelerometer(awareStudy: study)
accelerometer.setSensingIntervalWithHz(100)
accelerometer.setSavingIntervalWithSecond(5)
(accelerometer.storage as! SQLiteSeparatedStorage).fetchSizeAdjuster.setMaxFetchSize(2);
accelerometer.setSavingIntervalWithSecond(3)
(accelerometer.storage as! SQLiteSeparatedStorage).fetchSizeAdjuster.setMaxFetchSize(3);
accelerometer.storage?.setDebug(true)

let gyroscope = Gyroscope(awareStudy: study)
gyroscope.setSensingIntervalWithHz(100)
gyroscope.setSavingIntervalWithSecond(5)
gyroscope.setSavingIntervalWithSecond(3)
(gyroscope.storage as! SQLiteSeparatedStorage).fetchSizeAdjuster.setMaxFetchSize(3);


let rotation = Rotation(awareStudy: study)
rotation.setSensingIntervalWithHz(100)
rotation.setSavingIntervalWithSecond(5)
rotation.setSavingIntervalWithSecond(3)
(rotation.storage as! SQLiteSeparatedStorage).fetchSizeAdjuster.setMaxFetchSize(3);

let lAccelerometer = LinearAccelerometer(awareStudy: study)
lAccelerometer.setSensingIntervalWithHz(100)
lAccelerometer.setSavingIntervalWithSecond(5)
lAccelerometer.setSavingIntervalWithSecond(3)
(lAccelerometer.storage as! SQLiteSeparatedStorage).fetchSizeAdjuster.setMaxFetchSize(3);

let mag = Magnetometer(awareStudy: study)
mag.setSensingIntervalWithHz(100)
mag.setSavingIntervalWithSecond(5)
mag.setSavingIntervalWithSecond(3)
(mag.storage as! SQLiteSeparatedStorage).fetchSizeAdjuster.setMaxFetchSize(3);

let headphone = HeadphoneMotion(awareStudy: study)
headphone.setSensingIntervalWithHz(100)
headphone.setSavingIntervalWithSecond(5)
(headphone.storage as! SQLiteSeparatedStorage).fetchSizeAdjuster.setMaxFetchSize(3);
headphone.setSavingIntervalWithSecond(1)
(headphone.storage as! SQLiteSeparatedStorage).fetchSizeAdjuster.setMaxFetchSize(2);
headphone.storage?.setDebug(true)

// let battery = Battery()
// let screen = Screen()
Expand All @@ -85,28 +87,28 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
self.sensorManager.add([accelerometer,gyroscope,rotation,lAccelerometer,mag,headphone])
// self.sensorManager.add(accelerometer)
// self.sensorManager.add([accelerometer, gyroscope, battery, screen, call, ambientNoise, activity, step, bluetooth, cal, healthKit])
self.sensorManager.startAllSensors()
// self.sensorManager.startAllSensors()

// setup ESMs
// generate ESMItem
let pam = ESMItem(asPAMESMWithTrigger: "pam")
pam.setTitle("How do you feeling now?")
pam.setInstructions("Please select an image.")

// generate ESMSchedule
let esm = ESMSchedule()
esm.scheduleId = "schedule_1"
esm.startDate = Date()
esm.endDate = Date().addingTimeInterval(60*60*24*31)
esm.fireHours = [8,12,21]
esm.expirationThreshold = 60
esm.addESM(pam)
esm.notificationTitle = "Tap to answer the question."

// add the ESMSchedules into ESMScheduleManager
let esmManager = ESMScheduleManager.shared()
esmManager.deleteAllSchedules(withNotification: true)
esmManager.add(esm, withNotification: true)
// let pam = ESMItem(asPAMESMWithTrigger: "pam")
// pam.setTitle("How do you feeling now?")
// pam.setInstructions("Please select an image.")
//
// // generate ESMSchedule
// let esm = ESMSchedule()
// esm.scheduleId = "schedule_1"
// esm.startDate = Date()
// esm.endDate = Date().addingTimeInterval(60*60*24*31)
// esm.fireHours = [8,12,21]
// esm.expirationThreshold = 60
// esm.addESM(pam)
// esm.notificationTitle = "Tap to answer the question."
//
// // add the ESMSchedules into ESMScheduleManager
// let esmManager = ESMScheduleManager.shared()
// esmManager.deleteAllSchedules(withNotification: true)
// esmManager.add(esm, withNotification: true)

}

Expand Down
23 changes: 15 additions & 8 deletions Example/AWARE-SensingApp/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="15505" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="v3w-JE-bcQ">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="23504" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="v3w-JE-bcQ">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15510"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="23506"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand All @@ -17,30 +17,38 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Accelerometer" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="hdL-jw-tMA">
<rect key="frame" x="20" y="188" width="374" height="21"/>
<rect key="frame" x="20" y="192" width="374" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Gyroscope" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="tlx-tb-ZpJ">
<rect key="frame" x="20" y="249" width="374" height="21"/>
<rect key="frame" x="20" y="253" width="374" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Battery" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="VYy-Aw-bSU">
<rect key="frame" x="20" y="310" width="374" height="21"/>
<rect key="frame" x="20" y="314" width="374" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Screen" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="z0m-og-7Iw">
<rect key="frame" x="20" y="371" width="374" height="21"/>
<rect key="frame" x="20" y="375" width="374" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="wzg-wF-97w">
<rect key="frame" x="183" y="498" width="49" height="31"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<connections>
<action selector="didChangeSwitch:" destination="BYZ-38-t0r" eventType="valueChanged" id="CRf-6e-hyf"/>
</connections>
</switch>
</subviews>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
<color key="backgroundColor" white="0.66666666669999997" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="VYy-Aw-bSU" secondAttribute="trailing" constant="20" id="3MC-g8-Wwh"/>
Expand All @@ -60,7 +68,6 @@
<constraint firstItem="VYy-Aw-bSU" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="ona-lX-2Mk"/>
<constraint firstItem="VYy-Aw-bSU" firstAttribute="top" secondItem="tlx-tb-ZpJ" secondAttribute="bottom" constant="40" id="pur-8n-fU0"/>
</constraints>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>
<navigationItem key="navigationItem" id="wq7-Hi-vIc"/>
<connections>
Expand All @@ -80,7 +87,7 @@
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="v3w-JE-bcQ" sceneMemberID="viewController">
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" id="CM6-Ti-UQl">
<rect key="frame" x="0.0" y="44" width="414" height="44"/>
<rect key="frame" x="0.0" y="48" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<nil name="viewControllers"/>
Expand Down
41 changes: 41 additions & 0 deletions Example/AWARE-SensingApp/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,54 @@ class ViewController: UIViewController {
if let status = data["screen_status"] as? Int {
self.screenLabel.text = "\(status)"
}
} else if SENSOR_PLUGIN_HEADPHONE_MOTION == sensorName {
// print(data);
// if let x = data["double_values_0"] as? Double,
// let y = data["double_values_1"] as? Double,
// let z = data["double_values_2"] as? Double {
// print("\(String(format: "%.2f", x)),\(String(format: "%.2f", y)),\(String(format: "%.2f", z))")
// }
}
}
}

}
}

@IBAction func didChangeSwitch(_ sender: UISwitch) {
if (sender.isOn){
AWARESensorManager.shared().startAllSensors()
}else{
AWARESensorManager.shared().stopAllSensors()
AWARESensorManager.shared().setDebugToAllSensors(true)
AWARESensorManager.shared().setSyncProcessCallbackToAllSensorStorages { sensorName, progress, a, error in
switch (progress) {
case .cancel:
print("cancel")
case .complete:
print("complete")
case .continue:
print("continue")
case .error:
print("error")
case .locked:
print("locked")
case .unknown:
print("unknown")
case .uploading:
print("uploading")
@unknown default:
print("unknown---")
}

print(sensorName, progress.rawValue, a, error ?? "")
}
AWARESensorManager.shared().syncAllSensorsForcefully()

}
}


@objc func willEnterForegroundNotification(notification: NSNotification) {
UIApplication.shared.applicationIconBadgeNumber = 0
checkESMSchedules()
Expand Down
Loading

0 comments on commit 3a9d44f

Please sign in to comment.