Skip to content

Commit

Permalink
refactor: code refactor and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nisrulz committed Dec 21, 2017
1 parent fe5c16d commit b31241f
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
package github.nisrulz.projecteasydeviceinfo;

import android.Manifest;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.hardware.Sensor;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.appcompat.BuildConfig;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.widget.ArrayAdapter;
Expand Down Expand Up @@ -58,6 +60,7 @@ public class MainActivity extends AppCompatActivity {

private ArrayAdapter<String> adapter;

@SuppressLint("MissingPermission")
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -141,21 +144,21 @@ public void onSuccess(String adIdentifier, boolean adDonotTrack) {
List<Sensor> list = easySensorMod.getAllSensors();
for (Sensor s : list) {
if (s != null) {
StringBuilder stringBuilder = new StringBuilder().append("\nVendor : ")
.append(s.getVendor())
.append("\n")
.append("Version : ")
.append(s.getVersion())
.append("\n")
.append("Power : ")
.append(s.getPower())
.append("\n")
.append("Resolution : ")
.append(s.getResolution())
.append("\n")
.append("Max Range : ")
.append(s.getMaximumRange());
deviceDataMap.put("Sensor Name - " + s.getName(), stringBuilder.toString());
String stringBuilder = "\nVendor : "
+ s.getVendor()
+ "\n"
+ "Version : "
+ s.getVersion()
+ "\n"
+ "Power : "
+ s.getPower()
+ "\n"
+ "Resolution : "
+ s.getResolution()
+ "\n"
+ "Max Range : "
+ s.getMaximumRange();
deviceDataMap.put("Sensor Name - " + s.getName(), stringBuilder);
} else {
deviceDataMap.put("Sensor", "N/A");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public final int getBatteryHealth() {
*/
public final String getBatteryTechnology() {
return CheckValidityUtil.checkValidData(
getBatteryStatusIntent().getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY));
getBatteryStatusIntent().getStringExtra(BatteryManager.EXTRA_TECHNOLOGY));
}

/**
Expand Down Expand Up @@ -158,6 +158,7 @@ public final int getChargingSource() {
* @return the boolean
*/
public final boolean isBatteryPresent() {
return getBatteryStatusIntent().getExtras().getBoolean(BatteryManager.EXTRA_PRESENT);
return getBatteryStatusIntent().getExtras() != null && getBatteryStatusIntent().getExtras()
.getBoolean(BatteryManager.EXTRA_PRESENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,23 @@ public final boolean isRunningOnEmulator() {
public final int getDeviceRingerMode() {
int ringerMode = RingerMode.NORMAL;
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
switch (audioManager.getRingerMode()) {
case RINGER_MODE_NORMAL:
ringerMode = RingerMode.NORMAL;
break;
case RINGER_MODE_SILENT:
ringerMode = RingerMode.SILENT;
break;
case RINGER_MODE_VIBRATE:
ringerMode = RingerMode.VIBRATE;
break;
default:
//do nothing
break;
if (audioManager != null) {
switch (audioManager.getRingerMode()) {
case RINGER_MODE_NORMAL:
ringerMode = RingerMode.NORMAL;
break;
case RINGER_MODE_SILENT:
ringerMode = RingerMode.SILENT;
break;
case RINGER_MODE_VIBRATE:
ringerMode = RingerMode.VIBRATE;
break;
default:
//do nothing
break;
}
}

return ringerMode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,11 @@ public final String getBuildVersionRelease() {
*/
public final String getScreenDisplayID() {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
return CheckValidityUtil.checkValidData(String.valueOf(display.getDisplayId()));
if (wm != null) {
Display display = wm.getDefaultDisplay();
return CheckValidityUtil.checkValidData(String.valueOf(display.getDisplayId()));
}
return CheckValidityUtil.checkValidData("");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public EasyDisplayMod(final Context context) {
this.context = context;

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
display = wm.getDefaultDisplay();
if (wm != null) {
display = wm.getDefaultDisplay();
} else {
display = null;
}
}

/**
Expand Down Expand Up @@ -109,8 +113,11 @@ public final int[] getDisplayXYCoordinates(final MotionEvent event) {
*/
public final String getResolution() {
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
return CheckValidityUtil.checkValidData(metrics.heightPixels + "x" + metrics.widthPixels);
if (display != null) {
display.getMetrics(metrics);
return CheckValidityUtil.checkValidData(metrics.heightPixels + "x" + metrics.widthPixels);
}
return CheckValidityUtil.checkValidData("");
}

public final float getRefreshRate() {
Expand All @@ -119,9 +126,13 @@ public final float getRefreshRate() {

public final float getPhysicalSize() {
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
float x = (float) Math.pow(metrics.widthPixels / metrics.xdpi, 2);
float y = (float) Math.pow(metrics.heightPixels / metrics.ydpi, 2);
return (float) Math.sqrt(x + y);

if (display != null) {
display.getMetrics(metrics);
float x = (float) Math.pow(metrics.widthPixels / metrics.xdpi, 2);
float y = (float) Math.pow(metrics.heightPixels / metrics.ydpi, 2);
return (float) Math.sqrt(x + y);
}
return 0f;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ public final long getTotalRAM() {
MemoryInfo mi = new MemoryInfo();
ActivityManager activityManager =
(ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
activityManager.getMemoryInfo(mi);
totalMemory = mi.totalMem;
if (activityManager != null) {
activityManager.getMemoryInfo(mi);
totalMemory = mi.totalMem;
}
} else {
RandomAccessFile reader = null;
String load;
Expand Down Expand Up @@ -230,6 +232,7 @@ public float convertToGb(long valInBytes) {
*
* @return the float
*/
@SuppressWarnings("NumericOverflow")
public float convertToTb(long valInBytes) {
return (float) valInBytes / (BYTEFACTOR * BYTEFACTOR * BYTEFACTOR * BYTEFACTOR);
}
Expand Down
Loading

0 comments on commit b31241f

Please sign in to comment.