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.accessibility.magnification; 18 19 import android.annotation.Nullable; 20 import android.graphics.Rect; 21 import android.os.IBinder; 22 import android.os.RemoteException; 23 import android.util.Slog; 24 import android.view.accessibility.IWindowMagnificationConnection; 25 import android.view.accessibility.IWindowMagnificationConnectionCallback; 26 27 import com.android.internal.annotations.VisibleForTesting; 28 29 /** 30 * A class to manipulate window magnification through {@link WindowMagnificationConnectionWrapper}. 31 */ 32 public final class WindowMagnificationManager { 33 34 private static final String TAG = "WindowMagnificationMgr"; 35 private final Object mLock = new Object(); 36 @VisibleForTesting 37 @Nullable WindowMagnificationConnectionWrapper mConnectionWrapper; 38 private ConnectionCallback mConnectionCallback; 39 40 /** 41 * Sets {@link IWindowMagnificationConnection}. 42 * @param connection {@link IWindowMagnificationConnection} 43 */ setConnection(@ullable IWindowMagnificationConnection connection)44 public void setConnection(@Nullable IWindowMagnificationConnection connection) { 45 synchronized (mLock) { 46 //Reset connectionWrapper. 47 if (mConnectionWrapper != null) { 48 mConnectionWrapper.setConnectionCallback(null); 49 if (mConnectionCallback != null) { 50 mConnectionCallback.mExpiredDeathRecipient = true; 51 } 52 mConnectionWrapper.unlinkToDeath(mConnectionCallback); 53 mConnectionWrapper = null; 54 } 55 if (connection != null) { 56 mConnectionWrapper = new WindowMagnificationConnectionWrapper(connection); 57 } 58 59 if (mConnectionWrapper != null) { 60 try { 61 mConnectionCallback = new ConnectionCallback(); 62 mConnectionWrapper.linkToDeath(mConnectionCallback); 63 mConnectionWrapper.setConnectionCallback(mConnectionCallback); 64 } catch (RemoteException e) { 65 Slog.e(TAG, "setConnection failed", e); 66 mConnectionWrapper = null; 67 } 68 } 69 } 70 } 71 72 private class ConnectionCallback extends IWindowMagnificationConnectionCallback.Stub implements 73 IBinder.DeathRecipient { 74 private boolean mExpiredDeathRecipient = false; 75 76 @Override onWindowMagnifierBoundsChanged(int display, Rect frame)77 public void onWindowMagnifierBoundsChanged(int display, Rect frame) throws RemoteException { 78 } 79 80 @Override onChangeMagnificationMode(int display, int magnificationMode)81 public void onChangeMagnificationMode(int display, int magnificationMode) 82 throws RemoteException { 83 } 84 85 @Override binderDied()86 public void binderDied() { 87 synchronized (mLock) { 88 if (mExpiredDeathRecipient) { 89 Slog.w(TAG, "binderDied DeathRecipient is expired"); 90 return; 91 } 92 mConnectionWrapper.unlinkToDeath(this); 93 mConnectionWrapper = null; 94 mConnectionCallback = null; 95 } 96 } 97 } 98 } 99