Home | History | Annotate | Download | only in settings
      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;
     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 
     31 /**
     32  * Launch this when you want to confirm the user is present by asking them to enter their
     33  * PIN/password/pattern.
     34  */
     35 public class ConfirmDeviceCredentialActivity extends Activity {
     36     public static final String TAG = ConfirmDeviceCredentialActivity.class.getSimpleName();
     37 
     38     public static class InternalActivity extends ConfirmDeviceCredentialActivity {
     39     }
     40 
     41     public static Intent createIntent(CharSequence title, CharSequence details) {
     42         Intent intent = new Intent();
     43         intent.setClassName("com.android.settings",
     44                 ConfirmDeviceCredentialActivity.class.getName());
     45         intent.putExtra(KeyguardManager.EXTRA_TITLE, title);
     46         intent.putExtra(KeyguardManager.EXTRA_DESCRIPTION, details);
     47         return intent;
     48     }
     49 
     50     public static Intent createIntent(CharSequence title, CharSequence details, long challenge) {
     51         Intent intent = new Intent();
     52         intent.setClassName("com.android.settings",
     53                 ConfirmDeviceCredentialActivity.class.getName());
     54         intent.putExtra(KeyguardManager.EXTRA_TITLE, title);
     55         intent.putExtra(KeyguardManager.EXTRA_DESCRIPTION, details);
     56         intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE, challenge);
     57         intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE, true);
     58         return intent;
     59     }
     60 
     61     @Override
     62     public void onCreate(Bundle savedInstanceState) {
     63         super.onCreate(savedInstanceState);
     64 
     65         Intent intent = getIntent();
     66         String title = intent.getStringExtra(KeyguardManager.EXTRA_TITLE);
     67         String details = intent.getStringExtra(KeyguardManager.EXTRA_DESCRIPTION);
     68         int userId = Utils.getCredentialOwnerUserId(this);
     69         if (isInternalActivity()) {
     70             try {
     71                 userId = Utils.getUserIdFromBundle(this, intent.getExtras());
     72             } catch (SecurityException se) {
     73                 Log.e(TAG, "Invalid intent extra", se);
     74             }
     75         }
     76         final boolean isManagedProfile = UserManager.get(this).isManagedProfile(userId);
     77         // if the client app did not hand in a title and we are about to show the work challenge,
     78         // check whether there is a policy setting the organization name and use that as title
     79         if ((title == null) && isManagedProfile) {
     80             title = getTitleFromOrganizationName(userId);
     81         }
     82         ChooseLockSettingsHelper helper = new ChooseLockSettingsHelper(this);
     83         final LockPatternUtils lockPatternUtils = new LockPatternUtils(this);
     84         boolean launched;
     85         // If the target is a managed user and user key not unlocked yet, we will force unlock
     86         // tied profile so it will enable work mode and unlock managed profile, when personal
     87         // challenge is unlocked.
     88         if (isManagedProfile && isInternalActivity()
     89                 && !lockPatternUtils.isSeparateProfileChallengeEnabled(userId)) {
     90             // We set the challenge as 0L, so it will force to unlock managed profile when it
     91             // unlocks primary profile screen lock, by calling verifyTiedProfileChallenge()
     92             launched = helper.launchConfirmationActivityWithExternalAndChallenge(
     93                     0 /* request code */, null /* title */, title, details, true /* isExternal */,
     94                     0L /* challenge */, userId);
     95         } else {
     96             launched = helper.launchConfirmationActivity(0 /* request code */, null /* title */,
     97                     title, details, false /* returnCredentials */, true /* isExternal */, userId);
     98         }
     99         if (!launched) {
    100             Log.d(TAG, "No pattern, password or PIN set.");
    101             setResult(Activity.RESULT_OK);
    102         }
    103         finish();
    104     }
    105 
    106     private boolean isInternalActivity() {
    107         return this instanceof ConfirmDeviceCredentialActivity.InternalActivity;
    108     }
    109 
    110     private String getTitleFromOrganizationName(int userId) {
    111         DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(
    112                 Context.DEVICE_POLICY_SERVICE);
    113         CharSequence organizationNameForUser = (dpm != null)
    114                 ? dpm.getOrganizationNameForUser(userId) : null;
    115         return organizationNameForUser != null ? organizationNameForUser.toString() : null;
    116     }
    117 }
    118