3838import android .content .Intent ;
3939import android .content .IntentFilter ;
4040import android .graphics .Bitmap ;
41+ import android .graphics .Color ;
42+ import android .graphics .ColorFilter ;
43+ import android .graphics .ColorMatrixColorFilter ;
44+ import android .graphics .drawable .Drawable ;
4145import android .os .Bundle ;
4246import android .view .GestureDetector ;
4347import android .view .MotionEvent ;
48+ import android .view .View ;
4449import android .view .Window ;
4550import android .widget .ImageView ;
4651import 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 }
0 commit comments