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