Adopt Toolbar for overflow menu

Change-Id: I55958260b4279b504e79b7a104a813a1273ae403
diff --git a/res/drawable-hdpi/ic_more_vert_grey600_24dp.png b/res/drawable-hdpi/ic_more_vert_grey600_24dp.png
deleted file mode 100644
index e141502..0000000
--- a/res/drawable-hdpi/ic_more_vert_grey600_24dp.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-mdpi/ic_more_vert_grey600_24dp.png b/res/drawable-mdpi/ic_more_vert_grey600_24dp.png
deleted file mode 100644
index 4ed3435..0000000
--- a/res/drawable-mdpi/ic_more_vert_grey600_24dp.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-xhdpi/ic_more_vert_grey600_24dp.png b/res/drawable-xhdpi/ic_more_vert_grey600_24dp.png
deleted file mode 100644
index 7bc63a5..0000000
--- a/res/drawable-xhdpi/ic_more_vert_grey600_24dp.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-xxhdpi/ic_more_vert_grey600_24dp.png b/res/drawable-xxhdpi/ic_more_vert_grey600_24dp.png
deleted file mode 100644
index 44012b8..0000000
--- a/res/drawable-xxhdpi/ic_more_vert_grey600_24dp.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-xxxhdpi/ic_more_vert_grey600_24dp.png b/res/drawable-xxxhdpi/ic_more_vert_grey600_24dp.png
deleted file mode 100644
index 0042578..0000000
--- a/res/drawable-xxxhdpi/ic_more_vert_grey600_24dp.png
+++ /dev/null
Binary files differ
diff --git a/res/layout/activity_calculator_land.xml b/res/layout/activity_calculator_land.xml
index 9182598..1fe12db 100644
--- a/res/layout/activity_calculator_land.xml
+++ b/res/layout/activity_calculator_land.xml
@@ -21,12 +21,6 @@
     android:layout_height="match_parent"
     android:orientation="vertical">
 
-   <!-- Unclear we actually have the vertical space to do this.  Revisit! -->
-   <include
-        layout="@layout/extras"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content" />
-
     <include
         layout="@layout/display"
         android:layout_width="match_parent"
diff --git a/res/layout/activity_calculator_port.xml b/res/layout/activity_calculator_port.xml
index 3b4351a..0cb5dc7 100644
--- a/res/layout/activity_calculator_port.xml
+++ b/res/layout/activity_calculator_port.xml
@@ -22,11 +22,6 @@
     android:orientation="vertical">
 
     <include
-        layout="@layout/extras"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content" />
-
-    <include
         layout="@layout/display"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" />
diff --git a/res/layout/activity_calculator_tablet_port.xml b/res/layout/activity_calculator_tablet_port.xml
index 1261659..cf4b146 100644
--- a/res/layout/activity_calculator_tablet_port.xml
+++ b/res/layout/activity_calculator_tablet_port.xml
@@ -22,11 +22,6 @@
     android:orientation="vertical">
 
     <include
-        layout="@layout/extras"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content" />
-
-    <include
         layout="@layout/display"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" />
diff --git a/res/layout/display.xml b/res/layout/display.xml
index b5a5b60..4bcecf6 100644
--- a/res/layout/display.xml
+++ b/res/layout/display.xml
@@ -23,11 +23,27 @@
     android:background="@color/display_background_color"
     android:elevation="4dip">
 
+    <Toolbar
+        android:id="@+id/toolbar"
+        android:layout_width="match_parent"
+        android:layout_height="?android:attr/actionBarSize">
+
+       <TextView
+            android:id="@+id/deg_rad"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:fontFamily="sans-serif-medium"
+            android:textSize="16sp"
+            android:textAllCaps="true" />
+
+    </Toolbar>
+
     <com.android.calculator2.CalculatorEditText
         android:id="@+id/formula"
         style="@style/DisplayEditTextStyle.Formula"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
+        android:layout_below="@id/toolbar"
         android:inputType="text|textNoSuggestions"
         android:textIsSelectable="false"
         android:textColor="@color/display_formula_text_color" />
