1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package foo.bar.permission;
18 
19 import android.Manifest;
20 import android.app.Activity;
21 import android.app.LoaderManager;
22 import android.content.CursorLoader;
23 import android.content.Loader;
24 import android.content.pm.PackageManager;
25 import android.database.Cursor;
26 import android.os.Bundle;
27 import android.provider.CalendarContract;
28 import android.provider.ContactsContract;
29 import android.view.Menu;
30 import android.view.MenuItem;
31 import android.view.View;
32 import android.widget.CursorAdapter;
33 import android.widget.ListView;
34 import android.widget.SimpleCursorAdapter;
35 import android.widget.Toast;
36 
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.List;
40 
41 /**
42  * Simple sample of how to use the runtime permissions APIs.
43  */
44 public class PermissionActivity extends Activity implements LoaderManager.LoaderCallbacks<Cursor> {
45 
46     public static final String LOG_TAG = "PermissionActivity";
47 
48     private static final int CONTACTS_LOADER = 1;
49 
50     private static final int EVENTS_LOADER = 2;
51 
52     private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 1;
53 
54     private static final int PERMISSIONS_REQUEST_READ_CALENDAR = 2;
55 
56     private static final int PERMISSIONS_REQUEST_ALL_PERMISSIONS = 3;
57 
58     private final static String[] CONTACTS_COLUMNS = {
59             ContactsContract.Contacts.DISPLAY_NAME_PRIMARY
60     };
61 
62     private final static String[] CALENDAR_COLUMNS = {
63             CalendarContract.Events.TITLE
64     };
65 
66     private static final String[] CONTACTS_PROJECTION = {
67             ContactsContract.Contacts._ID,
68             ContactsContract.Contacts.DISPLAY_NAME_PRIMARY
69     };
70 
71     private static final String[] EVENTS_PROJECTION = {
72             CalendarContract.Events._ID,
73             CalendarContract.Events.TITLE
74     };
75 
76     private final static int[] TO_IDS = {
77             android.R.id.text1
78     };
79 
80     private ListView mListView;
81 
82     private CursorAdapter mContactsAdapter;
83     private CursorAdapter mEventsAdapter;
84 
85     @Override
onCreate(Bundle savedInstanceState)86     protected void onCreate(Bundle savedInstanceState) {
87         super.onCreate(savedInstanceState);
88         bindUi();
89     }
90 
91 
92     @Override
onCreateOptionsMenu(Menu menu)93     public boolean onCreateOptionsMenu(Menu menu) {
94         getMenuInflater().inflate(R.menu.actions, menu);
95         return true;
96     }
97 
98     @Override
onOptionsItemSelected(MenuItem item)99     public boolean onOptionsItemSelected(MenuItem item) {
100         switch (item.getItemId()) {
101             case R.id.show_contacts: {
102                 showContacts();
103                 return true;
104             }
105 
106             case R.id.show_events: {
107                 showEvents();
108                 return true;
109             }
110 
111             case R.id.request_all_perms: {
112                 requestPermissions();
113                 return true;
114             }
115         }
116 
117         return false;
118     }
119 
120     @Override
onCreateLoader(int loaderId, Bundle args)121     public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
122         switch (loaderId) {
123             case CONTACTS_LOADER: {
124                 return new CursorLoader(this,
125                         ContactsContract.Contacts.CONTENT_URI,
126                         CONTACTS_PROJECTION, null, null, null);
127             }
128 
129             case EVENTS_LOADER: {
130                 return new CursorLoader(this,
131                         CalendarContract.Events.CONTENT_URI,
132                         EVENTS_PROJECTION, null, null, null);
133             }
134 
135             default: {
136                 return null;
137             }
138         }
139     }
140 
141     @Override
onLoadFinished(Loader<Cursor> loader, Cursor cursor)142     public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
143         switch (loader.getId()) {
144             case CONTACTS_LOADER: {
145                 mContactsAdapter.swapCursor(cursor);
146             } break;
147 
148             case EVENTS_LOADER: {
149                 mEventsAdapter.swapCursor(cursor);
150             } break;
151         }
152     }
153 
154     @Override
onLoaderReset(Loader<Cursor> loader)155     public void onLoaderReset(Loader<Cursor> loader) {
156         switch (loader.getId()) {
157             case CONTACTS_LOADER: {
158                 mContactsAdapter.swapCursor(null);
159             } break;
160 
161             case EVENTS_LOADER: {
162                 mEventsAdapter.swapCursor(null);
163             } break;
164         }
165     }
166 
167     @Override
onBackPressed()168     public void onBackPressed() {
169         requestPermissions();
170     }
171 
172     @Override
onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)173     public void onRequestPermissionsResult(int requestCode, String permissions[],
174             int[] grantResults) {
175         switch (requestCode) {
176             case PERMISSIONS_REQUEST_READ_CONTACTS: {
177                 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
178                     showContacts();
179                 }
180             } break;
181 
182             case PERMISSIONS_REQUEST_READ_CALENDAR: {
183                 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
184                     showEvents();
185                 }
186             } break;
187 
188             case PERMISSIONS_REQUEST_ALL_PERMISSIONS: {
189                 for (int i = 0; i < permissions.length; i++) {
190                     if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
191                         switch (permissions[i]) {
192                             case Manifest.permission.READ_CALENDAR: {
193                                 Toast.makeText(this, "Granted Calendar!",
194                                         Toast.LENGTH_SHORT).show();
195                             } break;
196 
197                             case Manifest.permission.READ_CONTACTS: {
198                                 showContacts();
199                             } break;
200                         }
201                     }
202                 }
203             } break;
204         }
205     }
206 
bindUi()207     private void bindUi() {
208         setContentView(R.layout.permission_activity);
209 
210         mListView = (ListView) findViewById(R.id.list);
211         View emptyView = findViewById(R.id.empty_state);
212         mListView.setEmptyView(emptyView);
213 
214         mContactsAdapter = new SimpleCursorAdapter(this,
215                 android.R.layout.simple_list_item_1,
216                 null, CONTACTS_COLUMNS, TO_IDS, 0);
217 
218         mEventsAdapter = new SimpleCursorAdapter(this,
219                 android.R.layout.simple_list_item_1,
220                 null, CALENDAR_COLUMNS, TO_IDS, 0);
221     }
222 
showContacts()223     private void showContacts() {
224         if (checkSelfPermission(Manifest.permission.READ_CONTACTS)
225                 != PackageManager.PERMISSION_GRANTED) {
226             requestPermissions(new String[] {Manifest.permission.READ_CONTACTS},
227                     PERMISSIONS_REQUEST_READ_CONTACTS);
228             return;
229         }
230 
231         if (getLoaderManager().getLoader(CONTACTS_LOADER) == null) {
232             getLoaderManager().initLoader(CONTACTS_LOADER, null, this);
233         }
234         mListView.setAdapter(mContactsAdapter);
235     }
236 
showEvents()237     private void showEvents() {
238         if (checkSelfPermission(Manifest.permission.READ_CALENDAR)
239                 != PackageManager.PERMISSION_GRANTED) {
240             requestPermissions(new String[]{Manifest.permission.READ_CALENDAR},
241                     PERMISSIONS_REQUEST_READ_CALENDAR);
242             return;
243         }
244 
245         if (getLoaderManager().getLoader(EVENTS_LOADER) == null) {
246             getLoaderManager().initLoader(EVENTS_LOADER, null, this);
247         }
248         mListView.setAdapter(mEventsAdapter);
249     }
250 
requestPermissions()251     private void requestPermissions() {
252         List<String> permissions = new ArrayList<>();
253         if (checkSelfPermission(Manifest.permission.READ_CALENDAR)
254                 != PackageManager.PERMISSION_GRANTED) {
255             permissions.add(Manifest.permission.READ_CALENDAR);
256         }
257         if (checkSelfPermission(Manifest.permission.READ_CONTACTS)
258                 != PackageManager.PERMISSION_GRANTED) {
259             permissions.add(Manifest.permission.READ_CONTACTS);
260         }
261         if (!permissions.isEmpty()) {
262             requestPermissions(permissions.toArray(new String[0]),
263                     PERMISSIONS_REQUEST_ALL_PERMISSIONS);
264         }
265     }
266 }
267