6
6
public class DeviceId {
7
7
/**
8
8
* Enum used throughout Countly which controls what kind of ID Countly should use.
9
+ *
9
10
* @deprecated Replace this type with "DeviceIdType"
10
11
*/
11
12
public enum Type {
@@ -34,92 +35,69 @@ public enum Type {
34
35
@ NonNull
35
36
OpenUDIDProvider openUDIDProvider ;
36
37
37
- protected DeviceId (@ NonNull DeviceIdType type , @ Nullable String developerSuppliedId , @ NonNull StorageProvider givenStorageProvider , @ NonNull ModuleLog moduleLog , @ NonNull OpenUDIDProvider openUDIDProvider ) {
38
- if ( type == DeviceIdType .DEVELOPER_SUPPLIED ) {
39
- if (developerSuppliedId == null || "" .equals (developerSuppliedId )) {
40
- throw new IllegalStateException ("If using developer supplied type, a valid device ID should be provided, [" + developerSuppliedId + "]" );
38
+ protected DeviceId (@ NonNull DeviceIdType providedType , @ Nullable String providedId , @ NonNull StorageProvider givenStorageProvider , @ NonNull ModuleLog moduleLog , @ NonNull OpenUDIDProvider openUDIDProvider ) {
39
+ if ( providedType == DeviceIdType .DEVELOPER_SUPPLIED ) {
40
+ if (providedId == null || "" .equals (providedId )) {
41
+ throw new IllegalStateException ("If using developer supplied type, a valid device ID should be provided, [" + providedId + "]" );
41
42
}
42
- } else if ( type == DeviceIdType .TEMPORARY_ID ) {
43
- if (developerSuppliedId == null || "" .equals (developerSuppliedId )) {
44
- throw new IllegalStateException ("If using temporary ID type, a valid device ID should be provided, [" + developerSuppliedId + "]" );
43
+ } else if ( providedType == DeviceIdType .TEMPORARY_ID ) {
44
+ if (providedId == null || "" .equals (providedId )) {
45
+ throw new IllegalStateException ("If using temporary ID type, a valid device ID should be provided, [" + providedId + "]" );
45
46
}
46
47
47
- if (! developerSuppliedId .equals (temporaryCountlyDeviceId )) {
48
- throw new IllegalStateException ("If using temporary ID type, the device ID value should be the required one, [" + developerSuppliedId + "]" );
48
+ if (! providedId .equals (temporaryCountlyDeviceId )) {
49
+ throw new IllegalStateException ("If using temporary ID type, the device ID value should be the required one, [" + providedId + "]" );
49
50
}
50
-
51
- } else if (type == DeviceIdType .OPEN_UDID || type == DeviceIdType .ADVERTISING_ID ){
52
-
51
+ } else if (providedType == DeviceIdType .OPEN_UDID || providedType == DeviceIdType .ADVERTISING_ID ) {
52
+ //just adding this check for completeness
53
53
} else {
54
54
throw new IllegalStateException ("Null device ID type is not allowed" );
55
55
}
56
56
57
57
storageProvider = givenStorageProvider ;
58
58
this .openUDIDProvider = openUDIDProvider ;
59
-
60
- //setup the preferred device ID type
61
- this .type = type ;
62
- this .id = developerSuppliedId ;
63
59
L = moduleLog ;
64
60
65
-
66
- L .d ("[DeviceId-int] initialising with values, device ID:[" + this .id + "] type:[" + this .type + "]" );
61
+ L .d ("[DeviceId-int] initialising with values, device ID:[" + providedId + "] type:[" + providedType + "]" );
67
62
68
63
//check if there wasn't a value set before. Read if from storage
69
64
String storedId = storageProvider .getDeviceID ();
70
- if (storedId != null ) {
71
- //if there was a value saved previously, set it and it's type. Overwrite the in constructor set ones
72
- this .id = storedId ;
73
- this .type = retrieveStoredType ();
74
-
75
- L .d ("[DeviceId-int] retrieveId, Retrieving a previously set device ID:[" + this .id + "] type:[" + this .type + "]" );
76
- } else {
77
- L .d ("[DeviceId-int] retrieveId, no previous ID stored" );
78
- }
79
-
80
- //call init implicitly
81
- init ();
82
- }
83
-
84
- /**
85
- * Initialize device ID generation
86
- */
87
- private void init () {
88
65
DeviceIdType storedType = retrieveStoredType ();
89
- L .d ("[DeviceId-int] init, current type:[" + type + "] overriddenType:[" + storedType + "]" );
90
66
91
- // Some time ago some ID generation strategy was not available and SDK fell back to
92
- // some other strategy. We still have to use that strategy.
93
- if (storedType != null && storedType != type ) {
94
- L .i ("[DeviceId-int] init, Overridden device ID generation strategy detected: " + storedType + ", using it instead of " + this .type );
95
- type = storedType ;
96
- }
67
+ L .d ("[DeviceId-int] The following values were stored, device ID:[" + storedId + "] type:[" + storedType + "]" );
97
68
98
- if (type == null ) {
99
- L .e ("[DeviceId-int] init, device id type currently is null, falling back to OPEN_UDID" );
100
- type = DeviceIdType .OPEN_UDID ;
101
- }
69
+ if (storedId != null && storedType != null ) {
70
+ //values are set, just use them and ignore the provided ones
71
+ id = storedId ;
72
+ type = storedType ;
73
+ } else {
74
+ if (storedType == null && storedId != null ) {
75
+ L .e ("[DeviceId-int] init, device id type currently is null, falling back to OPEN_UDID" );
76
+ setAndStoreId (DeviceIdType .OPEN_UDID , storedId );
77
+ }
102
78
103
- String storedID = storageProvider .getDeviceID ();
104
-
105
- if (storedID == null ) {
106
- //id value will be regenerated only if the values isn't already set
107
- //this is to prevent the device id to change in case the underlying mechanism for openUDID or advertising ID changes
108
- switch (type ) {
109
- case DEVELOPER_SUPPLIED :
110
- // no initialization for developer id
111
- // we just store the provided value so that it's
112
- setAndStoreId (DeviceIdType .DEVELOPER_SUPPLIED , id );
113
- break ;
114
- case OPEN_UDID :
115
- L .i ("[DeviceId-int] Using OpenUDID" );
116
- fallbackToOpenUDID ();
117
- break ;
118
- case ADVERTISING_ID :
119
- // Fall back to OpenUDID on devices without google play services set up
120
- L .i ("[DeviceId-int] Use of Advertising ID is deprecated, falling back to OpenUDID" );
121
- fallbackToOpenUDID ();
122
- break ;
79
+ if (storedId == null ) {
80
+ //id value will be regenerated only if the values isn't already set
81
+ //this is to prevent the device id to change in case the underlying mechanism for openUDID or advertising ID changes
82
+ switch (providedType ) {
83
+ case TEMPORARY_ID :
84
+ setAndStoreId (DeviceIdType .TEMPORARY_ID , providedId );
85
+ break ;
86
+ case DEVELOPER_SUPPLIED :
87
+ // no initialization for developer id
88
+ // we just store the provided value so that it's
89
+ setAndStoreId (DeviceIdType .DEVELOPER_SUPPLIED , providedId );
90
+ break ;
91
+ case OPEN_UDID :
92
+ L .i ("[DeviceId-int] Using OpenUDID" );
93
+ setAndStoreId (DeviceIdType .OPEN_UDID , openUDIDProvider .getOpenUDID ());
94
+ break ;
95
+ case ADVERTISING_ID :
96
+ // Fall back to OpenUDID on devices without google play services set up
97
+ L .i ("[DeviceId-int] Use of Advertising ID is deprecated, falling back to OpenUDID" );
98
+ setAndStoreId (DeviceIdType .OPEN_UDID , openUDIDProvider .getOpenUDID ());
99
+ break ;
100
+ }
123
101
}
124
102
}
125
103
}
@@ -158,6 +136,7 @@ protected String getCurrentId() {
158
136
159
137
/**
160
138
* Used only for tests
139
+ *
161
140
* @param type
162
141
* @param id
163
142
*/
@@ -168,19 +147,19 @@ protected void setId(DeviceIdType type, String id) {
168
147
this .id = id ;
169
148
}
170
149
171
- protected void fallbackToOpenUDID () {
172
- setAndStoreId (DeviceIdType .OPEN_UDID , openUDIDProvider .getOpenUDID ());
173
- }
174
-
175
150
/**
176
151
* If a value is provided, it will take precedence and will not matter what the type is
177
152
*
178
- * @param type
179
153
* @param deviceId
180
154
*/
181
- protected void changeToId (@ NonNull DeviceIdType type , @ Nullable String deviceId ) {
182
- L .v ("[DeviceId-int] changeToId, Device ID is " + id + " (type " + type + ")" );
183
- setAndStoreId (type , deviceId );
155
+ protected void changeToCustomId (@ NonNull String deviceId ) {
156
+ L .v ("[DeviceId-int] changeToCustomId, Device ID is " + id );
157
+ setAndStoreId (DeviceIdType .DEVELOPER_SUPPLIED , deviceId );
158
+ }
159
+
160
+ protected void enterTempIDMode () {
161
+ L .v ("[DeviceId-int] enterTempIDMode" );
162
+ setAndStoreId (DeviceIdType .DEVELOPER_SUPPLIED , ly .count .android .sdk .DeviceId .temporaryCountlyDeviceId );
184
163
}
185
164
186
165
void setAndStoreId (DeviceIdType type , String deviceId ) {
0 commit comments