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

move isAutoTimeAndDate / isAutoTimeZone from react-native-device-info #65

Merged
merged 4 commits into from
Sep 8, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ build/
.gradle
local.properties
*.iml
android/gradle/
android/gradlew
android/gradlew.bat

# node.js
#
Expand Down
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,42 @@ console.log(RNLocalize.uses24HourClock());

---

### isAutoDateAndTime()
mikehardy marked this conversation as resolved.
Show resolved Hide resolved

Tells if the automatic date & time setting is enabled on the phone. **Android only**

#### Method type

```ts
type isAutoDateAndTime = () => boolean;
mikehardy marked this conversation as resolved.
Show resolved Hide resolved
```

#### Usage example

```js
console.log(RNLocalize.isAutoDateAndTime()); // true or false
```

---

### isAutoTimeZone()

Tells if the automatic time zone setting is enabled on the phone. **Android only**

#### Method type

```ts
type isAutoTimeZone = () => boolean;
```

#### Usage example

```js
console.log(RNLocalize.isAutoTimeZone());
```

---

### usesMetricSystem()

Returns `true` if the user prefers metric measure system, `false` if he prefers imperial.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.res.Configuration;
import android.os.Build;
import android.os.LocaleList;
import android.provider.Settings;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.View;
Expand Down Expand Up @@ -197,6 +198,26 @@ private WritableMap getNumberFormatSettings(Locale locale) {
return settings;
}

private boolean isAutoDateAndTime() {
mikehardy marked this conversation as resolved.
Show resolved Hide resolved
boolean isAutoDateAndTime;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
isAutoDateAndTime = Settings.System.getInt(getReactApplicationContext().getContentResolver(),Settings.System.AUTO_TIME, 0) != 0;
} else {
isAutoDateAndTime = Settings.Global.getInt(getReactApplicationContext().getContentResolver(),Settings.Global.AUTO_TIME, 0) != 0;
}
return isAutoDateAndTime;
}

private boolean isAutoTimeZone() {
boolean isAutoTimeZone;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
isAutoTimeZone = Settings.System.getInt(getReactApplicationContext().getContentResolver(),Settings.System.AUTO_TIME_ZONE, 0) != 0;
} else {
isAutoTimeZone = Settings.Global.getInt(getReactApplicationContext().getContentResolver(),Settings.Global.AUTO_TIME_ZONE, 0) != 0;
}
return isAutoTimeZone;
}

private WritableMap getExported() {
List<Locale> deviceLocales = getLocales();
List<String> currenciesCache = new ArrayList<>();
Expand Down Expand Up @@ -244,6 +265,8 @@ private WritableMap getExported() {
exported.putMap("numberFormatSettings", getNumberFormatSettings(currentLocale));
exported.putString("temperatureUnit", getTemperatureUnit(currentLocale));
exported.putString("timeZone", TimeZone.getDefault().getID());
exported.putBoolean("isAutoDateAndTime", isAutoDateAndTime());
exported.putBoolean("isAutoTimeZone", isAutoTimeZone());
exported.putBoolean("uses24HourClock", DateFormat.is24HourFormat(getReactApplicationContext()));
exported.putBoolean("usesMetricSystem", getUsesMetricSystem(currentLocale));

Expand Down
Binary file added example/android/app/debug.keystore
Binary file not shown.
8 changes: 8 additions & 0 deletions example/src/AsyncExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ export default class AsyncExample extends React.Component {
name="RNLocalize.uses24HourClock()"
value={RNLocalize.uses24HourClock()}
/>
<Line
mikehardy marked this conversation as resolved.
Show resolved Hide resolved
name="RNLocalize.isAutoDateAndTime()"
value={RNLocalize.isAutoDateAndTime()}
/>
<Line
name="RNLocalize.isAutoTimeZone()"
value={RNLocalize.isAutoTimeZone()}
/>
<Line
name="RNLocalize.usesMetricSystem()"
value={RNLocalize.usesMetricSystem()}
Expand Down
8 changes: 8 additions & 0 deletions example/src/SyncExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ export default class SyncExample extends React.Component {
name="RNLocalize.uses24HourClock()"
value={RNLocalize.uses24HourClock()}
/>
<Line
name="RNLocalize.isAutoDateAndTime()"
value={RNLocalize.isAutoDateAndTime()}
/>
<Line
name="RNLocalize.isAutoTimeZone()"
value={RNLocalize.isAutoTimeZone()}
/>
<Line
name="RNLocalize.usesMetricSystem()"
value={RNLocalize.usesMetricSystem()}
Expand Down
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ declare module "react-native-localize" {
temperatureUnit: TemperatureUnit;
timeZone: string;
uses24HourClock: boolean;
isAutoDateAndTime: boolean;
isAutoTimeZone: boolean;
usesMetricSystem: boolean;
};

Expand All @@ -36,6 +38,8 @@ declare module "react-native-localize" {
export function getTemperatureUnit(): TemperatureUnit;
export function getTimeZone(): string;
export function uses24HourClock(): boolean;
export function isAutoDateAndTime(): boolean;
export function isAutoTimeZone(): boolean;
export function usesMetricSystem(): boolean;

export function addEventListener(
Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export type LocalizationConstants = {|
temperatureUnit: TemperatureUnit,
timeZone: string,
uses24HourClock: boolean,
isAutoDateAndTime: boolean,
isAutoTimeZone: boolean,
usesMetricSystem: boolean,
|};

Expand Down Expand Up @@ -74,6 +76,12 @@ export function getTimeZone(): string {
export function uses24HourClock(): boolean {
return constants.uses24HourClock;
}
export function isAutoDateAndTime(): boolean {
return constants.isAutoDateAndTime;
}
export function isAutoTimeZone(): boolean {
return constants.isAutoTimeZone;
}
export function usesMetricSystem(): boolean {
return constants.usesMetricSystem;
}
Expand Down