VoiceInteractionServiceInfo.java revision fee756ff91ab4d8f0e09ddb050d22d88ebb66ae7
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 mRecognitionService;
44    private String mSettingsActivity;
45
46    public VoiceInteractionServiceInfo(PackageManager pm, ComponentName comp)
47            throws PackageManager.NameNotFoundException {
48        this(pm, pm.getServiceInfo(comp, PackageManager.GET_META_DATA));
49    }
50
51    public VoiceInteractionServiceInfo(PackageManager pm, ServiceInfo si) {
52        if (!Manifest.permission.BIND_VOICE_INTERACTION.equals(si.permission)) {
53            mParseError = "Service does not require permission "
54                    + Manifest.permission.BIND_VOICE_INTERACTION;
55            return;
56        }
57
58        XmlResourceParser parser = null;
59        try {
60            parser = si.loadXmlMetaData(pm, VoiceInteractionService.SERVICE_META_DATA);
61            if (parser == null) {
62                mParseError = "No " + VoiceInteractionService.SERVICE_META_DATA
63                        + " meta-data for " + si.packageName;
64                return;
65            }
66
67            Resources res = pm.getResourcesForApplication(si.applicationInfo);
68
69            AttributeSet attrs = Xml.asAttributeSet(parser);
70
71            int type;
72            while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
73                    && type != XmlPullParser.START_TAG) {
74            }
75
76            String nodeName = parser.getName();
77            if (!"voice-interaction-service".equals(nodeName)) {
78                mParseError = "Meta-data does not start with voice-interaction-service tag";
79                return;
80            }
81
82            TypedArray array = res.obtainAttributes(attrs,
83                    com.android.internal.R.styleable.VoiceInteractionService);
84            mSessionService = array.getString(
85                    com.android.internal.R.styleable.VoiceInteractionService_sessionService);
86            mRecognitionService = array.getString(
87                    com.android.internal.R.styleable.VoiceInteractionService_recognitionService);
88            mSettingsActivity = array.getString(
89                    com.android.internal.R.styleable.VoiceInteractionService_settingsActivity);
90            array.recycle();
91            if (mSessionService == null) {
92                mParseError = "No sessionService specified";
93                return;
94            }
95        } catch (XmlPullParserException e) {
96            mParseError = "Error parsing voice interation service meta-data: " + e;
97            Log.w(TAG, "error parsing voice interaction service meta-data", e);
98            return;
99        } catch (IOException e) {
100            mParseError = "Error parsing voice interation service meta-data: " + e;
101            Log.w(TAG, "error parsing voice interaction service meta-data", e);
102            return;
103        } catch (PackageManager.NameNotFoundException e) {
104            mParseError = "Error parsing voice interation service meta-data: " + e;
105            Log.w(TAG, "error parsing voice interaction service meta-data", e);
106            return;
107        } finally {
108            if (parser != null) parser.close();
109        }
110        mServiceInfo = si;
111    }
112
113    public String getParseError() {
114        return mParseError;
115    }
116
117    public ServiceInfo getServiceInfo() {
118        return mServiceInfo;
119    }
120
121    public String getSessionService() {
122        return mSessionService;
123    }
124
125    public String getRecognitionService() {
126        return mRecognitionService;
127    }
128
129    public String getSettingsActivity() {
130        return mSettingsActivity;
131    }
132}
133