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