/** Factory method that returns an explicit Intent for downloading an image. */ public static Intent makeIntent( Context context, int requestCode, Uri url, Uri directoryPathname, Handler downloadHandler) { // Create an intent that will download the image from the web. // TODO -x- you fill in here, replacing "null" with the proper // code, which involves (1) creating a RequestMessage // containing the various parameters passed into this method // and (2) storing this RequestMessage as a Message "extra" in // the Intent. RequestMessage requestMessage = RequestMessage.makeRequestMessage( requestCode, url, directoryPathname, new Messenger(downloadHandler)); Intent intent = new Intent(context, DownloadImagesStartedService.class); intent.putExtra(REQUEST_MESSAGE, requestMessage.getMessage()); return intent; }
/** Send the @a pathToImageFile back to the ImageModelImp's Handler via the @a Messenger. */ private void sendPath(Messenger messenger, Uri pathToImageFile, Uri url, int requestCode) { // Call the makeReplyMessage() factory method to create // Message. // TODO -x- you fill in here. RequestMessage requestMessage = RequestMessage.makeRequestMessage(requestCode, url, pathToImageFile, messenger); try { // Send the path to the image file back to the // ImageModelImpl's Handler via the Messenger. // TODO -x- you fill in here. requestMessage.getMessenger().send(requestMessage.getMessage()); } catch (RemoteException e) { Log.e(getClass().getName(), "Exception while sending reply message back to Activity.", e); } }
/** * Hook method dispatched by the IntentService framework to download the image requested via data * in an intent, store the image in a local file on the local device, and return the image file's * URI back to the ImageModelImpl's Handler via the Messenger passed with the intent. */ @Override public void onHandleIntent(Intent intent) { // Extract the RequestMessage from the intent. final RequestMessage requestMessage = RequestMessage.makeRequestMessage((Message) intent.getParcelableExtra(REQUEST_MESSAGE)); // Extract the URL for the image to download. // TODO -x- you fill in here. Uri uri = requestMessage.getImageURL(); // Download the requested image. // TODO -x??- you fill in here. Uri path = requestMessage.getDirectoryPathname(); // Extract the request code. // TODO -x- you fill in here. int requestCode = requestMessage.getRequestCode(); // Extract the Messenger stored as an extra in the // intent under the key MESSENGER. // TODO -x- you fill in here. Messenger messenger = requestMessage.getMessenger(); // Send the path to the image file back to the // MainActivity via the messenger. // TODO -x- you fill in here. sendPath(messenger, path, uri, requestCode); }