STORAGE CONTENT PROVIDER Storage File System Linux OS Internal - - PowerPoint PPT Presentation
STORAGE CONTENT PROVIDER Storage File System Linux OS Internal - - PowerPoint PPT Presentation
STORAGE CONTENT PROVIDER Storage File System Linux OS Internal External (Flash Memory) (SD card) ..\AppData\Local\Android\sdk\platform-tools\adb Exploring the file system File System Android allows to persists application data via the
Storage
Internal (Flash Memory) External (SD card) Linux OS File System ..\AppData\Local\Android\sdk\platform-tools\adb
Exploring the file system
File System
Android allows to persists application data via the file system. For each application the Android system creates a data/data/[application package] directory
Storage options
- SharedPreference
- Small amount of data
- Key-value pairs
- Private to an Activity or
- Shared among Activities
- Internal storage
- Small to medium amount of data
- Private to the application
- External storage
- Not private data (songs, video files)
- Database (SQLite)
- Structured data
- Private to the application
Shared Preferences
- Persistent map of primitive types
- Automatically persisted among application sessions
- Activity.getPreference(mode)
- Returns: SharedPreference is associated to the activity
- not accessible from other activities in the same package
- Mode used to create the preference (private is recommended)
- Returns a SharedPreference object
- Context.getSharedPreference(Name,mode)
- Returns: SharedPreference associated to the package
- Name of the file used to store the preferences
- Mode
Writing/Reading SharedPreference
- Call SharedPreference.edit() method
- Returns an SharedPreference.Editor instance
- Call put methods to add values
- Call Editor.commit() to make value persistent
- Call get methods to retrieve values
- SharedPreferenceFragment allows for using a simple
framework to set preferences
Example
File
- Represented by the File class (java.io package)
- File can be internal…
- Internal flash memory in the device
- Usually used for private small amount of data
- …or external
- SD Card
- Larger non-private storage area
- A file can be opened for reading or writing…
- Standard java way to use the file
File
- External file are stored in removable storage (i.e., SD
card)
- Check for the presence of the storage before accessing
any file
- Environment.getExternalStorageState()
- State: Mounted, ReadOnly, etc.
- Environment is a class (java.os package) providing info about
environment variables.
- Requires permission in the manifest file
SQLite
- Used for structured information
- Small foot-print relational DB
- It supports ACID transaction
- Implements most of the SQL92 standard
- Stores data into a text file
- /data/data/<package-name>/databases/
- Options to create in-memory DB
- The adb tool has sql3 command to show the db
Content Provider
- A content provider manages access to a central repository
- f data
- A content provider presents data to external applications
as one or more tables that are similar to the tables found in a relational database.
- A row represents an instance of some type of data the
provider collects, and each column in the row represents an individual piece of data collected for an instance
- The data itself can be stored in an SQlite database, on
the file system, in flat files or on a remote server.
- Permissions are required to use a content provider
Content provider
A content provider is identified by URI (content://authority/path/id) It exposes a set of standard operations Accessed through intent or contentResolver Accessed from other processes Intent ‘bus’
Intent-Filter
Activity
<uses-permission>
Content Provider’s URI
- content://authority/path/id
- Content = means one want to access a content provider
- Authority = name of the content provider
- Path = specific data set (table) in the content provider
- Id = specific row in the table (option)
- For example:
- Content://com.android.contacts/contacts
Accessing a Content provider
- Clients access to a content provider through a
ContentResolver
- ContentResolver allows access to the provider through a
set of db-like methods
- Insert,Update,Query,Delete
Main content providers
- AlarmClock
- Alarms to fire
- CallLog
- contains information about placed and received calls
- Contact provider
- Stores all information about contacts.
- Calendar provider
- Data stored in the ‘calendar’ (events, etc.)
- Media Provider
- contains meta data for all available media on both internal and external storage
devices.
- Settings
- global system-level device preferences
- User Disctionary
- user defined words for input methods to use for predictive text input.
- Full list: adb shell dumpsys (look for Content Provider)
ContentResolver query
- ContentResover.query () allows to access specific data
- Similar to an SQL query
- Content URI
- Columns to retrieve – String []
- SQL selection pattern - String
- SQL selection arguments – String []
- Sort Order - String
- Returns a Cursor object
CursorLoader
- It is a class that uses an AsyncTask to perform the query
in background
- To use a CursorLoader one has to implement the
LoaderCallbacks interfece
Insert a row
Other methods
- ContenteResolver.update(
- URI
- ContentValues
- SQL selection pattern – String
- SQL selection args – String []
- Return number of rows updated
- ContentResolver.delete()
- URI
- SQL Selection pattern – String
- SQL Selection args. – String []
- Returns number of row deleted
Creating a ContentProvider
- Implement a storage system for the data
- For example an SQL DB, but other storage options are good as
well
- Define a contract Class
- ContantProvider subclass
- Implements update(), etc. methods
- Define manifest file
- <provide> tag
- Contains authoriries attribute
Example: Contact Provider
- Maintains three type of data about a person, each of one
corresponds to an entry into a different table
- ContactsContract.Contacts
- A row represents a summary of info about a person
- Some column: PHOTO_ID,PHOTO_URI
- ContactsContract.RawContacts
- ContactsContracy.Data
Example: Google calendar Provider
Interface (rest,etc)
Calendar’s data model
Accessing Content Provider via Intents
Loading data from the project
- A file is also considered a raw resourse
- An interesting application is to display an html page into a
webview
WebView
- Allows to host a web page
- The page can be loaded from internet or stored locally
- Requires INTERNET permission in the manifest
- It is based on WebKit engine
- Navigate back and forward
- Zoom in and out
- Load data, etc..
- It may execute javascript code
- By default it is disabled
Using web view
- An interesting point is that data can be loaded from a local
file (or even passed as a string)
- browser.loadData (“<html> <body> Hello!</body></html>”,”UTF-8”);
- browser.loadUrl ("file:///android_asset/my_local_webpage1.html");
Binding JavaScript code to Android code
- It is possible to create interfaces between JavaScript code
and client-side Android code.
- In other words, JavaScript code can call a method in the
Android code
- This is very powerful but it’s also a source of risk
- One can write application using JS and HTML (this is
what PhoneGap does)
Binding JavaScript code to Android code
- Public methods are seen as JS function
- The object that is bound to your JavaScript runs in
another thread and not in the thread in which it was constructed.
- Only simple data can be passed
- JSON can be useful in this respect
Android Code HTML + JS .. Android.test() Add Interface (C,“Android”) Android.test(); Class C test
public method