Skip to content

Commit 230737e

Browse files
author
Robert Henigan
authored
Merge pull request #1355 from smartdevicelink/ios_Alignment/LockScreen
iOS alignment/lock screen
2 parents 62c6933 + 38324c8 commit 230737e

7 files changed

Lines changed: 98 additions & 11 deletions

File tree

android/sdl_android/src/main/java/com/smartdevicelink/managers/lockscreen/SDLLockScreenActivity.java

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,14 @@
3838
import android.content.Intent;
3939
import android.content.IntentFilter;
4040
import android.graphics.Bitmap;
41+
import android.graphics.Color;
42+
import android.graphics.ColorFilter;
43+
import android.graphics.ColorMatrixColorFilter;
44+
import android.graphics.drawable.Drawable;
4145
import android.os.Bundle;
4246
import android.view.GestureDetector;
4347
import android.view.MotionEvent;
48+
import android.view.View;
4449
import android.view.Window;
4550
import android.widget.ImageView;
4651
import android.widget.RelativeLayout;
@@ -64,6 +69,9 @@ public class SDLLockScreenActivity extends Activity {
6469
private static final int MIN_SWIPE_DISTANCE = 200;
6570
private boolean mIsDismissible;
6671
private GestureDetector mGestureDetector;
72+
private int backgroundColor = Color.parseColor("#394e60");
73+
private boolean useWhiteIconAndTextColor;
74+
6775

6876
private final BroadcastReceiver lockScreenBroadcastReceiver = new BroadcastReceiver() {
6977
@Override
@@ -137,18 +145,20 @@ public void initializeActivity(Intent intent){
137145
int customIcon = intent.getIntExtra(LOCKSCREEN_ICON_EXTRA, 0);
138146
int customView = intent.getIntExtra(LOCKSCREEN_CUSTOM_VIEW_EXTRA, 0);
139147
Bitmap deviceIcon = intent.getParcelableExtra(LOCKSCREEN_DEVICE_LOGO_BITMAP);
148+
backgroundColor = (customColor != 0) ? customColor : backgroundColor;
140149

141150
if (customView != 0){
142151
setCustomView(customView);
143152
} else {
144153
setContentView(R.layout.activity_sdllock_screen);
154+
setBackgroundColor();
155+
useWhiteIconAndTextColor = shouldUseWhiteForegroundForBackgroundColor();
145156

146-
if (customColor != 0){
147-
changeBackgroundColor(customColor);
148-
}
149-
157+
// set Lock Screen Icon
150158
if (customIcon != 0){
151159
changeIcon(customIcon);
160+
} else {
161+
setSdlLogo();
152162
}
153163

154164
if (deviceLogoEnabled && deviceIcon != null){
@@ -158,19 +168,85 @@ public void initializeActivity(Intent intent){
158168
String warningMsg = intent.getStringExtra(KEY_LOCKSCREEN_WARNING_MSG);
159169
if (mIsDismissible) {
160170
setLockscreenWarningMessage(warningMsg);
171+
} else if (!useWhiteIconAndTextColor) {
172+
setTextColorBlack();
161173
}
162174
}
163175
}
164176
}
165177

166-
private void changeBackgroundColor(int customColor) {
178+
/**
179+
* Sets the lockScreen logo
180+
*/
181+
private void setSdlLogo() {
182+
ImageView lockScreen_iv = findViewById(R.id.lockscreen_image);
183+
Drawable sdlIcon = getResources().getDrawable(R.drawable.sdl_lockscreen_icon);
184+
// Checks color contrast and determines if the logo should be black or white
185+
if (useWhiteIconAndTextColor) {
186+
int color = Color.parseColor("#ffffff");
187+
188+
int red = (color & 0xFF0000) / 0xFFFF;
189+
int green = (color & 0xFF00) / 0xFF;
190+
int blue = color & 0xFF;
191+
192+
float[] matrix = {0, 0, 0, 0, red,
193+
0, 0, 0, 0, green,
194+
0, 0, 0, 0, blue,
195+
0, 0, 0, 1, 0};
196+
197+
ColorFilter colorFilter = new ColorMatrixColorFilter(matrix);
198+
sdlIcon.setColorFilter(colorFilter);
199+
}
200+
lockScreen_iv.setImageDrawable(sdlIcon);
201+
}
202+
203+
/**
204+
* Changes the text color to white on the lockScreen
205+
*/
206+
private void setTextColorBlack() {
207+
TextView tv = findViewById(R.id.lockscreen_text);
208+
tv.setTextColor(Color.parseColor("#000000"));
209+
}
210+
211+
/**
212+
* Calculates the contrast of the background to determine if the Icon and Text color
213+
* should be white or black
214+
* @return True if Background and Icon should be white, False if black
215+
*/
216+
private boolean shouldUseWhiteForegroundForBackgroundColor() {
217+
float r = Color.red(backgroundColor) / 255f;
218+
float b = Color.blue(backgroundColor) / 255f;
219+
float g = Color.green(backgroundColor) / 255f;
220+
221+
// http://stackoverflow.com/a/3943023
222+
r = (r <= 0.3928f) ? (r / 12.92f) : (float) Math.pow(((r + 0.055f) / 1.055f), 2.4f);
223+
g = (g <= 0.3928f) ? (g / 12.92f) : (float) Math.pow(((g + 0.055f) / 1.055f), 2.4f);
224+
b = (b <= 0.3928f) ? (b / 12.92f) : (float) Math.pow(((b + 0.055f) / 1.055f), 2.4f);
225+
226+
float luminescence = 0.2126f * r + 0.7152f * g + 0.0722f * b;
227+
return luminescence <= 0.179;
228+
}
229+
230+
/**
231+
* Sets the color of the background
232+
* Will use default color if not set in LockScreenConfig
233+
*/
234+
private void setBackgroundColor() {
167235
RelativeLayout layout = findViewById(R.id.lockscreen_relative_layout);
168-
layout.setBackgroundColor(getResources().getColor(customColor));
236+
layout.setBackgroundColor(backgroundColor);
169237
}
170238

239+
/**
240+
* Used to change LockScreen default Icon to customIcon set in LockScreenConfig
241+
* @param customIcon
242+
*/
171243
private void changeIcon(int customIcon) {
172-
ImageView lockscreen_iv = findViewById(R.id.lockscreen_image);
173-
lockscreen_iv.setBackgroundResource(customIcon);
244+
ImageView lockScreen_iv = findViewById(R.id.lockscreen_image);
245+
lockScreen_iv.setVisibility(View.GONE);
246+
247+
ImageView lockScreenCustom_iv = findViewById(R.id.appIcon);
248+
lockScreenCustom_iv.setVisibility(View.VISIBLE);
249+
lockScreenCustom_iv.setBackgroundResource(customIcon);
174250
}
175251

176252
private void setDeviceLogo(Bitmap deviceLogo) {
@@ -183,6 +259,9 @@ private void setDeviceLogo(Bitmap deviceLogo) {
183259
private void setLockscreenWarningMessage(String msg) {
184260
TextView tv = findViewById(R.id.lockscreen_text);
185261
if (tv != null) {
262+
if (!useWhiteIconAndTextColor) {
263+
tv.setTextColor(Color.parseColor("#000000"));
264+
}
186265
tv.setText(msg != null ? msg : getString(R.string.default_lockscreen_warning_message));
187266
}
188267
}
-39 Bytes
Loading
-330 Bytes
Loading
-280 Bytes
Loading
-492 Bytes
Loading
-506 Bytes
Loading

android/sdl_android/src/main/res/layout/activity_sdllock_screen.xml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
22
android:id="@+id/lockscreen_relative_layout"
33
android:layout_width="fill_parent"
4-
android:layout_height="fill_parent"
5-
android:background="#2c3d4d">
4+
android:layout_height="fill_parent">
65

76
<LinearLayout
87
android:id="@+id/lockscreen_linear_layout"
@@ -13,12 +12,21 @@
1312

1413
<ImageView
1514
android:id="@+id/lockscreen_image"
15+
android:layout_width="187dp"
16+
android:layout_height="78dp"
17+
android:scaleType="fitXY"
18+
android:layout_gravity="center_horizontal"
19+
android:contentDescription="@string/lockscreen_image_description"
20+
android:adjustViewBounds="true" />
21+
22+
<ImageView
23+
android:id="@+id/appIcon"
1624
android:layout_width="120dp"
1725
android:layout_height="120dp"
1826
android:scaleType="fitXY"
1927
android:layout_gravity="center_horizontal"
2028
android:contentDescription="@string/lockscreen_image_description"
21-
android:background="@drawable/sdl_lockscreen_icon"/>
29+
android:visibility="gone"/>
2230

2331
<ImageView
2432
android:id="@+id/device_image"

0 commit comments

Comments
 (0)