Implement emergency dialer actions

Also updates visuals.

Bug: 19841919
Change-Id: If2e808d2ce7854db370c440c6c2638cc39bf4645
diff --git a/res/layout/emergency_dialer.xml b/res/layout/emergency_dialer.xml
index 5c721c5..a68f5de 100644
--- a/res/layout/emergency_dialer.xml
+++ b/res/layout/emergency_dialer.xml
@@ -16,10 +16,101 @@
 
 <!-- Layout for the emergency dialer; see EmergencyDialer.java. -->
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:id="@+id/top"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent" >
-    <include layout="@layout/dialpad_view" />
+        android:id="@+id/top"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:paddingLeft="16dp"
+        android:paddingRight="16dp">
+
+    <LinearLayout
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:layout_gravity="bottom"
+            android:orientation="vertical">
+
+        <!-- FrameLayout -->
+        <com.android.phone.EmergencyActionGroup
+                android:layout_height="64dp"
+                android:layout_width="match_parent"
+                android:layout_marginTop="16dp"
+                android:layout_marginBottom="24dp">
+
+            <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"
+                        />
+                <Button android:layout_width="0dp"
+                        android:layout_height="match_parent"
+                        android:layout_weight="1"
+                        android:id="@+id/action2"
+                        />
+                <Button android:layout_width="0dp"
+                        android:layout_height="match_parent"
+                        android:layout_weight="1"
+                        android:id="@+id/action3"
+                        />
+            </LinearLayout>
+
+            <FrameLayout
+                    android:layout_width="match_parent"
+                    android:layout_height="match_parent"
+                    android:id="@+id/selected_container"
+                    android:visibility="invisible"
+                    android:focusable="true"
+                    android:clickable="true">
+
+                <View
+                        android:layout_width="match_parent"
+                        android:layout_height="match_parent"
+                        android:backgroundTint="#ffe53935"
+                        android:focusable="false"
+                        android:clickable="false"
+                        style="?android:attr/buttonStyle"/>
+
+                <View
+                        android:layout_width="match_parent"
+                        android:layout_height="match_parent"
+                        android:id="@+id/ripple_view"
+                        android:backgroundTint="#22000000"
+                        android:visibility="invisible"
+                        android:focusable="false"
+                        android:clickable="false"
+                        style="?android:attr/buttonStyle"/>
+
+                <LinearLayout
+                        android:layout_width="match_parent"
+                        android:layout_height="match_parent"
+                        android:orientation="vertical"
+                        android:focusable="false"
+                        android:clickable="false"
+                        android:backgroundTint="#00000000"
+                        style="?android:attr/buttonStyle">
+                    <TextView
+                            android:layout_width="wrap_content"
+                            android:layout_height="wrap_content"
+                            android:id="@+id/selected_label"
+                            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:textStyle="italic" />
+                </LinearLayout>
+
+            </FrameLayout>
+
+        </com.android.phone.EmergencyActionGroup>
+
+        <include layout="@layout/dialpad_view_unthemed"
+                android:theme="@style/Dialpad_Dark" />
+
+    </LinearLayout>
+
     <FrameLayout
         android:id="@+id/floating_action_button_container"
         android:layout_width="@dimen/floating_action_button_width"
@@ -35,4 +126,5 @@
             android:contentDescription="@string/description_dial_button"
             android:src="@drawable/fab_ic_call"/>
     </FrameLayout>
+
 </FrameLayout>
diff --git a/res/layout/otacall_card.xml b/res/layout/otacall_card.xml
index 74b31e8..fea2077 100644
--- a/res/layout/otacall_card.xml
+++ b/res/layout/otacall_card.xml
@@ -115,7 +115,8 @@
                 android:layout_height="wrap_content"
                 android:orientation="vertical"
                 android:layout_marginTop="1dip"
-                android:visibility="gone" >
+                android:visibility="gone"
+                android:theme="@style/Dialpad_Light">
 
             <!-- Note there's no "dtmfDialerField" EditText here;
                  in the OTA UI there's no visible "digits" display
