Evolving the Android Core with Aspects

Carlo Pescio
Carlo PescioSoftware Architect, Consultant and Mentor at Eptacom - Dott. Carlo Pescio
Carlo Pescio @CarloPescio http://aspectroid.com
Carlo Pescio
Evolving the Android Core
with Aspects
Carlo Pescio @CarloPescio http://aspectroid.com
Android on custom devices
New hardware:
- Ethernet
- TV tuner
- Modbus
Different target / product line / market:
- Not a “personal device”
- Keeping “user data” not an option
- USB devices should autostart apps w/out asking user
- Foreground app can’t win
- Change low memory policies
Android architecture
Looks familiar ? 
Carlo Pescio @CarloPescio http://aspectroid.com
AudioManager
int mBecomingNoisyIntentDevices =
AudioSystem.DEVICE_OUT_WIRED_HEADSET |
AudioSystem.DEVICE_OUT_WIRED_HEADPHONE |
AudioSystem.DEVICE_OUT_ALL_A2DP |
AudioSystem.DEVICE_OUT_HDMI |
AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET |
AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET |
AudioSystem.DEVICE_OUT_ALL_USB |
AudioSystem.DEVICE_OUT_LINE;
Carlo Pescio @CarloPescio http://aspectroid.com
AudioManager
private void onSetWiredDeviceConnectionState(int device, int state,
String name)
{
synchronized (mConnectedDevices) {
if((state == 0) && ((device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) ||
(device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE)||
(device == AudioSystem.DEVICE_OUT_LINE))) {
setBluetoothA2dpOnInt(true);
}
boolean isUsb = ((device & ~AudioSystem.DEVICE_OUT_ALL_USB) == 0) ||
(((device & AudioSystem.DEVICE_BIT_IN) != 0) &&
((device & ~AudioSystem.DEVICE_IN_ALL_USB) == 0));
handleDeviceConnection((state == 1), device, (isUsb ? name : ""));
if (state != 0) {
if ((device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) ||
(device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE) ||
(device == AudioSystem.DEVICE_OUT_LINE)) {
setBluetoothA2dpOnInt(false);
}
Carlo Pescio @CarloPescio http://aspectroid.com
Evolving open source code
Plugin architecture: lucky you
Or: just fork & change the damn code
pull + 3 way compare
• Feature / Artifact symmetry is broken
• Features are scattered in small patches
• Product families => multiple forks
Carlo Pescio @CarloPescio http://aspectroid.com
aspectroid
exploring the
aspect-oriented
design space
Carlo Pescio @CarloPescio http://aspectroid.com
Aspect Orientation
Born to address Cross-Cutting, Scattered Concerns
Error handling, Transaction handling
Not very successful so far
No design discipline evolved
Core features:
Customize behavior w/out changing code
Avoid scattering a single concern around
Carlo Pescio @CarloPescio http://aspectroid.com
AspectJ crash course
Inter-type declaration:
add data members, member functions,
implement interfaces, …
aspect ScreenStateSettingsAspect
{
private CheckBoxPreference
DevelopmentSettings.stayOnPreference;
}
Carlo Pescio @CarloPescio http://aspectroid.com
AspectJ crash course
Pointcut (the “where”)
identify matching parts of code
(sophisticated syntax)
pointcut settingsUpdated() :
execution( private void
PowerManagerService.updateStayOnLocked(int));
pointcut adbSet() :
within(UsbDeviceManager) &&
set(boolean *.mAdbEnabled);
Carlo Pescio @CarloPescio http://aspectroid.com
AspectJ crash course
Advice (the when and how)
execute code before / after / around code
matched by a pointcut (join point)
after(boolean newVal) :
adbSet() && args(newVal)
{
// do something here
}
Carlo Pescio @CarloPescio http://aspectroid.com
AspectJ crash course
Aspect
scoping mechanism; also: artifact
privileged aspect
ScreenStatePowerAspect {
… inter-type declarations
… pointcuts
… advices
… functions
… data
}
Carlo Pescio @CarloPescio http://aspectroid.com
AspectJ & Android
Apps: easy
download plugin, add AspectJ nature to project
Core: not so easy
Shell calling Makefiles calling Python calling make
Strong expectations (class files etc.)
AJC can compile regular Java files
aspects don’t need .aj extension, .java is ok
Carlo Pescio @CarloPescio http://aspectroid.com
AspectJ in the core
- Create a compiler wrapper around ajc in C
- Standardizes options and behavior
- Like: files in a file list must have absolute path
- Minimal changes to the build scripts
- If you link with the aspectjrt library, I call the compiler wrapper
- Applicable to other JVM languages
# C o m m o n d e f i n i t i o n t o i n v o k e j a v a c o n t h e h o s t a n d t a r g e t . # # S o m e h i s t o r i c a l n o t e s : # - b e l o w w e w r i t e t h e l i s t o f j a v a
f i l e s t o j a v a - s o u r c e - l i s t t o a v o i d a r g u m e n t # l i s t l e n g t h p r o b l e m s w i t h C y g w i n # - w e f i l t e r o u t d u p l i c a t e j a v a f i l e n a m e s
b e c a u s e e c l i p s e ' s c o m p i l e r # d o e s n ' t l i k e t h e m . # # $ ( 1 ) : j a v a c # $ ( 2 ) : b o o t c l a s s p a t h d e f i n e c o m p i l e - j a v a $ ( h i d e ) r m - f
$ @ $ ( h i d e ) r m - r f $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) $ ( h i d e ) m k d i r - p $ ( d i r $ @ ) $ ( h i d e ) m k d i r - p
$ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) $ ( c a l l u n z i p - j a r - f i l e s , $ ( P R I V A T E _ S T A T I C _ J A V A _ L I B R A R I E S ) ,
$ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) ) $ ( c a l l d u m p - w o r d s - t o - f i l e , $ ( P R I V A T E _ J A V A _ S O U R C E S ) , $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R )
/ j a v a - s o u r c e - l i s t ) $ ( h i d e ) i f [ - d " $ ( P R I V A T E _ S O U R C E _ I N T E R M E D I A T E S _ D I R ) " ] ; t h e n  f i n d
$ ( P R I V A T E _ S O U R C E _ I N T E R M E D I A T E S _ D I R ) - n a m e ' * . j a v a ' > > $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) / j a v a - s o u r c e - l i s t ;  f i $ ( h i d e ) t r '
' '  n ' < $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) / j a v a - s o u r c e - l i s t  | s o r t - u > $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) / j a v a -
s o u r c e - l i s t - u n i q $ ( h i d e ) i f [ - s $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) / j a v a - s o u r c e - l i s t - u n i q ] ; t h e n  $ ( i f $ ( f i n d s t r i n g
a s p e c t j r t , $ ( P R I V A T E _ S T A T I C _ J A V A _ L I B R A R I E S ) ) , $ ( s u b s t j a v a c , a j c w , $ ( 1 ) ) , $ ( 1 ) ) - e n c o d i n g U T F - 8  $ ( s t r i p
$ ( P R I V A T E _ J A V A C _ D E B U G _ F L A G S ) )  $ ( i f $ ( f i n d s t r i n g t r u e , $ ( P R I V A T E _ W A R N I N G S _ E N A B L E ) ) , $ ( x l i n t _ u n c h e c k e d ) , )  $ ( 2 ) 
$ ( a d d p r e f i x - c l a s s p a t h , $ ( s t r i p  $ ( c a l l n o r m a l i z e - p a t h - l i s t , $ ( P R I V A T E _ A L L _ J A V A _ L I B R A R I E S ) ) ) )  $ ( i f
$ ( f i n d s t r i n g t r u e , $ ( P R I V A T E _ W A R N I N G S _ E N A B L E ) ) , $ ( x l i n t _ u n c h e c k e d ) , )  - e x t d i r s " " - d $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R )
 $ ( P R I V A T E _ J A V A C F L A G S )   @ $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) / j a v a - s o u r c e - l i s t - u n i q  | | ( r m - r f
$ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) ; e x i t 4 1 )  f i $ ( i f $ ( P R I V A T E _ J A V A _ L A Y E R S _ F I L E ) , $ ( h i d e ) b u i l d / t o o l s / j a v a - l a y e r s . p y 
$ ( P R I V A T E _ J A V A _ L A Y E R S _ F I L E )  @ $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) / j a v a - s o u r c e - l i s t - u n i q , ) $ ( h i d e ) r m - f
$ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) / j a v a - s o u r c e - l i s t $ ( h i d e ) r m - f $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) / j a v a - s o u r c e - l i s t -
u n i q $ ( i f $ ( P R I V A T E _ J A R _ E X C L U D E _ F I L E S ) , $ ( h i d e ) f i n d $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R )  - n a m e $ ( w o r d 1 ,
$ ( P R I V A T E _ J A R _ E X C L U D E _ F I L E S ) )  $ ( a d d p r e f i x - o - n a m e , $ ( w o r d l i s t 2 , 9 9 9 , $ ( P R I V A T E _ J A R _ E X C L U D E _ F I L E S ) ) )  | x a r g s
r m - r f ) $ ( i f $ ( P R I V A T E _ J A R _ P A C K A G E S ) ,  $ ( h i d e ) f i n d $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) - m i n d e p t h 1 - t y p e f 
$ ( f o r e a c h p k g , $ ( P R I V A T E _ J A R _ P A C K A G E S ) ,  - n o t - p a t h $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) / $ ( s u b s t . , / , $ ( p k g ) ) /  * )
- d e l e t e ;  f i n d $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) - e m p t y - d e l e t e ) $ ( i f $ ( P R I V A T E _ J A R _ E X C L U D E _ P A C K A G E S ) , $ ( h i d e ) r m
- r f  $ ( f o r e a c h p k g , $ ( P R I V A T E _ J A R _ E X C L U D E _ P A C K A G E S ) ,  $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) / $ ( s u b s t
. , / , $ ( p k g ) ) ) ) $ ( i f $ ( P R I V A T E _ R M T Y P E D E F S ) , $ ( h i d e ) $ ( R M T Y P E D E F S ) - v $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) ) $ ( i f
$ ( P R I V A T E _ J A R _ M A N I F E S T ) ,  $ ( h i d e ) s e d - e ' s / % B U I L D _ N U M B E R % / $ ( B U I L D _ N U M B E R ) / '  $ ( P R I V A T E _ J A R _ M A N I F E S T ) >
$ ( d i r $ @ ) / m a n i f e s t . m f & &  j a r - c f m $ @ $ ( d i r $ @ ) / m a n i f e s t . m f  - C $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) . ,
 $ ( h i d e ) j a r - c f $ @ - C $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) . ) e n d e f
