blob: d4f1f1155c4b015acf9d6201f74415452c581e88 [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001/**
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
17package com.android.settings;
18
19import android.content.Context;
20import android.content.Intent;
21import android.content.pm.ApplicationInfo;
22import android.content.pm.PackageManager;
23import android.content.pm.ResolveInfo;
Ying Wanga7188322010-01-04 18:45:10 -080024import android.os.SystemProperties;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080025import android.preference.Preference;
26import android.preference.PreferenceGroup;
27
28import java.util.List;
29
30public class Utils {
31
32 /**
33 * Set the preference's title to the matching activity's label.
34 */
35 public static final int UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY = 1;
36
37 /**
38 * Finds a matching activity for a preference's intent. If a matching
39 * activity is not found, it will remove the preference.
Ying Wanga7188322010-01-04 18:45:10 -080040 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080041 * @param context The context.
42 * @param parentPreferenceGroup The preference group that contains the
43 * preference whose intent is being resolved.
44 * @param preferenceKey The key of the preference whose intent is being
45 * resolved.
46 * @param flags 0 or one or more of
47 * {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY}
48 * .
49 * @return Whether an activity was found. If false, the preference was
50 * removed.
51 */
52 public static boolean updatePreferenceToSpecificActivityOrRemove(Context context,
53 PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) {
Ying Wanga7188322010-01-04 18:45:10 -080054
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080055 Preference preference = parentPreferenceGroup.findPreference(preferenceKey);
56 if (preference == null) {
57 return false;
58 }
Ying Wanga7188322010-01-04 18:45:10 -080059
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080060 Intent intent = preference.getIntent();
61 if (intent != null) {
62 // Find the activity that is in the system image
63 PackageManager pm = context.getPackageManager();
64 List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
65 int listSize = list.size();
66 for (int i = 0; i < listSize; i++) {
67 ResolveInfo resolveInfo = list.get(i);
68 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
69 != 0) {
Ying Wanga7188322010-01-04 18:45:10 -080070
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080071 // Replace the intent with this specific activity
72 preference.setIntent(new Intent().setClassName(
73 resolveInfo.activityInfo.packageName,
74 resolveInfo.activityInfo.name));
75
76 if ((flags & UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY) != 0) {
77 // Set the preference title to the activity's label
78 preference.setTitle(resolveInfo.loadLabel(pm));
79 }
Ying Wanga7188322010-01-04 18:45:10 -080080
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080081 return true;
82 }
83 }
84 }
85
86 // Did not find a matching activity, so remove the preference
87 parentPreferenceGroup.removePreference(preference);
Ying Wanga7188322010-01-04 18:45:10 -080088
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080089 return true;
90 }
Ying Wanga7188322010-01-04 18:45:10 -080091
92 /**
93 * Returns true if Monkey is running.
94 */
95 public static boolean isMonkeyRunning() {
96 return SystemProperties.getBoolean("ro.monkey", false);
97 }
98
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080099}