@@ -36,17 +52,12 @@
       We lay the result out to full width, but are careful to use only
       2/3 of the space, so that we have room when we expand.
       -->
-
     <com.android.calculator2.CalculatorResult
         android:id="@+id/result"
         style="@style/DisplayTextStyle.Result"
-        android:layout_alignParentRight="true"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_below="@id/formula"
-        android:inputType="none"
-        android:clickable="true"
-        android:textIsSelectable="false"
         android:textColor="@color/display_result_text_color" />
 
 </RelativeLayout>
diff --git a/res/layout/extras.xml b/res/layout/extras.xml
deleted file mode 100644
index 713413c..0000000
--- a/res/layout/extras.xml
+++ /dev/null
@@ -1,54 +0,0 @@
-<?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.
-
-  -->
-
-<!--
-  TODO: Use framework Toolbar instead of custom overflow menu.
-  Together with setActionBar, that should also fix the COPY/PASTE
-  ugliness.
-  It is not immediately obvious how to get the layout inside the
-  Toolbar correct.
-  -->
-
-<LinearLayout
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    android:id="@+id/toolbar"
-    android:layout_width="match_parent"
-    android:layout_height="wrap_content"
-    android:orientation="horizontal"
-    android:background="@color/display_background_color"
-    android:elevation="4dip">
-
-   <TextView
-        android:id="@+id/deg_rad"
-        android:layout_width="0dip"
-        android:layout_height="wrap_content"
-        android:layout_weight="1"
-        android:textSize="20sp"
-        android:textAllCaps="true"
-        android:background="@color/display_background_color" />
-
-   <ImageButton
-        android:id="@+id/overflow_menu"
-        android:layout_width="48dip"
-        android:layout_height="wrap_content"
-        android:src="@drawable/ic_more_vert_grey600_24dp"
-        android:background="?android:attr/selectableItemBackgroundBorderless"
-        android:contentDescription="@string/overflow_menu_description"
-        android:onClick="onButtonClick" />
-
-</LinearLayout>
diff --git a/res/values-land/styles.xml b/res/values-land/styles.xml
index cc8e64c..074796a 100644
--- a/res/values-land/styles.xml
+++ b/res/values-land/styles.xml
@@ -19,18 +19,18 @@
 <resources xmlns:android="http://schemas.android.com/apk/res/android">
 
     <style name="DisplayEditTextStyle.Formula">
-        <item name="android:paddingTop">24dip</item>
-        <item name="android:paddingBottom">8dip</item>
-        <item name="android:paddingStart">16dip</item>
-        <item name="android:paddingEnd">16dip</item>
+        <item name="android:paddingTop">0dip</item>
+        <item name="android:paddingBottom">6dip</item>
+        <item name="android:paddingStart">32dip</item>
+        <item name="android:paddingEnd">32dip</item>
         <item name="android:textSize">30sp</item>
     </style>
 
     <style name="DisplayTextStyle.Result">
-        <item name="android:paddingTop">8dip</item>
-        <item name="android:paddingBottom">24dip</item>
-        <item name="android:paddingStart">16dip</item>
-        <item name="android:paddingEnd">16dip</item>
+        <item name="android:paddingTop">6dip</item>
+        <item name="android:paddingBottom">12dip</item>
+        <item name="android:paddingStart">32dip</item>
+        <item name="android:paddingEnd">32dip</item>
         <item name="android:textSize">28sp</item>
     </style>
 
diff --git a/res/values-port/styles.xml b/res/values-port/styles.xml
index 348333f..a067b48 100644
--- a/res/values-port/styles.xml
+++ b/res/values-port/styles.xml
@@ -19,8 +19,8 @@
 <resources xmlns:android="http://schemas.android.com/apk/res/android">
 
     <style name="DisplayEditTextStyle.Formula">
-        <item name="android:paddingTop">48dip</item>
-        <item name="android:paddingBottom">24dip</item>
+        <item name="android:paddingTop">8dip</item>
+        <item name="android:paddingBottom">16dip</item>
         <item name="android:paddingStart">16dip</item>
         <item name="android:paddingEnd">16dip</item>
         <item name="minTextSize">36sp</item>
