Home | History | Annotate | Download | only in model
      1 /*
      2  * Copyright (C) 2015 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.packageinstaller.permission.model;
     18 
     19 import android.content.pm.PackageManager;
     20 import android.content.pm.PermissionInfo;
     21 
     22 public final class Permission {
     23     private final String mName;
     24     private final String mAppOp;
     25 
     26     private boolean mGranted;
     27     private boolean mAppOpAllowed;
     28     private int mFlags;
     29     private boolean mIsEphemeral;
     30     private boolean mIsRuntimeOnly;
     31 
     32     public Permission(String name, boolean granted,
     33             String appOp, boolean appOpAllowed, int flags, int protectionLevel) {
     34         mName = name;
     35         mGranted = granted;
     36         mAppOp = appOp;
     37         mAppOpAllowed = appOpAllowed;
     38         mFlags = flags;
     39         mIsEphemeral = (protectionLevel & PermissionInfo.PROTECTION_FLAG_INSTANT) != 0;
     40         mIsRuntimeOnly = (protectionLevel & PermissionInfo.PROTECTION_FLAG_RUNTIME_ONLY) != 0;
     41     }
     42 
     43     public String getName() {
     44         return mName;
     45     }
     46 
     47     public String getAppOp() {
     48         return mAppOp;
     49     }
     50 
     51     public int getFlags() {
     52         return mFlags;
     53     }
     54 
     55     public boolean hasAppOp() {
     56         return mAppOp != null;
     57     }
     58 
     59     public boolean isGranted() {
     60         return mGranted;
     61     }
     62 
     63     public boolean isReviewRequired() {
     64         return (mFlags & PackageManager.FLAG_PERMISSION_REVIEW_REQUIRED) != 0;
     65     }
     66 
     67     public void resetReviewRequired() {
     68         mFlags &= ~PackageManager.FLAG_PERMISSION_REVIEW_REQUIRED;
     69     }
     70 
     71     public void setGranted(boolean mGranted) {
     72         this.mGranted = mGranted;
     73     }
     74 
     75     public boolean isAppOpAllowed() {
     76         return mAppOpAllowed;
     77     }
     78 
     79     public boolean isUserFixed() {
     80         return (mFlags & PackageManager.FLAG_PERMISSION_USER_FIXED) != 0;
     81     }
     82 
     83     public void setUserFixed(boolean userFixed) {
     84         if (userFixed) {
     85             mFlags |= PackageManager.FLAG_PERMISSION_USER_FIXED;
     86         } else {
     87             mFlags &= ~PackageManager.FLAG_PERMISSION_USER_FIXED;
     88         }
     89     }
     90 
     91     public boolean isSystemFixed() {
     92         return (mFlags & PackageManager.FLAG_PERMISSION_SYSTEM_FIXED) != 0;
     93     }
     94 
     95     public boolean isPolicyFixed() {
     96         return (mFlags & PackageManager.FLAG_PERMISSION_POLICY_FIXED) != 0;
     97     }
     98 
     99     public boolean isUserSet() {
    100         return (mFlags & PackageManager.FLAG_PERMISSION_USER_SET) != 0;
    101     }
    102 
    103     public boolean isGrantedByDefault() {
    104         return (mFlags & PackageManager.FLAG_PERMISSION_GRANTED_BY_DEFAULT) != 0;
    105     }
    106 
    107     public void setUserSet(boolean userSet) {
    108         if (userSet) {
    109             mFlags |= PackageManager.FLAG_PERMISSION_USER_SET;
    110         } else {
    111             mFlags &= ~PackageManager.FLAG_PERMISSION_USER_SET;
    112         }
    113     }
    114 
    115     public void setPolicyFixed(boolean policyFixed) {
    116         if (policyFixed) {
    117             mFlags |= PackageManager.FLAG_PERMISSION_POLICY_FIXED;
    118         } else {
    119             mFlags &= ~PackageManager.FLAG_PERMISSION_POLICY_FIXED;
    120         }
    121     }
    122 
    123     public boolean shouldRevokeOnUpgrade() {
    124         return (mFlags & PackageManager.FLAG_PERMISSION_REVOKE_ON_UPGRADE) != 0;
    125     }
    126 
    127     public void setRevokeOnUpgrade(boolean revokeOnUpgrade) {
    128         if (revokeOnUpgrade) {
    129             mFlags |= PackageManager.FLAG_PERMISSION_REVOKE_ON_UPGRADE;
    130         } else {
    131             mFlags &= ~PackageManager.FLAG_PERMISSION_REVOKE_ON_UPGRADE;
    132         }
    133     }
    134 
    135     public void setAppOpAllowed(boolean mAppOpAllowed) {
    136         this.mAppOpAllowed = mAppOpAllowed;
    137     }
    138 
    139     public boolean isEphemeral() {
    140         return mIsEphemeral;
    141     }
    142 
    143     public boolean isRuntimeOnly() {
    144         return mIsRuntimeOnly;
    145     }
    146 
    147     public boolean isGrantingAllowed(boolean isEphemeralApp, boolean supportsRuntimePermissions) {
    148         return (!isEphemeralApp || isEphemeral())
    149                 && (supportsRuntimePermissions || !isRuntimeOnly());
    150     }
    151 }
    152