• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.car.settings.testutils;
18 
19 import android.content.ContentProvider;
20 import android.content.ContentValues;
21 import android.content.pm.PackageManager;
22 import android.content.res.Resources;
23 import android.database.Cursor;
24 import android.net.Uri;
25 import android.os.Bundle;
26 import android.text.TextUtils;
27 
28 import java.util.List;
29 
30 /**
31  * A simple content provider for tests.  This provider runs in the same process as the test.
32  */
33 public class TestContentProvider extends ContentProvider {
34 
35     public static final String TEST_TEXT_CONTENT = "TestContentProviderText";
36 
37     private static final String SCHEME = "content";
38     private static final String AUTHORITY =
39             "com.android.car.settings.testutils.TestContentProvider";
40     private static final String METHOD_GET_TEXT = "getText";
41     private static final String METHOD_GET_ICON = "getIcon";
42     private static final String KEY_PREFERENCE_TITLE = "com.android.settings.title";
43     private static final String KEY_PREFERENCE_SUMMARY = "com.android.settings.summary";
44     private static final String KEY_PREFERENCE_ICON = "com.android.settings.icon";
45     private static final String KEY_PREFERENCE_ICON_PACKAGE = "com.android.settings.icon_package";
46     private static final String TEXT_KEY = "textKey";
47     private static final String ICON_KEY = "iconKey";
48 
49 
50     @Override
onCreate()51     public boolean onCreate() {
52         return true;
53     }
54 
55     @Override
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)56     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
57             String sortOrder) {
58         return null;
59     }
60 
61     @Override
getType(Uri uri)62     public String getType(Uri uri) {
63         return null;
64     }
65 
66     @Override
insert(Uri uri, ContentValues values)67     public Uri insert(Uri uri, ContentValues values) {
68         return null;
69     }
70 
71     @Override
delete(Uri uri, String selection, String[] selectionArgs)72     public int delete(Uri uri, String selection, String[] selectionArgs) {
73         return 0;
74     }
75 
76     @Override
update(Uri uri, ContentValues values, String selection, String[] selectionArgs)77     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
78         return 0;
79     }
80 
81     @Override
call(String methodName, String uriString, Bundle extras)82     public Bundle call(String methodName, String uriString, Bundle extras) {
83         if (TextUtils.isEmpty(methodName)) {
84             return null;
85         }
86         if (TextUtils.isEmpty(uriString)) {
87             return null;
88         }
89         Uri uri = Uri.parse(uriString);
90         if (uri == null) {
91             return null;
92         }
93         // Only process URIs with valid scheme, authority, and no assigned port.
94         if (!(SCHEME.equals(uri.getScheme())
95                 && AUTHORITY.equals(uri.getAuthority())
96                 && (uri.getPort() == -1))) {
97             return null;
98         }
99         List<String> pathSegments = uri.getPathSegments();
100         // Path segments should consist of the method name and the argument. If the number of path
101         // segments is not exactly two, the content provider is not being called as intended.
102         if ((pathSegments == null) || (pathSegments.size() != 2)) {
103             return null;
104         }
105         // The first path segment needs to match the methodName.
106         if (!methodName.equals(pathSegments.get(0))) {
107             return null;
108         }
109         String key = pathSegments.get(1);
110         if (METHOD_GET_TEXT.equals(methodName)) {
111             if (TEXT_KEY.equals(key)) {
112                 Bundle bundle = new Bundle();
113                 bundle.putString(KEY_PREFERENCE_TITLE, TEST_TEXT_CONTENT);
114                 bundle.putString(KEY_PREFERENCE_SUMMARY, TEST_TEXT_CONTENT);
115                 return bundle;
116             }
117         } else if (METHOD_GET_ICON.equals(methodName)) {
118             if (ICON_KEY.equals(key)) {
119                 try {
120                     String packageName = getContext().getPackageName();
121                     PackageManager manager = getContext().getPackageManager();
122                     Resources resources = manager.getResourcesForApplication(packageName);
123                     int iconRes = resources.getIdentifier("test_icon", "drawable",
124                             packageName);
125                     if (iconRes == 0) {
126                         return null;
127                     }
128 
129                     Bundle bundle = new Bundle();
130                     bundle.putInt(KEY_PREFERENCE_ICON, iconRes);
131                     bundle.putString(KEY_PREFERENCE_ICON_PACKAGE, packageName);
132                     return bundle;
133 
134                 } catch (PackageManager.NameNotFoundException e) {
135                     return null;
136                 }
137             }
138         }
139         return null;
140     }
141 }
142