Skip to content

Commit d610250

Browse files
authored
Set client engine to Android TV if SDK is used on AndroidTV (#93)
1 parent 0fcfeb2 commit d610250

File tree

5 files changed

+108
-2
lines changed

5 files changed

+108
-2
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
# Optimizely Android X SDK Changelog
2+
### 1.1.0
3+
February 17, 2017
4+
5+
- Support Android TV SDK client engine
6+
- Update to java-core 1.5.0 (https://github.com/optimizely/java-sdk/blob/master/CHANGELOG.md#150)
7+
8+
29
### 1.0.0
310
January 23, 2017
411

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/****************************************************************************
2+
* Copyright 2017, Optimizely, Inc. and contributors *
3+
* *
4+
* Licensed under the Apache License, Version 2.0 (the "License"); *
5+
* you may not use this file except in compliance with the License. *
6+
* You may obtain a copy of the License at *
7+
* *
8+
* http://www.apache.org/licenses/LICENSE-2.0 *
9+
* *
10+
* Unless required by applicable law or agreed to in writing, software *
11+
* distributed under the License is distributed on an "AS IS" BASIS, *
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13+
* See the License for the specific language governing permissions and *
14+
* limitations under the License. *
15+
***************************************************************************/
16+
17+
package com.optimizely.ab.android.sdk;
18+
19+
import android.app.UiModeManager;
20+
import android.content.Context;
21+
import android.content.res.Configuration;
22+
import android.os.Build;
23+
import android.support.annotation.RequiresApi;
24+
import android.support.test.runner.AndroidJUnit4;
25+
26+
import com.optimizely.ab.event.internal.payload.Event;
27+
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
31+
import static junit.framework.Assert.assertEquals;
32+
import static org.mockito.Mockito.mock;
33+
import static org.mockito.Mockito.when;
34+
35+
@RunWith(AndroidJUnit4.class)
36+
public class OptimizelyClientEngineTest {
37+
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB_MR2)
38+
@Test
39+
public void testGetClientEngineFromContextAndroidTV() {
40+
Context context = mock(Context.class);
41+
UiModeManager uiModeManager = mock(UiModeManager.class);
42+
when(context.getSystemService(Context.UI_MODE_SERVICE)).thenReturn(uiModeManager);
43+
when(uiModeManager.getCurrentModeType()).thenReturn(Configuration.UI_MODE_TYPE_TELEVISION);
44+
assertEquals(Event.ClientEngine.ANDROID_TV_SDK, OptimizelyClientEngine.getClientEngineFromContext(context));
45+
}
46+
47+
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB_MR2)
48+
@Test
49+
public void testGetClientEngineFromContextAndroid() {
50+
Context context = mock(Context.class);
51+
UiModeManager uiModeManager = mock(UiModeManager.class);
52+
when(context.getSystemService(Context.UI_MODE_SERVICE)).thenReturn(uiModeManager);
53+
when(uiModeManager.getCurrentModeType()).thenReturn(Configuration.UI_MODE_TYPE_NORMAL);
54+
assertEquals(Event.ClientEngine.ANDROID_SDK, OptimizelyClientEngine.getClientEngineFromContext(context));
55+
}
56+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/****************************************************************************
2+
* Copyright 2017, Optimizely, Inc. and contributors *
3+
* *
4+
* Licensed under the Apache License, Version 2.0 (the "License"); *
5+
* you may not use this file except in compliance with the License. *
6+
* You may obtain a copy of the License at *
7+
* *
8+
* http://www.apache.org/licenses/LICENSE-2.0 *
9+
* *
10+
* Unless required by applicable law or agreed to in writing, software *
11+
* distributed under the License is distributed on an "AS IS" BASIS, *
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13+
* See the License for the specific language governing permissions and *
14+
* limitations under the License. *
15+
***************************************************************************/
16+
17+
package com.optimizely.ab.android.sdk;
18+
19+
import android.app.UiModeManager;
20+
import android.content.Context;
21+
import android.content.res.Configuration;
22+
import android.support.annotation.NonNull;
23+
import android.support.annotation.VisibleForTesting;
24+
25+
import com.optimizely.ab.event.internal.payload.Event;
26+
27+
public class OptimizelyClientEngine {
28+
29+
@VisibleForTesting
30+
public static Event.ClientEngine getClientEngineFromContext(@NonNull Context context) {
31+
UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
32+
33+
if (uiModeManager != null && uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
34+
return Event.ClientEngine.ANDROID_TV_SDK;
35+
}
36+
37+
return Event.ClientEngine.ANDROID_SDK;
38+
}
39+
40+
}

android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyManager.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,12 @@ protected void onPostExecute(UserProfile userProfile) {
380380
private OptimizelyClient buildOptimizely(@NonNull Context context, @NonNull String dataFile, @NonNull UserProfile userProfile) throws ConfigParseException {
381381
OptlyEventHandler eventHandler = OptlyEventHandler.getInstance(context);
382382
eventHandler.setDispatchInterval(eventHandlerDispatchInterval, eventHandlerDispatchIntervalTimeUnit);
383+
384+
Event.ClientEngine clientEngine = OptimizelyClientEngine.getClientEngineFromContext(context);
385+
383386
Optimizely optimizely = Optimizely.builder(dataFile, eventHandler)
384387
.withUserProfile(userProfile)
385-
.withClientEngine(Event.ClientEngine.ANDROID_SDK)
388+
.withClientEngine(clientEngine)
386389
.withClientVersion(BuildConfig.CLIENT_VERSION)
387390
.build();
388391
return new OptimizelyClient(optimizely, LoggerFactory.getLogger(OptimizelyClient.class));

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ ext {
5252
min_sdk_version = 10
5353
target_sdk_version = 24
5454

55-
java_core_ver = "1.3.0"
55+
java_core_ver = "1.5.0"
5656
android_logger_ver = "1.3.1"
5757
support_annotations_ver = "24.2.1"
5858
junit_ver = "4.12"

0 commit comments

Comments
 (0)