Home | History | Annotate | Download | only in password
      1 
      2 /*
      3  * Copyright (C) 2014 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package com.android.settings.password;
     19 
     20 import android.app.Activity;
     21 import android.app.KeyguardManager;
     22 import android.app.admin.DevicePolicyManager;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.os.Bundle;
     26 import android.os.UserManager;
     27 import android.util.Log;
     28 
     29 import com.android.internal.widget.LockPatternUtils;
     30 import com.android.settings.Utils;
     31 
     32 /**
     33  * Launch this when you want to confirm the user is present by asking them to enter their
     34  * PIN/password/pattern.
     35  */
     36 public class ConfirmDeviceCredentialActivity extends Activity {
     37     public static final String TAG = ConfirmDeviceCredentialActivity.class.getSimpleName();
     38 
     39     public static class InternalActivity extends ConfirmDeviceCredentialActivity {
     40     }
     41 
     42     public static Intent createIntent(CharSequence title, CharSequence details) {
     43         Intent intent = new Intent();
     44         intent.setClassName("com.android.settings",
     45                 ConfirmDeviceCredentialActivity.class.getName());
     46         intent.putExtra(KeyguardManager.EXTRA_TITLE, title);
     47         intent.putExtra(KeyguardManager.EXTRA_DESCRIPTION, details);
     48         return intent;
     49     }
     50 
     51     public static Intent createIntent(CharSequence title, CharSequence details, long challenge) {
     52         Intent intent = new Intent();
     53         intent.setClassName("com.android.settings",
     54                 ConfirmDeviceCredentialActivity.class.getName());
     55         intent.putExtra(KeyguardManager.EXTRA_TITLE, title);
     56         intent.putExtra(KeyguardManager.EXTRA_DESCRIPTION, details);
     57         intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE, challenge);
     58         intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE, true);
     59         return intent;
     60     }
     61 
     62     @Override
     63     public void onCreate(Bundle savedInstanceState) {
     64         super.onCreate(savedInstanceState);
     65 
     66         Intent intent = getIntent();
     67         String title = intent.getStringExtra(KeyguardManager.EXTRA_TITLE);
     68         String details = intent.getStringExtra(KeyguardManager.EXTRA_DESCRIPTION);
     69         String alternateButton = intent.getStringExtra(
     70                 KeyguardManager.EXTRA_ALTERNATE_BUTTON_LABEL);
     71         boolean frp = KeyguardManager.ACTION_CONFIRM_FRP_CREDENTIAL.equals(intent.getAction());
     72 
     73         int userId = Utils.getCredentialOwnerUserId(this);
     74         if (isInternalActivity()) {
     75             try {
     76                 userId = Utils.getUserIdFromBundle(this, intent.getExtras());
     77             } catch (SecurityException se) {
     78                 Log.e(TAG, "Invalid intent extra", se);
     79             }
     80         }
     81         final boolean isManagedProfile = UserManager.get(this).isManagedProfile(userId);
     82         // if the client app did not hand in a title and we are about to show the work challenge,
     83         // check whether there is a policy setting the organization name and use that as title
     84         if ((title == null) && isManagedProfile) {
     85             title = getTitleFromOrganizationName(userId);
     86         }
     87         ChooseLockSettingsHelper helper = new ChooseLockSettingsHelper(this);
     88         final LockPatternUtils lockPatternUtils = new LockPatternUtils(this);
     89         boolean launched;
     90         // If the target is a managed user and user key not unlocked yet, we will force unlock
     91         // tied profile so it will enable work mode and unlock managed profile, when personal
     92         // challenge is unlocked.
     93         if (frp) {
     94             launched = helper.launchFrpConfirmationActivity(0, title, details, alternateButton);
     95         } else if (isManagedProfile && isInternalActivity()
     96                 && !lockPatternUtils.isSeparateProfileChallengeEnabled(userId)) {
     97             // We set the challenge as 0L, so it will force to unlock managed profile when it
     98             // unlocks primary profile screen lock, by calling verifyTiedProfileChallenge()
     99             launched = helper.launchConfirmationActivityWithExternalAndChallenge(
    100                     0 /* request code */, null /* title */, title, details, true /* isExternal */,
    101                     0L /* challenge */, userId);
    102         } else {
    103             launched = helper.launchConfirmationActivity(0 /* request code */, null /* title */,
    104                     title, details, false /* returnCredentials */, true /* isExternal */, userId);
    105         }
    106         if (!launched) {
    107             Log.d(TAG, "No pattern, password or PIN set.");
    108             setResult(Activity.RESULT_OK);
    109         }
    110         finish();
    111     }
    112 
    113     private boolean isInternalActivity() {
    114         return this instanceof ConfirmDeviceCredentialActivity.InternalActivity;
    115     }
    116 
    117     private String getTitleFromOrganizationName(int userId) {
    118         DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(
    119                 Context.DEVICE_POLICY_SERVICE);
    120         CharSequence organizationNameForUser = (dpm != null)
    121                 ? dpm.getOrganizationNameForUser(userId) : null;
    122         return organizationNameForUser != null ? organizationNameForUser.toString() : null;
    123     }
    124 }
    125