1 /*
2  * Copyright (C) 2017 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 package com.android.tv.common.util;
17 
18 import android.content.Context;
19 import android.content.pm.PackageManager;
20 
21 /** Util class to handle permissions. */
22 public class PermissionUtils {
23     /** Permission to read the TV listings. */
24     public static final String PERMISSION_READ_TV_LISTINGS = "android.permission.READ_TV_LISTINGS";
25 
26     private static Boolean sHasAccessAllEpgPermission;
27     private static Boolean sHasAccessWatchedHistoryPermission;
28     private static Boolean sHasModifyParentalControlsPermission;
29     private static Boolean sHasChangeHdmiCecActiveSource;
30     private static Boolean sHasReadContentRatingSystem;
31 
32 
hasAccessAllEpg(Context context)33     public static boolean hasAccessAllEpg(Context context) {
34         if (sHasAccessAllEpgPermission == null) {
35             sHasAccessAllEpgPermission =
36                     context.checkSelfPermission(
37                                     "com.android.providers.tv.permission.ACCESS_ALL_EPG_DATA")
38                             == PackageManager.PERMISSION_GRANTED;
39         }
40         return sHasAccessAllEpgPermission;
41     }
42 
hasAccessWatchedHistory(Context context)43     public static boolean hasAccessWatchedHistory(Context context) {
44         if (sHasAccessWatchedHistoryPermission == null) {
45             sHasAccessWatchedHistoryPermission =
46                     context.checkSelfPermission(
47                                     "com.android.providers.tv.permission.ACCESS_WATCHED_PROGRAMS")
48                             == PackageManager.PERMISSION_GRANTED;
49         }
50         return sHasAccessWatchedHistoryPermission;
51     }
52 
hasModifyParentalControls(Context context)53     public static boolean hasModifyParentalControls(Context context) {
54         if (sHasModifyParentalControlsPermission == null) {
55             sHasModifyParentalControlsPermission =
56                     context.checkSelfPermission("android.permission.MODIFY_PARENTAL_CONTROLS")
57                             == PackageManager.PERMISSION_GRANTED;
58         }
59         return sHasModifyParentalControlsPermission;
60     }
61 
hasReadTvListings(Context context)62     public static boolean hasReadTvListings(Context context) {
63         return context.checkSelfPermission(PERMISSION_READ_TV_LISTINGS)
64                 == PackageManager.PERMISSION_GRANTED;
65     }
66 
hasInternet(Context context)67     public static boolean hasInternet(Context context) {
68         return context.checkSelfPermission("android.permission.INTERNET")
69                 == PackageManager.PERMISSION_GRANTED;
70     }
71 
hasWriteExternalStorage(Context context)72     public static boolean hasWriteExternalStorage(Context context) {
73         return context.checkSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE")
74                 == PackageManager.PERMISSION_GRANTED;
75     }
76 
hasChangeHdmiCecActiveSource(Context context)77     public static boolean hasChangeHdmiCecActiveSource(Context context) {
78         if (sHasChangeHdmiCecActiveSource == null) {
79             sHasChangeHdmiCecActiveSource =
80                     context.checkSelfPermission(
81                             "android.permission.CHANGE_HDMI_CEC_ACTIVE_SOURCE")
82                             == PackageManager.PERMISSION_GRANTED;
83         }
84         return sHasChangeHdmiCecActiveSource;
85     }
86 
hasReadContetnRatingSystem(Context context)87     public static boolean hasReadContetnRatingSystem(Context context) {
88         if (sHasReadContentRatingSystem == null) {
89             sHasReadContentRatingSystem =
90                     context.checkSelfPermission(
91                             "android.permission.READ_CONTENT_RATING_SYSTEMS")
92                             == PackageManager.PERMISSION_GRANTED;
93         }
94         return sHasReadContentRatingSystem;
95     }
96 }
97