Permissions.

Slides:



Advertisements
Similar presentations
Application Fundamentals Android Development. Announcements Posting in D2L Tutorials.
Advertisements

Android Security. N-Degree of Separation Applications can be thought as composed by Main Functionality Several Non-functional Concerns Security is a non-functional.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Debugging Android Applications
Intent An Intent describes the operation to be performed. Intents are used to start an activity using either of the following methods – Context.startActivity()
Bluetooth. Bluetooth is an open, wireless protocol for exchanging data between devices over a short distance. –managed by the Bluetooth Special Interest.
Data Storage: Part 1 (Preferences)
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
Introducing the Sudoku Example
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Software Architecture of Android Yaodong Bi, Ph.D. Department of Computing Sciences University of Scranton.
Networking: Part 2 (Accessing the Internet). The UI Thread When an application is launched, the system creates a “main” UI thread responsible for handling.
Data Storage: Part 4 (Content Providers). Content Providers Content providers allow the sharing of data between applications. Inter-process communication.
Mobile Application Development using Android Lecture 2.
DUE Hello World on the Android Platform.
Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal.
Overview of Android Application Development
COMP 365 Android Development.  Every android application has a manifest file called AndroidManifest.xml  Found in the Project folder  Contains critical.
Intent Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Networking: Part 1 (Web Content). Networking with Android Android provides A full-featured web browser based on Chromium, the open source browser engine.
Linking Activities using Intents How to navigate between Android Activities 1Linking Activities using Intents.
Applications with Multiple Activities. Most applications will have more than one activity. The main activity is started when the application is started.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Cosc 4735 Primer: Marshmallow Changes and new APIs in android 6.0 (api 23)
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Cosc 4735 Permissions Asking for them in API 23+.
Intents and Broadcast Receivers Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution.
School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments,
Developing Android Services. Objectives Creating a service that runs in background Performing long-running tasks in a separate thread Performing repeated.
Android Application -Architecture.
CS371m - Mobile Computing Runtime Permissions.
Android Mobile Application Development
Multimedia.
Data Storage: Part 2 (File System)
Location-Based Services: Part 2 (Google Maps)
Bluetooth.
Android 5: Interacting with Other Apps
CS371m - Mobile Computing Services and Broadcast Receivers
Reactive Android Development
Android Runtime – Dalvik VM
ListView: Part 2.
Java Programming Language
Chapter 13 Exception Handling
Introduction to Exceptions in Java
Android – Read/Write to External Storage
Reactive Android Development
Indexer AKEEL AHMED.
Android Introduction Camera.
Mobile Device Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
EE 422C Java Reflection re·flec·tion rəˈflekSH(ə)n/ noun
Developing Android Services
CIS 470 Mobile App Development
Android Programming Lecture 5
Chapter 12 Exception Handling
Android Sensor Programming
CS371m - Mobile Computing Runtime Permissions.
Many thanks to Jun Bum Lim for his help with this tutorial.
Exception Handling Chapter 9 Edited by JJ.
Android Topics UI Thread and Limited processing resources
Android Topics Asynchronous Callsbacks
Activities and Intents
Android Developer Fundamentals V2
Activities and Intents
CIS 470 Mobile App Development
Final Jim Brucker.
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions 2nd Lecture.
CA16R405 - Mobile Application Development (Theory)
Presentation transcript:

Permissions

Android Permissions Beginning with Android 6.0 (Marshmallow, API level 23), users must grant certain permissions to an application at runtime, not simply when the application is installed. Streamlines the application installation process Gives the user more control over the application’s functionality user can grant some permissions and deny others User can revoke the permissions at any time e.g., by going to the application’s Settings screen But … This approach complicates things for application developers. ©SoftMoore Consulting

Normal and Dangerous Permissions System permissions are divided into several protection levels, but from a developer perspective, the two most important protection levels are normal and dangerous permissions. Normal permissions require access to data or resources outside the application, but they pose very little risk to the user’s privacy or the operation of other applications. e.g., set an alarm or access the internet Dangerous permissions cover areas where an application needs data or resources that involve the user’s private information or that could potentially affect the operation of other applications. e.g., access to the user’s location or contact information ©SoftMoore Consulting

Declaring Permissions in the Manifest Regardless of whether a permission is categorized as normal or dangerous, all permissions needed by an application must be declared in the manifest. <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="edu.citadel.android.permission"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <application ... <activity </activity> </application> </manifest> ©SoftMoore Consulting

Checking for Permissions An application that needs a dangerous permission must check whether that permission is granted every time it performs an operation that requires that permission. Even if a permission was granted previously, the user can still revoke the permission by going to the application’s Settings screen. To check if an application has a permission, call method ContextCompat.checkSelfPermission(). ©SoftMoore Consulting

