Skip to content

Commit 3ac8fa1

Browse files
[Java] Add networking fallback to add device ID just before sending a request. (#163)
* feat: fallback for device id * refactor: remove unnecessary funcs
1 parent dae1fad commit 3ac8fa1

File tree

2 files changed

+7
-48
lines changed

2 files changed

+7
-48
lines changed

sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleDeviceIdCore.java

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -122,24 +122,6 @@ public void deviceIdChanged(String oldDeviceId, boolean withMerge) {
122122
}
123123
}
124124

125-
/**
126-
* Puts {@code "device_id"} parameter into all requests which don't have it yet
127-
*
128-
* @param config InternalConfig to run in
129-
* @param deviceId deviceId string
130-
* @return {@code true} if {@link Request}s changed successfully, {@code false} otherwise
131-
*/
132-
private boolean transformRequests(final InternalConfig config, final String deviceId) {
133-
return Storage.transform(config, Request.getStoragePrefix(), (id, data) -> {
134-
Request request = new Request(id);
135-
if (request.restore(data, L) && !request.params.has(Params.PARAM_DEVICE_ID)) {
136-
request.params.add(Params.PARAM_DEVICE_ID, deviceId);
137-
return request.store(L);
138-
}
139-
return null;
140-
});
141-
}
142-
143125
/**
144126
* Logging into app-specific account:
145127
* - reset device id and notify modules;
@@ -192,7 +174,6 @@ protected void changeDeviceIdInternal(String id, DeviceIdType type, boolean with
192174
L.d("[ModuleDeviceIdCore] changeDeviceIdInternal, Ending session because device id was unset from [" + old.id + "]");
193175
session.end(null, null, old.id);
194176
}
195-
changeOldRequestsDeviceIds(did);
196177
}
197178

198179
SDKCore.instance.notifyModulesDeviceIdChanged(old.id, withMerge);
@@ -239,34 +220,6 @@ protected Config.DID acquireId(final InternalConfig config) {
239220
return did;
240221
}
241222

242-
private void changeOldRequestsDeviceIds(Config.DID deviceId) {
243-
if (this.tasks == null) {
244-
this.tasks = new Tasks("deviceId", L);
245-
}
246-
tasks.run(new Tasks.Task<Object>(0L) {
247-
@Override
248-
public Object call() {
249-
// put device_id parameter into existing requests
250-
L.i("[ModuleDeviceIdCore] changeOldRequestsDeviceIds, Adding device_id to previous requests");
251-
boolean success = transformRequests(internalConfig, deviceId.id);
252-
if (success) {
253-
L.i("[ModuleDeviceIdCore] changeOldRequestsDeviceIds, First transform: success");
254-
} else {
255-
L.w("[ModuleDeviceIdCore] changeOldRequestsDeviceIds, First transform: failure");
256-
}
257-
258-
// do it second time in case new requests were added during first attempt
259-
success = transformRequests(internalConfig, deviceId.id);
260-
if (!success) {
261-
L.e("[ModuleDeviceIdCore] changeOldRequestsDeviceIds, Failed to put device_id into existing requests, following behaviour for unhandled requests is undefined.");
262-
} else {
263-
L.i("[ModuleDeviceIdCore] changeOldRequestsDeviceIds, Second transform: success");
264-
}
265-
return null;
266-
}
267-
});
268-
}
269-
270223
@Override
271224
public void stop(InternalConfig config, boolean clear) {
272225
if (tasks != null) {

sdk-java/src/main/java/ly/count/sdk/java/internal/Transport.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ public HttpURLConnection openConnection(String url, String params, boolean using
123123
*/
124124
HttpURLConnection connection(final Request request, final User user) throws IOException {
125125
String endpoint = request.params.remove(Request.ENDPOINT);
126+
127+
if (!request.params.has("device_id") && config.getDeviceId() != null) {
128+
//fallback if request does not have any device id
129+
request.params.add("device_id", config.getDeviceId().id);
130+
}
131+
126132
if (endpoint == null) {
127133
endpoint = "/i?";
128134
}
@@ -402,7 +408,7 @@ private void setPins(Set<String> keys, Set<String> certs) throws CertificateExce
402408
X509EncodedKeySpec spec = new X509EncodedKeySpec(data);
403409
KeyFactory kf = KeyFactory.getInstance("RSA");
404410
PublicKey k = kf.generatePublic(spec);
405-
411+
406412
keyPins.add(k.getEncoded());
407413
} catch (InvalidKeySpecException e) {
408414
L.d("[network] Certificate in instead of public key it seems " + e);

0 commit comments

Comments
 (0)