1 /*
2  * Copyright (C) 2023 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.verifier.notifications;
18 
19 import android.content.ContentProvider;
20 import android.content.ContentValues;
21 import android.content.res.AssetFileDescriptor;
22 import android.content.res.AssetManager;
23 import android.database.Cursor;
24 import android.net.Uri;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 
29 import java.io.FileNotFoundException;
30 import java.io.IOException;
31 
32 /** {@link ContentProvider} for assets used in Notification-related tests. */
33 public class AssetsProvider extends ContentProvider {
34     @Override
onCreate()35     public boolean onCreate() {
36         return true;
37     }
38 
39     @Nullable
40     @Override
openAssetFile(@onNull Uri uri, @NonNull String mode)41     public AssetFileDescriptor openAssetFile(@NonNull Uri uri, @NonNull String mode)
42             throws FileNotFoundException {
43         AssetManager assets = getContext().getAssets();
44         String filename = uri.getLastPathSegment();
45         if (mode.equals("r") && assets != null && filename != null) {
46             try {
47                 return assets.openFd("notifications/" + filename);
48             } catch (IOException e) {
49                 throw new RuntimeException(e);
50             }
51         }
52 
53         return super.openAssetFile(uri, mode);
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         throw new UnsupportedOperationException();
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         throw new UnsupportedOperationException();
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         throw new UnsupportedOperationException();
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         throw new UnsupportedOperationException();
85     }
86 }
87