1 /*
2  * Copyright (C) 2021 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 android.view.displayhash;
18 
19 
20 import static android.content.Context.DISPLAY_HASH_SERVICE;
21 
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.annotation.RequiresPermission;
25 import android.annotation.SystemService;
26 import android.annotation.TestApi;
27 import android.os.RemoteException;
28 import android.util.ArraySet;
29 import android.util.Log;
30 import android.view.WindowManagerGlobal;
31 
32 import com.android.internal.annotations.GuardedBy;
33 
34 import java.util.Collections;
35 import java.util.Set;
36 
37 /**
38  * Manages DisplayHash requests. The manager object can be retrieved by calling
39  * {@code Context.getSystemService(Context.DISPLAY_HASH_SERVICE)}
40  */
41 @SystemService(DISPLAY_HASH_SERVICE)
42 public final class DisplayHashManager {
43     private static final String TAG = "DisplayHashManager";
44 
45     private final Object mSupportedHashingAlgorithmLock = new Object();
46 
47     @GuardedBy("mSupportedAlgorithmLock")
48     private static Set<String> sSupportedHashAlgorithms;
49 
50     /**
51      * @hide
52      */
DisplayHashManager()53     public DisplayHashManager() {
54     }
55 
56     /**
57      * Get a Set of DisplayHash algorithms that the device supports.
58      *
59      * @return a String Set of supported hashing algorithms. The String value of one
60      * algorithm should be used when requesting to generate the DisplayHash.
61      */
62     @NonNull
getSupportedHashAlgorithms()63     public Set<String> getSupportedHashAlgorithms() {
64         synchronized (mSupportedHashingAlgorithmLock) {
65             if (sSupportedHashAlgorithms != null) {
66                 return sSupportedHashAlgorithms;
67             }
68 
69             try {
70                 String[] supportedAlgorithms = WindowManagerGlobal.getWindowManagerService()
71                         .getSupportedDisplayHashAlgorithms();
72                 if (supportedAlgorithms == null) {
73                     return Collections.emptySet();
74                 }
75                 sSupportedHashAlgorithms = new ArraySet<>(supportedAlgorithms);
76                 return sSupportedHashAlgorithms;
77             } catch (RemoteException e) {
78                 Log.e(TAG, "Failed to send request getSupportedHashingAlgorithms", e);
79                 throw e.rethrowFromSystemServer();
80             }
81         }
82     }
83 
84     /**
85      * Call to verify that the DisplayHash passed in was generated by the system.
86      *
87      * @param displayHash The hash to verify that it was generated by the system.
88      * @return a {@link VerifiedDisplayHash} if the hash was generated by the system or null
89      * if the hash cannot be verified.
90      */
91     @Nullable
verifyDisplayHash(@onNull DisplayHash displayHash)92     public VerifiedDisplayHash verifyDisplayHash(@NonNull DisplayHash displayHash) {
93         try {
94             return WindowManagerGlobal.getWindowManagerService().verifyDisplayHash(displayHash);
95         } catch (RemoteException e) {
96             Log.e(TAG, "Failed to send request verifyImpressionToken", e);
97             throw e.rethrowFromSystemServer();
98         }
99     }
100 
101     /**
102      * Call to enable or disable the throttling when generating a display hash. This should only be
103      * used for testing. Throttling is enabled by default.
104      *
105      * @hide
106      */
107     @TestApi
108     @RequiresPermission(android.Manifest.permission.READ_FRAME_BUFFER)
setDisplayHashThrottlingEnabled(boolean enable)109     public void setDisplayHashThrottlingEnabled(boolean enable) {
110         try {
111             WindowManagerGlobal.getWindowManagerService().setDisplayHashThrottlingEnabled(enable);
112         } catch (RemoteException e) {
113         }
114     }
115 }
116