1package com.android.wallpaperpicker.tileinfo;
2
3import android.app.WallpaperInfo;
4import android.app.WallpaperManager;
5import android.content.Context;
6import android.content.Intent;
7import android.content.pm.PackageManager;
8import android.content.pm.ResolveInfo;
9import android.graphics.drawable.Drawable;
10import android.os.AsyncTask;
11import android.service.wallpaper.WallpaperService;
12import android.util.Log;
13import android.view.LayoutInflater;
14import android.view.View;
15import android.view.ViewGroup;
16import android.widget.ImageView;
17import android.widget.TextView;
18
19import com.android.wallpaperpicker.R;
20import com.android.wallpaperpicker.WallpaperPickerActivity;
21
22import org.xmlpull.v1.XmlPullParserException;
23
24import java.io.IOException;
25import java.text.Collator;
26import java.util.ArrayList;
27import java.util.Collections;
28import java.util.Comparator;
29import java.util.List;
30
31public class LiveWallpaperInfo extends WallpaperTileInfo {
32
33    private static final String TAG = "LiveWallpaperTile";
34
35    private Drawable mThumbnail;
36    private WallpaperInfo mInfo;
37
38    public LiveWallpaperInfo(Drawable thumbnail, WallpaperInfo info, Intent intent) {
39        mThumbnail = thumbnail;
40        mInfo = info;
41    }
42
43    @Override
44    public void onClick(WallpaperPickerActivity a) {
45        Intent preview = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
46        preview.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
47                mInfo.getComponent());
48        a.startActivityForResultSafely(preview,
49                WallpaperPickerActivity.PICK_WALLPAPER_THIRD_PARTY_ACTIVITY);
50    }
51
52    @Override
53    public View createView(Context context, LayoutInflater inflator, ViewGroup parent) {
54        mView = inflator.inflate(R.layout.wallpaper_picker_live_wallpaper_item, parent, false);
55
56        ImageView image = (ImageView) mView.findViewById(R.id.wallpaper_image);
57        ImageView icon = (ImageView) mView.findViewById(R.id.wallpaper_icon);
58        if (mThumbnail != null) {
59            image.setImageDrawable(mThumbnail);
60            icon.setVisibility(View.GONE);
61        } else {
62            icon.setImageDrawable(mInfo.loadIcon(context.getPackageManager()));
63            icon.setVisibility(View.VISIBLE);
64        }
65
66        TextView label = (TextView) mView.findViewById(R.id.wallpaper_item_label);
67        label.setText(mInfo.loadLabel(context.getPackageManager()));
68        return mView;
69    }
70
71    /**
72     * An async task to load various live wallpaper tiles.
73     */
74    public static class LoaderTask extends AsyncTask<Void, Void, List<LiveWallpaperInfo>> {
75        private final Context mContext;
76
77        public LoaderTask(Context context) {
78            mContext = context;
79        }
80
81        @Override
82        protected List<LiveWallpaperInfo> doInBackground(Void... params) {
83            final PackageManager pm = mContext.getPackageManager();
84
85            List<ResolveInfo> list = pm.queryIntentServices(
86                    new Intent(WallpaperService.SERVICE_INTERFACE),
87                    PackageManager.GET_META_DATA);
88
89            Collections.sort(list, new Comparator<ResolveInfo>() {
90                final Collator mCollator = Collator.getInstance();
91
92                public int compare(ResolveInfo info1, ResolveInfo info2) {
93                    return mCollator.compare(info1.loadLabel(pm), info2.loadLabel(pm));
94                }
95            });
96
97            List<LiveWallpaperInfo> result = new ArrayList<>();
98
99            for (ResolveInfo resolveInfo : list) {
100                WallpaperInfo info = null;
101                try {
102                    info = new WallpaperInfo(mContext, resolveInfo);
103                } catch (XmlPullParserException | IOException e) {
104                    Log.w(TAG, "Skipping wallpaper " + resolveInfo.serviceInfo, e);
105                    continue;
106                }
107
108
109                Drawable thumb = info.loadThumbnail(pm);
110                Intent launchIntent = new Intent(WallpaperService.SERVICE_INTERFACE);
111                launchIntent.setClassName(info.getPackageName(), info.getServiceName());
112                result.add(new LiveWallpaperInfo(thumb, info, launchIntent));
113            }
114
115            return result;
116        }
117    }
118}