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