Home | History | Annotate | Download | only in launcher3
      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.content.ComponentName;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.pm.ActivityInfo;
     23 import android.content.pm.PackageManager;
     24 import android.content.pm.ResolveInfo;
     25 import android.graphics.Rect;
     26 import android.graphics.drawable.Drawable;
     27 import android.view.LayoutInflater;
     28 import android.view.View;
     29 import android.view.ViewGroup;
     30 import android.widget.BaseAdapter;
     31 import android.widget.ListAdapter;
     32 import android.widget.TextView;
     33 
     34 import com.android.launcher3.util.Thunk;
     35 
     36 import java.util.ArrayList;
     37 import java.util.List;
     38 
     39 public class ThirdPartyWallpaperPickerListAdapter extends BaseAdapter implements ListAdapter {
     40     private final LayoutInflater mInflater;
     41     private final PackageManager mPackageManager;
     42     private final int mIconSize;
     43 
     44     private List<ThirdPartyWallpaperTile> mThirdPartyWallpaperPickers =
     45             new ArrayList<ThirdPartyWallpaperTile>();
     46 
     47     public static class ThirdPartyWallpaperTile extends WallpaperPickerActivity.WallpaperTileInfo {
     48         @Thunk ResolveInfo mResolveInfo;
     49         public ThirdPartyWallpaperTile(ResolveInfo resolveInfo) {
     50             mResolveInfo = resolveInfo;
     51         }
     52         @Override
     53         public void onClick(WallpaperPickerActivity a) {
     54             final ComponentName itemComponentName = new ComponentName(
     55                     mResolveInfo.activityInfo.packageName, mResolveInfo.activityInfo.name);
     56             Intent launchIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
     57             launchIntent.setComponent(itemComponentName);
     58             a.startActivityForResultSafely(
     59                     launchIntent, WallpaperPickerActivity.PICK_WALLPAPER_THIRD_PARTY_ACTIVITY);
     60         }
     61     }
     62 
     63     public ThirdPartyWallpaperPickerListAdapter(Context context) {
     64         mInflater = LayoutInflater.from(context);
     65         mPackageManager = context.getPackageManager();
     66         mIconSize = context.getResources().getDimensionPixelSize(R.dimen.wallpaperItemIconSize);
     67         final PackageManager pm = mPackageManager;
     68 
     69         final Intent pickWallpaperIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
     70         final List<ResolveInfo> apps =
     71                 pm.queryIntentActivities(pickWallpaperIntent, 0);
     72 
     73         // Get list of image picker intents
     74         Intent pickImageIntent = new Intent(Intent.ACTION_GET_CONTENT);
     75         pickImageIntent.setType("image/*");
     76         final List<ResolveInfo> imagePickerActivities =
     77                 pm.queryIntentActivities(pickImageIntent, 0);
     78         final ComponentName[] imageActivities = new ComponentName[imagePickerActivities.size()];
     79         for (int i = 0; i < imagePickerActivities.size(); i++) {
     80             ActivityInfo activityInfo = imagePickerActivities.get(i).activityInfo;
     81             imageActivities[i] = new ComponentName(activityInfo.packageName, activityInfo.name);
     82         }
     83 
     84         outerLoop:
     85         for (ResolveInfo info : apps) {
     86             final ComponentName itemComponentName =
     87                     new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
     88             final String itemPackageName = itemComponentName.getPackageName();
     89             // Exclude anything from our own package, and the old Launcher,
     90             // and live wallpaper picker
     91             if (itemPackageName.equals(context.getPackageName()) ||
     92                     itemPackageName.equals("com.android.launcher") ||
     93                     itemPackageName.equals("com.android.wallpaper.livepicker")) {
     94                 continue;
     95             }
     96             // Exclude any package that already responds to the image picker intent
     97             for (ResolveInfo imagePickerActivityInfo : imagePickerActivities) {
     98                 if (itemPackageName.equals(
     99                         imagePickerActivityInfo.activityInfo.packageName)) {
    100                     continue outerLoop;
    101                 }
    102             }
    103             mThirdPartyWallpaperPickers.add(new ThirdPartyWallpaperTile(info));
    104         }
    105     }
    106 
    107     public int getCount() {
    108         return mThirdPartyWallpaperPickers.size();
    109     }
    110 
    111     public ThirdPartyWallpaperTile getItem(int position) {
    112         return mThirdPartyWallpaperPickers.get(position);
    113     }
    114 
    115     public long getItemId(int position) {
    116         return position;
    117     }
    118 
    119     public View getView(int position, View convertView, ViewGroup parent) {
    120         View view;
    121 
    122         if (convertView == null) {
    123             view = mInflater.inflate(R.layout.wallpaper_picker_third_party_item, parent, false);
    124         } else {
    125             view = convertView;
    126         }
    127 
    128         ResolveInfo info = mThirdPartyWallpaperPickers.get(position).mResolveInfo;
    129         TextView label = (TextView) view.findViewById(R.id.wallpaper_item_label);
    130         label.setText(info.loadLabel(mPackageManager));
    131         Drawable icon = info.loadIcon(mPackageManager);
    132         icon.setBounds(new Rect(0, 0, mIconSize, mIconSize));
    133         label.setCompoundDrawables(null, icon, null, null);
    134         return view;
    135     }
    136 }
    137