blob: 205de40af8a7b39a74d4e74cf41c76d051a6b922 [file] [log] [blame]
Joe Delfinod0f29282015-03-19 08:26:09 -04001/*
2 * Copyright 2015, 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;
18
19import android.app.admin.DevicePolicyManager;
20import android.app.Notification;
21import android.app.IntentService;
22import android.content.Context;
23import android.content.Intent;
24import android.os.IBinder;
25import android.os.UserHandle;
26
27/**
28 * Service for handling CA certs.
29 *
30 */
31public class CertService extends IntentService {
32 public static final String EXTRA_CA_CERT = "cacert";
33 public static final String EXTRA_REQUESTING_USER = "reqUser";
34 private static final String TAG = "CertService";
35
36 private static final int FOREGROUND_ID = 0xedc82;
37
38 private DevicePolicyManager mDevicePolicyManager;
39
40 public CertService() {
41 super(TAG);
42 }
43
44 public static void startService(Context context, Intent callingIntent) {
45 ProvisionLogger.logd("InstallCertService.startService");
46 Intent intent = new Intent(context, CertService.class);
47 intent.setAction(callingIntent.getAction());
48 intent.putExtras(callingIntent);
49 context.startService(intent);
50 }
51
52 /**
53 * Set this as a foreground service. This is done to help ensure that the
54 * service isn't killed by the OS.
55 */
56 private void startServiceInForeground() {
57 Notification notification = new Notification.Builder(this)
58 .setContentTitle(getString(R.string.provisioning))
59 .setContentText(getString(R.string.copying_certs))
60 .setSmallIcon(R.drawable.quantum_ic_https_white_24)
61 .build();
62 notification.flags |= Notification.FLAG_NO_CLEAR;
63 startForeground(FOREGROUND_ID, notification);
64 }
65
66 @Override
67 public IBinder onBind(Intent intent) {
68 return null;
69 }
70
71 @Override
72 public void onHandleIntent(final Intent intent) {
73 ProvisionLogger.logd("InstallCertService.onHandleIntent");
74
75 startServiceInForeground();
76
77 mDevicePolicyManager =
78 (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
79
80 if (InstallCertReceiver.INSTALL_CERT_ACTION.equals(intent.getAction())) {
81 installCert(intent.getByteArrayExtra(EXTRA_CA_CERT));
82 } else if (InstallCertRequestReceiver.REQUEST_CERT_ACTION.equals(
83 intent.getAction())) {
84 sendCerts((UserHandle) intent.getParcelableExtra(EXTRA_REQUESTING_USER));
85 }
86
87 stopForeground(true);
88 }
89
90 /**
91 * Installs the given ca cert.
92 */
93 private void installCert(final byte[] certToInstall) {
94 ProvisionLogger.logi("Installing CA cert");
95
96 mDevicePolicyManager.installCaCert(null, certToInstall);
97 }
98
99 /**
100 * Sends all user installed ca certs for this user to the given user.
101 */
102 private void sendCerts(final UserHandle handle) {
103 ProvisionLogger.logi("Sending CA certs");
104
105 for (byte[] cert : mDevicePolicyManager.getInstalledCaCerts(null)) {
106 Intent intent = new Intent(InstallCertReceiver.INSTALL_CERT_ACTION);
107 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
108 intent.putExtra(EXTRA_CA_CERT, cert);
109 sendBroadcastAsUser(intent, handle);
110 }
111
112 }
113}