@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SyncManager.sync(this); setContentView(R.layout.activity_online_stories); lv = (ListView) findViewById(android.R.id.list); et = (EditText) findViewById(R.id.search); esHelper = new ESHelper(); /* * Populate listview with the stories currently online Cache the stories * currently online */ ArrayList<Story> result = esHelper.getOnlineStories(); boolean connected = InternetDetector.connectedToInternet(getApplicationContext()); while (result == null && connected) { result = esHelper.getOnlineStories(); connected = InternetDetector.connectedToInternet(getApplicationContext()); } if (!connected) { Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_LONG).show(); finish(); } fillData(result); registerForContextMenu(getListView()); }
/** * This method is called from a button click that allows the user to search through the list of * online Stories. They must have a search string entered into the search text area. If they do * not it will just return the full list of online stories. If they have a search string that is * contained within a title or author of any online story, that story will be displayed in the * list for the user to chose from. * * <p>This method uses ElasticSearch (@link http://www.elasticsearch.org/) to search the * webservice for the online Stories. * * @see ESHelper * @param view The screen used to display the Online Story list for the user. */ public void onClickSearchButton(View view) { searchText = et.getText().toString(); if (searchText != null && !searchText.isEmpty()) { fillData(esHelper.searchOnlineStories(searchText)); } else { fillData(esHelper.getOnlineStories()); } }
@Override protected void onResume() { SyncManager.sync(this); super.onResume(); ArrayList<Story> result = esHelper.getOnlineStories(); boolean connected = InternetDetector.connectedToInternet(getApplicationContext()); while (result == null && connected) { result = esHelper.getOnlineStories(); connected = InternetDetector.connectedToInternet(getApplicationContext()); } if (!connected) { Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_LONG).show(); finish(); } /* Re-populate the listview with the online stories */ fillData(result); }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); /* * Populate the listview with the online stories at the start of the * activity */ fillData(esHelper.getOnlineStories()); }
/** * This method is called from a button click that allows the user to ask for a random Online Story * to be viewed for their reading pleasure. It will get a random story and pass this story to the * next activity for displaying to the user. Once this is complete the user should be presented * with a display of the first page of the story. * * @param view The screen used to display the Online Story list for the user. * @throws Exception */ public void onClickFeelingLuckButton(View view) { /* Generate and get a random Story for the user */ ArrayList<Story> storyList = esHelper.getOnlineStories(); if (storyList.size() > 0) { int randomStoryIndex = StoryController.feelingLucky(storyList.size()); Story randomStory = storyList.get(randomStoryIndex); int storyId = randomStory.getOnlineStoryId(); Story rightStory = esHelper.getOnlineStory(storyId); Intent firstStoryFragment = new Intent(getApplicationContext(), StoryFragmentActivity.class); /* * decode the Story so that the photos are returned to their normal * format */ try { Decoder decoder = new Decoder(this); currentStory = decoder.decodeStory(rightStory, 0); } catch (IOException e) { Log.d(TAG, e.getLocalizedMessage()); } catch (Exception e) { Log.d(TAG, e.getLocalizedMessage()); } firstStoryFragment.putExtra("story", currentStory); int nextStoryFragmentId = currentStory.getFirstStoryFragmentId(); /* send the first story fragment id through the intent */ firstStoryFragment.putExtra("storyFragmentId", nextStoryFragmentId); firstStoryFragment.putExtra("AnnotationMode", 0); firstStoryFragment.putExtra("FileHelperMode", -1); /* * start the StoryFragmentActivity to display the first fragment of * the selected story */ startActivity(firstStoryFragment); } }
public boolean onContextItemSelected(MenuItem item) { info = (AdapterContextMenuInfo) item.getMenuInfo(); position = info.position; /* * Get the story object of the selected story item */ currentStory = (Story) lv.getAdapter().getItem(position); switch (item.getItemId()) { case DOWNLOAD_ID: fHelper = new FileHelper(this, 0); Toast.makeText(getApplicationContext(), "Downloading...Please wait", Toast.LENGTH_LONG) .show(); try { Decoder decoder = new Decoder(this); currentStory = esHelper.getOnlineStory(currentStory.getOnlineStoryId()); currentStory = decoder.decodeStory(currentStory, 1); } catch (IOException e1) { e1.printStackTrace(); } catch (Exception e1) { e1.printStackTrace(); } try { /* * Save the story to file, via FileHelper if the download option * selected */ if (fHelper.addOfflineStory(currentStory)) { Toast.makeText( getApplicationContext(), "Selected Story is Downloaded.You have " + Integer.toString(fHelper.getOfflineStories().size()) + " stories.", Toast.LENGTH_LONG) .show(); } else { Toast.makeText( getApplicationContext(), "Network problem. Please check your network and try again.", Toast.LENGTH_LONG) .show(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; case READ_ID: /* * create intent to pass the selected story object and the first * story fragment id to the StoryFragmentActivity create intent to * pass the selected story object and the first story fragment id to * the StoryFragmentActivity */ Intent firstStoryFragment = new Intent(getApplicationContext(), StoryFragmentActivity.class); Toast.makeText(getApplicationContext(), "Loading...Please wait", Toast.LENGTH_LONG).show(); /* send the story object through the intent */ try { Decoder decoder = new Decoder(this); currentStory = esHelper.getOnlineStory(currentStory.getOnlineStoryId()); currentStory = decoder.decodeStory(currentStory, 0); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } Toast.makeText(getApplicationContext(), "Ready to read..", Toast.LENGTH_SHORT).show(); firstStoryFragment.putExtra("story", currentStory); int nextStoryFragmentId = currentStory.getFirstStoryFragmentId(); /* send the first story fragment id through the intent */ firstStoryFragment.putExtra("storyFragmentId", nextStoryFragmentId); firstStoryFragment.putExtra("AnnotationMode", 0); firstStoryFragment.putExtra("FileHelperMode", -1); /* * start the StoryFragmentActivity to display the first fragment of * the selected story */ startActivity(firstStoryFragment); return true; default: return super.onContextItemSelected(item); } }