Skip to content

Commit 34b2c6c

Browse files
committed
feat: add data transmission setting
1 parent 69c4f1d commit 34b2c6c

File tree

15 files changed

+275
-52
lines changed

15 files changed

+275
-52
lines changed

android/app/src/main/java/com/appzung/codepush/react/CodePushConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ public class CodePushConstants {
3333
public static final String LATEST_ROLLBACK_COUNT_KEY = "count";
3434
public static final String CLIENT_UNIQUE_ID_KEY = "clientUniqueId";
3535
public static final String TELEMETRY_ENABLED_KEY = "telemetryEnabled";
36+
public static final String DATA_TRANSMISSION_ENABLED_KEY = "dataTransmissionEnabled";
3637
}

android/app/src/main/java/com/appzung/codepush/react/CodePushNativeModule.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class CodePushNativeModule extends ReactContextBaseJavaModule {
4343
private String mBinaryContentsHash = null;
4444
private String mClientUniqueId = null;
4545
private boolean mTelemetryEnabled = true;
46+
private boolean mDataTransmissionEnabled = true;
4647
private LifecycleEventListener mLifecycleEventListener = null;
4748
private int mMinimumBackgroundDuration = 0;
4849

@@ -83,6 +84,17 @@ public CodePushNativeModule(ReactApplicationContext reactContext, CodePush codeP
8384
mTelemetryEnabled = true;
8485
}
8586
}
87+
88+
if (preferences.contains(CodePushConstants.DATA_TRANSMISSION_ENABLED_KEY)) {
89+
mDataTransmissionEnabled = preferences.getBoolean(CodePushConstants.DATA_TRANSMISSION_ENABLED_KEY, true);
90+
} else {
91+
int defaultDataTransmissionEnabledResId = reactContext.getResources().getIdentifier("CodePushDefaultDataTransmissionEnabled", "bool", reactContext.getPackageName());
92+
if (defaultDataTransmissionEnabledResId != 0) {
93+
mDataTransmissionEnabled = reactContext.getResources().getBoolean(defaultDataTransmissionEnabledResId);
94+
} else {
95+
mDataTransmissionEnabled = true;
96+
}
97+
}
8698
}
8799

