1 /* 2 * Copyright (C) 2010 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.commands.monkey; 18 19 import java.text.SimpleDateFormat; 20 import java.util.HashSet; 21 import java.util.Iterator; 22 import java.util.Set; 23 24 /** 25 * Misc utilities. 26 */ 27 public abstract class MonkeyUtils { 28 29 private static final java.util.Date DATE = new java.util.Date(); 30 private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat( 31 "yyyy-MM-dd HH:mm:ss.SSS "); 32 private static PackageFilter sFilter; 33 MonkeyUtils()34 private MonkeyUtils() { 35 } 36 37 /** 38 * Return calendar time in pretty string. 39 */ toCalendarTime(long time)40 public static synchronized String toCalendarTime(long time) { 41 DATE.setTime(time); 42 return DATE_FORMATTER.format(DATE); 43 } 44 getPackageFilter()45 public static PackageFilter getPackageFilter() { 46 if (sFilter == null) { 47 sFilter = new PackageFilter(); 48 } 49 return sFilter; 50 } 51 52 public static class PackageFilter { 53 private Set<String> mValidPackages = new HashSet<>(); 54 private Set<String> mInvalidPackages = new HashSet<>(); 55 PackageFilter()56 private PackageFilter() { 57 } 58 addValidPackages(Set<String> validPackages)59 public void addValidPackages(Set<String> validPackages) { 60 mValidPackages.addAll(validPackages); 61 } 62 addInvalidPackages(Set<String> invalidPackages)63 public void addInvalidPackages(Set<String> invalidPackages) { 64 mInvalidPackages.addAll(invalidPackages); 65 } 66 hasValidPackages()67 public boolean hasValidPackages() { 68 return mValidPackages.size() > 0; 69 } 70 isPackageValid(String pkg)71 public boolean isPackageValid(String pkg) { 72 return mValidPackages.contains(pkg); 73 } 74 isPackageInvalid(String pkg)75 public boolean isPackageInvalid(String pkg) { 76 return mInvalidPackages.contains(pkg); 77 } 78 79 /** 80 * Check whether we should run against the given package. 81 * 82 * @param pkg The package name. 83 * @return Returns true if we should run against pkg. 84 */ checkEnteringPackage(String pkg)85 public boolean checkEnteringPackage(String pkg) { 86 if (mInvalidPackages.size() > 0) { 87 if (mInvalidPackages.contains(pkg)) { 88 return false; 89 } 90 } else if (mValidPackages.size() > 0) { 91 if (!mValidPackages.contains(pkg)) { 92 return false; 93 } 94 } 95 return true; 96 } 97 dump()98 public void dump() { 99 if (mValidPackages.size() > 0) { 100 Iterator<String> it = mValidPackages.iterator(); 101 while (it.hasNext()) { 102 Logger.out.println(":AllowPackage: " + it.next()); 103 } 104 } 105 if (mInvalidPackages.size() > 0) { 106 Iterator<String> it = mInvalidPackages.iterator(); 107 while (it.hasNext()) { 108 Logger.out.println(":DisallowPackage: " + it.next()); 109 } 110 } 111 } 112 } 113 } 114