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.commands;
18 
19 import static android.dynamicmime.common.Constants.GROUP_FIRST;
20 import static android.dynamicmime.common.Constants.GROUP_SECOND;
21 import static android.dynamicmime.common.Constants.GROUP_THIRD;
22 import static android.dynamicmime.common.Constants.PACKAGE_HELPER_APP;
23 import static android.dynamicmime.common.Constants.PACKAGE_PREFERRED_APP;
24 import static android.dynamicmime.common.Constants.PACKAGE_UPDATE_APP;
25 
26 import android.content.Context;
27 import android.util.ArraySet;
28 
29 import androidx.annotation.Nullable;
30 
31 import java.util.Collections;
32 import java.util.Set;
33 
34 public interface MimeGroupCommands {
setMimeGroup(String mimeGroup, Set<String> mimeTypes)35     void setMimeGroup(String mimeGroup, Set<String> mimeTypes);
36     @Nullable
getMimeGroupInternal(String mimeGroup)37     Set<String> getMimeGroupInternal(String mimeGroup);
38 
addMimeTypeToGroup(String mimeGroup, String... mimeTypes)39     default void addMimeTypeToGroup(String mimeGroup, String... mimeTypes) {
40         ArraySet<String> typesToAdd = new ArraySet<>(mimeTypes);
41 
42         Set<String> newMimeTypes = new ArraySet<>(getMimeGroupInternal(mimeGroup));
43         newMimeTypes.addAll(typesToAdd);
44 
45         setMimeGroup(mimeGroup, newMimeTypes);
46     }
47 
removeMimeTypeFromGroup(String mimeGroup, String... mimeTypes)48     default void removeMimeTypeFromGroup(String mimeGroup, String... mimeTypes) {
49         ArraySet<String> typesToRemove = new ArraySet<>(mimeTypes);
50 
51         Set<String> newMimeTypes = new ArraySet<>(getMimeGroupInternal(mimeGroup));
52         newMimeTypes.removeAll(typesToRemove);
53 
54         setMimeGroup(mimeGroup, newMimeTypes);
55     }
56 
setMimeGroup(String mimeGroup, String... mimeTypes)57     default void setMimeGroup(String mimeGroup, String... mimeTypes) {
58         setMimeGroup(mimeGroup, new ArraySet<>(mimeTypes));
59     }
60 
clearMimeGroup(String mimeGroup)61     default void clearMimeGroup(String mimeGroup) {
62         setMimeGroup(mimeGroup, Collections.emptySet());
63     }
64 
clearGroups()65     default void clearGroups() {
66         safeClearMimeGroup(GROUP_FIRST);
67         safeClearMimeGroup(GROUP_SECOND);
68         safeClearMimeGroup(GROUP_THIRD);
69     }
70 
safeClearMimeGroup(String mimeGroup)71     default void safeClearMimeGroup(String mimeGroup) {
72         try {
73             clearMimeGroup(mimeGroup);
74         } catch (Throwable ignored) {
75         }
76     }
77 
testApp(Context context)78     static MimeGroupCommands testApp(Context context) {
79         return new TestAppCommands(context);
80     }
81 
helperApp(Context context)82     static MimeGroupCommands helperApp(Context context) {
83         return new AppCommands(context, PACKAGE_HELPER_APP);
84     }
85 
appWithUpdates(Context context)86     static MimeGroupCommands appWithUpdates(Context context) {
87         return new AppCommands(context, PACKAGE_UPDATE_APP);
88     }
89 
preferredApp(Context context)90     static MimeGroupCommands preferredApp(Context context) {
91         return new AppCommands(context, PACKAGE_PREFERRED_APP);
92     }
93 
noOp()94     static MimeGroupCommands noOp() {
95         return new MimeGroupCommands() {
96             @Override
97             public void setMimeGroup(String mimeGroup, Set<String> mimeTypes) {
98             }
99 
100             @Override
101             public Set<String> getMimeGroupInternal(String mimeGroup) {
102                 return null;
103             }
104         };
105     }
106 }
107