AspectJ in the core
Definitions.mk -> define compile-java
$(if $(findstring aspectjrt,
$(PRIVATE_STATIC_JAVA_LIBRARIES)),
$(subst javac,ajcw,$(1)), $(1))
Does it actually work?
Add a useful feature without changing sources
Carlo Pescio @CarloPescio http://aspectroid.com
How do we start?
• Explore code
• “Stay on while charging”
• UsbDeviceManager, PowerManagerService
• See ebook for details on the quest
• Tested as much as possible outside the core
• See ebook again
• Add aspects
• Make aspects robust !
A map
A map
A map
A map
Carlo Pescio @CarloPescio http://aspectroid.com
Extend the Settings app
- Add a new checkbox
- Persist the checkbox state somewhere
- Show the persisted value on resume
Settings Aspect (1/3)
privileged aspect ScreenStateSettingsAspect
{
private CheckBoxPreference
DevelopmentSettings.stayOnPreference;
pointcut onCreateExecuted() :
execution(public void DevelopmentSettings.onCreate(..)) ;
pointcut onResumeExecuted() :
execution(public void DevelopmentSettings.onResume(..)) ;
Settings Aspect (2/3)
after(DevelopmentSettings self):onCreateExecuted()&&target(self)
{
final Context ctx = self.getActivity();
PreferenceCategory targetCategory = (PreferenceCategory)self.
findPreference(DevelopmentSettings.DEBUG_DEBUGGING_CATEGORY_KEY);
self.stayOnPreference = new CheckBoxPreference(ctx);
self.stayOnPreference.setKey("aspectroidScreenState");
self.stayOnPreference.setTitle("Prevent sleeping while debugging");
self.stayOnPreference.setSummary("only for usb debugging");
targetCategory.addPreference(self.stayOnPreference);
self.stayOnPreference.setOnPreferenceChangeListener(
new OnPreferenceChangeListener() {
public boolean onPreferenceChange(
Preference preference, Object newValue) {
API.persistPreference(ctx, (Boolean) newValue);
return true;
} }); }
Settings Aspect (3/3)
after(DevelopmentSettings self) : onResumeExecuted() &&
target(self)
{
final Context ctx = self.getActivity();
boolean on = API.retrievePreference(ctx);
self.stayOnPreference.setChecked(on);
}
}

