1 /* 2 * Copyright (C) 2023 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.example.android.vdmdemo.host; 18 19 import android.app.WallpaperColors; 20 import android.app.WallpaperManager; 21 import android.content.ComponentName; 22 import android.content.Intent; 23 import android.content.pm.PackageManager; 24 import android.content.pm.PackageManager.ResolveInfoFlags; 25 import android.content.pm.ResolveInfo; 26 import android.graphics.Color; 27 import android.graphics.drawable.Drawable; 28 import android.graphics.drawable.ShapeDrawable; 29 import android.graphics.drawable.shapes.OvalShape; 30 import android.view.LayoutInflater; 31 import android.view.View; 32 import android.view.ViewGroup; 33 import android.widget.BaseAdapter; 34 import android.widget.ImageView; 35 import android.widget.TextView; 36 37 import java.util.ArrayList; 38 import java.util.List; 39 40 final class LauncherAdapter extends BaseAdapter { 41 42 private final List<ResolveInfo> mAvailableApps = new ArrayList<>(); 43 private final PackageManager mPackageManager; 44 private int mTextColor = Color.BLACK; 45 LauncherAdapter(PackageManager packageManager)46 LauncherAdapter(PackageManager packageManager) { 47 this(packageManager, null); 48 } 49 LauncherAdapter(PackageManager packageManager, WallpaperManager wallpaperManager)50 LauncherAdapter(PackageManager packageManager, WallpaperManager wallpaperManager) { 51 mPackageManager = packageManager; 52 53 if (wallpaperManager != null) { 54 WallpaperColors wallpaperColors = 55 wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_SYSTEM); 56 if ((wallpaperColors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) == 0) { 57 mTextColor = Color.WHITE; 58 } 59 } 60 61 mAvailableApps.addAll( 62 packageManager.queryIntentActivities( 63 new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER), 64 ResolveInfoFlags.of(PackageManager.MATCH_ALL))); 65 } 66 67 @Override getCount()68 public int getCount() { 69 return mAvailableApps.size(); 70 } 71 72 @Override getItem(int position)73 public Object getItem(int position) { 74 return mAvailableApps.get(position); 75 } 76 77 @Override getItemId(int position)78 public long getItemId(int position) { 79 return 0; 80 } 81 82 @Override getView(int position, View convertView, ViewGroup parent)83 public View getView(int position, View convertView, ViewGroup parent) { 84 final ResolveInfo ri = mAvailableApps.get(position); 85 final Drawable img = ri.loadIcon(mPackageManager); 86 if (convertView == null) { 87 convertView = 88 LayoutInflater.from(parent.getContext()) 89 .inflate(R.layout.launcher_grid_item, parent, false); 90 } 91 ImageView imageView = convertView.requireViewById(R.id.app_icon); 92 final Drawable background = new ShapeDrawable(new OvalShape()); 93 imageView.setBackground(background); 94 imageView.setImageDrawable(img); 95 96 TextView textView = convertView.requireViewById(R.id.app_title); 97 textView.setText(ri.loadLabel(mPackageManager)); 98 textView.setTextColor(mTextColor); 99 return convertView; 100 } 101 createPendingRemoteIntent(int position)102 public Intent createPendingRemoteIntent(int position) { 103 if (position >= mAvailableApps.size()) { 104 return null; 105 } 106 ResolveInfo ri = mAvailableApps.get(position); 107 if (ri == null) { 108 return null; 109 } 110 Intent intent = new Intent(Intent.ACTION_MAIN); 111 intent.addCategory(Intent.CATEGORY_LAUNCHER); 112 if (ri.activityInfo != null) { 113 intent.setComponent( 114 new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name)); 115 } else { 116 intent.setComponent(new ComponentName(ri.serviceInfo.packageName, ri.serviceInfo.name)); 117 } 118 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 119 return intent; 120 } 121 } 122