提交 0b472e96 编写于 作者: E Eric Frohnhoefer

Use single PreferenceStore for Ad ID and UUID

Change-Id: I4d6dc87adf575f05c7b10e722ff80a75d4540e8a
上级 ab795438
......@@ -23,18 +23,16 @@ import android.text.TextUtils;
import com.twitter.sdk.android.core.Twitter;
import com.twitter.sdk.android.core.internal.persistence.PreferenceStore;
import com.twitter.sdk.android.core.internal.persistence.PreferenceStoreImpl;
class AdvertisingInfoProvider {
private static final String ADVERTISING_INFO_PREFERENCES = "TwitterAdvertisingInfoPreferences";
private static final String PREFKEY_LIMIT_AD_TRACKING = "limit_ad_tracking_enabled";
private static final String PREFKEY_ADVERTISING_ID = "advertising_id";
private final Context context;
private final PreferenceStore preferenceStore;
AdvertisingInfoProvider(Context context) {
AdvertisingInfoProvider(Context context, PreferenceStore preferenceStore) {
this.context = context.getApplicationContext();
this.preferenceStore = new PreferenceStoreImpl(context, ADVERTISING_INFO_PREFERENCES);
this.preferenceStore = preferenceStore;
}
/**
......
......@@ -18,7 +18,6 @@
package com.twitter.sdk.android.core.internal;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import com.twitter.sdk.android.core.Twitter;
......@@ -30,15 +29,10 @@ import java.io.OutputStream;
import java.util.Scanner;
public class CommonUtils {
static final String CLS_SHARED_PREFERENCES_NAME = "com.crashlytics.prefs";
static final String TRACE_ENABLED_RESOURCE_NAME = "com.twitter.sdk.android.TRACE_ENABLED";
static final boolean TRACE_ENABLED_DEFAULT = false;
private static Boolean clsTrace;
public static SharedPreferences getSharedPrefs(Context context) {
return context.getSharedPreferences(CLS_SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
}
public static String streamToString(InputStream is) throws IOException {
// Previous code was running into this: http://code.google.com/p/android/issues/detail?id=14562
// on Android 2.3.3. The below code, cribbed from: http://weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html
......
......@@ -22,6 +22,8 @@ import android.content.SharedPreferences;
import android.os.Build;
import com.twitter.sdk.android.core.Twitter;
import com.twitter.sdk.android.core.internal.persistence.PreferenceStore;
import com.twitter.sdk.android.core.internal.persistence.PreferenceStoreImpl;
import java.util.Locale;
import java.util.UUID;
......@@ -30,7 +32,8 @@ import java.util.regex.Pattern;
public class IdManager {
static final String COLLECT_IDENTIFIERS_ENABLED = "com.twitter.sdk.android.COLLECT_IDENTIFIERS_ENABLED";
static final String PREFKEY_INSTALLATION_UUID = "crashlytics.installation.id";
static final String ADVERTISING_PREFERENCES = "com.twitter.sdk.android.AdvertisingPreferences";
static final String PREFKEY_INSTALLATION_UUID = "installation_uuid";
/**
* Regex for stripping all non-alphnumeric characters from ALL the identifier fields.
......@@ -41,8 +44,8 @@ public class IdManager {
private final ReentrantLock installationIdLock = new ReentrantLock();
private final boolean collectHardwareIds;
private final Context appContext;
private final String appIdentifier;
private final PreferenceStore preferenceStore;
AdvertisingInfoProvider advertisingInfoProvider;
AdvertisingInfo advertisingInfo;
......@@ -54,17 +57,22 @@ public class IdManager {
* is null
*/
public IdManager(Context appContext) {
this(appContext, new AdvertisingInfoProvider(appContext));
this(appContext, new PreferenceStoreImpl(appContext, ADVERTISING_PREFERENCES));
}
IdManager(Context appContext, AdvertisingInfoProvider advertisingInfoProvider) {
IdManager(Context appContext, PreferenceStore preferenceStore) {
this(appContext, preferenceStore, new AdvertisingInfoProvider(appContext, preferenceStore));
}
IdManager(Context appContext, PreferenceStore preferenceStore,
AdvertisingInfoProvider advertisingInfoProvider) {
if (appContext == null) {
throw new IllegalArgumentException("appContext must not be null");
}
this.appContext = appContext;
this.appIdentifier = appContext.getPackageName();
this.advertisingInfoProvider = advertisingInfoProvider;
this.preferenceStore = preferenceStore;
collectHardwareIds = CommonUtils.getBooleanResourceValue(appContext,
COLLECT_IDENTIFIERS_ENABLED, true);
......@@ -75,8 +83,8 @@ public class IdManager {
}
/**
* Apply consistent formatting and stripping of special characters. Null input is allowed, will return
* null.
* Apply consistent formatting and stripping of special characters. Null input is allowed,
* will return null.
*/
private String formatId(String id) {
return (id == null) ? null : ID_PATTERN.matcher(id).replaceAll("").toLowerCase(Locale.US);
......@@ -90,8 +98,8 @@ public class IdManager {
}
/**
* @return {@link String} identifying the version of Android OS that the device is running. Includes the
* public version number, and an incremental build number, like "4.2.2/573038"
* @return {@link String} identifying the version of Android OS that the device is running.
* Includes the public version number, and an incremental build number, like "4.2.2/573038"
*/
public String getOsVersionString() {
return getOsDisplayVersionString() + "/" + getOsBuildVersionString();
......@@ -114,7 +122,8 @@ public class IdManager {
}
/**
* @return {@link String} identifying the model of this device. Includes the manufacturer and model names.
* @return {@link String} identifying the model of this device. Includes the manufacturer and
* model names.
*/
public String getModelName() {
return String.format(Locale.US, "%s/%s", removeForwardSlashesIn(Build.MANUFACTURER),
......@@ -135,10 +144,10 @@ public class IdManager {
String toReturn = "";
if (collectHardwareIds) {
final SharedPreferences prefs = CommonUtils.getSharedPrefs(appContext);
final SharedPreferences prefs = preferenceStore.get();
toReturn = prefs.getString(PREFKEY_INSTALLATION_UUID, null);
if (toReturn == null) {
toReturn = createInstallationUUID(prefs);
toReturn = createInstallationUUID();
}
}
......@@ -146,18 +155,19 @@ public class IdManager {
}
/**
* Creates the Application Installation ID and stores it in shared prefs. This method is thread safe: if
* the ID already exists when the lock is acquired, that ID will be returned instead of a newly-created
* one.
* Creates the Application Installation ID and stores it in shared prefs. This method is thread
* safe: if the ID already exists when the lock is acquired, that ID will be returned instead of
* a newly-created one.
**/
private String createInstallationUUID(SharedPreferences prefs) {
private String createInstallationUUID() {
installationIdLock.lock();
try {
String uuid = prefs.getString(PREFKEY_INSTALLATION_UUID, null);
String uuid = preferenceStore.get().getString(PREFKEY_INSTALLATION_UUID, null);
if (uuid == null) {
uuid = formatId(UUID.randomUUID().toString());
prefs.edit().putString(PREFKEY_INSTALLATION_UUID, uuid).apply();
preferenceStore
.save(preferenceStore.edit().putString(PREFKEY_INSTALLATION_UUID, uuid));
}
return uuid;
......@@ -199,6 +209,4 @@ public class IdManager {
return toReturn;
}
}
......@@ -20,6 +20,9 @@ package com.twitter.sdk.android.core.internal;
import android.content.Context;
import android.content.SharedPreferences;
import com.twitter.sdk.android.core.internal.persistence.PreferenceStore;
import com.twitter.sdk.android.core.internal.persistence.PreferenceStoreImpl;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -28,7 +31,7 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import static com.twitter.sdk.android.core.internal.CommonUtils.CLS_SHARED_PREFERENCES_NAME;
import static com.twitter.sdk.android.core.internal.IdManager.ADVERTISING_PREFERENCES;
import static com.twitter.sdk.android.core.internal.IdManager.PREFKEY_INSTALLATION_UUID;
import static org.junit.Assert.assertEquals;
......@@ -46,6 +49,7 @@ public class IdManagerTest {
@Mock
AdvertisingInfoProvider mockAdvertisingInfoProvider;
PreferenceStore mockPreferenceStore;
AdvertisingInfo advertisingInfo;
IdManager idManager;
Context context;
......@@ -57,7 +61,10 @@ public class IdManagerTest {
when(mockAdvertisingInfoProvider.getAdvertisingInfo()).thenReturn(advertisingInfo);
context = RuntimeEnvironment.application;
idManager = new IdManager(context, mockAdvertisingInfoProvider);
mockPreferenceStore = new PreferenceStoreImpl(context, ADVERTISING_PREFERENCES);
idManager = new IdManager(context, mockPreferenceStore, mockAdvertisingInfoProvider);
}
@Test
......@@ -88,14 +95,14 @@ public class IdManagerTest {
assertNotNull(TEST_SECURE_ID, uuid);
final SharedPreferences prefs = context
.getSharedPreferences(CLS_SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
.getSharedPreferences(ADVERTISING_PREFERENCES, Context.MODE_PRIVATE);
assertEquals(uuid, prefs.getString(PREFKEY_INSTALLATION_UUID, ""));
}
@Test
public void testGetDeviceUUID_shouldReturnSavedUUID() {
final SharedPreferences prefs = context
.getSharedPreferences(CLS_SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
.getSharedPreferences(ADVERTISING_PREFERENCES, Context.MODE_PRIVATE);
prefs.edit().putString(PREFKEY_INSTALLATION_UUID, TEST_UUID).apply();
final String uuid = idManager.getDeviceUUID();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册