1/*
2 * Copyright 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.managedprovisioning.task;
18
19import static com.android.internal.util.Preconditions.checkNotNull;
20
21import android.content.Context;
22import android.content.pm.PackageManager;
23
24import com.android.managedprovisioning.common.ProvisionLogger;
25import com.android.managedprovisioning.R;
26import com.android.managedprovisioning.model.ProvisioningParams;
27
28/**
29 * Task to install an existing package on a given user.
30 */
31public class InstallExistingPackageTask extends AbstractProvisioningTask {
32
33    private final String mPackageName;
34
35    public InstallExistingPackageTask(
36            String packageName,
37            Context context,
38            ProvisioningParams params,
39            Callback callback) {
40        super(context, params, callback);
41
42        mPackageName = checkNotNull(packageName);
43    }
44
45    public int getStatusMsgId() {
46        return R.string.progress_install;
47    }
48
49    @Override
50    public void run(int userId) {
51        PackageManager pm = mContext.getPackageManager();
52        try {
53            int status = pm.installExistingPackageAsUser(mPackageName, userId);
54            if (status == PackageManager.INSTALL_SUCCEEDED) {
55                success();
56            } else {
57                ProvisionLogger.loge("Install failed, result code = " + status);
58                error(0);
59            }
60        } catch (PackageManager.NameNotFoundException e) {
61            error(0);
62        }
63
64    }
65}
66