1 /* 2 * Copyright (C) 2017 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; 18 19 import android.content.res.AssetManager; 20 import android.content.res.Resources; 21 import android.os.SystemProperties; 22 import android.util.ArrayMap; 23 import android.util.Log; 24 25 import java.io.BufferedReader; 26 import java.io.IOException; 27 import java.io.InputStreamReader; 28 29 /** 30 * Class that provides fallback values for {@link ApplicationInfo#category}. 31 * 32 * @hide 33 */ 34 public class FallbackCategoryProvider { 35 private static final String TAG = "FallbackCategoryProvider"; 36 37 private static final ArrayMap<String, Integer> sFallbacks = new ArrayMap<>(); 38 loadFallbacks()39 public static void loadFallbacks() { 40 sFallbacks.clear(); 41 if (SystemProperties.getBoolean("fw.ignore_fb_categories", false)) { 42 Log.d(TAG, "Ignoring fallback categories"); 43 return; 44 } 45 46 final AssetManager assets = new AssetManager(); 47 assets.addAssetPath("/system/framework/framework-res.apk"); 48 final Resources res = new Resources(assets, null, null); 49 50 try (BufferedReader reader = new BufferedReader(new InputStreamReader( 51 res.openRawResource(com.android.internal.R.raw.fallback_categories)))) { 52 String line; 53 while ((line = reader.readLine()) != null) { 54 if (line.charAt(0) == '#') continue; 55 final String[] split = line.split(","); 56 if (split.length == 2) { 57 sFallbacks.put(split[0], Integer.parseInt(split[1])); 58 } 59 } 60 Log.d(TAG, "Found " + sFallbacks.size() + " fallback categories"); 61 } catch (IOException | NumberFormatException e) { 62 Log.w(TAG, "Failed to read fallback categories", e); 63 } 64 } 65 getFallbackCategory(String packageName)66 public static int getFallbackCategory(String packageName) { 67 return sFallbacks.getOrDefault(packageName, ApplicationInfo.CATEGORY_UNDEFINED); 68 } 69 } 70