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;
29 
30 import com.android.internal.util.XmlUtils;
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 e) {
80             throw new RuntimeException("Error parsing alias", e);
81         } catch (XmlPullParserException e) {
82             throw new RuntimeException("Error parsing alias", e);
83         } catch (IOException e) {
84             throw new RuntimeException("Error parsing alias", e);
85         } finally {
86             if (parser != null) parser.close();
87         }
88     }
89 
parseAlias(XmlPullParser parser)90     private Intent parseAlias(XmlPullParser parser)
91             throws XmlPullParserException, IOException {
92         AttributeSet attrs = Xml.asAttributeSet(parser);
93 
94         Intent intent = null;
95 
96         int type;
97         while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
98                 && type != XmlPullParser.START_TAG) {
99         }
100 
101         String nodeName = parser.getName();
102         if (!"alias".equals(nodeName)) {
103             throw new RuntimeException(
104                     "Alias meta-data must start with <alias> tag; found"
105                     + nodeName + " at " + parser.getPositionDescription());
106         }
107 
108         int outerDepth = parser.getDepth();
109         while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
110                && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
111             if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
112                 continue;
113             }
114 
115             nodeName = parser.getName();
116             if ("intent".equals(nodeName)) {
117                 Intent gotIntent = Intent.parseIntent(getResources(), parser, attrs);
118                 if (intent == null) intent = gotIntent;
119             } else {
120                 XmlUtils.skipCurrentTag(parser);
121             }
122         }
123 
124         return intent;
125     }
126 
127 }
128