1 /* 2 * Copyright (C) 2011 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.sdkuilib.internal.repository.core; 18 19 import com.android.sdklib.AndroidVersion; 20 21 22 public class PkgCategoryApi extends PkgCategory { 23 24 /** Platform name, in the form "Android 1.2". Can be null if we don't have the name. */ 25 private String mPlatformName; 26 27 // When sorting by Source, key is the hash of the source's name. 28 // When storing by API, key is the AndroidVersion (API level >=1 + optional codename). 29 // We always want categories in order tools..platforms..extras; to achieve that tools 30 // and extras have the special values so they get "naturally" sorted the way we want 31 // them. 32 // (Note: don't use integer.max to avoid integers wrapping in comparisons. We can 33 // revisit the day we get 2^30 platforms.) 34 public final static AndroidVersion KEY_TOOLS = new AndroidVersion(Integer.MAX_VALUE / 2, null); 35 public final static AndroidVersion KEY_TOOLS_PREVIEW = 36 new AndroidVersion(Integer.MAX_VALUE / 2 - 1, null); 37 public final static AndroidVersion KEY_EXTRA = new AndroidVersion(-1, null); 38 39 public PkgCategoryApi(AndroidVersion version, String platformName, Object iconRef) { 40 super(version, null /*label*/, iconRef); 41 setPlatformName(platformName); 42 } 43 44 public String getPlatformName() { 45 return mPlatformName; 46 } 47 48 public void setPlatformName(String platformName) { 49 if (platformName != null) { 50 // Normal case for actual platform categories 51 mPlatformName = String.format("Android %1$s", platformName); 52 super.setLabel(null); 53 } 54 } 55 56 public String getApiLabel() { 57 AndroidVersion key = (AndroidVersion) getKey(); 58 if (key.equals(KEY_TOOLS)) { 59 return "TOOLS"; //$NON-NLS-1$ // for internal debug use only 60 } else if (key.equals(KEY_TOOLS_PREVIEW)) { 61 return "TOOLS-PREVIEW"; //$NON-NLS-1$ // for internal debug use only 62 } else if (key.equals(KEY_EXTRA)) { 63 return "EXTRAS"; //$NON-NLS-1$ // for internal debug use only 64 } else { 65 return key.toString(); 66 } 67 } 68 69 @Override 70 public String getLabel() { 71 String label = super.getLabel(); 72 if (label == null) { 73 AndroidVersion key = (AndroidVersion) getKey(); 74 75 if (key.equals(KEY_TOOLS)) { 76 label = "Tools"; 77 } else if (key.equals(KEY_TOOLS_PREVIEW)) { 78 label = "Tools (Preview Channel)"; 79 } else if (key.equals(KEY_EXTRA)) { 80 label = "Extras"; 81 } else { 82 if (mPlatformName != null) { 83 label = String.format("%1$s (%2$s)", mPlatformName, getApiLabel()); 84 } else { 85 label = getApiLabel(); 86 } 87 } 88 super.setLabel(label); 89 } 90 return label; 91 } 92 93 @Override 94 public void setLabel(String label) { 95 throw new UnsupportedOperationException("Use setPlatformName() instead."); 96 } 97 98 @Override 99 public String toString() { 100 return String.format("%s <API=%s, label=%s, #items=%d>", 101 this.getClass().getSimpleName(), 102 getApiLabel(), 103 getLabel(), 104 getItems().size()); 105 } 106 } 107