Skip to content

Commit

Permalink
Rework device utils
Browse files Browse the repository at this point in the history
Improve support to detect system feature.
  • Loading branch information
pranavpandey committed May 2, 2022
1 parent 5394de1 commit a6f13d1
Showing 1 changed file with 50 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,23 +199,37 @@ public static boolean connectWifi(@Nullable Context context, @Nullable String ss
return false;
}

/**
* Checks whether the device has a system feature.
*
* @param context The context to be used.
* @param feature The feature to be checked.
*
* @return {@code true} if the device has the system feature.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static boolean hasSystemFeature(@Nullable Context context, @NonNull String feature) {
if (context == null) {
return false;
}

return context.getPackageManager().hasSystemFeature(feature);
}

/**
* Checks whether the device has camera feature.
*
* @param context The context to be used.
*
* @return {@code true} if the device has camera feature.
*
* @see #hasSystemFeature(Context, String)
* @see PackageManager#FEATURE_CAMERA
* @see PackageManager#FEATURE_CAMERA_ANY
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static boolean hasCameraFeature(@Nullable Context context) {
if (context == null) {
return false;
}

return context.getPackageManager().hasSystemFeature(DynamicSdkUtils.is17()
return hasSystemFeature(context, DynamicSdkUtils.is17()
? PackageManager.FEATURE_CAMERA_ANY : PackageManager.FEATURE_CAMERA);
}

Expand All @@ -226,14 +240,41 @@ public static boolean hasCameraFeature(@Nullable Context context) {
*
* @return {@code true} if the device has flashlight feature.
*
* @see #hasSystemFeature(Context, String)
* @see PackageManager#FEATURE_CAMERA_FLASH
*/
public static boolean hasFlashlightFeature(@Nullable Context context) {
if (context == null) {
return false;
}
return hasSystemFeature(context, PackageManager.FEATURE_CAMERA_FLASH);
}

/**
* Detects the telephony feature by using {@link PackageManager}.
*
* @param context The context to get the package manager.
*
* @return {@code true} if the device has telephony feature.
*
* @see #hasSystemFeature(Context, String)
* @see PackageManager#FEATURE_TELEPHONY
*/
public static boolean hasTelephony(@NonNull Context context) {
return hasSystemFeature(context, PackageManager.FEATURE_TELEPHONY);
}

return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
/**
* Detects the hinge sensor feature by using {@link PackageManager}.
*
* @param context The context to get the package manager.
*
* @return {@code true} if the device has hinge sensor feature feature.
*
* @see #hasSystemFeature(Context, String)
* @see PackageManager#FEATURE_SENSOR_HINGE_ANGLE
*/
@TargetApi(Build.VERSION_CODES.R)
public static boolean hasHingeFeature(@NonNull Context context) {
return DynamicSdkUtils.is30() && hasSystemFeature(
context, PackageManager.FEATURE_SENSOR_HINGE_ANGLE);
}

/**
Expand Down Expand Up @@ -267,20 +308,6 @@ public static void vibrate(@Nullable Context context, long duration) {
}
}

/**
* Detects the telephony feature by using {@link PackageManager}.
*
* @param context The context to get the package manager.
*
* @return {@code true} if the device has telephony feature.
*
* @see PackageManager#hasSystemFeature(String)
* @see PackageManager#FEATURE_TELEPHONY
*/
public static boolean hasTelephony(@NonNull Context context) {
return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
}

/**
* Retrieve a date and time string from milliSeconds according to the system settings.
*
Expand Down

0 comments on commit a6f13d1

Please sign in to comment.