| 1 | /* \r |
| 2 | * Copyright (C) 2008 OpenIntents.org\r |
| 3 | *\r |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License");\r |
| 5 | * you may not use this file except in compliance with the License.\r |
| 6 | * You may obtain a copy of the License at\r |
| 7 | *\r |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0\r |
| 9 | *\r |
| 10 | * Unless required by applicable law or agreed to in writing, software\r |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS,\r |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r |
| 13 | * See the License for the specific language governing permissions and\r |
| 14 | * limitations under the License.\r |
| 15 | */\r |
| 16 | \r |
| 17 | package org.openintents.util;\r |
| 18 | \r |
| 19 | import java.util.List;\r |
| 20 | \r |
| 21 | import android.content.ComponentName;\r |
| 22 | import android.content.Context;\r |
| 23 | import android.content.Intent;\r |
| 24 | import android.content.pm.PackageManager;\r |
| 25 | import android.content.pm.ResolveInfo;\r |
| 26 | import android.view.Menu;\r |
| 27 | import android.view.MenuItem;\r |
| 28 | \r |
| 29 | /**\r |
| 30 | * Adds intent options with icons.\r |
| 31 | * \r |
| 32 | * This code is retrieved from this message:\r |
| 33 | * http://groups.google.com/group/android-developers/browse_frm/thread/3fed25cdda765b02\r |
| 34 | * \r |
| 35 | */\r |
| 36 | public class MenuIntentOptionsWithIcons {\r |
| 37 | \r |
| 38 | Context mContext;\r |
| 39 | Menu mMenu;\r |
| 40 | \r |
| 41 | public MenuIntentOptionsWithIcons(Context context, Menu menu) {\r |
| 42 | mContext = context;\r |
| 43 | mMenu = menu;\r |
| 44 | }\r |
| 45 | \r |
| 46 | public int addIntentOptions(int group, int id, int categoryOrder,\r |
| 47 | ComponentName caller, Intent[] specifics, Intent intent, int flags,\r |
| 48 | MenuItem[] outSpecificItems) {\r |
| 49 | PackageManager pm = mContext.getPackageManager();\r |
| 50 | final List<ResolveInfo> lri = pm.queryIntentActivityOptions(caller,\r |
| 51 | specifics, intent, 0);\r |
| 52 | final int N = lri != null ? lri.size() : 0;\r |
| 53 | if ((flags & Menu.FLAG_APPEND_TO_GROUP) == 0) {\r |
| 54 | mMenu.removeGroup(group);\r |
| 55 | }\r |
| 56 | for (int i = 0; i < N; i++) {\r |
| 57 | final ResolveInfo ri = lri.get(i);\r |
| 58 | Intent rintent = new Intent(ri.specificIndex < 0 ? intent\r |
| 59 | : specifics[ri.specificIndex]);\r |
| 60 | rintent.setComponent(new ComponentName(\r |
| 61 | ri.activityInfo.applicationInfo.packageName,\r |
| 62 | ri.activityInfo.name));\r |
| 63 | final MenuItem item = mMenu.add(group, id, categoryOrder,\r |
| 64 | ri.loadLabel(pm)).setIcon(ri.loadIcon(pm)).setIntent(\r |
| 65 | rintent);\r |
| 66 | if (outSpecificItems != null && ri.specificIndex >= 0) {\r |
| 67 | outSpecificItems[ri.specificIndex] = item;\r |
| 68 | }\r |
| 69 | }\r |
| 70 | return N;\r |
| 71 | }\r |
| 72 | }\r |