Skip to content

Commit 6f10894

Browse files
Add telem audio type support (#604)
* add telem audio type support * fix chain of responsibility to bluetooth > headphones > speaker > unknown
1 parent 331cefc commit 6f10894

9 files changed

Lines changed: 135 additions & 5 deletions

File tree

mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/MapboxTelemetry.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ private boolean isANavigationEvent(String eventType) {
636636
private void addGeneralNavigationMetadataTo(Hashtable<String, Object> eventWithAttributes) {
637637
eventWithAttributes.put(MapboxNavigationEvent.KEY_DEVICE, Build.MODEL);
638638
eventWithAttributes.put(MapboxNavigationEvent.KEY_VOLUME_LEVEL, TelemetryUtils.getVolumeLevel(context));
639+
eventWithAttributes.put(MapboxNavigationEvent.KEY_AUDIO_TYPE, TelemetryUtils.obtainAudioType(context));
639640
eventWithAttributes.put(MapboxNavigationEvent.KEY_SCREEN_BRIGHTNESS, TelemetryUtils.getScreenBrightness(context));
640641
eventWithAttributes.put(MapboxNavigationEvent.KEY_APPLICATION_STATE, TelemetryUtils.getApplicationState(context));
641642
eventWithAttributes.put(MapboxNavigationEvent.KEY_BATTERY_PLUGGED_IN, isPluggedIn());

mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/navigation/MapboxNavigationEvent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,6 @@ private static Hashtable<String, Object> getMetadata(
300300
event.put(KEY_ORIGINAL_GEOMETRY, originalGeometry);
301301
event.put(KEY_ORIGINAL_ESTIMATED_DISTANCE, originalEstimatedDistance);
302302
event.put(KEY_ORIGINAL_ESTIMATED_DURATION, originalEstimatedDuration);
303-
// audioType may be "null"
304-
addPairIntoEventIfNeeded(event, KEY_AUDIO_TYPE, audioType);
305303
return event;
306304
}
307305

@@ -317,6 +315,8 @@ private static void addPairIntoEventIfNeeded(Hashtable<String, Object> event, St
317315
// See NavigationMetricsWrapper.java in https://github.com/mapbox/mapbox-navigation-android
318316
if (value == null || value.equalsIgnoreCase("null")) {
319317
event.put(key, JSONObject.NULL);
318+
} else {
319+
event.put(key, value);
320320
}
321321
}
322322

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.mapbox.services.android.telemetry.utils;
2+
3+
4+
class AudioTypeChain {
5+
6+
AudioTypeResolver setup() {
7+
AudioTypeResolver unknown = new UnknownAudioType();
8+
AudioTypeResolver speaker = new SpeakerAudioType();
9+
speaker.nextChain(unknown);
10+
AudioTypeResolver headphones = new HeadphonesAudioType();
11+
headphones.nextChain(speaker);
12+
AudioTypeResolver rootOfTheChain = new BluetoothAudioType();
13+
rootOfTheChain.nextChain(headphones);
14+
15+
return rootOfTheChain;
16+
}
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.mapbox.services.android.telemetry.utils;
2+
3+
4+
import android.content.Context;
5+
6+
interface AudioTypeResolver {
7+
void nextChain(AudioTypeResolver chain);
8+
9+
String obtainAudioType(Context context);
10+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.mapbox.services.android.telemetry.utils;
2+
3+
4+
import android.content.Context;
5+
import android.media.AudioManager;
6+
7+
class BluetoothAudioType implements AudioTypeResolver {
8+
private static final String BLUETOOTH = "bluetooth";
9+
private AudioTypeResolver chain;
10+
11+
@Override
12+
public void nextChain(AudioTypeResolver chain) {
13+
this.chain = chain;
14+
}
15+
16+
@Override
17+
public String obtainAudioType(Context context) {
18+
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
19+
if (audioManager.isBluetoothScoOn()) {
20+
return BLUETOOTH;
21+
} else {
22+
return chain.obtainAudioType(context);
23+
}
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.mapbox.services.android.telemetry.utils;
2+
3+
4+
import android.content.Context;
5+
import android.media.AudioManager;
6+
7+
class HeadphonesAudioType implements AudioTypeResolver {
8+
private static final String HEADPHONES = "headphones";
9+
private AudioTypeResolver chain;
10+
11+
@Override
12+
public void nextChain(AudioTypeResolver chain) {
13+
this.chain = chain;
14+
}
15+
16+
@Override
17+
public String obtainAudioType(Context context) {
18+
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
19+
if (audioManager.isWiredHeadsetOn()) {
20+
return HEADPHONES;
21+
} else {
22+
return chain.obtainAudioType(context);
23+
}
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.mapbox.services.android.telemetry.utils;
2+
3+
4+
import android.content.Context;
5+
import android.media.AudioManager;
6+
7+
class SpeakerAudioType implements AudioTypeResolver {
8+
private static final String SPEAKER = "speaker";
9+
private AudioTypeResolver chain;
10+
11+
@Override
12+
public void nextChain(AudioTypeResolver chain) {
13+
this.chain = chain;
14+
}
15+
16+
@Override
17+
public String obtainAudioType(Context context) {
18+
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
19+
if (audioManager.isSpeakerphoneOn()) {
20+
return SPEAKER;
21+
} else {
22+
return chain.obtainAudioType(context);
23+
}
24+
}
25+
}

mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/utils/TelemetryUtils.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ public static int getScreenBrightness(Context context) {
182182
int screenBrightness;
183183
try {
184184
screenBrightness = android.provider.Settings.System.getInt(
185-
context.getContentResolver(),
186-
android.provider.Settings.System.SCREEN_BRIGHTNESS);
185+
context.getContentResolver(),
186+
android.provider.Settings.System.SCREEN_BRIGHTNESS);
187187

188188
// Android returns values between 0 and 255, here we normalize to 0-100.
189189
screenBrightness = (int) Math.floor(100.0 * screenBrightness / 255.0);
@@ -200,7 +200,7 @@ public static int getScreenBrightness(Context context) {
200200
public static int getVolumeLevel(Context context) {
201201
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
202202
return (int) Math.floor(100.0 * audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
203-
/ audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
203+
/ audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
204204
}
205205

206206
/**
@@ -209,4 +209,14 @@ public static int getVolumeLevel(Context context) {
209209
public static String buildUUID() {
210210
return UUID.randomUUID().toString();
211211
}
212+
213+
/**
214+
* Returns the current audio type
215+
*/
216+
public static String obtainAudioType(Context context) {
217+
AudioTypeChain audioTypeChain = new AudioTypeChain();
218+
AudioTypeResolver setupChain = audioTypeChain.setup();
219+
220+
return setupChain.obtainAudioType(context);
221+
}
212222
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.mapbox.services.android.telemetry.utils;
2+
3+
4+
import android.content.Context;
5+
6+
class UnknownAudioType implements AudioTypeResolver {
7+
private static final String UNKNOWN = "unknown";
8+
9+
@Override
10+
public void nextChain(AudioTypeResolver chain) {
11+
}
12+
13+
@Override
14+
public String obtainAudioType(Context context) {
15+
return UNKNOWN;
16+
}
17+
}

0 commit comments

Comments
 (0)