Color extraction on emergency dialer

Fixes: 37014702
Test: visual
Change-Id: I18636d22316c614cf43ccf435dca55f6a9a8d26c
diff --git a/Android.mk b/Android.mk
index 24fb423..f514ffc 100644
--- a/Android.mk
+++ b/Android.mk
@@ -20,6 +20,7 @@
         android-support-v7-preference \
         android-support-v7-recyclerview \
         android-support-v14-preference \
+        colorextraction \
         guava \
         volley
 
diff --git a/res/drawable/btn_emergency.xml b/res/drawable/btn_emergency.xml
new file mode 100644
index 0000000..6b22ddd
--- /dev/null
+++ b/res/drawable/btn_emergency.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2017 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<inset xmlns:android="http://schemas.android.com/apk/res/android"
+       android:insetTop="7dp"
+       android:insetRight="3dp"
+       android:insetBottom="7dp"
+       android:insetLeft="3dp">
+    <shape android:shape="rectangle">
+        <solid android:color="?attr/emergencyButtonBackgroundColor" />
+        <corners android:radius="2dp" />
+    </shape>
+</inset>
\ No newline at end of file
diff --git a/res/layout/emergency_dialer.xml b/res/layout/emergency_dialer.xml
index ee55c06..3b91b07 100644
--- a/res/layout/emergency_dialer.xml
+++ b/res/layout/emergency_dialer.xml
@@ -19,8 +19,8 @@
         android:id="@+id/top"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
-        android:paddingLeft="16dp"
-        android:paddingRight="16dp">
+        android:paddingLeft="50dp"
+        android:paddingRight="50dp">
 
     <LinearLayout
             android:layout_width="match_parent"
@@ -36,26 +36,29 @@
                 android:layout_marginTop="16dp"
                 android:layout_marginBottom="24dp">
 
+            <!-- Button that says: Emergency Information -->
             <LinearLayout
                     android:layout_width="match_parent"
                     android:layout_height="match_parent">
                 <Button android:layout_width="0dp"
                         android:layout_height="match_parent"
                         android:layout_weight="1"
-                        android:id="@+id/action1"
-                        />
+                        android:background="@drawable/btn_emergency"
+                        android:id="@+id/action1" />
                 <Button android:layout_width="0dp"
                         android:layout_height="match_parent"
                         android:layout_weight="1"
-                        android:id="@+id/action2"
-                        />
+                        android:background="@drawable/btn_emergency"
+                        android:id="@+id/action2" />
                 <Button android:layout_width="0dp"
                         android:layout_height="match_parent"
                         android:layout_weight="1"
-                        android:id="@+id/action3"
-                        />
+                        android:background="@drawable/btn_emergency"
+                        android:id="@+id/action3" />
             </LinearLayout>
 
+            <!-- View that shows up on top of "emergency information" button
+            and asks you to tap again to confirm the action -->
             <FrameLayout
                     android:layout_width="match_parent"
                     android:layout_height="match_parent"
@@ -76,7 +79,7 @@
                         android:layout_width="match_parent"
                         android:layout_height="match_parent"
                         android:id="@+id/ripple_view"
-                        android:backgroundTint="#22000000"
+                        android:backgroundTint="#22ffffff"
                         android:visibility="invisible"
                         android:focusable="false"
                         android:clickable="false"
@@ -94,12 +97,14 @@
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content"
                             android:id="@+id/selected_label"
+                            android:textColor="@android:color/white"
                             android:textAppearance="?android:attr/textAppearanceButton" />
                     <TextView
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content"
                             android:id="@+id/launch_hint"
                             android:text="@string/emergency_action_launch_hint"
+                            android:textColor="@android:color/white"
                             android:textStyle="italic" />
                 </LinearLayout>
 
@@ -108,7 +113,7 @@
         </com.android.phone.EmergencyActionGroup>
 
         <include layout="@layout/dialpad_view_unthemed"
-                android:theme="@style/Dialpad_Dark" />
+                android:theme="?attr/dialpadTheme" />
 
     </LinearLayout>
 
diff --git a/res/values/attrs.xml b/res/values/attrs.xml
index 30da2f7..61c5f97 100644
--- a/res/values/attrs.xml
+++ b/res/values/attrs.xml
@@ -51,4 +51,6 @@
     </declare-styleable>
 
     <attr name="preferenceBackgroundColor" format="color" />
+    <attr name="emergencyButtonBackgroundColor" format="color" />
+    <attr name="dialpadTheme" format="reference" />
 </resources>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 057352d..501aeb5 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -247,10 +247,19 @@
 
     <style name="EmergencyDialerTheme" parent="@*android:style/Theme.DeviceDefault.Settings.Dark.NoActionBar">
         <item name="android:colorPrimaryDark">?android:attr/colorPrimary</item>
-        <item name="android:windowBackground">?android:attr/colorPrimary</item>
+        <item name="android:colorBackgroundCacheHint">@null</item>
+        <item name="android:windowShowWallpaper">true</item>
         <item name="android:statusBarColor">@android:color/transparent</item>
         <item name="android:navigationBarColor">@android:color/transparent</item>
         <item name="android:homeAsUpIndicator">@drawable/ic_back_arrow</item>
