-
Notifications
You must be signed in to change notification settings - Fork 213
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
fix ANR issue caused by MediaDRM api #791
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
de8e981
make getDeviceId async
wenxi-zeng e35e071
bug fix
wenxi-zeng 235b71a
make GetDeviceIdTask testable
wenxi-zeng bb0cbe8
add unit tests
wenxi-zeng 7a9d78d
bug fix
wenxi-zeng 1e81ac3
clean up
wenxi-zeng 9c5cd09
add comments
wenxi-zeng 8918174
address comments
wenxi-zeng aa02a95
run spotlessApply
wenxi-zeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
analytics/src/main/java/com/segment/analytics/GetDeviceIdTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/** | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2014 Segment.io, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.segment.analytics; | ||
|
||
import static com.segment.analytics.internal.Utils.getUniqueID; | ||
import static com.segment.analytics.internal.Utils.isNullOrEmpty; | ||
|
||
import android.content.SharedPreferences; | ||
import java.util.UUID; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class GetDeviceIdTask { | ||
|
||
private final ExecutorService executor = Executors.newFixedThreadPool(2); | ||
|
||
private final AnalyticsContext analyticsContext; | ||
|
||
private final SharedPreferences segmentSharedPreference; | ||
|
||
private final CountDownLatch latch; | ||
|
||
private static final String DEVICE_ID_CACHE_KEY = "device.id"; | ||
|
||
public GetDeviceIdTask( | ||
AnalyticsContext analyticsContext, | ||
SharedPreferences segmentSharedPreference, | ||
CountDownLatch latch) { | ||
this.analyticsContext = analyticsContext; | ||
this.segmentSharedPreference = segmentSharedPreference; | ||
this.latch = latch; | ||
} | ||
|
||
public void execute() { | ||
if (cacheHit()) { | ||
return; | ||
} | ||
|
||
final Future<?> future = | ||
executor.submit( | ||
new Runnable() { | ||
@Override | ||
public void run() { | ||
String deviceId = getDeviceId(); | ||
|
||
if (!Thread.currentThread().isInterrupted()) { | ||
updateDeviceId(deviceId); | ||
updateCache(deviceId); | ||
} | ||
} | ||
}); | ||
|
||
executor.execute( | ||
new Runnable() { | ||
@Override | ||
public void run() { | ||
try { | ||
future.get(2, TimeUnit.SECONDS); | ||
} catch (Exception e) { | ||
future.cancel(true); | ||
String fallbackDeviceId = UUID.randomUUID().toString(); | ||
updateDeviceId(fallbackDeviceId); | ||
updateCache(fallbackDeviceId); | ||
} | ||
|
||
latch.countDown(); | ||
executor.shutdownNow(); | ||
} | ||
}); | ||
} | ||
|
||
String getDeviceId() { | ||
// unique id generated from DRM API | ||
String uniqueID = getUniqueID(); | ||
if (!isNullOrEmpty(uniqueID)) { | ||
return uniqueID; | ||
} | ||
|
||
// If this still fails, generate random identifier that does not persist across | ||
// installations | ||
return UUID.randomUUID().toString(); | ||
} | ||
|
||
private boolean cacheHit() { | ||
String cache = segmentSharedPreference.getString(DEVICE_ID_CACHE_KEY, null); | ||
|
||
if (cache != null) { | ||
updateDeviceId(cache); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
private void updateDeviceId(String deviceId) { | ||
synchronized (analyticsContext) { | ||
if (!analyticsContext.containsKey(AnalyticsContext.DEVICE_KEY)) { | ||
analyticsContext.put(AnalyticsContext.DEVICE_KEY, new AnalyticsContext.Device()); | ||
} | ||
|
||
AnalyticsContext.Device device = | ||
(AnalyticsContext.Device) analyticsContext.get(AnalyticsContext.DEVICE_KEY); | ||
device.put(AnalyticsContext.Device.DEVICE_ID_KEY, deviceId); | ||
} | ||
} | ||
|
||
private void updateCache(String deviceId) { | ||
SharedPreferences.Editor editor = segmentSharedPreference.edit(); | ||
editor.putString(DEVICE_ID_CACHE_KEY, deviceId); | ||
editor.apply(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -298,25 +298,12 @@ public static <T> List<T> immutableCopyOf(@Nullable List<T> list) { | |
return Collections.unmodifiableList(new ArrayList<>(list)); | ||
} | ||
|
||
/** Creates a unique device id. */ | ||
public static String getDeviceId() { | ||
// unique id generated from DRM API | ||
String uniqueID = getUniqueID(); | ||
if (!isNullOrEmpty(uniqueID)) { | ||
return uniqueID; | ||
} | ||
|
||
// If this still fails, generate random identifier that does not persist across | ||
// installations | ||
return UUID.randomUUID().toString(); | ||
} | ||
|
||
/** | ||
* Workaround for not able to get device id on Android 10 or above using DRM API {@see | ||
* https://stackoverflow.com/questions/58103580/android-10-imei-no-longer-available-on-api-29-looking-for-alternatives} | ||
* {@see https://developer.android.com/training/articles/user-data-ids} | ||
*/ | ||
private static String getUniqueID() { | ||
public static String getUniqueID() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. given that this is not a safe API to use anymore, would it make sense to move it into the |
||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) return null; | ||
|
||
UUID wideVineUuid = new UUID(-0x121074568629b532L, -0x5c37d8232ae2de13L); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets add a comment that deviceId will be populated async