Color extraction on emergency dialer

Fixes: 37014702
Test: visual
Change-Id: I18636d22316c614cf43ccf435dca55f6a9a8d26c
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());
+        }
+    }
 }