Carlo Pescio @CarloPescio http://aspectroid.com
Am I debugging?
- USB debugging enabled
- USB connected to a computer
- Not just charging
- Not in OTG / host mode
Carlo Pescio @CarloPescio http://aspectroid.com
UsbDeviceManager
See ebook for details
USB Aspect (1/3)
aspect ScreenStateUsbAspect {
boolean adbEnabled = false;
boolean adbConnected = false;
Context context;
before(Context ctx):
execution(UsbDeviceManager.new(Context)) && args( ctx ) {
context = ctx;
}
USB Aspect (2/3)
pointcut adbSet() : within(UsbDeviceManager) &&
set(boolean *.mAdbEnabled);
after(boolean newVal) : adbSet() && args(newVal) {
adbEnabled = newVal;
checkAdbOn();
}
USB Aspect (3/3)
pointcut connectedSet() : within(UsbDeviceManager) &&
set(boolean *.mConnected);
after(boolean newVal) : connectedSet() && args(newVal) {
adbConnected = newVal;
checkAdbOn();
}
void checkAdbOn() {
boolean isOn = adbEnabled && adbConnected;
API.storeAdbState(context, isOn);
}
} 
PowerManagerService (1)
public void systemReady(IAppOpsService appOps) {
// …
final ContentResolver resolver =
mContext.getContentResolver();
// …
resolver.registerContentObserver(
Settings.Global.getUriFor(
Settings.Global.STAY_ON_WHILE_PLUGGED_IN),
false, mSettingsObserver, UserHandle.USER_ALL);
//…
updateSettingsLocked();
//…
}
PowerManagerService (2)
private void updateStayOnLocked(int dirty) {
if( (dirty &
(DIRTY_BATTERY_STATE | DIRTY_SETTINGS)) != 0 ) {
final boolean wasStayOn = mStayOn;
if( mStayOnWhilePluggedInSetting != 0 && !
isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()){
mStayOn = mBatteryManagerInternal.isPowered(
mStayOnWhilePluggedInSetting);
}
else {
mStayOn = false;
}
if( mStayOn != wasStayOn ) {
mDirty |= DIRTY_STAY_ON;
}
}
}
Power Aspect (1/2)
privileged aspect ScreenStatePowerAspect {
pointcut systemReadyExecuted() : execution(
public void PowerManagerService.systemReady(..));
after(PowerManagerService self) :
systemReadyExecuted() && target(self) {
API.registerScreenOnObserver(self.mContext,
self.mSettingsObserver);
}
Power Aspect (2/2)
pointcut settingsUpdated() : execution(
private void PowerManagerService.updateStayOnLocked(int));
void around(PowerManagerService self, int dirty) :
settingsUpdated() && args(dirty) && target(self) {
proceed(self, dirty);
boolean keepOn = API.shouldKeepScrenOn(self.mContext);
if( keepOn && !self.mStayOn ) {
self.mStayOn = true;
self.mDirty |= PowerManagerService.DIRTY_STAY_ON;
}
}
} 
Carlo Pescio @CarloPescio http://aspectroid.com
Is it worth doing?
Core of science: experiments & falsifiability
port the aspects
from 5.0 to 6.0
Hardest candidate: PowerManagerService
Power mgmt changed in 6.0 to include Doze
Also the most fragile of my aspects
Carlo Pescio @CarloPescio http://aspectroid.com
File compare map
5.0 vs 6.0
Carlo Pescio @CarloPescio http://aspectroid.com
Still working?
Class: PowerManagerService still there
Member functions:
systemReady
updateStayOnLocked
Data members:
mContext, mSettingsObserver,
mStayOn, mDirty
still there, same
purpose & signature
still there,
same purpose

Carlo Pescio @CarloPescio http://aspectroid.com
Conclusions: core benefits
- Feature traceability and selection
- Features as artifacts, not changesets
- Simplified maintenance / porting
Carlo Pescio @CarloPescio http://aspectroid.com
Challenges
Aspectj is still “one project at a time”
Core is not aspect-friendly
very long methods, hard to pointcut inside
Core itself would actually benefit A LOT from AOP
ok google? 
Rejection of the unfamiliar
3-way compare is a known pain
branch X product in a product family is a known pain
Carlo Pescio @CarloPescio http://aspectroid.com
Why learning this stuff?
- New ways to solve problems
Customize FOSS by adding code
- New ways to structure your code
aspect-friendly
mix OOP and AOP
- In the end:
you maintain LESS code
[good] aspects can be quite resilient
Carlo Pescio @CarloPescio http://aspectroid.com
Get in touch
carlo.pescio@gmail.com
@CarloPescio
http://eptacom.net
http://aspectroid.com
1 of 41

Recommended

No Flex Zone: Empathy Driven Development by
No Flex Zone: Empathy Driven DevelopmentNo Flex Zone: Empathy Driven Development
No Flex Zone: Empathy Driven DevelopmentDuretti H.
5.1K views106 slides
Caballo de troya de descartes, de antonio hidalgo by
Caballo de troya de descartes, de antonio hidalgoCaballo de troya de descartes, de antonio hidalgo
Caballo de troya de descartes, de antonio hidalgoAntonio Hidalgo
219 views81 slides
Feedback gulele &amp;shuro meda 2009 by
Feedback gulele &amp;shuro meda 2009Feedback gulele &amp;shuro meda 2009
Feedback gulele &amp;shuro meda 2009berhanu taye
795 views32 slides
PyLadies Talk: Learn to love the command line! by
PyLadies Talk: Learn to love the command line!PyLadies Talk: Learn to love the command line!
PyLadies Talk: Learn to love the command line!Blanca Mancilla
424 views39 slides
Taureau Ailé by
Taureau AiléTaureau Ailé
Taureau AiléFrançois Jourde
10.2K views88 slides
Social Network Analysis With R by
Social Network Analysis With RSocial Network Analysis With R
Social Network Analysis With RDavid Chiu
5.8K views37 slides

More Related Content

What's hot

Interpret this... (PHPem 2016) by
Interpret this... (PHPem 2016)Interpret this... (PHPem 2016)
Interpret this... (PHPem 2016)James Titcumb
282 views64 slides
ពុទ្ធកិច្ច ៤៥ ព្រះវស្សា by
ពុទ្ធកិច្ច ៤៥ ព្រះវស្សាពុទ្ធកិច្ច ៤៥ ព្រះវស្សា
ពុទ្ធកិច្ច ៤៥ ព្រះវស្សាVantha Kago
350 views453 slides
Analyse transactionnelle et publicité by
Analyse transactionnelle et publicitéAnalyse transactionnelle et publicité
Analyse transactionnelle et publicitéFrançois Jourde
5.2K views122 slides
CFOT James Ferreira by
CFOT James FerreiraCFOT James Ferreira
CFOT James FerreiraJames Ferreira
81 views1 slide
BA UC by
BA UCBA UC
BA UCYem Marady
43 views1 slide

