Index: blimp/client/app/android/java/src/org/chromium/blimp/toolbar/ToolbarMenu.java |
diff --git a/blimp/client/app/android/java/src/org/chromium/blimp/toolbar/ToolbarMenu.java b/blimp/client/app/android/java/src/org/chromium/blimp/toolbar/ToolbarMenu.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5a3511aa0471d34b9d56559f3d43b2ed81e661f6 |
--- /dev/null |
+++ b/blimp/client/app/android/java/src/org/chromium/blimp/toolbar/ToolbarMenu.java |
@@ -0,0 +1,194 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.blimp.toolbar; |
+ |
+import android.content.ActivityNotFoundException; |
+import android.content.Context; |
+import android.content.Intent; |
+import android.net.Uri; |
+import android.view.View; |
+import android.widget.AdapterView; |
+import android.widget.AdapterView.OnItemClickListener; |
+import android.widget.ArrayAdapter; |
+import android.widget.ListPopupWindow; |
+ |
+import org.chromium.base.Log; |
+import org.chromium.blimp.R; |
+import org.chromium.blimp.session.BlimpClientSession; |
+import org.chromium.blimp.session.EngineInfo; |
+import org.chromium.blimp.settings.AboutBlimpPreferences; |
+import org.chromium.blimp.settings.Preferences; |
+ |
+import java.util.ArrayList; |
+import java.util.List; |
+ |
+/** |
+ * A PopupMenu attached to Blimp toolbar that presents various menu options to the user. |
+ */ |
+public class ToolbarMenu implements BlimpClientSession.ConnectionObserver { |
+ private static final String TAG = "ToolbarMenu"; |
+ |
+ private Context mContext; |
+ private ListPopupWindow mPopupMenu; |
+ private Toolbar mToolbar; |
+ |
+ private static final int ID_OPEN_IN_CHROME = 0; |
+ private static final int ID_VERSION_INFO = 1; |
+ private static final int ID_TOGGLE_DEBUG_INFO = 2; |
+ |
+ private List<String> mMenuTitles; |
+ private ArrayAdapter<String> mPopupMenuAdapter; |
+ private EngineInfo mEngineInfo; |
+ |
+ // Flag to set the visibility of debug view. |
+ private boolean mDebugInfoEnabled = false; |
+ |
+ /** |
+ * @return the mDebugInfoEnabled |
Kevin M
2016/05/20 01:02:02
Can you improve the comments here?
shaktisahu
2016/05/22 22:36:56
Done.
|
+ */ |
+ public boolean isDebugInfoEnabled() { |
+ return mDebugInfoEnabled; |
+ } |
+ |
+ /** |
+ * @param mDebugInfoEnabled the mDebugInfoEnabled to set |
Kevin M
2016/05/20 01:02:02
and here
shaktisahu
2016/05/22 22:36:56
Done.
|
+ */ |
+ public void setDebugInfoEnabled(boolean debugInfoEnabled) { |
+ this.mDebugInfoEnabled = debugInfoEnabled; |
+ } |
+ |
+ /** |
+ * An interface to be notified of user actions on ToolbarMenu. |
+ */ |
+ public interface ToolbarMenuDelegate { |
Kevin M
2016/05/20 01:02:02
Should the interface come before the field list? I
shaktisahu
2016/05/22 22:36:56
Done.
|
+ /** |
+ * Called to show the debug view. |
+ * @param show Show debug view if true, hide otherwise. |
+ */ |
+ public void showDebugView(boolean show); |
+ } |
+ |
+ private ToolbarMenuDelegate mDelegate; |
+ |
+ public ToolbarMenu(Context context, Toolbar toolbar) { |
+ mContext = context; |
+ mDelegate = (ToolbarMenuDelegate) mContext; |
+ mToolbar = toolbar; |
+ } |
+ |
+ /** |
+ * Opens up the menu, initializes if uninitialized. |
Kevin M
2016/05/20 01:02:02
"Opens a lazily-created menu?"
Would it be good t
|
+ * @param anchorView The view at which menu is to be anchored. |
+ */ |
+ public void showMenu(View anchorView) { |
+ if (mPopupMenu == null) { |
+ initializeMenu(anchorView); |
+ } |
+ mPopupMenu.show(); |
+ mPopupMenu.getListView().setDivider(null); |
+ } |
+ |
+ /** |
+ * Creates and initializes the app menu anchored to the specified view. |
+ * @param anchorView The anchor of the {@link ListPopupWindow} |
+ */ |
+ private void initializeMenu(View anchorView) { |
Kevin M
2016/05/20 01:02:02
Turn this into a creation method instead of an ini
shaktisahu
2016/05/22 22:36:56
hmm, initialize sounds good :) ....I think most of
Kevin M
2016/05/24 01:02:00
Acknowledged.
|
+ mPopupMenu = new ListPopupWindow(mContext); |
+ intializeMenuAdapter(); |
+ mPopupMenu.setAnchorView(anchorView); |
+ mPopupMenu.setWidth( |
+ mContext.getResources().getDimensionPixelSize(R.dimen.toolbar_popup_item_width)); |
+ mPopupMenu.setVerticalOffset(-anchorView.getHeight()); |
+ mPopupMenu.setModal(true); |
+ mPopupMenu.setOnItemClickListener(new OnItemClickListener() { |
+ @Override |
+ public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
+ Log.d(TAG, "clicked " + position); |
+ switch (position) { |
+ case ID_OPEN_IN_CHROME: |
+ openInChrome(); |
+ break; |
+ case ID_VERSION_INFO: |
+ showVersionInfo(); |
+ break; |
+ case ID_TOGGLE_DEBUG_INFO: |
+ toggleDebugInfo(); |
+ break; |
+ default: |
+ assert false; |
+ break; |
+ } |
+ mPopupMenu.dismiss(); |
+ } |
+ }); |
+ } |
+ |
+ private void intializeMenuAdapter() { |
Kevin M
2016/05/20 01:02:02
Comment?
shaktisahu
2016/05/22 22:36:56
Done.
|
+ mMenuTitles = new ArrayList<>(); |
+ mMenuTitles.add(mContext.getString(R.string.open_in_chrome)); |
+ mMenuTitles.add(mContext.getString(R.string.version_info)); |
+ mMenuTitles.add(mContext.getString( |
+ mDebugInfoEnabled ? R.string.hide_debug_info : R.string.show_debug_info)); |
+ |
+ mPopupMenuAdapter = |
+ new ArrayAdapter<String>(mContext, R.layout.toolbar_popup_item, mMenuTitles); |
+ mPopupMenu.setAdapter(mPopupMenuAdapter); |
+ } |
+ |
+ private void openInChrome() { |
Kevin M
2016/05/20 01:02:02
Comment?
shaktisahu
2016/05/22 22:36:56
Done.
|
+ Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(mToolbar.getUrl())); |
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
+ intent.setPackage("com.android.chrome"); |
+ try { |
+ mContext.startActivity(intent); |
+ } catch (ActivityNotFoundException e) { |
+ // Chrome is probably not installed, so try with the default browser |
+ intent.setPackage(null); |
+ mContext.startActivity(intent); |
+ } |
+ } |
+ |
+ private void showVersionInfo() { |
+ Intent intent = new Intent(); |
+ intent.setClass(mContext, Preferences.class); |
+ intent.putExtra(AboutBlimpPreferences.EXTRA_ASSIGNER_URL, mEngineInfo.assignerUrl); |
+ intent.putExtra(AboutBlimpPreferences.EXTRA_ENGINE_IP, mEngineInfo.ipAddress); |
+ intent.putExtra(AboutBlimpPreferences.EXTRA_ENGINE_VERSION, mEngineInfo.engineVersion); |
+ mContext.startActivity(intent); |
+ } |
+ |
+ private void toggleDebugInfo() { |
+ mDebugInfoEnabled = !mDebugInfoEnabled; |
+ mMenuTitles.set(ID_TOGGLE_DEBUG_INFO, |
+ mContext.getString( |
+ mDebugInfoEnabled ? R.string.hide_debug_info : R.string.show_debug_info)); |
+ mPopupMenuAdapter.notifyDataSetChanged(); |
+ mDelegate.showDebugView(mDebugInfoEnabled); |
+ } |
+ |
+ // BlimpClientSession.ConnectionObserver interface. |
+ @Override |
+ public void onAssignmentReceived( |
+ int result, int suggestedMessageResourceId, EngineInfo engineInfo) { |
+ mEngineInfo = engineInfo; |
+ } |
+ |
+ @Override |
+ public void onConnected() { |
+ if (mEngineInfo == null) return; |
+ |
+ mEngineInfo.setConnected(true); |
+ } |
+ |
+ @Override |
+ public void onDisconnected(String reason) { |
+ if (mEngineInfo == null) return; |
+ |
+ mEngineInfo.setConnected(false); |
+ } |
+ |
+ @Override |
+ public void updateDebugInfo(int received, int sent, int commits) {} |
+} |