blob: 956078772ca85d25937a68eb9e42fd44f7033b7b [file] [log] [blame]
Rahul Ravikumar05336002019-10-14 15:04:32 -07001/*
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 android.annotation.UnsupportedAppUsage;
20import org.apache.harmony.dalvik.ddmc.Chunk;
21import org.apache.harmony.dalvik.ddmc.ChunkHandler;
22import org.apache.harmony.dalvik.ddmc.DdmServer;
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 @UnsupportedAppUsage
74 public static void setAppName(String name, int userId) {
75 if (name == null || name.length() == 0)
76 return;
77
78 mAppName = name;
79
80 // if DDMS is already connected, send the app name up
81 sendAPNM(name, userId);
82 }
83
84 @UnsupportedAppUsage
85 public static String getAppName() {
86 return mAppName;
87 }
88
89 /*
90 * Send an APNM (APplication NaMe) chunk.
91 */
92 private static void sendAPNM(String appName, int userId) {
93 if (false)
94 Log.v("ddm", "Sending app name");
95
96 ByteBuffer out = ByteBuffer.allocate(
97 4 /* appName's length */
98 + appName.length()*2 /* appName */
99 + 4 /* userId */);
100 out.order(ChunkHandler.CHUNK_ORDER);
101 out.putInt(appName.length());
102 putString(out, appName);
103 out.putInt(userId);
104
105 Chunk chunk = new Chunk(CHUNK_APNM, out);
106 DdmServer.sendChunk(chunk);
107 }
108
109}
110