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.app;
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 android.content.BroadcastReceiver;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.util.ArraySet;
32 
33 import java.util.Set;
34 
35 /**
36  * Receiver of {@link android.dynamicmime.testapp.util.AppMimeGroups} commands
37  */
38 public class AppMimeGroupsReceiver extends BroadcastReceiver {
39 
40     @Override
onReceive(Context context, Intent intent)41     public void onReceive(Context context, Intent intent) {
42         if (!ACTION_REQUEST.equals(intent.getAction())) {
43             return;
44         }
45 
46         String mimeGroup = intent.getStringExtra(EXTRA_GROUP);
47         String[] mimeTypes = intent.getStringArrayExtra(EXTRA_MIMES);
48         int requestCode = intent.getIntExtra(EXTRA_REQUEST, -1);
49 
50         Intent response = new Intent(ACTION_RESPONSE);
51         try {
52             handleRequest(context, mimeGroup, mimeTypes, requestCode, response);
53             response.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
54             context.sendBroadcast(response);
55         } catch (IllegalArgumentException ignored) {
56         }
57     }
58 
handleRequest(Context context, String mimeGroup, String[] mimeTypes, int requestCode, Intent response)59     private void handleRequest(Context context, String mimeGroup, String[] mimeTypes,
60             int requestCode, Intent response) {
61         switch (requestCode) {
62             case REQUEST_SET:
63                 context.getPackageManager().setMimeGroup(mimeGroup, new ArraySet<>(mimeTypes));
64                 break;
65             case REQUEST_GET:
66                 response.putExtra(EXTRA_RESPONSE, getMimeGroup(context, mimeGroup));
67                 break;
68             default:
69                 throw new IllegalArgumentException("Unexpected request");
70         }
71     }
72 
getMimeGroup(Context context, String mimeGroup)73     private String[] getMimeGroup(Context context, String mimeGroup) {
74         Set<String> mimeTypes = context.getPackageManager().getMimeGroup(mimeGroup);
75         return mimeTypes != null ? mimeTypes.toArray(new String[0]) : null;
76     }
77 }
78