VoiceInteractionServiceInfo.java revision 18f0d357f9693fe787a3e3777d8fdf01357a6e3f
1/*
2 * Copyright (C) 2014 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 android.service.voice;
18
19import android.Manifest;
20import android.content.ComponentName;
21import android.content.pm.PackageManager;
22import android.content.pm.ServiceInfo;
23import android.content.res.Resources;
24import android.content.res.TypedArray;
25import android.content.res.XmlResourceParser;
26import android.speech.RecognitionService;
27import android.util.AttributeSet;
28import android.util.Log;
29import android.util.Xml;
30import org.xmlpull.v1.XmlPullParser;
31import org.xmlpull.v1.XmlPullParserException;
32
33import java.io.IOException;
34
35/** @hide */
36public class VoiceInteractionServiceInfo {
37    static final String TAG = "VoiceInteractionServiceInfo";
38
39    private String mParseError;
40
41    private ServiceInfo mServiceInfo;
42    private String mSessionService;
43    private String mSettingsActivity;
44
45    public VoiceInteractionServiceInfo(PackageManager pm, ComponentName comp)
46            throws PackageManager.NameNotFoundException {
47        this(pm, pm.getServiceInfo(comp, PackageManager.GET_META_DATA));
48    }
49
50    public VoiceInteractionServiceInfo(PackageManager pm, ServiceInfo si) {
51        if (!Manifest.permission.BIND_VOICE_INTERACTION.equals(si.permission)) {
52            mParseError = "Service does not require permission "
53                    + Manifest.permission.BIND_VOICE_INTERACTION;
54            return;
55        }
56
57        XmlResourceParser parser = null;
58        try {
59            parser = si.loadXmlMetaData(pm, VoiceInteractionService.SERVICE_META_DATA);
60            if (parser == null) {
61                mParseError = "No " + VoiceInteractionService.SERVICE_META_DATA
62                        + " meta-data for " + si.packageName;
63                return;
64            }
65
66            Resources res = pm.getResourcesForApplication(si.applicationInfo);
67
68            AttributeSet attrs = Xml.asAttributeSet(parser);
69
70            int type;
71            while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
72                    && type != XmlPullParser.START_TAG) {
73            }
74
75            String nodeName = parser.getName();
76            if (!"voice-interaction-service".equals(nodeName)) {
77                mParseError = "Meta-data does not start with voice-interaction-service tag";
78                return;
79            }
80
81            TypedArray array = res.obtainAttributes(attrs,
82                    com.android.internal.R.styleable.VoiceInteractionService);
83            mSessionService = array.getString(
84                    com.android.internal.R.styleable.VoiceInteractionService_sessionService);
85            mSettingsActivity = array.getString(
86                    com.android.internal.R.styleable.VoiceInteractionService_settingsActivity);
87            array.recycle();
88            if (mSessionService == null) {
89                mParseError = "No sessionService specified";
90                return;
91            }
92        } catch (XmlPullParserException e) {
93            mParseError = "Error parsing voice interation service meta-data: " + e;
94            Log.w(TAG, "error parsing voice interaction service meta-data", e);
95            return;
96        } catch (IOException e) {
97            mParseError = "Error parsing voice interation service meta-data: " + e;
98            Log.w(TAG, "error parsing voice interaction service meta-data", e);
99            return;
100        } catch (PackageManager.NameNotFoundException e) {
101            mParseError = "Error parsing voice interation service meta-data: " + e;
102            Log.w(TAG, "error parsing voice interaction service meta-data", e);
103            return;
104        } finally {
105            if (parser != null) parser.close();
106        }
107        mServiceInfo = si;
108    }
109
110    public String getParseError() {
111        return mParseError;
112    }
113
114    public ServiceInfo getServiceInfo() {
115        return mServiceInfo;
116    }
117
118    public String getSessionService() {
119        return mSessionService;
120    }
121
122    public String getSettingsActivity() {
123        return mSettingsActivity;
124    }
125}
126