KeyguardActivityLauncher.java revision 47cde77bc7ac5c3a1e486691596a7534ad855ff2
1/* 2 * Copyright (C) 2012 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.internal.policy.impl.keyguard; 18 19import android.app.ActivityManagerNative; 20import android.content.ActivityNotFoundException; 21import android.content.Context; 22import android.content.Intent; 23import android.content.pm.PackageManager; 24import android.content.pm.ResolveInfo; 25import android.os.RemoteException; 26import android.os.UserHandle; 27import android.provider.MediaStore; 28import android.util.Log; 29import android.view.WindowManager; 30 31import com.android.internal.widget.LockPatternUtils; 32 33import java.util.List; 34 35public abstract class KeyguardActivityLauncher { 36 private static final String TAG = KeyguardActivityLauncher.class.getSimpleName(); 37 private static final boolean DEBUG = KeyguardHostView.DEBUG; 38 private static final String META_DATA_KEYGUARD_LAYOUT = "com.android.keyguard.layout"; 39 private static final Intent SECURE_CAMERA_INTENT = 40 new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE) 41 .addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 42 private static final Intent INSECURE_CAMERA_INTENT = 43 new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA); 44 45 abstract Context getContext(); 46 47 abstract KeyguardSecurityCallback getCallback(); 48 49 abstract LockPatternUtils getLockPatternUtils(); 50 51 public static class CameraWidgetInfo { 52 public String contextPackage; 53 public int layoutId; 54 } 55 56 public CameraWidgetInfo getCameraWidgetInfo() { 57 CameraWidgetInfo info = new CameraWidgetInfo(); 58 Intent intent = getCameraIntent(); 59 PackageManager packageManager = getContext().getPackageManager(); 60 final List<ResolveInfo> appList = packageManager.queryIntentActivitiesAsUser( 61 intent, PackageManager.MATCH_DEFAULT_ONLY, getLockPatternUtils().getCurrentUser()); 62 if (appList.size() == 0) { 63 if (DEBUG) Log.d(TAG, "getCameraWidgetInfo(): Nothing found"); 64 return null; 65 } 66 ResolveInfo resolved = packageManager.resolveActivityAsUser(intent, 67 PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_META_DATA, 68 getLockPatternUtils().getCurrentUser()); 69 if (DEBUG) Log.d(TAG, "getCameraWidgetInfo(): resolved: " + resolved); 70 if (wouldLaunchResolverActivity(resolved, appList)) { 71 if (DEBUG) Log.d(TAG, "getCameraWidgetInfo(): Would launch resolver"); 72 return info; 73 } 74 if (resolved == null || resolved.activityInfo == null) { 75 return null; 76 } 77 if (resolved.activityInfo.metaData == null || resolved.activityInfo.metaData.isEmpty()) { 78 if (DEBUG) Log.d(TAG, "getCameraWidgetInfo(): no metadata found"); 79 return info; 80 } 81 int layoutId = resolved.activityInfo.metaData.getInt(META_DATA_KEYGUARD_LAYOUT); 82 if (layoutId == 0) { 83 if (DEBUG) Log.d(TAG, "getCameraWidgetInfo(): no layout specified"); 84 return info; 85 } 86 info.contextPackage = resolved.activityInfo.packageName; 87 info.layoutId = layoutId; 88 return info; 89 } 90 91 public void launchCamera() { 92 LockPatternUtils lockPatternUtils = getLockPatternUtils(); 93 if (lockPatternUtils.isSecure()) { 94 // Launch the secure version of the camera 95 if (wouldLaunchResolverActivity(SECURE_CAMERA_INTENT)) { 96 // TODO: Show disambiguation dialog instead. 97 // For now, we'll treat this like launching any other app from secure keyguard. 98 // When they do, user sees the system's ResolverActivity which lets them choose 99 // which secure camera to use. 100 launchActivity(SECURE_CAMERA_INTENT, false); 101 } else { 102 launchActivity(SECURE_CAMERA_INTENT, true); 103 } 104 } else { 105 // Launch the normal camera 106 launchActivity(INSECURE_CAMERA_INTENT, false); 107 } 108 } 109 110 /** 111 * Launches the said intent for the current foreground user. 112 * @param intent 113 * @param showsWhileLocked true if the activity can be run on top of keyguard. 114 * See {@link WindowManager#FLAG_SHOW_WHEN_LOCKED} 115 */ 116 public void launchActivity(final Intent intent, boolean showsWhileLocked) { 117 final Context context = getContext(); 118 LockPatternUtils lockPatternUtils = getLockPatternUtils(); 119 intent.addFlags( 120 Intent.FLAG_ACTIVITY_NEW_TASK 121 | Intent.FLAG_ACTIVITY_SINGLE_TOP 122 | Intent.FLAG_ACTIVITY_CLEAR_TOP); 123 boolean isSecure = lockPatternUtils.isSecure(); 124 if (!isSecure || showsWhileLocked) { 125 if (!isSecure) try { 126 ActivityManagerNative.getDefault().dismissKeyguardOnNextActivity(); 127 } catch (RemoteException e) { 128 Log.w(TAG, "can't dismiss keyguard on launch"); 129 } 130 try { 131 context.startActivityAsUser(intent, new UserHandle(UserHandle.USER_CURRENT)); 132 } catch (ActivityNotFoundException e) { 133 Log.w(TAG, "Activity not found for intent + " + intent.getAction()); 134 } 135 } else { 136 // Create a runnable to start the activity and ask the user to enter their 137 // credentials. 138 KeyguardSecurityCallback callback = getCallback(); 139 callback.setOnDismissRunnable(new Runnable() { 140 @Override 141 public void run() { 142 context.startActivityAsUser(intent, new UserHandle(UserHandle.USER_CURRENT)); 143 } 144 }); 145 callback.dismiss(false); 146 } 147 } 148 149 private Intent getCameraIntent() { 150 return getLockPatternUtils().isSecure() ? SECURE_CAMERA_INTENT : INSECURE_CAMERA_INTENT; 151 } 152 153 private boolean wouldLaunchResolverActivity(Intent intent) { 154 PackageManager packageManager = getContext().getPackageManager(); 155 ResolveInfo resolved = packageManager.resolveActivityAsUser(intent, 156 PackageManager.MATCH_DEFAULT_ONLY, getLockPatternUtils().getCurrentUser()); 157 List<ResolveInfo> appList = packageManager.queryIntentActivitiesAsUser( 158 intent, PackageManager.MATCH_DEFAULT_ONLY, getLockPatternUtils().getCurrentUser()); 159 return wouldLaunchResolverActivity(resolved, appList); 160 } 161 162 private boolean wouldLaunchResolverActivity(ResolveInfo resolved, List<ResolveInfo> appList) { 163 // If the list contains the above resolved activity, then it can't be 164 // ResolverActivity itself. 165 for (int i = 0; i < appList.size(); i++) { 166 ResolveInfo tmp = appList.get(i); 167 if (tmp.activityInfo.name.equals(resolved.activityInfo.name) 168 && tmp.activityInfo.packageName.equals(resolved.activityInfo.packageName)) { 169 return false; 170 } 171 } 172 return true; 173 } 174} 175