OSDN Git Service

DO NOT MERGE. Grant MMS Uri permissions as the calling UID.
[android-x86/frameworks-base.git] / tests / utils / testutils / java / com / android / internal / util / test / BidirectionalAsyncChannelServer.java
1 /*
2  * Copyright (C) 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
17 package com.android.internal.util.test;
18
19 import android.content.Context;
20 import android.os.Handler;
21 import android.os.Looper;
22 import android.os.Message;
23 import android.os.Messenger;
24 import android.util.Log;
25
26 import com.android.internal.util.AsyncChannel;
27
28 import java.util.HashMap;
29 import java.util.Map;
30
31 /**
32  * Provides an interface for the server side implementation of a bidirectional channel as described
33  * in {@link com.android.internal.util.AsyncChannel}.
34  */
35 public class BidirectionalAsyncChannelServer {
36
37     private static final String TAG = "BidirectionalAsyncChannelServer";
38
39     // Keeps track of incoming clients, which are identifiable by their messengers.
40     private final Map<Messenger, AsyncChannel> mClients = new HashMap<>();
41
42     private Messenger mMessenger;
43
44     public BidirectionalAsyncChannelServer(final Context context, final Looper looper,
45             final Handler messageHandler) {
46         Handler handler = new Handler(looper) {
47             @Override
48             public void handleMessage(Message msg) {
49                 AsyncChannel channel = mClients.get(msg.replyTo);
50                 switch (msg.what) {
51                     case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION:
52                         if (channel != null) {
53                             Log.d(TAG, "duplicate client connection: " + msg.sendingUid);
54                             channel.replyToMessage(msg,
55                                     AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED,
56                                     AsyncChannel.STATUS_FULL_CONNECTION_REFUSED_ALREADY_CONNECTED);
57                         } else {
58                             channel = new AsyncChannel();
59                             mClients.put(msg.replyTo, channel);
60                             channel.connected(context, this, msg.replyTo);
61                             channel.replyToMessage(msg, AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED,
62                                     AsyncChannel.STATUS_SUCCESSFUL);
63                         }
64                         break;
65                     case AsyncChannel.CMD_CHANNEL_DISCONNECT:
66                         channel.disconnect();
67                         break;
68
69                     case AsyncChannel.CMD_CHANNEL_DISCONNECTED:
70                         mClients.remove(msg.replyTo);
71                         break;
72
73                     default:
74                         messageHandler.handleMessage(msg);
75                         break;
76                 }
77             }
78         };
79         mMessenger = new Messenger(handler);
80     }
81
82     public Messenger getMessenger() {
83         return mMessenger;
84     }
85
86 }