Skip to content

Commit

Permalink
Merge pull request #38 from rmrs/feat/captioning_setting
Browse files Browse the repository at this point in the history
feat: add captioning setting
  • Loading branch information
erezrokah authored Dec 6, 2019
2 parents 81e53c2 + b9abb47 commit 977cb46
Show file tree
Hide file tree
Showing 13 changed files with 248 additions and 62 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ build/
.gradle
local.properties
*.iml
.project
.settings
.classpath

# BUCK
buck-out/
\.buckd/
*.keystore

*.keystore
14 changes: 14 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"printWidth": 80,
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"useTabs": false,
"overrides": [
{
"files": "*.json",
"options": { "printWidth": 200 }
}
]
}
101 changes: 67 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# react-native-settings

We created this module to allow us to query for specific device settings.
Expand All @@ -17,7 +16,7 @@ a setting or denies/grants a permission.
Currently we've only added a way to extract the 'location' setting (and airplane mode on Android).
We will add more in the future based on requirements.

[`react-native example`](https://github.com/rmrs/react-native-settings/tree/master/example) for both Android and iOS.
[`react-native example`](https://github.com/rmrs/react-native-settings/tree/master/example) for both Android and iOS.

## Getting started

Expand All @@ -31,7 +30,7 @@ We will add more in the future based on requirements.

In your `MainApplication.java` file register the receivers:

``` java
```java
...

import android.content.IntentFilter;
Expand Down Expand Up @@ -92,67 +91,101 @@ project(':react-native-settings').projectDir = new File(rootProject.projectDir,
#### Getting a setting

```javascript
import RNSettings from 'react-native-settings'
import RNSettings from 'react-native-settings';

RNSettings.getSetting(RNSettings.LOCATION_SETTING).then(result => {
if (result == RNSettings.ENABLED) {
console.log('location is enabled')
console.log('location is enabled');
} else {
console.log('location is disabled')
console.log('location is disabled');
}
})
});
```

#### Android only

```javascript
import RNSettings from 'react-native-settings'
import RNSettings from 'react-native-settings';

RNSettings.getSetting(RNSettings.AIRPLANE_MODE_SETTING).then(result => {
if (result == RNSettings.ENABLED) {
console.log('airplane mode is enabled')
console.log('airplane mode is enabled');
} else {
console.log('airplane mode is disabled')
console.log('airplane mode is disabled');
}
})
});

RNSettings.getSetting(RNSettings.CAPTIONING_SETTINGS).then(result => {
if (result == RNSettings.ENABLED) {
console.log('captioning is enabled');
} else {
console.log('captioning is disabled');
}
});
```

##### Open settings application in a specific setting

```javascript
import RNSettings from 'react-native-settings'

RNSettings.openSetting(RNSettings.ACTION_LOCATION_SOURCE_SETTINGS).
then((result) => {
if (result === RNSettings.ENABLED) {
console.log('location is enabled')
}

RNSettings.openSetting(RNSettings.ACTION_AIRPLANE_MODE_SETTINGS).
then((result) => {
if (result === RNSettings.ENABLED) {
console.log('airplane mode is enabled')
}
import RNSettings from 'react-native-settings';

RNSettings.openSetting(RNSettings.ACTION_LOCATION_SOURCE_SETTINGS).then(
result => {
if (result === RNSettings.ENABLED) {
console.log('location is enabled');
}
},
);

RNSettings.openSetting(RNSettings.ACTION_AIRPLANE_MODE_SETTINGS).then(
result => {
if (result === RNSettings.ENABLED) {
console.log('airplane mode is enabled');
}
},
);

RNSettings.openSetting(RNSettings.ACTION_CAPTIONING_SETTINGS).then(result => {
if (result === RNSettings.ENABLED) {
console.log('captioning is enabled');
}
});
```

##### Listen to setting change event (when applicable)

```javascript
import RNSettings from 'react-native-settings'
import { DeviceEventEmitter } from 'react-native'
import RNSettings from 'react-native-settings';
import { DeviceEventEmitter } from 'react-native';

_handleGPSProviderEvent = (e) => {
_handleGPSProviderEvent = e => {
if (e[RNSettings.LOCATION_SETTING] === RNSettings.DISABLED) {
console.log('Location was disabled')
console.log('Location was disabled');
}
}
};

_handleAirplaneModeEvent = (e) => {
_handleAirplaneModeEvent = e => {
if (e[RNSettings.AIRPLANE_MODE_SETTING] === RNSettings.ENABLED) {
console.log('airplane mode was enabled')
console.log('airplane mode was enabled');
}
}
};

DeviceEventEmitter.addListener(RNSettings.GPS_PROVIDER_EVENT, this._handleGPSProviderEvent)
DeviceEventEmitter.addListener(RNSettings.AIRPLANE_MODE_EVENT, this._handleAirplaneModeEvent)
_handleCaptioningEvent = e => {
if (e[RNSettings.CAPTIONING_SETTINGS] === RNSettings.ENABLED) {
console.log('captioning was enabled');
}
};

DeviceEventEmitter.addListener(
RNSettings.GPS_PROVIDER_EVENT,
this._handleGPSProviderEvent,
);
DeviceEventEmitter.addListener(
RNSettings.AIRPLANE_MODE_EVENT,
this._handleAirplaneModeEvent,
);
DeviceEventEmitter.addListener(
RNSettings.CAPTIONING_EVENT,
this._handleCaptioningEvent,
);
```
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ public class Constants {
//Broadcast Events
public static final String AIRPLANE_MODE_CHANGED = "AIRPLANE_MODE_CHANGED";
public static final String PROVIDERS_CHANGED = "PROVIDERS_CHANGED";
public static final String CAPTIONING_CHANGED = "CAPTIONING_CHANGED";

//Settings Names
public static final String LOCATION_SETTING = "LOCATION_SETTING";
public static final String AIRPLANE_MODE_SETTING = "AIRPLANE_MODE_SETTING";
public static final String CAPTIONING_SETTINGS = "CAPTIONING_SETTINGS";

//Settings Values
public static final String ENABLED = "ENABLED";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import android.content.IntentFilter;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import androidx.annotation.Nullable;
import android.content.BroadcastReceiver;

import com.facebook.react.bridge.NativeModule;
Expand All @@ -22,6 +22,7 @@
import com.facebook.react.modules.core.DeviceEventManagerModule;

import io.rumors.reactnativesettings.handlers.*;
import io.rumors.reactnativesettings.listeners.*;

import java.util.Map;
import java.util.HashMap;
Expand All @@ -30,6 +31,7 @@ public class RNSettingsModule extends ReactContextBaseJavaModule {
//javascript event names
private static final String GPS_PROVIDER_EVENT = "GPS_PROVIDER_EVENT";
private static final String AIRPLANE_MODE_EVENT = "AIRPLANE_MODE_EVENT";
private static final String CAPTIONING_EVENT = "CAPTIONING_EVENT";

//error values
private static final String E_FAILED_TO_GET_SETTINGS = "E_FAILED_TO_GET_SETTINGS";
Expand All @@ -38,6 +40,7 @@ public class RNSettingsModule extends ReactContextBaseJavaModule {
//open settings names
private static final String ACTION_LOCATION_SOURCE_SETTINGS = "ACTION_LOCATION_SOURCE_SETTINGS";
private static final String ACTION_AIRPLANE_MODE_SETTINGS = "ACTION_AIRPLANE_MODE_SETTINGS";
private static final String ACTION_CAPTIONING_SETTINGS = "ACTION_CAPTIONING_SETTINGS";

private Map<String, Integer> mOpenSettingToRequestCode = new HashMap<String, Integer>();
private Map<Integer, String> mRequestCodeToOpenSetting = new HashMap<Integer, String>();
Expand All @@ -61,23 +64,21 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
}
};

private class LocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String providerSetting = intent.getStringExtra(Constants.LOCATION_SETTING);
WritableMap params = Arguments.createMap();
params.putString(Constants.LOCATION_SETTING, providerSetting);
sendEvent(GPS_PROVIDER_EVENT, params);
private class SettingReceiver extends BroadcastReceiver {
private String mSettingName;
private String mEventName;

SettingReceiver(String settingName, String eventName) {
this.mSettingName = settingName;
this.mEventName = eventName;
}
}

private class AirplaneModeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String providerSetting = intent.getStringExtra(Constants.AIRPLANE_MODE_SETTING);
String providerSetting = intent.getStringExtra(mSettingName);
WritableMap params = Arguments.createMap();
params.putString(Constants.AIRPLANE_MODE_SETTING, providerSetting);
sendEvent(AIRPLANE_MODE_EVENT, params);
params.putString(mSettingName, providerSetting);
sendEvent(mEventName, params);
}
}

Expand All @@ -91,25 +92,48 @@ private void registerReceiver(Context reactContext, String filter, BroadcastRece
reactContext.registerReceiver(receiver, intentFilter);
}

public RNSettingsModule(ReactApplicationContext reactContext) {
super(reactContext);
this.mReactContext = reactContext;
private void initReceivers() {
registerReceiver(mReactContext, Constants.PROVIDERS_CHANGED, new SettingReceiver(Constants.LOCATION_SETTING, GPS_PROVIDER_EVENT));
registerReceiver(mReactContext, Constants.AIRPLANE_MODE_CHANGED, new SettingReceiver(Constants.AIRPLANE_MODE_SETTING, AIRPLANE_MODE_EVENT));
registerReceiver(mReactContext, Constants.CAPTIONING_CHANGED, new SettingReceiver(Constants.CAPTIONING_SETTINGS, CAPTIONING_EVENT));
}

this.registerReceiver(reactContext, Constants.PROVIDERS_CHANGED, new LocationReceiver());
this.registerReceiver(reactContext, Constants.AIRPLANE_MODE_CHANGED, new AirplaneModeReceiver());
private void initListeners() {
new CaptioningChangeListener(mReactContext);
}

mSettingsHandlers.put(Constants.LOCATION_SETTING, new LocationSettingsHandler(reactContext));
mSettingsHandlers.put(Constants.AIRPLANE_MODE_SETTING, new AirplaneModeSettingsHandler(reactContext));
private void initHandlers() {
mSettingsHandlers.put(Constants.LOCATION_SETTING, new LocationSettingsHandler(mReactContext));
mSettingsHandlers.put(Constants.AIRPLANE_MODE_SETTING, new AirplaneModeSettingsHandler(mReactContext));
mSettingsHandlers.put(Constants.CAPTIONING_SETTINGS, new CaptioningSettingsHandler(mReactContext));
}

private void initRequestCodes() {
mOpenSettingToRequestCode.put(Settings.ACTION_LOCATION_SOURCE_SETTINGS, 0);
mOpenSettingToRequestCode.put(Settings.ACTION_AIRPLANE_MODE_SETTINGS, 1);
mOpenSettingToRequestCode.put(Settings.ACTION_CAPTIONING_SETTINGS, 2);

mRequestCodeToOpenSetting.put(0, Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mRequestCodeToOpenSetting.put(1, Settings.ACTION_AIRPLANE_MODE_SETTINGS);
mRequestCodeToOpenSetting.put(2, Settings.ACTION_CAPTIONING_SETTINGS);
}

private void initSettingsActions() {
mOpenSettingToSettingsName.put(Settings.ACTION_LOCATION_SOURCE_SETTINGS, Constants.LOCATION_SETTING);
mOpenSettingToSettingsName.put(Settings.ACTION_AIRPLANE_MODE_SETTINGS, Constants.AIRPLANE_MODE_SETTING);
mOpenSettingToSettingsName.put(Settings.ACTION_CAPTIONING_SETTINGS, Constants.CAPTIONING_SETTINGS);
}

public RNSettingsModule(ReactApplicationContext reactContext) {
super(reactContext);
this.mReactContext = reactContext;

initReceivers();
initListeners();
initHandlers();
initRequestCodes();
initSettingsActions();

reactContext.addActivityEventListener(mActivityEventListener);
}

Expand All @@ -125,16 +149,19 @@ public Map<String, Object> getConstants() {
//event listeners
constants.put(GPS_PROVIDER_EVENT, GPS_PROVIDER_EVENT);
constants.put(AIRPLANE_MODE_EVENT, AIRPLANE_MODE_EVENT);
constants.put(CAPTIONING_EVENT, CAPTIONING_EVENT);

//get settings
constants.put(Constants.LOCATION_SETTING, Constants.LOCATION_SETTING);
constants.put(Constants.AIRPLANE_MODE_SETTING, Constants.AIRPLANE_MODE_SETTING);
constants.put(Constants.CAPTIONING_SETTINGS, Constants.CAPTIONING_SETTINGS);
constants.put(Constants.ENABLED, Constants.ENABLED);
constants.put(Constants.DISABLED, Constants.DISABLED);

//open settings
constants.put(ACTION_LOCATION_SOURCE_SETTINGS, Settings.ACTION_LOCATION_SOURCE_SETTINGS);
constants.put(ACTION_AIRPLANE_MODE_SETTINGS, Settings.ACTION_AIRPLANE_MODE_SETTINGS);
constants.put(ACTION_CAPTIONING_SETTINGS, Settings.ACTION_CAPTIONING_SETTINGS);
return constants;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.rumors.reactnativesettings.handlers;

import android.content.Context;
import android.view.accessibility.CaptioningManager;

import io.rumors.reactnativesettings.Constants;

public class CaptioningSettingsHandler implements SettingsHandler<String> {
private Context mContext;

public CaptioningSettingsHandler(Context context) {
this.mContext = context;
}

public String getSetting() {
CaptioningManager captioningManager = (CaptioningManager) mContext.getSystemService(Context.CAPTIONING_SERVICE);

if (captioningManager.isEnabled()) {
return Constants.ENABLED;
}

return Constants.DISABLED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public String getSetting() {
return Constants.DISABLED;
}
else
{
return Constants.ENABLED;

}
}
}
Loading

0 comments on commit 977cb46

Please sign in to comment.