1 package com.example.android.wearable.watchface;
2 
3 import android.Manifest;
4 import android.content.pm.PackageManager;
5 import android.os.Bundle;
6 import android.support.annotation.NonNull;
7 import android.support.v4.app.ActivityCompat;
8 import android.support.wearable.activity.WearableActivity;
9 import android.util.Log;
10 import android.view.View;
11 
12 /**
13  * Simple Activity for displaying Calendar Permission Rationale to user.
14  */
15 public class CalendarWatchFacePermissionActivity extends WearableActivity {
16 
17     private static final String TAG = "PermissionActivity";
18 
19     /* Id to identify permission request for calendar. */
20     private static final int PERMISSION_REQUEST_READ_CALENDAR = 1;
21 
22     @Override
onCreate(Bundle savedInstanceState)23     protected void onCreate(Bundle savedInstanceState) {
24         super.onCreate(savedInstanceState);
25         setContentView(R.layout.activity_calendar_watch_face_permission);
26         setAmbientEnabled();
27     }
28 
onClickEnablePermission(View view)29     public void onClickEnablePermission(View view) {
30         Log.d(TAG, "onClickEnablePermission()");
31 
32         // On 23+ (M+) devices, GPS permission not granted. Request permission.
33         ActivityCompat.requestPermissions(
34                 this,
35                 new String[]{Manifest.permission.READ_CALENDAR},
36                 PERMISSION_REQUEST_READ_CALENDAR);
37 
38     }
39 
40     /*
41      * Callback received when a permissions request has been completed.
42      */
43     @Override
onRequestPermissionsResult( int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)44     public void onRequestPermissionsResult(
45             int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
46 
47         Log.d(TAG, "onRequestPermissionsResult()");
48 
49         if (requestCode == PERMISSION_REQUEST_READ_CALENDAR) {
50             if ((grantResults.length == 1)
51                     && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
52                 finish();
53             }
54         }
55     }
56 }