diff --git a/res/values/colors.xml b/res/values/colors.xml
index 45b984e..67572ed 100644
--- a/res/values/colors.xml
+++ b/res/values/colors.xml
@@ -58,4 +58,5 @@
     <color name="network_operators_color_primary">#ff263238</color>
     <color name="network_operators_color_primary_dark">#ff21272b</color>
 
+    <color name="emergency_dialer_background">#ff263238</color>
 </resources>
diff --git a/res/values/ids.xml b/res/values/ids.xml
new file mode 100644
index 0000000..82a6add
--- /dev/null
+++ b/res/values/ids.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2015 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
+  -->
+
+<resources>
+    <item type="id" name="tag_intent" />
+</resources>
\ No newline at end of file
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 22732ef..4aa3922 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1292,4 +1292,7 @@
     <string name="tty_mode_key">button_tty_mode_key</string>
     <!-- DO NOT TRANSLATE. Internal key for a voicemail notification preference. -->
     <string name="wifi_calling_settings_key">button_wifi_calling_settings_key</string>
+
+    <!-- Hint appearing below a selected action on the emergency dialer telling user to tap again to execute the action [CHAR LIMIT=NONE] -->
+    <string name="emergency_action_launch_hint">Touch again to open</string>
 </resources>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 3d1b1ad..3c192cc 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -256,11 +256,12 @@
         <item name="android:src">@drawable/overflow_menu</item>
     </style>
 
-    <style name="EmergencyDialerTheme" parent="@android:style/Theme.Material.Light">
-        <item name="dialpad_key_button_touch_tint">@color/dialer_dialpad_touch_tint</item>
-        <item name="android:actionBarStyle">@style/TelephonyActionBarStyle</item>
-        <item name="android:colorPrimary">@color/dialer_theme_color</item>
-        <item name="android:colorPrimaryDark">@color/dialer_theme_color_dark</item>
+    <style name="EmergencyDialerTheme" parent="@android:style/Theme.Material.NoActionBar">
+        <item name="android:colorPrimary">@color/emergency_dialer_background</item>
+        <item name="android:colorPrimaryDark">@color/emergency_dialer_background</item>
+        <item name="android:windowBackground">@color/emergency_dialer_background</item>
+        <item name="android:statusBarColor">@android:color/transparent</item>
+        <item name="android:navigationBarColor">@android:color/transparent</item>
     </style>
 
     <style name="SimImportTheme" parent="@android:style/Theme.Material.Light">
