/** * 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); } }