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