Skip to content

Commit 977cb46

Browse files
authored
Merge pull request #38 from rmrs/feat/captioning_setting
feat: add captioning setting
2 parents 81e53c2 + b9abb47 commit 977cb46

File tree

13 files changed

+248
-62
lines changed

13 files changed

+248
-62
lines changed

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ build/
3838
.gradle
3939
local.properties
4040
*.iml
41+
.project
42+
.settings
43+
.classpath
4144

4245
# BUCK
4346
buck-out/
4447
\.buckd/
45-
*.keystore
46-
48+
*.keystore

.prettierrc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"printWidth": 80,
3+
"tabWidth": 2,
4+
"semi": true,
5+
"singleQuote": true,
6+
"trailingComma": "all",
7+
"useTabs": false,
8+
"overrides": [
9+
{
10+
"files": "*.json",
11+
"options": { "printWidth": 200 }
12+
}
13+
]
14+
}

README.md

Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# react-native-settings
32

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

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

2221
## Getting started
2322

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

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

34-
``` java
33+
```java
3534
...
3635

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

9493
```javascript
95-
import RNSettings from 'react-native-settings'
94+
import RNSettings from 'react-native-settings';
9695

9796
RNSettings.getSetting(RNSettings.LOCATION_SETTING).then(result => {
9897
if (result == RNSettings.ENABLED) {
99-
console.log('location is enabled')
98+
console.log('location is enabled');
10099
} else {
101-
console.log('location is disabled')
100+
console.log('location is disabled');
102101
}
103-
})
102+
});
104103
```
105104

106105
#### Android only
107106

108107
```javascript
109-
import RNSettings from 'react-native-settings'
108+
import RNSettings from 'react-native-settings';
110109

111110
RNSettings.getSetting(RNSettings.AIRPLANE_MODE_SETTING).then(result => {
112111
if (result == RNSettings.ENABLED) {
113-
console.log('airplane mode is enabled')
112+
console.log('airplane mode is enabled');
114113
} else {
115-
console.log('airplane mode is disabled')
114+
console.log('airplane mode is disabled');
116115
}
117-
})
116+
});
117+
118+
RNSettings.getSetting(RNSettings.CAPTIONING_SETTINGS).then(result => {
119+
if (result == RNSettings.ENABLED) {
120+
console.log('captioning is enabled');
121+
} else {
122+
console.log('captioning is disabled');
123+
}
124+
});
118125
```
119126

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

122129
```javascript
123-
import RNSettings from 'react-native-settings'
124-
125-
RNSettings.openSetting(RNSettings.ACTION_LOCATION_SOURCE_SETTINGS).
126-
then((result) => {
127-
if (result === RNSettings.ENABLED) {
128-
console.log('location is enabled')
129-
}
130-
131-
RNSettings.openSetting(RNSettings.ACTION_AIRPLANE_MODE_SETTINGS).
132-
then((result) => {
133-
if (result === RNSettings.ENABLED) {
134-
console.log('airplane mode is enabled')
135-
}
130+
import RNSettings from 'react-native-settings';
131+
132+
RNSettings.openSetting(RNSettings.ACTION_LOCATION_SOURCE_SETTINGS).then(
133+
result => {
134+
if (result === RNSettings.ENABLED) {
135+
console.log('location is enabled');
136+
}
137+
},
138+
);
139+
140+
RNSettings.openSetting(RNSettings.ACTION_AIRPLANE_MODE_SETTINGS).then(
141+
result => {
142+
if (result === RNSettings.ENABLED) {
143+
console.log('airplane mode is enabled');
144+
}
145+
},
146+
);
147+
148+
RNSettings.openSetting(RNSettings.ACTION_CAPTIONING_SETTINGS).then(result => {
149+
if (result === RNSettings.ENABLED) {
150+
console.log('captioning is enabled');
151+
}
152+
});
136153
```
137154

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

