SlideShare a Scribd company logo
1 of 64
Download to read offline
OFFLINE MODE
OF ANDROID APPS
@Ajit5ingh
ABOUT ME
new Presenter(
“Ajit Singh”,
“github.com/ajitsing”,
“www.singhajit.com”,
“@Ajit5ingh”
)
● Why offline mode?
● What it takes to build an offline mode
● Architecture & Code
● Network factors
● Testing
● Bugs & how to solve them
● Wrap up
AGENDA
WHY OFFLINE MODE?
● Lets understand by an example
● We can’t always get connected to internet
● Mobile networks are not stable
WHAT IT TAKES TO
BUILD AN OFFLINE
MODE
WHAT IT TAKES TO BUILD OFFLINE MODE
● UX
● Data
● Listening to network state updates
● Manage the network state
● Notifying user with the latest network state
USER EXPERIENCE
CAN NOT DO ANYTHING OFFLINE!
CAN DO SOMETHING!
CAN DO A LOT!!
DATA
DB CACHE
JSON files, sharedPreferences etc..
LISTEN FOR NETWORK CHANGE EVENTS
APP
IDENTIFYING INTERNET CONNECTIVITY
Figure out current network state using the
NetworkStateIdentifier
MANAGE NETWORK STATE
Update the network state
NOTIFYING USER WITH THE LATEST STATE
Update UI using the latest network state
OFFLINE MODE
ARCHITECTURE
ARCHITECTURE
APP
BENEFITS OF THIS ARCHITECTURE
● Event driven architecture
● App always has the copy of latest connectivity state
● Utilising less resources
● User gets notified almost immediately
CODE
10
User Permission
PERMISSION TO ACCESS NETWORK STATE
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"/>
10
Registering for network status
change events
LISTEN TO NETWORK CHANGE EVENT
<receiver

android:name=".NetworkStateChangeReceiver"

android:exported="false">

<intent-filter>

<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>

</intent-filter>

</receiver>
Deprecated for Apps targeting to API 24
LISTEN TO NETWORK CHANGE EVENT BELOW API 21
if (belowLollipop()) {

ctx.registerReceiver(receiver,
new IntentFilter(CONNECTIVITY_ACTION))
ctx.registerReceiver(receiver,
new IntentFilter(WIFI_STATE_CHANGE_ACTION))
}
LISTEN TO NETWORK CHANGE EVENT BELOW API 21
public class NetworkStateChangeReceiver extends BroadcastReceiver {
@Override

public void onReceive(Context context, Intent intent) {

networkStateManager.refresh();

}
}
LISTEN TO NETWORK CHANGE EVENT FOR API 21 & ABOVE
NetworkCallback networkCallback = new NetworkCallback() {

@Override

public void onAvailable(Network network) {

networkStateManager.refresh();

}



@Override

public void onLost(Network network) {

networkStateManager.refresh();

}

};



cm.registerNetworkCallback(networkRequest, networkCallback);
10
Managing network state
NETWORK STATE MANAGER
public void refresh() {

updateNetworkState();

broadcastNetworkChangeIntent();

}

networkStateManager.refresh();
10
Network state identifier
NETWORK CONNECTIVITY IDENTIFIER
NetworkInfo networkInfo = cm.getActiveNetworkInfo();

