Sie sind auf Seite 1von 17

Mobile Phone Programming

Lecture 10 - Android Security, Audio/Video, and Camera


NTU CSIE http://mikechen.com

Announcements

HTC Tattoo Android phones for 15 teams today!

Sign out from

iPhone University Program access for all registered students


iPhones available in early December

You can also borrow iPhone/iPod Touch/Android phones from me Project UI presentation on Dec 4
NTU CSIE http://mikechen.com

HTC Tattoo

Android 1.6 2.8-inch QVGA touch-screen (240x320) resolution

vs HVGA (320x480) on Hero

Qualcomm MSM7225, 528 MHz Sensors:


GPS 3.2MP camera G-sensor Digital Compass FM Radio
NTU CSIE http://mikechen.com

Project UI Presentation

Email me your slides by Thur, Dec 3, 11:59pm

prex with your Team ID e.g. 09 Tangible Memory.ppt

Presentation:
dene main user tasks initial UI sketches 3 users feedback (with another team assigned by TA) revised UI (sketch or photoshop) related apps (iPhone and Android)
NTU CSIE http://mikechen.com

News This Week (Nov 27)

Google Chrome OS released

Designed to run web apps (ie. data in the cloud) Optimized for current hardware architecture

e.g. supports SSD only

Based on Linux

NTU CSIE http://mikechen.com

News This Week (Nov 27)

Google Chrome OS released

HTML5 supports ofine operations, background processing, etc.

NTU CSIE http://mikechen.com

News This Week



iPhone on sale in South Korea

Nov 28 by KT Corp

Apple App Store automating private API checks


Developers are supposed to only use public API calls Static analysis tools scans the submitted apps Lesson: dont use private APIs for your app store submission

NTU CSIE http://mikechen.com

Overview
Industry news Cool app

ZipCar iPhone app http://www.zipcar.com/ iphone/

Android Security and Permissions Android Image Capture

NTU CSIE http://mikechen.com

Security & Permissions

Security Architecture

Process-level security model



Standard Linux process security e.g. user and group IDs that are assigned to applications
NTU CSIE http://mikechen.com

Security Architecture
No application, by default, has permission to

e.g. reading/writing users private data reading/writing another applications les access the network keeping the device awake

adversely impact other applications, OS, or users

Application process is a secure sandbox


Explicitly declare permissions it needs for additional capabilities not provided by the basic sandbox
NTU CSIE http://mikechen.com

User IDs and File Access



Each Android package (.apk) is given its own unique Linux user ID

assigned when installed on the device

Two packages can not normally run in the same process


except when using the sharedUserId attribute in AndroidManifest.xml to have them assigned the same user ID

treated as being the same application, with the same user ID and le permission

must be signed using the same certicate


NTU CSIE http://mikechen.com

User IDs and File Access



Any data stored by the application will be assigned the applications user ID, and not normally accessible to other applications However, can set les/preferences/DB to be MODE_WORLD_READABLE and/or MODE_WORLD_WRITEABLE

Similar to chmod a+rw getSharedPreferences(String, int), openFileOutput(String, int) openOrCreateDatabase(String, int, ...)
NTU CSIE http://mikechen.com

Permissions
Android apps by default has no permissions
associated with it

Add one or more <uses-permission> tags to AndroidManifest.xml declaring the permissions that your application needs Otherwise, youll (often) see a security Exception

e.g.

java.lang.RuntimeException: Unable to resume activity {tw.edu.ntu/ tw.edu.ntu.Demo}: java.lang.SecurityException: Requires ACCESS_FINE_LOCATION permission
NTU CSIE http://mikechen.com

Permissions Example
<manifest xmlns:android="http://schemas.android.com/apk/res/android package="tw.edu.ntu" > <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></ uses-permission> </manifest>

NTU CSIE http://mikechen.com

Permission Checks
Permission checks with
user are done at app installation

No checks with user at runtime

Permission failures often

results in SecurityException
always printed to system log

NTU CSIE http://mikechen.com

URI Permissions
Often want to grant ne-grained access
permission

e.g. email app wants to open a video attachment using another app

Grant the receiving activity permission to


access the specic data URI in the Intent

Intent.FLAG_GRANT_READ_URI_PERMISSION and/or Intent.FLAG_GRANT_WRITE_URI_PERMISSION.

