| 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.chrome.browser.identity; |
| 6 | |
| 7 | import android.content.Context; |
| 8 | import android.provider.Settings; |
| 9 | |
| 10 | import com.google.common.annotations.VisibleForTesting; |
| 11 | |
| 12 | import org.chromium.chrome.browser.util.HashUtil; |
| 13 | |
| 14 | import javax.annotation.Nullable; |
| 15 | |
| 16 | /** |
| 17 | * Unique identificator implementation that uses the Settings.Secure.ANDROID_ID field and MD5 |
| 18 | * hashing. |
| 19 | */ |
| 20 | public class SettingsSecureBasedIdentificationGenerator implements UniqueIdentificationGenerator { |
| 21 | public static final String GENERATOR_ID = "SETTINGS_SECURE_ANDROID_ID"; |
| 22 | private final Context mContext; |
| 23 | |
| 24 | public SettingsSecureBasedIdentificationGenerator(Context context) { |
| 25 | // Since we do not know the lifetime of the given context, we get the application context |
| 26 | // to ensure it is always possible to use it. |
| 27 | mContext = context.getApplicationContext(); |
| 28 | } |
| 29 | |
| 30 | @Override |
| 31 | public String getUniqueId(@Nullable String salt) { |
| 32 | String androidId = getAndroidId(); |
| 33 | if (androidId == null) { |
| 34 | return ""; |
| 35 | } |
| 36 | |
| 37 | String md5Hash = HashUtil.getMd5Hash( |
| 38 | new HashUtil.Params(androidId).withSalt(salt)); |
| 39 | return md5Hash == null ? "" : md5Hash; |
| 40 | } |
| 41 | |
| 42 | @VisibleForTesting |
| 43 | String getAndroidId() { |
| 44 | return Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.ANDROID_ID); |
| 45 | } |
| 46 | } |