Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Add new metric to indicate if the device is foldable #210

Merged
merged 4 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## XX.XX.XX
* Added a new metric for detecting whether or not a device has a hinge

## 24.1.0
* ! Minor breaking change ! Tracking of foreground and background reporting for APM is now disabled by default

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ of this software and associated documentation files (the "Software"), to deal
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import org.json.JSONException;
Expand All @@ -46,10 +43,7 @@ of this software and associated documentation files (the "Software"), to deal

import static androidx.test.InstrumentationRegistry.getContext;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

@RunWith(AndroidJUnit4.class)
Expand Down Expand Up @@ -233,6 +227,7 @@ public void testGetMetrics() throws UnsupportedEncodingException, JSONException
json.put("_locale", regularDeviceInfo.mp.getLocale());
json.put("_app_version", regularDeviceInfo.mp.getAppVersion(getContext()));
json.put("_manufacturer", regularDeviceInfo.mp.getManufacturer());
json.put("_has_hinge", regularDeviceInfo.mp.hasHinge(getContext()));
json.put("_device_type", regularDeviceInfo.mp.getDeviceType(getContext()));

String calculatedMetrics = URLDecoder.decode(regularDeviceInfo.getMetrics(getContext(), null), "UTF-8");
Expand Down Expand Up @@ -260,6 +255,7 @@ public void testGetMetricsWithOverride() throws UnsupportedEncodingException, JS
json.put("_app_version", regularDeviceInfo.mp.getAppVersion(getContext()));
json.put("_manufacturer", regularDeviceInfo.mp.getManufacturer());
json.put("_device_type", regularDeviceInfo.mp.getDeviceType(getContext()));
json.put("_has_hinge", regularDeviceInfo.mp.hasHinge(getContext()));
json.put("123", "bb");
json.put("456", "cc");
json.put("Test", "aa");
Expand Down Expand Up @@ -292,6 +288,7 @@ public void testGetMetricsWithOverride_2() throws UnsupportedEncodingException,
json.put("_locale", "d4");
json.put("_app_version", "d5");
json.put("_manufacturer", regularDeviceInfo.mp.getManufacturer());
json.put("_has_hinge", regularDeviceInfo.mp.hasHinge(getContext()));
json.put("_device_type", regularDeviceInfo.mp.getDeviceType(getContext()));
json.put("asd", "123");

Expand Down
24 changes: 21 additions & 3 deletions sdk/src/main/java/ly/count/android/sdk/DeviceInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,7 @@ public String getBatteryLevel(Context context) {
Intent batteryIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED), null, null, Context.RECEIVER_NOT_EXPORTED);
}
else {
} else {
batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
if (batteryIntent != null) {
Expand Down Expand Up @@ -539,6 +538,21 @@ public String isMuted(Context context) {
return "false";
}
}

/**
* Check if device is foldable
* requires API level 30
*
* @param context to use
* @return true if device is foldable
*/
@Override
public String hasHinge(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_SENSOR_HINGE_ANGLE) + "";
}
return "false";
}
};
}
}
Expand All @@ -561,7 +575,8 @@ JSONObject getCommonMetrics(@NonNull final Context context, @Nullable final Map<
"_os_version", mp.getOSVersion(),
"_resolution", mp.getResolution(context),
"_app_version", mp.getAppVersion(context),
"_manufacturer", mp.getManufacturer());
"_manufacturer", mp.getManufacturer(),
"_has_hinge", mp.hasHinge(context));

if (metricOverride != null) {
try {
Expand All @@ -583,6 +598,9 @@ JSONObject getCommonMetrics(@NonNull final Context context, @Nullable final Map<
if (metricOverride.containsKey("_manufacturer")) {
json.put("_manufacturer", metricOverride.get("_manufacturer"));
}
if (metricOverride.containsKey("_has_hinge")) {
json.put("_has_hinge", metricOverride.get("_has_hinge"));
}
} catch (Exception ex) {
Countly.sharedInstance().L.e("[DeviceInfo] SDK encountered failure while trying to apply metric override, " + ex.toString());
}
Expand Down
2 changes: 2 additions & 0 deletions sdk/src/main/java/ly/count/android/sdk/MetricProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,6 @@ interface MetricProvider {
String isOnline(Context context);

String isMuted(Context context);

String hasHinge(Context context);
}
Loading