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 17 package com.android.launcher3; 18 19 import android.app.WallpaperInfo; 20 import android.app.WallpaperManager; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.PackageManager; 24 import android.content.pm.ResolveInfo; 25 import android.graphics.drawable.Drawable; 26 import android.os.AsyncTask; 27 import android.service.wallpaper.WallpaperService; 28 import android.util.Log; 29 import android.view.LayoutInflater; 30 import android.view.View; 31 import android.view.ViewGroup; 32 import android.widget.BaseAdapter; 33 import android.widget.FrameLayout; 34 import android.widget.ImageView; 35 import android.widget.ListAdapter; 36 import android.widget.TextView; 37 38 import org.xmlpull.v1.XmlPullParserException; 39 40 import java.io.IOException; 41 import java.text.Collator; 42 import java.util.ArrayList; 43 import java.util.Collections; 44 import java.util.Comparator; 45 import java.util.List; 46 47 public 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(); 126 Utilities.startActivityForResultSafely( 127 a, preview, WallpaperPickerActivity.PICK_LIVE_WALLPAPER); 128 } 129 } 130 131 private class LiveWallpaperEnumerator extends 132 AsyncTask<List<ResolveInfo>, LiveWallpaperTile, Void> { 133 private Context mContext; 134 private int mWallpaperPosition; 135 136 public LiveWallpaperEnumerator(Context context) { 137 super(); 138 mContext = context; 139 mWallpaperPosition = 0; 140 } 141 142 @Override 143 protected Void doInBackground(List<ResolveInfo>... params) { 144 final PackageManager packageManager = mContext.getPackageManager(); 145 146 List<ResolveInfo> list = params[0]; 147 148 Collections.sort(list, new Comparator<ResolveInfo>() { 149 final Collator mCollator; 150 151 { 152 mCollator = Collator.getInstance(); 153 } 154 155 public int compare(ResolveInfo info1, ResolveInfo info2) { 156 return mCollator.compare(info1.loadLabel(packageManager), 157 info2.loadLabel(packageManager)); 158 } 159 }); 160 161 for (ResolveInfo resolveInfo : list) { 162 WallpaperInfo info = null; 163 try { 164 info = new WallpaperInfo(mContext, resolveInfo); 165 } catch (XmlPullParserException e) { 166 Log.w(LOG_TAG, "Skipping wallpaper " + resolveInfo.serviceInfo, e); 167 continue; 168 } catch (IOException e) { 169 Log.w(LOG_TAG, "Skipping wallpaper " + resolveInfo.serviceInfo, e); 170 continue; 171 } 172 173 174 Drawable thumb = info.loadThumbnail(packageManager); 175 Intent launchIntent = new Intent(WallpaperService.SERVICE_INTERFACE); 176 launchIntent.setClassName(info.getPackageName(), info.getServiceName()); 177 LiveWallpaperTile wallpaper = new LiveWallpaperTile(thumb, info, launchIntent); 178 publishProgress(wallpaper); 179 } 180 // Send a null object to show loading is finished 181 publishProgress((LiveWallpaperTile) null); 182 183 return null; 184 } 185 186 @Override 187 protected void onProgressUpdate(LiveWallpaperTile...infos) { 188 for (LiveWallpaperTile info : infos) { 189 if (info == null) { 190 LiveWallpaperListAdapter.this.notifyDataSetChanged(); 191 break; 192 } 193 info.mThumbnail.setDither(true); 194 if (mWallpaperPosition < mWallpapers.size()) { 195 mWallpapers.set(mWallpaperPosition, info); 196 } else { 197 mWallpapers.add(info); 198 } 199 mWallpaperPosition++; 200 } 201 } 202 } 203 } 204