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 android.device.collectors; 18 19 import android.device.collectors.annotations.OptionClass; 20 import android.os.Bundle; 21 import android.util.Log; 22 import androidx.annotation.VisibleForTesting; 23 24 import com.android.helpers.JankCollectionHelper; 25 26 import java.util.Arrays; 27 28 /** 29 * A {@link BaseCollectionListener} that captures and records jank metrics for a specific package or 30 * for all packages if none are specified. 31 */ 32 @OptionClass(alias = "jank-listener") 33 public class JankListener extends BaseCollectionListener<Double> { 34 private static final String LOG_TAG = JankListener.class.getSimpleName(); 35 36 @VisibleForTesting static final String PACKAGE_SEPARATOR = ","; 37 @VisibleForTesting static final String PACKAGE_NAMES_KEY = "jank-package-names"; 38 JankListener()39 public JankListener() { 40 createHelperInstance(new JankCollectionHelper()); 41 } 42 43 @VisibleForTesting JankListener(Bundle args, JankCollectionHelper helper)44 public JankListener(Bundle args, JankCollectionHelper helper) { 45 super(args, helper); 46 } 47 48 /** Tracks the provided packages if specified, or all packages if not specified. */ 49 @Override setupAdditionalArgs()50 public void setupAdditionalArgs() { 51 Bundle args = getArgsBundle(); 52 String pkgs = args.getString(PACKAGE_NAMES_KEY); 53 if (pkgs != null) { 54 Log.v(LOG_TAG, String.format("Adding packages: %s", pkgs)); 55 // Basic malformed input check: trim packages and remove empty ones. 56 String[] splitPkgs = 57 Arrays.stream(pkgs.split(PACKAGE_SEPARATOR)) 58 .map(String::trim) 59 .filter(item -> !item.isEmpty()) 60 .toArray(String[]::new); 61 ((JankCollectionHelper) mHelper).addTrackedPackages(splitPkgs); 62 } else { 63 Log.v(LOG_TAG, "Tracking all packages for jank."); 64 } 65 } 66 } 67