88100
@Override
@@ -409,6 +421,7 @@ public void getConfiguration(Promise promise) {
409421
configMap.putString("releaseChannelPublicId", mCodePush.getReleaseChannelPublicId());
410422
configMap.putString("serverUrl", mCodePush.getServerUrl());
411423
configMap.putBoolean("telemetryEnabled", mTelemetryEnabled);
424+
configMap.putBoolean("dataTransmissionEnabled", mDataTransmissionEnabled);
412425

413426
// The binary hash may be null in debug builds
414427
if (mBinaryContentsHash != null) {
@@ -776,6 +789,24 @@ public void getTelemetryEnabled(Promise promise) {
776789
promise.resolve(mTelemetryEnabled);
777790
}
778791

792+
@ReactMethod
793+
public void setDataTransmissionEnabled(boolean enabled, Promise promise) {
794+
try {
795+
SharedPreferences preferences = mCodePush.getContext().getSharedPreferences(CodePushConstants.CODE_PUSH_PREFERENCES, 0);
796+
preferences.edit().putBoolean(CodePushConstants.DATA_TRANSMISSION_ENABLED_KEY, enabled).apply();
797+
mDataTransmissionEnabled = enabled;
798+
promise.resolve(null);
799+
} catch (Exception e) {
800+
CodePushUtils.log(e);
801+
promise.reject(e);
802+
}
803+
}
804+
805+
@ReactMethod
806+
public void getDataTransmissionEnabled(Promise promise) {
807+
promise.resolve(mDataTransmissionEnabled);
808+
}
809+
779810
@ReactMethod
780811
public void addListener(String eventName) {
781812
// Set up any upstream listeners or background tasks as necessary

docs/advanced-usage.md

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,7 @@
22

33
### Privacy / GDPR
44

5-
#### Telemetry
6-
7-
CodePush collects anonymous usage data by default to help improve the service (e.g. to provide you with analytics).
8-
To be fully compliant with GDPR and other privacy regulations, you may want to disable this telemetry collection.
9-
10-
##### Disabling telemetry by default
11-
12-
You can disable telemetry collection by default by configuring your native app:
13-
14-
**Android:**
15-
16-
Add this to your `android/app/build.gradle` file:
17-
18-
```groovy
19-
android {
20-
// ...
21-
defaultConfig {
22-
// ...
23-
resValue "bool", "CodePushDefaultTelemetryEnabled", "false"
24-
}
25-
}
26-
```
27-
28-
**iOS:**
29-
30-
Add this to your `ios/YourApp/Info.plist` file:
31-
32-
```
33-
<key>CodePushDefaultTelemetryEnabled</key>
34-
<false/>
35-
```
36-
37-
##### Managing telemetry during runtime
38-
39-
You can also programmatically check or change telemetry status during app runtime:
40-
41-
```typescript
42-
import * as CodePush from '@appzung/react-native-code-push';
43-
44-
// Check if telemetry is enabled
45-
CodePush.getTelemetryEnabled().then((enabled) => {
46-
console.log('Telemetry is ' + (enabled ? 'enabled' : 'disabled'));
47-
});
48-
49-
// Enable telemetry
50-
CodePush.setTelemetryEnabled(true);
51-
52-
// Disable telemetry
53-
CodePush.setTelemetryEnabled(false);
54-
```
55-
56-
This setting will persist across app sessions.
5+
See [privacy.md](./privacy.md).
576

587
### Multiple environments
598

docs/api-js/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@
4242
- [clearUpdates](functions/clearUpdates.md)
4343
- [disallowRestart](functions/disallowRestart.md)
4444
- [getClientUniqueId](functions/getClientUniqueId.md)
45+
- [getDataTransmissionEnabled](functions/getDataTransmissionEnabled.md)
4546
- [getLogLevel](functions/getLogLevel.md)
4647
- [getTelemetryEnabled](functions/getTelemetryEnabled.md)
4748
- [getUpdateMetadata](functions/getUpdateMetadata.md)
4849
- [notifyAppReady](functions/notifyAppReady.md)
4950
- [resetClientUniqueId](functions/resetClientUniqueId.md)
5051
- [restartApp](functions/restartApp.md)
52+
- [setDataTransmissionEnabled](functions/setDataTransmissionEnabled.md)
5153
- [setLogLevel](functions/setLogLevel.md)
5254
- [setTelemetryEnabled](functions/setTelemetryEnabled.md)
5355
- [sync](functions/sync.md)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[**@appzung/react-native-code-push v11.0.0-rc5**](../README.md)
2+
3+
---
4+
5+
[@appzung/react-native-code-push](../README.md) / getDataTransmissionEnabled
6+
7+
# Function: getDataTransmissionEnabled()
8+
9+
> **getDataTransmissionEnabled**(): `Promise`\<`boolean`\>
10+
11+
Gets the current data transmission status.
12+
13+
When setDataTransmissionEnabled has never been called, returns the default value set in your configuration.
14+
15+
## Returns
16+
17+
`Promise`\<`boolean`\>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[**@appzung/react-native-code-push v11.0.0-rc5**](../README.md)
2+
3+
---
4+
5+
[@appzung/react-native-code-push](../README.md) / setDataTransmissionEnabled
6+
7+
# Function: setDataTransmissionEnabled()
8+
9+
> **setDataTransmissionEnabled**(`enabled`): `Promise`\<`void`\>
10+
11+
Controls data transmission for CodePush. This may be used for privacy optIn.
12+
13+
## Parameters
14+
15+
### enabled
16+
17+
`boolean`
18+
19+
When false, disables all external API calls to CodePush servers. Checking for updates will be disabled.
20+
21+
## Returns
22+
23+
`Promise`\<`void`\>

docs/privacy.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
## Privacy / GDPR
2+
3+
It is up to you as the developer to choose how and if to be GDPR-compliant with your application.
4+
We provide the following tools to help you implement privacy controls when needed.
5+
6+
From an end-user perspective, CodePush's core functionality of checking for and applying updates can be considered essential to the app's operation, as it ensures users receive bug fixes and improvements.
7+
However, the telemetry features that collect usage data to provide you with deployment analytics might be considered non-essential from a privacy standpoint and could require explicit user consent under privacy regulations like GDPR.
8+
9+
- [Data transmission](#data-transmission)
10+
- [Telemetry](#telemetry)
11+
12+
### Data transmission
13+
14+
By default, CodePush connects to the server to check for updates or for other API calls.
15+
16+
If you need to make it opt in or provide users with the ability to opt out of this behavior, you can use the data transmission controls.
17+
18+
#### Disabling data transmission by default (opt in)
19+
20+
You can disable data transmission by default by configuring your native app:
21+
22+
**Android:**
23+
24+
Add this to your `android/app/build.gradle` file:
25+
26+
```groovy
27+
android {
28+
// ...
29+
defaultConfig {
30+
// ...
31+
resValue "bool", "CodePushDefaultDataTransmissionEnabled", "false"
32+
}
33+
}
34+
```
35+
36+
**iOS:**
37+
38+
Add this to your `ios/YourApp/Info.plist` file:
39+
40+
```
41+
<key>CodePushDefaultDataTransmissionEnabled</key>
42+
<false/>
43+
```
44+
45+
#### Managing data transmission during runtime
46+
47+
You can also programmatically check or change data transmission status during app runtime.
48+
This will persist across app sessions.
49+
50+
If you enable data transmission when it was disabled, checking for updates will work again immediately but if your app usually checks for update on app start, the next check will be on app start. You may want to call `sync` manually after resuming data transmission. We don't do this automatically to make things atomic and for the flexibility of the sync options.
51+
52+
```typescript
53+
import * as CodePush from '@appzung/react-native-code-push';
54+
55+
// Check if data transmission is enabled
56+
CodePush.getDataTransmissionEnabled().then((enabled) => {
57+
console.log('Data transmission is ' + (enabled ? 'enabled' : 'disabled'));
58+
});
59+
60+
// Enable data transmission
61+
CodePush.setDataTransmissionEnabled(true);
62+
63+
// Disable data transmission
64+
CodePush.setDataTransmissionEnabled(false);
65+
```
66+
67+
When data transmission is disabled, the CodePush client will not connect to the server to check for updates or for any other API calls.
68+
69+
### Telemetry
70+
71+
CodePush collects anonymous usage data by default for reporting downloads and installs (to provide you with analytics).
72+
You may want to disable this telemetry collection for privacy regulations or user preferences.
73+
74+
#### Disabling telemetry by default (opt in)
75+
76+
You can disable telemetry by default by configuring your native app:
77+
78+
**Android:**
79+
80+
Add this to your `android/app/build.gradle` file:
81+
82+
```groovy
83+
android {
84+
// ...
85+
defaultConfig {
86+
// ...
87+
resValue "bool", "CodePushDefaultTelemetryEnabled", "false"
88+
}
89+
}
90+
```
91+
92+
**iOS:**
93+
94+
Add this to your `ios/YourApp/Info.plist` file:
95+
96+
```
97+
<key>CodePushDefaultTelemetryEnabled</key>
98+
<false/>
99+
```
100+
101+
#### Managing telemetry during runtime
102+
103+
You can also programmatically check or change telemetry status during app runtime.
104+
This will persist across app sessions.
105+
106+
```typescript
107+
import * as CodePush from '@appzung/react-native-code-push';
108+
109+
// Check if telemetry is enabled
110+
CodePush.getTelemetryEnabled().then((enabled) => {
111+
console.log('Telemetry is ' + (enabled ? 'enabled' : 'disabled'));
112+
});
113+
114+
// Enable telemetry
115+
CodePush.setTelemetryEnabled(true);
116+
117+
// Disable telemetry
118+
CodePush.setTelemetryEnabled(false);
119+
```

ios/CodePush/CodePush.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
@property (copy) NSString *serverURL;
106106
@property (copy) NSString *publicKey;
107107
@property (nonatomic) BOOL telemetryEnabled;
108+
@property (nonatomic) BOOL dataTransmissionEnabled;
108109

109110
+ (instancetype)current;
110111

ios/CodePush/CodePush.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,20 @@ - (void)restartAppInternal:(BOOL)onlyIfUpdateIsPending
10631063
resolve(@([[CodePushConfig current] telemetryEnabled]));
10641064
}
10651065

1066+
RCT_EXPORT_METHOD(setDataTransmissionEnabled:(BOOL)enabled
1067+
resolver:(RCTPromiseResolveBlock)resolve
1068+
rejecter:(RCTPromiseRejectBlock)reject)
1069+
{
1070+
[[CodePushConfig current] setDataTransmissionEnabled:enabled];
1071+
resolve(nil);
1072+
}
1073+
1074+
RCT_EXPORT_METHOD(getDataTransmissionEnabled:(RCTPromiseResolveBlock)resolve
1075+
rejecter:(RCTPromiseRejectBlock)reject)
1076+
{
1077+
resolve(@([[CodePushConfig current] dataTransmissionEnabled]));
1078+
}
1079+
10661080
#pragma mark - JavaScript-exported module methods (Private)
10671081

10681082
/*

ios/CodePush/CodePushConfig.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ @implementation CodePushConfig {
1414
static NSString * const ServerURLConfigKey = @"serverUrl";
1515
static NSString * const PublicKeyKey = @"publicKey";
1616
static NSString * const TelemetryEnabledKey = @"telemetryEnabled";
17+
static NSString * const DataTransmissionEnabledKey = @"dataTransmissionEnabled";
1718

1819
+ (instancetype)current
1920
{
@@ -60,6 +61,16 @@ - (instancetype)init
6061
telemetryEnabled = YES;
6162
}
6263

64+
NSNumber *defaultDataTransmissionEnabled = [infoDictionary objectForKey:@"CodePushDefaultDataTransmissionEnabled"];
65+
BOOL dataTransmissionEnabled;
66+
if ([userDefaults objectForKey:DataTransmissionEnabledKey] != nil) {
67+
dataTransmissionEnabled = [userDefaults boolForKey:DataTransmissionEnabledKey];
68+
} else if (defaultDataTransmissionEnabled != nil) {
69+
dataTransmissionEnabled = [defaultDataTransmissionEnabled boolValue];
70+
} else {
71+
dataTransmissionEnabled = YES;
72+
}
73+
6374
if (!serverURL) {
6475
serverURL = @"https://codepush.appzung.com/";
6576
}
@@ -76,6 +87,7 @@ - (instancetype)init
7687
[_configDictionary setObject:publicKey forKey:PublicKeyKey];
7788
}
7889
[_configDictionary setObject:@(telemetryEnabled) forKey:TelemetryEnabledKey];
90+
[_configDictionary setObject:@(dataTransmissionEnabled) forKey:DataTransmissionEnabledKey];
7991

8092
return self;
8193
}
@@ -128,6 +140,19 @@ - (void)setTelemetryEnabled:(BOOL)telemetryEnabled
128140
[preferences synchronize];
129141
}
130142

143+
- (BOOL)dataTransmissionEnabled
144+
{
145+
return [[_configDictionary objectForKey:DataTransmissionEnabledKey] boolValue];
146+
}
147+
148+
- (void)setDataTransmissionEnabled:(BOOL)dataTransmissionEnabled
149+
{
150+
[_configDictionary setValue:@(dataTransmissionEnabled) forKey:DataTransmissionEnabledKey];
151+
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
152+
[preferences setBool:dataTransmissionEnabled forKey:DataTransmissionEnabledKey];
153+
[preferences synchronize];
154+
}
155+
131156
- (void)setAppVersion:(NSString *)appVersion
132157
{
133158
[_configDictionary setValue:appVersion forKey:AppVersionConfigKey];

0 commit comments

Comments
 (0)