• 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 android.dynamicmime.testapp.util;
18 
19 import static android.dynamicmime.common.Constants.ACTION_REQUEST;
20 import static android.dynamicmime.common.Constants.ACTION_RESPONSE;
21 import static android.dynamicmime.common.Constants.EXTRA_GROUP;
22 import static android.dynamicmime.common.Constants.EXTRA_MIMES;
23 import static android.dynamicmime.common.Constants.EXTRA_REQUEST;
24 import static android.dynamicmime.common.Constants.EXTRA_RESPONSE;
25 import static android.dynamicmime.common.Constants.REQUEST_GET;
26 import static android.dynamicmime.common.Constants.REQUEST_SET;
27 
28 import static org.junit.Assert.assertNotNull;
29 
30 import android.content.Context;
31 import android.content.Intent;
32 
33 import com.android.compatibility.common.util.BlockingBroadcastReceiver;
34 
35 import java.util.concurrent.TimeUnit;
36 
37 /**
38  * Class that dispatches MIME groups related commands to another app
39  * Target app should register {@link android.dynamicmime.app.AppMimeGroupsReceiver}
40  * to handler requests
41  */
42 public class AppMimeGroups {
43     private final Context mContext;
44     private final String mTargetPackage;
45 
AppMimeGroups(Context context, String targetPackage)46     private AppMimeGroups(Context context, String targetPackage) {
47         mContext = context;
48         mTargetPackage = targetPackage;
49     }
50 
with(Context context, String targetPackage)51     public static AppMimeGroups with(Context context, String targetPackage) {
52         return new AppMimeGroups(context, targetPackage);
53     }
54 
set(String mimeGroup, String[] mimeTypes)55     public void set(String mimeGroup, String[] mimeTypes) {
56         sendRequestAndAwait(mimeGroup, REQUEST_SET, mimeTypes);
57     }
58 
get(String mimeGroup)59     public String[] get(String mimeGroup) {
60         return sendRequestAndAwait(mimeGroup, REQUEST_GET)
61                 .getStringArrayExtra(EXTRA_RESPONSE);
62     }
63 
sendRequestAndAwait(String mimeGroup, int requestSet)64     private Intent sendRequestAndAwait(String mimeGroup, int requestSet) {
65         return sendRequestAndAwait(mimeGroup, requestSet, null);
66     }
67 
sendRequestAndAwait(String mimeGroup, int request, String[] mimeTypes)68     private Intent sendRequestAndAwait(String mimeGroup, int request, String[] mimeTypes) {
69         BlockingBroadcastReceiver receiver =
70                 new BlockingBroadcastReceiver(mContext, ACTION_RESPONSE);
71         receiver.register();
72 
73         mContext.sendBroadcast(getRequestIntent(mimeGroup, mimeTypes, request));
74 
75         Intent response = receiver.awaitForBroadcast(TimeUnit.SECONDS.toMillis(60L));
76 
77         mContext.unregisterReceiver(receiver);
78 
79         assertNotNull(response);
80 
81         return response;
82     }
83 
getRequestIntent(String mimeGroup, String[] mimeTypes, int request)84     private Intent getRequestIntent(String mimeGroup, String[] mimeTypes, int request) {
85         Intent intent = new Intent(ACTION_REQUEST);
86         intent.setPackage(mTargetPackage);
87         intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
88         intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
89 
90         intent.putExtra(EXTRA_GROUP, mimeGroup);
91         intent.putExtra(EXTRA_MIMES, mimeTypes);
92         intent.putExtra(EXTRA_REQUEST, request);
93 
94         return intent;
95     }
96 }
97