140157
```javascript
141-
import RNSettings from 'react-native-settings'
142-
import { DeviceEventEmitter } from 'react-native'
158+
import RNSettings from 'react-native-settings';
159+
import { DeviceEventEmitter } from 'react-native';
143160

144-
_handleGPSProviderEvent = (e) => {
161+
_handleGPSProviderEvent = e => {
145162
if (e[RNSettings.LOCATION_SETTING] === RNSettings.DISABLED) {
146-
console.log('Location was disabled')
163+
console.log('Location was disabled');
147164
}
148-
}
165+
};
149166

150-
_handleAirplaneModeEvent = (e) => {
167+
_handleAirplaneModeEvent = e => {
151168
if (e[RNSettings.AIRPLANE_MODE_SETTING] === RNSettings.ENABLED) {
152-
console.log('airplane mode was enabled')
169+
console.log('airplane mode was enabled');
153170
}
154-
}
171+
};
155172

156-
DeviceEventEmitter.addListener(RNSettings.GPS_PROVIDER_EVENT, this._handleGPSProviderEvent)
157-
DeviceEventEmitter.addListener(RNSettings.AIRPLANE_MODE_EVENT, this._handleAirplaneModeEvent)
173+
_handleCaptioningEvent = e => {
174+
if (e[RNSettings.CAPTIONING_SETTINGS] === RNSettings.ENABLED) {
175+
console.log('captioning was enabled');
176+
}
177+
};
178+
179+
DeviceEventEmitter.addListener(
180+
RNSettings.GPS_PROVIDER_EVENT,
181+
this._handleGPSProviderEvent,
182+
);
183+
DeviceEventEmitter.addListener(
184+
RNSettings.AIRPLANE_MODE_EVENT,
185+
this._handleAirplaneModeEvent,
186+
);
187+
DeviceEventEmitter.addListener(
188+
RNSettings.CAPTIONING_EVENT,
189+
this._handleCaptioningEvent,
190+
);
158191
```

