1 /* 2 * Copyright (C) 2013 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 package com.android.tradefed.device; 17 18 import java.util.HashMap; 19 import java.util.Map; 20 21 /** 22 * Container for an application's package info parsed from device. 23 */ 24 public class PackageInfo { 25 26 // numeric flag constants. Copied from 27 // frameworks/base/core/java/android/content/pm/ApplicationInfo.java 28 private static final int FLAG_UPDATED_SYSTEM_APP = 1 << 7; 29 private static final int FLAG_SYSTEM = 1 << 0; 30 31 // string flag constants. Used for newer platforms 32 private static final String FLAG_UPDATED_SYSTEM_APP_TEXT = " UPDATED_SYSTEM_APP "; 33 private static final String FLAG_SYSTEM_TEXT = " SYSTEM "; 34 35 private final String mPackageName; 36 private boolean mIsSystemApp; 37 private boolean mIsUpdatedSystemApp; 38 private Map<String, String> mAttributes = new HashMap<String, String>(); 39 PackageInfo(String pkgName)40 PackageInfo(String pkgName) { 41 mPackageName = pkgName; 42 } 43 44 /** 45 * Returns <code>true</code> if this is a system app that has been updated. 46 */ isUpdatedSystemApp()47 public boolean isUpdatedSystemApp() { 48 return mIsUpdatedSystemApp; 49 } 50 51 /** 52 * Returns <code>true</code> if this is a system app. 53 */ isSystemApp()54 public boolean isSystemApp() { 55 return mIsSystemApp; 56 } 57 58 /** 59 * Returns the package name of the application. 60 */ getPackageName()61 public String getPackageName() { 62 return mPackageName; 63 } 64 65 /** 66 * Returns the version name of the application. 67 * Note: this will return <code>null</code> if 'versionName' attribute was not found, such as 68 * on froyo devices. 69 */ getVersionName()70 public String getVersionName() { 71 return mAttributes.get("versionName"); 72 } 73 setIsUpdatedSystemApp(boolean isUpdatedSystemApp)74 void setIsUpdatedSystemApp(boolean isUpdatedSystemApp) { 75 mIsUpdatedSystemApp = isUpdatedSystemApp; 76 } 77 addAttribute(String name, String value)78 void addAttribute(String name, String value) { 79 mAttributes.put(name, value); 80 if (name.equals("pkgFlags") || name.equals("flags")) { 81 // do special handling of pkgFlags, which can be either hex or string form depending on 82 // device OS version 83 if (!parseFlagsAsInt(value)) { 84 parseFlagsAsString(value); 85 } 86 } 87 } 88 parseFlagsAsString(String flagString)89 private void parseFlagsAsString(String flagString) { 90 mIsSystemApp = flagString.contains(FLAG_SYSTEM_TEXT); 91 mIsUpdatedSystemApp = flagString.contains(FLAG_UPDATED_SYSTEM_APP_TEXT); 92 } 93 parseFlagsAsInt(String value)94 private boolean parseFlagsAsInt(String value) { 95 try { 96 int flags = Integer.decode(value); 97 mIsSystemApp = (flags & FLAG_SYSTEM) != 0; 98 // note: FLAG_UPDATED_SYSTEM_APP never seems to be set. Rely on parsing hidden system 99 // packages 100 mIsUpdatedSystemApp = (flags & FLAG_UPDATED_SYSTEM_APP) != 0; 101 return true; 102 } catch (NumberFormatException e) { 103 // not an int, fall through 104 } 105 return false; 106 } 107 } 108