What's hot(13)

Interpret this... (PHPem 2016) by James Titcumb
Interpret this... (PHPem 2016)Interpret this... (PHPem 2016)
Interpret this... (PHPem 2016)
James Titcumb282 views
ពុទ្ធកិច្ច ៤៥ ព្រះវស្សា by Vantha Kago
ពុទ្ធកិច្ច ៤៥ ព្រះវស្សាពុទ្ធកិច្ច ៤៥ ព្រះវស្សា
ពុទ្ធកិច្ច ៤៥ ព្រះវស្សា
Vantha Kago350 views
Analyse transactionnelle et publicité by François Jourde
Analyse transactionnelle et publicitéAnalyse transactionnelle et publicité
Analyse transactionnelle et publicité
François Jourde5.2K views
Thesis Abstract & Intro by Andrew Nash
Thesis Abstract & IntroThesis Abstract & Intro
Thesis Abstract & Intro
Andrew Nash179 views
Sthiem Report4 Rip by guest62c54a
Sthiem Report4 RipSthiem Report4 Rip
Sthiem Report4 Rip
guest62c54a378 views
Le magazine Paranoia, Automne 2003. Vol 10, No 2, Issue 33 by Guy Boulianne
Le magazine Paranoia, Automne 2003. Vol 10, No 2, Issue 33Le magazine Paranoia, Automne 2003. Vol 10, No 2, Issue 33
Le magazine Paranoia, Automne 2003. Vol 10, No 2, Issue 33
Guy Boulianne804 views
Findes der millioner i velfærdsteknologi? by Lakeside A/S
Findes der millioner i velfærdsteknologi?Findes der millioner i velfærdsteknologi?
Findes der millioner i velfærdsteknologi?
Lakeside A/S339 views
Piotr Szotkowski about "Ruby smells" by Pivorak MeetUp
Piotr Szotkowski about "Ruby smells"Piotr Szotkowski about "Ruby smells"
Piotr Szotkowski about "Ruby smells"
Pivorak MeetUp413 views

Viewers also liked

Internal Android Library Management (DroidCon SF 2016, Droidcon Italy 2016) by
Internal Android Library Management (DroidCon SF 2016, Droidcon Italy 2016)Internal Android Library Management (DroidCon SF 2016, Droidcon Italy 2016)
Internal Android Library Management (DroidCon SF 2016, Droidcon Italy 2016)Kelly Shuster
14.3K views174 slides
Crafting Great Hypotheses - Droidcon 2016 by
Crafting Great Hypotheses - Droidcon 2016Crafting Great Hypotheses - Droidcon 2016
Crafting Great Hypotheses - Droidcon 2016Hoang Huynh
1.1K views41 slides
A realtime infrastructure for Android apps: Firebase may be what you need..an... by
A realtime infrastructure for Android apps: Firebase may be what you need..an...A realtime infrastructure for Android apps: Firebase may be what you need..an...
A realtime infrastructure for Android apps: Firebase may be what you need..an...Alessandro Martellucci
2K views39 slides
Add ClassyShark to your Android toolbox by
Add ClassyShark to your Android toolboxAdd ClassyShark to your Android toolbox
Add ClassyShark to your Android toolboxBoris Farber
3.8K views40 slides
Data Binding in Action using MVVM pattern by
Data Binding in Action using MVVM patternData Binding in Action using MVVM pattern
Data Binding in Action using MVVM patternFabio Collini
5.5K views79 slides
World-Class Testing Development Pipeline for Android by
 World-Class Testing Development Pipeline for Android World-Class Testing Development Pipeline for Android
World-Class Testing Development Pipeline for AndroidPedro Vicente Gómez Sánchez
9.3K views61 slides

Viewers also liked(10)

Internal Android Library Management (DroidCon SF 2016, Droidcon Italy 2016) by Kelly Shuster
Internal Android Library Management (DroidCon SF 2016, Droidcon Italy 2016)Internal Android Library Management (DroidCon SF 2016, Droidcon Italy 2016)
Internal Android Library Management (DroidCon SF 2016, Droidcon Italy 2016)
Kelly Shuster14.3K views
Crafting Great Hypotheses - Droidcon 2016 by Hoang Huynh
Crafting Great Hypotheses - Droidcon 2016Crafting Great Hypotheses - Droidcon 2016
Crafting Great Hypotheses - Droidcon 2016
Hoang Huynh1.1K views
A realtime infrastructure for Android apps: Firebase may be what you need..an... by Alessandro Martellucci
A realtime infrastructure for Android apps: Firebase may be what you need..an...A realtime infrastructure for Android apps: Firebase may be what you need..an...
A realtime infrastructure for Android apps: Firebase may be what you need..an...
Add ClassyShark to your Android toolbox by Boris Farber
Add ClassyShark to your Android toolboxAdd ClassyShark to your Android toolbox
Add ClassyShark to your Android toolbox
Boris Farber3.8K views
Data Binding in Action using MVVM pattern by Fabio Collini
Data Binding in Action using MVVM patternData Binding in Action using MVVM pattern
Data Binding in Action using MVVM pattern
Fabio Collini5.5K views
Engage and retain users in the android world - Droidcon Italy 2016 by Matteo Bonifazi
Engage and retain users in the android world - Droidcon Italy 2016Engage and retain users in the android world - Droidcon Italy 2016
Engage and retain users in the android world - Droidcon Italy 2016
Matteo Bonifazi1.2K views
A friend in need - A JS indeed by Yonatan Levin
A friend in need - A JS indeedA friend in need - A JS indeed
A friend in need - A JS indeed
Yonatan Levin1.1K views
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin by Xavier Hallade
Mastering the NDK with Android Studio 2.0 and the gradle-experimental pluginMastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Xavier Hallade23.2K views

Similar to Evolving the Android Core with Aspects

