Skip to content

Commit

Permalink
BREAKING CHANGE: API capitalization changes, empty return value stand…
Browse files Browse the repository at this point in the history
…ardization
  • Loading branch information
mikehardy committed Sep 1, 2019
1 parent 9c38fb9 commit 9463583
Show file tree
Hide file tree
Showing 12 changed files with 537 additions and 359 deletions.
33 changes: 30 additions & 3 deletions CHANGELOG.md
@@ -1,6 +1,12 @@
<!-- markdownlint-disable MD024 MD034 MD033 -->
# Release Notes

## 3.0.0-rc.3

* fix some real android device v2->v3 discrepancies
* BREAKING CHANGE: more capitalization changes for API calls to standardize (see below)
* BREAKING CHANGE: more return value changes to standardize return values (see below)

## 3.0.0-rc.2

* fix all emulator/simulator v2->v3 discrepancies
Expand All @@ -10,14 +16,35 @@
Each BREAKING CHANGE contains the required information to migrate. The example App.js shows sample usage.

* BREAKING CHANGE: Every API call returns a Promise now (and thus no more Android constructor with async boolean argument)
* BREAKING CHANGE: Renamed getSupportedABIs to getSupportedAbis (note lower case!) for consistency with other getXxxAbis methods
* BREAKING CHANGE: all events prefixed with 'RNDeviceInfo_' to future-proof against collisions (https://github.com/react-native-community/react-native-device-info/issues/620)
* This was required to improve module load speed, handle dynamic values, and release the main queue for iOS
* BREAKING CHANGE: Every API call with acronyms ('getIP', 'getABI' etc follows pure camel-case now, e.g. 'getIp', 'getAbi')
* This naming style is a consensus standard. Previously APIs here were half one way half the other. Now they are consistent
* isAirPlaneMode -> isAirplaneMode
* getIPAddress -> getIpAddress
* getMACAddress -> getMACAddress
* getAPILevel -> GetApiLevel
* getBaseOS -> getBaseOs
* getInstanceID -> getInstanceId
* getUniqueID -> getUniqueId
* supportedABIs -> supportedAbis
* BREAKING CHANGE: all events prefixed with 'RNDeviceInfo_' (https://github.com/react-native-community/react-native-device-info/issues/620)
* This is required as event names are a global namespace and collisions are inevitable otherwise
* powerStateDidChange -> RNDeviceInfo_powerStateDidChange
* batteryLevelDidChange -> RNDeviceInfo_batteryLevelDidChange
* batteryLevelIsLow -> RNDeviceInfo_batteryLevelIsLow
* BREAKING CHANGE: Android `getBuildNumber` returns string like iOS (https://github.com/react-native-community/react-native-device-info/pull/648)
* BREAKING CHANGE: remove is24Hour, getTimezone, isAutoTimeZone and isAutoDateAndTime, getDeviceLocale, getDeviceCountry, getPreferredLocales. react-native-localize is superior
* BREAKING CHANGE: remove is24Hour, getTimezone, isAutoTimeZone and isAutoDateAndTime, getDeviceLocale, getDeviceCountry, getPreferredLocales
* This was the result of a survey. It removes API duplication in the react-native-community modules
* Related PR: https://github.com/react-native-community/react-native-localize/pull/65
* Use `yarn add https://github.com/mikehardy/react-native-localize.git#e062f0d2dc3171dc18fdb7b7139d347ad03933dc` to maintain isAutoTimeZone + isAutoDateAndTime until merged
* BREAKING CHANGE: iOS switch deprecated WebView for WebKit / getUserAgent returns Promise (https://github.com/react-native-community/react-native-device-info/pull/757)
* The change from WebView to WebKit was required as the API is being removed from the iOS platform
* BREAKING CHANGE: if an API is platform-specific, all non-implementing platforms will return standard values of -1, false, or 'unknown' depending on return type
* This was how most APIs behaved before but it was not 100% - some returned null or empty string before
* getPhoneNumber sometimes returned null, now it will be 'unknown' if not known
* deprecated: IP-address-related methods deprecated - use @react-native-community/netinfo or react-native-network-info or react-native-carrier-info
* feat: all APIs are now restricted in Javascirpt to the platforms they have full implementations on so the web polyfill is up to date
* feat: 'getAndroidId' on Android returns android.provider.Settings.Secure.ANDROID_ID, read platform docs for usage
* feat: `getUsedMemory` (https://github.com/rebeccahughes/react-native-device-info/pull/356)
* feat: getDeviceName() without Bluetooth permission on Android (https://github.com/react-native-community/react-native-device-info/issues/735)
* feat: TurboModule support (https://github.com/react-native-community/react-native-device-info/pull/745) for these purposes (https://github.com/react-native-community/react-native-localize/pull/65)
Expand Down
204 changes: 112 additions & 92 deletions README.md

Large diffs are not rendered by default.

84 changes: 56 additions & 28 deletions android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java
Expand Up @@ -52,7 +52,6 @@

import static android.provider.Settings.Secure.getString;

@SuppressWarnings("WeakerAccess")
@ReactModule(name = RNDeviceModule.NAME)
public class RNDeviceModule extends ReactContextBaseJavaModule {
public static final String NAME = "RNDeviceInfo";
Expand Down Expand Up @@ -186,6 +185,7 @@ public void isPinOrFingerprintSet(Promise p) {
KeyguardManager keyguardManager = (KeyguardManager) getReactApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
if (keyguardManager != null) {
p.resolve(keyguardManager.isKeyguardSecure());
return;
}
p.reject("EUNSPECIFIED", "Unable to determine keyguard status. KeyguardManager was null");
}
Expand All @@ -208,7 +208,6 @@ public void getIpAddress(Promise p) {
}

@ReactMethod
@SuppressWarnings("ConstantConditions")
public void getCameraPresence(Promise p) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CameraManager manager=(CameraManager)getReactApplicationContext().getSystemService(Context.CAMERA_SERVICE);
Expand Down Expand Up @@ -341,14 +340,14 @@ public void getBatteryLevel(Promise p) {
}

@ReactMethod
public void isAirPlaneMode(Promise p) {
boolean isAirPlaneMode;
public void isAirplaneMode(Promise p) {
boolean isAirplaneMode;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
isAirPlaneMode = Settings.System.getInt(getReactApplicationContext().getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 0) != 0;
isAirplaneMode = Settings.System.getInt(getReactApplicationContext().getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 0) != 0;
} else {
isAirPlaneMode = Settings.Global.getInt(getReactApplicationContext().getContentResolver(),Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
isAirplaneMode = Settings.Global.getInt(getReactApplicationContext().getContentResolver(),Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}
p.resolve(isAirPlaneMode);
p.resolve(isAirplaneMode);
}

@ReactMethod
Expand All @@ -359,8 +358,7 @@ public void hasSystemFeature(String feature, Promise p) {
return;
}

boolean hasFeature = getReactApplicationContext().getPackageManager().hasSystemFeature(feature);
p.resolve(hasFeature);
p.resolve(getReactApplicationContext().getPackageManager().hasSystemFeature(feature));
}

@ReactMethod
Expand All @@ -377,7 +375,6 @@ public void getSystemAvailableFeatures(Promise p) {
p.resolve(promiseArray);
}

@SuppressWarnings("ConstantConditions")
@ReactMethod
public void isLocationEnabled(Promise p) {
boolean locationEnabled = false;
Expand All @@ -388,6 +385,7 @@ public void isLocationEnabled(Promise p) {
locationEnabled = mLocationManager.isLocationEnabled();
} catch (Exception e) {
p.reject("EUNSPECIFIED", "Unable to determine if location enabled. LocationManager was null");
return;
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int locationMode = Settings.Secure.getInt(getReactApplicationContext().getContentResolver(), Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
Expand All @@ -401,14 +399,14 @@ public void isLocationEnabled(Promise p) {
}

@ReactMethod
@SuppressWarnings("ConstantConditions")
public void getAvailableLocationProviders(Promise p) {
LocationManager mLocationManager = (LocationManager) getReactApplicationContext().getSystemService(Context.LOCATION_SERVICE);
List<String> providers = Collections.emptyList();
try {
providers = mLocationManager.getProviders(false);
} catch (Exception e) {
p.reject("EUNSPECIFIED", "Unable to get location providers. LocationManager was null");
return;
}

WritableMap providersAvailability = Arguments.createMap();
Expand All @@ -422,7 +420,7 @@ public void getAvailableLocationProviders(Promise p) {
@ReactMethod
public void getInstallReferrer(Promise p) {
SharedPreferences sharedPref = getReactApplicationContext().getSharedPreferences("react-native-device-info", Context.MODE_PRIVATE);
p.resolve(sharedPref.getString("installReferrer", null));
p.resolve(sharedPref.getString("installReferrer", Build.UNKNOWN));
}

private PackageInfo getPackageInfo() throws Exception {
Expand All @@ -449,7 +447,7 @@ public void getBuildNumber(Promise p) {

@ReactMethod
public void getBuildVersion(Promise p) {
p.resolve("not available");
p.resolve(Build.UNKNOWN);
}

@ReactMethod
Expand Down Expand Up @@ -482,19 +480,43 @@ public void getAppName(Promise p) {
@ReactMethod
public void getDeviceName(Promise p) {
try {
if (Build.VERSION.SDK_INT >= 25) {
p.resolve(Settings.Global.getString(getReactApplicationContext().getContentResolver(), Settings.Global.DEVICE_NAME));
} else {
p.resolve(Settings.Secure.getString(getReactApplicationContext().getContentResolver(), "bluetooth_name"));
}
String bluetoothName = Settings.Secure.getString(getReactApplicationContext().getContentResolver(), "bluetooth_name");
if (bluetoothName != null) {
p.resolve(bluetoothName);
return;
}

if (Build.VERSION.SDK_INT >= 25) {
String deviceName = Settings.Global.getString(getReactApplicationContext().getContentResolver(), Settings.Global.DEVICE_NAME);
if (deviceName != null) {
p.resolve(deviceName);
return;
}
}
} catch (Exception e) {
p.reject(e);
return;
}
p.resolve(Build.UNKNOWN);
}

@SuppressLint("HardwareIds")
@SuppressLint({"HardwareIds", "MissingPermission"})
@ReactMethod
public void getSerialNumber(Promise p) { p.resolve(Build.SERIAL); }
public void getSerialNumber(Promise p) {
try {
if (Build.VERSION.SDK_INT >= 26) {
if (getReactApplicationContext().checkCallingOrSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
p.resolve(Build.getSerial());
return;
}
}
} catch (Exception e) {
// This is almost always a PermissionException. We will log it but return unknown
System.err.println("getSerialNumber failed, it probably should not be used: " + e.getMessage());
}

p.resolve(Build.UNKNOWN);
}

@ReactMethod
public void getSystemName(Promise p) { p.resolve("Android"); }
Expand Down Expand Up @@ -560,6 +582,10 @@ public void getDeviceName(Promise p) {
@ReactMethod
public void getUniqueId(Promise p) { p.resolve(getString(getReactApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID)); }

@SuppressLint("HardwareIds")
@ReactMethod
public void getAndroidId(Promise p) { p.resolve(getString(getReactApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID)); }

@ReactMethod
public void getMaxMemory(Promise p) { p.resolve((double)Runtime.getRuntime().maxMemory()); }

Expand All @@ -574,6 +600,7 @@ public void getTotalMemory(Promise p) {
actMgr.getMemoryInfo(memInfo);
} else {
p.reject("EUNSPECIFIED", "Unable to getMemoryInfo. ActivityManager was null");
return;
}
p.resolve((double)memInfo.totalMem);
}
Expand All @@ -591,30 +618,31 @@ public void getInstanceId(Promise p) {
}

@ReactMethod
public void getBaseOS(Promise p) {
public void getBaseOs(Promise p) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
p.resolve(Build.VERSION.BASE_OS);
} else {
p.resolve("not available");
return;
}
p.resolve(Build.UNKNOWN);

}

@ReactMethod
public void getPreviewSdkInt(Promise p) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
p.resolve(Build.VERSION.PREVIEW_SDK_INT);
} else {
p.resolve("not available");
return;
}
p.resolve(Build.UNKNOWN);
}

@ReactMethod
public void getSecurityPatch(Promise p) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
p.resolve(Build.VERSION.SECURITY_PATCH);
} else {
p.resolve("not available");
return;
}
p.resolve(Build.UNKNOWN);
}

@ReactMethod
Expand Down Expand Up @@ -644,7 +672,7 @@ public void getPhoneNumber(Promise p) {
p.reject("EUNSPECIFIED", "Unable to getPhoneNumber. TelephonyManager was null");
}
} else {
p.resolve(null);
p.resolve(Build.UNKNOWN);
}
}

Expand Down
48 changes: 0 additions & 48 deletions default/index.js

This file was deleted.

15 changes: 8 additions & 7 deletions deviceinfo.d.ts
Expand Up @@ -17,7 +17,7 @@ export interface LocationProviderInfo {
}

declare const _default: {
getUniqueID: () => Promise<string>;
getUniqueId: () => Promise<string>;
getManufacturer: () => Promise<string>;
getBrand: () => Promise<string>;
getModel: () => Promise<string>;
Expand All @@ -33,7 +33,7 @@ declare const _default: {
getDeviceName: () => Promise<string>;
getUsedMemory: () => Promise<number>;
getUserAgent: () => Promise<string>;
getInstanceID: () => Promise<string>;
getInstanceId: () => Promise<string>;
getInstallReferrer: () => string | null;
isEmulator: () => Promise<boolean>;
isTablet: () => Promise<boolean>;
Expand All @@ -47,7 +47,7 @@ declare const _default: {
getProduct: () => Promise<string>;
getTags: () => Promise<string>;
getType: () => Promise<string>;
getBaseOS: () => Promise<string>;
getBaseOs: () => Promise<string>;
getPreviewSdkInt: () => Promise<number>;
getSecurityPatch: () => Promise<string>;
getCodename: () => Promise<string>;
Expand All @@ -57,11 +57,12 @@ declare const _default: {
getFirstInstallTime: () => Promise<number>;
getLastUpdateTime: () => Promise<number>;
getSerialNumber: () => Promise<string>;
getIPAddress: () => Promise<string>;
getAndroidId: () => Promise<string>;
getIpAddress: () => Promise<string>;
getCameraPresence: () => Promise<boolean>;
getMACAddress: () => Promise<string>;
getMacAddress: () => Promise<string>;
getPhoneNumber: () => Promise<string>;
getAPILevel: () => Promise<number>;
getApiLevel: () => Promise<number>;
getCarrier: () => Promise<string>;
getTotalMemory: () => Promise<number>;
getMaxMemory: () => Promise<number>;
Expand All @@ -71,7 +72,7 @@ declare const _default: {
getPowerState: () => Promise<PowerState>;
isBatteryCharging: () => Promise<boolean>;
isLandscape: () => Promise<boolean>;
isAirPlaneMode: () => Promise<boolean>;
isAirplaneMode: () => Promise<boolean>;
getDeviceType: () => Promise<DeviceType>;
supportedAbis: () => Promise<string[]>;
supported32BitAbis: () => Promise<string[]>;
Expand Down

0 comments on commit 9463583

Please sign in to comment.