04085c80acb9062c2fe1b72a931d0eccb5dd0dd3
[platform/framework/web/crosswalk.git] / src / chrome / android / java / src / org / chromium / chrome / browser / identity / SettingsSecureBasedIdentificationGenerator.java
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 org.chromium.base.VisibleForTesting;
11 import org.chromium.chrome.browser.util.HashUtil;
12
13 import javax.annotation.Nullable;
14
15 /**
16  * Unique identificator implementation that uses the Settings.Secure.ANDROID_ID field and MD5
17  * hashing.
18  */
19 public class SettingsSecureBasedIdentificationGenerator implements UniqueIdentificationGenerator {
20     public static final String GENERATOR_ID = "SETTINGS_SECURE_ANDROID_ID";
21     private final Context mContext;
22
23     public SettingsSecureBasedIdentificationGenerator(Context context) {
24         // Since we do not know the lifetime of the given context, we get the application context
25         // to ensure it is always possible to use it.
26         mContext = context.getApplicationContext();
27     }
28
29     @Override
30     public String getUniqueId(@Nullable String salt) {
31         String androidId = getAndroidId();
32         if (androidId == null) {
33             return "";
34         }
35
36         String md5Hash = HashUtil.getMd5Hash(
37                 new HashUtil.Params(androidId).withSalt(salt));
38         return md5Hash == null ? "" : md5Hash;
39     }
40
41     @VisibleForTesting
42     String getAndroidId() {
43         return Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.ANDROID_ID);
44     }
45 }