ConfirmUserCreationActivity.java revision 12747879b0204b9dfee997eddc981d09289e8b77
1/*
2 * Copyright (C) 2016 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.app;
18
19import android.accounts.Account;
20import android.accounts.AccountManager;
21import android.content.DialogInterface;
22import android.content.Intent;
23import android.content.pm.ApplicationInfo;
24import android.content.pm.PackageInfo;
25import android.content.pm.PackageManager.NameNotFoundException;
26import android.content.pm.UserInfo;
27import android.os.Bundle;
28import android.os.PersistableBundle;
29import android.os.UserManager;
30import android.util.Log;
31
32import com.android.internal.R;
33
34/**
35 * Activity to confirm with the user that it is ok to create a new user, as requested by
36 * an app. It has to do some checks to decide what kind of prompt the user should be shown.
37 * Particularly, it needs to check if the account requested already exists on another user.
38 */
39public class ConfirmUserCreationActivity extends AlertActivity
40        implements DialogInterface.OnClickListener {
41
42    private static final String TAG = "CreateUser";
43
44    private String mUserName;
45    private String mAccountName;
46    private String mAccountType;
47    private PersistableBundle mAccountOptions;
48    private boolean mCanProceed;
49    private UserManager mUserManager;
50
51    @Override
52    public void onCreate(Bundle icicle) {
53        super.onCreate(icicle);
54
55        Intent intent = getIntent();
56        mUserName = intent.getStringExtra(UserManager.EXTRA_USER_NAME);
57        mAccountName = intent.getStringExtra(UserManager.EXTRA_USER_ACCOUNT_NAME);
58        mAccountType = intent.getStringExtra(UserManager.EXTRA_USER_ACCOUNT_TYPE);
59        mAccountOptions = (PersistableBundle)
60                intent.getParcelableExtra(UserManager.EXTRA_USER_ACCOUNT_OPTIONS);
61
62        mUserManager = getSystemService(UserManager.class);
63
64        String message = checkUserCreationRequirements();
65
66        final AlertController.AlertParams ap = mAlertParams;
67        ap.mMessage = message;
68        ap.mPositiveButtonText = getString(android.R.string.ok);
69        ap.mPositiveButtonListener = this;
70
71        // Show the negative button if the user actually has a choice
72        if (mCanProceed) {
73            ap.mNegativeButtonText = getString(android.R.string.cancel);
74            ap.mNegativeButtonListener = this;
75        }
76        setupAlert();
77    }
78
79    private String checkUserCreationRequirements() {
80        final String callingPackage = getCallingPackage();
81        if (callingPackage == null) {
82            throw new SecurityException(
83                    "User Creation intent must be launched with startActivityForResult");
84        }
85        final ApplicationInfo appInfo;
86        try {
87            appInfo = getPackageManager().getApplicationInfo(callingPackage, 0);
88        } catch (NameNotFoundException nnfe) {
89            throw new SecurityException(
90                    "Cannot find the calling package");
91        }
92        final String message;
93        // Check the user restrictions
94        boolean cantCreateUser = mUserManager.hasUserRestriction(UserManager.DISALLOW_ADD_USER);
95        // Check the system state and user count
96        boolean cantCreateAnyMoreUsers = !mUserManager.canAddMoreUsers();
97        // Check the account existence
98        final Account account = new Account(mAccountName, mAccountType);
99        boolean accountExists = mAccountName != null && mAccountType != null
100                && (AccountManager.get(this).someUserHasAccount(account)
101                    | mUserManager.someUserHasSeedAccount(mAccountName, mAccountType));
102        mCanProceed = true;
103        final String appName = appInfo.loadLabel(getPackageManager()).toString();
104        if (cantCreateUser) {
105            message = getString(R.string.user_creation_cannot_add, appName);
106            mCanProceed = false;
107        } else if (cantCreateAnyMoreUsers) {
108            message = getString(R.string.user_creation_cannot_add_any_more, appName);
109            mCanProceed = false;
110        } else if (accountExists) {
111            message = getString(R.string.user_creation_account_exists, appName, mAccountName);
112        } else {
113            message = getString(R.string.user_creation_adding, appName, mAccountName);
114        }
115        return message;
116    }
117
118    @Override
119    public void onClick(DialogInterface dialog, int which) {
120        setResult(RESULT_CANCELED);
121        if (which == BUTTON_POSITIVE && mCanProceed) {
122            Log.i(TAG, "Ok, creating user");
123            UserInfo user = mUserManager.createUser(mUserName, 0);
124            if (user == null) {
125                Log.e(TAG, "Couldn't create user");
126                finish();
127                return;
128            }
129            mUserManager.setSeedAccountData(user.id, mAccountName, mAccountType, mAccountOptions);
130            setResult(RESULT_OK);
131        }
132        finish();
133    }
134}
135