Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko.toolbar; |
michael@0 | 7 | |
michael@0 | 8 | import org.mozilla.gecko.GeckoAppShell; |
michael@0 | 9 | import org.mozilla.gecko.GeckoEvent; |
michael@0 | 10 | import org.mozilla.gecko.R; |
michael@0 | 11 | import org.mozilla.gecko.gfx.BitmapUtils; |
michael@0 | 12 | import org.mozilla.gecko.util.GeckoEventListener; |
michael@0 | 13 | import org.mozilla.gecko.util.ThreadUtils; |
michael@0 | 14 | import org.mozilla.gecko.widget.GeckoPopupMenu; |
michael@0 | 15 | |
michael@0 | 16 | import org.json.JSONArray; |
michael@0 | 17 | import org.json.JSONException; |
michael@0 | 18 | import org.json.JSONObject; |
michael@0 | 19 | |
michael@0 | 20 | import android.content.Context; |
michael@0 | 21 | import android.content.res.Resources; |
michael@0 | 22 | import android.graphics.Bitmap; |
michael@0 | 23 | import android.graphics.drawable.BitmapDrawable; |
michael@0 | 24 | import android.graphics.drawable.Drawable; |
michael@0 | 25 | import android.util.AttributeSet; |
michael@0 | 26 | import android.util.Log; |
michael@0 | 27 | import android.view.ContextMenu; |
michael@0 | 28 | import android.view.Menu; |
michael@0 | 29 | import android.view.MenuItem; |
michael@0 | 30 | import android.view.View; |
michael@0 | 31 | import android.widget.Button; |
michael@0 | 32 | import android.widget.ImageButton; |
michael@0 | 33 | import android.widget.ImageView; |
michael@0 | 34 | import android.widget.LinearLayout; |
michael@0 | 35 | |
michael@0 | 36 | import java.util.UUID; |
michael@0 | 37 | import java.util.ArrayList; |
michael@0 | 38 | |
michael@0 | 39 | public class PageActionLayout extends LinearLayout implements GeckoEventListener, |
michael@0 | 40 | View.OnClickListener, |
michael@0 | 41 | View.OnLongClickListener { |
michael@0 | 42 | private final String LOGTAG = "GeckoPageActionLayout"; |
michael@0 | 43 | private final String MENU_BUTTON_KEY = "MENU_BUTTON_KEY"; |
michael@0 | 44 | private final int DEFAULT_PAGE_ACTIONS_SHOWN = 2; |
michael@0 | 45 | |
michael@0 | 46 | private ArrayList<PageAction> mPageActionList; |
michael@0 | 47 | private GeckoPopupMenu mPageActionsMenu; |
michael@0 | 48 | private Context mContext; |
michael@0 | 49 | private LinearLayout mLayout; |
michael@0 | 50 | |
michael@0 | 51 | // By default it's two, can be changed by calling setNumberShown(int) |
michael@0 | 52 | private int mMaxVisiblePageActions; |
michael@0 | 53 | |
michael@0 | 54 | public PageActionLayout(Context context, AttributeSet attrs) { |
michael@0 | 55 | super(context, attrs); |
michael@0 | 56 | mContext = context; |
michael@0 | 57 | mLayout = this; |
michael@0 | 58 | |
michael@0 | 59 | mPageActionList = new ArrayList<PageAction>(); |
michael@0 | 60 | setNumberShown(DEFAULT_PAGE_ACTIONS_SHOWN); |
michael@0 | 61 | refreshPageActionIcons(); |
michael@0 | 62 | |
michael@0 | 63 | registerEventListener("PageActions:Add"); |
michael@0 | 64 | registerEventListener("PageActions:Remove"); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | private void setNumberShown(int count) { |
michael@0 | 68 | mMaxVisiblePageActions = count; |
michael@0 | 69 | |
michael@0 | 70 | for(int index = 0; index < count; index++) { |
michael@0 | 71 | if ((this.getChildCount() - 1) < index) { |
michael@0 | 72 | mLayout.addView(createImageButton()); |
michael@0 | 73 | } |
michael@0 | 74 | } |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | public void onDestroy() { |
michael@0 | 78 | unregisterEventListener("PageActions:Add"); |
michael@0 | 79 | unregisterEventListener("PageActions:Remove"); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | protected void registerEventListener(String event) { |
michael@0 | 83 | GeckoAppShell.getEventDispatcher().registerEventListener(event, this); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | protected void unregisterEventListener(String event) { |
michael@0 | 87 | GeckoAppShell.getEventDispatcher().unregisterEventListener(event, this); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | @Override |
michael@0 | 91 | public void handleMessage(String event, JSONObject message) { |
michael@0 | 92 | try { |
michael@0 | 93 | if (event.equals("PageActions:Add")) { |
michael@0 | 94 | final String id = message.getString("id"); |
michael@0 | 95 | final String title = message.getString("title"); |
michael@0 | 96 | final String imageURL = message.optString("icon"); |
michael@0 | 97 | final boolean mImportant = message.getBoolean("important"); |
michael@0 | 98 | |
michael@0 | 99 | addPageAction(id, title, imageURL, new OnPageActionClickListeners() { |
michael@0 | 100 | @Override |
michael@0 | 101 | public void onClick(String id) { |
michael@0 | 102 | GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("PageActions:Clicked", id)); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | @Override |
michael@0 | 106 | public boolean onLongClick(String id) { |
michael@0 | 107 | GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("PageActions:LongClicked", id)); |
michael@0 | 108 | return true; |
michael@0 | 109 | } |
michael@0 | 110 | }, mImportant); |
michael@0 | 111 | } else if (event.equals("PageActions:Remove")) { |
michael@0 | 112 | final String id = message.getString("id"); |
michael@0 | 113 | |
michael@0 | 114 | removePageAction(id); |
michael@0 | 115 | } |
michael@0 | 116 | } catch(JSONException ex) { |
michael@0 | 117 | Log.e(LOGTAG, "Error deocding", ex); |
michael@0 | 118 | } |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | private void addPageAction(final String id, final String title, final String imageData, final OnPageActionClickListeners mOnPageActionClickListeners, boolean mImportant) { |
michael@0 | 122 | final PageAction pageAction = new PageAction(id, title, null, mOnPageActionClickListeners, mImportant); |
michael@0 | 123 | |
michael@0 | 124 | int insertAt = mPageActionList.size(); |
michael@0 | 125 | while(insertAt > 0 && mPageActionList.get(insertAt-1).isImportant()) { |
michael@0 | 126 | insertAt--; |
michael@0 | 127 | } |
michael@0 | 128 | mPageActionList.add(insertAt, pageAction); |
michael@0 | 129 | |
michael@0 | 130 | BitmapUtils.getDrawable(mContext, imageData, new BitmapUtils.BitmapLoader() { |
michael@0 | 131 | @Override |
michael@0 | 132 | public void onBitmapFound(final Drawable d) { |
michael@0 | 133 | if (mPageActionList.contains(pageAction)) { |
michael@0 | 134 | pageAction.setDrawable(d); |
michael@0 | 135 | refreshPageActionIcons(); |
michael@0 | 136 | } |
michael@0 | 137 | } |
michael@0 | 138 | }); |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | private void removePageAction(String id) { |
michael@0 | 142 | for(int i = 0; i < mPageActionList.size(); i++) { |
michael@0 | 143 | if (mPageActionList.get(i).getID().equals(id)) { |
michael@0 | 144 | mPageActionList.remove(i); |
michael@0 | 145 | refreshPageActionIcons(); |
michael@0 | 146 | return; |
michael@0 | 147 | } |
michael@0 | 148 | } |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | private ImageButton createImageButton() { |
michael@0 | 152 | ImageButton imageButton = new ImageButton(mContext, null, R.style.UrlBar_ImageButton_Icon); |
michael@0 | 153 | imageButton.setLayoutParams(new LayoutParams(mContext.getResources().getDimensionPixelSize(R.dimen.page_action_button_width), LayoutParams.MATCH_PARENT)); |
michael@0 | 154 | imageButton.setScaleType(ImageView.ScaleType.CENTER_INSIDE); |
michael@0 | 155 | imageButton.setOnClickListener(this); |
michael@0 | 156 | imageButton.setOnLongClickListener(this); |
michael@0 | 157 | return imageButton; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | @Override |
michael@0 | 161 | public void onClick(View v) { |
michael@0 | 162 | String buttonClickedId = (String)v.getTag(); |
michael@0 | 163 | if (buttonClickedId != null) { |
michael@0 | 164 | if (buttonClickedId.equals(MENU_BUTTON_KEY)) { |
michael@0 | 165 | showMenu(v, mPageActionList.size() - mMaxVisiblePageActions + 1); |
michael@0 | 166 | } else { |
michael@0 | 167 | getPageActionWithId(buttonClickedId).onClick(); |
michael@0 | 168 | } |
michael@0 | 169 | } |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | @Override |
michael@0 | 173 | public boolean onLongClick(View v) { |
michael@0 | 174 | String buttonClickedId = (String)v.getTag(); |
michael@0 | 175 | if (buttonClickedId.equals(MENU_BUTTON_KEY)) { |
michael@0 | 176 | showMenu(v, mPageActionList.size() - mMaxVisiblePageActions + 1); |
michael@0 | 177 | return true; |
michael@0 | 178 | } else { |
michael@0 | 179 | return getPageActionWithId(buttonClickedId).onLongClick(); |
michael@0 | 180 | } |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | private void setActionForView(final ImageButton view, final PageAction pageAction) { |
michael@0 | 184 | if (pageAction == null) { |
michael@0 | 185 | view.setTag(null); |
michael@0 | 186 | ThreadUtils.postToUiThread(new Runnable() { |
michael@0 | 187 | @Override |
michael@0 | 188 | public void run () { |
michael@0 | 189 | view.setImageDrawable(null); |
michael@0 | 190 | view.setVisibility(View.GONE); |
michael@0 | 191 | view.setContentDescription(null); |
michael@0 | 192 | } |
michael@0 | 193 | }); |
michael@0 | 194 | return; |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | view.setTag(pageAction.getID()); |
michael@0 | 198 | ThreadUtils.postToUiThread(new Runnable() { |
michael@0 | 199 | @Override |
michael@0 | 200 | public void run () { |
michael@0 | 201 | view.setImageDrawable(pageAction.getDrawable()); |
michael@0 | 202 | view.setVisibility(View.VISIBLE); |
michael@0 | 203 | view.setContentDescription(pageAction.getTitle()); |
michael@0 | 204 | } |
michael@0 | 205 | }); |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | private void refreshPageActionIcons() { |
michael@0 | 209 | final Resources resources = mContext.getResources(); |
michael@0 | 210 | for(int index = 0; index < this.getChildCount(); index++) { |
michael@0 | 211 | final ImageButton v = (ImageButton)this.getChildAt(index); |
michael@0 | 212 | final PageAction pageAction = getPageActionForViewAt(index); |
michael@0 | 213 | |
michael@0 | 214 | // If there are more pageactions then buttons, set the menu icon. Otherwise set the page action's icon if there is a page action. |
michael@0 | 215 | if (index == (this.getChildCount() - 1) && mPageActionList.size() > mMaxVisiblePageActions) { |
michael@0 | 216 | v.setTag(MENU_BUTTON_KEY); |
michael@0 | 217 | ThreadUtils.postToUiThread(new Runnable() { |
michael@0 | 218 | @Override |
michael@0 | 219 | public void run () { |
michael@0 | 220 | v.setImageDrawable(resources.getDrawable(R.drawable.icon_pageaction)); |
michael@0 | 221 | v.setVisibility((pageAction != null) ? View.VISIBLE : View.GONE); |
michael@0 | 222 | v.setContentDescription(resources.getString(R.string.page_action_dropmarker_description)); |
michael@0 | 223 | } |
michael@0 | 224 | }); |
michael@0 | 225 | } else { |
michael@0 | 226 | setActionForView(v, pageAction); |
michael@0 | 227 | } |
michael@0 | 228 | } |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | private PageAction getPageActionForViewAt(int index) { |
michael@0 | 232 | /** |
michael@0 | 233 | * We show the user the most recent pageaction added since this keeps the user aware of any new page actions being added |
michael@0 | 234 | * Also, the order of the pageAction is important i.e. if a page action is added, instead of shifting the pagactions to the |
michael@0 | 235 | * left to make space for the new one, it would be more visually appealing to have the pageaction appear in the blank space. |
michael@0 | 236 | * |
michael@0 | 237 | * buttonIndex is needed for this reason because every new View added to PageActionLayout gets added to the right of its neighbouring View. |
michael@0 | 238 | * Hence the button on the very leftmost has the index 0. We want our pageactions to start from the rightmost |
michael@0 | 239 | * and hence we maintain the insertion order of the child Views which is essentially the reverse of their index |
michael@0 | 240 | */ |
michael@0 | 241 | |
michael@0 | 242 | int buttonIndex = (this.getChildCount() - 1) - index; |
michael@0 | 243 | int totalVisibleButtons = ((mPageActionList.size() < this.getChildCount()) ? mPageActionList.size() : this.getChildCount()); |
michael@0 | 244 | |
michael@0 | 245 | if (mPageActionList.size() > buttonIndex) { |
michael@0 | 246 | // Return the pageactions starting from the end of the list for the number of visible pageactions. |
michael@0 | 247 | return mPageActionList.get((mPageActionList.size() - totalVisibleButtons) + buttonIndex); |
michael@0 | 248 | } |
michael@0 | 249 | return null; |
michael@0 | 250 | } |
michael@0 | 251 | |
michael@0 | 252 | private PageAction getPageActionWithId(String id) { |
michael@0 | 253 | for(int i = 0; i < mPageActionList.size(); i++) { |
michael@0 | 254 | PageAction pageAction = mPageActionList.get(i); |
michael@0 | 255 | if (pageAction.getID().equals(id)) { |
michael@0 | 256 | return pageAction; |
michael@0 | 257 | } |
michael@0 | 258 | } |
michael@0 | 259 | return null; |
michael@0 | 260 | } |
michael@0 | 261 | |
michael@0 | 262 | private void showMenu(View mPageActionButton, int toShow) { |
michael@0 | 263 | if (mPageActionsMenu == null) { |
michael@0 | 264 | mPageActionsMenu = new GeckoPopupMenu(mPageActionButton.getContext(), mPageActionButton); |
michael@0 | 265 | mPageActionsMenu.inflate(0); |
michael@0 | 266 | mPageActionsMenu.setOnMenuItemClickListener(new GeckoPopupMenu.OnMenuItemClickListener() { |
michael@0 | 267 | @Override |
michael@0 | 268 | public boolean onMenuItemClick(MenuItem item) { |
michael@0 | 269 | int id = item.getItemId(); |
michael@0 | 270 | for(int i = 0; i < mPageActionList.size(); i++) { |
michael@0 | 271 | PageAction pageAction = mPageActionList.get(i); |
michael@0 | 272 | if (pageAction.key() == id) { |
michael@0 | 273 | pageAction.onClick(); |
michael@0 | 274 | return true; |
michael@0 | 275 | } |
michael@0 | 276 | } |
michael@0 | 277 | return false; |
michael@0 | 278 | } |
michael@0 | 279 | }); |
michael@0 | 280 | } |
michael@0 | 281 | Menu menu = mPageActionsMenu.getMenu(); |
michael@0 | 282 | menu.clear(); |
michael@0 | 283 | |
michael@0 | 284 | for(int i = 0; i < mPageActionList.size(); i++) { |
michael@0 | 285 | if (i < toShow) { |
michael@0 | 286 | PageAction pageAction = mPageActionList.get(i); |
michael@0 | 287 | MenuItem item = menu.add(Menu.NONE, pageAction.key(), Menu.NONE, pageAction.getTitle()); |
michael@0 | 288 | item.setIcon(pageAction.getDrawable()); |
michael@0 | 289 | } |
michael@0 | 290 | } |
michael@0 | 291 | mPageActionsMenu.show(); |
michael@0 | 292 | } |
michael@0 | 293 | |
michael@0 | 294 | private static interface OnPageActionClickListeners { |
michael@0 | 295 | public void onClick(String id); |
michael@0 | 296 | public boolean onLongClick(String id); |
michael@0 | 297 | } |
michael@0 | 298 | |
michael@0 | 299 | private static class PageAction { |
michael@0 | 300 | private OnPageActionClickListeners mOnPageActionClickListeners; |
michael@0 | 301 | private Drawable mDrawable; |
michael@0 | 302 | private String mTitle; |
michael@0 | 303 | private String mId; |
michael@0 | 304 | private int key; |
michael@0 | 305 | private boolean mImportant; |
michael@0 | 306 | |
michael@0 | 307 | public PageAction(String id, |
michael@0 | 308 | String title, |
michael@0 | 309 | Drawable image, |
michael@0 | 310 | OnPageActionClickListeners onPageActionClickListeners, |
michael@0 | 311 | boolean important) { |
michael@0 | 312 | mId = id; |
michael@0 | 313 | mTitle = title; |
michael@0 | 314 | mDrawable = image; |
michael@0 | 315 | mOnPageActionClickListeners = onPageActionClickListeners; |
michael@0 | 316 | mImportant = important; |
michael@0 | 317 | |
michael@0 | 318 | key = UUID.fromString(mId.subSequence(1, mId.length() - 2).toString()).hashCode(); |
michael@0 | 319 | } |
michael@0 | 320 | |
michael@0 | 321 | public Drawable getDrawable() { |
michael@0 | 322 | return mDrawable; |
michael@0 | 323 | } |
michael@0 | 324 | |
michael@0 | 325 | public void setDrawable(Drawable d) { |
michael@0 | 326 | mDrawable = d; |
michael@0 | 327 | } |
michael@0 | 328 | |
michael@0 | 329 | public String getTitle() { |
michael@0 | 330 | return mTitle; |
michael@0 | 331 | } |
michael@0 | 332 | |
michael@0 | 333 | public String getID() { |
michael@0 | 334 | return mId; |
michael@0 | 335 | } |
michael@0 | 336 | |
michael@0 | 337 | public int key() { |
michael@0 | 338 | return key; |
michael@0 | 339 | } |
michael@0 | 340 | |
michael@0 | 341 | public boolean isImportant() { |
michael@0 | 342 | return mImportant; |
michael@0 | 343 | } |
michael@0 | 344 | |
michael@0 | 345 | public void onClick() { |
michael@0 | 346 | if (mOnPageActionClickListeners != null) { |
michael@0 | 347 | mOnPageActionClickListeners.onClick(mId); |
michael@0 | 348 | } |
michael@0 | 349 | } |
michael@0 | 350 | |
michael@0 | 351 | public boolean onLongClick() { |
michael@0 | 352 | if (mOnPageActionClickListeners != null) { |
michael@0 | 353 | return mOnPageActionClickListeners.onLongClick(mId); |
michael@0 | 354 | } |
michael@0 | 355 | return false; |
michael@0 | 356 | } |
michael@0 | 357 | } |
michael@0 | 358 | } |