summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/speeddial/ContextMenu.java
diff options
context:
space:
mode:
authorcalderwoodra <calderwoodra@google.com>2018-05-02 12:33:06 -0700
committerWeijia Xu <weijiaxu@google.com>2018-05-04 06:15:45 +0000
commitd22ef290c45701ef65d9d0e004a64c683c8c2788 (patch)
treee7a3a892e171451b8ca891b0291340e46fc448e4 /java/com/android/dialer/speeddial/ContextMenu.java
parent9c70e4ca34c2f3f0c5d48b549602603eed7a6c2e (diff)
Migrated context menu to be a PopupMenu instead.
This gives us huge amounts of functionality with very little technical cost but some trade off in UX. Bug: 77761183,78492250 Test: existing PiperOrigin-RevId: 195133774 Change-Id: I57e48b5defc4ae1c7bfbed13e3fbc16ebd607944
Diffstat (limited to 'java/com/android/dialer/speeddial/ContextMenu.java')
-rw-r--r--java/com/android/dialer/speeddial/ContextMenu.java156
1 files changed, 81 insertions, 75 deletions
diff --git a/java/com/android/dialer/speeddial/ContextMenu.java b/java/com/android/dialer/speeddial/ContextMenu.java
index 09505ab99..e0a4551db 100644
--- a/java/com/android/dialer/speeddial/ContextMenu.java
+++ b/java/com/android/dialer/speeddial/ContextMenu.java
@@ -17,101 +17,107 @@
package com.android.dialer.speeddial;
import android.content.Context;
-import android.support.annotation.Nullable;
+import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
-import android.util.AttributeSet;
+import android.support.v7.widget.PopupMenu;
+import android.support.v7.widget.PopupMenu.OnMenuItemClickListener;
+import android.text.TextUtils;
+import android.view.Gravity;
+import android.view.MenuItem;
import android.view.View;
-import android.widget.LinearLayout;
-import android.widget.TextView;
import com.android.dialer.common.Assert;
import com.android.dialer.speeddial.database.SpeedDialEntry.Channel;
import com.android.dialer.speeddial.loader.SpeedDialUiItem;
-/** Floating menu which presents contact options available to the contact. */
-public class ContextMenu extends LinearLayout {
-
- private ContextMenuItemListener listener;
-
- private TextView voiceView;
- private TextView videoView;
- private TextView smsView;
-
- private SpeedDialUiItem speedDialUiItem;
- private Channel voiceChannel;
- private Channel videoChannel;
-
- public ContextMenu(Context context, @Nullable AttributeSet attrs) {
- super(context, attrs);
+/** {@link PopupMenu} which presents contact options for starred contacts. */
+public class ContextMenu extends PopupMenu implements OnMenuItemClickListener {
+
+ private final ContextMenuItemListener listener;
+
+ private final SpeedDialUiItem speedDialUiItem;
+ private final Channel voiceChannel;
+ private final Channel videoChannel;
+
+ private boolean visible;
+
+ /**
+ * Creates a new context menu and displays it.
+ *
+ * @see #show()
+ */
+ public static ContextMenu show(
+ Context context,
+ View anchor,
+ ContextMenuItemListener contextMenuListener,
+ SpeedDialUiItem speedDialUiItem) {
+ ContextMenu menu = new ContextMenu(context, anchor, contextMenuListener, speedDialUiItem);
+ menu.show();
+ menu.visible = true;
+ return menu;
}
- @Override
- protected void onFinishInflate() {
- super.onFinishInflate();
-
- videoView = findViewById(R.id.video_call_container);
- videoView.setOnClickListener(v -> placeVideoCall());
-
- smsView = findViewById(R.id.send_message_container);
- smsView.setOnClickListener(v -> listener.openSmsConversation(voiceChannel.number()));
-
- voiceView = findViewById(R.id.voice_call_container);
- voiceView.setOnClickListener(v -> placeVoiceCall());
-
- findViewById(R.id.remove_container)
- .setOnClickListener(v -> listener.removeFavoriteContact(speedDialUiItem));
- findViewById(R.id.contact_info_container)
- .setOnClickListener(v -> listener.openContactInfo(speedDialUiItem));
+ /**
+ * Hides the context menu.
+ *
+ * @see #dismiss()
+ */
+ public void hide() {
+ dismiss();
+ visible = false;
}
- /** Shows the menu and updates the menu's position w.r.t. the view it's related to. */
- public void showMenu(
- View parentLayout,
- View childLayout,
- SpeedDialUiItem speedDialUiItem,
- ContextMenuItemListener listener) {
- this.speedDialUiItem = speedDialUiItem;
+ private ContextMenu(
+ @NonNull Context context,
+ @NonNull View anchor,
+ ContextMenuItemListener listener,
+ SpeedDialUiItem speedDialUiItem) {
+ super(context, anchor, Gravity.CENTER);
this.listener = listener;
-
- int[] childLocation = new int[2];
- int[] parentLocation = new int[2];
- childLayout.getLocationOnScreen(childLocation);
- parentLayout.getLocationOnScreen(parentLocation);
-
- setX((float) (childLocation[0] + .5 * childLayout.getWidth() - .5 * getWidth()));
- setY(childLocation[1] - parentLocation[1] + childLayout.getHeight());
-
+ this.speedDialUiItem = speedDialUiItem;
voiceChannel = speedDialUiItem.getDefaultVoiceChannel();
videoChannel = speedDialUiItem.getDefaultVideoChannel();
- voiceView.setVisibility(videoChannel == null ? View.GONE : View.VISIBLE);
- videoView.setVisibility(videoChannel == null ? View.GONE : View.VISIBLE);
- smsView.setVisibility(voiceChannel == null ? View.GONE : View.VISIBLE);
-
- // TODO(calderwoodra): a11y
- // TODO(calderwoodra): animate this similar to the bubble menu
- setVisibility(View.VISIBLE);
- }
- /** Returns true if the view was hidden. */
- public void hideMenu() {
- this.speedDialUiItem = null;
- this.listener = null;
- if (getVisibility() == View.VISIBLE) {
- // TODO(calderwoodra): a11y
- // TODO(calderwoodra): animate this similar to the bubble menu
- setVisibility(View.INVISIBLE);
+ setOnMenuItemClickListener(this);
+ getMenuInflater().inflate(R.menu.starred_contact_context_menu, getMenu());
+ getMenu().findItem(R.id.voice_call_container).setVisible(voiceChannel != null);
+ getMenu().findItem(R.id.video_call_container).setVisible(videoChannel != null);
+ getMenu().findItem(R.id.send_message_container).setVisible(voiceChannel != null);
+ if (voiceChannel != null) {
+ String secondaryInfo =
+ TextUtils.isEmpty(voiceChannel.label())
+ ? voiceChannel.number()
+ : context.getString(
+ R.string.call_subject_type_and_number,
+ voiceChannel.label(),
+ voiceChannel.number());
+ getMenu().findItem(R.id.starred_contact_context_menu_title).setTitle(secondaryInfo);
+ getMenu().findItem(R.id.starred_contact_context_menu_title).setVisible(true);
+ } else {
+ getMenu().findItem(R.id.starred_contact_context_menu_title).setVisible(false);
}
}
- private void placeVoiceCall() {
- listener.placeCall(Assert.isNotNull(voiceChannel));
- }
-
- private void placeVideoCall() {
- listener.placeCall(Assert.isNotNull(videoChannel));
+ @Override
+ public boolean onMenuItemClick(MenuItem menuItem) {
+ if (menuItem.getItemId() == R.id.voice_call_container) {
+ listener.placeCall(Assert.isNotNull(voiceChannel));
+ } else if (menuItem.getItemId() == R.id.video_call_container) {
+ listener.placeCall(Assert.isNotNull(videoChannel));
+ } else if (menuItem.getItemId() == R.id.send_message_container) {
+ listener.openSmsConversation(voiceChannel.number());
+ } else if (menuItem.getItemId() == R.id.remove_container) {
+ listener.removeFavoriteContact(speedDialUiItem);
+ } else if (menuItem.getItemId() == R.id.contact_info_container) {
+ listener.openContactInfo(speedDialUiItem);
+ } else {
+ throw Assert.createIllegalStateFailException("Menu option click not handled");
+ }
+ return true;
}
+ @VisibleForTesting(otherwise = VisibleForTesting.NONE)
public boolean isVisible() {
- return getVisibility() == View.VISIBLE;
+ return visible;
}
/** Listener to report user clicks on menu items. */