Add UI for mainline modules licenses.

Added a module licenses option that lives in Legal information settings.
Clicking that option opens module licenses page, which displays every
module by name, filtered to exclude modules without license files.
Clicking a module in the list opens HTMLViewer.

Created ModuleLicensesProvider, a new ContentProvider that serves as a
redirect for the Uris sent to HTMLViewer so that they open asset files.
In order to provide the redirect, the provider will write the license file
to a file in Settings' cache directory when the license does not exist
in the cache or is outdated. The provider then opens that cached file.

Fixes: 135183006
Test: robotests
Change-Id: I7d69da34780c8c4efb150d0c0411078c12bc80d8
diff --git a/src/com/android/settings/Settings.java b/src/com/android/settings/Settings.java
index b4ebc57..ab59da4 100644
--- a/src/com/android/settings/Settings.java
+++ b/src/com/android/settings/Settings.java
@@ -56,6 +56,7 @@
     public static class NightDisplaySettingsActivity extends SettingsActivity { /* empty */ }
     public static class NightDisplaySuggestionActivity extends NightDisplaySettingsActivity { /* empty */ }
     public static class MyDeviceInfoActivity extends SettingsActivity { /* empty */ }
+    public static class ModuleLicensesActivity extends SettingsActivity { /* empty */ }
     public static class ApplicationSettingsActivity extends SettingsActivity { /* empty */ }
     public static class ManageApplicationsActivity extends SettingsActivity { /* empty */ }
     public static class ManageAssistActivity extends SettingsActivity { /* empty */ }
diff --git a/src/com/android/settings/core/gateway/SettingsGateway.java b/src/com/android/settings/core/gateway/SettingsGateway.java
index b35a974..5a81e71 100644
--- a/src/com/android/settings/core/gateway/SettingsGateway.java
+++ b/src/com/android/settings/core/gateway/SettingsGateway.java
@@ -72,6 +72,7 @@
 import com.android.settings.deviceinfo.StorageSettings;
 import com.android.settings.deviceinfo.aboutphone.MyDeviceInfoFragment;
 import com.android.settings.deviceinfo.firmwareversion.FirmwareVersionSettings;
+import com.android.settings.deviceinfo.legal.ModuleLicensesDashboard;
 import com.android.settings.display.NightDisplaySettings;
 import com.android.settings.dream.DreamSettings;
 import com.android.settings.enterprise.EnterprisePrivacySettings;
@@ -175,6 +176,7 @@
             UserDictionarySettings.class.getName(),
             DisplaySettings.class.getName(),
             MyDeviceInfoFragment.class.getName(),
+            ModuleLicensesDashboard.class.getName(),
             ManageApplications.class.getName(),
             FirmwareVersionSettings.class.getName(),
             ManageAssist.class.getName(),
@@ -318,6 +320,7 @@
             Settings.DateTimeSettingsActivity.class.getName(),
             Settings.EnterprisePrivacySettingsActivity.class.getName(),
             Settings.MyDeviceInfoActivity.class.getName(),
+            Settings.ModuleLicensesActivity.class.getName(),
             UserBackupSettingsActivity.class.getName(),
     };
 }
