1 /*
2  * Copyright 2019 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.pump.fragment;
18 
19 import android.Manifest;
20 import android.content.Intent;
21 import android.content.pm.PackageManager;
22 import android.net.Uri;
23 import android.os.Bundle;
24 import android.provider.Settings;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 
29 import androidx.annotation.NonNull;
30 import androidx.annotation.Nullable;
31 import androidx.annotation.UiThread;
32 import androidx.core.content.ContextCompat;
33 import androidx.fragment.app.Fragment;
34 
35 import com.android.pump.R;
36 import com.android.pump.activity.PumpActivity;
37 import com.android.pump.util.IntentUtils;
38 
39 import java.util.Collection;
40 import java.util.HashSet;
41 
42 @UiThread
43 public class PermissionFragment extends Fragment {
44     private static final int REQUEST_REQUIRED_PERMISSIONS = 42;
45     private static final String[] REQUIRED_PERMISSIONS = {
46         Manifest.permission.INTERNET,
47         Manifest.permission.READ_EXTERNAL_STORAGE,
48         Manifest.permission.WRITE_EXTERNAL_STORAGE
49     };
50 
51     private boolean mShowRationale;
52 
newInstance()53     public static @NonNull Fragment newInstance() {
54         return new PermissionFragment();
55     }
56 
57     @Override
onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)58     public @NonNull View onCreateView(@NonNull LayoutInflater inflater,
59             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
60         View view = inflater.inflate(R.layout.fragment_permission, container, false);
61 
62         view.findViewById(R.id.fragment_permission_button)
63                 .setOnClickListener((v) -> requestMissingRequiredPermissions());
64 
65         return view;
66     }
67 
68     @Override
onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)69     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
70             @NonNull int[] grantResults) {
71         if (requestCode == REQUEST_REQUIRED_PERMISSIONS) {
72             boolean granted = true;
73             boolean showRationale = false;
74             for (int i = 0; i < grantResults.length; ++i) {
75                 if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
76                     granted = false;
77                     showRationale |= shouldShowRequestPermissionRationale(permissions[i]);
78                 }
79             }
80             if (granted) {
81                 // TODO We shouldn't reference PumpActivity from here
82                 ((PumpActivity) requireActivity()).initialize();
83             } else if (!showRationale && !mShowRationale) {
84                 // If we were not supposed to show the rationale before requestPermissions(...) and
85                 // we still shouldn't show the rationale it means the user previously selected
86                 // "don't ask again" in the permission request dialog. In this case we bring up the
87                 // system permission settings for this package.
88                 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
89                 intent.setData(Uri.fromParts("package", requireActivity().getPackageName(), null));
90                 IntentUtils.startExternalActivity(requireContext(), intent);
91             }
92         } else {
93             super.onRequestPermissionsResult(requestCode, permissions, grantResults);
94         }
95     }
96 
requestMissingRequiredPermissions()97     private void requestMissingRequiredPermissions() {
98         Collection<String> missing = new HashSet<>();
99 
100         mShowRationale = false;
101         for (String permission : REQUIRED_PERMISSIONS) {
102             if (ContextCompat.checkSelfPermission(requireContext(), permission)
103                     != PackageManager.PERMISSION_GRANTED) {
104                 missing.add(permission);
105                 mShowRationale |= shouldShowRequestPermissionRationale(permission);
106             }
107         }
108 
109         if (!missing.isEmpty()) {
110             requestPermissions(missing.toArray(new String[0]), REQUEST_REQUIRED_PERMISSIONS);
111         }
112     }
113 }
114