Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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.launcher3; |
| 18 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.pm.ActivityInfo; |
| 23 | import android.content.pm.PackageManager; |
| 24 | import android.content.pm.ResolveInfo; |
Michael Jurka | 5b4b690 | 2013-10-03 18:15:08 -0700 | [diff] [blame] | 25 | import android.graphics.Rect; |
| 26 | import android.graphics.drawable.Drawable; |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 27 | import android.view.LayoutInflater; |
| 28 | import android.view.View; |
| 29 | import android.view.ViewGroup; |
| 30 | import android.widget.BaseAdapter; |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 31 | import android.widget.ListAdapter; |
| 32 | import android.widget.TextView; |
| 33 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 34 | import com.android.launcher3.util.Thunk; |
| 35 | |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 36 | import java.util.ArrayList; |
| 37 | import java.util.List; |
| 38 | |
| 39 | public class ThirdPartyWallpaperPickerListAdapter extends BaseAdapter implements ListAdapter { |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 40 | private final LayoutInflater mInflater; |
| 41 | private final PackageManager mPackageManager; |
Michael Jurka | 5b4b690 | 2013-10-03 18:15:08 -0700 | [diff] [blame] | 42 | private final int mIconSize; |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 43 | |
Michael Jurka | 16205da | 2013-09-30 18:07:18 -0700 | [diff] [blame] | 44 | private List<ThirdPartyWallpaperTile> mThirdPartyWallpaperPickers = |
| 45 | new ArrayList<ThirdPartyWallpaperTile>(); |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 46 | |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 47 | public static class ThirdPartyWallpaperTile extends WallpaperPickerActivity.WallpaperTileInfo { |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 48 | @Thunk ResolveInfo mResolveInfo; |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 49 | public ThirdPartyWallpaperTile(ResolveInfo resolveInfo) { |
| 50 | mResolveInfo = resolveInfo; |
| 51 | } |
Michael Jurka | 8a34bdb | 2013-09-25 07:57:33 -0700 | [diff] [blame] | 52 | @Override |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 53 | public void onClick(WallpaperPickerActivity a) { |
| 54 | final ComponentName itemComponentName = new ComponentName( |
| 55 | mResolveInfo.activityInfo.packageName, mResolveInfo.activityInfo.name); |
| 56 | Intent launchIntent = new Intent(Intent.ACTION_SET_WALLPAPER); |
| 57 | launchIntent.setComponent(itemComponentName); |
Michael Jurka | 7ad868b | 2013-12-12 15:04:25 +0100 | [diff] [blame] | 58 | a.startActivityForResultSafely( |
| 59 | launchIntent, WallpaperPickerActivity.PICK_WALLPAPER_THIRD_PARTY_ACTIVITY); |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 63 | public ThirdPartyWallpaperPickerListAdapter(Context context) { |
Sunny Goyal | 1d0b093 | 2015-03-20 18:51:38 -0700 | [diff] [blame^] | 64 | mInflater = LayoutInflater.from(context); |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 65 | mPackageManager = context.getPackageManager(); |
Michael Jurka | 5b4b690 | 2013-10-03 18:15:08 -0700 | [diff] [blame] | 66 | mIconSize = context.getResources().getDimensionPixelSize(R.dimen.wallpaperItemIconSize); |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 67 | final PackageManager pm = mPackageManager; |
| 68 | |
| 69 | final Intent pickWallpaperIntent = new Intent(Intent.ACTION_SET_WALLPAPER); |
| 70 | final List<ResolveInfo> apps = |
| 71 | pm.queryIntentActivities(pickWallpaperIntent, 0); |
| 72 | |
| 73 | // Get list of image picker intents |
| 74 | Intent pickImageIntent = new Intent(Intent.ACTION_GET_CONTENT); |
| 75 | pickImageIntent.setType("image/*"); |
| 76 | final List<ResolveInfo> imagePickerActivities = |
| 77 | pm.queryIntentActivities(pickImageIntent, 0); |
| 78 | final ComponentName[] imageActivities = new ComponentName[imagePickerActivities.size()]; |
| 79 | for (int i = 0; i < imagePickerActivities.size(); i++) { |
| 80 | ActivityInfo activityInfo = imagePickerActivities.get(i).activityInfo; |
| 81 | imageActivities[i] = new ComponentName(activityInfo.packageName, activityInfo.name); |
| 82 | } |
| 83 | |
| 84 | outerLoop: |
| 85 | for (ResolveInfo info : apps) { |
| 86 | final ComponentName itemComponentName = |
| 87 | new ComponentName(info.activityInfo.packageName, info.activityInfo.name); |
| 88 | final String itemPackageName = itemComponentName.getPackageName(); |
| 89 | // Exclude anything from our own package, and the old Launcher, |
| 90 | // and live wallpaper picker |
| 91 | if (itemPackageName.equals(context.getPackageName()) || |
| 92 | itemPackageName.equals("com.android.launcher") || |
| 93 | itemPackageName.equals("com.android.wallpaper.livepicker")) { |
| 94 | continue; |
| 95 | } |
| 96 | // Exclude any package that already responds to the image picker intent |
| 97 | for (ResolveInfo imagePickerActivityInfo : imagePickerActivities) { |
| 98 | if (itemPackageName.equals( |
| 99 | imagePickerActivityInfo.activityInfo.packageName)) { |
| 100 | continue outerLoop; |
| 101 | } |
| 102 | } |
Michael Jurka | 16205da | 2013-09-30 18:07:18 -0700 | [diff] [blame] | 103 | mThirdPartyWallpaperPickers.add(new ThirdPartyWallpaperTile(info)); |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | public int getCount() { |
| 108 | return mThirdPartyWallpaperPickers.size(); |
| 109 | } |
| 110 | |
Michael Jurka | 16205da | 2013-09-30 18:07:18 -0700 | [diff] [blame] | 111 | public ThirdPartyWallpaperTile getItem(int position) { |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 112 | return mThirdPartyWallpaperPickers.get(position); |
| 113 | } |
| 114 | |
| 115 | public long getItemId(int position) { |
| 116 | return position; |
| 117 | } |
| 118 | |
| 119 | public View getView(int position, View convertView, ViewGroup parent) { |
| 120 | View view; |
| 121 | |
| 122 | if (convertView == null) { |
Michael Jurka | 5b4b690 | 2013-10-03 18:15:08 -0700 | [diff] [blame] | 123 | view = mInflater.inflate(R.layout.wallpaper_picker_third_party_item, parent, false); |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 124 | } else { |
| 125 | view = convertView; |
| 126 | } |
| 127 | |
Michael Jurka | 16205da | 2013-09-30 18:07:18 -0700 | [diff] [blame] | 128 | ResolveInfo info = mThirdPartyWallpaperPickers.get(position).mResolveInfo; |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 129 | TextView label = (TextView) view.findViewById(R.id.wallpaper_item_label); |
| 130 | label.setText(info.loadLabel(mPackageManager)); |
Michael Jurka | 5b4b690 | 2013-10-03 18:15:08 -0700 | [diff] [blame] | 131 | Drawable icon = info.loadIcon(mPackageManager); |
| 132 | icon.setBounds(new Rect(0, 0, mIconSize, mIconSize)); |
| 133 | label.setCompoundDrawables(null, icon, null, null); |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 134 | return view; |
| 135 | } |
| 136 | } |