storage content providerberaldi/macc_16/slides/08.pdfcontent provider • a content provider manages...

31
STORAGE CONTENT PROVIDER

Upload: others

Post on 22-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

STORAGECONTENT PROVIDER

Page 2: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

Storage

Internal(Flash Memory)

External(SD card)

Linux OS

File System

..\AppData\Local\Android\sdk\platform-tools\adb

Page 3: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

Exploring the file system

Page 4: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

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

Page 5: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

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

Page 6: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

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

Page 7: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

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

Page 8: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

Example

Page 9: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

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

Page 10: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

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

Page 11: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

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

Page 12: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

Content Provider• A content provider manages access to a central repository

of 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

Page 13: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

Content providerA content provider is identified by URI (content://authority/path/id)It exposes a set of standard operationsAccessed through intent or contentResolverAccessed from other processes

Intent ‘bus’

Intent-Filter

Activity

<uses-permission>

Page 14: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

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

Page 15: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

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

Page 16: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

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)

Page 17: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

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

Page 18: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

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

Page 19: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

Insert a row

Page 20: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

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

Page 21: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

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

Page 22: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

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

Page 23: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

Example: Google calendar Provider

Interface(rest,etc)

Page 24: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

Calendar’s data model

Page 25: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

Accessing Content Provider via Intents

Page 26: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

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

Page 27: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

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

Page 28: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

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");

Page 29: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

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)

Page 30: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

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 Ctest

public method

Page 31: STORAGE CONTENT PROVIDERberaldi/MACC_16/slides/08.pdfContent Provider • A content provider manages access to a central repository of data • A content provider presents data to

Navigation functions