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