@@ -29,8 +29,8 @@
     </style>
 
     <style name="DisplayTextStyle.Result">
-        <item name="android:paddingTop">24dip</item>
-        <item name="android:paddingBottom">48dip</item>
+        <item name="android:paddingTop">16dip</item>
+        <item name="android:paddingBottom">32dip</item>
         <item name="android:paddingStart">16dip</item>
         <item name="android:paddingEnd">16dip</item>
         <item name="android:textSize">28sp</item>
diff --git a/res/values-sw600dp-land/styles.xml b/res/values-sw600dp-land/styles.xml
index 21bdd90..18774f2 100644
--- a/res/values-sw600dp-land/styles.xml
+++ b/res/values-sw600dp-land/styles.xml
@@ -19,7 +19,7 @@
 <resources xmlns:android="http://schemas.android.com/apk/res/android">
 
     <style name="DisplayEditTextStyle.Formula">
-        <item name="android:paddingTop">46dip</item>
+        <item name="android:paddingTop">0dip</item>
         <item name="android:paddingBottom">26dip</item>
         <item name="android:paddingStart">44dip</item>
         <item name="android:paddingEnd">44dip</item>
diff --git a/res/values-sw600dp-port/styles.xml b/res/values-sw600dp-port/styles.xml
index 251c734..b5f1569 100644
--- a/res/values-sw600dp-port/styles.xml
+++ b/res/values-sw600dp-port/styles.xml
@@ -20,7 +20,7 @@
 <resources xmlns:android="http://schemas.android.com/apk/res/android">
 
     <style name="DisplayEditTextStyle.Formula">
-        <item name="android:paddingTop">72dip</item>
+        <item name="android:paddingTop">16dip</item>
         <item name="android:paddingBottom">32dip</item>
         <item name="android:paddingStart">44dip</item>
         <item name="android:paddingEnd">44dip</item>
diff --git a/res/values-sw800dp-land/styles.xml b/res/values-sw800dp-land/styles.xml
index c88136d..a5bfdd3 100644
--- a/res/values-sw800dp-land/styles.xml
+++ b/res/values-sw800dp-land/styles.xml
@@ -19,7 +19,7 @@
 <resources xmlns:android="http://schemas.android.com/apk/res/android">
 
     <style name="DisplayEditTextStyle.Formula">
-        <item name="android:paddingTop">72dip</item>
+        <item name="android:paddingTop">16dip</item>
         <item name="android:paddingBottom">26dip</item>
         <item name="android:paddingStart">44dip</item>
         <item name="android:paddingEnd">44dip</item>
diff --git a/res/values-sw800dp-port/styles.xml b/res/values-sw800dp-port/styles.xml
index 3aec308..47e88f1 100644
--- a/res/values-sw800dp-port/styles.xml
+++ b/res/values-sw800dp-port/styles.xml
@@ -19,7 +19,7 @@
 <resources xmlns:android="http://schemas.android.com/apk/res/android">
 
     <style name="DisplayEditTextStyle.Formula">
-        <item name="android:paddingTop">72dip</item>
+        <item name="android:paddingTop">16dip</item>
         <item name="android:paddingBottom">32dip</item>
         <item name="android:paddingStart">44dip</item>
         <item name="android:paddingEnd">44dip</item>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 843bf2d..12295e5 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -21,6 +21,7 @@
         <item name="android:colorPrimary">@color/calculator_accent_color</item>
         <item name="android:navigationBarColor">@android:color/black</item>
         <item name="android:statusBarColor">@color/calculator_accent_color</item>
+        <item name="android:windowActionModeOverlay">true</item>
         <item name="android:windowContentOverlay">@null</item>
     </style>
 
