blob: f46da53ec5ed4603218dac49e24620dd20d9bd82 [file] [log] [blame]
Michael Jurka998e4ff2013-09-18 16:04:36 +02001/*
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
17package com.android.launcher3;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.pm.ActivityInfo;
23import android.content.pm.PackageManager;
24import android.content.pm.ResolveInfo;
Michael Jurka5b4b6902013-10-03 18:15:08 -070025import android.graphics.Rect;
26import android.graphics.drawable.Drawable;
Michael Jurka998e4ff2013-09-18 16:04:36 +020027import android.view.LayoutInflater;
28import android.view.View;
29import android.view.ViewGroup;
30import android.widget.BaseAdapter;
Michael Jurka998e4ff2013-09-18 16:04:36 +020031import android.widget.ListAdapter;
32import android.widget.TextView;
33
Adam Cohen091440a2015-03-18 14:16:05 -070034import com.android.launcher3.util.Thunk;
35
Michael Jurka998e4ff2013-09-18 16:04:36 +020036import java.util.ArrayList;
37import java.util.List;
38
39public class ThirdPartyWallpaperPickerListAdapter extends BaseAdapter implements ListAdapter {
Michael Jurka998e4ff2013-09-18 16:04:36 +020040 private final LayoutInflater mInflater;
41 private final PackageManager mPackageManager;
Michael Jurka5b4b6902013-10-03 18:15:08 -070042 private final int mIconSize;
Michael Jurka998e4ff2013-09-18 16:04:36 +020043
Michael Jurka16205da2013-09-30 18:07:18 -070044 private List<ThirdPartyWallpaperTile> mThirdPartyWallpaperPickers =
45 new ArrayList<ThirdPartyWallpaperTile>();
Michael Jurka998e4ff2013-09-18 16:04:36 +020046
Michael Jurka1e4e6dd2013-09-23 23:02:05 +010047 public static class ThirdPartyWallpaperTile extends WallpaperPickerActivity.WallpaperTileInfo {
Adam Cohen091440a2015-03-18 14:16:05 -070048 @Thunk ResolveInfo mResolveInfo;
Michael Jurka1e4e6dd2013-09-23 23:02:05 +010049 public ThirdPartyWallpaperTile(ResolveInfo resolveInfo) {
50 mResolveInfo = resolveInfo;
51 }
Michael Jurka8a34bdb2013-09-25 07:57:33 -070052 @Override
Michael Jurka1e4e6dd2013-09-23 23:02:05 +010053 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 Jurka7ad868b2013-12-12 15:04:25 +010058 a.startActivityForResultSafely(
59 launchIntent, WallpaperPickerActivity.PICK_WALLPAPER_THIRD_PARTY_ACTIVITY);
Michael Jurka1e4e6dd2013-09-23 23:02:05 +010060 }
61 }
62
Michael Jurka998e4ff2013-09-18 16:04:36 +020063 public ThirdPartyWallpaperPickerListAdapter(Context context) {
Sunny Goyal1d0b0932015-03-20 18:51:38 -070064 mInflater = LayoutInflater.from(context);
Michael Jurka998e4ff2013-09-18 16:04:36 +020065 mPackageManager = context.getPackageManager();
Michael Jurka5b4b6902013-10-03 18:15:08 -070066 mIconSize = context.getResources().getDimensionPixelSize(R.dimen.wallpaperItemIconSize);
Michael Jurka998e4ff2013-09-18 16:04:36 +020067 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 Jurka16205da2013-09-30 18:07:18 -0700103 mThirdPartyWallpaperPickers.add(new ThirdPartyWallpaperTile(info));
Michael Jurka998e4ff2013-09-18 16:04:36 +0200104 }
105 }
106
107 public int getCount() {
108 return mThirdPartyWallpaperPickers.size();
109 }
110
Michael Jurka16205da2013-09-30 18:07:18 -0700111 public ThirdPartyWallpaperTile getItem(int position) {
Michael Jurka998e4ff2013-09-18 16:04:36 +0200112 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 Jurka5b4b6902013-10-03 18:15:08 -0700123 view = mInflater.inflate(R.layout.wallpaper_picker_third_party_item, parent, false);
Michael Jurka998e4ff2013-09-18 16:04:36 +0200124 } else {
125 view = convertView;
126 }
127
Michael Jurka16205da2013-09-30 18:07:18 -0700128 ResolveInfo info = mThirdPartyWallpaperPickers.get(position).mResolveInfo;
Michael Jurka998e4ff2013-09-18 16:04:36 +0200129 TextView label = (TextView) view.findViewById(R.id.wallpaper_item_label);
130 label.setText(info.loadLabel(mPackageManager));
Michael Jurka5b4b6902013-10-03 18:15:08 -0700131 Drawable icon = info.loadIcon(mPackageManager);
132 icon.setBounds(new Rect(0, 0, mIconSize, mIconSize));
133 label.setCompoundDrawables(null, icon, null, null);
Michael Jurka998e4ff2013-09-18 16:04:36 +0200134 return view;
135 }
136}