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 static volatile Names sNames = new Names("", "");
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      * Sets all names to the same name.
72      */
73     @UnsupportedAppUsage
setAppName(String name, int userId)74     public static void setAppName(String name, int userId) {
75         setAppName(name, name, userId);
76     }
77 
78     /**
79      * Set the application name.  Called when we get named, which may be
80      * before or after DDMS connects.  For the latter we need to send up
81      * an APNM message.
82      */
83     @UnsupportedAppUsage
setAppName(String appName, String pkgName, int userId)84     public static void setAppName(String appName, String pkgName, int userId) {
85         if (appName == null || appName.isEmpty() || pkgName == null || pkgName.isEmpty()) return;
86 
87         sNames = new Names(appName, pkgName);
88 
89         // if DDMS is already connected, send the app name up
90         sendAPNM(appName, pkgName, userId);
91     }
92 
93     @UnsupportedAppUsage
getNames()94     public static Names getNames() {
95         return sNames;
96     }
97 
98     /**
99      * Send an APNM (APplication NaMe) chunk.
100      */
sendAPNM(String appName, String pkgName, int userId)101     private static void sendAPNM(String appName, String pkgName, int userId) {
102         if (false)
103             Log.v("ddm", "Sending app name");
104 
105         ByteBuffer out = ByteBuffer.allocate(
106                             4 /* appName's length */
107                             + appName.length() * 2 /* appName */
108                             + 4 /* userId */
109                             + 4 /* pkgName's length */
110                             + pkgName.length() * 2 /* pkgName */);
111         out.order(ChunkHandler.CHUNK_ORDER);
112         out.putInt(appName.length());
113         putString(out, appName);
114         out.putInt(userId);
115         out.putInt(pkgName.length());
116         putString(out, pkgName);
117 
118         Chunk chunk = new Chunk(CHUNK_APNM, out);
119         DdmServer.sendChunk(chunk);
120     }
121 
122     /**
123      * A class that encapsulates the app and package names into a single
124      * instance, effectively synchronizing the two names.
125      */
126     static final class Names {
127 
128         private final String mAppName;
129 
130         private final String mPkgName;
131 
Names(String appName, String pkgName)132         private Names(String appName, String pkgName) {
133             mAppName = appName;
134             mPkgName = pkgName;
135         }
136 
getAppName()137         public String getAppName() {
138             return mAppName;
139         }
140 
getPkgName()141         public String getPkgName() {
142             return mPkgName;
143         }
144 
145     }
146 
147 }
148 
149