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