Winson | 022e707 | 2020-01-28 10:52:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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.content.pm.parsing.component; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.content.IntentFilter; |
| 22 | import android.content.pm.parsing.ParsingPackage; |
| 23 | import android.content.res.Configuration; |
| 24 | import android.content.res.Resources; |
| 25 | import android.content.res.TypedArray; |
| 26 | import android.content.res.XmlResourceParser; |
| 27 | import android.os.Build; |
| 28 | import android.util.Slog; |
| 29 | |
| 30 | import com.android.internal.annotations.VisibleForTesting; |
| 31 | import android.content.pm.parsing.ParsingPackageUtils; |
| 32 | import android.content.pm.parsing.result.ParseInput; |
| 33 | import android.content.pm.parsing.result.ParseResult; |
| 34 | |
| 35 | import org.xmlpull.v1.XmlPullParserException; |
| 36 | |
| 37 | import java.io.IOException; |
| 38 | |
| 39 | /** @hide */ |
| 40 | class ParsedMainComponentUtils { |
| 41 | |
| 42 | private static final String TAG = ParsingPackageUtils.TAG; |
| 43 | |
| 44 | @NonNull |
| 45 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 46 | static <Component extends ParsedMainComponent> ParseResult<Component> parseMainComponent( |
| 47 | Component component, String tag, String[] separateProcesses, ParsingPackage pkg, |
| 48 | TypedArray array, int flags, boolean useRoundIcon, ParseInput input, |
| 49 | int bannerAttr, int descriptionAttr, @Nullable Integer directBootAwareAttr, |
| 50 | @Nullable Integer enabledAttr, int iconAttr, int labelAttr, int logoAttr, int nameAttr, |
| 51 | @Nullable Integer processAttr, int roundIconAttr, @Nullable Integer splitNameAttr) { |
| 52 | ParseResult<Component> result = ParsedComponentUtils.parseComponent(component, tag, pkg, |
| 53 | array, useRoundIcon, input, bannerAttr, descriptionAttr, iconAttr, labelAttr, |
| 54 | logoAttr, nameAttr, roundIconAttr); |
| 55 | if (result.isError()) { |
| 56 | return result; |
| 57 | } |
| 58 | |
| 59 | if (directBootAwareAttr != null) { |
| 60 | component.directBootAware = array.getBoolean(directBootAwareAttr, false); |
| 61 | if (component.isDirectBootAware()) { |
| 62 | pkg.setPartiallyDirectBootAware(true); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if (enabledAttr != null) { |
| 67 | component.enabled = array.getBoolean(enabledAttr, true); |
| 68 | } |
| 69 | |
| 70 | if (processAttr != null) { |
| 71 | CharSequence processName; |
| 72 | if (pkg.getTargetSdkVersion() >= Build.VERSION_CODES.FROYO) { |
| 73 | processName = array.getNonConfigurationString(processAttr, |
| 74 | Configuration.NATIVE_CONFIG_VERSION); |
| 75 | } else { |
| 76 | // Some older apps have been seen to use a resource reference |
| 77 | // here that on older builds was ignored (with a warning). We |
| 78 | // need to continue to do this for them so they don't break. |
| 79 | processName = array.getNonResourceString(processAttr); |
| 80 | } |
| 81 | |
| 82 | // Backwards-compat, ignore error |
| 83 | ParseResult<String> processNameResult = ComponentParseUtils.buildProcessName( |
| 84 | pkg.getPackageName(), pkg.getProcessName(), processName, flags, |
| 85 | separateProcesses, input); |
| 86 | if (processNameResult.isSuccess()) { |
| 87 | component.setProcessName(processNameResult.getResult()); |
| 88 | } else { |
| 89 | // Backwards-compat, ignore error |
| 90 | processNameResult.ignoreError(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if (splitNameAttr != null) { |
| 95 | component.splitName = array.getNonConfigurationString(splitNameAttr, 0); |
| 96 | } |
| 97 | |
| 98 | return input.success(component); |
| 99 | } |
| 100 | |
| 101 | static ParseResult<ParsedIntentInfo> parseIntentFilter( |
| 102 | ParsedMainComponent mainComponent, |
| 103 | ParsingPackage pkg, Resources resources, XmlResourceParser parser, |
| 104 | boolean visibleToEphemeral, boolean allowGlobs, boolean allowAutoVerify, |
| 105 | boolean allowImplicitEphemeralVisibility, boolean failOnNoActions, |
| 106 | ParseInput input) throws IOException, XmlPullParserException { |
| 107 | ParseResult<ParsedIntentInfo> intentResult = ParsedIntentInfoUtils.parseIntentInfo( |
| 108 | mainComponent.getName(), pkg, resources, parser, allowGlobs, |
| 109 | allowAutoVerify, input); |
| 110 | if (intentResult.isError()) { |
| 111 | return input.error(intentResult); |
| 112 | } |
| 113 | |
| 114 | ParsedIntentInfo intent = intentResult.getResult(); |
| 115 | int actionCount = intent.countActions(); |
| 116 | if (actionCount == 0 && failOnNoActions) { |
| 117 | Slog.w(TAG, "No actions in " + parser.getName() + " at " + pkg.getBaseCodePath() + " " |
| 118 | + parser.getPositionDescription()); |
| 119 | // Backward-compat, do not actually fail |
| 120 | return input.success(null); |
| 121 | } |
| 122 | |
| 123 | int intentVisibility; |
| 124 | if (visibleToEphemeral) { |
| 125 | intentVisibility = IntentFilter.VISIBILITY_EXPLICIT; |
| 126 | } else if (allowImplicitEphemeralVisibility |
| 127 | && ComponentParseUtils.isImplicitlyExposedIntent(intent)){ |
| 128 | intentVisibility = IntentFilter.VISIBILITY_IMPLICIT; |
| 129 | } else { |
| 130 | intentVisibility = IntentFilter.VISIBILITY_NONE; |
| 131 | } |
| 132 | intent.setVisibilityToInstantApp(intentVisibility); |
| 133 | |
| 134 | return input.success(intentResult.getResult()); |
| 135 | } |
| 136 | |
| 137 | } |