1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.service.translation;
import android.Manifest;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.app.AppGlobals;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ServiceInfo;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.os.RemoteException;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Slog;
import android.util.Xml;
import com.android.internal.R;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.io.PrintWriter;
/**
* {@link ServiceInfo} and meta-data about an {@link TranslationService}.
*
* @hide
*/
public final class TranslationServiceInfo {
private static final String TAG = "TranslationServiceInfo";
private static final String XML_TAG_SERVICE = "translation-service";
@NonNull
private final ServiceInfo mServiceInfo;
@Nullable
private final String mSettingsActivity;
private static ServiceInfo getServiceInfoOrThrow(ComponentName comp, boolean isTemp,
@UserIdInt int userId) throws PackageManager.NameNotFoundException {
int flags = PackageManager.GET_META_DATA;
if (!isTemp) {
flags |= PackageManager.MATCH_SYSTEM_ONLY;
}
ServiceInfo si = null;
try {
si = AppGlobals.getPackageManager().getServiceInfo(comp, flags, userId);
} catch (RemoteException e) {
}
if (si == null) {
throw new NameNotFoundException("Could not get serviceInfo for "
+ (isTemp ? " (temp)" : "(default system)")
+ " " + comp.flattenToShortString());
}
return si;
}
@NonNull
public ServiceInfo getServiceInfo() {
return mServiceInfo;
}
@Nullable
public String getSettingsActivity() {
return mSettingsActivity;
}
public TranslationServiceInfo(@NonNull Context context, @NonNull ComponentName comp,
boolean isTemporaryService, @UserIdInt int userId)
throws PackageManager.NameNotFoundException {
this(context, getServiceInfoOrThrow(comp, isTemporaryService, userId));
}
private TranslationServiceInfo(@NonNull Context context, @NonNull ServiceInfo si) {
// Check for permission.
if (!Manifest.permission.BIND_TRANSLATION_SERVICE.equals(si.permission)) {
Slog.w(TAG, "TranslationServiceInfo from '" + si.packageName
+ "' does not require permission "
+ Manifest.permission.BIND_TRANSLATION_SERVICE);
throw new SecurityException("Service does not require permission "
+ Manifest.permission.BIND_TRANSLATION_SERVICE);
}
mServiceInfo = si;
// Get the metadata, if declared.
// TODO: Try to find more easier way to do this.
final XmlResourceParser parser = si.loadXmlMetaData(context.getPackageManager(),
TranslationService.SERVICE_META_DATA);
if (parser == null) {
mSettingsActivity = null;
return;
}
String settingsActivity = null;
try {
final Resources resources = context.getPackageManager().getResourcesForApplication(
si.applicationInfo);
int type = 0;
while (type != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
type = parser.next();
}
if (XML_TAG_SERVICE.equals(parser.getName())) {
final AttributeSet allAttributes = Xml.asAttributeSet(parser);
TypedArray afsAttributes = null;
try {
afsAttributes = resources.obtainAttributes(allAttributes,
com.android.internal.R.styleable.TranslationService);
settingsActivity = afsAttributes.getString(
R.styleable.ContentCaptureService_settingsActivity);
} finally {
if (afsAttributes != null) {
afsAttributes.recycle();
}
}
} else {
Log.e(TAG, "Meta-data does not start with translation-service tag");
}
} catch (PackageManager.NameNotFoundException | IOException | XmlPullParserException e) {
Log.e(TAG, "Error parsing auto fill service meta-data", e);
}
mSettingsActivity = settingsActivity;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append(getClass().getSimpleName());
builder.append("[").append(mServiceInfo);
builder.append(", settings:").append(mSettingsActivity);
return builder.toString();
}
/**
* Dumps the service information.
*/
public void dump(@NonNull String prefix, @NonNull PrintWriter pw) {
pw.print(prefix);
pw.print("Component: ");
pw.println(getServiceInfo().getComponentName());
pw.print(prefix);
pw.print("Settings: ");
pw.println(mSettingsActivity);
}
}
|