|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +package io.flutter.embedding.engine.loader; |
| 6 | + |
| 7 | +import android.content.Context; |
| 8 | +import android.content.pm.ApplicationInfo; |
| 9 | +import android.content.pm.PackageManager; |
| 10 | +import android.content.res.XmlResourceParser; |
| 11 | +import android.os.Bundle; |
| 12 | +import android.security.NetworkSecurityPolicy; |
| 13 | +import androidx.annotation.NonNull; |
| 14 | +import java.io.IOException; |
| 15 | +import org.json.JSONArray; |
| 16 | +import org.xmlpull.v1.XmlPullParserException; |
| 17 | + |
| 18 | +/** Loads application information given a Context. */ |
| 19 | +final class ApplicationInfoLoader { |
| 20 | + // XML Attribute keys supported in AndroidManifest.xml |
| 21 | + static final String PUBLIC_AOT_SHARED_LIBRARY_NAME = |
| 22 | + FlutterLoader.class.getName() + '.' + FlutterLoader.AOT_SHARED_LIBRARY_NAME; |
| 23 | + static final String PUBLIC_VM_SNAPSHOT_DATA_KEY = |
| 24 | + FlutterLoader.class.getName() + '.' + FlutterLoader.VM_SNAPSHOT_DATA_KEY; |
| 25 | + static final String PUBLIC_ISOLATE_SNAPSHOT_DATA_KEY = |
| 26 | + FlutterLoader.class.getName() + '.' + FlutterLoader.ISOLATE_SNAPSHOT_DATA_KEY; |
| 27 | + static final String PUBLIC_FLUTTER_ASSETS_DIR_KEY = |
| 28 | + FlutterLoader.class.getName() + '.' + FlutterLoader.FLUTTER_ASSETS_DIR_KEY; |
| 29 | + static final String NETWORK_POLICY_METADATA_KEY = "io.flutter.network-policy"; |
| 30 | + |
| 31 | + @NonNull |
| 32 | + private static ApplicationInfo getApplicationInfo(@NonNull Context applicationContext) { |
| 33 | + try { |
| 34 | + return applicationContext |
| 35 | + .getPackageManager() |
| 36 | + .getApplicationInfo(applicationContext.getPackageName(), PackageManager.GET_META_DATA); |
| 37 | + } catch (PackageManager.NameNotFoundException e) { |
| 38 | + throw new RuntimeException(e); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private static String getString(Bundle metadata, String key) { |
| 43 | + if (metadata == null) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + return metadata.getString(key, null); |
| 47 | + } |
| 48 | + |
| 49 | + private static String getNetworkPolicy(ApplicationInfo appInfo, Context context) { |
| 50 | + // We cannot use reflection to look at networkSecurityConfigRes because |
| 51 | + // Android throws an error when we try to access fields marked as @hide. |
| 52 | + // Instead we rely on metadata. |
| 53 | + Bundle metadata = appInfo.metaData; |
| 54 | + if (metadata == null) { |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + int networkSecurityConfigRes = metadata.getInt(NETWORK_POLICY_METADATA_KEY, 0); |
| 59 | + if (networkSecurityConfigRes <= 0) { |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + JSONArray output = new JSONArray(); |
| 64 | + try { |
| 65 | + XmlResourceParser xrp = context.getResources().getXml(networkSecurityConfigRes); |
| 66 | + xrp.next(); |
| 67 | + int eventType = xrp.getEventType(); |
| 68 | + while (eventType != XmlResourceParser.END_DOCUMENT) { |
| 69 | + if (eventType == XmlResourceParser.START_TAG) { |
| 70 | + if (xrp.getName().equals("domain-config")) { |
| 71 | + parseDomainConfig(xrp, output, false); |
| 72 | + } |
| 73 | + } |
| 74 | + eventType = xrp.next(); |
| 75 | + } |
| 76 | + } catch (IOException | XmlPullParserException e) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + return output.toString(); |
| 80 | + } |
| 81 | + |
| 82 | + private static boolean getUseEmbeddedView(ApplicationInfo appInfo) { |
| 83 | + Bundle bundle = appInfo.metaData; |
| 84 | + return bundle != null && bundle.getBoolean("io.flutter.embedded_views_preview"); |
| 85 | + } |
| 86 | + |
| 87 | + private static void parseDomainConfig( |
| 88 | + XmlResourceParser xrp, JSONArray output, boolean inheritedCleartextPermitted) |
| 89 | + throws IOException, XmlPullParserException { |
| 90 | + boolean cleartextTrafficPermitted = |
| 91 | + xrp.getAttributeBooleanValue( |
| 92 | + null, "cleartextTrafficPermitted", inheritedCleartextPermitted); |
| 93 | + while (true) { |
| 94 | + int eventType = xrp.next(); |
| 95 | + if (eventType == XmlResourceParser.START_TAG) { |
| 96 | + if (xrp.getName().equals("domain")) { |
| 97 | + // There can be multiple domains. |
| 98 | + parseDomain(xrp, output, cleartextTrafficPermitted); |
| 99 | + } else if (xrp.getName().equals("domain-config")) { |
| 100 | + parseDomainConfig(xrp, output, cleartextTrafficPermitted); |
| 101 | + } else { |
| 102 | + skipTag(xrp); |
| 103 | + } |
| 104 | + } else if (eventType == XmlResourceParser.END_TAG) { |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private static void skipTag(XmlResourceParser xrp) throws IOException, XmlPullParserException { |
| 111 | + String name = xrp.getName(); |
| 112 | + int eventType = xrp.getEventType(); |
| 113 | + while (eventType != XmlResourceParser.END_TAG || xrp.getName() != name) { |
| 114 | + eventType = xrp.next(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private static void parseDomain( |
| 119 | + XmlResourceParser xrp, JSONArray output, boolean cleartextPermitted) |
| 120 | + throws IOException, XmlPullParserException { |
| 121 | + boolean includeSubDomains = xrp.getAttributeBooleanValue(null, "includeSubdomains", false); |
| 122 | + xrp.next(); |
| 123 | + if (xrp.getEventType() != XmlResourceParser.TEXT) { |
| 124 | + throw new IllegalStateException("Expected text"); |
| 125 | + } |
| 126 | + String domain = xrp.getText().trim(); |
| 127 | + JSONArray outputArray = new JSONArray(); |
| 128 | + outputArray.put(domain); |
| 129 | + outputArray.put(includeSubDomains); |
| 130 | + outputArray.put(cleartextPermitted); |
| 131 | + output.put(outputArray); |
| 132 | + xrp.next(); |
| 133 | + if (xrp.getEventType() != XmlResourceParser.END_TAG) { |
| 134 | + throw new IllegalStateException("Expected end of domain tag"); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Initialize our Flutter config values by obtaining them from the manifest XML file, falling back |
| 140 | + * to default values. |
| 141 | + */ |
| 142 | + @NonNull |
| 143 | + public static FlutterApplicationInfo load(@NonNull Context applicationContext) { |
| 144 | + ApplicationInfo appInfo = getApplicationInfo(applicationContext); |
| 145 | + // Prior to API 23, cleartext traffic is allowed. |
| 146 | + boolean clearTextPermitted = true; |
| 147 | + if (android.os.Build.VERSION.SDK_INT >= 23) { |
| 148 | + clearTextPermitted = NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted(); |
| 149 | + } |
| 150 | + |
| 151 | + return new FlutterApplicationInfo( |
| 152 | + getString(appInfo.metaData, PUBLIC_AOT_SHARED_LIBRARY_NAME), |
| 153 | + getString(appInfo.metaData, PUBLIC_VM_SNAPSHOT_DATA_KEY), |
| 154 | + getString(appInfo.metaData, PUBLIC_ISOLATE_SNAPSHOT_DATA_KEY), |
| 155 | + getString(appInfo.metaData, PUBLIC_FLUTTER_ASSETS_DIR_KEY), |
| 156 | + getNetworkPolicy(appInfo, applicationContext), |
| 157 | + appInfo.nativeLibraryDir, |
| 158 | + clearTextPermitted, |
| 159 | + getUseEmbeddedView(appInfo)); |
| 160 | + } |
| 161 | +} |
0 commit comments