Home | History | Annotate | Download | only in manifest
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
      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.ide.eclipse.adt.internal.editors.manifest;
     18 
     19 import static com.android.xml.AndroidManifest.ATTRIBUTE_MIN_SDK_VERSION;
     20 import static com.android.xml.AndroidManifest.ATTRIBUTE_TARGET_SDK_VERSION;
     21 
     22 import com.android.annotations.VisibleForTesting;
     23 import com.android.ide.eclipse.adt.AdtUtils;
     24 import com.android.ide.eclipse.adt.internal.editors.AndroidContentAssist;
     25 import com.android.ide.eclipse.adt.internal.sdk.AndroidTargetData;
     26 import com.android.ide.eclipse.adt.internal.sdk.Sdk;
     27 import com.android.sdklib.AndroidVersion;
     28 import com.android.sdklib.IAndroidTarget;
     29 import com.android.utils.Pair;
     30 
     31 import org.eclipse.jface.text.contentassist.ICompletionProposal;
     32 import org.w3c.dom.Node;
     33 
     34 import java.util.ArrayList;
     35 import java.util.List;
     36 
     37 /**
     38  * Content Assist Processor for AndroidManifest.xml
     39  */
     40 @VisibleForTesting
     41 public final class ManifestContentAssist extends AndroidContentAssist {
     42 
     43     /**
     44      * Constructor for ManifestContentAssist
     45      */
     46     public ManifestContentAssist() {
     47         super(AndroidTargetData.DESCRIPTOR_MANIFEST);
     48     }
     49 
     50     @Override
     51     protected boolean computeAttributeValues(List<ICompletionProposal> proposals, int offset,
     52             String parentTagName, String attributeName, Node node, String wordPrefix,
     53             boolean skipEndTag, int replaceLength) {
     54         if (attributeName.endsWith(ATTRIBUTE_MIN_SDK_VERSION)
     55                 || attributeName.endsWith(ATTRIBUTE_TARGET_SDK_VERSION)) {
     56             // The user is completing the minSdkVersion attribute: it should be
     57             // an integer for the API version, but we'll add full Android version
     58             // names to make it more obvious what they're selecting
     59 
     60             List<Pair<String, String>> choices = new ArrayList<Pair<String, String>>();
     61             int max = AdtUtils.getHighestKnownApiLevel();
     62             // Look for any more recent installed versions the user may have
     63             Sdk sdk = Sdk.getCurrent();
     64             if (sdk == null) {
     65                 return false;
     66             }
     67             IAndroidTarget[] targets = sdk.getTargets();
     68             for (IAndroidTarget target : targets) {
     69                 AndroidVersion version = target.getVersion();
     70                 int apiLevel = version.getApiLevel();
     71                 if (apiLevel > max) {
     72                     if (version.isPreview()) {
     73                         // Use codename, not API level, as version string for preview versions
     74                         choices.add(Pair.of(version.getCodename(), version.getCodename()));
     75                     } else {
     76                         choices.add(Pair.of(Integer.toString(apiLevel), target.getFullName()));
     77                     }
     78                 }
     79             }
     80             for (int api = max; api >= 1; api--) {
     81                 String name = AdtUtils.getAndroidName(api);
     82                 choices.add(Pair.of(Integer.toString(api), name));
     83             }
     84             char needTag = 0;
     85             addMatchingProposals(proposals, choices.toArray(), offset, node, wordPrefix,
     86                     needTag, true /* isAttribute */, false /* isNew */,
     87                     skipEndTag /* skipEndTag */, replaceLength);
     88             return true;
     89         } else {
     90             return super.computeAttributeValues(proposals, offset, parentTagName, attributeName,
     91                     node, wordPrefix, skipEndTag, replaceLength);
     92         }
     93     }
     94 }
     95