The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame^] | 1 | /** |
| 2 | * Copyright (C) 2007 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy |
| 6 | * 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, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations |
| 14 | * under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.settings; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.Intent; |
| 21 | import android.content.pm.ApplicationInfo; |
| 22 | import android.content.pm.PackageManager; |
| 23 | import android.content.pm.ResolveInfo; |
| 24 | import android.preference.Preference; |
| 25 | import android.preference.PreferenceGroup; |
| 26 | |
| 27 | import java.util.List; |
| 28 | |
| 29 | public class Utils { |
| 30 | |
| 31 | /** |
| 32 | * Set the preference's title to the matching activity's label. |
| 33 | */ |
| 34 | public static final int UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY = 1; |
| 35 | |
| 36 | /** |
| 37 | * Finds a matching activity for a preference's intent. If a matching |
| 38 | * activity is not found, it will remove the preference. |
| 39 | * |
| 40 | * @param context The context. |
| 41 | * @param parentPreferenceGroup The preference group that contains the |
| 42 | * preference whose intent is being resolved. |
| 43 | * @param preferenceKey The key of the preference whose intent is being |
| 44 | * resolved. |
| 45 | * @param flags 0 or one or more of |
| 46 | * {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY} |
| 47 | * . |
| 48 | * @return Whether an activity was found. If false, the preference was |
| 49 | * removed. |
| 50 | */ |
| 51 | public static boolean updatePreferenceToSpecificActivityOrRemove(Context context, |
| 52 | PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) { |
| 53 | |
| 54 | Preference preference = parentPreferenceGroup.findPreference(preferenceKey); |
| 55 | if (preference == null) { |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | Intent intent = preference.getIntent(); |
| 60 | if (intent != null) { |
| 61 | // Find the activity that is in the system image |
| 62 | PackageManager pm = context.getPackageManager(); |
| 63 | List<ResolveInfo> list = pm.queryIntentActivities(intent, 0); |
| 64 | int listSize = list.size(); |
| 65 | for (int i = 0; i < listSize; i++) { |
| 66 | ResolveInfo resolveInfo = list.get(i); |
| 67 | if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 68 | != 0) { |
| 69 | |
| 70 | // Replace the intent with this specific activity |
| 71 | preference.setIntent(new Intent().setClassName( |
| 72 | resolveInfo.activityInfo.packageName, |
| 73 | resolveInfo.activityInfo.name)); |
| 74 | |
| 75 | if ((flags & UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY) != 0) { |
| 76 | // Set the preference title to the activity's label |
| 77 | preference.setTitle(resolveInfo.loadLabel(pm)); |
| 78 | } |
| 79 | |
| 80 | return true; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // Did not find a matching activity, so remove the preference |
| 86 | parentPreferenceGroup.removePreference(preference); |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | } |