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