PreviewInflater.java revision 781c29c9a866dbf0a01d76838e912a5fec9247fe
1/* 2 * Copyright (C) 2014 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.systemui.statusbar.policy; 18 19import android.content.Context; 20import android.content.Intent; 21import android.content.pm.PackageManager; 22import android.content.pm.ResolveInfo; 23import android.content.pm.UserInfo; 24import android.os.UserHandle; 25import android.util.Log; 26import android.view.LayoutInflater; 27import android.view.View; 28 29import com.android.internal.widget.LockPatternUtils; 30import com.android.keyguard.KeyguardActivityLauncher; 31import com.android.systemui.statusbar.phone.KeyguardPreviewContainer; 32 33import java.util.List; 34 35/** 36 * Utility class to inflate previews for phone and camera affordance. 37 */ 38public class PreviewInflater { 39 40 private static final String TAG = "PreviewInflater"; 41 42 private static final String META_DATA_KEYGUARD_LAYOUT = "com.android.keyguard.layout"; 43 44 private Context mContext; 45 private LockPatternUtils mLockPatternUtils; 46 47 public PreviewInflater(Context context, LockPatternUtils lockPatternUtils) { 48 mContext = context; 49 mLockPatternUtils = lockPatternUtils; 50 } 51 52 public View inflatePreview(Intent intent) { 53 WidgetInfo info = getWidgetInfo(intent); 54 if (info == null) { 55 return null; 56 } 57 View v = inflateWidgetView(info); 58 if (v == null) { 59 return null; 60 } 61 KeyguardPreviewContainer container = new KeyguardPreviewContainer(mContext, null); 62 container.addView(v); 63 return container; 64 } 65 66 private View inflateWidgetView(WidgetInfo widgetInfo) { 67 View widgetView = null; 68 try { 69 Context appContext = mContext.createPackageContext( 70 widgetInfo.contextPackage, Context.CONTEXT_RESTRICTED); 71 LayoutInflater appInflater = (LayoutInflater) 72 appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 73 appInflater = appInflater.cloneInContext(appContext); 74 widgetView = appInflater.inflate(widgetInfo.layoutId, null, false); 75 } catch (PackageManager.NameNotFoundException|RuntimeException e) { 76 Log.w(TAG, "Error creating widget view", e); 77 } 78 return widgetView; 79 } 80 81 private WidgetInfo getWidgetInfo(Intent intent) { 82 WidgetInfo info = new WidgetInfo(); 83 PackageManager packageManager = mContext.getPackageManager(); 84 final List<ResolveInfo> appList = packageManager.queryIntentActivitiesAsUser( 85 intent, PackageManager.MATCH_DEFAULT_ONLY, mLockPatternUtils.getCurrentUser()); 86 if (appList.size() == 0) { 87 return null; 88 } 89 ResolveInfo resolved = packageManager.resolveActivityAsUser(intent, 90 PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_META_DATA, 91 mLockPatternUtils.getCurrentUser()); 92 if (wouldLaunchResolverActivity(resolved, appList)) { 93 return null; 94 } 95 if (resolved == null || resolved.activityInfo == null) { 96 return null; 97 } 98 if (resolved.activityInfo.metaData == null || resolved.activityInfo.metaData.isEmpty()) { 99 return null; 100 } 101 int layoutId = resolved.activityInfo.metaData.getInt(META_DATA_KEYGUARD_LAYOUT); 102 if (layoutId == 0) { 103 return null; 104 } 105 info.contextPackage = resolved.activityInfo.packageName; 106 info.layoutId = layoutId; 107 return info; 108 } 109 110 public boolean wouldLaunchResolverActivity(Intent intent) { 111 PackageManager packageManager = mContext.getPackageManager(); 112 final List<ResolveInfo> appList = packageManager.queryIntentActivitiesAsUser( 113 intent, PackageManager.MATCH_DEFAULT_ONLY, mLockPatternUtils.getCurrentUser()); 114 if (appList.size() == 0) { 115 return false; 116 } 117 ResolveInfo resolved = packageManager.resolveActivityAsUser(intent, 118 PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_META_DATA, 119 mLockPatternUtils.getCurrentUser()); 120 return wouldLaunchResolverActivity(resolved, appList); 121 } 122 123 private boolean wouldLaunchResolverActivity(ResolveInfo resolved, List<ResolveInfo> appList) { 124 // If the list contains the above resolved activity, then it can't be 125 // ResolverActivity itself. 126 for (int i = 0; i < appList.size(); i++) { 127 ResolveInfo tmp = appList.get(i); 128 if (tmp.activityInfo.name.equals(resolved.activityInfo.name) 129 && tmp.activityInfo.packageName.equals(resolved.activityInfo.packageName)) { 130 return false; 131 } 132 } 133 return true; 134 } 135 136 private static class WidgetInfo { 137 String contextPackage; 138 int layoutId; 139 } 140} 141