1 /*
2  * Copyright (C) 2019 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.android.sts.common;
18 
19 import com.android.ddmlib.Log;
20 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
21 
22 import com.google.common.collect.ImmutableSet;
23 
24 import java.util.Set;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27 
28 public class HostsideMainlineModuleDetector {
29     private static final String LOG_TAG = "MainlineModuleDetector";
30 
31     private BaseHostJUnit4Test context;
32 
33     private static ImmutableSet<String> playManagedModules;
34 
HostsideMainlineModuleDetector(BaseHostJUnit4Test context)35     public HostsideMainlineModuleDetector(BaseHostJUnit4Test context) {
36         this.context = context;
37     }
38 
getPlayManagedModules()39     public synchronized Set<String> getPlayManagedModules() throws Exception {
40         if (playManagedModules == null) {
41             context.getDevice().executeShellV2Command("logcat -c");
42             String output =
43                     context.getDevice()
44                             .executeShellV2Command(
45                                     "am start"
46                                         + " com.android.cts.mainlinemoduledetector/.MainlineModuleDetector")
47                             .getStdout();
48             Log.logAndDisplay(Log.LogLevel.INFO, LOG_TAG, "am output: " + output);
49             Thread.sleep(5 * 1000L);
50             String logcat =
51                     context.getDevice()
52                             .executeShellV2Command("logcat -d -s MainlineModuleDetector:I")
53                             .getStdout();
54             Log.logAndDisplay(Log.LogLevel.INFO, LOG_TAG, "Found logcat output: " + logcat);
55             Matcher matcher = Pattern.compile("Play managed modules are: <(.*?)>").matcher(logcat);
56             if (matcher.find()) {
57                 playManagedModules = ImmutableSet.copyOf(matcher.group(1).split(","));
58             } else {
59                 playManagedModules = ImmutableSet.of();
60             }
61         }
62         return playManagedModules;
63     }
64 }
65