blob: 7f33973c8d5cc3258b457f8b6132f6eaea42d159 [file] [log] [blame]
Christopher Tate8662cab52012-02-23 14:59:36 -08001/*
2 * Copyright (C) 2012 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.server;
18
19import android.content.Context;
20import android.content.Intent;
21import android.content.pm.PackageManager;
22import android.os.Binder;
23import android.os.Handler;
24import android.os.IBinder;
25import android.os.IUpdateLock;
26import android.os.RemoteException;
Christopher Tate8662cab52012-02-23 14:59:36 -080027import android.os.TokenWatcher;
28import android.os.UpdateLock;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070029import android.os.UserHandle;
Christopher Tate8662cab52012-02-23 14:59:36 -080030import android.util.Slog;
31
32import java.io.FileDescriptor;
33import java.io.PrintWriter;
34
35public class UpdateLockService extends IUpdateLock.Stub {
36 static final boolean DEBUG = false;
37 static final String TAG = "UpdateLockService";
38
39 // signatureOrSystem required to use update locks
40 static final String PERMISSION = "android.permission.UPDATE_LOCK";
41
42 Context mContext;
43 LockWatcher mLocks;
44
45 class LockWatcher extends TokenWatcher {
46 LockWatcher(Handler h, String tag) {
47 super(h, tag);
48 }
49
50 public void acquired() {
51 if (DEBUG) {
52 Slog.d(TAG, "first acquire; broadcasting convenient=false");
53 }
54 sendLockChangedBroadcast(false);
55 }
56 public void released() {
57 if (DEBUG) {
58 Slog.d(TAG, "last release; broadcasting convenient=true");
59 }
60 sendLockChangedBroadcast(true);
61 }
62 }
63
64 UpdateLockService(Context context) {
65 mContext = context;
66 mLocks = new LockWatcher(new Handler(), "UpdateLocks");
67
68 // Consider just-booting to be a reasonable time to allow
69 // interruptions for update installation etc.
70 sendLockChangedBroadcast(true);
71 }
72
73 void sendLockChangedBroadcast(boolean state) {
74 // Safe early during boot because this broadcast only goes to registered receivers.
75 long oldIdent = Binder.clearCallingIdentity();
76 try {
77 Intent intent = new Intent(UpdateLock.UPDATE_LOCK_CHANGED)
78 .putExtra(UpdateLock.NOW_IS_CONVENIENT, state)
79 .putExtra(UpdateLock.TIMESTAMP, System.currentTimeMillis())
Christopher Tate5bb59da2012-03-01 12:39:25 -080080 .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070081 mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
Christopher Tate8662cab52012-02-23 14:59:36 -080082 } finally {
83 Binder.restoreCallingIdentity(oldIdent);
84 }
85 }
86
87 @Override
88 public void acquireUpdateLock(IBinder token, String tag) throws RemoteException {
89 if (DEBUG) {
90 Slog.d(TAG, "acquire(" + token + ") by " + makeTag(tag));
91 }
92
93 mContext.enforceCallingOrSelfPermission(PERMISSION, "acquireUpdateLock");
94 mLocks.acquire(token, makeTag(tag));
95 }
96
97 @Override
98 public void releaseUpdateLock(IBinder token) throws RemoteException {
99 if (DEBUG) {
100 Slog.d(TAG, "release(" + token + ')');
101 }
102
103 mContext.enforceCallingOrSelfPermission(PERMISSION, "releaseUpdateLock");
104 mLocks.release(token);
105 };
106
107 private String makeTag(String tag) {
108 return "{tag=" + tag
109 + " uid=" + Binder.getCallingUid()
110 + " pid=" + Binder.getCallingPid() + '}';
111 }
112
113 @Override
114 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
115 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
116 != PackageManager.PERMISSION_GRANTED) {
117 pw.println("Permission Denial: can't dump update lock service from from pid="
118 + Binder.getCallingPid()
119 + ", uid=" + Binder.getCallingUid());
120 return;
121 }
122
123 mLocks.dump(pw);
124 }
125}