VoiceInteractionServiceInfo.java revision ecc7498b2bcf65adf20f5544a2994c68b75f8cea
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            /* Not yet time
103            if (mRecognitionService == null) {
104                mParseError = "No recogitionService specified";
105                return;
106            } */
107        } catch (XmlPullParserException e) {
108            mParseError = "Error parsing voice interation service meta-data: " + e;
109            Log.w(TAG, "error parsing voice interaction service meta-data", e);
110            return;
111        } catch (IOException e) {
112            mParseError = "Error parsing voice interation service meta-data: " + e;
113            Log.w(TAG, "error parsing voice interaction service meta-data", e);
114            return;
115        } catch (PackageManager.NameNotFoundException e) {
116            mParseError = "Error parsing voice interation service meta-data: " + e;
117            Log.w(TAG, "error parsing voice interaction service meta-data", e);
118            return;
119        } finally {
120            if (parser != null) parser.close();
121        }
122        mServiceInfo = si;
123    }
124
125    public String getParseError() {
126        return mParseError;
127    }
128
129    public ServiceInfo getServiceInfo() {
130        return mServiceInfo;
131    }
132
133    public String getSessionService() {
134        return mSessionService;
135    }
136
137    public String getRecognitionService() {
138        return mRecognitionService;
139    }
140
141    public String getSettingsActivity() {
142        return mSettingsActivity;
143    }
144}
145