1/*
2 * Copyright 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// Returns just the class name and root package information
18function getComponentClassName(componentFullName) {
19  const classParts = componentFullName.split('.');
20
21  if (classParts.length <= 3) {
22    return componentFullName;
23  }
24
25  const className = classParts.slice(-1).pop();
26
27  return `${classParts[0]}.${classParts[1]}.(...).${className}`
28}
29
30const hashCode = /([A-Fa-f0-9]{7}|[A-Fa-f0-9]{6})/;
31const packageRegex = /(([a-z][a-z_0-9]*\.)*([a-z][a-z_0-9]*))/;
32const qualifiedClassNameRegex = /(([a-z][a-z_0-9]*\.)*[A-Z_]($[A-Z_]|[\w_])*)/;
33
34const surfaceRegex =
35  new RegExp(/^Surface\(.*\)\/@0x/.source + hashCode.source +
36    / - .*/.source + "$");
37
38const moduleName =
39  new RegExp("^" +
40    "(" + packageRegex.source + /\//.source + ")?" +
41    qualifiedClassNameRegex.source +
42    /(\$.*)?/.source +
43    /(\#.*)?/.source +
44    "$");
45
46function getSimplifiedLayerName(layerName) {
47  // Get rid of prepended hash code
48  let removedHashCodePrefix = false;
49  if (new RegExp("^" + hashCode.source + " ").test(layerName)) {
50    layerName = layerName.split(" ").slice(1).join(" ");
51    removedHashCodePrefix = true;
52  }
53
54  if (/^ActivityRecord\{.*\}?(\#[0-9]+)?$/.test(layerName)) {
55    return "ActivityRecord";
56  }
57
58  if (/^WindowToken\{.*\}(\#[0-9]*)?$/.test(layerName)) {
59    return "WindowToken";
60  }
61
62  if (/^WallpaperWindowToken\{.*\}(\#[0-9]*)?$/.test(layerName)) {
63    return "WallpaperWindowToken";
64  }
65
66  if (surfaceRegex.test(layerName)) {
67    return "Surface - " + layerName.split("- ").slice(-1).pop();
68  }
69
70  if (moduleName.test(layerName)) {
71    return layerName.split(".").slice(-1).pop();
72  }
73
74  if (removedHashCodePrefix) {
75    return layerName;
76  }
77
78  return null;
79}
80
81export { getComponentClassName, getSimplifiedLayerName };