Home | History | Annotate | Download | only in util
      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 
     30     public static boolean hasAccessAllEpg(Context context) {
     31         if (sHasAccessAllEpgPermission == null) {
     32             sHasAccessAllEpgPermission =
     33                     context.checkSelfPermission(
     34                                     "com.android.providers.tv.permission.ACCESS_ALL_EPG_DATA")
     35                             == PackageManager.PERMISSION_GRANTED;
     36         }
     37         return sHasAccessAllEpgPermission;
     38     }
     39 
     40     public static boolean hasAccessWatchedHistory(Context context) {
     41         if (sHasAccessWatchedHistoryPermission == null) {
     42             sHasAccessWatchedHistoryPermission =
     43                     context.checkSelfPermission(
     44                                     "com.android.providers.tv.permission.ACCESS_WATCHED_PROGRAMS")
     45                             == PackageManager.PERMISSION_GRANTED;
     46         }
     47         return sHasAccessWatchedHistoryPermission;
     48     }
     49 
     50     public static boolean hasModifyParentalControls(Context context) {
     51         if (sHasModifyParentalControlsPermission == null) {
     52             sHasModifyParentalControlsPermission =
     53                     context.checkSelfPermission("android.permission.MODIFY_PARENTAL_CONTROLS")
     54                             == PackageManager.PERMISSION_GRANTED;
     55         }
     56         return sHasModifyParentalControlsPermission;
     57     }
     58 
     59     public static boolean hasReadTvListings(Context context) {
     60         return context.checkSelfPermission(PERMISSION_READ_TV_LISTINGS)
     61                 == PackageManager.PERMISSION_GRANTED;
     62     }
     63 
     64     public static boolean hasInternet(Context context) {
     65         return context.checkSelfPermission("android.permission.INTERNET")
     66                 == PackageManager.PERMISSION_GRANTED;
     67     }
     68 }
     69