1 /*
2  * Copyright (C) 2018 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.example.android.intentplayground;
18 
19 import java.util.Arrays;
20 import java.util.HashSet;
21 import java.util.Set;
22 import java.util.stream.Collectors;
23 
24 import static java.util.Collections.emptySet;
25 
26 /**
27  * Represents the different intent flags related to activities and tasks.
28  */
29 enum IntentFlag {
30     SINGLE_TOP ("FLAG_ACTIVITY_SINGLE_TOP", emptySet(), emptySet(), emptySet()),
31     BROUGHT_TO_FRONT ("FLAG_ACTIVITY_BROUGHT_TO_FRONT", emptySet(), emptySet(), emptySet()),
32     NEW_TASK ("FLAG_ACTIVITY_NEW_TASK", emptySet(), emptySet(), emptySet()),
33     CLEAR_TASK ("FLAG_ACTIVITY_CLEAR_TASK", emptySet(), emptySet(), setOf(NEW_TASK)),
34     CLEAR_TOP ("FLAG_ACTIVITY_CLEAR_TOP", setOf(SINGLE_TOP, NEW_TASK), emptySet(), emptySet()),
35     MATCH_EXTERNAL("FLAG_ACTIVITY_MATCH_EXTERNAL", emptySet(), emptySet(), emptySet()),
36     MULTIPLE_TASK ("FLAG_ACTIVITY_MULTIPLE_TASK", emptySet(), emptySet(), setOf(NEW_TASK)),
37     NEW_DOCUMENT ("FLAG_ACTIVITY_NEW_DOCUMENT", setOf(MULTIPLE_TASK), emptySet(),
38             emptySet()),
39     RETAIN_IN_RECENTS ("FLAG_ACTIVITY_RETAIN_IN_RECENTS", setOf(NEW_DOCUMENT),
40             emptySet(), emptySet()),
41     CLEAR_WHEN_TASK_RESET ("FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET", emptySet(), emptySet(),
42             emptySet()),
43     EXCLUDE_FROM_RECENTS ("FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS", emptySet(), setOf(RETAIN_IN_RECENTS),
44             emptySet()),
45     FORWARD_RESULT ("FLAG_ACTIVITY_FORWARD_RESULT", emptySet(), emptySet(), emptySet()),
46     LAUNCHED_FROM_HISTORY ("FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY", emptySet(), emptySet(),
47             emptySet()),
48     LAUNCH_ADJACENT ("FLAG_ACTIVITY_LAUNCH_ADJACENT", setOf(MULTIPLE_TASK), emptySet(),
49             setOf(NEW_TASK)),
50     NO_ANIMATION ("FLAG_ACTIVITY_NO_ANIMATION", emptySet(), emptySet(), emptySet()),
51     NO_HISTORY ("FLAG_ACTIVITY_NO_HISTORY", emptySet(), emptySet(), emptySet()),
52     NO_USER_ACTION ("FLAG_ACTIVITY_NO_USER_ACTION", emptySet(), emptySet(), emptySet()),
53     PREVIOUS_IS_TOP ("FLAG_ACTIVITY_PREVIOUS_IS_TOP", emptySet(), emptySet(), emptySet()),
54     REORDER_TO_FRONT ("FLAG_ACTIVITY_REORDER_TO_FRONT", emptySet(), setOf(CLEAR_TOP), emptySet()),
55     RESET_TASK_IF_NEEDED ("FLAG_ACTIVITY_RESET_TASK_IF_NEEDED", emptySet(), emptySet(), emptySet()),
56     TASK_ON_HOME ("FLAG_ACTIVITY_TASK_ON_HOME", emptySet(), emptySet(), setOf(NEW_TASK));
57 
58     public String name;
59     private Set<IntentFlag> mComplements = new HashSet<>();
60     private Set<IntentFlag> mConflicts = new HashSet<>();
61     private Set<IntentFlag> mRequests = new HashSet<>();
62 
IntentFlag(String name, Set<IntentFlag> complements, Set<IntentFlag> conflicts, Set<IntentFlag> requests)63     IntentFlag(String name, Set<IntentFlag> complements, Set<IntentFlag> conflicts,
64                Set<IntentFlag> requests) {
65         this.name = name;
66         this.mComplements = complements;
67         this.mConflicts = conflicts;
68         this.mRequests = requests;
69     }
70 
71     /**
72      * @return A set of flags that complement the action of this flag.
73      */
getComplements()74     public Set<IntentFlag> getComplements() {
75         return mComplements;
76     }
77 
78     /**
79      * @return A set of flags that conflict with the action of this flag.
80      */
getConflicts()81     public Set<IntentFlag> getConflicts() {
82         return mConflicts;
83     }
84 
85     /**
86      * @return A set of flags that are necessary for the action of this flag.
87      */
getRequests()88     public Set<IntentFlag> getRequests() {
89         return mRequests;
90     }
91 
getName()92     public String getName() {
93         return name;
94     }
95 
96     /**
97      * Convenience method to create a set of intent flags.
98      */
setOf(IntentFlag... flags)99     protected static Set<IntentFlag> setOf(IntentFlag... flags) {
100         return Arrays.stream(flags).collect(Collectors.toSet());
101     }
102 }
103 
104