Slide 1

Slide 1 text

Challenges of Building HTC Sense David Wu [email protected] O’Reilly Android Open Conference 2011 October 11, 2011 HTC Proprietary and Confidential – Internal use and strategic partner use only © 2010 HTC Corporation. All rights reserved.

Slide 2

Slide 2 text

HTC Proprietary and Confidential 2

Slide 3

Slide 3 text

HTC Proprietary and Confidential 3

Slide 4

Slide 4 text

HTC Proprietary and Confidential 4 H , I’m D v d F l o @w m n

Slide 5

Slide 5 text

HTC Proprietary and Confidential 5

Slide 6

Slide 6 text

HTC Proprietary and Confidential 6

Slide 7

Slide 7 text

HTC Proprietary and Confidential 7

Slide 8

Slide 8 text

HTC Proprietary and Confidential 8

Slide 9

Slide 9 text

HTC Proprietary and Confidential 9 Agenda •  Social Features in HTC Sense •  Challenge 1 - Seamlessness •  Challenge 2 – Sync •  Challenge 3 – Performance

Slide 10

Slide 10 text

H C S n e HTC Proprietary and Confidential 10

Slide 11

Slide 11 text

HTC Proprietary and Confidential 11

Slide 12

Slide 12 text

HTC Proprietary and Confidential 12 H C S n e

Slide 13

Slide 13 text

HTC Proprietary and Confidential 13 S o k A d o d

Slide 14

Slide 14 text

HTC Proprietary and Confidential 14

Slide 15

Slide 15 text

S c a E p r e c HTC Proprietary and Confidential 15

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

C n a t S c a U d t s

Slide 18

Slide 18 text

C n a t S c a U d t s

Slide 19

Slide 19 text

C n a t S c a U d t s

Slide 20

Slide 20 text

HTC Proprietary and Confidential 20 F i n C a n l

Slide 21

Slide 21 text

HTC Proprietary and Confidential 21 F i n S r a

Slide 22

Slide 22 text

HTC Proprietary and Confidential 22 S c a L c s r e

Slide 23

Slide 23 text

HTC Proprietary and Confidential 23 S c a C o k

Slide 24

Slide 24 text

HTC Proprietary and Confidential 24 S c a C l n a

Slide 25

Slide 25 text

HTC Proprietary and Confidential 25

Slide 26

Slide 26 text

HTC Proprietary and Confidential 26 Agenda •  Social Features in HTC Sense •  Challenge 1 - Seamlessness •  Challenge 2 – Sync •  Challenge 3 – Performance

Slide 27

Slide 27 text

S a l s n s HTC Proprietary and Confidential 27

Slide 28

Slide 28 text

Host Host HTC Proprietary and Confidential 28 Host Plugin Plugin Plugin Query Request •  Aggregated data retrieval •  UI rendering •  Refresh •  Load more

Slide 29

Slide 29 text

HTC Proprietary and Confidential 29 Broadcast Intent and Class Loading •  Request with callback: •  Host broadcast Intents •  Plugin registers an IntentReceiver •  Query: •  Dynamic class loading T y #1

Slide 30

Slide 30 text

HTC Proprietary and Confidential 30 Problems •  Broadcast Intent problems: •  Overhead in receiving the Broadcast Intent •  Difficult synchronization between requests coming from different hosts •  Dynamic class loading problems: •  Host and plugins require the same UID •  Huge performance penalty due to dynamic class loading via Java Reflection •  Fat host processes •  Debugging difficulty •  Unintuitive control code for dynamic content filtering

Slide 31

Slide 31 text

HTC Proprietary and Confidential 31 Binder IPC and ContentProvider •  Request with callback: •  Remote binder call with callback mechanism •  Query: •  ContentProvider with ContentObserver •  Standardized data types •  Limited UI rendering with markup languages T y #2

Slide 32

Slide 32 text

HTC Proprietary and Confidential 32 Problems •  Database access performance issues •  Schema optimization via indexing •  Query optimization •  Prepared statements •  Transactions •  Reduce disk I/O by implementing a memcache layer •  Multiple ContentObservers may make the system unusually busy •  Use dirty flag and do lazy requery

Slide 33

Slide 33 text

