Hitchikers guide to Modern Android Development

65
Murat Yener @yenerm Hitchhikers Guide to Modern Android Development

Transcript of Hitchikers guide to Modern Android Development

Page 1: Hitchikers guide to Modern Android Development

Murat Yener @yenerm

Hitchhikers Guide to Modern Android Development

Page 2: Hitchikers guide to Modern Android Development

Murat Yener @yenerm

Hitchhikers Guide to Modern Android Development

Page 3: Hitchikers guide to Modern Android Development

Soo, who am I?!?

Java, Web, Mobile, GWT, Flex Developer Android Developer at Intel Eclipse Comitter Book Author (Java EE Design Patterns) GDG Istanbul Organizer since 2009 Conference Speaker (JavaOne, Devoxx, EclipseCon..)

Page 4: Hitchikers guide to Modern Android Development

Soo, who am I?!?

Java, Web, Mobile, GWT, Flex Developer Android Developer at Intel Eclipse Comitter Book Author (Java EE Design Patterns) GDG Istanbul Organizer since 2009 Conference Speaker (JavaOne, Devoxx, EclipseCon..)

Page 5: Hitchikers guide to Modern Android Development

ABOUT USLorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat it.

Page 6: Hitchikers guide to Modern Android Development

SOCIAL MEDIALorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat it.

History of Android

Page 7: Hitchikers guide to Modern Android Development

5

pre-historic (cupcake, donut)

Page 8: Hitchikers guide to Modern Android Development

6

genesis (eclair, froyo, gingerbread)

Page 9: Hitchikers guide to Modern Android Development

7

(r)evolution (>4.0)

Page 10: Hitchikers guide to Modern Android Development

8

modern era

5.0 APIS

Android StudioMaterial Design

Wear

Gradle Libraries

Page 11: Hitchikers guide to Modern Android Development

9

Android OS

• Notifications (metadata)

• Camera 2.0

• Job Scheduler

• Printing Framework

• Multiple Sim (>=5.1)

Page 12: Hitchikers guide to Modern Android Development

9

Android OS

• Notifications (metadata)

• Camera 2.0

• Job Scheduler

• Printing Framework

• Multiple Sim (>=5.1)

VISIBILITY_PRIVATE VISIBILITY_PUBLIC VISIBILITY_SECRET

Page 13: Hitchikers guide to Modern Android Development

9

Android OS

• Notifications (metadata)

• Camera 2.0

• Job Scheduler

• Printing Framework

• Multiple Sim (>=5.1)

Page 14: Hitchikers guide to Modern Android Development

9

Android OS

• Notifications (metadata)

• Camera 2.0

• Job Scheduler

• Printing Framework

• Multiple Sim (>=5.1)

CameraManager manager = (CameraManager) getSystemService(CAMERA_SERVICE);

manager.openCamera(cameraId, mStateCallback, null);

