1 /*
2  * Copyright (C) 2016 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.android.permissiontestappmv1;
18 
19 import android.provider.Settings;
20 import androidx.core.app.ActivityCompat;
21 import androidx.core.content.ContextCompat;
22 import android.os.Bundle;
23 import android.view.inputmethod.InputMethodManager;
24 import android.widget.Button;
25 import android.view.View;
26 import android.content.Context;
27 import android.content.pm.PackageManager;
28 import android.widget.EditText;
29 import android.widget.Toast;
30 import android.view.inputmethod.InputMethodManager;
31 import android.content.ContentResolver;
32 import android.Manifest;
33 import android.app.Activity;
34 
35 public class MainActivity extends Activity {
36 
37     private static final int READ_PERMISSION_RESULT = 1;
38 
39     @Override
onCreate(Bundle savedInstanceState)40     protected void onCreate(Bundle savedInstanceState) {
41         super.onCreate(savedInstanceState);
42         setContentView(R.layout.activity_main);
43 
44         final Button buttonGetPermission = (Button) findViewById(R.id.buttonGetPermission);
45         buttonGetPermission.setOnClickListener(new View.OnClickListener() {
46             public void onClick(View v) {
47                 showContacts();
48             }
49         });
50     }
51 
52     /**
53       * This method asks for 'READ_CONTACTS' permission on button 'Get Contact Permission' press
54       * Method does nothing with Contact content
55       */
showContacts()56     private void showContacts() {
57         if (ContextCompat.checkSelfPermission(this,
58                 Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
59             if (ActivityCompat.shouldShowRequestPermissionRationale(this,
60                     Manifest.permission.READ_CONTACTS)) {
61             }
62             ActivityCompat.requestPermissions(this, new String[] {
63                     Manifest.permission.READ_CONTACTS
64             }, READ_PERMISSION_RESULT);
65             return;
66         }
67     }
68 }
69