The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 android.app; |
| 18 | |
| 19 | import org.xmlpull.v1.XmlPullParser; |
| 20 | import org.xmlpull.v1.XmlPullParserException; |
| 21 | |
| 22 | import android.content.Intent; |
| 23 | import android.content.pm.ActivityInfo; |
| 24 | import android.content.pm.PackageManager; |
| 25 | import android.content.res.XmlResourceParser; |
| 26 | import android.os.Bundle; |
| 27 | import android.util.AttributeSet; |
| 28 | import android.util.Xml; |
Tom Taylor | d4a4729 | 2009-12-21 13:59:18 -0800 | [diff] [blame^] | 29 | import com.android.common.XmlUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
| 31 | import java.io.IOException; |
| 32 | |
| 33 | /** |
| 34 | * Stub activity that launches another activity (and then finishes itself) |
| 35 | * based on information in its component's manifest meta-data. This is a |
| 36 | * simple way to implement an alias-like mechanism. |
| 37 | * |
| 38 | * To use this activity, you should include in the manifest for the associated |
| 39 | * component an entry named "android.app.alias". It is a reference to an XML |
| 40 | * resource describing an intent that launches the real application. |
| 41 | */ |
| 42 | public class AliasActivity extends Activity { |
| 43 | /** |
| 44 | * This is the name under which you should store in your component the |
| 45 | * meta-data information about the alias. It is a reference to an XML |
| 46 | * resource describing an intent that launches the real application. |
| 47 | * {@hide} |
| 48 | */ |
| 49 | public final String ALIAS_META_DATA = "android.app.alias"; |
| 50 | |
| 51 | @Override |
| 52 | protected void onCreate(Bundle savedInstanceState) { |
| 53 | super.onCreate(savedInstanceState); |
| 54 | |
| 55 | XmlResourceParser parser = null; |
| 56 | try { |
| 57 | ActivityInfo ai = getPackageManager().getActivityInfo( |
| 58 | getComponentName(), PackageManager.GET_META_DATA); |
| 59 | parser = ai.loadXmlMetaData(getPackageManager(), |
| 60 | ALIAS_META_DATA); |
| 61 | if (parser == null) { |
| 62 | throw new RuntimeException("Alias requires a meta-data field " |
| 63 | + ALIAS_META_DATA); |
| 64 | } |
| 65 | |
| 66 | Intent intent = parseAlias(parser); |
| 67 | if (intent == null) { |
| 68 | throw new RuntimeException( |
| 69 | "No <intent> tag found in alias description"); |
| 70 | } |
| 71 | |
| 72 | startActivity(intent); |
| 73 | finish(); |
| 74 | |
| 75 | } catch (PackageManager.NameNotFoundException e) { |
| 76 | throw new RuntimeException("Error parsing alias", e); |
| 77 | } catch (XmlPullParserException e) { |
| 78 | throw new RuntimeException("Error parsing alias", e); |
| 79 | } catch (IOException e) { |
| 80 | throw new RuntimeException("Error parsing alias", e); |
| 81 | } finally { |
| 82 | if (parser != null) parser.close(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private Intent parseAlias(XmlPullParser parser) |
| 87 | throws XmlPullParserException, IOException { |
| 88 | AttributeSet attrs = Xml.asAttributeSet(parser); |
| 89 | |
| 90 | Intent intent = null; |
| 91 | |
| 92 | int type; |
| 93 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 94 | && type != XmlPullParser.START_TAG) { |
| 95 | } |
| 96 | |
| 97 | String nodeName = parser.getName(); |
| 98 | if (!"alias".equals(nodeName)) { |
| 99 | throw new RuntimeException( |
| 100 | "Alias meta-data must start with <alias> tag; found" |
| 101 | + nodeName + " at " + parser.getPositionDescription()); |
| 102 | } |
| 103 | |
| 104 | int outerDepth = parser.getDepth(); |
| 105 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 106 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 107 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | nodeName = parser.getName(); |
| 112 | if ("intent".equals(nodeName)) { |
| 113 | Intent gotIntent = Intent.parseIntent(getResources(), parser, attrs); |
| 114 | if (intent == null) intent = gotIntent; |
| 115 | } else { |
| 116 | XmlUtils.skipCurrentTag(parser); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return intent; |
| 121 | } |
| 122 | |
| 123 | } |