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             .putExtra(WallpaperPickerActivity.EXTRA_WALLPAPER_OFFSET,
     59                     a.getWallpaperParallaxOffset());
     60             a.startActivityForResultSafely(
     61                     launchIntent, WallpaperPickerActivity.PICK_WALLPAPER_THIRD_PARTY_ACTIVITY);
     62         }
     63     }
     64 
     65     public ThirdPartyWallpaperPickerListAdapter(Context context) {
     66         mInflater = LayoutInflater.from(context);
     67         mPackageManager = context.getPackageManager();
     68         mIconSize = context.getResources().getDimensionPixelSize(R.dimen.wallpaperItemIconSize);
     69         final PackageManager pm = mPackageManager;
     70 
     71         final Intent pickWallpaperIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
     72         final List<ResolveInfo> apps =
     73                 pm.queryIntentActivities(pickWallpaperIntent, 0);
     74 
     75         // Get list of image picker intents
     76         Intent pickImageIntent = new Intent(Intent.ACTION_GET_CONTENT);
     77         pickImageIntent.setType("image/*");
     78         final List<ResolveInfo> imagePickerActivities =
     79                 pm.queryIntentActivities(pickImageIntent, 0);
     80         final ComponentName[] imageActivities = new ComponentName[imagePickerActivities.size()];
     81         for (int i = 0; i < imagePickerActivities.size(); i++) {
     82             ActivityInfo activityInfo = imagePickerActivities.get(i).activityInfo;
     83             imageActivities[i] = new ComponentName(activityInfo.packageName, activityInfo.name);
     84         }
     85 
     86         outerLoop:
     87         for (ResolveInfo info : apps) {
     88             final ComponentName itemComponentName =
     89                     new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
     90             final String itemPackageName = itemComponentName.getPackageName();
     91             // Exclude anything from our own package, and the old Launcher,
     92             // and live wallpaper picker
     93             if (itemPackageName.equals(context.getPackageName()) ||
     94                     itemPackageName.equals("com.android.launcher") ||
     95                     itemPackageName.equals("com.android.wallpaper.livepicker")) {
     96                 continue;
     97             }
     98             // Exclude any package that already responds to the image picker intent
     99             for (ResolveInfo imagePickerActivityInfo : imagePickerActivities) {
    100                 if (itemPackageName.equals(
    101                         imagePickerActivityInfo.activityInfo.packageName)) {
    102                     continue outerLoop;
    103                 }
    104             }
    105             mThirdPartyWallpaperPickers.add(new ThirdPartyWallpaperTile(info));
    106         }
    107     }
    108 
    109     public int getCount() {
    110         return mThirdPartyWallpaperPickers.size();
    111     }
    112 
    113     public ThirdPartyWallpaperTile getItem(int position) {
    114         return mThirdPartyWallpaperPickers.get(position);
    115     }
    116 
    117     public long getItemId(int position) {
    118         return position;
    119     }
    120 
    121     public View getView(int position, View convertView, ViewGroup parent) {
    122         View view;
    123 
    124         if (convertView == null) {
    125             view = mInflater.inflate(R.layout.wallpaper_picker_third_party_item, parent, false);
    126         } else {
    127             view = convertView;
    128         }
    129 
    130         ResolveInfo info = mThirdPartyWallpaperPickers.get(position).mResolveInfo;
    131         TextView label = (TextView) view.findViewById(R.id.wallpaper_item_label);
    132         label.setText(info.loadLabel(mPackageManager));
    133         Drawable icon = info.loadIcon(mPackageManager);
    134         icon.setBounds(new Rect(0, 0, mIconSize, mIconSize));
    135         label.setCompoundDrawables(null, icon, null, null);
    136         return view;
    137     }
    138 }
    139