Frontend architecture on big and small sites by
Frontend architecture on big and small sitesFrontend architecture on big and small sites
Frontend architecture on big and small sitesToni Pinel
844 views49 slides
rpm-building-101.pdf by
rpm-building-101.pdfrpm-building-101.pdf
rpm-building-101.pdfstroganovboris
4 views26 slides
Create Custom Post Type Plugin by
Create Custom Post Type PluginCreate Custom Post Type Plugin
Create Custom Post Type PluginJan Wilson
10K views56 slides
Spacebrew MADess: Running Your Own Server by
Spacebrew MADess: Running Your Own ServerSpacebrew MADess: Running Your Own Server
Spacebrew MADess: Running Your Own ServerJulio Terra
7K views26 slides
From simple to more advanced: Lessons learned in 13 months with Tableau by
From simple to more advanced: Lessons learned in 13 months with TableauFrom simple to more advanced: Lessons learned in 13 months with Tableau
From simple to more advanced: Lessons learned in 13 months with TableauSergii Khomenko
546 views25 slides
Ford ranger xlt by
Ford   ranger xltFord   ranger xlt
Ford ranger xltJose Luis Vanegas
7 views1 slide

Similar to Evolving the Android Core with Aspects(20)

Frontend architecture on big and small sites by Toni Pinel
Frontend architecture on big and small sitesFrontend architecture on big and small sites
Frontend architecture on big and small sites
Toni Pinel844 views
Create Custom Post Type Plugin by Jan Wilson
Create Custom Post Type PluginCreate Custom Post Type Plugin
Create Custom Post Type Plugin
Jan Wilson10K views
Spacebrew MADess: Running Your Own Server by Julio Terra
Spacebrew MADess: Running Your Own ServerSpacebrew MADess: Running Your Own Server
Spacebrew MADess: Running Your Own Server
Julio Terra7K views
From simple to more advanced: Lessons learned in 13 months with Tableau by Sergii Khomenko
From simple to more advanced: Lessons learned in 13 months with TableauFrom simple to more advanced: Lessons learned in 13 months with Tableau
From simple to more advanced: Lessons learned in 13 months with Tableau
Sergii Khomenko546 views
Modeling avengers – open source technology mix for saving the world econ fr by Cédric Brun
Modeling avengers – open source technology mix for saving the world econ frModeling avengers – open source technology mix for saving the world econ fr
Modeling avengers – open source technology mix for saving the world econ fr
Cédric Brun448 views
Modeling avengers – open source technology mix for saving the world by Cédric Brun
Modeling avengers – open source technology mix for saving the worldModeling avengers – open source technology mix for saving the world
Modeling avengers – open source technology mix for saving the world
Cédric Brun841 views
Ultra Basic Guide to Photography by Owen Wang
Ultra Basic Guide to PhotographyUltra Basic Guide to Photography
Ultra Basic Guide to Photography
Owen Wang479 views
Debugging Your CDN - Austin Spires at Fastly Altitude 2015 by Fastly
Debugging Your CDN - Austin Spires at Fastly Altitude 2015Debugging Your CDN - Austin Spires at Fastly Altitude 2015
Debugging Your CDN - Austin Spires at Fastly Altitude 2015
Fastly1.1K views
Geb for Testing Your Grails Application GR8Conf India 2016 by Jacob Aae Mikkelsen
Geb for Testing Your Grails Application  GR8Conf India 2016Geb for Testing Your Grails Application  GR8Conf India 2016
Geb for Testing Your Grails Application GR8Conf India 2016
Jacob Aae Mikkelsen1.1K views
Sahana castle restaurant menu by Mani Krishna
Sahana castle restaurant menuSahana castle restaurant menu
Sahana castle restaurant menu
Mani Krishna228 views
People + Machines by Digiday
People + MachinesPeople + Machines
People + Machines
Digiday460 views
Pane'ramik - Effective Marketing with Video by Blake Rea
Pane'ramik - Effective Marketing with VideoPane'ramik - Effective Marketing with Video
Pane'ramik - Effective Marketing with Video
Blake Rea33 views

Recently uploaded

DSD-INT 2023 Modelling litter in the Yarra and Maribyrnong Rivers (Australia)... by
DSD-INT 2023 Modelling litter in the Yarra and Maribyrnong Rivers (Australia)...DSD-INT 2023 Modelling litter in the Yarra and Maribyrnong Rivers (Australia)...
DSD-INT 2023 Modelling litter in the Yarra and Maribyrnong Rivers (Australia)...Deltares
9 views34 slides
Neo4j y GenAI by
Neo4j y GenAI Neo4j y GenAI
Neo4j y GenAI Neo4j
42 views41 slides
Tridens DevOps by
Tridens DevOpsTridens DevOps
Tridens DevOpsTridens
9 views28 slides
Roadmap y Novedades de producto by
Roadmap y Novedades de productoRoadmap y Novedades de producto
Roadmap y Novedades de productoNeo4j
50 views33 slides
El Arte de lo Possible by
El Arte de lo PossibleEl Arte de lo Possible
El Arte de lo PossibleNeo4j
38 views35 slides
Navigating container technology for enhanced security by Niklas Saari by
Navigating container technology for enhanced security by Niklas SaariNavigating container technology for enhanced security by Niklas Saari
Navigating container technology for enhanced security by Niklas SaariMetosin Oy
8 views34 slides

Recently uploaded(20)

