Home | History | Annotate | Download | only in tileinfo
      1 package com.android.wallpaperpicker.tileinfo;
      2 
      3 import android.app.WallpaperInfo;
      4 import android.app.WallpaperManager;
      5 import android.content.Context;
      6 import android.content.Intent;
      7 import android.content.pm.PackageManager;
      8 import android.content.pm.ResolveInfo;
      9 import android.graphics.drawable.Drawable;
     10 import android.os.AsyncTask;
     11 import android.service.wallpaper.WallpaperService;
     12 import android.util.Log;
     13 import android.view.LayoutInflater;
     14 import android.view.View;
     15 import android.view.ViewGroup;
     16 import android.widget.ImageView;
     17 import android.widget.TextView;
     18 
     19 import com.android.wallpaperpicker.R;
     20 import com.android.wallpaperpicker.WallpaperPickerActivity;
     21 
     22 import org.xmlpull.v1.XmlPullParserException;
     23 
     24 import java.io.IOException;
     25 import java.text.Collator;
     26 import java.util.ArrayList;
     27 import java.util.Collections;
     28 import java.util.Comparator;
     29 import java.util.List;
     30 
     31 public 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 }