Home | History | Annotate | Download | only in livepicker
      1 /*
      2  * Copyright (C) 2009 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.wallpaper.livepicker;
     18 
     19 import android.app.ListActivity;
     20 import android.app.WallpaperInfo;
     21 import android.os.Bundle;
     22 import android.content.pm.PackageManager;
     23 import android.content.pm.ResolveInfo;
     24 import android.content.pm.ComponentInfo;
     25 import android.content.Intent;
     26 import android.content.res.Resources;
     27 import android.graphics.drawable.Drawable;
     28 import android.graphics.drawable.BitmapDrawable;
     29 import android.graphics.Paint;
     30 import android.graphics.Canvas;
     31 import android.graphics.Bitmap;
     32 import android.util.Log;
     33 import android.view.Gravity;
     34 import android.view.View;
     35 import android.view.ViewGroup;
     36 import android.view.LayoutInflater;
     37 import android.service.wallpaper.WallpaperService;
     38 import android.widget.BaseAdapter;
     39 import android.widget.TextView;
     40 import android.widget.ImageView;
     41 import android.widget.AdapterView;
     42 import android.text.Html;
     43 
     44 import java.util.ArrayList;
     45 import java.util.List;
     46 import java.util.Collections;
     47 import java.util.Comparator;
     48 import java.io.IOException;
     49 import java.text.Collator;
     50 
     51 import org.xmlpull.v1.XmlPullParserException;
     52 
     53 public class LiveWallpaperListActivity extends ListActivity implements AdapterView.OnItemClickListener {
     54     private static final String LOG_TAG = "LiveWallpapersPicker";
     55 
     56     private static final int REQUEST_PREVIEW = 100;
     57 
     58     private PackageManager mPackageManager;
     59 
     60     private ArrayList<Drawable> mThumbnails;
     61     private ArrayList<WallpaperInfo> mWallpaperInfos;
     62     private ArrayList<Intent> mWallpaperIntents;
     63 
     64     @Override
     65     protected void onCreate(Bundle savedInstanceState) {
     66         super.onCreate(savedInstanceState);
     67         setContentView(R.layout.live_wallpaper_list);
     68 
     69         mPackageManager = getPackageManager();
     70 
     71         findLiveWallpapers();
     72 
     73         setListAdapter(new LiveWallpapersAdapter());
     74         getListView().setOnItemClickListener(this);
     75     }
     76 
     77     // TODO: THIS SHOULD HAPPEN IN AN ASYNCTASK
     78     private void findLiveWallpapers() {
     79         List<ResolveInfo> list = mPackageManager.queryIntentServices(
     80                 new Intent(WallpaperService.SERVICE_INTERFACE),
     81                 PackageManager.GET_META_DATA);
     82 
     83         int listSize = list.size();
     84 
     85         mThumbnails = new ArrayList<Drawable>(listSize);
     86         mWallpaperIntents = new ArrayList<Intent>(listSize);
     87         mWallpaperInfos = new ArrayList<WallpaperInfo>(listSize);
     88 
     89         Resources res = getResources();
     90         Drawable galleryIcon = res.getDrawable(R.drawable.livewallpaper_placeholder);
     91 
     92         Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
     93         paint.setTextAlign(Paint.Align.CENTER);
     94 
     95         Canvas canvas = new Canvas();
     96 
     97         Collections.sort(list, new Comparator<ResolveInfo>() {
     98             final Collator mCollator;
     99 
    100             {
    101                 mCollator = Collator.getInstance();
    102             }
    103 
    104             public int compare(ResolveInfo info1, ResolveInfo info2) {
    105                 return mCollator.compare(info1.loadLabel(mPackageManager),
    106                         info2.loadLabel(mPackageManager));
    107             }
    108         });
    109 
    110         for (int i = 0; i < listSize; i++) {
    111             ResolveInfo resolveInfo = list.get(i);
    112             ComponentInfo ci = resolveInfo.serviceInfo;
    113             WallpaperInfo info;
    114             try {
    115                 info = new WallpaperInfo(this, resolveInfo);
    116             } catch (XmlPullParserException e) {
    117                 Log.w(LOG_TAG, "Skipping wallpaper " + ci, e);
    118                 continue;
    119             } catch (IOException e) {
    120                 Log.w(LOG_TAG, "Skipping wallpaper " + ci, e);
    121                 continue;
    122             }
    123 
    124             String packageName = info.getPackageName();
    125             String className = info.getServiceName();
    126 
    127             Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);
    128             intent.setClassName(packageName, className);
    129 
    130             mWallpaperIntents.add(intent);
    131             mWallpaperInfos.add(info);
    132 
    133             Drawable thumb = info.loadThumbnail(mPackageManager);
    134             if (thumb == null) {
    135                 int thumbWidth = res.getDimensionPixelSize(R.dimen.live_wallpaper_thumbnail_width);
    136                 int thumbHeight = res.getDimensionPixelSize(R.dimen.live_wallpaper_thumbnail_height);
    137 
    138                 Bitmap thumbnail = Bitmap.createBitmap(thumbWidth, thumbHeight,
    139                         Bitmap.Config.ARGB_8888);
    140 
    141                 paint.setColor(res.getColor(R.color.live_wallpaper_thumbnail_background));
    142                 canvas.setBitmap(thumbnail);
    143                 canvas.drawPaint(paint);
    144 
    145                 galleryIcon.setBounds(0, 0, thumbWidth, thumbHeight);
    146                 ((BitmapDrawable) galleryIcon).setGravity(Gravity.CENTER);
    147                 galleryIcon.draw(canvas);
    148 
    149                 String title = info.loadLabel(mPackageManager).toString();
    150 
    151                 paint.setColor(res.getColor(R.color.live_wallpaper_thumbnail_text_color));
    152                 paint.setTextSize(
    153                         res.getDimensionPixelSize(R.dimen.live_wallpaper_thumbnail_text_size));
    154 
    155                 canvas.drawText(title, (int) (thumbWidth * 0.5),
    156                         thumbHeight - res.getDimensionPixelSize(
    157                                 R.dimen.live_wallpaper_thumbnail_text_offset), paint);
    158 
    159                 thumb = new BitmapDrawable(res, thumbnail);
    160             }
    161 
    162             thumb.setDither(true);
    163             mThumbnails.add(thumb);
    164         }
    165     }
    166 
    167     @Override
    168     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    169         super.onActivityResult(requestCode, resultCode, data);
    170 
    171         if (requestCode == REQUEST_PREVIEW) {
    172             if (resultCode == RESULT_OK) finish();
    173         }
    174     }
    175 
    176     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    177         final Intent intent = mWallpaperIntents.get(position);
    178         final WallpaperInfo info = mWallpaperInfos.get(position);
    179         LiveWallpaperPreview.showPreview(this, REQUEST_PREVIEW, intent, info);
    180     }
    181 
    182     static class ViewHolder {
    183         TextView titleAuthor;
    184         TextView description;
    185         ImageView thumbnail;
    186     }
    187 
    188     private class LiveWallpapersAdapter extends BaseAdapter {
    189         private final LayoutInflater mInflater;
    190 
    191         LiveWallpapersAdapter() {
    192             mInflater = LayoutInflater.from(LiveWallpaperListActivity.this);
    193         }
    194 
    195         public int getCount() {
    196             return mWallpaperInfos.size();
    197         }
    198 
    199         public Object getItem(int position) {
    200             return mWallpaperInfos.get(position);
    201         }
    202 
    203         public long getItemId(int position) {
    204             return position;
    205         }
    206 
    207         public View getView(int position, View convertView, ViewGroup parent) {
    208             ViewHolder holder;
    209             if (convertView == null) {
    210                 convertView = mInflater.inflate(R.layout.live_wallpaper_entry, parent, false);
    211 
    212                 holder = new ViewHolder();
    213                 holder.titleAuthor = (TextView) convertView.findViewById(R.id.title_author);
    214                 holder.description = (TextView) convertView.findViewById(R.id.description);
    215                 holder.thumbnail = (ImageView) convertView.findViewById(R.id.thumbnail);
    216                 convertView.setTag(holder);
    217             } else {
    218                 holder = (ViewHolder) convertView.getTag();
    219             }
    220 
    221             WallpaperInfo info = mWallpaperInfos.get(position);
    222             holder.thumbnail.setImageDrawable(mThumbnails.get(position));
    223             // author not currently used
    224             holder.titleAuthor.setText(info.loadLabel(mPackageManager));
    225             try {
    226                 holder.description.setVisibility(View.VISIBLE);
    227                 holder.description.setText(Html.fromHtml(
    228                         info.loadDescription(mPackageManager).toString()));
    229             } catch (Resources.NotFoundException e) {
    230                 holder.description.setVisibility(View.GONE);
    231             }
    232 
    233             return convertView;
    234         }
    235     }
    236 }
    237