private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() { @Override public void onOpened(CameraDevice camera) { try { mPreviewBuilder = camera(CameraDevice.TEMPLATE_PREVIEW); } catch (CameraAccessException e){ e.printStackTrace(); } //… try { mCameraDevice.createCaptureSession(Arrays.asList(surface), mPreviewStateCallback, null); } catch (CameraAccessException e) { e.printStackTrace();} } };

private CameraCaptureSession.StateCallback mPreviewStateCallback = new CameraCaptureSession.StateCallback() { @Override public void onConfigured(CameraCaptureSession session) {…} };

Page 15: Hitchikers guide to Modern Android Development

9

Android OS

• Notifications (metadata)

• Camera 2.0

• Job Scheduler

• Printing Framework

• Multiple Sim (>=5.1)

Page 16: Hitchikers guide to Modern Android Development

9

Android OS

• Notifications (metadata)

• Camera 2.0

• Job Scheduler

• Printing Framework

• Multiple Sim (>=5.1)

JobInfo uploadTask = new JobInfo.Builder(mJobId,

mServiceComponent /* JobService component */)

.setRequiredNetworkCapabilities(JobInfo.NetworkType.UNMETERED)

.build();

JobScheduler jobScheduler =

(JobScheduler)

context.getSystemService(Context.JOB_SCHEDULER_SERVICE);

jobScheduler.schedule(uploadTask);

Page 17: Hitchikers guide to Modern Android Development

9

Android OS

• Notifications (metadata)

• Camera 2.0

• Job Scheduler

• Printing Framework

• Multiple Sim (>=5.1)

Page 18: Hitchikers guide to Modern Android Development

9

Android OS

• Notifications (metadata)

• Camera 2.0

• Job Scheduler

• Printing Framework

• Multiple Sim (>=5.1)

mPdfRenderer = new PdfRenderer(mFileDescriptor);

int x=mPdfRenderer.getPageCount();

mCurrentPage = mPdfRenderer.openPage(x);

bitmap = Bitmap.createBitmap(mCurrentPage.getWidth(),

mCurrentPage.getHeight(),Bitmap.Config.ARGB_8888);

mCurrentPage.render(bitmap, null, null,

PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);

Page 19: Hitchikers guide to Modern Android Development

9

Android OS

• Notifications (metadata)

• Camera 2.0

• Job Scheduler

• Printing Framework

• Multiple Sim (>=5.1)

Page 20: Hitchikers guide to Modern Android Development

9

Android OS

• Notifications (metadata)

• Camera 2.0

• Job Scheduler

• Printing Framework

• Multiple Sim (>=5.1)

SubscriptionManager.OnSubscriptionsChangedListener{…}

Page 21: Hitchikers guide to Modern Android Development

9

Android OS

• Notifications (metadata)

• Camera 2.0

• Job Scheduler

• Printing Framework

• Multiple Sim (>=5.1)

Page 22: Hitchikers guide to Modern Android Development

10

Material Design

A material metaphor is the unifying theory of a rationalized space and a system of motion. The material is grounded in tactile reality, inspired by the

study of paper and ink, yet technologically advanced and open to imagination and magic..

Page 23: Hitchikers guide to Modern Android Development

11

Surfaces

• Paper like surfaces

• Multiple layers (up to 10)

• Scrolls till end or behind

Page 24: Hitchikers guide to Modern Android Development

12

Layout

• 2 Keylines..

• Keyline 1 @ 16dp

• Keyline 2 @ 72dp

Page 25: Hitchikers guide to Modern Android Development

13

Colors and Theming

• Main and accent color

• Primary: toolbars

• Primary.Dark: status bar

Page 26: Hitchikers guide to Modern Android Development

14

Palette API

• Get color palette from your pics

• Vibrant, Muted

• Swatch

Page 27: Hitchikers guide to Modern Android Development

14

Palette API

• Get color palette from your pics

• Vibrant, Muted

• Swatch

PaletteItem darkMuted = palette.getDarkMutedColor();

PaletteItem muted = palette.getMutedColor();

PaletteItem lightMuted = palette.getLightMutedColor();

PaletteItem vibrant = palette.getVibrantColor();

PaletteItem lightVibrant = palette.getLightVibrantColor();

PaletteItem darkVibrant = palette.getDarkVibrantColor();

Page 28: Hitchikers guide to Modern Android Development

14

Palette API

• Get color palette from your pics

• Vibrant, Muted

• Swatch

Page 29: Hitchikers guide to Modern Android Development

14

Palette API

• Get color palette from your pics

• Vibrant, Muted

• Swatch

Palette.Swatch swatch = palette.getVibrantSwatch();

if (swatch != null) {

titleView.setBackgroundColor(swatch.getRgb());

titleView.setTextColor(swatch.getTitleTextColor());

}

Page 30: Hitchikers guide to Modern Android Development

14

Palette API

• Get color palette from your pics

• Vibrant, Muted

• Swatch

Page 31: Hitchikers guide to Modern Android Development

15

Animation

• Authentic Motion: realistic swift movement

• Responsive Interaction: respond to user

• Meaningful Transitions: physics, not random moves

• Delightful Details: small makes change

Page 32: Hitchikers guide to Modern Android Development

15

Animation

• Authentic Motion: realistic swift movement

• Responsive Interaction: respond to user

• Meaningful Transitions: physics, not random moves

• Delightful Details: small makes change

Page 33: Hitchikers guide to Modern Android Development

16

New Platforms

• teachings of Google Glass…

• Android Wear

• Android Auto

• Android TV

Page 34: Hitchikers guide to Modern Android Development

17

Google Glass

• development: GDK for native apps or Glassware for HTML

• limitations: no touch screen, limited input / battery / cpu, actions! (start a run, take a photo)

Page 35: Hitchikers guide to Modern Android Development

18

Android Wear - Notifications

Automatically works! -Phone transmits notifications to wear -You can code wear only notifications

Page 36: Hitchikers guide to Modern Android Development

19

Android Wear -Development

dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.google.android.support:wearable:+' compile 'com.google.android.gms:play-services-wearable:+' }

1

Page 37: Hitchikers guide to Modern Android Development

20

Android Wear -Development

2

<activity android:name="MyNoteActivity"> <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="com.google.android.voicesearch.SELF_NOTE" /> </intent-filter>

</activity>

Page 38: Hitchikers guide to Modern Android Development

21

private void displaySpeechRecognizer() { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

startActivityForResult(intent, SPEECH_REQUEST_CODE);

}

Android Wear -Development

3

Page 39: Hitchikers guide to Modern Android Development

22

Android Wear -Development

4

// Build the notification and add the action via WearableExtender Notification notification = new NotificationCompat.Builder(mContext) .setSmallIcon(R.drawable.ic_message) .setContentTitle(getString(R.string.title)) .setContentText(getString(R.string.content)) .extend(new WearableExtender().addAction(action)) .build();

Page 40: Hitchikers guide to Modern Android Development

23

Nexus Player - Development

<activity android:name=".TvMainActivity“ > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LEANBACK_LAUNCHER" /> </intent-filter> </activity>

compile "com.android.support:leanback-v17:21.0.+“

Page 41: Hitchikers guide to Modern Android Development

24

Android Studio - Basics

• IntelliJ based

• Graduated from beta!

• Better drag and drop UI designer

Page 42: Hitchikers guide to Modern Android Development

25

Android Studio - Gradleapply plugin: ‘com.android.application'

android { compileSdkVersion 21 buildToolsVersion "21.1.2" defaultConfig { applicationId "com.devchronicles.example" minSdkVersion 21 targetSdkVersion 21 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:palette-v7:21.0.0' }

Page 43: Hitchikers guide to Modern Android Development

26

Tools

• Android Device Monitor

• Battery Historian

• Faster Emulator

Page 44: Hitchikers guide to Modern Android Development

26

Tools

• Android Device Monitor

• Battery Historian

• Faster Emulator

Page 45: Hitchikers guide to Modern Android Development

26

Tools

• Android Device Monitor

• Battery Historian

• Faster Emulator

Page 46: Hitchikers guide to Modern Android Development

27

Libraries

“No man is an island..” -Jon Bon Jovi

Page 47: Hitchikers guide to Modern Android Development

28

Greenbus

• Event Driven

• Event dispatch via bus

• Uses reflection

• Easy/clean

compile 'de.greenrobot:eventbus:2.4.0'

Page 48: Hitchikers guide to Modern Android Development

28

Greenbus

• Event Driven

• Event dispatch via bus

• Uses reflection

• Easy/clean

public class SampleEvent { String message; }

Subscriber:

eventBus.register(this);

public void onEvent(SampleEvent event) { event.message };

Producer:

eventBus.post(event);

compile 'de.greenrobot:eventbus:2.4.0'

Page 49: Hitchikers guide to Modern Android Development

28

Greenbus

• Event Driven

• Event dispatch via bus

• Uses reflection

• Easy/clean

compile 'de.greenrobot:eventbus:2.4.0'

Page 50: Hitchikers guide to Modern Android Development

29

• From creators of greenbus

• Standart sqlite

• No ‘create table’ etc..

• Generator for model and dao

GreenDAO

compile 'de.greenrobot:greendao'

Page 51: Hitchikers guide to Modern Android Development

29

• From creators of greenbus

• Standart sqlite

• No ‘create table’ etc..

• Generator for model and dao

GreenDAO

new DaoMaster.DevOpenHelper(this, "notes-db", null);

daoMaster = new DaoMaster(db); daoSession = daoMaster.newSession(); noteDao = daoSession.getNoteDao();

Note note = new Note(null, noteText, comment, new Date()); noteDao.insert(note);

compile 'de.greenrobot:greendao'

Page 52: Hitchikers guide to Modern Android Development

29

• From creators of greenbus

• Standart sqlite

• No ‘create table’ etc..

• Generator for model and dao

GreenDAO

compile 'de.greenrobot:greendao'

Page 53: Hitchikers guide to Modern Android Development

30

JobQueue

• Easy to prioritize

• Delay

• or group jobs

compile 'com.path:android-priority-jobqueue:1.1.2'

Page 54: Hitchikers guide to Modern Android Development

30

JobQueue

• Easy to prioritize

• Delay

• or group jobs

jobManager.addJobInBackground(new PostTweetJob(status));

public class PostTweetJob extends Job { public static final int PRIORITY = 1; public PostTweetJob(String text) { super(new Params(PRIORITY).requireNetwork().persist()); } @Override public void onAdded() {} @Override public void onRun() throws Throwable { webservice.postTweet(text); } @Override protected boolean shouldReRunOnThrowable(Throwable throwable) {}

compile 'com.path:android-priority-jobqueue:1.1.2'

Page 55: Hitchikers guide to Modern Android Development

30

JobQueue

• Easy to prioritize

• Delay

• or group jobs

compile 'com.path:android-priority-jobqueue:1.1.2'

Page 56: Hitchikers guide to Modern Android Development

31

Dagger

• Dependency Injection

• Standard javax.inject

• Compile time code generation

compile 'com.squareup:dagger:1.1.2' compile 'com.squareup:dagger-compiler:1.1.2'

Page 57: Hitchikers guide to Modern Android Development

31

Dagger

• Dependency Injection

• Standard javax.inject

• Compile time code generation

class CoffeeMaker { @Inject Heater heater; @Inject Pump pump; ... }

@Module class DripCoffeeModule { @Provides Heater provideHeater() { return new ElectricHeater(); }

@Provides Pump providePump(Thermosiphon pump) { return pump; } }

compile 'com.squareup:dagger:1.1.2' compile 'com.squareup:dagger-compiler:1.1.2'

Page 58: Hitchikers guide to Modern Android Development

31

Dagger

• Dependency Injection

• Standard javax.inject

• Compile time code generation

compile 'com.squareup:dagger:1.1.2' compile 'com.squareup:dagger-compiler:1.1.2'

Page 59: Hitchikers guide to Modern Android Development

32

Retrofit

• Turns your REST api to Java Interface

• Uses OkHttp

• synchronous, asynchronous, observable

compile 'com.squareup.retrofit:retrofit:1.9.0'

Page 60: Hitchikers guide to Modern Android Development

32

Retrofit

• Turns your REST api to Java Interface

• Uses OkHttp

• synchronous, asynchronous, observable

public interface GitHubService { @GET("/users/{user}/repos") List<Repo> listRepos(@Path("user") String user); }

RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint("https://api.github.com") .build();

GitHubService service = restAdapter.create(GitHubService.class);

List<Repo> repos = service.listRepos("octocat");

compile 'com.squareup.retrofit:retrofit:1.9.0'

Page 61: Hitchikers guide to Modern Android Development

32

Retrofit

• Turns your REST api to Java Interface

• Uses OkHttp

• synchronous, asynchronous, observable

compile 'com.squareup.retrofit:retrofit:1.9.0'

Page 62: Hitchikers guide to Modern Android Development

33

Volley

• Super easy

• Internal queue

• Internal cache

• separate thread

• not good for large downloads

compile 'com.squareup.retrofit:retrofit:1.9.0'

Page 63: Hitchikers guide to Modern Android Development

33

Volley

• Super easy

• Internal queue

• Internal cache

• separate thread

• not good for large downloads

RequestQueue queue = Volley.newRequestQueue(this); StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) {

//.. } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { //.. } });

queue.add(stringRequest);

compile 'com.squareup.retrofit:retrofit:1.9.0'

Page 64: Hitchikers guide to Modern Android Development

33

Volley

• Super easy

• Internal queue

• Internal cache

• separate thread

• not good for large downloads

compile 'com.squareup.retrofit:retrofit:1.9.0'

Page 65: Hitchikers guide to Modern Android Development

Come join the fun, join a GDG!!

@yenerm@gdgistanbul

[email protected]