summaryrefslogtreecommitdiffstats
path: root/src/com/android/contacts/common/activity/RequestPermissionsActivity.java
blob: 1b18e10fef82b3f72d0bf84bfba3dfb512a0ec7a (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
/*
 * Copyright (C) 2015 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.contacts.common.activity;

import android.Manifest.permission;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Trace;

/**
 * Repeatedly ask the user for runtime permissions, until they grant all the permissions.
 * For now this is designed for activities used in Contacts. However, the ImportVCardActivity is
 * also used in the Dialer. When Dialer begins to support runtime permissions in their app, they
 * may wish to use a more targeted list of permissions or allow the user to reject using
 * some permissions.
 *
 * At the time of writing this Activity, most permissions cause crashes when not granted.
 * So it is risky to not possess them.
 *
 */
public class RequestPermissionsActivity extends Activity {
    public static final String PREVIOUS_ACTIVITY_INTENT = "previous_intent";

    private static final int PERMISSIONS_REQUEST_ALL_PERMISSIONS = 1;
    private static String[] permissions = new String[]{
            permission.ACCESS_FINE_LOCATION, // Location Group
            permission.READ_CONTACTS, // Contacts group
            permission.READ_CALL_LOG, // Permission group phone
            permission.READ_CALENDAR, // Calendar group
            permission.READ_SMS, // SMS group
            permission.WRITE_EXTERNAL_STORAGE, // Storage group
    };

    private Intent mPreviousActivityIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPreviousActivityIntent = (Intent) getIntent().getExtras().get(PREVIOUS_ACTIVITY_INTENT);

        if (savedInstanceState == null) {
            requestPermissions();
        }
    }

    /**
     * If any permissions the Contacts app needs are missing, open an Activity
     * to prompt the user for these permissions. Moreover, finish the current activity.
     *
     * This is designed to be called inside {@link android.app.Activity#onCreate}
     */
    public static boolean startPermissionActivity(Activity activity) {
        if (!RequestPermissionsActivity.hasPermissions(activity)) {
            final Intent intent = new Intent(activity,  RequestPermissionsActivity.class);
            intent.putExtra(PREVIOUS_ACTIVITY_INTENT, activity.getIntent());
            activity.startActivity(intent);
            activity.finish();
            return true;
        }
        return false;
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[],
            int[] grantResults) {
        if (isAllGranted(grantResults)) {
            mPreviousActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(mPreviousActivityIntent);
            finish();
            overridePendingTransition(0, 0);
        } else {
            requestPermissions();
        }
    }

    private boolean isAllGranted(int[] grantResult) {
        for (int result : grantResult) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    private void requestPermissions() {
        Trace.beginSection("requestPermissions");
        requestPermissions(permissions, PERMISSIONS_REQUEST_ALL_PERMISSIONS);
        Trace.endSection();
    }

    public static boolean hasPermissions(Context context) {
        Trace.beginSection("hasPermission");
        try {
            for (String permission : permissions) {
                if (context.checkSelfPermission(permission)
                        != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
            return true;
        } finally {
            Trace.endSection();
        }

    }
}