S n HTC Proprietary and Confidential 33

Slide 34

Slide 34 text

HTC Proprietary and Confidential 34 Problems with Background Sync •  Bandwidth usage •  Battery usage

Slide 35

Slide 35 text

HTC Proprietary and Confidential 35 Important Problem with Trade-Off Solutions •  Can’t disclose actual sales numbers, but to put things in perspective: •  2 out of every 5 Android phones sold is an HTC •  1 out of every 2 Windows phones sold is an HTC •  Every 0.68 sec there is a new HTC phone activated •  Who cares? •  Network operators •  Online services •  Users

Slide 36

Slide 36 text

HTC Proprietary and Confidential 36 Improving Bandwidth and Battery Usage •  Monitor device states •  Partial sync •  Smart sync

Slide 37

Slide 37 text

HTC Proprietary and Confidential 37 Monitoring Device States •  Network connectivity •  Online vs. Offline •  Roaming vs. Not Roaming •  WiFi vs. GPRS/EDGE •  Battery level •  Storage capacity •  The less free space left, the higher the disk latency •  AlarmManager intervals •  Best interval (sleep) vs. Minimum interval (wakeup) •  Check out Reto Meier’s “Android Protips” session at Google I/O 2011

Slide 38

Slide 38 text

HTC Proprietary and Confidential 38 Partial Sync •  Sync only important data •  Only contacts explicitly marked as “favorite” or with frequent interactivities

Slide 39

Slide 39 text

HTC Proprietary and Confidential 39 Partial Sync •  Sync only important data •  Only contacts explicitly marked as “favorite” or with frequent interactivities •  Only a limited number of most recent stream posts Current time

Slide 40

Slide 40 text

HTC Proprietary and Confidential 40 Partial Sync •  Sync only important data •  Only contacts explicitly marked as “favorite” or with frequent interactivities •  Only a limited number of most recent stream posts Current time

Slide 41

Slide 41 text

HTC Proprietary and Confidential 41 Partial Sync •  Sync only important data •  Only contacts explicitly marked as “favorite” or with frequent interactivities •  Only a limited number of most recent stream posts Current time Current time

Slide 42

Slide 42 text

HTC Proprietary and Confidential 42 Partial Sync •  Avoid fetching or writing the same data more than once •  Use hash to speed up data comparisons

Slide 43

Slide 43 text

HTC Proprietary and Confidential 43 Partial Sync •  Avoid fetching or writing the same data more than once •  Use hash to speed up data comparisons •  Deep integration with native clients •  Facebook vs. Facebook for HTC Sense

Slide 44

Slide 44 text

HTC Proprietary and Confidential 44 Smart Sync •  Use application heuristics •  Sync if and only if •  At least one widget is installed on the Home screen •  It has been at least one hour since the last sync •  Do not sync if historically the stream timeline has sparse data

Slide 45

Slide 45 text

P r o m n e HTC Proprietary and Confidential 45

Slide 46

Slide 46 text

HTC Proprietary and Confidential 46 Outline for Improving Performance •  Aggregated content retrieval from multiple data sources •  Improving application launch time •  Scrolling performance •  General coding practices

Slide 47

Slide 47 text

HTC Proprietary and Confidential 47 Aggregated Content Retrieval Host Host Application Data Source Data Source Data Source Sorted

Slide 48

Slide 48 text

HTC Proprietary and Confidential 48 Aggregated Content Retrieval •  Aggregated content retrieval from multiple data sources •  Quick sort •  Need all content to be in memory •  Long startup delay

Slide 49

Slide 49 text

HTC Proprietary and Confidential 49 Aggregated Content Retrieval •  Aggregated content retrieval from multiple data sources •  Quick sort •  Need all content to be in memory •  Long startup delay •  Merge sort •  Make use of SQLite indexed sorting capability •  Maintain a min/max heap and a set of position markers for each data source •  Minimal startup delay •  Amortized sorting cost, which is barely noticeable

Slide 50

Slide 50 text