diff --git a/src/com/android/settings/deviceinfo/legal/ModuleLicensePreference.java b/src/com/android/settings/deviceinfo/legal/ModuleLicensePreference.java
new file mode 100644
index 0000000..e012275
--- /dev/null
+++ b/src/com/android/settings/deviceinfo/legal/ModuleLicensePreference.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2019 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 com.android.settings.deviceinfo.legal;
+
+import android.content.ActivityNotFoundException;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.ModuleInfo;
+import android.util.Log;
+import android.widget.Toast;
+
+import androidx.preference.Preference;
+
+import com.android.settings.R;
+
+/**
+ * Preference in a list that represents a mainline module that has a licenses file.
+ */
+public class ModuleLicensePreference extends Preference {
+    private static final String TAG = "ModuleLicensePreference";
+    private final ModuleInfo mModule;
+
+    public ModuleLicensePreference(Context context, ModuleInfo module) {
+        super(context);
+        mModule = module;
+        setKey(module.getPackageName());
+        setTitle(module.getName());
+    }
+
+    @Override
+    protected void onClick() {
+        // Kick off external viewer due to WebView security restrictions (Settings cannot use
+        // WebView because it is UID 1000).
+        Intent intent = new Intent(Intent.ACTION_VIEW)
+                .setDataAndType(
+                        ModuleLicenseProvider.getUriForPackage(mModule.getPackageName()),
+                        ModuleLicenseProvider.LICENSE_FILE_MIME_TYPE)
+                .putExtra(Intent.EXTRA_TITLE, mModule.getName())
+                .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
+                .addCategory(Intent.CATEGORY_DEFAULT)
+                .setPackage("com.android.htmlviewer");
+        try {
+            getContext().startActivity(intent);
+        } catch (ActivityNotFoundException e) {
+            Log.e(TAG, "Failed to find viewer", e);
+            showError();
+        }
+    }
+
+    private void showError() {
+        Toast.makeText(
+                getContext(), R.string.settings_license_activity_unavailable, Toast.LENGTH_LONG)
+                .show();
+    }
+}
diff --git a/src/com/android/settings/deviceinfo/legal/ModuleLicenseProvider.java b/src/com/android/settings/deviceinfo/legal/ModuleLicenseProvider.java
new file mode 100644
index 0000000..6731c69
--- /dev/null
+++ b/src/com/android/settings/deviceinfo/legal/ModuleLicenseProvider.java
@@ -0,0 +1,194 @@
+/*
+ * Copyright (C) 2019 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 com.android.settings.deviceinfo.legal;
+
+import android.content.ContentProvider;
+import android.content.ContentResolver;
+import android.content.ContentValues;
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.content.res.AssetManager;
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.ParcelFileDescriptor;
+import android.util.Log;
+
+import androidx.annotation.VisibleForTesting;
+import androidx.core.util.Preconditions;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
+import java.util.List;
+import java.util.zip.GZIPInputStream;
+
+public class ModuleLicenseProvider extends ContentProvider {
+    private static final String TAG = "ModuleLicenseProvider";
+
+    public static final String AUTHORITY = "com.android.settings.module_licenses";
+    static final String GZIPPED_LICENSE_FILE_NAME = "NOTICE.html.gz";
+    static final String LICENSE_FILE_NAME = "NOTICE.html";
+    static final String LICENSE_FILE_MIME_TYPE = "text/html";
+    static final String PREFS_NAME = "ModuleLicenseProvider";
+
+    @Override
+    public boolean onCreate() {
+        return true;
+    }
+
+    @Override
+    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
+            String sortOrder) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public String getType(Uri uri) {
+        checkUri(getContext(), uri);
+        return LICENSE_FILE_MIME_TYPE;
+    }
+
+    @Override
+    public Uri insert(Uri uri, ContentValues values) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public int delete(Uri uri, String selection, String[] selectionArgs) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public ParcelFileDescriptor openFile(Uri uri, String mode) {
+        final Context context = getContext();
+        checkUri(context, uri);
+        Preconditions.checkArgument("r".equals(mode), "Read is the only supported mode");
+
+        try {
+            String packageName = uri.getPathSegments().get(0);
+            File cachedFile = getCachedHtmlFile(context, packageName);
+            if (isCachedHtmlFileOutdated(context, packageName)) {
+                try (InputStream in = new GZIPInputStream(
+                        getPackageAssetManager(context.getPackageManager(), packageName)
+                                .open(GZIPPED_LICENSE_FILE_NAME))) {
+                    File directory = getCachedFileDirectory(context, packageName);
+                    if (!directory.exists()) {
+                        directory.mkdir();
+                    }
+                    Files.copy(in, cachedFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
+                }
+                // Now that the file is saved, write the package's version code to shared prefs
+                SharedPreferences.Editor editor = getPrefs(context).edit();
+                editor.putLong(
+                        packageName,
+                        getPackageInfo(context, packageName).getLongVersionCode())
+                                .commit();
+            }
+            return ParcelFileDescriptor.open(cachedFile, ParcelFileDescriptor.MODE_READ_ONLY);
+        } catch (PackageManager.NameNotFoundException e) {
+            Log.wtf(TAG, "checkUri should have already caught this error", e);
+        } catch (IOException e) {
+            Log.e(TAG, "Could not open file descriptor", e);
+        }
+        return null;
+    }
+
+    /**
+     * Returns true if the cached file for the given package is outdated. A cached file is
+     * outdated if one of the following are true:
+     * 1. the shared prefs does not contain a version code for this package
+     * 2. The version code does not match the package's version code
+     * 3. There is no file or the file is empty.
+     */
+    @VisibleForTesting
+    static boolean isCachedHtmlFileOutdated(Context context, String packageName)
+            throws PackageManager.NameNotFoundException {
+        SharedPreferences prefs = getPrefs(context);
+        File file = getCachedHtmlFile(context, packageName);
+        return !prefs.contains(packageName)
+                || prefs.getLong(packageName, 0L)
+                        != getPackageInfo(context, packageName).getLongVersionCode()
+                || !file.exists() || file.length() == 0;
+    }
+
+    static AssetManager getPackageAssetManager(PackageManager packageManager, String packageName)
+            throws PackageManager.NameNotFoundException {
+        return packageManager.getResourcesForApplication(
+                packageManager.getPackageInfo(packageName, PackageManager.MATCH_APEX)
+                        .applicationInfo)
+                                .getAssets();
+    }
+
+    static Uri getUriForPackage(String packageName) {
+        return new Uri.Builder()
+                .scheme(ContentResolver.SCHEME_CONTENT)
+                .authority(AUTHORITY)
+                .appendPath(packageName)
+                .appendPath(LICENSE_FILE_NAME)
+                .build();
+    }
+
+    private static void checkUri(Context context, Uri uri) {
+        List<String> pathSegments = uri.getPathSegments();
+        // A URI is valid iff it:
+        // 1. is a content URI
+        // 2. uses the correct authority
+        // 3. has exactly 2 segments and the last one is NOTICE.html
+        // 4. (checked below) first path segment is the package name of a module
+        if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())
+                || !AUTHORITY.equals(uri.getAuthority())
+                || pathSegments == null
+                || pathSegments.size() != 2
+                || !LICENSE_FILE_NAME.equals(pathSegments.get(1))) {
+            throw new IllegalArgumentException(uri + "is not a valid URI");
+        }
+        // Grab the first path segment, which is the package name of the module and make sure that
+        // there's actually a module for that package. getModuleInfo will throw if it does not
+        // exist.
+        try {
+            context.getPackageManager().getModuleInfo(pathSegments.get(0), 0 /* flags */);
+        } catch (PackageManager.NameNotFoundException e) {
+            throw new IllegalArgumentException(uri + "is not a valid URI", e);
+        }
+    }
+
+    private static File getCachedFileDirectory(Context context, String packageName) {
+        return new File(context.getCacheDir(), packageName);
+    }
+
+    private static File getCachedHtmlFile(Context context, String packageName) {
+        return new File(context.getCacheDir() + "/" + packageName, LICENSE_FILE_NAME);
+    }
+
+    private static  PackageInfo getPackageInfo(Context context, String packageName)
+            throws PackageManager.NameNotFoundException {
+        return context.getPackageManager().getPackageInfo(packageName, PackageManager.MATCH_APEX);
+    }
+
+    private static SharedPreferences getPrefs(Context context) {
+        return context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
+    }
+}
diff --git a/src/com/android/settings/deviceinfo/legal/ModuleLicensesDashboard.java b/src/com/android/settings/deviceinfo/legal/ModuleLicensesDashboard.java
new file mode 100644
index 0000000..f74b68f
--- /dev/null
+++ b/src/com/android/settings/deviceinfo/legal/ModuleLicensesDashboard.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2019 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 com.android.settings.deviceinfo.legal;
+
+import android.app.settings.SettingsEnums;
+
+import com.android.settings.R;
+import com.android.settings.dashboard.DashboardFragment;
+
+public class ModuleLicensesDashboard extends DashboardFragment {
+    private static final String TAG = "ModuleLicensesDashboard";
+
+    @Override
+    public int getMetricsCategory() {
+        return SettingsEnums.MODULE_LICENSES_DASHBOARD;
+    }
+
+    @Override
+    protected String getLogTag() {
+        return TAG;
+    }
+
+    @Override
+    protected int getPreferenceScreenResId() {
+        return R.xml.module_licenses;
+    }
+
+    @Override
+    public int getHelpResource() {
+        return 0;
+    }
+}
diff --git a/src/com/android/settings/deviceinfo/legal/ModuleLicensesListPreferenceController.java b/src/com/android/settings/deviceinfo/legal/ModuleLicensesListPreferenceController.java
new file mode 100644
index 0000000..9faff85
--- /dev/null
+++ b/src/com/android/settings/deviceinfo/legal/ModuleLicensesListPreferenceController.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2019 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 com.android.settings.deviceinfo.legal;
+
+import android.content.Context;
+import android.content.pm.ModuleInfo;
+import android.content.pm.PackageManager;
+
+import com.android.settings.core.BasePreferenceController;
+
+import java.util.List;
+
+public class ModuleLicensesListPreferenceController extends BasePreferenceController {
+    public ModuleLicensesListPreferenceController(Context context,
+            String preferenceKey) {
+        super(context, preferenceKey);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        PackageManager packageManager = mContext.getPackageManager();
+        List<ModuleInfo> modules = packageManager.getInstalledModules(0 /* flags */);
+        return modules.stream().anyMatch(new ModuleLicensesPreferenceController.Predicate(mContext))
+                ? AVAILABLE
+                : CONDITIONALLY_UNAVAILABLE;
+    }
+}
diff --git a/src/com/android/settings/deviceinfo/legal/ModuleLicensesPreferenceController.java b/src/com/android/settings/deviceinfo/legal/ModuleLicensesPreferenceController.java
new file mode 100644
index 0000000..dd5edbb
--- /dev/null
+++ b/src/com/android/settings/deviceinfo/legal/ModuleLicensesPreferenceController.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2019 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 com.android.settings.deviceinfo.legal;
+
+import android.content.Context;
+import android.content.pm.ModuleInfo;
+import android.content.pm.PackageManager;
+
+import androidx.preference.PreferenceGroup;
+import androidx.preference.PreferenceScreen;
+
+import com.android.internal.util.ArrayUtils;
+import com.android.settings.core.BasePreferenceController;
+
+import java.io.IOException;
+import java.util.Comparator;
+import java.util.List;
+
+public class ModuleLicensesPreferenceController extends BasePreferenceController {
+    public ModuleLicensesPreferenceController(Context context, String preferenceKey) {
+        super(context, preferenceKey);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return AVAILABLE_UNSEARCHABLE;
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+
+        PackageManager packageManager = mContext.getPackageManager();
+        List<ModuleInfo> modules = packageManager.getInstalledModules(0 /* flags */);
+        PreferenceGroup group = screen.findPreference(getPreferenceKey());
+        modules.stream()
+                .sorted(Comparator.comparing(o -> o.getName().toString()))
+                .filter(new Predicate(mContext))
+                .forEach(module ->
+                        group.addPreference(
+                                new ModuleLicensePreference(group.getContext(), module)));
+    }
+
+    static class Predicate implements java.util.function.Predicate<ModuleInfo> {
+        private final Context mContext;
+
+        public Predicate(Context context) {
+            mContext = context;
+        }
+        @Override
+        public boolean test(ModuleInfo module) {
+            try {
+                return ArrayUtils.contains(
+                        ModuleLicenseProvider.getPackageAssetManager(
+                                mContext.getPackageManager(),
+                                module.getPackageName())
+                                        .list(""),
+                        ModuleLicenseProvider.GZIPPED_LICENSE_FILE_NAME);
+            } catch (IOException | PackageManager.NameNotFoundException e) {
+                return false;
+            }
+        }
+    }
+}