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

fix: Use DisplayMetrics instead of WindowManager for StrictMode Error #481

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
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
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 All @@ -8,7 +9,6 @@
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.mparticle.MParticle;
import com.mparticle.internal.Constants.MessageKey;
Expand Down Expand Up @@ -196,9 +196,9 @@ JSONObject getStaticDeviceInfo(Context appContext) {
attributes.put(MessageKey.OS_VERSION_INT, Build.VERSION.SDK_INT);
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 +210,11 @@ 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
11 changes: 4 additions & 7 deletions android-core/src/main/java/com/mparticle/internal/MPUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import android.os.StatFs;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.view.Display;
import android.view.WindowManager;
import android.util.DisplayMetrics;

import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
Expand Down Expand Up @@ -369,14 +368,12 @@ public static String getTimeZone() {
}

public static int getOrientation(Context context) {
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display getOrient = windowManager.getDefaultDisplay();
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int orientation = Configuration.ORIENTATION_UNDEFINED;
if (getOrient.getWidth() == getOrient.getHeight()) {
if (displayMetrics.widthPixels == displayMetrics.heightPixels) {
orientation = Configuration.ORIENTATION_SQUARE;
} else {
if (getOrient.getWidth() < getOrient.getHeight()) {
if (displayMetrics.widthPixels < displayMetrics.heightPixels) {
orientation = Configuration.ORIENTATION_PORTRAIT;
} else {
orientation = Configuration.ORIENTATION_LANDSCAPE;
Expand Down
Loading