Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0078e1b7 authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Add support for fallback categories.

When an app doesn't define a category, look for a fallback category
in a hard-coded list.  This change only defines a fallback for a
single package, but device-specific overlays can be used to provide
more detailed fallback lists.

The precidence order is: app manifest > installer hint > fallback

Test: builds, boots, fallback categories work
Bug: 33815939
Change-Id: I1f5ca76fb7e5743a4500c0a1230a754266f34d9e
parent 9bc89af3
Loading
Loading
Loading
Loading
+69 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.content.pm;

import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.SystemProperties;
import android.util.ArrayMap;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Class that provides fallback values for {@link ApplicationInfo#category}.
 *
 * @hide
 */
public class FallbackCategoryProvider {
    private static final String TAG = "FallbackCategoryProvider";

    private static final ArrayMap<String, Integer> sFallbacks = new ArrayMap<>();

    public static void loadFallbacks() {
        sFallbacks.clear();
        if (SystemProperties.getBoolean("fw.ignore_fb_categories", false)) {
            Log.d(TAG, "Ignoring fallback categories");
            return;
        }

        final AssetManager assets = new AssetManager();
        assets.addAssetPath("/system/framework/framework-res.apk");
        final Resources res = new Resources(assets, null, null);

        try (BufferedReader reader = new BufferedReader(new InputStreamReader(
                res.openRawResource(com.android.internal.R.raw.fallback_categories)))) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.charAt(0) == '#') continue;
                final String[] split = line.split(",");
                if (split.length == 2) {
                    sFallbacks.put(split[0], Integer.parseInt(split[1]));
                }
            }
            Log.d(TAG, "Found " + sFallbacks.size() + " fallback categories");
        } catch (IOException | NumberFormatException e) {
            Log.w(TAG, "Failed to read fallback categories", e);
        }
    }

    public static int getFallbackCategory(String packageName) {
        return sFallbacks.getOrDefault(packageName, ApplicationInfo.CATEGORY_UNDEFINED);
    }
}
+3 −0
Original line number Diff line number Diff line
@@ -6260,6 +6260,9 @@ public class PackageParser {
        if (ai.category == ApplicationInfo.CATEGORY_UNDEFINED) {
            ai.category = state.categoryHint;
        }
        if (ai.category == ApplicationInfo.CATEGORY_UNDEFINED) {
            ai.category = FallbackCategoryProvider.getFallbackCategory(ai.packageName);
        }
    }

    public static ApplicationInfo generateApplicationInfo(Package p, int flags,
+3 −0
Original line number Diff line number Diff line
# Default app categories used if neither the package nor installer sets anything
# First column is package name, second column is raw ApplicationInfo.CATEGORY value
com.android.printspooler,7
+2 −0
Original line number Diff line number Diff line
@@ -2793,4 +2793,6 @@
  <java-symbol type="string" name="app_category_maps" />
  <java-symbol type="string" name="app_category_productivity" />

  <java-symbol type="raw" name="fallback_categories" />

</resources>
+5 −0
Original line number Diff line number Diff line
@@ -127,6 +127,7 @@ import android.content.pm.EphemeralApplicationInfo;
import android.content.pm.EphemeralRequest;
import android.content.pm.EphemeralResolveInfo;
import android.content.pm.EphemeralResponse;
import android.content.pm.FallbackCategoryProvider;
import android.content.pm.FeatureInfo;
import android.content.pm.IOnPermissionsChangeListener;
import android.content.pm.IPackageDataObserver;
@@ -2822,6 +2823,10 @@ public class PackageManagerService extends IPackageManager.Stub {
        Runtime.getRuntime().gc();
        Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
        Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "loadFallbacks");
        FallbackCategoryProvider.loadFallbacks();
        Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
        // The initial scanning above does many calls into installd while
        // holding the mPackages lock, but we're mostly interested in yelling
        // once we have a booted system.