• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.permission2;
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.support.v4.app.ActivityCompat;
30 import android.support.v4.content.ContextCompat;
31 import android.view.Menu;
32 import android.view.MenuItem;
33 import android.view.View;
34 import android.widget.CursorAdapter;
35 import android.widget.ListView;
36 import android.widget.SimpleCursorAdapter;
37 import android.widget.Toast;
38 
39 /**
40  * Simple sample of how to use the runtime permissions APIs.
41  */
42 public class PermissionActivity extends Activity implements LoaderManager.LoaderCallbacks<Cursor>,
43         ActivityCompat.OnRequestPermissionsResultCallback {
44 
45     public static final String LOG_TAG = "PermissionActivity";
46 
47     private static final int CONTACTS_LOADER = 1;
48 
49     private static final int EVENTS_LOADER = 2;
50 
51     private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 1;
52 
53     private static final int PERMISSIONS_REQUEST_READ_CALENDAR = 2;
54 
55     private static final int PERMISSIONS_REQUEST_ALL_PERMISSIONS = 3;
56 
57     private final static String[] CONTACTS_COLUMNS = {
58             ContactsContract.Contacts.DISPLAY_NAME_PRIMARY
59     };
60 
61     private final static String[] CALENDAR_COLUMNS = {
62             CalendarContract.Events.TITLE
63     };
64 
65     private static final String[] CONTACTS_PROJECTION = {
66             ContactsContract.Contacts._ID,
67             ContactsContract.Contacts.DISPLAY_NAME_PRIMARY
68     };
69 
70     private static final String[] EVENTS_PROJECTION = {
71             CalendarContract.Events._ID,
72             CalendarContract.Events.TITLE
73     };
74 
75     private final static int[] TO_IDS = {
76             android.R.id.text1
77     };
78 
79     private ListView mListView;
80 
81     private CursorAdapter mContactsAdapter;
82     private CursorAdapter mEventsAdapter;
83 
84     @Override
onCreate(Bundle savedInstanceState)85     protected void onCreate(Bundle savedInstanceState) {
86         super.onCreate(savedInstanceState);
87         bindUi();
88     }
89 
90 
91     @Override
onCreateOptionsMenu(Menu menu)92     public boolean onCreateOptionsMenu(Menu menu) {
93         getMenuInflater().inflate(R.menu.actions, menu);
94         return true;
95     }
96 
97     @Override
onOptionsItemSelected(MenuItem item)98     public boolean onOptionsItemSelected(MenuItem item) {
99         switch (item.getItemId()) {
100             case R.id.show_contacts: {
101                 showContacts();
102                 return true;
103             }
104 
105             case R.id.show_events: {
106                 showEvents();
107                 return true;
108             }
109 
110             case R.id.request_all_perms: {
111                 requestPermissions();
112                 return true;
113             }
114         }
115 
116         return false;
117     }
118 
119     @Override
onCreateLoader(int loaderId, Bundle args)120     public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
121         switch (loaderId) {
122             case CONTACTS_LOADER: {
123                 return new CursorLoader(this,
124                         ContactsContract.Contacts.CONTENT_URI,
125                         CONTACTS_PROJECTION, null, null, null);
126             }
127 
128             case EVENTS_LOADER: {
129                 return new CursorLoader(this,
130                         CalendarContract.Events.CONTENT_URI,
131                         EVENTS_PROJECTION, null, null, null);
132             }
133 
134             default: {
135                 return null;
136             }
137         }
138     }
139 
140     @Override
onLoadFinished(Loader<Cursor> loader, Cursor cursor)141     public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
142         switch (loader.getId()) {
143             case CONTACTS_LOADER: {
144                 mContactsAdapter.swapCursor(cursor);
145             } break;
146 
147             case EVENTS_LOADER: {
148                 mEventsAdapter.swapCursor(cursor);
149             } break;
150         }
151     }
152 
153     @Override
onLoaderReset(Loader<Cursor> loader)154     public void onLoaderReset(Loader<Cursor> loader) {
155         switch (loader.getId()) {
156             case CONTACTS_LOADER: {
157                 mContactsAdapter.swapCursor(null);
158             } break;
159 
160             case EVENTS_LOADER: {
161                 mEventsAdapter.swapCursor(null);
162             } break;
163         }
164     }
165 
166     @Override
onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)167     public void onRequestPermissionsResult(int requestCode, String permissions[],
168             int[] grantResults) {
169         switch (requestCode) {
170             case PERMISSIONS_REQUEST_READ_CONTACTS: {
171                 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
172                     showContacts();
173                 }
174             } break;
175 
176             case PERMISSIONS_REQUEST_READ_CALENDAR: {
177                 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
178                     showEvents();
179                 }
180             } break;
181         }
182     }
183 
bindUi()184     private void bindUi() {
185         setContentView(R.layout.permission_activity);
186 
187         mListView = (ListView) findViewById(R.id.list);
188         View emptyView = findViewById(R.id.empty_state);
189         mListView.setEmptyView(emptyView);
190 
191         mContactsAdapter = new SimpleCursorAdapter(this,
192                 android.R.layout.simple_list_item_1,
193                 null, CONTACTS_COLUMNS, TO_IDS, 0);
194 
195         mEventsAdapter = new SimpleCursorAdapter(this,
196                 android.R.layout.simple_list_item_1,
197                 null, CALENDAR_COLUMNS, TO_IDS, 0);
198     }
199 
showContacts()200     private void showContacts() {
201         if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
202                 != PackageManager.PERMISSION_GRANTED) {
203             if (ActivityCompat.shouldShowRequestPermissionRationale(this,
204                     Manifest.permission.READ_CONTACTS)) {
205                 Toast.makeText(this, "I really need it, dude!", Toast.LENGTH_SHORT).show();
206             }
207             ActivityCompat.requestPermissions(this, new String[]{
208                             Manifest.permission.READ_CONTACTS},
209                     PERMISSIONS_REQUEST_READ_CONTACTS);
210             return;
211         }
212 
213         if (getLoaderManager().getLoader(CONTACTS_LOADER) == null) {
214             getLoaderManager().initLoader(CONTACTS_LOADER, null, this);
215         }
216         mListView.setAdapter(mContactsAdapter);
217     }
218 
showEvents()219     private void showEvents() {
220         if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR)
221                 != PackageManager.PERMISSION_GRANTED) {
222             if (ActivityCompat.shouldShowRequestPermissionRationale(this,
223                     Manifest.permission.READ_CALENDAR)) {
224                 Toast.makeText(this, "I really need it, dude!", Toast.LENGTH_SHORT).show();
225             }
226             ActivityCompat.requestPermissions(this, new String[]{
227                             Manifest.permission.READ_CALENDAR},
228                     PERMISSIONS_REQUEST_READ_CALENDAR);
229             return;
230         }
231 
232         if (getLoaderManager().getLoader(EVENTS_LOADER) == null) {
233             getLoaderManager().initLoader(EVENTS_LOADER, null, this);
234         }
235         mListView.setAdapter(mEventsAdapter);
236     }
237 
requestPermissions()238     private void requestPermissions() {
239         if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR)
240                 != PackageManager.PERMISSION_GRANTED
241             || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
242                 != PackageManager.PERMISSION_GRANTED) {
243             String[] permissions = new String[]{
244                     Manifest.permission.READ_CONTACTS,
245                     Manifest.permission.READ_CALENDAR
246             };
247             ActivityCompat.requestPermissions(this, permissions,
248                     PERMISSIONS_REQUEST_ALL_PERMISSIONS);
249         }
250     }
251 }
252