Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.pm.PackageManager; |
| 21 | import android.gsi.GsiProgress; |
| 22 | import android.gsi.IGsiService; |
| 23 | import android.os.IBinder; |
| 24 | import android.os.IBinder.DeathRecipient; |
| 25 | import android.os.IDynamicAndroidService; |
| 26 | import android.os.RemoteException; |
| 27 | import android.os.ServiceManager; |
| 28 | import android.util.Slog; |
| 29 | |
| 30 | /** |
| 31 | * DynamicAndroidService implements IDynamicAndroidService. It provides permission check before |
| 32 | * passing requests to gsid |
| 33 | */ |
| 34 | public class DynamicAndroidService extends IDynamicAndroidService.Stub implements DeathRecipient { |
| 35 | private static final String TAG = "DynamicAndroidService"; |
| 36 | private static final String NO_SERVICE_ERROR = "no gsiservice"; |
| 37 | |
| 38 | private Context mContext; |
| 39 | private volatile IGsiService mGsiService; |
| 40 | |
| 41 | DynamicAndroidService(Context context) { |
| 42 | mContext = context; |
| 43 | } |
| 44 | |
| 45 | private static IGsiService connect(DeathRecipient recipient) throws RemoteException { |
| 46 | IBinder binder = ServiceManager.getService("gsiservice"); |
| 47 | if (binder == null) { |
| 48 | throw new RemoteException(NO_SERVICE_ERROR); |
| 49 | } |
| 50 | /** |
| 51 | * The init will restart gsiservice if it crashed and the proxy object will need to be |
| 52 | * re-initialized in this case. |
| 53 | */ |
| 54 | binder.linkToDeath(recipient, 0); |
| 55 | return IGsiService.Stub.asInterface(binder); |
| 56 | } |
| 57 | |
| 58 | /** implements DeathRecipient */ |
| 59 | @Override |
| 60 | public void binderDied() { |
| 61 | Slog.w(TAG, "gsiservice died; reconnecting"); |
| 62 | synchronized (this) { |
| 63 | mGsiService = null; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | private IGsiService getGsiService() throws RemoteException { |
| 68 | checkPermission(); |
| 69 | synchronized (this) { |
| 70 | if (mGsiService == null) { |
| 71 | mGsiService = connect(this); |
| 72 | } |
| 73 | return mGsiService; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | private void checkPermission() { |
| 78 | if (mContext.checkCallingOrSelfPermission( |
| 79 | android.Manifest.permission.MANAGE_DYNAMIC_ANDROID) |
| 80 | != PackageManager.PERMISSION_GRANTED) { |
| 81 | throw new SecurityException("Requires MANAGE_DYNAMIC_ANDROID permission"); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public boolean startInstallation(long systemSize, long userdataSize) throws RemoteException { |
| 87 | return getGsiService().startGsiInstall(systemSize, userdataSize, true) == 0; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public GsiProgress getInstallationProgress() throws RemoteException { |
| 92 | return getGsiService().getInstallProgress(); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public boolean abort() throws RemoteException { |
| 97 | return getGsiService().cancelGsiInstall(); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public boolean isInUse() throws RemoteException { |
| 102 | return getGsiService().isGsiRunning(); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public boolean isInstalled() throws RemoteException { |
| 107 | return getGsiService().isGsiInstalled(); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public boolean remove() throws RemoteException { |
| 112 | return getGsiService().removeGsiInstall(); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public boolean toggle() throws RemoteException { |
| 117 | IGsiService gsiService = getGsiService(); |
| 118 | if (gsiService.isGsiRunning()) { |
| 119 | return gsiService.disableGsiInstall(); |
| 120 | } else { |
| 121 | return gsiService.setGsiBootable() == 0; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public boolean write(byte[] buf) throws RemoteException { |
| 127 | return getGsiService().commitGsiChunkFromMemory(buf); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public boolean commit() throws RemoteException { |
| 132 | return getGsiService().setGsiBootable() == 0; |
| 133 | } |
| 134 | } |