public String getRandomWord(HandlerContext handlerContext) throws IOException { RandomAccessFile file = new RandomAccessFile( ((PixGameHandlerConfig) handlerContext.getHandlerConfig()).getDictPath(), "r"); file.seek((long) (Math.random() * file.length())); // Eat the characters of the current line to go to beginning of the next line file.readLine(); // Now read and return the line return file.readLine(); }
private static Twitter getTwitter(HandlerContext handlerContext) { Twitter res = (Twitter) handlerContext.get("twitter"); if (res == null) { TwitterLinksHandlerConfig twitterLinksHandlerConfig = (TwitterLinksHandlerConfig) handlerContext.getHandlerConfig(); ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder .setDebugEnabled(true) .setOAuthConsumerKey(twitterLinksHandlerConfig.getOauthConsumerKey()); configurationBuilder.setOAuthConsumerSecret( twitterLinksHandlerConfig.getOauthConsumerSecret()); configurationBuilder.setOAuthAccessToken(twitterLinksHandlerConfig.getOauthAccessToken()); configurationBuilder.setOAuthAccessTokenSecret( twitterLinksHandlerConfig.getOauthAccessTokenSecret()); TwitterFactory twitterFactory = new TwitterFactory(configurationBuilder.build()); res = twitterFactory.getInstance(); handlerContext.put("twitter", res); } return res; }
private Customsearch getCustomsearch(HandlerContext handlerContext) { Customsearch res = (Customsearch) handlerContext.get("customsearch"); if (res == null) { Customsearch.Builder customSearchBuilder; try { customSearchBuilder = new Customsearch.Builder( GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, null); customSearchBuilder.setApplicationName(APPLICATION_NAME); String key = ((PixGameHandlerConfig) handlerContext.getHandlerConfig()).getKey(); customSearchBuilder.setCustomsearchRequestInitializer( new CustomsearchRequestInitializer(key)); res = customSearchBuilder.build(); handlerContext.put("customsearch", res); } catch (Exception e) { Log.e( TAG, "PixGameHandler Could not initialize! PixGameHandler will not work until this problem is resolved", e); } } return res; }
private long queryGoogle(HandlerContext handlerContext, Connection connection, String searchTerms) throws IOException { if (Config.LOGD) Log.d(TAG, "queryGoogle searchTerms=" + searchTerms); Customsearch customsearch = getCustomsearch(handlerContext); Customsearch.Cse.List list = customsearch.cse().list("\"" + searchTerms + "\""); String cx = ((PixGameHandlerConfig) handlerContext.getHandlerConfig()).getCx(); list.setCx(cx); list.setSearchType("image"); list.setFields("items/link,searchInformation/totalResults"); list.setNum((long) RESULT_SIZE); list.setStart((long) mGuessCount + 1); // Execute the query Search search = list.execute(); mSearchResults = search.getItems(); return search.getSearchInformation().getTotalResults(); }