DdmHandleAppName.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
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
17package android.ddm;
18
19import org.apache.harmony.dalvik.ddmc.Chunk;
20import org.apache.harmony.dalvik.ddmc.ChunkHandler;
21import org.apache.harmony.dalvik.ddmc.DdmServer;
22import android.util.Config;
23import android.util.Log;
24import java.nio.ByteBuffer;
25
26
27/**
28 * Track our app name.  We don't (currently) handle any inbound packets.
29 */
30public class DdmHandleAppName extends ChunkHandler {
31
32    public static final int CHUNK_APNM = type("APNM");
33
34    private volatile static String mAppName = "";
35
36    private static DdmHandleAppName mInstance = new DdmHandleAppName();
37
38
39    /* singleton, do not instantiate */
40    private DdmHandleAppName() {}
41
42    /**
43     * Register for the messages we're interested in.
44     */
45    public static void register() {}
46
47    /**
48     * Called when the DDM server connects.  The handler is allowed to
49     * send messages to the server.
50     */
51    public void connected() {}
52
53    /**
54     * Called when the DDM server disconnects.  Can be used to disable
55     * periodic transmissions or clean up saved state.
56     */
57    public void disconnected() {}
58
59    /**
60     * Handle a chunk of data.
61     */
62    public Chunk handleChunk(Chunk request) {
63        return null;
64    }
65
66
67
68    /**
69     * Set the application name.  Called when we get named, which may be
70     * before or after DDMS connects.  For the latter we need to send up
71     * an APNM message.
72     */
73    public static void setAppName(String name) {
74        if (name == null || name.length() == 0)
75            return;
76
77        mAppName = name;
78
79        // if DDMS is already connected, send the app name up
80        sendAPNM(name);
81    }
82
83    public static String getAppName() {
84        return mAppName;
85    }
86
87    /*
88     * Send an APNM (APplication NaMe) chunk.
89     */
90    private static void sendAPNM(String appName) {
91        if (Config.LOGV)
92            Log.v("ddm", "Sending app name");
93
94        ByteBuffer out = ByteBuffer.allocate(4 + appName.length()*2);
95        out.order(ChunkHandler.CHUNK_ORDER);
96        out.putInt(appName.length());
97        putString(out, appName);
98
99        Chunk chunk = new Chunk(CHUNK_APNM, out);
100        DdmServer.sendChunk(chunk);
101    }
102
103}
104
105