android/src/main/java/io/rumors/reactnativesettings/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ public class Constants {
44
//Broadcast Events
55
public static final String AIRPLANE_MODE_CHANGED = "AIRPLANE_MODE_CHANGED";
66
public static final String PROVIDERS_CHANGED = "PROVIDERS_CHANGED";
7+
public static final String CAPTIONING_CHANGED = "CAPTIONING_CHANGED";
78

89
//Settings Names
910
public static final String LOCATION_SETTING = "LOCATION_SETTING";
1011
public static final String AIRPLANE_MODE_SETTING = "AIRPLANE_MODE_SETTING";
12+
public static final String CAPTIONING_SETTINGS = "CAPTIONING_SETTINGS";
1113

1214
//Settings Values
1315
public static final String ENABLED = "ENABLED";

android/src/main/java/io/rumors/reactnativesettings/RNSettingsModule.java

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import android.content.IntentFilter;
77
import android.app.Activity;
88
import android.content.Context;
9-
import android.support.annotation.Nullable;
9+
import androidx.annotation.Nullable;
1010
import android.content.BroadcastReceiver;
1111

1212
import com.facebook.react.bridge.NativeModule;
@@ -22,6 +22,7 @@
2222
import com.facebook.react.modules.core.DeviceEventManagerModule;
2323

2424
import io.rumors.reactnativesettings.handlers.*;
25+
import io.rumors.reactnativesettings.listeners.*;
2526

2627
import java.util.Map;
2728
import java.util.HashMap;
@@ -30,6 +31,7 @@ public class RNSettingsModule extends ReactContextBaseJavaModule {
3031
//javascript event names
3132
private static final String GPS_PROVIDER_EVENT = "GPS_PROVIDER_EVENT";
3233
private static final String AIRPLANE_MODE_EVENT = "AIRPLANE_MODE_EVENT";
34+
private static final String CAPTIONING_EVENT = "CAPTIONING_EVENT";
3335

3436
//error values
3537
private static final String E_FAILED_TO_GET_SETTINGS = "E_FAILED_TO_GET_SETTINGS";
@@ -38,6 +40,7 @@ public class RNSettingsModule extends ReactContextBaseJavaModule {
3840
//open settings names
3941
private static final String ACTION_LOCATION_SOURCE_SETTINGS = "ACTION_LOCATION_SOURCE_SETTINGS";
4042
private static final String ACTION_AIRPLANE_MODE_SETTINGS = "ACTION_AIRPLANE_MODE_SETTINGS";
43+
private static final String ACTION_CAPTIONING_SETTINGS = "ACTION_CAPTIONING_SETTINGS";
4144

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

64-
private class LocationReceiver extends BroadcastReceiver {
65-
@Override
66-
public void onReceive(Context context, Intent intent) {
67-
String providerSetting = intent.getStringExtra(Constants.LOCATION_SETTING);
68-
WritableMap params = Arguments.createMap();
69-
params.putString(Constants.LOCATION_SETTING, providerSetting);
70-
sendEvent(GPS_PROVIDER_EVENT, params);
67+
private class SettingReceiver extends BroadcastReceiver {
68+
private String mSettingName;
69+
private String mEventName;
70+
71+
SettingReceiver(String settingName, String eventName) {
72+
this.mSettingName = settingName;
73+
this.mEventName = eventName;
7174
}
72-
}
7375

74-
private class AirplaneModeReceiver extends BroadcastReceiver {
7576
@Override
7677
public void onReceive(Context context, Intent intent) {
77-
String providerSetting = intent.getStringExtra(Constants.AIRPLANE_MODE_SETTING);
78+
String providerSetting = intent.getStringExtra(mSettingName);
7879
WritableMap params = Arguments.createMap();
79-
params.putString(Constants.AIRPLANE_MODE_SETTING, providerSetting);
80-
sendEvent(AIRPLANE_MODE_EVENT, params);
80+
params.putString(mSettingName, providerSetting);
81+
sendEvent(mEventName, params);
8182
}
8283
}
8384

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

94-
public RNSettingsModule(ReactApplicationContext reactContext) {
95-
super(reactContext);
96-
this.mReactContext = reactContext;
95+
private void initReceivers() {
96+
registerReceiver(mReactContext, Constants.PROVIDERS_CHANGED, new SettingReceiver(Constants.LOCATION_SETTING, GPS_PROVIDER_EVENT));
97+
registerReceiver(mReactContext, Constants.AIRPLANE_MODE_CHANGED, new SettingReceiver(Constants.AIRPLANE_MODE_SETTING, AIRPLANE_MODE_EVENT));
98+
registerReceiver(mReactContext, Constants.CAPTIONING_CHANGED, new SettingReceiver(Constants.CAPTIONING_SETTINGS, CAPTIONING_EVENT));
99+
}
97100

98-
this.registerReceiver(reactContext, Constants.PROVIDERS_CHANGED, new LocationReceiver());
99-
this.registerReceiver(reactContext, Constants.AIRPLANE_MODE_CHANGED, new AirplaneModeReceiver());
101+
private void initListeners() {
102+
new CaptioningChangeListener(mReactContext);
103+
}
100104

101-
mSettingsHandlers.put(Constants.LOCATION_SETTING, new LocationSettingsHandler(reactContext));
102-
mSettingsHandlers.put(Constants.AIRPLANE_MODE_SETTING, new AirplaneModeSettingsHandler(reactContext));
105+
private void initHandlers() {
106+
mSettingsHandlers.put(Constants.LOCATION_SETTING, new LocationSettingsHandler(mReactContext));
107+
mSettingsHandlers.put(Constants.AIRPLANE_MODE_SETTING, new AirplaneModeSettingsHandler(mReactContext));
108+
mSettingsHandlers.put(Constants.CAPTIONING_SETTINGS, new CaptioningSettingsHandler(mReactContext));
109+
}
103110

111+
private void initRequestCodes() {
104112
mOpenSettingToRequestCode.put(Settings.ACTION_LOCATION_SOURCE_SETTINGS, 0);
105113
mOpenSettingToRequestCode.put(Settings.ACTION_AIRPLANE_MODE_SETTINGS, 1);
114+
mOpenSettingToRequestCode.put(Settings.ACTION_CAPTIONING_SETTINGS, 2);
106115

107116
mRequestCodeToOpenSetting.put(0, Settings.ACTION_LOCATION_SOURCE_SETTINGS);
108117
mRequestCodeToOpenSetting.put(1, Settings.ACTION_AIRPLANE_MODE_SETTINGS);
118+
mRequestCodeToOpenSetting.put(2, Settings.ACTION_CAPTIONING_SETTINGS);
119+
}
109120

121+
private void initSettingsActions() {
110122
mOpenSettingToSettingsName.put(Settings.ACTION_LOCATION_SOURCE_SETTINGS, Constants.LOCATION_SETTING);
111123
mOpenSettingToSettingsName.put(Settings.ACTION_AIRPLANE_MODE_SETTINGS, Constants.AIRPLANE_MODE_SETTING);
124+
mOpenSettingToSettingsName.put(Settings.ACTION_CAPTIONING_SETTINGS, Constants.CAPTIONING_SETTINGS);
125+
}
126+
127+
public RNSettingsModule(ReactApplicationContext reactContext) {
128+
super(reactContext);
129+
this.mReactContext = reactContext;
112130

131+
initReceivers();
132+
initListeners();
133+
initHandlers();
134+
initRequestCodes();
135+
initSettingsActions();
136+
113137
reactContext.addActivityEventListener(mActivityEventListener);
114138
}
115139

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

129154
//get settings
130155
constants.put(Constants.LOCATION_SETTING, Constants.LOCATION_SETTING);
131156
constants.put(Constants.AIRPLANE_MODE_SETTING, Constants.AIRPLANE_MODE_SETTING);
157+
constants.put(Constants.CAPTIONING_SETTINGS, Constants.CAPTIONING_SETTINGS);
132158
constants.put(Constants.ENABLED, Constants.ENABLED);
133159
constants.put(Constants.DISABLED, Constants.DISABLED);
134160

135161
//open settings
136162
constants.put(ACTION_LOCATION_SOURCE_SETTINGS, Settings.ACTION_LOCATION_SOURCE_SETTINGS);
137163
constants.put(ACTION_AIRPLANE_MODE_SETTINGS, Settings.ACTION_AIRPLANE_MODE_SETTINGS);
164+
constants.put(ACTION_CAPTIONING_SETTINGS, Settings.ACTION_CAPTIONING_SETTINGS);
138165
return constants;
139166
}
140167

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.rumors.reactnativesettings.handlers;
2+
3+
import android.content.Context;
4+
import android.view.accessibility.CaptioningManager;
5+
6+
import io.rumors.reactnativesettings.Constants;
7+
8+
public class CaptioningSettingsHandler implements SettingsHandler<String> {
9+
private Context mContext;
10+
11+
public CaptioningSettingsHandler(Context context) {
12+
this.mContext = context;
13+
}
14+
15+
public String getSetting() {
16+
CaptioningManager captioningManager = (CaptioningManager) mContext.getSystemService(Context.CAPTIONING_SERVICE);
17+
18+
if (captioningManager.isEnabled()) {
19+
return Constants.ENABLED;
20+
}
21+
22+
return Constants.DISABLED;
23+
}
24+
}

android/src/main/java/io/rumors/reactnativesettings/handlers/LocationSettingsHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public String getSetting() {
5252
return Constants.DISABLED;
5353
}
5454
else
55+
{
5556
return Constants.ENABLED;
56-
57+
}
5758
}
5859
}

0 commit comments

Comments
 (0)