DSD-INT 2023 Modelling litter in the Yarra and Maribyrnong Rivers (Australia)... by Deltares
DSD-INT 2023 Modelling litter in the Yarra and Maribyrnong Rivers (Australia)...DSD-INT 2023 Modelling litter in the Yarra and Maribyrnong Rivers (Australia)...
DSD-INT 2023 Modelling litter in the Yarra and Maribyrnong Rivers (Australia)...
Deltares9 views
Neo4j y GenAI by Neo4j
Neo4j y GenAI Neo4j y GenAI
Neo4j y GenAI
Neo4j42 views
Tridens DevOps by Tridens
Tridens DevOpsTridens DevOps
Tridens DevOps
Tridens9 views
Roadmap y Novedades de producto by Neo4j
Roadmap y Novedades de productoRoadmap y Novedades de producto
Roadmap y Novedades de producto
Neo4j50 views
El Arte de lo Possible by Neo4j
El Arte de lo PossibleEl Arte de lo Possible
El Arte de lo Possible
Neo4j38 views
Navigating container technology for enhanced security by Niklas Saari by Metosin Oy
Navigating container technology for enhanced security by Niklas SaariNavigating container technology for enhanced security by Niklas Saari
Navigating container technology for enhanced security by Niklas Saari
Metosin Oy8 views
Software evolution understanding: Automatic extraction of software identifier... by Ra'Fat Al-Msie'deen
Software evolution understanding: Automatic extraction of software identifier...Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...
DSD-INT 2023 European Digital Twin Ocean and Delft3D FM - Dols by Deltares
DSD-INT 2023 European Digital Twin Ocean and Delft3D FM - DolsDSD-INT 2023 European Digital Twin Ocean and Delft3D FM - Dols
DSD-INT 2023 European Digital Twin Ocean and Delft3D FM - Dols
Deltares7 views
Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI... by Marc Müller
Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI...Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI...
Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI...
Marc Müller36 views
What Can Employee Monitoring Software Do?​ by wAnywhere
What Can Employee Monitoring Software Do?​What Can Employee Monitoring Software Do?​
What Can Employee Monitoring Software Do?​
wAnywhere21 views
DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM... by Deltares
DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM...DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM...
DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM...
Deltares7 views
DSD-INT 2023 FloodAdapt - A decision-support tool for compound flood risk mit... by Deltares
DSD-INT 2023 FloodAdapt - A decision-support tool for compound flood risk mit...DSD-INT 2023 FloodAdapt - A decision-support tool for compound flood risk mit...
DSD-INT 2023 FloodAdapt - A decision-support tool for compound flood risk mit...
Deltares13 views
Headless JS UG Presentation.pptx by Jack Spektor
Headless JS UG Presentation.pptxHeadless JS UG Presentation.pptx
Headless JS UG Presentation.pptx
Jack Spektor7 views
DSD-INT 2023 - Delft3D User Days - Welcome - Day 3 - Afternoon by Deltares
DSD-INT 2023 - Delft3D User Days - Welcome - Day 3 - AfternoonDSD-INT 2023 - Delft3D User Days - Welcome - Day 3 - Afternoon
DSD-INT 2023 - Delft3D User Days - Welcome - Day 3 - Afternoon
Deltares13 views
Copilot Prompting Toolkit_All Resources.pdf by Riccardo Zamana
Copilot Prompting Toolkit_All Resources.pdfCopilot Prompting Toolkit_All Resources.pdf
Copilot Prompting Toolkit_All Resources.pdf
Riccardo Zamana6 views
DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut... by Deltares
DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut...DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut...
DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut...
Deltares6 views
SUGCON ANZ Presentation V2.1 Final.pptx by Jack Spektor
SUGCON ANZ Presentation V2.1 Final.pptxSUGCON ANZ Presentation V2.1 Final.pptx
SUGCON ANZ Presentation V2.1 Final.pptx
Jack Spektor22 views
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema by Deltares
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - GeertsemaDSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema
Deltares17 views

