Performance #4 network
-
Upload
vitali-pekelis -
Category
Technology
-
view
106 -
download
9
Transcript of Performance #4 network
![Page 1: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/1.jpg)
Net… work!
Yonatan Levin04/7/2016
Wifi: GoogleGuestPSKpass: pUp3EkaP
4
![Page 2: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/2.jpg)
First,
![Page 3: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/3.jpg)
Yonatan LevinGoogle Developer Expert
parahalllevin.yonatan
![Page 4: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/4.jpg)
> 1200 members Largest Android Active Community
![Page 5: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/5.jpg)
Yonatan LevinGoogle Developer
Expert & Android @ Gett
Idan FelixSenior Android &
Redhead Varonis
Jonathan Yarkoni
Android Developer & Advocate Ironsource
Android Academy Staff
Britt Barak
Android LeadStealth Startup
Muiriel Felix
Android Design
![Page 6: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/6.jpg)
52 Cities > 20M usersRuby, Go, Python, Microservices
![Page 7: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/7.jpg)
Logistics
![Page 8: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/8.jpg)
https://www.facebook.com/groups/android.academy.ils/
![Page 9: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/9.jpg)
What’s next?10/8 - Felix- Battery & CPU
14/9 - Britt- Threading
![Page 10: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/10.jpg)
30 / 10 / 2016New course coming
![Page 11: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/11.jpg)
Special Guest!
![Page 13: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/13.jpg)
What’s in menu?- Network- Offline- Scheduler- Batching- Pre-fetching- Whisky
![Page 14: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/14.jpg)
![Page 15: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/15.jpg)
![Page 16: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/16.jpg)
What was I doing wrong?
![Page 17: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/17.jpg)
Common errors- Polling chat/message from server every 5 seconds
even when app in the background- Pulling photos/articles from server every time user
opens the Gallery even when nothing is changed- Retrying failing networking requests till them will
succeed.- Service that never stops...- A lot of bugs :)
![Page 18: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/18.jpg)
![Page 19: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/19.jpg)
What actually happen
![Page 20: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/20.jpg)
Example
![Page 21: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/21.jpg)
Example
![Page 22: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/22.jpg)
Two scenarios
![Page 23: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/23.jpg)
I want it now!
![Page 24: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/24.jpg)
![Page 25: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/25.jpg)
Does it really works for most of the time?
![Page 26: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/26.jpg)
What we want really to give the user?
![Page 27: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/27.jpg)
![Page 28: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/28.jpg)
Ideal world?
![Page 29: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/29.jpg)
![Page 30: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/30.jpg)
Let’s start with basics
![Page 31: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/31.jpg)
1It’s also called HTTP
Net… work
![Page 32: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/32.jpg)
Hypertext Transfer Protocol.This makes HTTP a stateless protocol. The
communication usually takes place over TCP/IP, but any reliable transport can be used. The default port for TCP/IP is 80, but other ports can also be used.
HTTP
![Page 33: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/33.jpg)
URL
The protocol is typically http, but it can also be https for secure communications.
![Page 34: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/34.jpg)
HTTP VerbSpecifying the action we want to perform on host
GET: fetch an existing resource. The URL contains all the necessary information the server needs to locate and return the resource.
POST: create a new resource. POST requests usually carry a payload that specifies the data for the new resource.
PUT: update an existing resource. The payload may contain the updated data for the resource.
DELETE: delete an existing resource.
![Page 35: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/35.jpg)
Status CodeIn return, the server responds with status codes and message payloads
1xx: Informational Messages - Expect: 100-continue
2xx: Successful - 200 OK, 204 No Content
3xx: Redirection - This requires the client to take additional action. The most common use-case is to jump to a different URL in order to fetch the resource.
4xx: Client Error - 404 Not Found, 400 Bad Request,401 Unauthorized,
5xx: Server Error - 503 Service Unavailable
![Page 36: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/36.jpg)
Request and Response Message Formats
![Page 37: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/37.jpg)
Request and Response Message Formats
![Page 38: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/38.jpg)
Request or response message
![Page 39: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/39.jpg)
Request GETGET /articles/http-basics HTTP/1.1
Host: www.articles.com
Connection: keep-alive
Cache-Control: no-cache
Pragma: no-cache
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
![Page 40: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/40.jpg)
Response Format
HTTP-Version SP Status-Code SP Reason-Phrase CRLF
HTTP/1.1 200 OK
![Page 41: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/41.jpg)
HTTP & HTTPS
A TCP stream is broken into IP packets, and it ensures that those packets always arrive in the correct order without fail. HTTP is an application layer protocol over TCP, which is over IP.
![Page 42: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/42.jpg)
The total overlook
![Page 43: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/43.jpg)
Should I connect every time?!
![Page 44: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/44.jpg)
Persistent connection
![Page 45: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/45.jpg)
Parallel connections
If there are six assets that the client needs to download from a website, the client makes six parallel connections to download those assets, resulting in a faster turnaround.
![Page 46: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/46.jpg)
Pipelining
![Page 47: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/47.jpg)
Server sideestablishing a socket to start listening on port 80 (or some other
port)
receiving the request and parsing the message
processing the response
setting response headers
sending the response to the client
close the connection if a Connection: close request header was found
![Page 48: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/48.jpg)
2Because really there is only one
HTTP Client
![Page 49: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/49.jpg)
URL url = new URL(myurl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(10000 /* milliseconds */); conn.setConnectTimeout(15000 /* milliseconds */); conn.setRequestMethod("GET"); conn.setDoInput(true); // Starts the query conn.connect(); int response = conn.getResponseCode(); is = conn.getInputStream();
// Convert the InputStream into a string String contentAsString = readIt(is, len); return contentAsString;
HttpURLConnection
![Page 50: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/50.jpg)
Wait! I forgot something!
![Page 51: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/51.jpg)
NetworkOnMainThreadException
![Page 52: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/52.jpg)
Brave one that know how to implement readIt(InputStream
is)?
![Page 53: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/53.jpg)
ReadIt()public String readIt(InputStream stream) throws IOException, UnsupportedEncodingException { Reader reader = null; reader = new InputStreamReader(stream, "UTF-8"); char[] buffer = new char[1024]; reader.read(buffer); return new String(buffer);}
![Page 54: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/54.jpg)
What if it’s image?
![Page 55: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/55.jpg)
Luckily, we have
![Page 56: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/56.jpg)
![Page 57: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/57.jpg)
Retrofit 2
A type-safe HTTP client for Android and Java
![Page 58: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/58.jpg)
build.gradle
dependencies { ... compile 'com.squareup.retrofit2:retrofit:2.1.0' ...}
![Page 59: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/59.jpg)
public interface UserService { @POST("me") Call<User>me();}Retrofit retrofit = Retrofit.Builder() .baseUrl("https://your.api.url/v2/"); .build();
UserService service = retrofit.create(UserService.class);
// the request url for service.me() is:// https://your.api.url/v2/me
![Page 60: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/60.jpg)
OkHttp IntegratedRetrofit 2 relies on OkHttp as the HTTP client and has its own dependency to the library as well
compile 'com.squareup.okhttp3:okhttp:3.3.1'
OkHttpClient client = httpClient.build();Retrofit retrofit = Retrofit.Builder() .baseUrl("https://your.api.url/v2/"); .client(client) .build();
![Page 61: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/61.jpg)
Why OkHttp called “Ok”?
![Page 62: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/62.jpg)
InterceptorsOkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();clientBuilder.connectTimeout(CONNECTION_TIMEOUT * 1000, TimeUnit.MILLISECONDS);clientBuilder.addInterceptor(mGTHeadersInterceptor);
Retrofit gtServiceRetrofit = new Retrofit.Builder() .baseUrl(mGTBaseUrl) .client(clientBuilder.build()) .addConverterFactory(GTResponseConverterFactory.create(mGson)) .build();
mGTServiceApi = gtTServiceRetrofit.create(GTServiceApi.class);
![Page 63: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/63.jpg)
Interceptorspublic class GTHeadersInterceptor implements Interceptor {public Response intercept(Chain chain) throws IOException { Request original = chain.request();
// Customize the request Request request = original.newBuilder() .header("Accept", "application/json") .header("Authorization", "auth-token") .method(original.method(), original.body()) .build();
Response response = chain.proceed(request);
// Customize or return the response return response; }}
![Page 64: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/64.jpg)
Sync Requestpublic interface UserService { @POST("/login") Call<User> login();}
// synchronousCall<User> call = userService.login();User user = call.execute().body();
![Page 65: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/65.jpg)
ASycn RequestCall<User> call = userService.login();call.enqueue(new Callback<User>() { @Override public void onResponse(Call<User> call, Response<User> response) { // response.isSuccessful() is true if the response code is 2xx
}
@Override public void onFailure(Call<User> call, Throwable t) { // handle execution failures like no internet connectivity }}
![Page 66: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/66.jpg)
Cancel requestCall<User> call = userService.login();User user = call.execute().body();
// changed your mind, cancel the requestcall.cancel();
![Page 67: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/67.jpg)
Convertor
Available Converters
Gson: com.squareup.retrofit2:converter-gson:2.1.0
Moshi: com.squareup.retrofit2:converter-moshi:2.1.0
Jackson: com.squareup.retrofit2:converter-jackson:2.1.0
SimpleXML: com.squareup.retrofit2:converter-simplexml:2.1.0
ProtoBuf: com.squareup.retrofit2:converter-protobuf:2.1.0
Wire: com.squareup.retrofit2:converter-wire:2.1.0
![Page 68: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/68.jpg)
Add convertorRetrofit retrofit = Retrofit.Builder() .baseUrl("https://your.api.url/v2/"); .addConverterFactory(ProtoConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build();
![Page 69: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/69.jpg)
Multiple Convertors
First come - first serverFirst fail, pass to next neOne that succeed - consume the response.
Add the esoteric first and more general like GSON last.
![Page 70: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/70.jpg)
הנקרא :הבנתI want to add query parameter to every request. What should i
do?
![Page 71: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/71.jpg)
URL
The protocol is typically http, but it can also be https for secure communications.
![Page 72: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/72.jpg)
Back to requests@GET("api/dbx/drivers/self/history/{page}/{items_per_page}")Call<JobsHistoryResponse> getDriverJobsHistory( @Path("page") int page, @Path("items_per_page") int itemsPerPage, @Query("date_from") String dateFrom, @Query("date_to") String dateTo);
api/dbx/drivers/self/history/1/30?date_from=”01012016”&date_to=”30012016”
![Page 73: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/73.jpg)
Form@FormUrlEncoded@POST("api/dbx/orders/{order_id}/arrival_eta")Call<VoidResponse> postDriverExternalEtaBeforeArrival( @Header("Driver-Id") int mDriverId, @Path("order_id") int orderId, @Field("directions_eta") long directionsETA);
api/dbx/orders/1235675/arrival_eta
Body: { directions_eta=1235723847328 }
![Page 74: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/74.jpg)
More read
https://futurestud.io/blog/retrofit-getting-started-and-android-client
![Page 75: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/75.jpg)
Shot of whisky?
![Page 76: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/76.jpg)
What if i need different BASE_URL for couple requests?
![Page 77: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/77.jpg)
Download Image from S3public interface UserService { @GET public Call<ResponseBody> profilePicture(@Url String url);}
Retrofit retrofit = Retrofit.Builder() .baseUrl("https://your.api.url/"); .build();
UserService service = retrofit.create(UserService.class);service.profilePicture("https://s3.amazon.com/profile-picture/path");
![Page 79: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/79.jpg)
2Execute at right time
Schedulers
![Page 80: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/80.jpg)
SyncAdapter
![Page 81: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/81.jpg)
What is it and how i eat that
Android Framework API >= 7 (Android 2.1)Synchronizing data between an Android device and web serversYou specify what you should sync , how often - and it will do the rest
![Page 82: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/82.jpg)
Benefits?
Plug-in architectureAutomated executionAutomated network checkingImproved battery performanceAccount management and authentication
![Page 83: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/83.jpg)
Let’s compareCustom Sync Sync Adapter
Network Availability - manually Network Availability - Automatically
Pending Queue - manually Pending Queue - Automatically
Refresh on Network - manually Refresh on Network - Automatically
Periodic Update - manually Periodic Update - Automatically
Sync Setting - manually Sync Setting - Automatically
Network Bandwidth - manually Network Bandwidth - Automatically
Battery Efficient - ?? Depend on you Battery Efficient - Yes
Survive on Reboot - Depends on you Survive on Reboot - Yes
![Page 84: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/84.jpg)
![Page 85: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/85.jpg)
How to?
Sqlite Database: I guess you all are master of Sqlite database, SyncAdapter will store data in Sqlite using Content Provider. You may choose other options as well.
Content Provider: Act as bridge between your database and SyncAdapter. To expose your data in Rest like URL pattern.
AbstractAccountAuthenticator: We need to extend this class and override methods, It is primarily used to manage authentication and account management. To use SyncAdapter you must have custom account. This class is responsible to create account, maintain auth token.
![Page 86: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/86.jpg)
How to?
Authenticator Service: This is normal Service, which we are using daily. The only difference is that this service create object of AbstractAccountAuthenticator class and bind.
AbstractThreadedSyncAdapter: As developer we need to extend this class and override methods. This is the main piece of SyncAdapter puzzle. It has method onPerformSync, in which we need to write our code.
Sync Service: This is normal Service. It use to create object of AbstractThreadedSyncAdapter class and bind.
![Page 87: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/87.jpg)
How to?Authenticator.xml: You need to create this file under res/xml/ folder. This file is required to bind your authenticator component into the sync adapter and account frameworks, you need to provide these framework with metadata that describes the component. You can choose your own file name.
SyncAdapter.xml: You need to create this file under res/xml/ folder. The metadata specifies the account type you've created for your sync adapter, declares a content provider authority associated with your app.
AndroidManifest.xml: You must register Sync Service, Authenticator service and few other things in AndroidManifast file in order to work SyncAdapter, This is the final piece of puzzle.
![Page 88: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/88.jpg)
![Page 89: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/89.jpg)
JobScheduler/GCMNetworkManager
![Page 90: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/90.jpg)
What?
Schedule the task to execute it when certain conditions met.(charging, idle, connected to a network or connected to an unmetered network)
![Page 91: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/91.jpg)
Why two?
JobScheduler was introduced in API >= 21 (Lollipop).GCMNetworkManager - is part of GCM package. When using on devices >= 21, use JobScheduler underneath.
![Page 92: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/92.jpg)
Deep Dive
![Page 93: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/93.jpg)
build.gradle
dependencies { ... compile 'com.google.android.gms:play-services-gcm:9.0.2' ...}
![Page 94: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/94.jpg)
AndroidManifest.xml<service
android:name="com.google.codelab.networkmanager.BestTimeService" android:permission="com.google.android.gms.permission.BIND_NETWORK_
TASK_SERVICE"android:exported="true">
<intent-filter> <action android:name="com.google.android.gms.gcm.ACTION_TASK_READY"/> </intent-filter>
</service>
![Page 95: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/95.jpg)
BestTimeService.java/** * Task run by GcmNetworkManager when all the requirements of the scheduled * task are met. */public class BestTimeService extends GcmTaskService {
...}
![Page 96: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/96.jpg)
BestTimeService.java@Overridepublic int onRunTask(TaskParams taskParams) { Log.i(TAG, "onRunTask"); switch (taskParams.getTag()) { case TAG_TASK_ONEOFF_LOG: Log.i(TAG, TAG_TASK_ONEOFF_LOG); // This is where useful work would go return GcmNetworkManager.RESULT_SUCCESS; case TAG_TASK_PERIODIC_LOG: Log.i(TAG, TAG_TASK_PERIODIC_LOG); // This is where useful work would go return GcmNetworkManager.RESULT_SUCCESS; default: return GcmNetworkManager.RESULT_FAILURE; }}
![Page 97: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/97.jpg)
Activityprivate GcmNetworkManager mGcmNetworkManager;
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ... mGcmNetworkManager = GcmNetworkManager.getInstance(this);}
![Page 98: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/98.jpg)
Scheduling a taskTask task = new OneoffTask.Builder() .setService(BestTimeService.class) .setExecutionWindow(0, 30) .setTag(BestTimeService.TAG_TASK_ONEOFF_LOG) .setUpdateCurrent(false) .setRequiredNetwork(Task.NETWORK_STATE_CONNECTED) .setRequiresCharging(false) .build();
mGcmNetworkManager.schedule(task);
![Page 99: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/99.jpg)
What’s in it?
Service: The specific GcmTaskService that will control the task. This will allow us to cancel it later.
Execution window: The time period in which the task will execute. First param is the lower bound and the second is the upper bound (both are in seconds). This one is mandatory.
Tag: We’ll use the tag to identify in the onRunTask method which task is currently being run. Each tag should be unique, and the max length is 100.
![Page 100: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/100.jpg)
What’s in it?
Update Current: This determines whether this task should override any pre-existing tasks with the same tag. By default, this is false, so new tasks don’t override existing ones.
Required Network: Sets a specific network state to run on. If that network state is unavailable, then the task won’t be executed until it becomes available.
Requires Charging: Whether the task requires the device to be connected to power in order to execute.
![Page 101: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/101.jpg)
Scheduling a periodic taskTask task = new PeriodicTask.Builder() .setService(BestTimeService.class) .setPeriod(30) .setFlex(10) .setTag(BestTimeService.TAG_TASK_PERIODIC_LOG) .setPersisted(true) .build();
mGcmNetworkManager.schedule(task);
![Page 102: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/102.jpg)
What’s in it?
Period: Specifies that the task should recur once every interval at most, where the interval is the input param in seconds. By default, you have no control over where in that period the task will execute. This setter is mandatory.
Flex: Specifies how close to the end of the period (set above) the task may execute. With a period of 30 seconds and a flex of 10, the scheduler will execute the task between the 20-30 second range.
![Page 103: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/103.jpg)
What’s in it?
Persisted: Determines whether the task should be persisted across reboots. Defaults to true for periodic tasks, and is not supported for one-off tasks. Requires “Receive Boot Completed” permission, or the setter will be ignored.
![Page 104: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/104.jpg)
Cancel TaskmGcmNetworkManager.cancelAllTasks(BestTimeService.class);
mGcmNetworkManager.cancelTask( BestTimeService.TAG_TASK_PERIODIC_LOG, BestTimeService.class);
![Page 105: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/105.jpg)
There is a problem hiding here
![Page 106: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/106.jpg)
![Page 107: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/107.jpg)
Not all devices shipped with Play Servicesint resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);if (resultCode == ConnectionResult.SUCCESS) { mGcmNetworkManager.schedule(task);} else { // Deal with this networking task some other way}
![Page 108: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/108.jpg)
When Google Play updated it removes all scheduled periodic tasks
public class BestTimeService extends GcmTaskService {
@Override public void onInitializeTasks() { super.onInitializeTasks(); // Reschedule removed tasks here }}
![Page 109: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/109.jpg)
4Predict what your user will need
Prefetch data
![Page 110: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/110.jpg)
Prefetch strategy
The goal is simple: Reduce the number of radio activations required to download the data.
![Page 111: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/111.jpg)
Result
Improve the latency, Lower the required bandwidthReduce download times.
![Page 112: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/112.jpg)
User Experience!!!!!
![Page 113: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/113.jpg)
Strategy
Download the data that has of 50% chance to be used by user in his sessionOrPrefetched data should be enough for 2-5 minutes of use
![Page 114: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/114.jpg)
Let’s practice!
![Page 115: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/115.jpg)
Example
![Page 116: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/116.jpg)
4Minimizing the Effect of Regular UpdatesWith GCM/FCM
Triggered update
![Page 117: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/117.jpg)
Polling
ServerOur App
Felix is dancing salsa?
No
Felix is dancing salsa?
NoFelix is dancing
salsa?
No
Felix is dancing salsa?
Yes!!!
![Page 119: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/119.jpg)
What if there is 50M clients?
![Page 120: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/120.jpg)
GCM and FCM
![Page 121: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/121.jpg)
GCM and FCM
![Page 122: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/122.jpg)
How to
dependencies { compile 'com.google.firebase:firebase-messaging:9.2.0'}
![Page 123: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/123.jpg)
<service android:name=".MyFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter></service>…<service android:name=".MyFirebaseInstanceIDService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter></service>
![Page 124: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/124.jpg)
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
@Override public void onTokenRefresh() { // Get updated InstanceID token. String refreshedToken = FirebaseInstanceId.getInstance().getToken();
sendRegistrationToServer(refreshedToken); }}
![Page 125: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/125.jpg)
https://fcm.googleapis.com/fcm/sendContent-Type:application/jsonAuthorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA{ "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...", "notification" : { "body" : "great match!", "title" : "Portugal vs. Denmark", "icon" : "myicon" }, "data" : { "Nick" : "Mario", "Room" : "PortugalVSDenmark" }}
![Page 126: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/126.jpg)
Message strategyNotifications delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.
Messages with both notification and data payload. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
![Page 127: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/127.jpg)
public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { Log.d(TAG, "From: " + remoteMessage.getFrom()); sendNotification(remoteMessage.getNotification().getBody()); }}
MyFirebaseMessagingService
![Page 128: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/128.jpg)
4Don’t download that you already have
Redundant Download
![Page 129: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/129.jpg)
Don’t download what you already have
![Page 130: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/130.jpg)
Cache = Lastlong currentTime = System.currentTimeMillis();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
long expires = conn.getHeaderFieldDate("Expires", currentTime);long lastModified = conn.getHeaderFieldDate("Last-Modified", currentTime);
setDataExpirationDate(expires);
if (lastModified < lastUpdateTime) { // Skip update} else { // Parse update}
![Page 131: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/131.jpg)
How to do that in Retrofit?
![Page 132: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/132.jpg)
4The shy guy that no shy anymore
Doze Mode
![Page 133: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/133.jpg)
Doze, the bro :)
No network accessNo jobsNo syncsNo wakelocksNo alarmsGPS
![Page 134: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/134.jpg)
![Page 135: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/135.jpg)
Doze Mode on Marshmallow
![Page 136: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/136.jpg)
Not shy anymore
![Page 137: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/137.jpg)
Doze Mode on Nougat
![Page 138: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/138.jpg)
Doze Mode on Nougat
![Page 139: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/139.jpg)
Doze, bye- User pickup the phone- User plug the phone to the charger- Real alarm (clock) is going to kick on
![Page 140: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/140.jpg)
So how i survive my background service to track
Felix?
![Page 141: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/141.jpg)
Will doze mode affect my app?
![Page 142: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/142.jpg)
GCM
Use GCM with High priority - but treat it with special care{ "to" : "...", "priority" : "high", "notification" : { ... }, "data" : { ... }}
![Page 143: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/143.jpg)
AlarmManager
setAndAllowWhileIdle() or setExactAndAllowWhileIdle().
Could be triggered only once in every fifteen minutes
![Page 144: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/144.jpg)
WhiteList
An app can fire the ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS intent to take the user directly to the Battery Optimization, where they can add the app.
An app holding the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission can trigger a system dialog to let the user add the app to the whitelist directly, without going to settings. The app fires a ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent to trigger the dialog.
The user can manually remove apps from the whitelist as needed.
![Page 145: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/145.jpg)
WhiteList
An app holding the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission can trigger a system dialog to let the user add the app to the whitelist directly, without going to settings. The app fires a ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent to trigger the dialog.
![Page 146: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/146.jpg)
Note: Google Play policies prohibit apps from requesting direct
exemption from Power Management features in Android 6.0+ (Doze and
App Standby) unless the core function of the app is adversely
affected.
![Page 147: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/147.jpg)
![Page 148: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/148.jpg)
Payload
Because size is matter4
![Page 149: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/149.jpg)
Serialization
Flatbuffers4
![Page 150: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/150.jpg)
Serialization/Deserialization
Serialization
Deserialization
![Page 151: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/151.jpg)
JSON Class Representation
{ "starwars": { "number_of_episodes": 1, "realDarthVaderName": "Anakin Skywalker", "nextEpisodeRelease": "01-12-2016 01:00:00+3:00GMT" }}
![Page 152: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/152.jpg)
Serialization/Deserialization
Advantage - It’s human readable. And it’s biggest weak point.
Memory overhead
![Page 153: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/153.jpg)
- Faster- Lighter
FlatBuffers
![Page 154: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/154.jpg)
How it works?
![Page 155: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/155.jpg)
Process
1.Create schema2.Compile schema with flatc compiler3.Import generated files into your project4.Read from byte[]:
java.nio.ByteBuffer buf = builder.dataBuffer();// Deserialize the data from the buffer.StarWars starWars = StarWars.getRootAsStarWars(buf);
![Page 156: Performance #4 network](https://reader035.fdocuments.in/reader035/viewer/2022062502/589cae801a28abbe4a8b562b/html5/thumbnails/156.jpg)