aboutsummaryrefslogtreecommitdiff
path: root/resources/src/main/java/org/robolectric/manifest/BroadcastReceiverData.java
blob: 37b08631cc11ea35da1975b8fb5b36cd9d25abcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package org.robolectric.manifest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;

public class BroadcastReceiverData extends PackageItemData {

  private static final String EXPORTED = "android:exported";
  private static final String NAME = "android:name";
  private static final String PERMISSION = "android:permission";

  private final Map<String, String> attributes;
  private final List<String> actions;
  private List<IntentFilterData> intentFilters;

  public BroadcastReceiverData(
      Map<String, String> attributes, MetaData metaData, List<IntentFilterData> intentFilters) {
    super(attributes.get(NAME), metaData);
    this.attributes = attributes;
    this.actions = new ArrayList<>();
    this.intentFilters = new LinkedList<>(intentFilters);
  }

  public BroadcastReceiverData(String className, MetaData metaData) {
    super(className, metaData);
    this.actions = new ArrayList<>();
    this.attributes = new HashMap<>();
    intentFilters = new LinkedList<>();
  }

  public List<String> getActions() {
    return actions;
  }

  public void addAction(String action) {
    this.actions.add(action);
  }

  public void setPermission(final String permission) {
    attributes.put(PERMISSION, permission);
  }

  public String getPermission() {
    return attributes.get(PERMISSION);
  }

  /**
   * Get the intent filters defined for the broadcast receiver.
   *
   * @return A list of intent filters.
   */
  @Nonnull
  public List<IntentFilterData> getIntentFilters() {
    return intentFilters;
  }

  /**
   * Get the map for all attributes defined for the broadcast receiver.
   *
   * @return map of attributes names to values from the manifest.
   */
  @Nonnull
  public Map<String, String> getAllAttributes() {
    return attributes;
  }

  /**
   * Returns whether this broadcast receiver is exported by checking the XML attribute.
   *
   * @return true if the broadcast receiver is exported
   */
  public boolean isExported() {
    boolean defaultValue = !intentFilters.isEmpty();
    return (attributes.containsKey(EXPORTED)
        ? Boolean.parseBoolean(attributes.get(EXPORTED))
        : defaultValue);
  }
}