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.
5 package org.chromium.chrome.browser.identity;
7 import android.content.Context;
8 import android.content.SharedPreferences;
9 import android.preference.PreferenceManager;
11 import org.chromium.base.VisibleForTesting;
13 import java.util.UUID;
15 import javax.annotation.Nullable;
18 * Generates unique IDs that are {@link UUID} strings.
20 public class UuidBasedUniqueIdentificationGenerator implements UniqueIdentificationGenerator {
21 public static final String GENERATOR_ID = "UUID";
22 private final Context mContext;
23 private final String mPreferenceKey;
25 public UuidBasedUniqueIdentificationGenerator(Context context, String preferenceKey) {
27 mPreferenceKey = preferenceKey;
31 public String getUniqueId(@Nullable String salt) {
32 SharedPreferences preferences = PreferenceManager
33 .getDefaultSharedPreferences(mContext);
34 String storedUniqueId = preferences.getString(mPreferenceKey, null);
35 if (storedUniqueId != null) {
36 return storedUniqueId;
39 // Generate a new unique ID.
40 String uniqueId = getUUID();
42 // Store the field so we ensure we always return the same unique ID.
43 SharedPreferences.Editor editor = preferences.edit();
44 editor.putString(mPreferenceKey, uniqueId);
52 return UUID.randomUUID().toString();