Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building Apps for Android Auto

Arnav Gupta
September 16, 2016

Building Apps for Android Auto

Arnav Gupta

September 16, 2016
Tweet

More Decks by Arnav Gupta

Other Decks in Technology

Transcript

  1. . . . and soon on your toaster Well .

    . . Maybe quite not yet
  2. Chrysler Jeep Hack Wired Magazine, July 2015 Charlie Miller and

    Chris Valasek Hacked a Chrysler Jeep and took control of steering and throttle. July, 2015 (Not using Android Auto)
  3. Building Apps for Android Auto š Right now – only

    two types of Apps : š Audio Apps – Playback to car’s audio system š Messaging Apps – Text communication using voice commands š Android Auto v1 Does not have OBD/CAN bus data available in an API š Cannot control anything other than audio/video output for now
  4. Building Apps for Android Auto š targetSdkVersion >= 21 š

    minSdkVersion >= 21 š Does NOT work with Lollipop phones š Needs Android v4 Support Library
  5. Designing for Android Auto š Simple UI š Large buttons

    š Max depth = 6 š No ”BACK” button
  6. Designing for Android Auto š Simple UI š Large buttons

    š Max depth = 6 š No ”BACK” button
  7. Distraction Free Design š More options = more car accidents

    š Small buttons = more accidents š Low contrast = more accidents š Scrolling = more accidents š Video/Picture ads = u kidding bro ?
  8. The navigation activity bar š Buttons – š Navigation š

    Communication š Home š Media š Car Dash š Single tap = default app, Double tap = optional apps
  9. Testing Android Auto š Install the Android “Desktop Head Unit

    Simulator” in Android SDK š Install the “Android Auto App” on your Phone š Connect phone to PC
  10. Testing Android Auto š adb forward tcp:5277 tcp:5277 š cd

    <sdk>/extras/google/auto š ./desktop-head-unit
  11. Add your Service to Manifest <service android:name=".MyMusicService" android:exported="true"> <intent-filter> <action

    android:name="android.media.browse.MediaBrowserService" /> </intent-filter> </service>
  12. Register a session for Playback @Override public void onCreate() {

    super.onCreate(); mSession = new MediaSession(this, "MyMusicService"); setSessionToken(mSession.getSessionToken()); mSession.setCallback(new MediaSessionCallback()); mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS); }
  13. Create Receivers and Service public class MessageReadReceiver extends BroadcastReceiver {

    … } public class MessageReplyReceiver extends BroadcastReceiver { … } public class MyMessagingService extends Service { … }
  14. Register them <service android:name=".MyMessagingService"></service> <receiver android:name=".MessageReadReceiver"> <intent-filter> <action android:name="com.codingblocks.exampleandroidauto.ACTION_MESSAGE_READ" />

    </intent-filter> </receiver> <receiver android:name=".MessageReplyReceiver"> <intent-filter> <action android:name="com.codingblocks.exampleandroidauto.ACTION_MESSAGE_REPLY" /> </intent-filter> </receiver>
  15. When message arrives @Override public void onReceive(Context context, Intent intent)

    { if (MyMessagingService.READ_ACTION.equals(intent.getAction())) { int conversationId = intent.getIntExtra(MyMessagingService.CONVERSATION_ID, -1); if (conversationId != -1) { Log.d(TAG, "Conversation " + conversationId + " was read"); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); notificationManager.cancel(conversationId); } } }
  16. When user replies @Override public void onReceive(Context context, Intent intent)

    { if (MyMessagingService.REPLY_ACTION.equals(intent.getAction())) { int conversationId = intent.getIntExtra(MyMessagingService.CONVERSATION_ID, -1); CharSequence reply = getMessageText(intent); Log.d(TAG, "Got reply (" + reply + ") for ConversationId " + conversationId); } }
  17. Get the user’s reply text from voice private CharSequence getMessageText(Intent

    intent) { Bundle remoteInput = RemoteInput.getResultsFromIntent(intent); if (remoteInput != null) { return remoteInput.getCharSequence(MyMessagingService.EXTRA_VOICE_REP LY); } return null; }
  18. Create handler in Service class IncomingHandler extends Handler { @Override

    public void handleMessage(Message msg) { sendNotification(1, "This is a sample message", "John Doe", System.currentTimeMillis()); } }
  19. Start read receiver from Service // A pending Intent for

    reads PendingIntent readPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), conversationId, createIntent(conversationId, READ_ACTION), PendingIntent.FLAG_UPDATE_CURRENT);
  20. Start reply receiver from Service // Building a Pending Intent

    for the reply action to trigger PendingIntent replyIntent = PendingIntent.getBroadcast(getApplicationContext(), conversationId, createIntent(conversationId, REPLY_ACTION), PendingIntent.FLAG_UPDATE_CURRENT);
  21. Create notifications on the home screen NotificationCompat.Builder builder = new

    NotificationCompat.Builder(getApplicationContext()) //.setSmallIcon(R.drawable.notification_icon) //.setLargeIcon(personBitmap) .setContentText(message) .setWhen(timestamp) .setContentTitle(participant) .setContentIntent(readPendingIntent) .extend(new CarExtender() .setUnreadConversation(unreadConvBuilder.build())); mNotificationManager.notify(conversationId, builder.build());
  22. Read More Android Auto resources from Google http://andriod.com/auto https://developer.android.com /training/auto

    https://github.com/googlesam ples/android-MessagingService https://github.com/googlesam ples/android- MediaBrowserService