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.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.pm.ActivityInfo;
23import android.content.pm.PackageManager;
24import android.content.pm.ResolveInfo;
25import android.os.Bundle;
26import android.util.Log;
27import android.view.LayoutInflater;
28import android.view.View;
29
30import com.android.internal.widget.LockPatternUtils;
31import com.android.keyguard.KeyguardUpdateMonitor;
32import com.android.systemui.statusbar.phone.KeyguardPreviewContainer;
33
34import java.util.List;
35
36/**
37 * Utility class to inflate previews for phone and camera affordance.
38 */
39public class PreviewInflater {
40
41    private static final String TAG = "PreviewInflater";
42
43    private static final String META_DATA_KEYGUARD_LAYOUT = "com.android.keyguard.layout";
44
45    private Context mContext;
46    private LockPatternUtils mLockPatternUtils;
47
48    public PreviewInflater(Context context, LockPatternUtils lockPatternUtils) {
49        mContext = context;
50        mLockPatternUtils = lockPatternUtils;
51    }
52
53    public View inflatePreview(Intent intent) {
54        WidgetInfo info = getWidgetInfo(intent);
55        return inflatePreview(info);
56    }
57
58    public View inflatePreviewFromService(ComponentName componentName) {
59        WidgetInfo info = getWidgetInfoFromService(componentName);
60        return inflatePreview(info);
61    }
62
63    private KeyguardPreviewContainer inflatePreview(WidgetInfo info) {
64        if (info == null) {
65            return null;
66        }
67        View v = inflateWidgetView(info);
68        if (v == null) {
69            return null;
70        }
71        KeyguardPreviewContainer container = new KeyguardPreviewContainer(mContext, null);
72        container.addView(v);
73        return container;
74    }
75
76    private View inflateWidgetView(WidgetInfo widgetInfo) {
77        View widgetView = null;
78        try {
79            Context appContext = mContext.createPackageContext(
80                    widgetInfo.contextPackage, Context.CONTEXT_RESTRICTED);
81            LayoutInflater appInflater = (LayoutInflater)
82                    appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
83            appInflater = appInflater.cloneInContext(appContext);
84            widgetView = appInflater.inflate(widgetInfo.layoutId, null, false);
85        } catch (PackageManager.NameNotFoundException|RuntimeException e) {
86            Log.w(TAG, "Error creating widget view", e);
87        }
88        return widgetView;
89    }
90
91    private WidgetInfo getWidgetInfoFromService(ComponentName componentName) {
92        PackageManager packageManager = mContext.getPackageManager();
93        // Look for the preview specified in the service meta-data
94        try {
95            Bundle metaData = packageManager.getServiceInfo(
96                    componentName, PackageManager.GET_META_DATA).metaData;
97            return getWidgetInfoFromMetaData(componentName.getPackageName(), metaData);
98        } catch (PackageManager.NameNotFoundException e) {
99            Log.w(TAG, "Failed to load preview; " + componentName.flattenToShortString()
100                    + " not found", e);
101        }
102        return null;
103    }
104
105    private WidgetInfo getWidgetInfoFromMetaData(String contextPackage,
106            Bundle metaData) {
107        if (metaData == null) {
108            return null;
109        }
110        int layoutId = metaData.getInt(META_DATA_KEYGUARD_LAYOUT);
111        if (layoutId == 0) {
112            return null;
113        }
114        WidgetInfo info = new WidgetInfo();
115        info.contextPackage = contextPackage;
116        info.layoutId = layoutId;
117        return info;
118    }
119
120    private WidgetInfo getWidgetInfo(Intent intent) {
121        PackageManager packageManager = mContext.getPackageManager();
122        final List<ResolveInfo> appList = packageManager.queryIntentActivitiesAsUser(
123                intent, PackageManager.MATCH_DEFAULT_ONLY, KeyguardUpdateMonitor.getCurrentUser());
124        if (appList.size() == 0) {
125            return null;
126        }
127        ResolveInfo resolved = packageManager.resolveActivityAsUser(intent,
128                PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_META_DATA,
129                KeyguardUpdateMonitor.getCurrentUser());
130        if (wouldLaunchResolverActivity(resolved, appList)) {
131            return null;
132        }
133        if (resolved == null || resolved.activityInfo == null) {
134            return null;
135        }
136        return getWidgetInfoFromMetaData(resolved.activityInfo.packageName,
137                resolved.activityInfo.metaData);
138    }
139
140    public static boolean wouldLaunchResolverActivity(Context ctx, Intent intent,
141            int currentUserId) {
142        return getTargetActivityInfo(ctx, intent, currentUserId) == null;
143    }
144
145    /**
146     * @return the target activity info of the intent it resolves to a specific package or
147     *         {@code null} if it resolved to the resolver activity
148     */
149    public static ActivityInfo getTargetActivityInfo(Context ctx, Intent intent,
150            int currentUserId) {
151        PackageManager packageManager = ctx.getPackageManager();
152        final List<ResolveInfo> appList = packageManager.queryIntentActivitiesAsUser(
153                intent, PackageManager.MATCH_DEFAULT_ONLY, currentUserId);
154        if (appList.size() == 0) {
155            return null;
156        }
157        ResolveInfo resolved = packageManager.resolveActivityAsUser(intent,
158                PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_META_DATA, currentUserId);
159        if (resolved == null || wouldLaunchResolverActivity(resolved, appList)) {
160            return null;
161        } else {
162            return resolved.activityInfo;
163        }
164    }
165
166    private static boolean wouldLaunchResolverActivity(
167            ResolveInfo resolved, List<ResolveInfo> appList) {
168        // If the list contains the above resolved activity, then it can't be
169        // ResolverActivity itself.
170        for (int i = 0; i < appList.size(); i++) {
171            ResolveInfo tmp = appList.get(i);
172            if (tmp.activityInfo.name.equals(resolved.activityInfo.name)
173                    && tmp.activityInfo.packageName.equals(resolved.activityInfo.packageName)) {
174                return false;
175            }
176        }
177        return true;
178    }
179
180    private static class WidgetInfo {
181        String contextPackage;
182        int layoutId;
183    }
184}
185