+        <item name="emergencyButtonBackgroundColor">#3cffffff</item>
+        <item name="dialpadTheme">@style/Dialpad_DarkTransparent</item>
+    </style>
+
+    <style name="EmergencyDialerThemeDark" parent="@style/EmergencyDialerTheme">
+        <item name="android:textColor">?android:attr/textColorPrimaryInverse</item>
+        <item name="emergencyButtonBackgroundColor">#19414549</item>
+        <item name="dialpadTheme">@style/Dialpad_LightTransparent</item>
     </style>
 
     <style name="SimImportTheme" parent="@android:style/Theme.Material.Light">
diff --git a/src/com/android/phone/EmergencyDialer.java b/src/com/android/phone/EmergencyDialer.java
index 53f6f7e..88f6039 100644
--- a/src/com/android/phone/EmergencyDialer.java
+++ b/src/com/android/phone/EmergencyDialer.java
@@ -20,11 +20,15 @@
 import android.app.AlertDialog;
 import android.app.Dialog;
 import android.app.StatusBarManager;
+import android.app.WallpaperManager;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.res.Resources;
+import android.graphics.Color;
+import android.graphics.Point;
+import android.graphics.drawable.ColorDrawable;
 import android.media.AudioManager;
 import android.media.ToneGenerator;
 import android.net.Uri;
@@ -51,10 +55,14 @@
 import android.view.View;
 import android.view.WindowManager;
 import android.widget.EditText;
+import android.widget.FrameLayout;
 
 import com.android.phone.common.dialpad.DialpadKeyButton;
 import com.android.phone.common.util.ViewUtil;
 
+import com.google.android.colorextraction.ColorExtractor;
+import com.google.android.colorextraction.ColorExtractor.GradientColors;
+import com.google.android.colorextraction.drawable.GradientDrawable;
 
 /**
  * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
@@ -75,7 +83,7 @@
  */
 public class EmergencyDialer extends Activity implements View.OnClickListener,
         View.OnLongClickListener, View.OnKeyListener, TextWatcher,
-        DialpadKeyButton.OnPressedListener {
+        DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener {
     // Keys used with onSaveInstanceState().
     private static final String LAST_NUMBER = "lastNumber";
 
@@ -106,6 +114,9 @@
 
     private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
 
+    /** 90% opacity, different from other gradients **/
+    private static final int BACKGROUND_GRADIENT_ALPHA = 230;
+
     EditText mDigits;
     private View mDialButton;
     private View mDelete;
@@ -130,6 +141,11 @@
 
     private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
 
+    // Background gradient
+    private ColorExtractor mColorExtractor;
+    private GradientDrawable mBackgroundGradient;
+    private boolean mSupportsDarkText;
+
     @Override
     public void beforeTextChanged(CharSequence s, int start, int count, int after) {
         // Do nothing
@@ -176,6 +192,10 @@
 
         getWindow().setAttributes(lp);
 
+        mColorExtractor = new ColorExtractor(this);
+        GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK);
+        updateTheme(lockScreenColors.supportsDarkText());
+
         setContentView(R.layout.emergency_dialer);
 
         mDigits = (EditText) findViewById(R.id.digits);
@@ -186,6 +206,14 @@
         mDigits.setInputType(InputType.TYPE_NULL);
         maybeAddNumberFormatting();
 
+        mBackgroundGradient = new GradientDrawable(this);
+        Point displaySize = new Point();
+        ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
+                .getDefaultDisplay().getSize(displaySize);
+        mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
+        mBackgroundGradient.setAlpha(BACKGROUND_GRADIENT_ALPHA);
+        getWindow().setBackgroundDrawable(mBackgroundGradient);
+
         // Check for the presence of the keypad
         View view = findViewById(R.id.one);
         if (view != null) {
@@ -462,6 +490,17 @@
     }
 
     @Override
+    protected void onStart() {
+        super.onStart();
+
+        mColorExtractor.addOnColorsChangedListener(this);
+        GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK);
+        // Do not animate when view isn't visible yet, just set an initial state.
+        mBackgroundGradient.setColors(lockScreenColors, false);
+        updateTheme(lockScreenColors.supportsDarkText());
+    }
+
+    @Override
     protected void onResume() {
         super.onResume();
 
@@ -508,6 +547,43 @@
         }
     }
 
+    @Override
+    protected void onStop() {
+        super.onStop();
+
+        mColorExtractor.removeOnColorsChangedListener(this);
+    }
+
+    /**
+     * Sets theme based on gradient colors
+     * @param supportsDarkText true if gradient supports dark text
+     */
+    private void updateTheme(boolean supportsDarkText) {
+        if (mSupportsDarkText == supportsDarkText) {
+            return;
+        }
+        mSupportsDarkText = supportsDarkText;
+
+        // We can't change themes after inflation, in this case we'll have to recreate
+        // the whole activity.
+        if (mBackgroundGradient != null) {
+            recreate();
+            return;
+        }
+
+        int vis = getWindow().getDecorView().getSystemUiVisibility();
+        if (supportsDarkText) {
+            vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
+            vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
+            setTheme(R.style.EmergencyDialerThemeDark);
+        } else {
+            vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
+            vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
+            setTheme(R.style.EmergencyDialerTheme);
+        }
+        getWindow().getDecorView().setSystemUiVisibility(vis);
+    }
+
     /**
      * place the call, but check to make sure it is a viable number.
      */
@@ -657,4 +733,12 @@
         }
         PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
     }
+
+    @Override
+    public void onColorsChanged(GradientColors colors, int which) {
+        if ((which & WallpaperManager.FLAG_LOCK) != 0) {
+            mBackgroundGradient.setColors(colors);
+            updateTheme(colors.supportsDarkText());
+        }
+    }
 }