Requesting and Requiring Permissions
In the late 1990s a wave of viruses spread through the Internet, delivered via email, using contact information culled from Microsoft Outlook. A virus would simply email copies of itself to each of the Outlook contacts that had an email address. This was possible because, at the time, Outlook did not take any steps to protect data from programs using the Outlook API, since that API was designed for ordinary developers, not virus authors.
Nowadays, many applications that hold onto contact data secure that data by requiring that a user explicitly grant rights for other programs to access the contact information. Those rights could be granted on a case‑by‑case basis or once at install time.
Android is no different, in that it requires permissions for applications to read or write contact data. Android’s permission system is useful well beyond contact data, and for content providers and services beyond those supplied by the Android framework.
You, as an Android developer, will frequently need to ensure your applications have the appropriate permissions to do what you want to do with other applications’ data. You may also elect to require permissions for other applications to use your data or services, if you make those available to other Android components. This chapter covers how to accomplish both these ends.
Mother, May I?
Requesting the use of other applications’ data or services requires the element to be added to your file. Your manifest may have zero or more uses‑permission elements, all as direct children of the root element.
The element takes a single attribute, , which is the name of the permission your application requires:
The stock system permissions all begin with and are listed in the Android SDK documentation for . Third‑party applications may have their own permissions, which hopefully they have documented for you. Here are some of the more important built‑in permissions:
• , if your application wishes to access the Internet through any means, from raw Java sockets through the widget
• , , and the like for reading data out of the built‑in content providers
• , , and the like for modifying data in the built‑in content providers
Permissions are confirmed at the time the application is installed – the user will be prompted to confirm it is OK for your application to do what the permission calls for. This prompt is not available in the current emulator, however.
If you do not have the desired permission and you try to do something that needs it, you may get a informing you of the missing permission, but this is not a guarantee – failures may come in other forms, depending on if something else is catching and trying to handle that exception.
Halt! Who Goes There?
The other side of the coin, of course, is to secure your own application. If your application is merely activities and intent receivers, security may be just an outbound thing, where you request the right to use resources of other applications. If, on the other hand, you put content providers or services in your application, you will want to implement inbound security to control which applications can do what with the data.
Note that the issue here is less about whether other applications might “mess up” your data, and more about privacy of the user’s information or use of services that might incur expense. That is where the stock permissions for built‑in Android applications are focused – can you read or modify contacts, can you send SMS messages, etc. If your application does not store information that might be considered private, security is less of an issue. If, on the other hand, your application stores private data, such as medical information, security is much more important.
The first step to securing your own application using permissions is to declare said permissions, once again in the file. In this case, instead of , you add elements. Once again, you can have zero or more elements, all as direct children of the root element.
Declaring a permission is slightly more complicated than using a permission. There are three pieces of information you need to supply:
1. The symbolic name of the permission. To keep your permissions from colliding with those from other applications, you should use your application’s Java namespace as a prefix.
2. A label for the permission: something short that is understandable by users.
3. A description for the permission: something a wee bit longer that is understandable by your users.
This does not enforce the permission. Rather, it indicates that it is a possible permission; your application must still flag security violations as they occur.
There are two ways for your application to enforce permissions, dictating where and under what circumstances they are required. You can enforce permissions in your code, but the easier option is to indicate in the manifest where permissions are required.
Дата добавления: 2015-05-16; просмотров: 753;