Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming...
Transcript of Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming...
![Page 1: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/1.jpg)
Android App Programming
Unit 2: Databases, Runtime, UX Programming
Thomas NowakUniversité Paris-Sud
![Page 2: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/2.jpg)
Last Time
![Page 3: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/3.jpg)
Activities
• Single block of user interaction
• In the simplest case, one screen
• Can be a window or part of the screen
• To display something, set its content:
• setContentView(View)
![Page 4: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/4.jpg)
Intents
• Mechanism to launch activities from other activities
• To change activity: need to create object of type Intent, then call the method startActivity with that object
• ex :Intent i = new Intent(this, OtherActivity.class); startActivity(i);
![Page 5: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/5.jpg)
Bundles
• Mechanism to pass data to activities: Bundles
• Can put data in and retrieve it
• ex:bundle.putFloat(“pi”, 3.14); bundle.getFloat(“pi”);
![Page 6: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/6.jpg)
Web services
• web service = set of responses from a web server that aren’t HTML
• return formats: typically JSON or XML
• query formats: often HTTP GET request w/ query strings
• ex: https://www.google.com/search?q=hi
• find web APIs: www.programmableweb.com
![Page 7: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/7.jpg)
Databases
![Page 8: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/8.jpg)
Saving State• several possibilities:
• passing bundles (ugh)
• write into a file (uuugh)
• databases
• preferencesSharedPreferences prefs = getPreferences(MODE_PRIVATE)
![Page 9: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/9.jpg)
Databases• SQL databases:
• relational data model
• proven, robust, optimized
• NoSQL databases:
• different data model, e.g., objects, graphs
• less data translation necessary
![Page 10: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/10.jpg)
Databases
• Physical location of data varies:
• locally (on the phone)
• remote (on one or more servers on the internet)
• e.g., Firebase
![Page 11: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/11.jpg)
SQLite
• DBMS integrated into Android: SQLite
• packaged into the app
• runs in the same process
• no connection setup necessary
• use the class SQLiteDatabase included in Android
![Page 12: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/12.jpg)
SQLite in Android• get instance of SQLiteDatabase via openOrCreateDatabase
• call db.rawQuery with SQL statement for results
• loop over result set with a Cursor
• for SQL statements without result, call db.execSQL
• there are specialized methods for some operations, e.g., db.insert
![Page 13: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/13.jpg)
Android Runtime
![Page 14: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/14.jpg)
Runtime• runtime = execution environment of programs
• implements the computational model of the programming language
• ex: C calling conventions
• call stack, parameter/return value passing, caller/callee saved registers
• ex: Python interpreter
• ex: Java Virtual Machine
![Page 15: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/15.jpg)
Non-virtual Machines
ALU
R1 R2
R1
add R1, R2
![Page 16: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/16.jpg)
else if(strncmp(op, "add", OP_LEN) == 0) { int *op1 = find_reg(arg1); int *op2 = find_reg(arg2); int *res = op1; *res = *op1 + *op2; program_counter++; } else if(strncmp(op, "sub", OP_LEN) == 0) { int *op1 = find_reg(arg1);
Virtual Machines
ALU
R1 R2
R1
add R1, R2add R1, R2
![Page 17: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/17.jpg)
Virtual Machineselse if(strncmp(op, "add", OP_LEN) == 0) { int *op1 = find_reg(arg1); int *op2 = find_reg(arg2); int *res = op1; *res = *op1 + *op2; program_counter++; } else if(strncmp(op, "sub", OP_LEN) == 0) { int *op1 = find_reg(arg1);
![Page 18: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/18.jpg)
Dalvik and ART
![Page 19: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/19.jpg)
JIT in ART
![Page 20: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/20.jpg)
Stack vs Heap• variables on the stack: implicitly destroyed after method
return (pop of stack frame)
• variables/constantes on the heap: destruction isn’t implicit; no temporal or logical ordering between objects on the heap
• error source in C++: managing the heap, in particular failure to destroy unused objects (memory leak)
• alternative of manually managing the heap: automatic garbage collection by the runtime
![Page 21: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/21.jpg)
Garbage Collection• garbage collection = automatic identification and of
unused objects
• basic mechanism: identification of unused object based on active references
• choice of moment of collection
• goal: don’t block execution of the program
• additional challenge: avoid fragmentation of memory
![Page 22: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/22.jpg)
Reference Counting
• idea: count the number of references to every object
• if count = 0, then destroy the object
• destroying one object can (transitively) cause destruction of a large number of other objects; problematic if immediate destruction
• problem with cyclic references
![Page 23: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/23.jpg)
Mark-and-Sweep
• mark: calculate successors in the directed graph of objects and their references
• sweep: destroy all non-marked objects
• moment of collection:
• stop-the-world
• concurrent
![Page 24: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/24.jpg)
Android Runtime
• originally Dalvik virtual machine
• now ART w/ AOT and JIT compilation of .dex files to native code
• runtime includes garbage collector (even outside of VM)
![Page 25: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/25.jpg)
UX Programming
![Page 26: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/26.jpg)
UX Design
![Page 27: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/27.jpg)
Material Design
![Page 28: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/28.jpg)
Constraint Layout
![Page 29: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/29.jpg)
Fragments
![Page 30: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/30.jpg)
Hamburger Menus
![Page 31: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/31.jpg)
Notifications and Dialogs
![Page 32: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/32.jpg)
MVC
![Page 33: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/33.jpg)
MVP
![Page 34: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/34.jpg)
MVVM
![Page 35: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/35.jpg)
Reactive Programming
• reactive programming = asynchronous programming = callback methods = observer pattern
• manage streams of data
• well-adapted paradigm for asynchronous (UX) programs
• Android library: RxAndroid
![Page 36: Android App Programmingnowak/teaching/android/Lecture02.pdf · 2018-11-06 · Reactive Programming • reactive programming = asynchronous programming = callback methods = observer](https://reader034.fdocuments.us/reader034/viewer/2022052612/5f0f61367e708231d443deee/html5/thumbnails/36.jpg)
Questions