1 /*
2  * Copyright (C) 2024 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.healthconnect.tests.documentprovider.utils;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.os.Bundle;
22 
23 /** Handles requests from test cases to configure the test document provider. */
24 public final class DocumentProviderActivityRequestHandler {
25     /** Handles requests from test cases to configure the test document provider. */
handleRequest(Activity activity)26     public static void handleRequest(Activity activity) {
27         Intent response = new Intent(DocumentProviderIntent.RESPONSE);
28         try {
29             handleRequestFromBundle(activity.getIntent().getExtras());
30         } catch (Exception e) {
31             response.putExtra(DocumentProviderIntent.EXCEPTION, e);
32         } finally {
33             activity.sendBroadcast(response);
34             activity.finish();
35         }
36     }
37 
handleRequestFromBundle(Bundle bundle)38     private static void handleRequestFromBundle(Bundle bundle) {
39         String queryType = bundle.getString(DocumentProviderIntent.ACTION_TYPE);
40         switch (queryType) {
41             case DocumentProviderIntent.SET_DOCUMENT_PROVIDER_THROWS_EXCEPTION:
42                 FakeDocumentProviderRoots.INSTANCE.setThrowsException();
43                 break;
44             case DocumentProviderIntent.CLEAR_DOCUMENT_PROVIDER_ROOTS:
45                 FakeDocumentProviderRoots.INSTANCE.clear();
46                 break;
47             case DocumentProviderIntent.ADD_DOCUMENT_PROVIDER_ROOT:
48                 FakeDocumentProviderRoots.INSTANCE.addRoot(bundle);
49                 break;
50             default:
51                 throw new IllegalStateException(
52                         "Unknown query received from launcher app: " + queryType);
53         }
54     }
55 }
56