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 import com.android.sdklib.AndroidVersion; 20 21 22 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_EXTRA = new AndroidVersion(-1, null); 36 37 public PkgCategoryApi(AndroidVersion version, String platformName, Object iconRef) { 38 super(version, null /*label*/, iconRef); 39 setPlatformName(platformName); 40 } 41 42 public String getPlatformName() { 43 return mPlatformName; 44 } 45 46 public void setPlatformName(String platformName) { 47 if (platformName != null) { 48 // Normal case for actual platform categories 49 mPlatformName = String.format("Android %1$s", platformName); 50 super.setLabel(null); 51 } 52 } 53 54 public String getApiLabel() { 55 AndroidVersion key = (AndroidVersion) getKey(); 56 if (key.equals(KEY_TOOLS)) { 57 return "TOOLS"; //$NON-NLS-1$ // for internal debug use only 58 } else if (key.equals(KEY_EXTRA)) { 59 return "EXTRAS"; //$NON-NLS-1$ // for internal debug use only 60 } else { 61 return key.toString(); 62 } 63 } 64 65 @Override 66 public String getLabel() { 67 String label = super.getLabel(); 68 if (label == null) { 69 AndroidVersion key = (AndroidVersion) getKey(); 70 71 if (key.equals(KEY_TOOLS)) { 72 label = "Tools"; 73 } else if (key.equals(KEY_EXTRA)) { 74 label = "Extras"; 75 } else { 76 if (mPlatformName != null) { 77 label = String.format("%1$s (%2$s)", mPlatformName, getApiLabel()); 78 } else { 79 label = getApiLabel(); 80 } 81 } 82 super.setLabel(label); 83 } 84 return label; 85 } 86 87 @Override 88 public void setLabel(String label) { 89 throw new UnsupportedOperationException("Use setPlatformName() instead."); 90 } 91 92 @Override 93 public String toString() { 94 return String.format("%s <API=%s, label=%s, #items=%d>", 95 this.getClass().getSimpleName(), 96 getApiLabel(), 97 getLabel(), 98 getItems().size()); 99 } 100 } 101