summaryrefslogtreecommitdiffstats
path: root/src/com/android/car/stream/PermissionsActivity.java
blob: 16e7719baf41e21fa2f9701acd7facfc8fad54b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Copyright (c) 2016, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.car.stream;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import com.android.car.stream.notifications.StreamNotificationListenerService;

import java.util.ArrayList;
import java.util.List;

/**
 * A trampoline activity that checks if all permissions necessary are granted.
 */
public class PermissionsActivity extends Activity {
    private static final String TAG = "PermissionsActivity";
    private static final String NOTIFICATION_LISTENER_ENABLED = "enabled_notification_listeners";

    public static final int CAR_PERMISSION_REQUEST_CODE = 1013; // choose a unique number

    private static final String[] PERMISSIONS = new String[]{
            android.Manifest.permission.READ_PHONE_STATE,
            android.Manifest.permission.CALL_PHONE,
            android.Manifest.permission.READ_CALL_LOG,
            android.Manifest.permission.READ_CONTACTS,
            android.Manifest.permission.ACCESS_FINE_LOCATION,
            android.Manifest.permission.RECEIVE_SMS,
            android.Manifest.permission.READ_EXTERNAL_STORAGE
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        boolean permissionsCheckOnly = getIntent().getExtras()
                .getBoolean(StreamConstants.STREAM_PERMISSION_CHECK_PERMISSIONS_ONLY);

        if (permissionsCheckOnly) {
            boolean allPermissionsGranted = hasNotificationListenerPermission()
                    && arePermissionGranted(PERMISSIONS);
            setResult(allPermissionsGranted ? RESULT_OK : RESULT_CANCELED);
            finish();
            return;
        }

        if (!hasNotificationListenerPermission()) {
            showNotificationListenerSettings();
        } else {
            maybeRequestPermissions();
        }
    }

    private void maybeRequestPermissions() {
        boolean permissionGranted = arePermissionGranted(PERMISSIONS);
        if (!permissionGranted) {
            requestPermissions(PERMISSIONS, CAR_PERMISSION_REQUEST_CODE);
        } else {
            startService(new Intent(this, StreamService.class));
            finish();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
            int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == CAR_PERMISSION_REQUEST_CODE) {
            List<String> granted = new ArrayList<>();
            List<String> notGranted = new ArrayList<>();
            for (int i = 0; i < permissions.length; i++) {
                String permission = permissions[i];
                int grantResult = grantResults[i];
                if (grantResult == PackageManager.PERMISSION_GRANTED) {
                    granted.add(permission);
                } else {
                    notGranted.add(permission);
                }
            }

            if (notGranted.size() > 0) {
                StringBuilder stb = new StringBuilder();
                for (String s : notGranted) {
                    stb.append(" ").append(s);
                }
                showDialog(getString(R.string.permission_not_granted, stb.toString()));
            } else {
                showDialog(getString(R.string.all_permission_granted));
                startService(new Intent(this, StreamService.class));
            }

            if (arePermissionGranted(PERMISSIONS)) {
                setResult(Activity.RESULT_OK);
            }
            finish();
        }
    }

    private void showDialog(String message) {
        new AlertDialog.Builder(this /* context */)
                .setTitle(getString(R.string.permission_dialog_title))
                .setMessage(message)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setPositiveButton(
                        getString(R.string.permission_dialog_positive_button_text),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        })
                .show();
    }

    private boolean arePermissionGranted(String[] permissions) {
        for (String permission : permissions) {
            if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
                Log.e(TAG, "Permission is not granted: " + permission);
                return false;
            }
        }
        return true;
    }

    private boolean hasNotificationListenerPermission() {
        ComponentName notificationListener = new ComponentName(this,
                StreamNotificationListenerService.class);
        String listeners = Settings.Secure.getString(getContentResolver(),
                NOTIFICATION_LISTENER_ENABLED);
        return listeners != null && listeners.contains(notificationListener.flattenToString());
    }

    private void showNotificationListenerSettings() {
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle(getString(R.string.car_notification_permission_dialog_title))
                .setMessage(getString(R.string.car_notification_permission_dialog_text))
                .setCancelable(false)
                .setNeutralButton(getString(android.R.string.ok),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                                Intent settingsIntent =
                                        new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
                                startActivity(settingsIntent);
                            }
                        })
                .create();
        dialog.show();
    }
}