HTC Proprietary and Confidential 50 Aggregated Content Retrieval •  Aggregated content retrieval from multiple data sources •  Quick sort •  Need all content to be in memory •  Long startup delay •  Merge sort •  Make use of SQLite indexed sorting capability •  Maintain a min/max heap and a set of position markers for each data source •  Minimal startup delay •  Amortized sorting cost, which is barely noticeable •  Reduces application launch time from 1800ms to 600ms in FriendStream

Slide 51

Slide 51 text

HTC Proprietary and Confidential 51 Aggregated Content Retrieval •  Aggregated content retrieval from multiple data sources •  Quick sort •  Need all content to be in memory •  Long startup delay •  Merge sort •  Make use of SQLite indexed sorting capability •  Maintain a min/max heap and a set of position markers for each data source •  Minimal startup delay •  Amortized sorting cost, which is barely noticeable •  Reduces application launch time from 1800ms to 600ms in FriendStream •  Same technique used in Contacts, Messages, Calendar, etc.

Slide 52

Slide 52 text

HTC Proprietary and Confidential 52 Outline for Improving Performance •  Aggregated content retrieval from multiple data sources •  Improving application launch time •  Scrolling performance •  General coding practices

Slide 53

Slide 53 text

HTC Proprietary and Confidential 53 Application Launch Times •  Perceived launch time (fake screen) • android:windowBackground=“@drawable/fakebg”!

Slide 54

Slide 54 text

HTC Proprietary and Confidential 54 Application Launch Times •  Perceived launch time (fake screen) • android:windowBackground=“@drawable/fakebg”! • getWindow().setBackgroundDrawable(null);!

Slide 55

Slide 55 text

HTC Proprietary and Confidential 55 Application Launch Times •  Perceived launch time (fake screen) • android:windowBackground=“@drawable/fakebg”! • getWindow().setBackgroundDrawable(null);! •  Reduce layout complexity

Slide 56

Slide 56 text

HTC Proprietary and Confidential 56 Application Launch Times •  Perceived launch time (fake screen) • android:windowBackground=“@drawable/fakebg”! • getWindow().setBackgroundDrawable(null);! •  Reduce layout complexity •  Optimize database query •  Minimize fillWindow time •  Only select columns that are needed •  Use limits •  Profile SQLite queries

Slide 57

Slide 57 text

HTC Proprietary and Confidential 57 Prepared Statements •  Precompiling frequently used SQL statements String sql = “INSERT INTO table VALUES (?, ?)”;! SQLiteStatement stmt = mDatabase.compileStatement(sql);! ! DatabaseUtils.bindObjectToProgram(stmt, 1, 1);! DatabaseUtils.bindObjectToProgram(stmt, 2, 2);! ! stmt.execute();! stmt.close();!

Slide 58

Slide 58 text

HTC Proprietary and Confidential 58 Outline for Improving Performance •  Aggregated content retrieval from multiple data sources •  Improving application launch time •  Perceived launch time •  Database query optimization •  Prepared statements •  Transactions •  Scrolling performance •  General coding practices

Slide 59

Slide 59 text

HTC Proprietary and Confidential 59 Improve Scrolling Performance •  Minimize getView() and draw() •  Check your ListAdapter.getView() •  Use convertViews •  Cache findViewById() results •  Do not set a Window background •  Avoid runtime scaling of images •  Reuse byte buffer in BitmapFactory.decodeStream() to avoid unnecessary GC

Slide 60

Slide 60 text

HTC Proprietary and Confidential 60 Outline for Improving Performance •  Aggregated content retrieval from multiple data sources •  Improving application launch time •  Perceived launch time •  Database query optimization •  Prepared statements •  Transactions •  Scrolling performance •  General coding practices

Slide 61

Slide 61 text

HTC Proprietary and Confidential 61 General Coding Tips •  Use System.arraycopy • for (int i = 0; i

Slide 62

Slide 62 text

HTC Proprietary and Confidential 62 General Coding Tips •  Use System.arraycopy • for (int i = 0; i

Slide 63

Slide 63 text

HTC Proprietary and Confidential 63 General Coding Tips •  Use System.arraycopy • for (int i = 0; i

Slide 64

Slide 64 text

HTC Proprietary and Confidential 64 D v d W @w m n d v d@w -m n.c m b o .w -m n.c m

Slide 65

Slide 65 text

HTC Proprietary and Confidential 65