networkInfo.isConnected();
NETWORK FACTORS
NETWORK FACTORS
WiFi Mobile Data Low Connectivity
Flight Mode
B
A
T
T
E
R
Y
Power Save Mode
Roaming
Roaming
POTENTIAL ISSUES
ARE YOU REALLY OFFLINE?
AND…
10
But Why??
WATCH OUT!
NetworkConnectivityIdentifier NetworkCallback
App
10
Lets take a look at some of the
common mistakes while using
Android Network APIs
NETWORK CONNECTIVITY IDENTIFIER
NullPointerException!!
public boolean isConnectedToInternet() {

NetworkInfo networkInfo = cm.getActiveNetworkInfo();

return networkInfo.isConnected();

}
NETWORK CONNECTIVITY IDENTIFIER
What about multiple networks!!
public boolean isConnectedToInternet() {

NetworkInfo networkInfo = cm.getActiveNetworkInfo();

return networkInfo != null && networkInfo.isConnected();

}
NETWORK CONNECTIVITY IDENTIFIER
What about unwanted networks!!
public boolean isConnectedToInternet() {

Network[] allNetworks = cm.getAllNetworks();

for (Network network : allNetworks) {

NetworkInfo networkInfo = cm.getNetworkInfo(network);
if (networkInfo != null) {

return networkInfo.isConnected();
}

}

return false;

}
NETWORK CONNECTIVITY IDENTIFIER
private boolean isWifiOrMobile(NetworkInfo networkInfo) {

List<Integer> networks = asList(TYPE_MOBILE, TYPE_WIFI);

return networks.contains(networkInfo.getType());

}
WHAT DID WE DO
● Added null check for NetworkInfo
● Handled multiple networks
● Filtered WIFI and Mobile Data networks only
BE CAREFUL WITH NETWORK CALLBACK
NetworkCallback networkCallback = new NetworkCallback() {

@Override

public void onAvailable(Network network) {

networkStateManager.setConnectedToInternet(true);

}



@Override

public void onLost(Network network) {

networkStateManager.setConnectedToInternet(false);

}

};



cm.registerNetworkCallback(networkRequest, networkCallback);
BE CAREFUL WITH NETWORK CALLBACK
Is it good enough??
NetworkCallback networkCallback = new NetworkCallback() {

@Override

public void onAvailable(Network network) {

networkStateManager.refresh();

}



@Override

public void onLost(Network network) {

networkStateManager.refresh();

}

};



cm.registerNetworkCallback(networkRequest, networkCallback);
BE CAREFUL WITH NETWORK CALLBACK
NetworkCallback networkCallback = new NetworkCallback() {

@Override

public void onAvailable(Network network) {

networkStateManager.refresh();

}
@Override

public void onCapabilitiesChanged(Network n, NetworkCapabilities nc) {

networkStateManager.refresh();

}


@Override

public void onLost(Network network) {

networkStateManager.refresh();

}

};



cm.registerNetworkCallback(networkRequest, networkCallback);
HAVE WE SOLVED ALL THE PROBLEMS?
● Power Save Mode
● Handling on different devices
● Other random connectivity issues
HANDLING ISSUES
UNDERSTANDING THE USER BEHAVIOUR
INCREASE TRIGGER POINTS
GIVE THE USER CHANCE TO GET OUT OF OFFLINE MODE
Activity’s
onResume()
Fragment’s
onResume() Network
Call?
User
Action
TESTING
TESTING PYRAMID
UNIT TESTING
● Network state management logic
● Broadcasting network change event to the app
● Showing offline UI components
INSTRUMENTATION TESTING
● Showing offline UI components on broadcast of network
change event
● Content of UI components
● Interactions with offline components
AUTOMATION TESTING
TESTING USER JOURNEYS
● When user goes online to offline
● When user comes back online
● Interactions with offline components
USING ADB TO GO OFFLINE
MANUAL TESTING
WHY MANUAL TESTING?
● Low connectivity scenarios
● Combination of multiple networks
● Fluctuating network conditions
TESTING IN LIFTS
TESTING WHILE TRAVELLING
● Build something that is useful to the user
● Be careful while using android network API
● Always read the documentation of network APIs
● Understand user behaviour
● Manual testing
● Test with combination of mobile data and wifi
● Use automation testing to test offline UI components
WRAP UP!
THANK YOU
Questions? Feedback?
@Ajit5ingh
www.singhajit.com

More Related Content

Recently uploaded

result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 

Recently uploaded (20)

PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 

Featured

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software
 

Featured (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 

Offline mode of Android Apps