LiveWallpaperListAdapter.java revision ffed65886ecd83f52dc1c052897511c95a5f371a
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.app.WallpaperInfo;
20import android.app.WallpaperManager;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
24import android.content.pm.ResolveInfo;
25import android.graphics.drawable.Drawable;
26import android.os.AsyncTask;
27import android.service.wallpaper.WallpaperService;
28import android.util.Log;
29import android.view.LayoutInflater;
30import android.view.View;
31import android.view.ViewGroup;
32import android.widget.BaseAdapter;
33import android.widget.FrameLayout;
34import android.widget.ImageView;
35import android.widget.ListAdapter;
36import android.widget.TextView;
37
38import org.xmlpull.v1.XmlPullParserException;
39
40import java.io.IOException;
41import java.text.Collator;
42import java.util.ArrayList;
43import java.util.Collections;
44import java.util.Comparator;
45import java.util.List;
46
47public class LiveWallpaperListAdapter extends BaseAdapter implements ListAdapter {
48    private static final String LOG_TAG = "LiveWallpaperListAdapter";
49
50    private final LayoutInflater mInflater;
51    private final PackageManager mPackageManager;
52
53    private List<LiveWallpaperTile> mWallpapers;
54
55    @SuppressWarnings("unchecked")
56    public LiveWallpaperListAdapter(Context context) {
57        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
58        mPackageManager = context.getPackageManager();
59
60        List<ResolveInfo> list = mPackageManager.queryIntentServices(
61                new Intent(WallpaperService.SERVICE_INTERFACE),
62                PackageManager.GET_META_DATA);
63
64        mWallpapers = new ArrayList<LiveWallpaperTile>();
65
66        new LiveWallpaperEnumerator(context).execute(list);
67    }
68
69    public int getCount() {
70        if (mWallpapers == null) {
71            return 0;
72        }
73        return mWallpapers.size();
74    }
75
76    public LiveWallpaperTile getItem(int position) {
77        return mWallpapers.get(position);
78    }
79
80    public long getItemId(int position) {
81        return position;
82    }
83
84    public View getView(int position, View convertView, ViewGroup parent) {
85        View view;
86
87        if (convertView == null) {
88            view = mInflater.inflate(R.layout.wallpaper_picker_live_wallpaper_item, parent, false);
89        } else {
90            view = convertView;
91        }
92
93        WallpaperPickerActivity.setWallpaperItemPaddingToZero((FrameLayout) view);
94
95        LiveWallpaperTile wallpaperInfo = mWallpapers.get(position);
96        wallpaperInfo.setView(view);
97        ImageView image = (ImageView) view.findViewById(R.id.wallpaper_image);
98        ImageView icon = (ImageView) view.findViewById(R.id.wallpaper_icon);
99        if (wallpaperInfo.mThumbnail != null) {
100            image.setImageDrawable(wallpaperInfo.mThumbnail);
101            icon.setVisibility(View.GONE);
102        } else {
103            icon.setImageDrawable(wallpaperInfo.mInfo.loadIcon(mPackageManager));
104            icon.setVisibility(View.VISIBLE);
105        }
106
107        TextView label = (TextView) view.findViewById(R.id.wallpaper_item_label);
108        label.setText(wallpaperInfo.mInfo.loadLabel(mPackageManager));
109
110        return view;
111    }
112
113    public static class LiveWallpaperTile extends WallpaperPickerActivity.WallpaperTileInfo {
114        private Drawable mThumbnail;
115        private WallpaperInfo mInfo;
116        public LiveWallpaperTile(Drawable thumbnail, WallpaperInfo info, Intent intent) {
117            mThumbnail = thumbnail;
118            mInfo = info;
119        }
120        @Override
121        public void onClick(WallpaperPickerActivity a) {
122            Intent preview = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
123            preview.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
124                    mInfo.getComponent());
125            a.onLiveWallpaperPickerLaunch(mInfo);
126            a.startActivityForResultSafely(preview, WallpaperPickerActivity.PICK_LIVE_WALLPAPER);
127        }
128    }
129
130    private class LiveWallpaperEnumerator extends
131            AsyncTask<List<ResolveInfo>, LiveWallpaperTile, Void> {
132        private Context mContext;
133        private int mWallpaperPosition;
134
135        public LiveWallpaperEnumerator(Context context) {
136            super();
137            mContext = context;
138            mWallpaperPosition = 0;
139        }
140
141        @Override
142        protected Void doInBackground(List<ResolveInfo>... params) {
143            final PackageManager packageManager = mContext.getPackageManager();
144
145            List<ResolveInfo> list = params[0];
146
147            Collections.sort(list, new Comparator<ResolveInfo>() {
148                final Collator mCollator;
149
150                {
151                    mCollator = Collator.getInstance();
152                }
153
154                public int compare(ResolveInfo info1, ResolveInfo info2) {
155                    return mCollator.compare(info1.loadLabel(packageManager),
156                            info2.loadLabel(packageManager));
157                }
158            });
159
160            for (ResolveInfo resolveInfo : list) {
161                WallpaperInfo info = null;
162                try {
163                    info = new WallpaperInfo(mContext, resolveInfo);
164                } catch (XmlPullParserException e) {
165                    Log.w(LOG_TAG, "Skipping wallpaper " + resolveInfo.serviceInfo, e);
166                    continue;
167                } catch (IOException e) {
168                    Log.w(LOG_TAG, "Skipping wallpaper " + resolveInfo.serviceInfo, e);
169                    continue;
170                }
171
172
173                Drawable thumb = info.loadThumbnail(packageManager);
174                Intent launchIntent = new Intent(WallpaperService.SERVICE_INTERFACE);
175                launchIntent.setClassName(info.getPackageName(), info.getServiceName());
176                LiveWallpaperTile wallpaper = new LiveWallpaperTile(thumb, info, launchIntent);
177                publishProgress(wallpaper);
178            }
179            // Send a null object to show loading is finished
180            publishProgress((LiveWallpaperTile) null);
181
182            return null;
183        }
184
185        @Override
186        protected void onProgressUpdate(LiveWallpaperTile...infos) {
187            for (LiveWallpaperTile info : infos) {
188                if (info == null) {
189                    LiveWallpaperListAdapter.this.notifyDataSetChanged();
190                    break;
191                }
192                if (info.mThumbnail != null) {
193                    info.mThumbnail.setDither(true);
194                }
195                if (mWallpaperPosition < mWallpapers.size()) {
196                    mWallpapers.set(mWallpaperPosition, info);
197                } else {
198                    mWallpapers.add(info);
199                }
200                mWallpaperPosition++;
201            }
202        }
203    }
204}
205