Checking for Permissions (continued) Example int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE); if (permissionCheck == PackageManager.PERMISSION_GRANTED) { makeCall(); } If the application does not have the permission, this method returns PERMISSION_DENIED, and the application must explicitly ask the user for permission. ©SoftMoore Consulting

Requesting Permissions If an application doesn’t already have the permission it needs, it can call requestPermissions() to request the appropriate permissions. The application passes an array of the permissions it wants to request plus an integer request code to identify this permission request. The requestPermissions() method functions asynchronously. method returns immediately system presents a dialog box to the user ©SoftMoore Consulting

Permission Groups All dangerous permissions belong to permission groups. Examples Permission group CONTACTS includes permissions READ_CONTACTS, WRITE_CONTACTS, and GET_ACCOUNTS. Permission group PHONE includes permissions READ_PHONE_STATE, CALL_PHONE, READ_CALL_LOG, etc. An application requests a specific permission, but the system dialog box requests permission for an entire group; i.e., it does not describe the specific permission within the group that is being requested. If the user grants permission, then all permissions within the group are granted. ©SoftMoore Consulting

Example: Requesting Permissions private static final int PERMISSION_REQUEST_CALL_PHONE = 100; ... // must check for permissions at runtime before placing a call int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE); if (permissionCheck == PackageManager.PERMISSION_GRANTED) { makeCall(); } else // request the permission ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, PERMISSION_REQUEST_CALL_PHONE); passes an array of requests so that more than one permission can be requested at the same time ©SoftMoore Consulting

Example: Requesting Permissions (continued) ©SoftMoore Consulting

Handling the Response to the Permission Request When the user responds to a permissions dialog request, the system invokes the application’s onRequestPermissionsResult() method, passing it the user’s response and the request code that was initially passed to requestPermissions(). An application will need to override method onRequestPermissionsResult() to find out whether the permission was granted. ©SoftMoore Consulting

Example: Handling the Response to the Permission Request @Override public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) { if (requestCode == PERMISSION_REQUEST_CALL_PHONE) if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) makeCall(); } else ... // notify user that permission was denied value passed to method requestPermissions() ©SoftMoore Consulting

Permission Request Rationale In some circumstances, you might want to help the user understand why an application needs a certain dangerous permission. One approach is to provide an explanation only if the user has already turned down that permission request. To help in these situations where the user might need an explanation, Android provides a utility method, shouldShowRequestPermissionRationale(). returns true if the app has requested this permission previously and the user denied the request usually called in method onRequestPermissionResult() if the permission was not granted ©SoftMoore Consulting

Example: Permission Request Rationale private void checkCallPhonePermission() { int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE); if (permissionCheck == PackageManager.PERMISSION_GRANTED) ... } else if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) ... // show explanation asynchronously; do not block UI thread ... // call method ActivityCompat.requestPermissions() after checking if permission has been granted but before requesting the permission ©SoftMoore Consulting

Example: Permission Request Rationale (continued) private void checkCallPhonePermission() { // must check for permissions at runtime before placing a call int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE); if (permissionCheck == PackageManager.PERMISSION_GRANTED) makeCall(); } else if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) // show explanation asynchronously -- do not block ui thread String message = " ... "; Toast toast = Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG); toast.show(); // request the permission ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, PERMISSION_REQUEST_CALL_PHONE); ©SoftMoore Consulting

Permission Usage Notes Try not to overwhelm the user with permission requests. Consider using an intent. Rather than have an application perform a task, use an intent to ask another application to perform the task, and get results from the other application. Ask only for the permissions needed. Explain why the permissions are needed by the application; e.g., in a Toast or Snackbar, or in a separate tutorial. ©SoftMoore Consulting

Making a Phone Call in Android /** * Initiates a telephone call to the specified phone number. */ private void makeCall() { try Uri uri = Uri.parse("tel:" + phoneNumToCall); Intent intent = new Intent(Intent.ACTION_CALL, uri); startActivity(intent); } catch (SecurityException ex) String errorMsg = "No permission to make phone call."; Log.e(LOG_TAG, errorMsg, ex); ©SoftMoore Consulting

Making a Phone Call in Android (continiued) ©SoftMoore Consulting

Relevant Links Normal and Dangerous Permissions Normal Permissions https://developer.android.com/guide/topics/permissions/requesting.html#normal-dangerous Normal Permissions https://developer.android.com/guide/topics/permissions/normal-permissions.html Requesting Permissions at Run Time https://developer.android.com/training/permissions/requesting.html Permissions Usage Notes https://developer.android.com/training/permissions/usage-notes.html Android Runtime Permissions Request Tutorial http://www.truiton.com/2016/04/obtaining-runtime-permissions-android-marshmallow-6-0/ ©SoftMoore Consulting