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