/** Sets search engines to be shown for user-entered queries. */ private void setSearchEngines(JSONObject data) { try { JSONObject suggest = data.getJSONObject("suggest"); String suggestEngine = suggest.isNull("engine") ? null : suggest.getString("engine"); String suggestTemplate = suggest.isNull("template") ? null : suggest.getString("template"); mSuggestionsEnabled = suggest.getBoolean("enabled"); boolean suggestionsPrompted = suggest.getBoolean("prompted"); JSONArray engines = data.getJSONArray("searchEngines"); ArrayList<SearchEngine> searchEngines = new ArrayList<SearchEngine>(); for (int i = 0; i < engines.length(); i++) { JSONObject engineJSON = engines.getJSONObject(i); String name = engineJSON.getString("name"); String iconURI = engineJSON.getString("iconURI"); Bitmap icon = BitmapUtils.getBitmapFromDataURI(iconURI); if (name.equals(suggestEngine) && suggestTemplate != null) { // suggest engine should be at the front of the list searchEngines.add(0, new SearchEngine(name, icon)); // The only time Tabs.getInstance().getSelectedTab() should // be null is when we're restoring after a crash. We should // never restore private tabs when that happens, so it // should be safe to assume that null means non-private. Tab tab = Tabs.getInstance().getSelectedTab(); if (tab == null || !tab.isPrivate()) mSuggestClient = new SuggestClient( GeckoApp.mAppContext, suggestTemplate, SUGGESTION_TIMEOUT, SUGGESTION_MAX); } else { searchEngines.add(new SearchEngine(name, icon)); } } mSearchEngines = searchEngines; mCursorAdapter.notifyDataSetChanged(); // show suggestions opt-in if user hasn't been prompted if (!suggestionsPrompted && mSuggestClient != null) { showSuggestionsOptIn(); } } catch (JSONException e) { Log.e(LOGTAG, "Error getting search engine JSON", e); } filterSuggestions(mSearchTerm); }
private void showNotification(JSONObject message) { NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext); // These attributes are required final String id; try { builder.setContentTitle(message.getString(TITLE_ATTR)); builder.setContentText(message.getString(TEXT_ATTR)); id = message.getString(ID_ATTR); } catch (JSONException ex) { Log.i(LOGTAG, "Error parsing", ex); return; } Uri imageUri = Uri.parse(message.optString(SMALLICON_ATTR)); builder.setSmallIcon(BitmapUtils.getResource(imageUri, R.drawable.ic_status_logo)); JSONArray light = message.optJSONArray(LIGHT_ATTR); if (light != null && light.length() == 3) { try { builder.setLights(light.getInt(0), light.getInt(1), light.getInt(2)); } catch (JSONException ex) { Log.i(LOGTAG, "Error parsing", ex); } } boolean ongoing = message.optBoolean(ONGOING_ATTR); builder.setOngoing(ongoing); if (message.has(WHEN_ATTR)) { long when = message.optLong(WHEN_ATTR); builder.setWhen(when); } if (message.has(PRIORITY_ATTR)) { int priority = message.optInt(PRIORITY_ATTR); builder.setPriority(priority); } if (message.has(LARGE_ICON_ATTR)) { Bitmap b = BitmapUtils.getBitmapFromDataURI(message.optString(LARGE_ICON_ATTR)); builder.setLargeIcon(b); } if (message.has(PROGRESS_VALUE_ATTR) && message.has(PROGRESS_MAX_ATTR) && message.has(PROGRESS_INDETERMINATE_ATTR)) { try { final int progress = message.getInt(PROGRESS_VALUE_ATTR); final int progressMax = message.getInt(PROGRESS_MAX_ATTR); final boolean progressIndeterminate = message.getBoolean(PROGRESS_INDETERMINATE_ATTR); builder.setProgress(progressMax, progress, progressIndeterminate); } catch (JSONException ex) { Log.i(LOGTAG, "Error parsing", ex); } } JSONArray actions = message.optJSONArray(ACTIONS_ATTR); if (actions != null) { try { for (int i = 0; i < actions.length(); i++) { JSONObject action = actions.getJSONObject(i); final PendingIntent pending = buildButtonClickPendingIntent(message, action); final String actionTitle = action.getString(ACTION_TITLE_ATTR); final Uri actionImage = Uri.parse(action.optString(ACTION_ICON_ATTR)); builder.addAction( BitmapUtils.getResource(actionImage, R.drawable.ic_status_logo), actionTitle, pending); } } catch (JSONException ex) { Log.i(LOGTAG, "Error parsing", ex); } } PendingIntent pi = buildNotificationPendingIntent(message, CLICK_EVENT); builder.setContentIntent(pi); PendingIntent deletePendingIntent = buildNotificationPendingIntent(message, CLEARED_EVENT); builder.setDeleteIntent(deletePendingIntent); GeckoAppShell.notificationClient.add(id.hashCode(), builder.build()); boolean persistent = message.optBoolean(PERSISTENT_ATTR); // We add only not persistent notifications to the list since we want to purge only // them when geckoapp is destroyed. if (!persistent && !mClearableNotifications.containsKey(id)) { mClearableNotifications.put(id, message.toString()); } }
private void addAddonMenuItem(final MenuItemInfo info) { if (mMenu == null) { if (mAddonMenuItemsCache == null) mAddonMenuItemsCache = new Vector<MenuItemInfo>(); mAddonMenuItemsCache.add(info); return; } Menu menu; if (info.parent == 0) { menu = mMenu; } else { MenuItem parent = mMenu.findItem(info.parent); if (parent == null) return; if (!parent.hasSubMenu()) { mMenu.removeItem(parent.getItemId()); menu = mMenu.addSubMenu(Menu.NONE, parent.getItemId(), Menu.NONE, parent.getTitle()); if (parent.getIcon() != null) ((SubMenu) menu).getItem().setIcon(parent.getIcon()); } else { menu = parent.getSubMenu(); } } final MenuItem item = menu.add(Menu.NONE, info.id, Menu.NONE, info.label); item.setOnMenuItemClickListener( new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { Log.i(LOGTAG, "menu item clicked"); GeckoAppShell.sendEventToGecko( GeckoEvent.createBroadcastEvent( "Menu:Clicked", Integer.toString(info.id - ADDON_MENU_OFFSET))); return true; } }); if (info.icon != null) { if (info.icon.startsWith("data")) { BitmapDrawable drawable = new BitmapDrawable(BitmapUtils.getBitmapFromDataURI(info.icon)); item.setIcon(drawable); } else if (info.icon.startsWith("jar:") || info.icon.startsWith("file://")) { GeckoAppShell.getHandler() .post( new Runnable() { public void run() { try { URL url = new URL(info.icon); InputStream is = (InputStream) url.getContent(); try { Drawable drawable = Drawable.createFromStream(is, "src"); item.setIcon(drawable); } finally { is.close(); } } catch (Exception e) { Log.w(LOGTAG, "Unable to set icon", e); } } }); } } item.setCheckable(info.checkable); item.setChecked(info.checked); item.setEnabled(info.enabled); item.setVisible(info.visible); }