OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / sdk / ddms / libs / ddmlib / src / com / android / ddmlib / HandleAppName.java
1 /*
2  * Copyright (C) 2007 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.ddmlib;
18
19 import java.io.IOException;
20 import java.nio.ByteBuffer;
21
22 /**
23  * Handle the "app name" chunk (APNM).
24  */
25 final class HandleAppName extends ChunkHandler {
26
27     public static final int CHUNK_APNM = ChunkHandler.type("APNM");
28
29     private static final HandleAppName mInst = new HandleAppName();
30
31
32     private HandleAppName() {}
33
34     /**
35      * Register for the packets we expect to get from the client.
36      */
37     public static void register(MonitorThread mt) {
38         mt.registerChunkHandler(CHUNK_APNM, mInst);
39     }
40
41     /**
42      * Client is ready.
43      */
44     @Override
45     public void clientReady(Client client) throws IOException {}
46
47     /**
48      * Client went away.
49      */
50     @Override
51     public void clientDisconnected(Client client) {}
52
53     /**
54      * Chunk handler entry point.
55      */
56     @Override
57     public void handleChunk(Client client, int type, ByteBuffer data,
58             boolean isReply, int msgId) {
59
60         Log.d("ddm-appname", "handling " + ChunkHandler.name(type));
61
62         if (type == CHUNK_APNM) {
63             assert !isReply;
64             handleAPNM(client, data);
65         } else {
66             handleUnknownChunk(client, type, data, isReply, msgId);
67         }
68     }
69
70     /*
71      * Handle a reply to our APNM message.
72      */
73     private static void handleAPNM(Client client, ByteBuffer data) {
74         int appNameLen;
75         String appName;
76
77         appNameLen = data.getInt();
78         appName = getString(data, appNameLen);
79
80         Log.d("ddm-appname", "APNM: app='" + appName + "'");
81
82         ClientData cd = client.getClientData();
83         synchronized (cd) {
84             cd.setClientDescription(appName);
85         }
86
87         client = checkDebuggerPortForAppName(client, appName);
88
89         if (client != null) {
90             client.update(Client.CHANGE_NAME);
91         }
92     }
93  }
94