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