English subtitles

← The State of Things

Get Embed Code
4 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. We've improved the initial experience
    feedback in the case of poor
  2. connectivity, but what happens if
    the weather server goes down or
  3. if from behind a captive portal?
  4. In that case, we could get either no
    data or some form of corrupted data and
  5. the user still will have
    no idea what is happening.
  6. Let's start by enumerating the states,
  7. we want to communicate
    with the user about.
  8. While Java supports enumerated types,
    they're somewhat expensive and
  9. inefficient by comparison to using
    static final integer constants.
  10. So at Android,
    we recommend avoiding them.
  11. That being said, it would be nice
    to have some type safety and
  12. that's where the annotations we've added
    to the Android Support Library can help.
  13. Well, first of all,
    what are annotations?
  14. In Java, annotations are specified
    beginning with an @ sign,
  15. such as the commonly
    used @override here.
  16. They contain information or metadata
    that gets associated with the code in
  17. our app, but do not directly change
    the functionality of the code.
  18. They may be used by
    development tools and
  19. depending on the way they are retained,
    may be read at runtime.
  20. To use the support annotations,
    we just go to Android Studio
  21. to the Project Structure dialog and
    add the support annotations dependency.
  22. Since the integer we are storing for
    our state can only have a few
  23. predefined values, it would be nice for
    our tools to be able to help us.
  24. Cache assigning unsupported
    value to our state,
  25. an auto complete the typing
    of variables of this type.
  26. The support annotations and
  27. library provides functionality that does
    just that using the IntDef annotation.
  28. Here's how this works.
  29. When we define our integer constants,
    we also create an interface annotation.
  30. As in this example from inside
    the app from PAD Library,
  31. we then annotate this new
    annotation with IntDef,
  32. containing the different integer
    constants that are supported.
  33. The retention policy of source tells
    the tool chain that we do not need
  34. to preserve this annotation
    in the class or at runtime.
  35. Making the annotation not have an impact
    on our runtime or distribution sides.
  36. The IDE will now flag cases where we try
    to return a value not in this set from
  37. getNavigationMode or
  38. pass an integer not from this set
    into this set, @NavigationModeMethod.
  39. So what location states do
    we need to think about?
  40. The first stage is that
    everything is okay.
  41. The second stage is that
    our server is down.
  42. The third stage is that our
    server is malfunctioning.
  43. The fourth stage is that we don't
    know if the server is up or
  44. down, because we haven't
    tried to contact it yet.
  45. We'll continue to add states here, as we
    work to make our app production ready.