1/* 2 * Copyright (C) 2016 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 17package com.android.launcher3.compat; 18 19import android.content.ComponentName; 20import android.content.Context; 21import android.content.Intent; 22import android.content.pm.ApplicationInfo; 23import android.graphics.drawable.Drawable; 24import android.util.Log; 25 26/** 27 * {@link LauncherActivityInfoCompat} which loads its data only when needed. 28 */ 29public class DeferredLauncherActivityInfo extends LauncherActivityInfoCompat { 30 31 private final ComponentName mComponent; 32 private final UserHandleCompat mUser; 33 private final Context mContext; 34 35 private LauncherActivityInfoCompat mActualInfo; 36 37 public DeferredLauncherActivityInfo( 38 ComponentName component, UserHandleCompat user, Context context) { 39 mComponent = component; 40 mUser = user; 41 mContext = context; 42 } 43 44 @Override 45 public ComponentName getComponentName() { 46 return mComponent; 47 } 48 49 @Override 50 public UserHandleCompat getUser() { 51 return mUser; 52 } 53 54 private synchronized LauncherActivityInfoCompat getActualInfo() { 55 if (mActualInfo == null) { 56 Intent intent = new Intent(Intent.ACTION_MAIN) 57 .addCategory(Intent.CATEGORY_LAUNCHER) 58 .setComponent(mComponent); 59 mActualInfo = LauncherAppsCompat.getInstance(mContext).resolveActivity(intent, mUser); 60 } 61 return mActualInfo; 62 } 63 64 @Override 65 public CharSequence getLabel() { 66 return getActualInfo().getLabel(); 67 } 68 69 @Override 70 public Drawable getIcon(int density) { 71 return getActualInfo().getIcon(density); 72 } 73 74 @Override 75 public ApplicationInfo getApplicationInfo() { 76 return getActualInfo().getApplicationInfo(); 77 } 78 79 @Override 80 public long getFirstInstallTime() { 81 return getActualInfo().getFirstInstallTime(); 82 } 83} 84