Hitchikers guide to Modern Android Development
-
Upload
murat-yener -
Category
Documents
-
view
187 -
download
3
Transcript of Hitchikers guide to Modern Android Development
Murat Yener @yenerm
Hitchhikers Guide to Modern Android Development
Murat Yener @yenerm
Hitchhikers 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..)
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..)
ABOUT USLorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat it.
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
5
pre-historic (cupcake, donut)
6
genesis (eclair, froyo, gingerbread)
7
(r)evolution (>4.0)
8
modern era
5.0 APIS
Android StudioMaterial Design
Wear
Gradle Libraries
9
Android OS
• Notifications (metadata)
• Camera 2.0
• Job Scheduler
• Printing Framework
• Multiple Sim (>=5.1)
9
Android OS
• Notifications (metadata)
• Camera 2.0
• Job Scheduler
• Printing Framework
• Multiple Sim (>=5.1)
VISIBILITY_PRIVATE VISIBILITY_PUBLIC VISIBILITY_SECRET
9
Android OS
• Notifications (metadata)
• Camera 2.0
• Job Scheduler
• Printing Framework
• Multiple Sim (>=5.1)
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) {…} };
9
Android OS
• Notifications (metadata)
• Camera 2.0
• Job Scheduler
• Printing Framework
• Multiple Sim (>=5.1)
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);
9
Android OS
• Notifications (metadata)
• Camera 2.0
• Job Scheduler
• Printing Framework
• Multiple Sim (>=5.1)
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);
9
Android OS
• Notifications (metadata)
• Camera 2.0
• Job Scheduler
• Printing Framework
• Multiple Sim (>=5.1)
9
Android OS
• Notifications (metadata)
• Camera 2.0
• Job Scheduler
• Printing Framework
• Multiple Sim (>=5.1)
SubscriptionManager.OnSubscriptionsChangedListener{…}
9
Android OS
• Notifications (metadata)
• Camera 2.0
• Job Scheduler
• Printing Framework
• Multiple Sim (>=5.1)
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..
11
Surfaces
• Paper like surfaces
• Multiple layers (up to 10)
• Scrolls till end or behind
12
Layout
• 2 Keylines..
• Keyline 1 @ 16dp
• Keyline 2 @ 72dp
13
Colors and Theming
• Main and accent color
• Primary: toolbars
• Primary.Dark: status bar
14
Palette API
• Get color palette from your pics
• Vibrant, Muted
• Swatch
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();
14
Palette API
• Get color palette from your pics
• Vibrant, Muted
• Swatch
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());
}
14
Palette API
• Get color palette from your pics
• Vibrant, Muted
• Swatch
15
Animation
• Authentic Motion: realistic swift movement
• Responsive Interaction: respond to user
• Meaningful Transitions: physics, not random moves
• Delightful Details: small makes change
15
Animation
• Authentic Motion: realistic swift movement
• Responsive Interaction: respond to user
• Meaningful Transitions: physics, not random moves
• Delightful Details: small makes change
16
New Platforms
• teachings of Google Glass…
• Android Wear
• Android Auto
• Android TV
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)
18
Android Wear - Notifications
Automatically works! -Phone transmits notifications to wear -You can code wear only notifications
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
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>
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
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();
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.+“
24
Android Studio - Basics
• IntelliJ based
• Graduated from beta!
• Better drag and drop UI designer
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' }
26
Tools
• Android Device Monitor
• Battery Historian
• Faster Emulator
26
Tools
• Android Device Monitor
• Battery Historian
• Faster Emulator
26
Tools
• Android Device Monitor
• Battery Historian
• Faster Emulator
27
Libraries
“No man is an island..” -Jon Bon Jovi
28
Greenbus
• Event Driven
• Event dispatch via bus
• Uses reflection
• Easy/clean
compile 'de.greenrobot:eventbus:2.4.0'
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'
28
Greenbus
• Event Driven
• Event dispatch via bus
• Uses reflection
• Easy/clean
compile 'de.greenrobot:eventbus:2.4.0'
29
• From creators of greenbus
• Standart sqlite
• No ‘create table’ etc..
• Generator for model and dao
GreenDAO
compile 'de.greenrobot:greendao'
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'
29
• From creators of greenbus
• Standart sqlite
• No ‘create table’ etc..
• Generator for model and dao
GreenDAO
compile 'de.greenrobot:greendao'
30
JobQueue
• Easy to prioritize
• Delay
• or group jobs
compile 'com.path:android-priority-jobqueue:1.1.2'
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'
30
JobQueue
• Easy to prioritize
• Delay
• or group jobs
compile 'com.path:android-priority-jobqueue:1.1.2'
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'
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'
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'
32
Retrofit
• Turns your REST api to Java Interface
• Uses OkHttp
• synchronous, asynchronous, observable
compile 'com.squareup.retrofit:retrofit:1.9.0'
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'
32
Retrofit
• Turns your REST api to Java Interface
• Uses OkHttp
• synchronous, asynchronous, observable
compile 'com.squareup.retrofit:retrofit:1.9.0'
33
Volley
• Super easy
• Internal queue
• Internal cache
• separate thread
• not good for large downloads
compile 'com.squareup.retrofit:retrofit:1.9.0'
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'
33
Volley
• Super easy
• Internal queue
• Internal cache
• separate thread
• not good for large downloads
compile 'com.squareup.retrofit:retrofit:1.9.0'