1 package com.android.wallpaperpicker.tileinfo;
2 
3 import android.content.ComponentName;
4 import android.content.Context;
5 import android.content.Intent;
6 import android.content.pm.PackageManager;
7 import android.content.pm.ResolveInfo;
8 import android.graphics.Rect;
9 import android.graphics.drawable.Drawable;
10 import android.view.LayoutInflater;
11 import android.view.View;
12 import android.view.ViewGroup;
13 import android.widget.TextView;
14 
15 import com.android.wallpaperpicker.R;
16 import com.android.wallpaperpicker.WallpaperPickerActivity;
17 import com.android.wallpaperpicker.WallpaperUtils;
18 
19 import java.util.ArrayList;
20 import java.util.HashSet;
21 import java.util.List;
22 
23 public class ThirdPartyWallpaperInfo extends WallpaperTileInfo {
24 
25     private final ResolveInfo mResolveInfo;
26     private final int mIconSize;
27 
ThirdPartyWallpaperInfo(ResolveInfo resolveInfo, int iconSize)28     public ThirdPartyWallpaperInfo(ResolveInfo resolveInfo, int iconSize) {
29         mResolveInfo = resolveInfo;
30         mIconSize = iconSize;
31     }
32 
33     @Override
onClick(WallpaperPickerActivity a)34     public void onClick(WallpaperPickerActivity a) {
35         final ComponentName itemComponentName = new ComponentName(
36                 mResolveInfo.activityInfo.packageName, mResolveInfo.activityInfo.name);
37         Intent launchIntent = new Intent(Intent.ACTION_SET_WALLPAPER)
38             .setComponent(itemComponentName)
39             .putExtra(WallpaperUtils.EXTRA_WALLPAPER_OFFSET,
40                     a.getWallpaperParallaxOffset());
41         a.startActivityForResultSafely(
42                 launchIntent, WallpaperPickerActivity.PICK_WALLPAPER_THIRD_PARTY_ACTIVITY);
43     }
44 
45     @Override
createView(Context context, LayoutInflater inflator, ViewGroup parent)46     public View createView(Context context, LayoutInflater inflator, ViewGroup parent) {
47         mView = inflator.inflate(R.layout.wallpaper_picker_third_party_item, parent, false);
48 
49         TextView label = (TextView) mView.findViewById(R.id.wallpaper_item_label);
50         label.setText(mResolveInfo.loadLabel(context.getPackageManager()));
51         Drawable icon = mResolveInfo.loadIcon(context.getPackageManager());
52         icon.setBounds(new Rect(0, 0, mIconSize, mIconSize));
53         label.setCompoundDrawables(null, icon, null, null);
54         return mView;
55     }
56 
getAll(Context context)57     public static List<ThirdPartyWallpaperInfo> getAll(Context context) {
58         ArrayList<ThirdPartyWallpaperInfo> result = new ArrayList<>();
59         int iconSize = context.getResources().getDimensionPixelSize(R.dimen.wallpaperItemIconSize);
60 
61         final PackageManager pm = context.getPackageManager();
62         Intent pickImageIntent = new Intent(Intent.ACTION_GET_CONTENT).setType("image/*");
63         HashSet<String> excludePackages = new HashSet<>();
64         // Exclude packages which contain an image picker
65         for (ResolveInfo info : pm.queryIntentActivities(pickImageIntent, 0)) {
66             excludePackages.add(info.activityInfo.packageName);
67         }
68         excludePackages.add(context.getPackageName());
69         excludePackages.add("com.android.wallpaper.livepicker");
70 
71         final Intent pickWallpaperIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
72         for (ResolveInfo info : pm.queryIntentActivities(pickWallpaperIntent, 0)) {
73             if (!excludePackages.contains(info.activityInfo.packageName)) {
74                 result.add(new ThirdPartyWallpaperInfo(info, iconSize));
75             }
76         }
77         return result;
78     }
79 }