diff --git a/src/com/android/phone/EmergencyActionGroup.java b/src/com/android/phone/EmergencyActionGroup.java
new file mode 100644
index 0000000..96adc5d
--- /dev/null
+++ b/src/com/android/phone/EmergencyActionGroup.java
@@ -0,0 +1,290 @@
+/*
+ * Copyright (C) 2015 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
+ */
+
+package com.android.phone;
+
+import android.animation.Animator;
+import android.animation.AnimatorListenerAdapter;
+import android.annotation.Nullable;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.provider.Settings;
+import android.telephony.TelephonyManager;
+import android.text.TextUtils;
+import android.util.AttributeSet;
+import android.view.View;
+import android.view.ViewAnimationUtils;
+import android.view.ViewGroup;
+import android.view.animation.AnimationUtils;
+import android.view.animation.Interpolator;
+import android.widget.Button;
+import android.widget.FrameLayout;
+import android.widget.TextView;
+
+import java.util.List;
+
+public class EmergencyActionGroup extends FrameLayout implements View.OnClickListener {
+
+    private static final long HIDE_DELAY = 3000;
+    private static final int RIPPLE_DURATION = 600;
+    private static final long RIPPLE_PAUSE = 1000;
+
+    private final Interpolator mFastOutLinearInInterpolator;
+
+    private ViewGroup mSelectedContainer;
+    private TextView mSelectedLabel;
+    private View mRippleView;
+    private View mLaunchHint;
+
+    private View mLastRevealed;
+
+    public EmergencyActionGroup(Context context, @Nullable AttributeSet attrs) {
+        super(context, attrs);
+        mFastOutLinearInInterpolator = AnimationUtils.loadInterpolator(context,
+                android.R.interpolator.fast_out_linear_in);
+    }
+
+    @Override
+    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+    }
+
+    @Override
+    protected void onFinishInflate() {
+        super.onFinishInflate();
+
+        setupAssistActions();
+
+        mSelectedContainer = (ViewGroup) findViewById(R.id.selected_container);
+        mSelectedContainer.setOnClickListener(this);
+        mSelectedLabel = (TextView) findViewById(R.id.selected_label);
+        mRippleView = findViewById(R.id.ripple_view);
+        mLaunchHint = findViewById(R.id.launch_hint);
+    }
+
+    private void setupAssistActions() {
+        int[] buttonIds = new int[] {R.id.action1, R.id.action2, R.id.action3};
+
+        List<ResolveInfo> infos = resolveAssistPackageAndQueryActivites();
+
+        for (int i = 0; i < 3; i++) {
+            Button button = (Button) findViewById(buttonIds[i]);
+            boolean visible = false;
+
+            button.setOnClickListener(this);
+
+            if (infos != null && infos.size() > i && infos.get(i) != null) {
+                ResolveInfo info = infos.get(i);
+                ComponentName name = getComponentName(info);
+
+                button.setTag(R.id.tag_intent,
+                        new Intent(TelephonyManager.ACTION_EMERGENCY_ASSISTANCE)
+                                .setComponent(name));
+                button.setText(info.loadLabel(getContext().getPackageManager()));
+                visible = true;
+            }
+
+            button.setVisibility(visible ? View.VISIBLE : View.GONE);
+        }
+    }
+
+    private List<ResolveInfo> resolveAssistPackageAndQueryActivites() {
+        List<ResolveInfo> infos = queryAssistActivities();
+
+        if (infos == null || infos.isEmpty()) {
+            PackageManager packageManager = getContext().getPackageManager();
+            Intent queryIntent = new Intent(TelephonyManager.ACTION_EMERGENCY_ASSISTANCE);
+            infos = packageManager.queryIntentActivities(queryIntent, 0);
+
+            PackageInfo bestMatch = null;
+            for (int i = 0; i < infos.size(); i++) {
+                if (infos.get(i).activityInfo == null) continue;
+                String packageName = infos.get(i).activityInfo.packageName;
+                PackageInfo packageInfo;
+                try {
+                    packageInfo = packageManager.getPackageInfo(packageName, 0);
+                } catch (PackageManager.NameNotFoundException e) {
+                    continue;
+                }
+                // Get earliest installed app, but prioritize system apps.
+                if (bestMatch == null
+                        || !isSystemApp(bestMatch) && isSystemApp(packageInfo)
+                        || isSystemApp(bestMatch) == isSystemApp(packageInfo)
+                                && bestMatch.firstInstallTime > packageInfo.firstInstallTime) {
+                    bestMatch = packageInfo;
+                }
+            }
+
+            if (bestMatch != null) {
+                Settings.Secure.putString(getContext().getContentResolver(),
+                        Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION,
+                        bestMatch.packageName);
+                return queryAssistActivities();
+            } else {
+                return null;
+            }
+        } else {
+            return infos;
+        }
+    }
+
+    private List<ResolveInfo> queryAssistActivities() {
+        String assistPackage = Settings.Secure.getString(
+                getContext().getContentResolver(),
+                Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION);
+        List<ResolveInfo> infos = null;
+
+        if (!TextUtils.isEmpty(assistPackage)) {
+            Intent queryIntent = new Intent(TelephonyManager.ACTION_EMERGENCY_ASSISTANCE)
+                    .setPackage(assistPackage);
+            infos = getContext().getPackageManager().queryIntentActivities(queryIntent, 0);
+        }
+        return infos;
+    }
+
+    private boolean isSystemApp(PackageInfo info) {
+        return info.applicationInfo != null
+                && (info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
+    }
+
+    private ComponentName getComponentName(ResolveInfo resolveInfo) {
+        if (resolveInfo == null || resolveInfo.activityInfo == null) return null;
+        return new ComponentName(resolveInfo.activityInfo.packageName,
+                resolveInfo.activityInfo.name);
+    }
+
+    @Override
+    public void onClick(View v) {
+        switch (v.getId()) {
+            case R.id.action1:
+            case R.id.action2:
+            case R.id.action3:
+                revealTheButton(v);
+                break;
+            case R.id.selected_container:
+                getContext().startActivity((Intent) v.getTag(R.id.tag_intent));
+                break;
+        }
+    }
+
+    private void revealTheButton(View v) {
+        mSelectedContainer.setVisibility(VISIBLE);
+        int centerX = v.getLeft() + v.getWidth() / 2;
+        int centerY = v.getTop() + v.getHeight() / 2;
+        Animator reveal = ViewAnimationUtils.createCircularReveal(
+                mSelectedContainer,
+                centerX,
+                centerY,
+                0,
+                Math.max(centerX, mSelectedContainer.getWidth() - centerX)
+                        + Math.max(centerY, mSelectedContainer.getHeight() - centerY));
+        reveal.start();
+
+        animateHintText(mSelectedLabel, v, reveal);
+        animateHintText(mLaunchHint, v, reveal);
+
+        mSelectedLabel.setText(((Button) v).getText());
+        mSelectedContainer.setTag(R.id.tag_intent, v.getTag(R.id.tag_intent));
+        mLastRevealed = v;
+        mSelectedContainer.postDelayed(mHideRunnable, HIDE_DELAY);
+        mSelectedContainer.postDelayed(mRippleRunnable, RIPPLE_PAUSE / 2);
+    }
+
+    private void animateHintText(View selectedView, View v, Animator reveal) {
+        selectedView.setTranslationX(
+                (v.getLeft() + v.getWidth() / 2 - mSelectedContainer.getWidth() / 2) / 5);
+        selectedView.animate()
+                .setDuration(reveal.getDuration() / 3)
+                .setStartDelay(reveal.getDuration() / 5)
+                .translationX(0)
+                .setInterpolator(mFastOutLinearInInterpolator)
+                .start();
+    }
+
+    private void hideTheButton() {
+        View v = mLastRevealed;
+        int centerX = v.getLeft() + v.getWidth() / 2;
+        int centerY = v.getTop() + v.getHeight() / 2;
+        Animator reveal = ViewAnimationUtils.createCircularReveal(
+                mSelectedContainer,
+                centerX,
+                centerY,
+                Math.max(centerX, mSelectedContainer.getWidth() - centerX)
+                        + Math.max(centerY, mSelectedContainer.getHeight() - centerY),
+                0);
+        reveal.addListener(new AnimatorListenerAdapter() {
+            @Override
+            public void onAnimationEnd(Animator animation) {
+                mSelectedContainer.setVisibility(INVISIBLE);
+                mSelectedContainer.removeCallbacks(mRippleRunnable);
+            }
+        });
+        reveal.start();
+    }
+
+    private void startRipple() {
+        final View ripple = mRippleView;
+        ripple.animate().cancel();
+        ripple.setVisibility(VISIBLE);
+        Animator reveal = ViewAnimationUtils.createCircularReveal(
+                ripple,
+                ripple.getLeft() + ripple.getWidth() / 2,
+                ripple.getTop() + ripple.getHeight() / 2,
+                0,
+                ripple.getWidth() / 2);
+        reveal.setDuration(RIPPLE_DURATION);
+        reveal.start();
+
+        ripple.setAlpha(0);
+        ripple.animate().alpha(1).setDuration(RIPPLE_DURATION / 2)
+                .withEndAction(new Runnable() {
+            @Override
+            public void run() {
+                ripple.animate().alpha(0).setDuration(RIPPLE_DURATION / 2)
+                        .withEndAction(new Runnable() {
+                            @Override
+                            public void run() {
+                                ripple.setVisibility(INVISIBLE);
+                                postDelayed(mRippleRunnable, RIPPLE_PAUSE);
+                            }
+                        }).start();
+            }
+        }).start();
+    }
+
+    private final Runnable mHideRunnable = new Runnable() {
+        @Override
+        public void run() {
+            if (!isAttachedToWindow()) return;
+            hideTheButton();
+        }
+    };
+
+    private final Runnable mRippleRunnable = new Runnable() {
+        @Override
+        public void run() {
+            if (!isAttachedToWindow()) return;
+            startRipple();
+        }
+    };
+
+
+}