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 android.content.Intent;
20 import android.content.pm.ActivityInfo;
21 import android.content.pm.PackageManager;
22 import android.content.res.XmlResourceParser;
23 import android.os.Bundle;
24 import android.util.AttributeSet;
25 import android.util.Xml;
26 
27 import com.android.internal.util.XmlUtils;
28 
29 import org.xmlpull.v1.XmlPullParser;
30 import org.xmlpull.v1.XmlPullParserException;
31 
32 import java.io.IOException;
33 
34 /**
35  * Stub activity that launches another activity (and then finishes itself)
36  * based on information in its component's manifest meta-data.  This is a
37  * simple way to implement an alias-like mechanism.
38  *
39  * To use this activity, you should include in the manifest for the associated
40  * component an entry named "android.app.alias".  It is a reference to an XML
41  * resource describing an intent that launches the real application.
42  *
43  * @deprecated Use {@code <activity-alias>} or subclass Activity directly.
44  */
45 @Deprecated
46 public class AliasActivity extends Activity {
47     /**
48      * This is the name under which you should store in your component the
49      * meta-data information about the alias.  It is a reference to an XML
50      * resource describing an intent that launches the real application.
51      * {@hide}
52      */
53     public final String ALIAS_META_DATA = "android.app.alias";
54 
55     @Override
onCreate(Bundle savedInstanceState)56     protected void onCreate(Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58 
59         XmlResourceParser parser = null;
60         try {
61             ActivityInfo ai = getPackageManager().getActivityInfo(
62                     getComponentName(), PackageManager.GET_META_DATA);
63             parser = ai.loadXmlMetaData(getPackageManager(),
64                     ALIAS_META_DATA);
65             if (parser == null) {
66                 throw new RuntimeException("Alias requires a meta-data field "
67                         + ALIAS_META_DATA);
68             }
69 
70             Intent intent = parseAlias(parser);
71             if (intent == null) {
72                 throw new RuntimeException(
73                         "No <intent> tag found in alias description");
74             }
75 
76             startActivity(intent);
77             finish();
78 
79         } catch (PackageManager.NameNotFoundException | XmlPullParserException | IOException e) {
80             throw new RuntimeException("Error parsing alias", e);
81         } finally {
82             if (parser != null) parser.close();
83         }
84     }
85 
parseAlias(XmlPullParser parser)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 }
124