NTU CSIE http://mikechen.com

10min break

Audio Playback/Record

Audio/Video Playback
To play audio or video from your application,
use the MediaPlayer class.
raw resource le from the system an available network (URL)

Media can be played from


NTU CSIE http://mikechen.com

Playback from res/raw


Add audio les to res/raw Create an instance of MediaPlayer
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_1); mp.start(); mp.pause(); mp.stop()

To replay the media, reset() and prepare() the

create() calls prepare() the rst time.

MediaPlayer object before calling start() again.


NTU CSIE http://mikechen.com

Playback from le/network


Create an instance of MediaPlayer Use setDataSource() to specify content
MediaPlayer mp = new MediaPlayer(); mp.setDataSource("/sdcard/pixar.mp4"); mp.prepare(); mp.start()

To copy le to device:

adb push pixar.mp3 sdcard


NTU CSIE http://mikechen.com

Playback Video from le/network

Create an instance of VideoView


VideoView vv = (VideoView) findViewById(R.id.VideoView01); vv.setVideoPath("/sdcard/pixar.mp4"); vv.start();

But, it does not support res/raw

Use SurfaceView instead (and write lots more code)

NTU CSIE http://mikechen.com

Playback Video from res/raw


Setup SurfaceHolder.Callback
SurfaceView sv = (SurfaceView) findViewById (R.id.SurfaceView01); SurfaceHolder mHolder = sv.getHolder(); mHolder.addCallback(surfaceCallback); // doesnt own its buffers mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

NTU CSIE http://mikechen.com

Playback Video from res/raw


Congure the MediaPlayer after the surface has
been created in surfaceCreated(SurfaceHolder holder)
AssetFileDescriptor adf = mContext.getResources().openRawResourceFd (R.raw.pixar); mMp.setDataSource(adf.getFileDescriptor(), adf.getStartOffset(), adf.getLength()); mMp.setDisplay(holder); mMp.prepare(); mMp.start();
NTU CSIE http://mikechen.com

Image Capture

iPhone/Android Sensors (2009/9)



GPS (all Android, iPhone 3G/3GS) 3D accelerometer (all) Compass/Magnetometer (most Android, iPhone 3GS) Camera (all except iPod Touch) Microphone Bluetooth + WiFi

except in China and Korea

GSM/UMTS (GPRS/Edge/HSDPA) Proximity & light sensor


NTU CSIE http://mikechen.com

Camera vs Video
Camera uses full image sensor resolution Video is typically 320x240

Motorola Droid is 720x480 (HDTV 480p)

e.g. HTC Hero is 5.0 mega pixels

NTU CSIE http://mikechen.com

android.hardware.Camera
Connect/disconnect with the camera service:

set capture settings, start/stop preview, snap a picture, and retrieve frames for encoding for video.

There is no default constructor for this class.


Use open() to get a Camera object.
<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" />

NTU CSIE http://mikechen.com

Camera Live Preview


Also use SurfaceView
public void surfaceCreated(SurfaceHolder holder) { mCamera.setPreviewDisplay(holder); }
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Camera.Parameters parameters = mCamera.getParameters(); parameters.setPreviewSize(width, height); parameters.setPreviewFormat(PixelFormat.JPEG); mCamera.setParameters(parameters); mCamera.startPreview(); }
NTU CSIE http://mikechen.com

Camera Image Capture


Asynchronous takePicture(...)
camera.takePicture(Camera.ShutterCallback shutter, Camera.PictureCallback raw, Camera.PictureCallback jpeg); Camera.PictureCallback photoCallback = new Camera.PictureCallback() { public void onPictureTaken(byte[] data, Camera camera) { ... } }

NTU CSIE http://mikechen.com

Camera Frame Grab


Preview callback
mCamera.setPreviewCallback(new PreviewCallback(){ public void onPreviewFrame(byte[] data, Camera camera) { ... } };

NTU CSIE http://mikechen.com

Next 2 Weeks
Email me your UI presentation slides by Thur,
Dec 3, 11:59pm

iPhone guest speakers on Dec 11 Email me to borrow Android and iPhones Questions?
NTU CSIE http://mikechen.com

prex with your Team ID e.g. 09 Tangible Memory.ppt

phone for every team in early Dec

Das könnte Ihnen auch gefallen