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