the android winds of change
play

The Android winds of change From Kit-Kat to L, and the power of - PowerPoint PPT Presentation

The Android winds of change From Kit-Kat to L, and the power of saving power Why are you here? Info on the new IDE, and setting up projects Want to know the changes L brings to your Kit-Kat apps How to analyse your apps


  1. The Android winds of change From Kit-Kat to L, and the power of saving power

  2. Why are you here? • Info on the new IDE, and setting up projects � • Want to know the changes L brings to your Kit-Kat apps � • How to analyse your app’s performance

  3. Android • Java based � • Approximately annual upgrade � • Previous version: Kit-Kat � • Based on Holo design � • Next version: Something starting with L � • Based on Material design

  4. The app: Pokedex • Master/Detail view of Pokemon � • Originally designed for Android Kit-Kat

  5. The migration • Change the structure of the app � • Style it � • Improve performance

  6. The IDE & project setup

  7. A comparison • Used Eclipse with ADT plugin to develop � • Ant to build, especially via CI � • Manual dependency management Build Tool Dependency Updates Management Eclipse 
 Ant Manual via 
 ADT plugin not with ADT lib imports actively being updated Android Gradle Automatic 
 Constantly being Studio via Gradle updated with new IDE features

  8. What do I need to change? <manifest xmlns:android="http://schemas.android.com/apk/res/android"> � … � � <uses-sdk � android:minSdkVersion="15" � android:targetSdkVersion="18" /> � � … � </manifest>

  9. What do I need to change? android { � compileSdkVersion 'android-L' � buildToolsVersion '20.0.0' � defaultConfig { � targetSdkVersion 'L' � } � } dependencies { � compile 'com.android.support:support-v4:+' � compile 'com.android.support:cardview-v7:+' � compile 'com.android.support:recyclerview-v7:+' � compile 'com.android.support:palette-v7:+' � }

  10. New components

  11. RecyclerView • More advanced and flexible than ListView � • Forces the ViewHolder pattern � • Recycling process is more efficient � • Requires an external LayoutManager

  12. RecyclerView in action < android.support.v7.widget.RecyclerView 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id=“@+id/my_recycler_view"> � </android.support.v7.widget.RecyclerView>

  13. 
 RecyclerView in action protected void onCreate(Bundle savedInstanceState) { 
 // Set the content view 
 … 
 RecyclerView recyclerView = 
 � � (RecyclerView)findViewById(R.id.my_recycler_view); � RecyclerView.LayoutManager layoutManager = 
 � � � � new LinearLayoutManager(this); 
 recyclerView.setLayoutManager(layoutManager); � … 
 // Setup adapter 
 }

  14. ViewHolder pattern? View scrolls off screen View is held off screen Scroll RecyclerView up View is recycled when needed avoiding the need 
 to recreate the View, thus improving app performance

  15. Implementing the ViewHolder pattern Create a RecyclerView.ViewHolder 1 Bind the data to the Create the 4 2 element’s view in RecyclerView.Adapter onBindViewHolder() to use the ViewHolder 3 Inflate the element’s view in RecyclerView.Adapter’s onCreateViewHolder()

  16. Implementing the ViewHolder pattern Create a RecyclerView.ViewHolder 1 Bind the data to the Create the 4 2 element’s view in RecyclerView.Adapter onBindViewHolder() to use the ViewHolder 3 Inflate the element’s view in RecyclerView.Adapter’s onCreateViewHolder()

  17. Implementing the ViewHolder pattern Create a RecyclerView.ViewHolder 1 Bind the data to the Create the 4 2 element’s view in RecyclerView.Adapter onBindViewHolder() to use the ViewHolder 3 Inflate the element’s view in RecyclerView.Adapter’s onCreateViewHolder()

  18. Implementing the ViewHolder pattern Create a RecyclerView.ViewHolder 1 Bind the data to the Create the 4 2 element’s view in RecyclerView.Adapter onBindViewHolder() to use the ViewHolder 3 Inflate the element’s view in RecyclerView.Adapter’s onCreateViewHolder()

  19. How does this look? public class PokedexHolder extends RecyclerView.ViewHolder { � public CardView card; � public ImageView image; � public TextView text; � � public PokedexHolder(View v) { � super(v); � card = (CardView) v.findViewById(R.id.card_view); � image = (ImageView) v.findViewById(R.id.img_image); � text = (TextView) v.findViewById(R.id.txt_name); � } � }

  20. How does this look? public class PokemonArrayAdapter 
 � � extends RecyclerView.Adapter<PokedexHolder> 
 � � implements View.OnClickListener { � @Override � public PokedexHolder onCreateViewHolder(ViewGroup viewGroup, int pos) { � View v = LayoutInflater.from(viewGroup.getContext()) 
 � � � .inflate(R.layout.row_pokemon, viewGroup, false); � PokedexHolder holder = new PokedexHolder(v); � holder.card.setOnClickListener(this); 
 return holder; � } � … � }

  21. How does this look? pubic class PokemonAdapter { � … � public void onBindViewHolder( 
 � � PokedexHolder pokedexHolder, int position) { � Pokemon p = getPokemonFromList(position); � � pokedexHolder.image.setImageDrawable( 
 � � context.getResources().getDrawable(p.getImage())); � pokedexHolder.text.setText(p.getName()); � } � }

  22. How does this look? protected void onCreate(Bundle savedInstanceState) { � � � � PokemonArrayAdapter adapter = 
 � � � new PokemonArrayAdapter(this, items); � � � recyclerView.setAdapter(adapter); � }

  23. CardView • Material design > Paper > CardView � • Mobile, Tablet, Wearables(Glass, Watch), TV, Auto � • Rounded corners � • Elevations / Shadows

  24. 
 
 
 
 CardView <android.support.v7.widget.CardView 
 xmlns:card_view="http://schemas.android.com/apk/res-auto" 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 card_view:cardCornerRadius="4dp"> 
 <!-- Insert card layout here --> 
 </android.support.v7.widget.CardView> �

  25. Progress

  26. Material design: What is it?

  27. What is Material design? • More paper look and feel � • To provide a way to the user to relate to elements on devices using: � • cards � • shadows � • elevations

  28. How does it differ from Kit-Kat? • Bright bold colours vs splashes of colour � • Motion to indicate actions have been performed � • Custom palettes � • Elevations and shadows

  29. Themes • Themes aren’t new � • Base theme: “Theme.material” � • Dark, Light, Light with DarkActionBar

  30. Colours • Wider gamut of colours � • Combinations of primary and secondary colours

  31. Palettes & named colours • Custom palettes � • Named colours

  32. Changing the Pokedex theme

  33. Changing the Pokedex theme <resources> � <!-- Base application theme. --> � <style name=“AppTheme" parent="android:Theme.Material.Light"> � <item name="android:colorPrimary">@color/red</item> � <item name="android:textColorPrimary">@color/white</item> � <item name="android:colorPrimaryDark">@color/dark_red</item> � <item name="android:windowBackground">@color/green</item> � <item name="android:navigationBarColor">@color/red</item> � </style> � </resources>

  34. 
 
 Adding custom colours public void onBindViewHolder(...) { 
 Palette palette = Palette.generate(image); � if ( palette.getDarkMutedColor() != null ) { � holder.text.setBackgroundColor( � � � palette.getDarkMutedColor().getRgb() ); 
 } 
 if ( palette.getVibrantColor() != null ) { � holder.text.setTextColor( � � � palette.getVibrantColor().getRgb() ); 
 } 
 }

  35. Visually complete!

  36. But the battery is draining quickly…

  37. But the battery is draining quickly… We need to fix this before 
 it goes to the Play Store!

  38. Battery Performance

  39. Project Volta • Performance improvements in Android platform � • Tools to analyse application efficiency � • Increase user awareness of power consumption

  40. Project Volta Waking a device for 1 sec = 2 mins of standby time lost

  41. Battery stats adb shell dumpsys batterystats --charged <package-name>

  42. Battery Historian Python script used to create an HTML visualisation 
 of the data obtained from batterystats command Battery Battery Stats HTML Visualisation Historian

  43. What does the output look like?

  44. What can we infer from this graph?

  45. What can we infer from this graph?

  46. Pokedex Performance

  47. Power Management Strategies

  48. Being lazy is good • Make apps Lazy first � • Reduce number of time app is active � • Coalesce actions together � • Defer actions to a later time � • eg: when charging

  49. Consider this What is the longest time I am willing 
 to wait to complete this task?

  50. JobScheduler • Allows you to schedule jobs to occur at a later time.

  51. What is a job? • A job is a non user-facing network call 
 eg: � • CPU intensive operations you’d prefer when the user isn’t around � • Some job you need to perform periodically, or in the future

  52. 
 
 
 
 
 Create a job example #1 Every fifteen hours, perform some database clean-up and upload some logs to the server � �

Recommend


More recommend