Skip to content

Commit

Permalink
fix: Use DisplayMetrics instead of WindowManager for StrictMode Error (
Browse files Browse the repository at this point in the history
  • Loading branch information
Mansi-mParticle authored May 29, 2024
1 parent 716bc13 commit 648c079
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mparticle.internal;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
Expand Down Expand Up @@ -197,8 +198,9 @@ JSONObject getStaticDeviceInfo(Context appContext) {
attributes.put(MessageKey.MODEL, android.os.Build.MODEL);
attributes.put(MessageKey.RELEASE_VERSION, Build.VERSION.RELEASE);

Application application = (Application) appContext;
// device ID
addAndroidId(attributes, appContext);
addAndroidId(attributes, application);

attributes.put(MessageKey.DEVICE_BLUETOOTH_ENABLED, MPUtility.isBluetoothEnabled(appContext));
attributes.put(MessageKey.DEVICE_BLUETOOTH_VERSION, MPUtility.getBluetoothVersion(appContext));
Expand All @@ -210,12 +212,10 @@ JSONObject getStaticDeviceInfo(Context appContext) {
attributes.put(MessageKey.DEVICE_ROOTED, rootedObject);

// screen height/width
WindowManager windowManager = (WindowManager) appContext.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
attributes.put(MessageKey.SCREEN_HEIGHT, metrics.heightPixels);
attributes.put(MessageKey.SCREEN_WIDTH, metrics.widthPixels);
attributes.put(MessageKey.SCREEN_DPI, metrics.densityDpi);
DisplayMetrics displayMetrics = appContext.getResources().getDisplayMetrics();
attributes.put(MessageKey.SCREEN_HEIGHT, displayMetrics.heightPixels);
attributes.put(MessageKey.SCREEN_WIDTH, displayMetrics.widthPixels);
attributes.put(MessageKey.SCREEN_DPI, displayMetrics.densityDpi);

// locales
Locale locale = Locale.getDefault();
Expand Down
19 changes: 10 additions & 9 deletions android-core/src/main/java/com/mparticle/internal/MPUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.os.StatFs;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

Expand Down Expand Up @@ -369,17 +370,17 @@ public static String getTimeZone() {
}

public static int getOrientation(Context context) {
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display getOrient = windowManager.getDefaultDisplay();
int orientation = Configuration.ORIENTATION_UNDEFINED;
if (getOrient.getWidth() == getOrient.getHeight()) {
orientation = Configuration.ORIENTATION_SQUARE;
} else {
if (getOrient.getWidth() < getOrient.getHeight()) {
orientation = Configuration.ORIENTATION_PORTRAIT;
if (context != null) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
if (displayMetrics.widthPixels == displayMetrics.heightPixels) {
orientation = Configuration.ORIENTATION_SQUARE;
} else {
orientation = Configuration.ORIENTATION_LANDSCAPE;
if (displayMetrics.widthPixels < displayMetrics.heightPixels) {
orientation = Configuration.ORIENTATION_PORTRAIT;
} else {
orientation = Configuration.ORIENTATION_LANDSCAPE;
}
}
}
return orientation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package com.mparticle.internal

import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.res.Configuration
import android.content.res.Resources
import android.telephony.TelephonyManager
import android.util.DisplayMetrics
import com.mparticle.mock.MockContext
import com.mparticle.mock.utils.RandomUtils
import org.json.JSONArray
Expand All @@ -11,6 +14,7 @@ import org.json.JSONObject
import org.junit.Assert
import org.junit.Test
import org.mockito.Mockito
import org.mockito.Mockito.`when`
import java.util.Collections
import java.util.UUID

Expand Down Expand Up @@ -253,4 +257,68 @@ class MPUtilityTest {
Assert.assertEquals("234sdvsda", MPUtility.toNumberOrString("234sdvsda"))
Assert.assertNull(MPUtility.toNumberOrString(null))
}

@Test
fun testGetOrientation() {
val mockResources = Mockito.mock(
Resources::class.java
)
val context = Mockito.mock(
Context::class.java
)
val displayMetrics = Mockito.mock(
DisplayMetrics::class.java
)
`when`(context.getResources()).thenReturn(mockResources)
`when`(mockResources.getDisplayMetrics()).thenReturn(displayMetrics)
displayMetrics.widthPixels = 1080
displayMetrics.heightPixels = 1920
val orientation: Int = MPUtility.getOrientation(context)
Assert.assertEquals(Configuration.ORIENTATION_PORTRAIT, orientation)
}

@Test
fun testGetOrientation_When_ORIENTATION_LANDSCAPE() {
val mockResources = Mockito.mock(
Resources::class.java
)
val context = Mockito.mock(
Context::class.java
)
val displayMetrics = Mockito.mock(
DisplayMetrics::class.java
)
`when`(context.getResources()).thenReturn(mockResources)
`when`(mockResources.getDisplayMetrics()).thenReturn(displayMetrics)
displayMetrics.widthPixels = 1953
displayMetrics.heightPixels = 1920
val orientation: Int = MPUtility.getOrientation(context)
Assert.assertEquals(Configuration.ORIENTATION_LANDSCAPE, orientation)
}

@Test
fun testGetOrientation_When_ORIENTATION_SQUARE() {
val mockResources = Mockito.mock(
Resources::class.java
)
val context = Mockito.mock(
Context::class.java
)
val displayMetrics = Mockito.mock(
DisplayMetrics::class.java
)
`when`(context.getResources()).thenReturn(mockResources)
`when`(mockResources.getDisplayMetrics()).thenReturn(displayMetrics)
displayMetrics.widthPixels = 850
displayMetrics.heightPixels = 850
val orientation: Int = MPUtility.getOrientation(context)
Assert.assertEquals(Configuration.ORIENTATION_SQUARE, orientation)
}

@Test
fun testGetOrientation_When_Context_IS_NULL() {
val context: Context? = null
val orientation: Int = MPUtility.getOrientation(context)
Assert.assertEquals(Configuration.ORIENTATION_UNDEFINED, orientation)
}
}

0 comments on commit 648c079

Please sign in to comment.