diff --git a/src/com/android/calculator2/Calculator.java b/src/com/android/calculator2/Calculator.java
index 8298a44..0d4ab2d 100644
--- a/src/com/android/calculator2/Calculator.java
+++ b/src/com/android/calculator2/Calculator.java
@@ -73,10 +73,8 @@
 import android.view.ViewGroupOverlay;
 import android.view.animation.AccelerateDecelerateInterpolator;
 import android.webkit.WebView;
-import android.widget.Button;
-import android.widget.PopupMenu;
-import android.widget.PopupMenu.OnMenuItemClickListener;
 import android.widget.TextView;
+import android.widget.Toolbar;
 
 import com.android.calculator2.CalculatorEditText.OnTextSizeChangeListener;
 
@@ -90,7 +88,7 @@
 import java.text.DecimalFormatSymbols;  // TODO: May eventually not need this here.
 
 public class Calculator extends Activity
-        implements OnTextSizeChangeListener, OnLongClickListener, OnMenuItemClickListener, CalculatorEditText.PasteListener {
+        implements OnTextSizeChangeListener, OnLongClickListener, CalculatorEditText.PasteListener {
 
     /**
      * Constant for an invalid resource id.
@@ -178,15 +176,15 @@
     private Evaluator mEvaluator;
 
     private View mDisplayView;
+    private TextView mModeView;
     private CalculatorEditText mFormulaEditText;
     private CalculatorResult mResult;
-    private TextView mDegRadDisplay;
+
     private ViewPager mPadViewPager;
     private View mDeleteButton;
-    private View mEqualButton;
     private View mClearButton;
-    private View mOverflowMenuButton;
-    private Button mDegRadButton;
+    private View mEqualButton;
+    private TextView mModeButton;
 
     private View mCurrentButton;
     private Animator mCurrentAnimator;
@@ -199,11 +197,16 @@
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_calculator);
+        setActionBar((Toolbar) findViewById(R.id.toolbar));
+
+        // Hide all default options in the ActionBar.
+        getActionBar().setDisplayOptions(0);
 
         mDisplayView = findViewById(R.id.display);
+        mModeView = (TextView) findViewById(R.id.deg_rad);
         mFormulaEditText = (CalculatorEditText) findViewById(R.id.formula);
         mResult = (CalculatorResult) findViewById(R.id.result);
-        mDegRadDisplay = (TextView) findViewById(R.id.deg_rad);
+
         mPadViewPager = (ViewPager) findViewById(R.id.pad_pager);
         mDeleteButton = findViewById(R.id.del);
         mClearButton = findViewById(R.id.clr);
@@ -211,8 +214,7 @@
         if (mEqualButton == null || mEqualButton.getVisibility() != View.VISIBLE) {
             mEqualButton = findViewById(R.id.pad_operator).findViewById(R.id.eq);
         }
-        mOverflowMenuButton = findViewById(R.id.overflow_menu);
-        mDegRadButton = (Button)findViewById(R.id.mode_deg_rad);
+        mModeButton = (TextView) findViewById(R.id.mode_deg_rad);
 
         mEvaluator = new Evaluator(this, mResult);
         mResult.setEvaluator(mEvaluator);
@@ -278,7 +280,7 @@
 
     // Set the state, updating delete label and display colors.
     // This restores display positions on moving to INPUT.
-    // But movement/animation for moving to RESULT has already been done. 
+    // But movement/animation for moving to RESULT has already been done.
     private void setState(CalculatorState state) {
         if (mCurrentState != state) {
             if (state == CalculatorState.INPUT) {
@@ -308,6 +310,8 @@
                 getWindow().setStatusBarColor(
                         getResources().getColor(R.color.calculator_accent_color));
             }
+
+            invalidateOptionsMenu();
         }
     }
 
@@ -338,16 +342,14 @@
     // to reflect the indicated current degree mode (true = degrees)
     // TODO: Hide the top corner display until the advanced panel is exposed.
     private void updateDegreeMode(boolean dm) {
-        Resources res = getResources();
-        String descr;
         if (dm) {
-            mDegRadDisplay.setText(R.string.mode_deg);
-            mDegRadButton.setText(R.string.mode_rad);
-            mDegRadButton.setContentDescription(res.getString(R.string.desc_mode_rad));
+            mModeView.setText(R.string.mode_deg);
+            mModeButton.setText(R.string.mode_rad);
+            mModeButton.setContentDescription(getString(R.string.desc_mode_rad));
         } else {
-            mDegRadDisplay.setText(R.string.mode_rad);
-            mDegRadButton.setText(R.string.mode_deg);
-            mDegRadButton.setContentDescription(res.getString(R.string.desc_mode_deg));
+            mModeView.setText(R.string.mode_rad);
+            mModeButton.setText(R.string.mode_deg);
+            mModeButton.setContentDescription(getString(R.string.desc_mode_deg));
         }
     }
 
@@ -379,23 +381,20 @@
 
     public void onButtonClick(View view) {
         mCurrentButton = view;
-        int id = view.getId();
 
         // Always cancel in-progress evaluation.
         // If we were waiting for the result, do nothing else.
         mEvaluator.cancelAll();
+
         if (mCurrentState == CalculatorState.EVALUATE
                 || mCurrentState == CalculatorState.ANIMATE) {
             onCancelled();
             return;
         }
+
+
+        final int id = view.getId();
         switch (id) {
-            case R.id.overflow_menu:
-                PopupMenu menu = constructPopupMenu();
-                if (menu != null) {
-                    menu.show();
-                }
-                break;
             case R.id.eq:
                 onEquals();
                 break;
@@ -453,6 +452,9 @@
 
     // Initial evaluation completed successfully.  Initiate display.
     public void onEvaluate(int initDisplayPrec, String truncatedWholeNumber) {
+        // Invalidate any options that may depend on the current result.
+        invalidateOptionsMenu();
+
         if (mCurrentState == CalculatorState.INPUT) {
             // Just update small result display.
             mResult.displayResult(initDisplayPrec, truncatedWholeNumber);
@@ -697,25 +699,25 @@
         mFormulaEditText.requestFocus();
      }
 
-    // Overflow menu handling.
-    private PopupMenu constructPopupMenu() {
-        final PopupMenu popupMenu = new PopupMenu(this, mOverflowMenuButton);
-        mOverflowMenuButton.setOnTouchListener(popupMenu.getDragToOpenListener());
-        popupMenu.inflate(R.menu.overflow);
-        final Menu menu = popupMenu.getMenu();
+    @Override
+    public boolean onCreateOptionsMenu(Menu menu) {
+        getMenuInflater().inflate(R.menu.overflow, menu);
+        return true;
+    }
+
+    @Override
+    public boolean onPrepareOptionsMenu(Menu menu) {
         if (mCurrentState != CalculatorState.RESULT) {
             menu.findItem(R.id.menu_fraction).setEnabled(false);
             menu.findItem(R.id.menu_leading).setEnabled(false);
         } else if (mEvaluator.getRational() == null) {
             menu.findItem(R.id.menu_fraction).setEnabled(false);
         }
-        popupMenu.setOnMenuItemClickListener(this);
-        onPrepareOptionsMenu(menu);
-        return popupMenu;
+        return true;
     }
 
     @Override
-    public boolean onMenuItemClick(MenuItem item) {
+    public boolean onOptionsItemSelected(MenuItem item) {
         switch (item.getItemId()) {
             case R.id.menu_help:
                 displayHelpMessage();
diff --git a/src/com/android/calculator2/CalculatorResult.java b/src/com/android/calculator2/CalculatorResult.java
index 59e9d84..6fac5a6 100644
--- a/src/com/android/calculator2/CalculatorResult.java
+++ b/src/com/android/calculator2/CalculatorResult.java
@@ -95,6 +95,10 @@
         mGestureDetector = new GestureDetector(context,
             new GestureDetector.SimpleOnGestureListener() {
                 @Override
+                public boolean onDown(MotionEvent e) {
+                    return true;
+                }
+                @Override
                 public boolean onFling(MotionEvent e1, MotionEvent e2,
                                        float velocityX, float velocityY) {
                     if (!mScroller.isFinished()) {