STORAGE CONTENT PROVIDER Storage File System Linux OS Internal - - PowerPoint PPT Presentation

storage content provider storage
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

STORAGE CONTENT PROVIDER

slide-2
SLIDE 2

Storage

Internal (Flash Memory) External (SD card) Linux OS File System ..\AppData\Local\Android\sdk\platform-tools\adb

slide-3
SLIDE 3

Exploring the file system

slide-4
SLIDE 4

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

slide-5
SLIDE 5

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
slide-6
SLIDE 6

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
slide-7
SLIDE 7

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

slide-8
SLIDE 8

Example

slide-9
SLIDE 9

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
slide-10
SLIDE 10

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
slide-11
SLIDE 11

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
slide-12
SLIDE 12

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
slide-13
SLIDE 13

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>

slide-14
SLIDE 14

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
slide-15
SLIDE 15

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
slide-16
SLIDE 16

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)
slide-17
SLIDE 17

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
slide-18
SLIDE 18

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

slide-19
SLIDE 19

Insert a row

slide-20
SLIDE 20

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
slide-21
SLIDE 21

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
slide-22
SLIDE 22

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
slide-23
SLIDE 23

Example: Google calendar Provider

Interface (rest,etc)

slide-24
SLIDE 24

Calendar’s data model

slide-25
SLIDE 25

Accessing Content Provider via Intents

slide-26
SLIDE 26

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

slide-27
SLIDE 27

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
slide-28
SLIDE 28

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");
slide-29
SLIDE 29

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)

slide-30
SLIDE 30

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

slide-31
SLIDE 31

Navigation functions