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 com.android.settings.intelligence.suggestions.eligibility; 18 19 import android.content.Context; 20 import android.content.pm.ApplicationInfo; 21 import android.content.pm.PackageManager; 22 import android.content.pm.ResolveInfo; 23 import android.content.res.Resources; 24 import android.util.Log; 25 26 public class ProviderEligibilityChecker { 27 /** 28 * If defined and not true, do not should optional step. 29 */ 30 private static final String META_DATA_IS_SUPPORTED = "com.android.settings.is_supported"; 31 private static final String TAG = "ProviderEligibility"; 32 isEligible(Context context, String id, ResolveInfo info)33 public static boolean isEligible(Context context, String id, ResolveInfo info) { 34 return isSystemApp(id, info) 35 && isEnabledInMetadata(context, id, info); 36 } 37 isSystemApp(String id, ResolveInfo info)38 private static boolean isSystemApp(String id, ResolveInfo info) { 39 final boolean isSystemApp = info != null 40 && info.activityInfo != null 41 && info.activityInfo.applicationInfo != null 42 && (info.activityInfo.applicationInfo.flags 43 & ApplicationInfo.FLAG_SYSTEM) != 0; 44 if (!isSystemApp) { 45 Log.i(TAG, id + " is not system app, not eligible for suggestion"); 46 } 47 return isSystemApp; 48 } 49 isEnabledInMetadata(Context context, String id, ResolveInfo info)50 private static boolean isEnabledInMetadata(Context context, String id, ResolveInfo info) { 51 final int isSupportedResource = info.activityInfo.metaData.getInt(META_DATA_IS_SUPPORTED); 52 try { 53 final Resources res = context.getPackageManager() 54 .getResourcesForApplication(info.activityInfo.applicationInfo); 55 boolean isSupported = 56 isSupportedResource != 0 ? res.getBoolean(isSupportedResource) : true; 57 if (!isSupported) { 58 Log.i(TAG, id + " requires unsupported resource " + isSupportedResource); 59 } 60 return isSupported; 61 } catch (PackageManager.NameNotFoundException e) { 62 Log.w(TAG, "Cannot find resources for " + id, e); 63 return false; 64 } catch (Resources.NotFoundException e) { 65 Log.w(TAG, "Cannot find resources for " + id, e); 66 return false; 67 } 68 } 69 } 70