OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.blimp.toolbar; | |
6 | |
7 import android.content.ActivityNotFoundException; | |
8 import android.content.Context; | |
9 import android.content.Intent; | |
10 import android.net.Uri; | |
11 import android.view.View; | |
12 import android.widget.AdapterView; | |
13 import android.widget.AdapterView.OnItemClickListener; | |
14 import android.widget.ArrayAdapter; | |
15 import android.widget.ListPopupWindow; | |
16 | |
17 import org.chromium.base.Log; | |
18 import org.chromium.blimp.R; | |
19 import org.chromium.blimp.session.BlimpClientSession; | |
20 import org.chromium.blimp.session.EngineInfo; | |
21 import org.chromium.blimp.settings.AboutBlimpPreferences; | |
22 import org.chromium.blimp.settings.Preferences; | |
23 | |
24 import java.util.ArrayList; | |
25 import java.util.List; | |
26 | |
27 /** | |
28 * A PopupMenu attached to Blimp toolbar that presents various menu options to t
he user. | |
29 */ | |
30 public class ToolbarMenu implements BlimpClientSession.ConnectionObserver { | |
31 /** | |
32 * An interface to be notified of user actions on ToolbarMenu. | |
33 */ | |
34 public interface ToolbarMenuDelegate { | |
35 /** | |
36 * Called to show the debug view. | |
37 * @param show Show debug view if true, hide otherwise. | |
38 */ | |
39 public void showDebugView(boolean show); | |
40 } | |
41 | |
42 private static final String TAG = "ToolbarMenu"; | |
43 | |
44 private Context mContext; | |
45 private ListPopupWindow mPopupMenu; | |
46 private Toolbar mToolbar; | |
47 | |
48 private static final int ID_OPEN_IN_CHROME = 0; | |
49 private static final int ID_VERSION_INFO = 1; | |
50 private static final int ID_TOGGLE_DEBUG_INFO = 2; | |
51 | |
52 private List<String> mMenuTitles; | |
53 private ArrayAdapter<String> mPopupMenuAdapter; | |
54 private EngineInfo mEngineInfo; | |
55 | |
56 // Flag to set the visibility of debug view. | |
57 private boolean mDebugInfoEnabled = false; | |
58 | |
59 /** | |
60 * Returns if the user has enabled debug view from the menu. | |
61 * @return true if debug view is showing, false otherwise | |
62 */ | |
63 public boolean isDebugInfoEnabled() { | |
64 return mDebugInfoEnabled; | |
65 } | |
66 | |
67 private ToolbarMenuDelegate mDelegate; | |
68 | |
69 public ToolbarMenu(Context context, Toolbar toolbar) { | |
70 mContext = context; | |
71 mDelegate = (ToolbarMenuDelegate) mContext; | |
72 mToolbar = toolbar; | |
73 } | |
74 | |
75 /** | |
76 * Opens up a lazily created menu on the toolbar. | |
77 * @param anchorView The view at which menu is to be anchored. | |
78 */ | |
79 public void showMenu(View anchorView) { | |
80 if (mPopupMenu == null) { | |
81 initializeMenu(anchorView); | |
82 } | |
83 mPopupMenu.show(); | |
84 mPopupMenu.getListView().setDivider(null); | |
85 } | |
86 | |
87 /** | |
88 * Creates and initializes the app menu anchored to the specified view. | |
89 * @param anchorView The anchor of the {@link ListPopupWindow} | |
90 */ | |
91 private void initializeMenu(View anchorView) { | |
92 mPopupMenu = new ListPopupWindow(mContext); | |
93 intializeMenuAdapter(); | |
94 mPopupMenu.setAnchorView(anchorView); | |
95 mPopupMenu.setWidth( | |
96 mContext.getResources().getDimensionPixelSize(R.dimen.toolbar_po
pup_item_width)); | |
97 mPopupMenu.setVerticalOffset(-anchorView.getHeight()); | |
98 mPopupMenu.setModal(true); | |
99 mPopupMenu.setOnItemClickListener(new OnItemClickListener() { | |
100 @Override | |
101 public void onItemClick(AdapterView<?> parent, View view, int positi
on, long id) { | |
102 Log.d(TAG, "clicked " + position); | |
103 switch (position) { | |
104 case ID_OPEN_IN_CHROME: | |
105 openInChrome(); | |
106 break; | |
107 case ID_VERSION_INFO: | |
108 showVersionInfo(); | |
109 break; | |
110 case ID_TOGGLE_DEBUG_INFO: | |
111 toggleDebugInfo(); | |
112 break; | |
113 default: | |
114 assert false; | |
115 break; | |
116 } | |
117 mPopupMenu.dismiss(); | |
118 } | |
119 }); | |
120 } | |
121 | |
122 /** | |
123 * Creates an adapter for the toolbar menu. | |
124 */ | |
125 private void intializeMenuAdapter() { | |
126 mMenuTitles = new ArrayList<>(); | |
127 mMenuTitles.add(mContext.getString(R.string.open_in_chrome)); | |
128 mMenuTitles.add(mContext.getString(R.string.version_info)); | |
129 mMenuTitles.add(mContext.getString( | |
130 mDebugInfoEnabled ? R.string.hide_debug_info : R.string.show_deb
ug_info)); | |
131 | |
132 mPopupMenuAdapter = | |
133 new ArrayAdapter<String>(mContext, R.layout.toolbar_popup_item,
mMenuTitles); | |
134 mPopupMenu.setAdapter(mPopupMenuAdapter); | |
135 } | |
136 | |
137 /** | |
138 * Opens the current URL in chrome. | |
139 */ | |
140 private void openInChrome() { | |
141 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(mToolbar.getUrl
())); | |
142 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
143 intent.setPackage("com.android.chrome"); | |
144 try { | |
145 mContext.startActivity(intent); | |
146 } catch (ActivityNotFoundException e) { | |
147 // Chrome is not installed, so try with the default browser | |
148 intent.setPackage(null); | |
149 mContext.startActivity(intent); | |
150 } | |
151 } | |
152 | |
153 private void showVersionInfo() { | |
154 Intent intent = new Intent(); | |
155 intent.setClass(mContext, Preferences.class); | |
156 if (mEngineInfo != null) { | |
157 intent.putExtra(AboutBlimpPreferences.EXTRA_ENGINE_IP, mEngineInfo.i
pAddress); | |
158 intent.putExtra(AboutBlimpPreferences.EXTRA_ENGINE_VERSION, mEngineI
nfo.engineVersion); | |
159 } | |
160 mContext.startActivity(intent); | |
161 } | |
162 | |
163 private void toggleDebugInfo() { | |
164 mDebugInfoEnabled = !mDebugInfoEnabled; | |
165 mMenuTitles.set(ID_TOGGLE_DEBUG_INFO, | |
166 mContext.getString( | |
167 mDebugInfoEnabled ? R.string.hide_debug_info : R.string.
show_debug_info)); | |
168 mPopupMenuAdapter.notifyDataSetChanged(); | |
169 mDelegate.showDebugView(mDebugInfoEnabled); | |
170 } | |
171 | |
172 // BlimpClientSession.ConnectionObserver interface. | |
173 @Override | |
174 public void onAssignmentReceived( | |
175 int result, int suggestedMessageResourceId, EngineInfo engineInfo) { | |
176 mEngineInfo = engineInfo; | |
177 } | |
178 | |
179 @Override | |
180 public void onConnected() { | |
181 if (mEngineInfo == null) return; | |
182 | |
183 mEngineInfo.setConnected(true); | |
184 } | |
185 | |
186 @Override | |
187 public void onDisconnected(String reason) { | |
188 if (mEngineInfo == null) return; | |
189 | |
190 mEngineInfo.setConnected(false); | |
191 } | |
192 | |
193 @Override | |
194 public void updateDebugStatsUI(int received, int sent, int commits) {} | |
195 } | |
OLD | NEW |