| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.compat.annotation.UnsupportedAppUsage; |
| 22 |
import android.os.Build; |
| 23 |
import android.os.Parcel; |
| 24 |
import android.os.Parcelable; |
| 25 |
import android.text.TextUtils; |
| 26 |
import android.util.proto.ProtoOutputStream; |
| 27 |
|
| 28 |
import java.io.PrintWriter; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public final class ComponentName implements Parcelable, Cloneable, Comparable<ComponentName> { |
| 41 |
private final String mPackage; |
| 42 |
private final String mClass; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public static @NonNull ComponentName createRelative(@NonNull String pkg, @NonNull String cls) { |
| 60 |
if (TextUtils.isEmpty(cls)) { |
| 61 |
throw new IllegalArgumentException("class name cannot be empty"); |
| 62 |
} |
| 63 |
|
| 64 |
final String fullName; |
| 65 |
if (cls.charAt(0) == '.') { |
| 66 |
|
| 67 |
fullName = pkg + cls; |
| 68 |
} else { |
| 69 |
|
| 70 |
fullName = cls; |
| 71 |
} |
| 72 |
return new ComponentName(pkg, fullName); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public static @NonNull ComponentName createRelative(@NonNull Context pkg, @NonNull String cls) { |
| 91 |
return createRelative(pkg.getPackageName(), cls); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public ComponentName(@NonNull String pkg, @NonNull String cls) { |
| 103 |
if (pkg == null) throw new NullPointerException("package name is null"); |
| 104 |
if (cls == null) throw new NullPointerException("class name is null"); |
| 105 |
mPackage = pkg; |
| 106 |
mClass = cls; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public ComponentName(@NonNull Context pkg, @NonNull String cls) { |
| 118 |
if (cls == null) throw new NullPointerException("class name is null"); |
| 119 |
mPackage = pkg.getPackageName(); |
| 120 |
mClass = cls; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public ComponentName(@NonNull Context pkg, @NonNull Class<?> cls) { |
| 132 |
mPackage = pkg.getPackageName(); |
| 133 |
mClass = cls.getName(); |
| 134 |
} |
| 135 |
|
| 136 |
public ComponentName clone() { |
| 137 |
return new ComponentName(mPackage, mClass); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public @NonNull String getPackageName() { |
| 144 |
return mPackage; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public @NonNull String getClassName() { |
| 151 |
return mClass; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public String getShortClassName() { |
| 159 |
if (mClass.startsWith(mPackage)) { |
| 160 |
int PN = mPackage.length(); |
| 161 |
int CN = mClass.length(); |
| 162 |
if (CN > PN && mClass.charAt(PN) == '.') { |
| 163 |
return mClass.substring(PN, CN); |
| 164 |
} |
| 165 |
} |
| 166 |
return mClass; |
| 167 |
} |
| 168 |
|
| 169 |
private static void appendShortClassName(StringBuilder sb, String packageName, |
| 170 |
String className) { |
| 171 |
if (className.startsWith(packageName)) { |
| 172 |
int PN = packageName.length(); |
| 173 |
int CN = className.length(); |
| 174 |
if (CN > PN && className.charAt(PN) == '.') { |
| 175 |
sb.append(className, PN, CN); |
| 176 |
return; |
| 177 |
} |
| 178 |
} |
| 179 |
sb.append(className); |
| 180 |
} |
| 181 |
|
| 182 |
private static void printShortClassName(PrintWriter pw, String packageName, |
| 183 |
String className) { |
| 184 |
if (className.startsWith(packageName)) { |
| 185 |
int PN = packageName.length(); |
| 186 |
int CN = className.length(); |
| 187 |
if (CN > PN && className.charAt(PN) == '.') { |
| 188 |
pw.write(className, PN, CN-PN); |
| 189 |
return; |
| 190 |
} |
| 191 |
} |
| 192 |
pw.print(className); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
@Nullable |
| 202 |
public static String flattenToShortString(@Nullable ComponentName componentName) { |
| 203 |
return componentName == null ? null : componentName.flattenToShortString(); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public @NonNull String flattenToString() { |
| 219 |
return mPackage + "/" + mClass; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public @NonNull String flattenToShortString() { |
| 234 |
StringBuilder sb = new StringBuilder(mPackage.length() + mClass.length()); |
| 235 |
appendShortString(sb, mPackage, mClass); |
| 236 |
return sb.toString(); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
public void appendShortString(StringBuilder sb) { |
| 241 |
appendShortString(sb, mPackage, mClass); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 246 |
public static void appendShortString(StringBuilder sb, String packageName, String className) { |
| 247 |
sb.append(packageName).append('/'); |
| 248 |
appendShortClassName(sb, packageName, className); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 253 |
public static void printShortString(PrintWriter pw, String packageName, String className) { |
| 254 |
pw.print(packageName); |
| 255 |
pw.print('/'); |
| 256 |
printShortClassName(pw, packageName, className); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public static @Nullable ComponentName unflattenFromString(@NonNull String str) { |
| 276 |
int sep = str.indexOf('/'); |
| 277 |
if (sep < 0 || (sep+1) >= str.length()) { |
| 278 |
return null; |
| 279 |
} |
| 280 |
String pkg = str.substring(0, sep); |
| 281 |
String cls = str.substring(sep+1); |
| 282 |
if (cls.length() > 0 && cls.charAt(0) == '.') { |
| 283 |
cls = pkg + cls; |
| 284 |
} |
| 285 |
return new ComponentName(pkg, cls); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
public String toShortString() { |
| 293 |
return "{" + mPackage + "/" + mClass + "}"; |
| 294 |
} |
| 295 |
|
| 296 |
@Override |
| 297 |
public String toString() { |
| 298 |
return "ComponentInfo{" + mPackage + "/" + mClass + "}"; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
public void dumpDebug(ProtoOutputStream proto, long fieldId) { |
| 303 |
final long token = proto.start(fieldId); |
| 304 |
proto.write(ComponentNameProto.PACKAGE_NAME, mPackage); |
| 305 |
proto.write(ComponentNameProto.CLASS_NAME, mClass); |
| 306 |
proto.end(token); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
@Override |
| 316 |
public boolean equals(@Nullable Object obj) { |
| 317 |
if (obj instanceof ComponentName) { |
| 318 |
ComponentName other = (ComponentName) obj; |
| 319 |
|
| 320 |
return mPackage.equals(other.mPackage) |
| 321 |
&& mClass.equals(other.mClass); |
| 322 |
} else { |
| 323 |
return false; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
@Override |
| 328 |
public int hashCode() { |
| 329 |
return mPackage.hashCode() + mClass.hashCode(); |
| 330 |
} |
| 331 |
|
| 332 |
public int compareTo(ComponentName that) { |
| 333 |
int v; |
| 334 |
v = this.mPackage.compareTo(that.mPackage); |
| 335 |
if (v != 0) { |
| 336 |
return v; |
| 337 |
} |
| 338 |
return this.mClass.compareTo(that.mClass); |
| 339 |
} |
| 340 |
|
| 341 |
public int describeContents() { |
| 342 |
return 0; |
| 343 |
} |
| 344 |
|
| 345 |
public void writeToParcel(Parcel out, int flags) { |
| 346 |
|
| 347 |
|
| 348 |
out.writeString(mPackage); |
| 349 |
out.writeString(mClass); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
public static void writeToParcel(ComponentName c, Parcel out) { |
| 362 |
if (c != null) { |
| 363 |
c.writeToParcel(out, 0); |
| 364 |
} else { |
| 365 |
out.writeString(null); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
public static ComponentName readFromParcel(Parcel in) { |
| 381 |
String pkg = in.readString(); |
| 382 |
return pkg != null ? new ComponentName(pkg, in) : null; |
| 383 |
} |
| 384 |
|
| 385 |
public static final @android.annotation.NonNull Parcelable.Creator<ComponentName> CREATOR |
| 386 |
= new Parcelable.Creator<ComponentName>() { |
| 387 |
public ComponentName createFromParcel(Parcel in) { |
| 388 |
return new ComponentName(in); |
| 389 |
} |
| 390 |
|
| 391 |
public ComponentName[] newArray(int size) { |
| 392 |
return new ComponentName[size]; |
| 393 |
} |
| 394 |
}; |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
public ComponentName(Parcel in) { |
| 407 |
mPackage = in.readString(); |
| 408 |
if (mPackage == null) throw new NullPointerException( |
| 409 |
"package name is null"); |
| 410 |
mClass = in.readString(); |
| 411 |
if (mClass == null) throw new NullPointerException( |
| 412 |
"class name is null"); |
| 413 |
} |
| 414 |
|
| 415 |
private ComponentName(String pkg, Parcel in) { |
| 416 |
mPackage = pkg; |
| 417 |
mClass = in.readString(); |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
@FunctionalInterface |
| 425 |
public interface WithComponentName { |
| 426 |
|
| 427 |
ComponentName getComponentName(); |
| 428 |
} |
| 429 |
} |