PreferredActivity.java revision a3f133afe885f9e005dfc0584cb7b3b90f75f665
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
17package com.android.server.pm;
18
19import com.android.internal.util.XmlUtils;
20import com.android.server.PreferredComponent;
21
22import org.xmlpull.v1.XmlPullParser;
23import org.xmlpull.v1.XmlPullParserException;
24import org.xmlpull.v1.XmlSerializer;
25
26import android.content.ComponentName;
27import android.content.IntentFilter;
28import android.util.Log;
29
30import java.io.IOException;
31
32class PreferredActivity extends IntentFilter implements PreferredComponent.Callbacks {
33    private static final String TAG = "PreferredActivity";
34
35    private static final boolean DEBUG_FILTERS = false;
36    static final String ATTR_USER_ID = "userId";
37
38    final PreferredComponent mPref;
39    final int mUserId;
40
41    PreferredActivity(IntentFilter filter, int match, ComponentName[] set, ComponentName activity) {
42        this(filter, match, set, activity, 0);
43    }
44
45    PreferredActivity(IntentFilter filter, int match, ComponentName[] set, ComponentName activity,
46            int userId) {
47        super(filter);
48        mUserId = userId;
49        mPref = new PreferredComponent(this, match, set, activity);
50    }
51
52    PreferredActivity(XmlPullParser parser) throws XmlPullParserException, IOException {
53        String userIdString = parser.getAttributeValue(null, ATTR_USER_ID);
54        if (userIdString != null && userIdString.length() > 0) {
55            mUserId = Integer.parseInt(userIdString);
56        } else {
57            // Old format with no userId specified - assume primary user
58            mUserId = 0;
59        }
60        mPref = new PreferredComponent(this, parser);
61    }
62
63    public void writeToXml(XmlSerializer serializer) throws IOException {
64        serializer.attribute(null, ATTR_USER_ID, Integer.toString(mUserId));
65        mPref.writeToXml(serializer);
66        serializer.startTag(null, "filter");
67            super.writeToXml(serializer);
68        serializer.endTag(null, "filter");
69    }
70
71    public boolean onReadTag(String tagName, XmlPullParser parser) throws XmlPullParserException,
72            IOException {
73        if (tagName.equals("filter")) {
74            if (DEBUG_FILTERS) {
75                Log.i(TAG, "Starting to parse filter...");
76            }
77            readFromXml(parser);
78            if (DEBUG_FILTERS) {
79                Log.i(TAG, "Finished filter: depth=" + parser.getDepth() + " tag="
80                        + parser.getName());
81            }
82        } else {
83            PackageManagerService.reportSettingsProblem(Log.WARN,
84                    "Unknown element under <preferred-activities>: " + parser.getName());
85            XmlUtils.skipCurrentTag(parser);
86        }
87        return true;
88    }
89}
90