1 /*
2  * Copyright (C) 2022 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.cts.mocka11yime;
18 
19 import android.content.ContentProvider;
20 import android.content.ContentValues;
21 import android.database.Cursor;
22 import android.net.Uri;
23 import android.os.Bundle;
24 import android.os.PersistableBundle;
25 
26 import androidx.annotation.AnyThread;
27 import androidx.annotation.GuardedBy;
28 import androidx.annotation.NonNull;
29 import androidx.annotation.Nullable;
30 
31 /**
32  * {@link ContentProvider} to receive {@link MockA11yImeSettings} via
33  * {@link ContentProvider#call(String, String, String, Bundle)}.
34  */
35 public final class MockA11yImeContentProvider extends ContentProvider {
36 
37     private static final Object sParamsLock = new Object();
38 
39     @GuardedBy("sParamsLock")
40     @Nullable
41     private static MockA11yImeSettings sSettings = null;
42 
43     @GuardedBy("sParamsLock")
44     @Nullable
45     private static String sClientPackageName = null;
46 
47     @GuardedBy("sParamsLock")
48     @Nullable
49     private static String sEventCallbackIntentActionName = null;
50 
51     @Override
onCreate()52     public boolean onCreate() {
53         return false;
54     }
55 
56     @Nullable
57     @Override
query(@onNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder)58     public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection,
59             @Nullable String[] selectionArgs, @Nullable String sortOrder) {
60         return null;
61     }
62 
63     @Nullable
64     @Override
getType(@onNull Uri uri)65     public String getType(@NonNull Uri uri) {
66         return null;
67     }
68 
69     @Nullable
70     @Override
insert(@onNull Uri uri, @Nullable ContentValues values)71     public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
72         return null;
73     }
74 
75     @Override
delete(@onNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs)76     public int delete(@NonNull Uri uri, @Nullable String selection,
77             @Nullable String[] selectionArgs) {
78         return 0;
79     }
80 
81     @Override
update(@onNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs)82     public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection,
83             @Nullable String[] selectionArgs) {
84         return 0;
85     }
86 
87     @Override
call(String authority, @MockA11yImeConstants.ContentProviderCommand String method, String arg, Bundle extras)88     public Bundle call(String authority,
89             @MockA11yImeConstants.ContentProviderCommand String method, String arg, Bundle extras) {
90         if (!MockA11yImeConstants.SETTINGS_PROVIDER_AUTHORITY.equals(authority)) {
91             return Bundle.EMPTY;
92         }
93 
94         switch (method) {
95             case MockA11yImeConstants.ContentProviderCommand.DELETE:
96                 setParams(null, null, null);
97                 return Bundle.EMPTY;
98             case MockA11yImeConstants.ContentProviderCommand.WRITE: {
99                 final String callingPackageName = getCallingPackage();
100                 if (callingPackageName == null) {
101                     throw new SecurityException("Failed to obtain the calling package name.");
102                 }
103                 setParams(callingPackageName, extras.getString(
104                                 MockA11yImeConstants.BundleKey.EVENT_CALLBACK_INTENT_ACTION_NAME),
105                         new MockA11yImeSettings(extras.getParcelable(
106                                 MockA11yImeConstants.BundleKey.SETTINGS, PersistableBundle.class)));
107                 return Bundle.EMPTY;
108             }
109             default:
110                 return Bundle.EMPTY;
111         }
112     }
113 
114     @AnyThread
setParams(@ullable String clientPackageName, @Nullable String eventCallbackIntentActionName, @Nullable MockA11yImeSettings settings)115     private static void setParams(@Nullable String clientPackageName,
116             @Nullable String eventCallbackIntentActionName,
117             @Nullable MockA11yImeSettings settings) {
118         synchronized (sParamsLock) {
119             sClientPackageName = clientPackageName;
120             sEventCallbackIntentActionName = eventCallbackIntentActionName;
121             sSettings = settings;
122         }
123     }
124 
125     @AnyThread
126     @Nullable
getSettings()127     static MockA11yImeSettings getSettings() {
128         synchronized (sParamsLock) {
129             return sSettings;
130         }
131     }
132 
133     @AnyThread
134     @Nullable
getClientPackageName()135     static String getClientPackageName() {
136         synchronized (sParamsLock) {
137             return sClientPackageName;
138         }
139     }
140 
141     @AnyThread
142     @Nullable
getEventCallbackActionName()143     static String getEventCallbackActionName() {
144         synchronized (sParamsLock) {
145             return sEventCallbackIntentActionName;
146         }
147     }
148 }
149