This repository has been archived by the owner on Mar 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't attempt to get device serial on Android Q., instead use Android…
… ID. Also remove reflection access and that will be flagged in non SDK interfaces
- Loading branch information
Showing
4 changed files
with
69 additions
and
31 deletions.
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
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,62 @@ | ||
package com.securepreferences; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.content.Context; | ||
import android.os.Build; | ||
import android.provider.Settings; | ||
import android.text.TextUtils; | ||
|
||
public class Utils { | ||
|
||
/** | ||
* This method is here for backwards compatibility reasons. Recommend supplying your own Salt | ||
* | ||
* @param context | ||
* @return Consistent between app restarts, device restarts, factory resets, | ||
* however cannot be guaranteed on OS updates. | ||
*/ | ||
@SuppressLint("MissingPermission") | ||
static String getDefaultSalt(Context context) { | ||
|
||
//Android Q removes all access to Serial, fallback to Settings.Secure.ANDROID_ID | ||
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) { | ||
return getSecureDeviceId(context); | ||
} else { | ||
return getDeviceSerialNumber(context); | ||
} | ||
} | ||
|
||
@SuppressLint("HardwareIds") | ||
private static String getSecureDeviceId(Context context) { | ||
return Settings.Secure.getString( | ||
context.getContentResolver(), | ||
Settings.Secure.ANDROID_ID | ||
); | ||
} | ||
|
||
/** | ||
* Gets the hardware serial number of this device. This only for backwards compatibility | ||
* | ||
* @return serial number or Settings.Secure.ANDROID_ID if not available. | ||
*/ | ||
@SuppressLint("MissingPermission") | ||
private static String getDeviceSerialNumber(Context context) { | ||
try { | ||
String deviceSerial = ""; | ||
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) { | ||
deviceSerial = Build.getSerial(); | ||
} else { | ||
deviceSerial = Build.SERIAL; | ||
} | ||
|
||
if (TextUtils.isEmpty(deviceSerial)) { | ||
return getSecureDeviceId(context); | ||
} else { | ||
return deviceSerial; | ||
} | ||
} catch (Exception ignored) { | ||
// Fall back to Android_ID | ||
return getSecureDeviceId(context); | ||
} | ||
} | ||
} |
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