Evolving the Android Core with Aspects

  • 1. Carlo Pescio @CarloPescio http://aspectroid.com Carlo Pescio Evolving the Android Core with Aspects
  • 2. Carlo Pescio @CarloPescio http://aspectroid.com Android on custom devices New hardware: - Ethernet - TV tuner - Modbus Different target / product line / market: - Not a “personal device” - Keeping “user data” not an option - USB devices should autostart apps w/out asking user - Foreground app can’t win - Change low memory policies
  • 4. Carlo Pescio @CarloPescio http://aspectroid.com AudioManager int mBecomingNoisyIntentDevices = AudioSystem.DEVICE_OUT_WIRED_HEADSET | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE | AudioSystem.DEVICE_OUT_ALL_A2DP | AudioSystem.DEVICE_OUT_HDMI | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | AudioSystem.DEVICE_OUT_ALL_USB | AudioSystem.DEVICE_OUT_LINE;
  • 5. Carlo Pescio @CarloPescio http://aspectroid.com AudioManager private void onSetWiredDeviceConnectionState(int device, int state, String name) { synchronized (mConnectedDevices) { if((state == 0) && ((device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) || (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE)|| (device == AudioSystem.DEVICE_OUT_LINE))) { setBluetoothA2dpOnInt(true); } boolean isUsb = ((device & ~AudioSystem.DEVICE_OUT_ALL_USB) == 0) || (((device & AudioSystem.DEVICE_BIT_IN) != 0) && ((device & ~AudioSystem.DEVICE_IN_ALL_USB) == 0)); handleDeviceConnection((state == 1), device, (isUsb ? name : "")); if (state != 0) { if ((device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) || (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE) || (device == AudioSystem.DEVICE_OUT_LINE)) { setBluetoothA2dpOnInt(false); }
  • 6. Carlo Pescio @CarloPescio http://aspectroid.com Evolving open source code Plugin architecture: lucky you Or: just fork & change the damn code pull + 3 way compare • Feature / Artifact symmetry is broken • Features are scattered in small patches • Product families => multiple forks
  • 7. Carlo Pescio @CarloPescio http://aspectroid.com aspectroid exploring the aspect-oriented design space
  • 8. Carlo Pescio @CarloPescio http://aspectroid.com Aspect Orientation Born to address Cross-Cutting, Scattered Concerns Error handling, Transaction handling Not very successful so far No design discipline evolved Core features: Customize behavior w/out changing code Avoid scattering a single concern around
  • 9. Carlo Pescio @CarloPescio http://aspectroid.com AspectJ crash course Inter-type declaration: add data members, member functions, implement interfaces, … aspect ScreenStateSettingsAspect { private CheckBoxPreference DevelopmentSettings.stayOnPreference; }
  • 10. Carlo Pescio @CarloPescio http://aspectroid.com AspectJ crash course Pointcut (the “where”) identify matching parts of code (sophisticated syntax) pointcut settingsUpdated() : execution( private void PowerManagerService.updateStayOnLocked(int)); pointcut adbSet() : within(UsbDeviceManager) && set(boolean *.mAdbEnabled);
  • 11. Carlo Pescio @CarloPescio http://aspectroid.com AspectJ crash course Advice (the when and how) execute code before / after / around code matched by a pointcut (join point) after(boolean newVal) : adbSet() && args(newVal) { // do something here }
  • 12. Carlo Pescio @CarloPescio http://aspectroid.com AspectJ crash course Aspect scoping mechanism; also: artifact privileged aspect ScreenStatePowerAspect { … inter-type declarations … pointcuts … advices … functions … data }
  • 13. Carlo Pescio @CarloPescio http://aspectroid.com AspectJ & Android Apps: easy download plugin, add AspectJ nature to project Core: not so easy Shell calling Makefiles calling Python calling make Strong expectations (class files etc.) AJC can compile regular Java files aspects don’t need .aj extension, .java is ok
  • 14. Carlo Pescio @CarloPescio http://aspectroid.com AspectJ in the core - Create a compiler wrapper around ajc in C - Standardizes options and behavior - Like: files in a file list must have absolute path - Minimal changes to the build scripts - If you link with the aspectjrt library, I call the compiler wrapper - Applicable to other JVM languages
  • 15. # C o m m o n d e f i n i t i o n t o i n v o k e j a v a c o n t h e h o s t a n d t a r g e t . # # S o m e h i s t o r i c a l n o t e s : # - b e l o w w e w r i t e t h e l i s t o f j a v a f i l e s t o j a v a - s o u r c e - l i s t t o a v o i d a r g u m e n t # l i s t l e n g t h p r o b l e m s w i t h C y g w i n # - w e f i l t e r o u t d u p l i c a t e j a v a f i l e n a m e s b e c a u s e e c l i p s e ' s c o m p i l e r # d o e s n ' t l i k e t h e m . # # $ ( 1 ) : j a v a c # $ ( 2 ) : b o o t c l a s s p a t h d e f i n e c o m p i l e - j a v a $ ( h i d e ) r m - f $ @ $ ( h i d e ) r m - r f $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) $ ( h i d e ) m k d i r - p $ ( d i r $ @ ) $ ( h i d e ) m k d i r - p $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) $ ( c a l l u n z i p - j a r - f i l e s , $ ( P R I V A T E _ S T A T I C _ J A V A _ L I B R A R I E S ) , $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) ) $ ( c a l l d u m p - w o r d s - t o - f i l e , $ ( P R I V A T E _ J A V A _ S O U R C E S ) , $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) / j a v a - s o u r c e - l i s t ) $ ( h i d e ) i f [ - d " $ ( P R I V A T E _ S O U R C E _ I N T E R M E D I A T E S _ D I R ) " ] ; t h e n f i n d $ ( P R I V A T E _ S O U R C E _ I N T E R M E D I A T E S _ D I R ) - n a m e ' * . j a v a ' > > $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) / j a v a - s o u r c e - l i s t ; f i $ ( h i d e ) t r ' ' ' n ' < $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) / j a v a - s o u r c e - l i s t | s o r t - u > $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) / j a v a - s o u r c e - l i s t - u n i q $ ( h i d e ) i f [ - s $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) / j a v a - s o u r c e - l i s t - u n i q ] ; t h e n $ ( i f $ ( f i n d s t r i n g a s p e c t j r t , $ ( P R I V A T E _ S T A T I C _ J A V A _ L I B R A R I E S ) ) , $ ( s u b s t j a v a c , a j c w , $ ( 1 ) ) , $ ( 1 ) ) - e n c o d i n g U T F - 8 $ ( s t r i p $ ( P R I V A T E _ J A V A C _ D E B U G _ F L A G S ) ) $ ( i f $ ( f i n d s t r i n g t r u e , $ ( P R I V A T E _ W A R N I N G S _ E N A B L E ) ) , $ ( x l i n t _ u n c h e c k e d ) , ) $ ( 2 ) $ ( a d d p r e f i x - c l a s s p a t h , $ ( s t r i p $ ( c a l l n o r m a l i z e - p a t h - l i s t , $ ( P R I V A T E _ A L L _ J A V A _ L I B R A R I E S ) ) ) ) $ ( i f $ ( f i n d s t r i n g t r u e , $ ( P R I V A T E _ W A R N I N G S _ E N A B L E ) ) , $ ( x l i n t _ u n c h e c k e d ) , ) - e x t d i r s " " - d $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) $ ( P R I V A T E _ J A V A C F L A G S ) @ $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) / j a v a - s o u r c e - l i s t - u n i q | | ( r m - r f $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) ; e x i t 4 1 ) f i $ ( i f $ ( P R I V A T E _ J A V A _ L A Y E R S _ F I L E ) , $ ( h i d e ) b u i l d / t o o l s / j a v a - l a y e r s . p y $ ( P R I V A T E _ J A V A _ L A Y E R S _ F I L E ) @ $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) / j a v a - s o u r c e - l i s t - u n i q , ) $ ( h i d e ) r m - f $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) / j a v a - s o u r c e - l i s t $ ( h i d e ) r m - f $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) / j a v a - s o u r c e - l i s t - u n i q $ ( i f $ ( P R I V A T E _ J A R _ E X C L U D E _ F I L E S ) , $ ( h i d e ) f i n d $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) - n a m e $ ( w o r d 1 , $ ( P R I V A T E _ J A R _ E X C L U D E _ F I L E S ) ) $ ( a d d p r e f i x - o - n a m e , $ ( w o r d l i s t 2 , 9 9 9 , $ ( P R I V A T E _ J A R _ E X C L U D E _ F I L E S ) ) ) | x a r g s r m - r f ) $ ( i f $ ( P R I V A T E _ J A R _ P A C K A G E S ) , $ ( h i d e ) f i n d $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) - m i n d e p t h 1 - t y p e f $ ( f o r e a c h p k g , $ ( P R I V A T E _ J A R _ P A C K A G E S ) , - n o t - p a t h $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) / $ ( s u b s t . , / , $ ( p k g ) ) / * ) - d e l e t e ; f i n d $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) - e m p t y - d e l e t e ) $ ( i f $ ( P R I V A T E _ J A R _ E X C L U D E _ P A C K A G E S ) , $ ( h i d e ) r m - r f $ ( f o r e a c h p k g , $ ( P R I V A T E _ J A R _ E X C L U D E _ P A C K A G E S ) , $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) / $ ( s u b s t . , / , $ ( p k g ) ) ) ) $ ( i f $ ( P R I V A T E _ R M T Y P E D E F S ) , $ ( h i d e ) $ ( R M T Y P E D E F S ) - v $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) ) $ ( i f $ ( P R I V A T E _ J A R _ M A N I F E S T ) , $ ( h i d e ) s e d - e ' s / % B U I L D _ N U M B E R % / $ ( B U I L D _ N U M B E R ) / ' $ ( P R I V A T E _ J A R _ M A N I F E S T ) > $ ( d i r $ @ ) / m a n i f e s t . m f & & j a r - c f m $ @ $ ( d i r $ @ ) / m a n i f e s t . m f - C $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) . , $ ( h i d e ) j a r - c f $ @ - C $ ( P R I V A T E _ C L A S S _ I N T E R M E D I A T E S _ D I R ) . ) e n d e f AspectJ in the core Definitions.mk -> define compile-java $(if $(findstring aspectjrt, $(PRIVATE_STATIC_JAVA_LIBRARIES)), $(subst javac,ajcw,$(1)), $(1))
  • 16. Does it actually work? Add a useful feature without changing sources
  • 17. Carlo Pescio @CarloPescio http://aspectroid.com How do we start? • Explore code • “Stay on while charging” • UsbDeviceManager, PowerManagerService • See ebook for details on the quest • Tested as much as possible outside the core • See ebook again • Add aspects • Make aspects robust !
  • 18. A map
  • 19. A map
  • 20. A map
  • 21. A map
  • 22. Carlo Pescio @CarloPescio http://aspectroid.com Extend the Settings app - Add a new checkbox - Persist the checkbox state somewhere - Show the persisted value on resume
  • 23. Settings Aspect (1/3) privileged aspect ScreenStateSettingsAspect { private CheckBoxPreference DevelopmentSettings.stayOnPreference; pointcut onCreateExecuted() : execution(public void DevelopmentSettings.onCreate(..)) ; pointcut onResumeExecuted() : execution(public void DevelopmentSettings.onResume(..)) ;
  • 24. Settings Aspect (2/3) after(DevelopmentSettings self):onCreateExecuted()&&target(self) { final Context ctx = self.getActivity(); PreferenceCategory targetCategory = (PreferenceCategory)self. findPreference(DevelopmentSettings.DEBUG_DEBUGGING_CATEGORY_KEY); self.stayOnPreference = new CheckBoxPreference(ctx); self.stayOnPreference.setKey("aspectroidScreenState"); self.stayOnPreference.setTitle("Prevent sleeping while debugging"); self.stayOnPreference.setSummary("only for usb debugging"); targetCategory.addPreference(self.stayOnPreference); self.stayOnPreference.setOnPreferenceChangeListener( new OnPreferenceChangeListener() { public boolean onPreferenceChange( Preference preference, Object newValue) { API.persistPreference(ctx, (Boolean) newValue); return true; } }); }
  • 25. Settings Aspect (3/3) after(DevelopmentSettings self) : onResumeExecuted() && target(self) { final Context ctx = self.getActivity(); boolean on = API.retrievePreference(ctx); self.stayOnPreference.setChecked(on); } } 
  • 26. Carlo Pescio @CarloPescio http://aspectroid.com Am I debugging? - USB debugging enabled - USB connected to a computer - Not just charging - Not in OTG / host mode
  • 27. Carlo Pescio @CarloPescio http://aspectroid.com UsbDeviceManager See ebook for details
  • 28. USB Aspect (1/3) aspect ScreenStateUsbAspect { boolean adbEnabled = false; boolean adbConnected = false; Context context; before(Context ctx): execution(UsbDeviceManager.new(Context)) && args( ctx ) { context = ctx; }
  • 29. USB Aspect (2/3) pointcut adbSet() : within(UsbDeviceManager) && set(boolean *.mAdbEnabled); after(boolean newVal) : adbSet() && args(newVal) { adbEnabled = newVal; checkAdbOn(); }
  • 30. USB Aspect (3/3) pointcut connectedSet() : within(UsbDeviceManager) && set(boolean *.mConnected); after(boolean newVal) : connectedSet() && args(newVal) { adbConnected = newVal; checkAdbOn(); } void checkAdbOn() { boolean isOn = adbEnabled && adbConnected; API.storeAdbState(context, isOn); } } 
  • 31. PowerManagerService (1) public void systemReady(IAppOpsService appOps) { // … final ContentResolver resolver = mContext.getContentResolver(); // … resolver.registerContentObserver( Settings.Global.getUriFor( Settings.Global.STAY_ON_WHILE_PLUGGED_IN), false, mSettingsObserver, UserHandle.USER_ALL); //… updateSettingsLocked(); //… }
  • 32. PowerManagerService (2) private void updateStayOnLocked(int dirty) { if( (dirty & (DIRTY_BATTERY_STATE | DIRTY_SETTINGS)) != 0 ) { final boolean wasStayOn = mStayOn; if( mStayOnWhilePluggedInSetting != 0 && ! isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()){ mStayOn = mBatteryManagerInternal.isPowered( mStayOnWhilePluggedInSetting); } else { mStayOn = false; } if( mStayOn != wasStayOn ) { mDirty |= DIRTY_STAY_ON; } } }
  • 33. Power Aspect (1/2) privileged aspect ScreenStatePowerAspect { pointcut systemReadyExecuted() : execution( public void PowerManagerService.systemReady(..)); after(PowerManagerService self) : systemReadyExecuted() && target(self) { API.registerScreenOnObserver(self.mContext, self.mSettingsObserver); }
  • 34. Power Aspect (2/2) pointcut settingsUpdated() : execution( private void PowerManagerService.updateStayOnLocked(int)); void around(PowerManagerService self, int dirty) : settingsUpdated() && args(dirty) && target(self) { proceed(self, dirty); boolean keepOn = API.shouldKeepScrenOn(self.mContext); if( keepOn && !self.mStayOn ) { self.mStayOn = true; self.mDirty |= PowerManagerService.DIRTY_STAY_ON; } } } 
  • 35. Carlo Pescio @CarloPescio http://aspectroid.com Is it worth doing? Core of science: experiments & falsifiability port the aspects from 5.0 to 6.0 Hardest candidate: PowerManagerService Power mgmt changed in 6.0 to include Doze Also the most fragile of my aspects
  • 36. Carlo Pescio @CarloPescio http://aspectroid.com File compare map 5.0 vs 6.0
  • 37. Carlo Pescio @CarloPescio http://aspectroid.com Still working? Class: PowerManagerService still there Member functions: systemReady updateStayOnLocked Data members: mContext, mSettingsObserver, mStayOn, mDirty still there, same purpose & signature still there, same purpose 
  • 38. Carlo Pescio @CarloPescio http://aspectroid.com Conclusions: core benefits - Feature traceability and selection - Features as artifacts, not changesets - Simplified maintenance / porting
  • 39. Carlo Pescio @CarloPescio http://aspectroid.com Challenges Aspectj is still “one project at a time” Core is not aspect-friendly very long methods, hard to pointcut inside Core itself would actually benefit A LOT from AOP ok google?  Rejection of the unfamiliar 3-way compare is a known pain branch X product in a product family is a known pain
  • 40. Carlo Pescio @CarloPescio http://aspectroid.com Why learning this stuff? - New ways to solve problems Customize FOSS by adding code - New ways to structure your code aspect-friendly mix OOP and AOP - In the end: you maintain LESS code [good] aspects can be quite resilient
  • 41. Carlo Pescio @CarloPescio http://aspectroid.com Get in touch carlo.pescio@gmail.com @CarloPescio http://eptacom.net http://aspectroid.com