• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import android.content.pm.ActivityInfo;
4 import android.content.pm.ApplicationInfo;
5 import android.content.pm.ResolveInfo;
6 import android.os.Build;
7 
8 /** Utilities for {@link ResolveInfo}. */
9 // TODO: Create a ResolveInfoBuilder in androidx and migrate factory methods there.
10 public class ShadowResolveInfo {
11 
12   /**
13    * Creates a {@link ResolveInfo}.
14    *
15    * @param displayName Display name.
16    * @param packageName Package name.
17    * @return Resolve info instance.
18    */
newResolveInfo(String displayName, String packageName)19   public static ResolveInfo newResolveInfo(String displayName, String packageName) {
20     return newResolveInfo(displayName, packageName, null);
21   }
22 
23   /**
24    * Creates a {@link ResolveInfo}.
25    *
26    * @param displayName Display name.
27    * @param packageName Package name.
28    * @param activityName Activity name.
29    * @return Resolve info instance.
30    */
newResolveInfo(String displayName, String packageName, String activityName)31   public static ResolveInfo newResolveInfo(String displayName, String packageName, String activityName) {
32     ResolveInfo resInfo = new ResolveInfo();
33     ActivityInfo actInfo = new ActivityInfo();
34     actInfo.applicationInfo = new ApplicationInfo();
35     actInfo.packageName = packageName;
36     actInfo.applicationInfo.packageName = packageName;
37     actInfo.name = activityName;
38     resInfo.activityInfo = actInfo;
39     resInfo.nonLocalizedLabel = displayName;
40     return resInfo;
41   }
42 
43   /**
44    * Copies {@link ResolveInfo}.
45    *
46    * <p>Note that this is shallow copy as performed by the copy constructor existing in API 17.
47    */
newResolveInfo(ResolveInfo orig)48   public static ResolveInfo newResolveInfo(ResolveInfo orig) {
49     ResolveInfo copy;
50     if (Build.VERSION.SDK_INT >= 17) {
51       copy = new ResolveInfo(orig);
52     } else {
53       copy = new ResolveInfo();
54       copy.activityInfo = orig.activityInfo;
55       copy.serviceInfo = orig.serviceInfo;
56       copy.filter = orig.filter;
57       copy.priority = orig.priority;
58       copy.preferredOrder = orig.preferredOrder;
59       copy.match = orig.match;
60       copy.specificIndex = orig.specificIndex;
61       copy.labelRes = orig.labelRes;
62       copy.nonLocalizedLabel = orig.nonLocalizedLabel;
63       copy.icon = orig.icon;
64       copy.resolvePackageName = orig.resolvePackageName;
65     }
66     // For some reason isDefault field is not copied by the copy constructor.
67     copy.isDefault = orig.isDefault;
68     return copy;
69   }
70 }
71