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 17 package android.service.voice; 18 19 import android.Manifest; 20 import android.app.AppGlobals; 21 import android.content.ComponentName; 22 import android.content.pm.PackageManager; 23 import android.content.pm.ServiceInfo; 24 import android.content.res.Resources; 25 import android.content.res.TypedArray; 26 import android.content.res.XmlResourceParser; 27 import android.os.RemoteException; 28 import android.util.AttributeSet; 29 import android.util.Log; 30 import android.util.Xml; 31 import org.xmlpull.v1.XmlPullParser; 32 import org.xmlpull.v1.XmlPullParserException; 33 34 import java.io.IOException; 35 36 /** @hide */ 37 public 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