Download presentation
Presentation is loading. Please wait.
Published byTobias Garrison Modified over 8 years ago
1
School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS – The Android Support Library, Persistent Storage, and Permissions Dr. Rainer Wasinger, Dr. James Montgomery School of Engineering and ICT
2
School of Engineering and Information and Communication Technology Mobile Device Operating System Updates 2 The frequency of OS updates can differ substantially per device manufacturer.
3
School of Engineering and Information and Communication Technology Mobile Device Operating System Updates 3
4
School of Engineering and Information and Communication Technology Mobile Device Operating System Updates 4
5
School of Engineering and Information and Communication Technology Mobile Device Operating System Updates 5
6
School of Engineering and Information and Communication Technology The Android Support Library What is it? –Originally released in 2011 as the Android Compatibility Library. –A collection of libraries that provides backward-compatible versions of the Android framework APIs. They are a collection, and divided into two categories: Compatibility libraries. Component libraries. What does it do? –Backports capabilities added to newer versions of Android. –Provides access to new widgets, containers, and other classes that Google wishes to make available for Android developers. Why is it needed? –To help support the huge number and variety of devices running different versions of Android OS. Consider: Smartphones, tablets, watches, televisions, cars, and the automated home. –Helps apps to be designed with a measure of consistency in the functionality and UX across across many devices. –Google considers the use of support libraries as best practice and includes these in most of its sample code and the Android Studio project templates. 6
7
School of Engineering and Information and Communication Technology The Android Support Library Two categories: –Compatibility libraries: These focus on back porting features from newer framework releases so that devices running previous releases can take advantage of the newer APIs. The most-used compatibilities are: v4 ( com.android.support:support-v4:23.2.1 ), which supports back to API 4 (Android OS 1.6), including things like Fragment and Loader. v7 ( com.android.support:appcompat-v7:23.2.1 ), which supports back to API 7 (Android OS 2.1), including things like ActionBar, Toolbar, and Material Design support. –Component libraries: These are smaller more modular libraries that enable developers to add features that are otherwise not part of the standard framework. Design support library ( com.android.support:design:23.2.1 ), providing support for various material design components and patterns such as navigation drawers, foating action buttons, snackbars, and tabs. Note: If you are including the v4 support and v7 appcompat libraries in your application, you should specify a minimum SDK version of "7" (and not "4"). The highest support library level you include in your application determines the lowest API version in which it can operate. 7
8
School of Engineering and Information and Communication Technology Using the Support Libraries These libraries are available via the Android SDK Manager. We include them in the Gradle build file: dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' } And can then access them via code: package au.edu.utas.propertyapp; import android.support.v7.app.AppCompatActivity;... public class PropertyApp extends AppCompatActivity implements OnItemSelectedListener { 8
9
School of Engineering and Information and Communication Technology Using the Support Libraries 9 apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "au.edu.utas.propertyapp" minSdkVersion 8 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' } Notice also the properties: compileSdkVersion buildToolsVersion minSdkVersion targetSdkVersion
10
School of Engineering and Information and Communication Technology Persistent Storage Several options to save persistent application data: –Shared Preferences –Internal Storage –External Storage –SQLite Databases –Network Connection Parsers: XML and JSON. –Android supports both XML parsing (e.g. org.xmlpull.v1XmlPullParser) and JSON parsing (android.util.JsonReader) Raw resources, XML resources, and Assets 10
11
School of Engineering and Information and Communication Technology Persistent Storage – Shared Preferences Provides a general framework to save and retrieve persistent key-value pairs of primitive data types. This is the simplest method for storing data. To use, we retrieve a SharedPreferences object as follows: SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); where : PREFS_NAME is the preferences file we wish to access, and MODE_PRIVATE means the created file can only be accessed by the calling application. To write or read values, we first call edit() to get the SharedPreferences.Editor SharedPreferences.Editor editor = settings.edit(); We can then write values using methods like putString() editor.putBoolean("silentMode", mSilentMode); And read values using methods like getString() editor.getBoolean("silentMode", mSilentMode); 11
12
School of Engineering and Information and Communication Technology Persistent Storage – Shared Preferences public class Calculator extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; @Override protected void onCreate(Bundle state){ super.onCreate(state);... // Restore preferences SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); boolean silent = settings.getBoolean("silentMode", false); setSilent(silent); } @Override protected void onStop(){ super.onStop(); SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("silentMode", mSilentMode); // Commit the edits! editor.commit(); } 12
13
School of Engineering and Information and Communication Technology Persistent Storage – Shared Preferences Multiple ways to retrieve SharedPreferences: –SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); Use this if you need multiple preference files –SharedPreferences settings = getPreferences(Context.MODE_PRIVATE); Use this if you need only one preference file. Also using a PreferenceManager, which can be used to help create preference hierarchies from XML files. 13
14
School of Engineering and Information and Communication Technology Persistent Storage – Internal Storage Files can also be stored on a device’s internal storage or external storage. –Internal storage: The on-board flash storage of a device, which is not accessible by the user, except via installed apps. –External storage: Shared storage that is accessible by the user (e.g. by mounting it as a drive on a host computer via a USB cable). This storage includes both primary external storage that may be inbuilt and removable storage media such as an SD card. 14 Internal StorageExternal Storage Is always available.Is not always available (e.g. when an SD card is removed). Files are accessible only by your app. It’s world-readable, so may be edited outside of your control. When the user uninstalls your app, the system removes all of your app’s files from internal storage as well. When the user uninstalls your app, the system only removes your app’s files if you save them using getExternalFilesDir().
15
School of Engineering and Information and Communication Technology Persistent Storage – Internal Storage To create a private file in internal storage and write to it: String FILENAME = "hello_file"; String writeString = "hello world!"; FileOutputStream fos; try { fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(writeString.getBytes()); fos.close(); } catch (Exception e) { e.printStackTrace(); } 15
16
School of Engineering and Information and Communication Technology Persistent Storage – Internal Storage To read a private file from internal storage: String FILENAME = "hello_file"; FileInputStream fis; InputStreamReader isr; BufferedReader bufferedReader; StringBuffer stringBuffer = new StringBuffer(""); try { fis = openFileInput(FILENAME); isr = new InputStreamReader(fis); bufferedReader = new BufferedReader (isr); String readString = bufferedReader.readLine(); while (readString!=null) { stringBuffer.append(readString); readString = bufferedReader.readLine(); } isr.close(); fis.close(); } catch (Exception e) { e.printStackTrace(); } 16
17
School of Engineering and Information and Communication Technology Persistent Storage – External Storage To read and write to external storage, your app must first acquire the following permissions: –READ_EXTERNAL_STORAGE –WRITE_EXTERNAL_STORAGE This is done by modifying the Android Manifest file:... If you need to both read and write files, then you need to request only the WRITE_EXTERNAL_STORAGE permission, because it implicitly requires read access as well. 17
18
School of Engineering and Information and Communication Technology Persistent Storage – External Storage Because it is possible that the external storage is not readable or writeable, we need to check the state of the external storage first: public boolean isExternalStorageWritable() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { return true; } return false; } public boolean isExternalStorageReadable() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { return true; } return false; } Other states returned by getExternalStorageState: –MEDIA_UNKNOWN, MEDIA_REMOVED, MEDIA_UNMOUNTED, MEDIA_CHECKING, MEDIA_NOFS, MEDIA_MOUNTED, MEDIA_MOUNTED_READ_ONLY, MEDIA_SHARED, MEDIA_BAD_REMOVAL, or MEDIA_UNMOUNTABLE. 18
19
School of Engineering and Information and Communication Technology Persistent Storage – External Storage Generally, files that the user will save through your app will typically be saved to a “public” location on the device. Some of the shared public directories include: –Music/, Pictures/, and Ringtones/ public File getAlbumStorageDir(String albumName) { // Get the directory for the user's public pictures directory. File file = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES), albumName); if (!file.mkdirs()) { Log.e(LOG_TAG, "Directory not created"); } return file; } 19
20
School of Engineering and Information and Communication Technology Persistent Storage – SQLite Databases Android provides full support for SQLite databases. Databases that you create are accessible by name to your application, but not outside the application. To create a database, we use a subclass of SQLiteOpenHelper and then override the onCreate() method: public class PropertyDB { private static final String DATABASE_NAME = "propertydata"; private static final int DATABASE_VERSION = 2; private static final String PROPERTY_TABLE= "property_data"; public static final String KEY_ROWID = "_id"; public static final String KEY_PROPERTY_ID= "property_id"; public static final String KEY_ADDRESS = "address"; public static final String KEY_PRICE = "price"; private static final String PROPERTY_TABLE_CREATE = "create table " + PROPERTY_TABLE + " (" + KEY_ROWID + " integer primary key autoincrement, " + KEY_PROPERTY_ID + " long integer not null, " + KEY_ADDRESS + " string not null, " + KEY_PRICE + " string not null, " private SQLiteDatabase mDb; private DatabaseHelper mDbHelper;... 20
21
School of Engineering and Information and Communication Technology Persistent Storage – SQLite Databases SQLiteOpenHelper is used to wrap up the logic to create and upgrade a database. You need to implement three methods at minimum – the constructor, onCreate, and onUpgrade: private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { //This creates the database. super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(PROPERTY_TABLE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + PROPERTY_TABLE); onCreate(db); } public void reset(SQLiteDatabase db) { db.execSQL("DROP TABLE IF EXISTS " + PROPERTY_TABLE); onCreate(db); } 21
22
School of Engineering and Information and Communication Technology Persistent Storage – SQLite Databases Accessing the database: mDbHelper = new DatabaseHelper(mCtx); mDb = mDbHelper.getWritableDatabase(); Reading information from the database: public Cursor getProperty(String id) { Cursor c = mDb.query(PROPERTY_TABLE, null, KEY_PROPERTY_ID + "=" + id, null, null, null, null); return c; } Adding to the database: public void addProperty(Property p) { ContentValues propValues = new ContentValues(); // Build our property values propValues.put(KEY_PROPERTY_ID, p.getPropertyID()); propValues.put(KEY_ADDRESS, p.getAddress()); propValues.put(KEY_PRICE, p.getPrice()); // Insert into the database mDb.insert(PROPERTY_TABLE, null, propValues); } Closing the database: mDb.close(); 22
23
School of Engineering and Information and Communication Technology Persistent Storage Raw resources, XML resources, and Assets –Raw resources are one way to deploy a file. They get put in the application.apk file as part of the packaging process and are accessed using the getResources() function shown below. –XML resources and Assets are two other ways to package files into an application’s.apk file. // Get the static XML resource file for reading. InputStream is = null; if (toRent) { is = context.getResources().openRawResource(R.raw.searchresults_camperdown_rent); } else { //toBuy is = context.getResources().openRawResource(R.raw.searchresults_camperdown_buy); } 23
24
School of Engineering and Information and Communication Technology Android System Permissions Android applications have no permissions associated with them by default, meaning they can not do anything that would adversely impact the user experience or any data on the device. To make use of protected features, one or more tags must be included in the AndroidManifest.xml file. 24 Example application from Google Play: “Locale” (pre Android 6.0)
25
School of Engineering and Information and Communication Technology System Permissions (cont.) All of the permissions required to generate the security details in the “Locale” example are: 25
26
School of Engineering and Information and Communication Technology System Permissions – AndroidManifest.xml <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="apt.tutorial.two“ android:versionCode="1“ android:versionName="1.0">... <category android:name="android.intent.category.LAUNCHER" /> 26 See http://developer.android.com/reference/android/Manifest.permission.html for a full list of permissions. There are currently over 100 that have been defined.http://developer.android.com/reference/android/Manifest.permission.html
27
School of Engineering and Information and Communication Technology System Permissions (as at Apr.2016): 137 ACCESS_CHECKIN_PROPERTIES, ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION, ACCESS_LOCATION_EXTRA_COMMANDS, ACCESS_NETWORK_STATE, ACCESS_NOTIFICATION_POLICY, ACCESS_WIFI_STATE, ACCOUNT_MANAGER, ADD_VOICEMAIL, BATTERY_STATS, BIND_ACCESSIBILITY_SERVICE, BIND_APPWIDGET, BIND_CARRIER_MESSAGING_SERVICE, BIND_CARRIER_SERVICES, BIND_CHOOSER_TARGET_SERVICE, BIND_DEVICE_ADMIN, BIND_DREAM_SERVICE, BIND_INCALL_SERVICE, BIND_INPUT_METHOD, BIND_MIDI_DEVICE_SERVICE, BIND_NFC_SERVICE, BIND_NOTIFICATION_LISTENER_SERVICE, BIND_PRINT_SERVICE, BIND_REMOTEVIEWS, BIND_TELECOM_CONNECTION_SERVICE, BIND_TEXT_SERVICE, BIND_TV_INPUT, BIND_VOICE_INTERACTION, BIND_VPN_SERVICE, BIND_WALLPAPER, BLUETOOTH, BLUETOOTH_ADMIN, BLUETOOTH_PRIVILEGED, BODY_SENSORS, BROADCAST_PACKAGE_REMOVED, BROADCAST_SMS, BROADCAST_STICKY, BROADCAST_WAP_PUSH, CALL_PHONE, CALL_PRIVILEGED, CAMERA, CAPTURE_AUDIO_OUTPUT, CAPTURE_SECURE_VIDEO_OUTPUT, CAPTURE_VIDEO_OUTPUT, CHANGE_COMPONENT_ENABLED_STATE, CHANGE_CONFIGURATION, CHANGE_NETWORK_STATE, CHANGE_WIFI_MULTICAST_STATE, CHANGE_WIFI_STATE, CLEAR_APP_CACHE, CONTROL_LOCATION_UPDATES, DELETE_CACHE_FILES, DELETE_PACKAGES, DIAGNOSTIC, DISABLE_KEYGUARD, DUMP, EXPAND_STATUS_BAR, FACTORY_TEST, FLASHLIGHT, GET_ACCOUNTS, GET_ACCOUNTS_PRIVILEGED, GET_PACKAGE_SIZE, GET_TASKS, GLOBAL_SEARCH, INSTALL_LOCATION_PROVIDER, INSTALL_PACKAGES, INSTALL_SHORTCUT, INTERNET, KILL_BACKGROUND_PROCESSES, LOCATION_HARDWARE, MANAGE_DOCUMENTS, MASTER_CLEAR, MEDIA_CONTENT_CONTROL, MODIFY_AUDIO_SETTINGS, MODIFY_PHONE_STATE, MOUNT_FORMAT_FILESYSTEMS, MOUNT_UNMOUNT_FILESYSTEMS, NFC, PACKAGE_USAGE_STATS, PERSISTENT_ACTIVITY, PROCESS_OUTGOING_CALLS, READ_CALENDAR, READ_CALL_LOG, READ_CONTACTS, READ_EXTERNAL_STORAGE, READ_FRAME_BUFFER, READ_INPUT_STATE, READ_LOGS, READ_PHONE_STATE, READ_SMS, READ_SYNC_SETTINGS, READ_SYNC_STATS, READ_VOICEMAIL, REBOOT, RECEIVE_BOOT_COMPLETED, RECEIVE_MMS, RECEIVE_SMS, RECEIVE_WAP_PUSH, RECORD_AUDIO, REORDER_TASKS, REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, REQUEST_INSTALL_PACKAGES, RESTART_PACKAGES, SEND_RESPOND_VIA_MESSAGE, SEND_SMS, SET_ALARM, SET_ALWAYS_FINISH, SET_ANIMATION_SCALE, SET_DEBUG_APP, SET_PREFERRED_APPLICATIONS, SET_PROCESS_LIMIT, SET_TIME, SET_TIME_ZONE, SET_WALLPAPER, SET_WALLPAPER_HINTS, SIGNAL_PERSISTENT_PROCESSES, STATUS_BAR, SYSTEM_ALERT_WINDOW, TRANSMIT_IR, UNINSTALL_SHORTCUT, UPDATE_DEVICE_STATS, USE_FINGERPRINT, USE_SIP, VIBRATE, WAKE_LOCK, WRITE_APN_SETTINGS, WRITE_CALENDAR, WRITE_CALL_LOG, WRITE_CONTACTS, WRITE_EXTERNAL_STORAGE, WRITE_GSERVICES, WRITE_SECURE_SETTINGS, WRITE_SETTINGS, WRITE_SYNC_SETTINGS, WRITE_VOICEMAIL 27
28
School of Engineering and Information and Communication Technology System Permissions – Before Android 6.0 Example: Facebook Permissions 28
29
School of Engineering and Information and Communication Technology System Permissions – Before Android 6.0 29 Example: Facebook Permissions (cont.)
30
School of Engineering and Information and Communication Technology 30 System Permissions – Before Android 6.0 Example: Facebook Permissions (cont.)
31
School of Engineering and Information and Communication Technology System Permissions – From Android 6.0+ 31 Example: Facebook Permissions
32
School of Engineering and Information and Communication Technology System Permissions – What changed? Permissions are granted differently in Android 6.0 (API Level 23) and higher. On Android 6.0 (API Level 23) and higher: –If the device is running Android 6.0 or higher AND the app’s targetSdkVersion is 23 or higher, the app requests permissions from the user at run-time. –The user can revoke the permissions at any time, so the app needs to check whether it has the permissions every time it runs. On Android 5.1 (API Level 22) and lower: -If the device is running Android 5.1 or lower OR the app’s targetSdkVersion is 22 or lower, the system asks the user to grant the permissions when the user installs the app (as seen in the earlier “Locale” and “Facebook” examples). -Once the user installs the app, the only way to revoke the permission is to uninstall the app. This is a huge change!!! 32
33
School of Engineering and Information and Communication Technology System Permissions – Protection Levels System permissions are divided into several protection levels, two of which are normal and dangerous. –Normal Protection Level: These are permissions that a user ‘might’ care about, but probably not. When we request the Internet permission in the manifest, the user is not informed about the permission at install time (on Android 6.0 and higher). Those permissions classified as normal are: ACCESS_LOCATION_EXTRA_COMMANDS, ACCESS_NETWORK_STATE, ACCESS_NOTIFICATION_POLICY, ACCESS_WIFI_STATE, BLUETOOTH, BLUETOOTH_ADMIN, BROADCAST_STICKY, CHANGE_NETWORK_STATE, CHANGE_WIFI_MULTICAST_STATE, CHANGE_WIFI_STATE, DISABLE_KEYGUARD, EXPAND_STATUS_BAR, GET_PACKAGE_SIZE, INSTALL_SHORTCUT, INTERNET, KILL_BACKGROUND_PROCESSES, MODIFY_AUDIO_SETTINGS, NFC, READ_SYNC_SETTINGS, READ_SYNC_STATS, RECEIVE_BOOT_COMPLETED, REORDER_TASKS, REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, REQUEST_INSTALL_PACKAGES, SET_ALARM, SET_TIME_ZONE, SET_WALLPAPER, SET_WALLPAPER_HINTS, TRANSMIT_IR, UNINSTALL_SHORTCUT, USE_FINGERPRINT, VIBRATE, WAKE_LOCK, WRITE_SYNC_SETTINGS 33
34
School of Engineering and Information and Communication Technology System Permissions – Protection Levels –Dangerous Protection Level: These are permissions where the app wants data or resources that involve the user’s private information or could potentially affect the user’s stored data or the operation of other apps. In Android 6.0+, permissions considered to be dangerous not only have to be requested via elements in the manifest, but also have to ask the user to grant those permissions at runtime. Note 1: If an app lists normal permissions in its manifest, the system automatically grants those permissions. Note 2: If an app requests a dangerous permission listed in its manifest, and the app already has another dangerous permission in the same permission group, the system immediately grants the permission without any interaction from the user. 34
35
School of Engineering and Information and Communication Technology System Permissions – Protection Levels There are 9 dangerous permission groups that Android 6.0 manages as user-controllable permissions: 35 Permission GroupPermission CALENDARREAD_CALENDAR, WRITE_CALENDAR CAMERA CONTACTSGET_ACCOUNTS, READ_CONTACTS, WRITE_CONTACTS LOCATIONACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION MICROPHONERECORD_AUDIO PHONEADD_VOICEMAIL, CALL_PHONE, PROCESS_OUTGOING_CALLS, READ_CALL_LOG, READ_PHONE_STATE, USE_SIP, WRITE_CALL_LOG SENSORSBODY_SENSORS SMSREAD_CELL_BROADCASTS, READ_SMS, RECEIVE_SMS, RECEIVE_MMS, RECEIVE_WAP_PUSH, SEND_SMS STORAGEREAD_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE
36
School of Engineering and Information and Communication Technology System Permissions – Code Examples … more on System Permissions in the next lecture 36
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.