1 /* 2 * Copyright 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 com.example.android.system.runtimepermissions.contacts; 18 19 import android.content.ContentProviderOperation; 20 import android.content.ContentResolver; 21 import android.content.OperationApplicationException; 22 import android.database.Cursor; 23 import android.os.Bundle; 24 import android.os.RemoteException; 25 import android.provider.ContactsContract; 26 import android.support.annotation.Nullable; 27 import android.support.design.widget.Snackbar; 28 import android.support.v4.app.Fragment; 29 import android.support.v4.app.LoaderManager; 30 import android.support.v4.content.CursorLoader; 31 import android.support.v4.content.Loader; 32 import android.view.LayoutInflater; 33 import android.view.View; 34 import android.view.ViewGroup; 35 import android.widget.Button; 36 import android.widget.TextView; 37 38 import com.example.android.system.runtimepermissions.R; 39 40 import java.util.ArrayList; 41 42 /** 43 * Displays the first contact stored on the device and contains an option to add a dummy contact. 44 * <p> 45 * This Fragment is only used to illustrate that access to the Contacts ContentProvider API has 46 * been granted (or denied) as part of the runtime permissions model. It is not relevant for the 47 * use 48 * of the permissions API. 49 * <p> 50 * This fragments demonstrates a basic use case for accessing the Contacts Provider. The 51 * implementation is based on the training guide available here: 52 * https://developer.android.com/training/contacts-provider/retrieve-names.html 53 */ 54 public class ContactsFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> { 55 56 private static final String TAG = "Contacts"; 57 /** 58 * Projection for the content provider query includes the id and primary name of a contact. 59 */ 60 private static final String[] PROJECTION = {ContactsContract.Contacts._ID, 61 ContactsContract.Contacts.DISPLAY_NAME_PRIMARY}; 62 /** 63 * Sort order for the query. Sorted by primary name in ascending order. 64 */ 65 private static final String ORDER = ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + " ASC"; 66 private static String DUMMY_CONTACT_NAME = "__DUMMY CONTACT from runtime permissions sample"; 67 68 private TextView mMessageText; 69 70 /** 71 * Creates a new instance of a ContactsFragment. 72 */ 73 public static ContactsFragment newInstance() { 74 return new ContactsFragment(); 75 } 76 77 78 @Nullable 79 @Override 80 public View onCreateView(LayoutInflater inflater, ViewGroup container, 81 Bundle savedInstanceState) { 82 View rootView = inflater.inflate(R.layout.fragment_contacts, container, false); 83 84 mMessageText = rootView.findViewById(R.id.contact_message); 85 86 // Register a listener to add a dummy contact when a button is clicked. 87 Button button = rootView.findViewById(R.id.contact_add); 88 button.setOnClickListener(new View.OnClickListener() { 89 @Override 90 public void onClick(View view) { 91 insertDummyContact(); 92 } 93 }); 94 95 // Register a listener to display the first contact when a button is clicked. 96 button = rootView.findViewById(R.id.contact_load); 97 button.setOnClickListener(new View.OnClickListener() { 98 @Override 99 public void onClick(View view) { 100 loadContact(); 101 } 102 }); 103 return rootView; 104 } 105 106 /** 107 * Restart the Loader to query the Contacts content provider to display the first contact. 108 */ 109 private void loadContact() { 110 getLoaderManager().restartLoader(0, null, this); 111 } 112 113 /** 114 * Initialises a new {@link CursorLoader} that queries the {@link ContactsContract}. 115 */ 116 @Override 117 public Loader<Cursor> onCreateLoader(int i, Bundle bundle) { 118 return new CursorLoader(getActivity(), ContactsContract.Contacts.CONTENT_URI, PROJECTION, 119 null, null, ORDER); 120 } 121 122 123 /** 124 * Dislays either the name of the first contact or a message. 125 */ 126 @Override 127 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { 128 if (cursor != null) { 129 final int totalCount = cursor.getCount(); 130 if (totalCount > 0) { 131 cursor.moveToFirst(); 132 String name = cursor 133 .getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 134 mMessageText.setText( 135 getResources().getString(R.string.contacts_string, totalCount, name)); 136 } else { 137 mMessageText.setText(R.string.contacts_empty); 138 } 139 } 140 } 141 142 @Override 143 public void onLoaderReset(Loader<Cursor> loader) { 144 mMessageText.setText(R.string.contacts_empty); 145 } 146 147 /** 148 * Accesses the Contacts content provider directly to insert a new contact. 149 * <p> 150 * The contact is called "__DUMMY ENTRY" and only contains a name. 151 */ 152 private void insertDummyContact() { 153 // Two operations are needed to insert a new contact. 154 ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(2); 155 156 // First, set up a new raw contact. 157 ContentProviderOperation.Builder op = 158 ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) 159 .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) 160 .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null); 161 operations.add(op.build()); 162 163 // Next, set the name for the contact. 164 op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 165 .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 166 .withValue(ContactsContract.Data.MIMETYPE, 167 ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) 168 .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, 169 DUMMY_CONTACT_NAME); 170 operations.add(op.build()); 171 172 // Apply the operations. 173 ContentResolver resolver = getActivity().getContentResolver(); 174 try { 175 resolver.applyBatch(ContactsContract.AUTHORITY, operations); 176 } catch (RemoteException | OperationApplicationException e) { 177 Snackbar.make(mMessageText.getRootView(), "Could not add a new contact: " + 178 e.getMessage(), Snackbar.LENGTH_LONG); 179 } 180 } 181 } 182